xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 110d3047a3ec033de00322b1a8068b1215efa97a)
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_list *ha_list;
111 	struct netdev_hw_addr *ha;
112 
113 	if (!f || !netdev)
114 		return;
115 
116 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
117 		ha_list = &netdev->uc;
118 	else
119 		ha_list = &netdev->mc;
120 
121 	netdev_hw_addr_list_for_each(ha, ha_list) {
122 		if (ether_addr_equal(ha->addr, f->macaddr)) {
123 			ha->refcount += delta;
124 			if (ha->refcount <= 0)
125 				ha->refcount = 1;
126 			break;
127 		}
128 	}
129 }
130 
131 /**
132  * i40e_hw_to_dev - get device pointer from the hardware structure
133  * @hw: pointer to the device HW structure
134  **/
135 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
136 {
137 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
138 
139 	return &pf->pdev->dev;
140 }
141 
142 /**
143  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to fill out
146  * @size: size of memory requested
147  * @alignment: what to align the allocation to
148  **/
149 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
150 			  u64 size, u32 alignment)
151 {
152 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
153 
154 	mem->size = ALIGN(size, alignment);
155 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
156 				     GFP_KERNEL);
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_dma_mem - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
168 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
169 {
170 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
171 
172 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
173 	mem->va = NULL;
174 	mem->pa = 0;
175 	mem->size = 0;
176 
177 	return 0;
178 }
179 
180 /**
181  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
182  * @hw:   pointer to the HW structure
183  * @mem:  ptr to mem struct to fill out
184  * @size: size of memory requested
185  **/
186 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
187 			   u32 size)
188 {
189 	mem->size = size;
190 	mem->va = kzalloc(size, GFP_KERNEL);
191 
192 	if (!mem->va)
193 		return -ENOMEM;
194 
195 	return 0;
196 }
197 
198 /**
199  * i40e_free_virt_mem - OS specific memory free for shared code
200  * @hw:   pointer to the HW structure
201  * @mem:  ptr to mem struct to free
202  **/
203 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
204 {
205 	/* it's ok to kfree a NULL pointer */
206 	kfree(mem->va);
207 	mem->va = NULL;
208 	mem->size = 0;
209 
210 	return 0;
211 }
212 
213 /**
214  * i40e_get_lump - find a lump of free generic resource
215  * @pf: board private structure
216  * @pile: the pile of resource to search
217  * @needed: the number of items needed
218  * @id: an owner id to stick on the items assigned
219  *
220  * Returns the base item index of the lump, or negative for error
221  **/
222 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
223 			 u16 needed, u16 id)
224 {
225 	int ret = -ENOMEM;
226 	int i, j;
227 
228 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
229 		dev_info(&pf->pdev->dev,
230 			 "param err: pile=%s needed=%d id=0x%04x\n",
231 			 pile ? "<valid>" : "<null>", needed, id);
232 		return -EINVAL;
233 	}
234 
235 	/* Allocate last queue in the pile for FDIR VSI queue
236 	 * so it doesn't fragment the qp_pile
237 	 */
238 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
239 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
240 			dev_err(&pf->pdev->dev,
241 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
242 				pile->num_entries - 1);
243 			return -ENOMEM;
244 		}
245 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
246 		return pile->num_entries - 1;
247 	}
248 
249 	i = 0;
250 	while (i < pile->num_entries) {
251 		/* skip already allocated entries */
252 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
253 			i++;
254 			continue;
255 		}
256 
257 		/* do we have enough in this lump? */
258 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
259 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
260 				break;
261 		}
262 
263 		if (j == needed) {
264 			/* there was enough, so assign it to the requestor */
265 			for (j = 0; j < needed; j++)
266 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
267 			ret = i;
268 			break;
269 		}
270 
271 		/* not enough, so skip over it and continue looking */
272 		i += j;
273 	}
274 
275 	return ret;
276 }
277 
278 /**
279  * i40e_put_lump - return a lump of generic resource
280  * @pile: the pile of resource to search
281  * @index: the base item index
282  * @id: the owner id of the items assigned
283  *
284  * Returns the count of items in the lump
285  **/
286 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
287 {
288 	int valid_id = (id | I40E_PILE_VALID_BIT);
289 	int count = 0;
290 	u16 i;
291 
292 	if (!pile || index >= pile->num_entries)
293 		return -EINVAL;
294 
295 	for (i = index;
296 	     i < pile->num_entries && pile->list[i] == valid_id;
297 	     i++) {
298 		pile->list[i] = 0;
299 		count++;
300 	}
301 
302 
303 	return count;
304 }
305 
306 /**
307  * i40e_find_vsi_from_id - searches for the vsi with the given id
308  * @pf: the pf structure to search for the vsi
309  * @id: id of the vsi it is searching for
310  **/
311 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
312 {
313 	int i;
314 
315 	for (i = 0; i < pf->num_alloc_vsi; i++)
316 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
317 			return pf->vsi[i];
318 
319 	return NULL;
320 }
321 
322 /**
323  * i40e_service_event_schedule - Schedule the service task to wake up
324  * @pf: board private structure
325  *
326  * If not already scheduled, this puts the task into the work queue
327  **/
328 void i40e_service_event_schedule(struct i40e_pf *pf)
329 {
330 	if ((!test_bit(__I40E_DOWN, pf->state) &&
331 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
332 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
333 		queue_work(i40e_wq, &pf->service_task);
334 }
335 
336 /**
337  * i40e_tx_timeout - Respond to a Tx Hang
338  * @netdev: network interface device structure
339  * @txqueue: queue number timing out
340  *
341  * If any port has noticed a Tx timeout, it is likely that the whole
342  * device is munged, not just the one netdev port, so go for the full
343  * reset.
344  **/
345 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
346 {
347 	struct i40e_netdev_priv *np = netdev_priv(netdev);
348 	struct i40e_vsi *vsi = np->vsi;
349 	struct i40e_pf *pf = vsi->back;
350 	struct i40e_ring *tx_ring = NULL;
351 	unsigned int i;
352 	u32 head, val;
353 
354 	pf->tx_timeout_count++;
355 
356 	/* with txqueue index, find the tx_ring struct */
357 	for (i = 0; i < vsi->num_queue_pairs; i++) {
358 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
359 			if (txqueue ==
360 			    vsi->tx_rings[i]->queue_index) {
361 				tx_ring = vsi->tx_rings[i];
362 				break;
363 			}
364 		}
365 	}
366 
367 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
368 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
369 	else if (time_before(jiffies,
370 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
371 		return;   /* don't do any new action before the next timeout */
372 
373 	/* don't kick off another recovery if one is already pending */
374 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
375 		return;
376 
377 	if (tx_ring) {
378 		head = i40e_get_head(tx_ring);
379 		/* Read interrupt register */
380 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
381 			val = rd32(&pf->hw,
382 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
383 						tx_ring->vsi->base_vector - 1));
384 		else
385 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
386 
387 		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",
388 			    vsi->seid, txqueue, tx_ring->next_to_clean,
389 			    head, tx_ring->next_to_use,
390 			    readl(tx_ring->tail), val);
391 	}
392 
393 	pf->tx_timeout_last_recovery = jiffies;
394 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
395 		    pf->tx_timeout_recovery_level, txqueue);
396 
397 	switch (pf->tx_timeout_recovery_level) {
398 	case 1:
399 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
400 		break;
401 	case 2:
402 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
403 		break;
404 	case 3:
405 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
406 		break;
407 	default:
408 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
409 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
410 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
411 		break;
412 	}
413 
414 	i40e_service_event_schedule(pf);
415 	pf->tx_timeout_recovery_level++;
416 }
417 
418 /**
419  * i40e_get_vsi_stats_struct - Get System Network Statistics
420  * @vsi: the VSI we care about
421  *
422  * Returns the address of the device statistics structure.
423  * The statistics are actually updated from the service task.
424  **/
425 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
426 {
427 	return &vsi->net_stats;
428 }
429 
430 /**
431  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
432  * @ring: Tx ring to get statistics from
433  * @stats: statistics entry to be updated
434  **/
435 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
436 					    struct rtnl_link_stats64 *stats)
437 {
438 	u64 bytes, packets;
439 	unsigned int start;
440 
441 	do {
442 		start = u64_stats_fetch_begin(&ring->syncp);
443 		packets = ring->stats.packets;
444 		bytes   = ring->stats.bytes;
445 	} while (u64_stats_fetch_retry(&ring->syncp, start));
446 
447 	stats->tx_packets += packets;
448 	stats->tx_bytes   += bytes;
449 }
450 
451 /**
452  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
453  * @netdev: network interface device structure
454  * @stats: data structure to store statistics
455  *
456  * Returns the address of the device statistics structure.
457  * The statistics are actually updated from the service task.
458  **/
459 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
460 				  struct rtnl_link_stats64 *stats)
461 {
462 	struct i40e_netdev_priv *np = netdev_priv(netdev);
463 	struct i40e_vsi *vsi = np->vsi;
464 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
465 	struct i40e_ring *ring;
466 	int i;
467 
468 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
469 		return;
470 
471 	if (!vsi->tx_rings)
472 		return;
473 
474 	rcu_read_lock();
475 	for (i = 0; i < vsi->num_queue_pairs; i++) {
476 		u64 bytes, packets;
477 		unsigned int start;
478 
479 		ring = READ_ONCE(vsi->tx_rings[i]);
480 		if (!ring)
481 			continue;
482 		i40e_get_netdev_stats_struct_tx(ring, stats);
483 
484 		if (i40e_enabled_xdp_vsi(vsi)) {
485 			ring = READ_ONCE(vsi->xdp_rings[i]);
486 			if (!ring)
487 				continue;
488 			i40e_get_netdev_stats_struct_tx(ring, stats);
489 		}
490 
491 		ring = READ_ONCE(vsi->rx_rings[i]);
492 		if (!ring)
493 			continue;
494 		do {
495 			start   = u64_stats_fetch_begin(&ring->syncp);
496 			packets = ring->stats.packets;
497 			bytes   = ring->stats.bytes;
498 		} while (u64_stats_fetch_retry(&ring->syncp, start));
499 
500 		stats->rx_packets += packets;
501 		stats->rx_bytes   += bytes;
502 
503 	}
504 	rcu_read_unlock();
505 
506 	/* following stats updated by i40e_watchdog_subtask() */
507 	stats->multicast	= vsi_stats->multicast;
508 	stats->tx_errors	= vsi_stats->tx_errors;
509 	stats->tx_dropped	= vsi_stats->tx_dropped;
510 	stats->rx_errors	= vsi_stats->rx_errors;
511 	stats->rx_dropped	= vsi_stats->rx_dropped;
512 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
513 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
514 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
515 }
516 
517 /**
518  * i40e_vsi_reset_stats - Resets all stats of the given vsi
519  * @vsi: the VSI to have its stats reset
520  **/
521 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
522 {
523 	struct rtnl_link_stats64 *ns;
524 	int i;
525 
526 	if (!vsi)
527 		return;
528 
529 	ns = i40e_get_vsi_stats_struct(vsi);
530 	memset(ns, 0, sizeof(*ns));
531 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
532 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
533 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
534 	if (vsi->rx_rings && vsi->rx_rings[0]) {
535 		for (i = 0; i < vsi->num_queue_pairs; i++) {
536 			memset(&vsi->rx_rings[i]->stats, 0,
537 			       sizeof(vsi->rx_rings[i]->stats));
538 			memset(&vsi->rx_rings[i]->rx_stats, 0,
539 			       sizeof(vsi->rx_rings[i]->rx_stats));
540 			memset(&vsi->tx_rings[i]->stats, 0,
541 			       sizeof(vsi->tx_rings[i]->stats));
542 			memset(&vsi->tx_rings[i]->tx_stats, 0,
543 			       sizeof(vsi->tx_rings[i]->tx_stats));
544 		}
545 	}
546 	vsi->stat_offsets_loaded = false;
547 }
548 
549 /**
550  * i40e_pf_reset_stats - Reset all of the stats for the given PF
551  * @pf: the PF to be reset
552  **/
553 void i40e_pf_reset_stats(struct i40e_pf *pf)
554 {
555 	int i;
556 
557 	memset(&pf->stats, 0, sizeof(pf->stats));
558 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
559 	pf->stat_offsets_loaded = false;
560 
561 	for (i = 0; i < I40E_MAX_VEB; i++) {
562 		if (pf->veb[i]) {
563 			memset(&pf->veb[i]->stats, 0,
564 			       sizeof(pf->veb[i]->stats));
565 			memset(&pf->veb[i]->stats_offsets, 0,
566 			       sizeof(pf->veb[i]->stats_offsets));
567 			memset(&pf->veb[i]->tc_stats, 0,
568 			       sizeof(pf->veb[i]->tc_stats));
569 			memset(&pf->veb[i]->tc_stats_offsets, 0,
570 			       sizeof(pf->veb[i]->tc_stats_offsets));
571 			pf->veb[i]->stat_offsets_loaded = false;
572 		}
573 	}
574 	pf->hw_csum_rx_error = 0;
575 }
576 
577 /**
578  * i40e_compute_pci_to_hw_id - compute index form PCI function.
579  * @vsi: ptr to the VSI to read from.
580  * @hw: ptr to the hardware info.
581  **/
582 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
583 {
584 	int pf_count = i40e_get_pf_count(hw);
585 
586 	if (vsi->type == I40E_VSI_SRIOV)
587 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
588 
589 	return hw->port + BIT(7);
590 }
591 
592 /**
593  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
594  * @hw: ptr to the hardware info.
595  * @hireg: the high 32 bit reg to read.
596  * @loreg: the low 32 bit reg to read.
597  * @offset_loaded: has the initial offset been loaded yet.
598  * @offset: ptr to current offset value.
599  * @stat: ptr to the stat.
600  *
601  * Since the device stats are not reset at PFReset, they will not
602  * be zeroed when the driver starts.  We'll save the first values read
603  * and use them as offsets to be subtracted from the raw values in order
604  * to report stats that count from zero.
605  **/
606 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
607 			       bool offset_loaded, u64 *offset, u64 *stat)
608 {
609 	u64 new_data;
610 
611 	new_data = rd64(hw, loreg);
612 
613 	if (!offset_loaded || new_data < *offset)
614 		*offset = new_data;
615 	*stat = new_data - *offset;
616 }
617 
618 /**
619  * i40e_stat_update48 - read and update a 48 bit stat from the chip
620  * @hw: ptr to the hardware info
621  * @hireg: the high 32 bit reg to read
622  * @loreg: the low 32 bit reg to read
623  * @offset_loaded: has the initial offset been loaded yet
624  * @offset: ptr to current offset value
625  * @stat: ptr to the stat
626  *
627  * Since the device stats are not reset at PFReset, they likely will not
628  * be zeroed when the driver starts.  We'll save the first values read
629  * and use them as offsets to be subtracted from the raw values in order
630  * to report stats that count from zero.  In the process, we also manage
631  * the potential roll-over.
632  **/
633 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
634 			       bool offset_loaded, u64 *offset, u64 *stat)
635 {
636 	u64 new_data;
637 
638 	if (hw->device_id == I40E_DEV_ID_QEMU) {
639 		new_data = rd32(hw, loreg);
640 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
641 	} else {
642 		new_data = rd64(hw, loreg);
643 	}
644 	if (!offset_loaded)
645 		*offset = new_data;
646 	if (likely(new_data >= *offset))
647 		*stat = new_data - *offset;
648 	else
649 		*stat = (new_data + BIT_ULL(48)) - *offset;
650 	*stat &= 0xFFFFFFFFFFFFULL;
651 }
652 
653 /**
654  * i40e_stat_update32 - read and update a 32 bit stat from the chip
655  * @hw: ptr to the hardware info
656  * @reg: the hw reg to read
657  * @offset_loaded: has the initial offset been loaded yet
658  * @offset: ptr to current offset value
659  * @stat: ptr to the stat
660  **/
661 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
662 			       bool offset_loaded, u64 *offset, u64 *stat)
663 {
664 	u32 new_data;
665 
666 	new_data = rd32(hw, reg);
667 	if (!offset_loaded)
668 		*offset = new_data;
669 	if (likely(new_data >= *offset))
670 		*stat = (u32)(new_data - *offset);
671 	else
672 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
673 }
674 
675 /**
676  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
677  * @hw: ptr to the hardware info
678  * @reg: the hw reg to read and clear
679  * @stat: ptr to the stat
680  **/
681 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
682 {
683 	u32 new_data = rd32(hw, reg);
684 
685 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
686 	*stat += new_data;
687 }
688 
689 /**
690  * i40e_stats_update_rx_discards - update rx_discards.
691  * @vsi: ptr to the VSI to be updated.
692  * @hw: ptr to the hardware info.
693  * @stat_idx: VSI's stat_counter_idx.
694  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
695  * @stat_offset: ptr to stat_offset to store first read of specific register.
696  * @stat: ptr to VSI's stat to be updated.
697  **/
698 static void
699 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
700 			      int stat_idx, bool offset_loaded,
701 			      struct i40e_eth_stats *stat_offset,
702 			      struct i40e_eth_stats *stat)
703 {
704 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
705 			   &stat_offset->rx_discards, &stat->rx_discards);
706 	i40e_stat_update64(hw,
707 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
708 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
709 			   offset_loaded, &stat_offset->rx_discards_other,
710 			   &stat->rx_discards_other);
711 }
712 
713 /**
714  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
715  * @vsi: the VSI to be updated
716  **/
717 void i40e_update_eth_stats(struct i40e_vsi *vsi)
718 {
719 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
720 	struct i40e_pf *pf = vsi->back;
721 	struct i40e_hw *hw = &pf->hw;
722 	struct i40e_eth_stats *oes;
723 	struct i40e_eth_stats *es;     /* device's eth stats */
724 
725 	es = &vsi->eth_stats;
726 	oes = &vsi->eth_stats_offsets;
727 
728 	/* Gather up the stats that the hw collects */
729 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
730 			   vsi->stat_offsets_loaded,
731 			   &oes->tx_errors, &es->tx_errors);
732 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
733 			   vsi->stat_offsets_loaded,
734 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
735 
736 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
737 			   I40E_GLV_GORCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->rx_bytes, &es->rx_bytes);
740 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
741 			   I40E_GLV_UPRCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->rx_unicast, &es->rx_unicast);
744 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
745 			   I40E_GLV_MPRCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->rx_multicast, &es->rx_multicast);
748 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
749 			   I40E_GLV_BPRCL(stat_idx),
750 			   vsi->stat_offsets_loaded,
751 			   &oes->rx_broadcast, &es->rx_broadcast);
752 
753 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
754 			   I40E_GLV_GOTCL(stat_idx),
755 			   vsi->stat_offsets_loaded,
756 			   &oes->tx_bytes, &es->tx_bytes);
757 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
758 			   I40E_GLV_UPTCL(stat_idx),
759 			   vsi->stat_offsets_loaded,
760 			   &oes->tx_unicast, &es->tx_unicast);
761 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
762 			   I40E_GLV_MPTCL(stat_idx),
763 			   vsi->stat_offsets_loaded,
764 			   &oes->tx_multicast, &es->tx_multicast);
765 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
766 			   I40E_GLV_BPTCL(stat_idx),
767 			   vsi->stat_offsets_loaded,
768 			   &oes->tx_broadcast, &es->tx_broadcast);
769 
770 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
771 				      vsi->stat_offsets_loaded, oes, es);
772 
773 	vsi->stat_offsets_loaded = true;
774 }
775 
776 /**
777  * i40e_update_veb_stats - Update Switch component statistics
778  * @veb: the VEB being updated
779  **/
780 void i40e_update_veb_stats(struct i40e_veb *veb)
781 {
782 	struct i40e_pf *pf = veb->pf;
783 	struct i40e_hw *hw = &pf->hw;
784 	struct i40e_eth_stats *oes;
785 	struct i40e_eth_stats *es;     /* device's eth stats */
786 	struct i40e_veb_tc_stats *veb_oes;
787 	struct i40e_veb_tc_stats *veb_es;
788 	int i, idx = 0;
789 
790 	idx = veb->stats_idx;
791 	es = &veb->stats;
792 	oes = &veb->stats_offsets;
793 	veb_es = &veb->tc_stats;
794 	veb_oes = &veb->tc_stats_offsets;
795 
796 	/* Gather up the stats that the hw collects */
797 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->tx_discards, &es->tx_discards);
800 	if (hw->revision_id > 0)
801 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
802 				   veb->stat_offsets_loaded,
803 				   &oes->rx_unknown_protocol,
804 				   &es->rx_unknown_protocol);
805 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
806 			   veb->stat_offsets_loaded,
807 			   &oes->rx_bytes, &es->rx_bytes);
808 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
809 			   veb->stat_offsets_loaded,
810 			   &oes->rx_unicast, &es->rx_unicast);
811 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
812 			   veb->stat_offsets_loaded,
813 			   &oes->rx_multicast, &es->rx_multicast);
814 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
815 			   veb->stat_offsets_loaded,
816 			   &oes->rx_broadcast, &es->rx_broadcast);
817 
818 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
819 			   veb->stat_offsets_loaded,
820 			   &oes->tx_bytes, &es->tx_bytes);
821 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
822 			   veb->stat_offsets_loaded,
823 			   &oes->tx_unicast, &es->tx_unicast);
824 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
825 			   veb->stat_offsets_loaded,
826 			   &oes->tx_multicast, &es->tx_multicast);
827 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
828 			   veb->stat_offsets_loaded,
829 			   &oes->tx_broadcast, &es->tx_broadcast);
830 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
831 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
832 				   I40E_GLVEBTC_RPCL(i, idx),
833 				   veb->stat_offsets_loaded,
834 				   &veb_oes->tc_rx_packets[i],
835 				   &veb_es->tc_rx_packets[i]);
836 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
837 				   I40E_GLVEBTC_RBCL(i, idx),
838 				   veb->stat_offsets_loaded,
839 				   &veb_oes->tc_rx_bytes[i],
840 				   &veb_es->tc_rx_bytes[i]);
841 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
842 				   I40E_GLVEBTC_TPCL(i, idx),
843 				   veb->stat_offsets_loaded,
844 				   &veb_oes->tc_tx_packets[i],
845 				   &veb_es->tc_tx_packets[i]);
846 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
847 				   I40E_GLVEBTC_TBCL(i, idx),
848 				   veb->stat_offsets_loaded,
849 				   &veb_oes->tc_tx_bytes[i],
850 				   &veb_es->tc_tx_bytes[i]);
851 	}
852 	veb->stat_offsets_loaded = true;
853 }
854 
855 /**
856  * i40e_update_vsi_stats - Update the vsi statistics counters.
857  * @vsi: the VSI to be updated
858  *
859  * There are a few instances where we store the same stat in a
860  * couple of different structs.  This is partly because we have
861  * the netdev stats that need to be filled out, which is slightly
862  * different from the "eth_stats" defined by the chip and used in
863  * VF communications.  We sort it out here.
864  **/
865 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
866 {
867 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
868 	struct i40e_pf *pf = vsi->back;
869 	struct rtnl_link_stats64 *ons;
870 	struct rtnl_link_stats64 *ns;   /* netdev stats */
871 	struct i40e_eth_stats *oes;
872 	struct i40e_eth_stats *es;     /* device's eth stats */
873 	u64 tx_restart, tx_busy;
874 	struct i40e_ring *p;
875 	u64 bytes, packets;
876 	unsigned int start;
877 	u64 tx_linearize;
878 	u64 tx_force_wb;
879 	u64 tx_stopped;
880 	u64 rx_p, rx_b;
881 	u64 tx_p, tx_b;
882 	u16 q;
883 
884 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
885 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
886 		return;
887 
888 	ns = i40e_get_vsi_stats_struct(vsi);
889 	ons = &vsi->net_stats_offsets;
890 	es = &vsi->eth_stats;
891 	oes = &vsi->eth_stats_offsets;
892 
893 	/* Gather up the netdev and vsi stats that the driver collects
894 	 * on the fly during packet processing
895 	 */
896 	rx_b = rx_p = 0;
897 	tx_b = tx_p = 0;
898 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
899 	tx_stopped = 0;
900 	rx_page = 0;
901 	rx_buf = 0;
902 	rx_reuse = 0;
903 	rx_alloc = 0;
904 	rx_waive = 0;
905 	rx_busy = 0;
906 	rcu_read_lock();
907 	for (q = 0; q < vsi->num_queue_pairs; q++) {
908 		/* locate Tx ring */
909 		p = READ_ONCE(vsi->tx_rings[q]);
910 		if (!p)
911 			continue;
912 
913 		do {
914 			start = u64_stats_fetch_begin(&p->syncp);
915 			packets = p->stats.packets;
916 			bytes = p->stats.bytes;
917 		} while (u64_stats_fetch_retry(&p->syncp, start));
918 		tx_b += bytes;
919 		tx_p += packets;
920 		tx_restart += p->tx_stats.restart_queue;
921 		tx_busy += p->tx_stats.tx_busy;
922 		tx_linearize += p->tx_stats.tx_linearize;
923 		tx_force_wb += p->tx_stats.tx_force_wb;
924 		tx_stopped += p->tx_stats.tx_stopped;
925 
926 		/* locate Rx ring */
927 		p = READ_ONCE(vsi->rx_rings[q]);
928 		if (!p)
929 			continue;
930 
931 		do {
932 			start = u64_stats_fetch_begin(&p->syncp);
933 			packets = p->stats.packets;
934 			bytes = p->stats.bytes;
935 		} while (u64_stats_fetch_retry(&p->syncp, start));
936 		rx_b += bytes;
937 		rx_p += packets;
938 		rx_buf += p->rx_stats.alloc_buff_failed;
939 		rx_page += p->rx_stats.alloc_page_failed;
940 		rx_reuse += p->rx_stats.page_reuse_count;
941 		rx_alloc += p->rx_stats.page_alloc_count;
942 		rx_waive += p->rx_stats.page_waive_count;
943 		rx_busy += p->rx_stats.page_busy_count;
944 
945 		if (i40e_enabled_xdp_vsi(vsi)) {
946 			/* locate XDP ring */
947 			p = READ_ONCE(vsi->xdp_rings[q]);
948 			if (!p)
949 				continue;
950 
951 			do {
952 				start = u64_stats_fetch_begin(&p->syncp);
953 				packets = p->stats.packets;
954 				bytes = p->stats.bytes;
955 			} while (u64_stats_fetch_retry(&p->syncp, start));
956 			tx_b += bytes;
957 			tx_p += packets;
958 			tx_restart += p->tx_stats.restart_queue;
959 			tx_busy += p->tx_stats.tx_busy;
960 			tx_linearize += p->tx_stats.tx_linearize;
961 			tx_force_wb += p->tx_stats.tx_force_wb;
962 		}
963 	}
964 	rcu_read_unlock();
965 	vsi->tx_restart = tx_restart;
966 	vsi->tx_busy = tx_busy;
967 	vsi->tx_linearize = tx_linearize;
968 	vsi->tx_force_wb = tx_force_wb;
969 	vsi->tx_stopped = tx_stopped;
970 	vsi->rx_page_failed = rx_page;
971 	vsi->rx_buf_failed = rx_buf;
972 	vsi->rx_page_reuse = rx_reuse;
973 	vsi->rx_page_alloc = rx_alloc;
974 	vsi->rx_page_waive = rx_waive;
975 	vsi->rx_page_busy = rx_busy;
976 
977 	ns->rx_packets = rx_p;
978 	ns->rx_bytes = rx_b;
979 	ns->tx_packets = tx_p;
980 	ns->tx_bytes = tx_b;
981 
982 	/* update netdev stats from eth stats */
983 	i40e_update_eth_stats(vsi);
984 	ons->tx_errors = oes->tx_errors;
985 	ns->tx_errors = es->tx_errors;
986 	ons->multicast = oes->rx_multicast;
987 	ns->multicast = es->rx_multicast;
988 	ons->rx_dropped = oes->rx_discards_other;
989 	ns->rx_dropped = es->rx_discards_other;
990 	ons->rx_missed_errors = oes->rx_discards;
991 	ns->rx_missed_errors = es->rx_discards;
992 	ons->tx_dropped = oes->tx_discards;
993 	ns->tx_dropped = es->tx_discards;
994 
995 	/* pull in a couple PF stats if this is the main vsi */
996 	if (vsi == pf->vsi[pf->lan_vsi]) {
997 		ns->rx_crc_errors = pf->stats.crc_errors;
998 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
999 		ns->rx_length_errors = pf->stats.rx_length_errors;
1000 	}
1001 }
1002 
1003 /**
1004  * i40e_update_pf_stats - Update the PF statistics counters.
1005  * @pf: the PF to be updated
1006  **/
1007 static void i40e_update_pf_stats(struct i40e_pf *pf)
1008 {
1009 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1010 	struct i40e_hw_port_stats *nsd = &pf->stats;
1011 	struct i40e_hw *hw = &pf->hw;
1012 	u32 val;
1013 	int i;
1014 
1015 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1016 			   I40E_GLPRT_GORCL(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1019 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1020 			   I40E_GLPRT_GOTCL(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1023 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1024 			   pf->stat_offsets_loaded,
1025 			   &osd->eth.rx_discards,
1026 			   &nsd->eth.rx_discards);
1027 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1028 			   I40E_GLPRT_UPRCL(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->eth.rx_unicast,
1031 			   &nsd->eth.rx_unicast);
1032 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1033 			   I40E_GLPRT_MPRCL(hw->port),
1034 			   pf->stat_offsets_loaded,
1035 			   &osd->eth.rx_multicast,
1036 			   &nsd->eth.rx_multicast);
1037 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1038 			   I40E_GLPRT_BPRCL(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->eth.rx_broadcast,
1041 			   &nsd->eth.rx_broadcast);
1042 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1043 			   I40E_GLPRT_UPTCL(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->eth.tx_unicast,
1046 			   &nsd->eth.tx_unicast);
1047 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1048 			   I40E_GLPRT_MPTCL(hw->port),
1049 			   pf->stat_offsets_loaded,
1050 			   &osd->eth.tx_multicast,
1051 			   &nsd->eth.tx_multicast);
1052 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1053 			   I40E_GLPRT_BPTCL(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->eth.tx_broadcast,
1056 			   &nsd->eth.tx_broadcast);
1057 
1058 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1059 			   pf->stat_offsets_loaded,
1060 			   &osd->tx_dropped_link_down,
1061 			   &nsd->tx_dropped_link_down);
1062 
1063 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->crc_errors, &nsd->crc_errors);
1066 
1067 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->mac_local_faults,
1074 			   &nsd->mac_local_faults);
1075 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->mac_remote_faults,
1078 			   &nsd->mac_remote_faults);
1079 
1080 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->rx_length_errors,
1083 			   &nsd->rx_length_errors);
1084 
1085 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1086 			   pf->stat_offsets_loaded,
1087 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1088 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1091 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1092 			   pf->stat_offsets_loaded,
1093 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1094 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1095 			   pf->stat_offsets_loaded,
1096 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1097 
1098 	for (i = 0; i < 8; i++) {
1099 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1100 				   pf->stat_offsets_loaded,
1101 				   &osd->priority_xoff_rx[i],
1102 				   &nsd->priority_xoff_rx[i]);
1103 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1104 				   pf->stat_offsets_loaded,
1105 				   &osd->priority_xon_rx[i],
1106 				   &nsd->priority_xon_rx[i]);
1107 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1108 				   pf->stat_offsets_loaded,
1109 				   &osd->priority_xon_tx[i],
1110 				   &nsd->priority_xon_tx[i]);
1111 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1112 				   pf->stat_offsets_loaded,
1113 				   &osd->priority_xoff_tx[i],
1114 				   &nsd->priority_xoff_tx[i]);
1115 		i40e_stat_update32(hw,
1116 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1117 				   pf->stat_offsets_loaded,
1118 				   &osd->priority_xon_2_xoff[i],
1119 				   &nsd->priority_xon_2_xoff[i]);
1120 	}
1121 
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1123 			   I40E_GLPRT_PRC64L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_64, &nsd->rx_size_64);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1127 			   I40E_GLPRT_PRC127L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_127, &nsd->rx_size_127);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1131 			   I40E_GLPRT_PRC255L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_255, &nsd->rx_size_255);
1134 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1135 			   I40E_GLPRT_PRC511L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->rx_size_511, &nsd->rx_size_511);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1139 			   I40E_GLPRT_PRC1023L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1143 			   I40E_GLPRT_PRC1522L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1146 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1147 			   I40E_GLPRT_PRC9522L(hw->port),
1148 			   pf->stat_offsets_loaded,
1149 			   &osd->rx_size_big, &nsd->rx_size_big);
1150 
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1152 			   I40E_GLPRT_PTC64L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_64, &nsd->tx_size_64);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1156 			   I40E_GLPRT_PTC127L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_127, &nsd->tx_size_127);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1160 			   I40E_GLPRT_PTC255L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_255, &nsd->tx_size_255);
1163 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1164 			   I40E_GLPRT_PTC511L(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->tx_size_511, &nsd->tx_size_511);
1167 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1168 			   I40E_GLPRT_PTC1023L(hw->port),
1169 			   pf->stat_offsets_loaded,
1170 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1171 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1172 			   I40E_GLPRT_PTC1522L(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1175 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1176 			   I40E_GLPRT_PTC9522L(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->tx_size_big, &nsd->tx_size_big);
1179 
1180 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1181 			   pf->stat_offsets_loaded,
1182 			   &osd->rx_undersize, &nsd->rx_undersize);
1183 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1184 			   pf->stat_offsets_loaded,
1185 			   &osd->rx_fragments, &nsd->rx_fragments);
1186 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1187 			   pf->stat_offsets_loaded,
1188 			   &osd->rx_oversize, &nsd->rx_oversize);
1189 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1190 			   pf->stat_offsets_loaded,
1191 			   &osd->rx_jabber, &nsd->rx_jabber);
1192 
1193 	/* FDIR stats */
1194 	i40e_stat_update_and_clear32(hw,
1195 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1196 			&nsd->fd_atr_match);
1197 	i40e_stat_update_and_clear32(hw,
1198 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1199 			&nsd->fd_sb_match);
1200 	i40e_stat_update_and_clear32(hw,
1201 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1202 			&nsd->fd_atr_tunnel_match);
1203 
1204 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1205 	nsd->tx_lpi_status =
1206 		       FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
1207 	nsd->rx_lpi_status =
1208 		       FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
1209 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1210 			   pf->stat_offsets_loaded,
1211 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1212 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1213 			   pf->stat_offsets_loaded,
1214 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1215 
1216 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1217 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1218 		nsd->fd_sb_status = true;
1219 	else
1220 		nsd->fd_sb_status = false;
1221 
1222 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1223 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1224 		nsd->fd_atr_status = true;
1225 	else
1226 		nsd->fd_atr_status = false;
1227 
1228 	pf->stat_offsets_loaded = true;
1229 }
1230 
1231 /**
1232  * i40e_update_stats - Update the various statistics counters.
1233  * @vsi: the VSI to be updated
1234  *
1235  * Update the various stats for this VSI and its related entities.
1236  **/
1237 void i40e_update_stats(struct i40e_vsi *vsi)
1238 {
1239 	struct i40e_pf *pf = vsi->back;
1240 
1241 	if (vsi == pf->vsi[pf->lan_vsi])
1242 		i40e_update_pf_stats(pf);
1243 
1244 	i40e_update_vsi_stats(vsi);
1245 }
1246 
1247 /**
1248  * i40e_count_filters - counts VSI mac filters
1249  * @vsi: the VSI to be searched
1250  *
1251  * Returns count of mac filters
1252  **/
1253 int i40e_count_filters(struct i40e_vsi *vsi)
1254 {
1255 	struct i40e_mac_filter *f;
1256 	struct hlist_node *h;
1257 	int bkt;
1258 	int cnt = 0;
1259 
1260 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1261 		++cnt;
1262 
1263 	return cnt;
1264 }
1265 
1266 /**
1267  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1268  * @vsi: the VSI to be searched
1269  * @macaddr: the MAC address
1270  * @vlan: the vlan
1271  *
1272  * Returns ptr to the filter object or NULL
1273  **/
1274 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1275 						const u8 *macaddr, s16 vlan)
1276 {
1277 	struct i40e_mac_filter *f;
1278 	u64 key;
1279 
1280 	if (!vsi || !macaddr)
1281 		return NULL;
1282 
1283 	key = i40e_addr_to_hkey(macaddr);
1284 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1285 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1286 		    (vlan == f->vlan))
1287 			return f;
1288 	}
1289 	return NULL;
1290 }
1291 
1292 /**
1293  * i40e_find_mac - Find a mac addr in the macvlan filters list
1294  * @vsi: the VSI to be searched
1295  * @macaddr: the MAC address we are searching for
1296  *
1297  * Returns the first filter with the provided MAC address or NULL if
1298  * MAC address was not found
1299  **/
1300 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1301 {
1302 	struct i40e_mac_filter *f;
1303 	u64 key;
1304 
1305 	if (!vsi || !macaddr)
1306 		return NULL;
1307 
1308 	key = i40e_addr_to_hkey(macaddr);
1309 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1310 		if ((ether_addr_equal(macaddr, f->macaddr)))
1311 			return f;
1312 	}
1313 	return NULL;
1314 }
1315 
1316 /**
1317  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1318  * @vsi: the VSI to be searched
1319  *
1320  * Returns true if VSI is in vlan mode or false otherwise
1321  **/
1322 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1323 {
1324 	/* If we have a PVID, always operate in VLAN mode */
1325 	if (vsi->info.pvid)
1326 		return true;
1327 
1328 	/* We need to operate in VLAN mode whenever we have any filters with
1329 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1330 	 * time, incurring search cost repeatedly. However, we can notice two
1331 	 * things:
1332 	 *
1333 	 * 1) the only place where we can gain a VLAN filter is in
1334 	 *    i40e_add_filter.
1335 	 *
1336 	 * 2) the only place where filters are actually removed is in
1337 	 *    i40e_sync_filters_subtask.
1338 	 *
1339 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1340 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1341 	 * we have to perform the full search after deleting filters in
1342 	 * i40e_sync_filters_subtask, but we already have to search
1343 	 * filters here and can perform the check at the same time. This
1344 	 * results in avoiding embedding a loop for VLAN mode inside another
1345 	 * loop over all the filters, and should maintain correctness as noted
1346 	 * above.
1347 	 */
1348 	return vsi->has_vlan_filter;
1349 }
1350 
1351 /**
1352  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1353  * @vsi: the VSI to configure
1354  * @tmp_add_list: list of filters ready to be added
1355  * @tmp_del_list: list of filters ready to be deleted
1356  * @vlan_filters: the number of active VLAN filters
1357  *
1358  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1359  * behave as expected. If we have any active VLAN filters remaining or about
1360  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1361  * so that they only match against untagged traffic. If we no longer have any
1362  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1363  * so that they match against both tagged and untagged traffic. In this way,
1364  * we ensure that we correctly receive the desired traffic. This ensures that
1365  * when we have an active VLAN we will receive only untagged traffic and
1366  * traffic matching active VLANs. If we have no active VLANs then we will
1367  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1368  *
1369  * Finally, in a similar fashion, this function also corrects filters when
1370  * there is an active PVID assigned to this VSI.
1371  *
1372  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1373  *
1374  * This function is only expected to be called from within
1375  * i40e_sync_vsi_filters.
1376  *
1377  * NOTE: This function expects to be called while under the
1378  * mac_filter_hash_lock
1379  */
1380 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1381 					 struct hlist_head *tmp_add_list,
1382 					 struct hlist_head *tmp_del_list,
1383 					 int vlan_filters)
1384 {
1385 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1386 	struct i40e_mac_filter *f, *add_head;
1387 	struct i40e_new_mac_filter *new;
1388 	struct hlist_node *h;
1389 	int bkt, new_vlan;
1390 
1391 	/* To determine if a particular filter needs to be replaced we
1392 	 * have the three following conditions:
1393 	 *
1394 	 * a) if we have a PVID assigned, then all filters which are
1395 	 *    not marked as VLAN=PVID must be replaced with filters that
1396 	 *    are.
1397 	 * b) otherwise, if we have any active VLANS, all filters
1398 	 *    which are marked as VLAN=-1 must be replaced with
1399 	 *    filters marked as VLAN=0
1400 	 * c) finally, if we do not have any active VLANS, all filters
1401 	 *    which are marked as VLAN=0 must be replaced with filters
1402 	 *    marked as VLAN=-1
1403 	 */
1404 
1405 	/* Update the filters about to be added in place */
1406 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1407 		if (pvid && new->f->vlan != pvid)
1408 			new->f->vlan = pvid;
1409 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1410 			new->f->vlan = 0;
1411 		else if (!vlan_filters && new->f->vlan == 0)
1412 			new->f->vlan = I40E_VLAN_ANY;
1413 	}
1414 
1415 	/* Update the remaining active filters */
1416 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1417 		/* Combine the checks for whether a filter needs to be changed
1418 		 * and then determine the new VLAN inside the if block, in
1419 		 * order to avoid duplicating code for adding the new filter
1420 		 * then deleting the old filter.
1421 		 */
1422 		if ((pvid && f->vlan != pvid) ||
1423 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1424 		    (!vlan_filters && f->vlan == 0)) {
1425 			/* Determine the new vlan we will be adding */
1426 			if (pvid)
1427 				new_vlan = pvid;
1428 			else if (vlan_filters)
1429 				new_vlan = 0;
1430 			else
1431 				new_vlan = I40E_VLAN_ANY;
1432 
1433 			/* Create the new filter */
1434 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1435 			if (!add_head)
1436 				return -ENOMEM;
1437 
1438 			/* Create a temporary i40e_new_mac_filter */
1439 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1440 			if (!new)
1441 				return -ENOMEM;
1442 
1443 			new->f = add_head;
1444 			new->state = add_head->state;
1445 
1446 			/* Add the new filter to the tmp list */
1447 			hlist_add_head(&new->hlist, tmp_add_list);
1448 
1449 			/* Put the original filter into the delete list */
1450 			f->state = I40E_FILTER_REMOVE;
1451 			hash_del(&f->hlist);
1452 			hlist_add_head(&f->hlist, tmp_del_list);
1453 		}
1454 	}
1455 
1456 	vsi->has_vlan_filter = !!vlan_filters;
1457 
1458 	return 0;
1459 }
1460 
1461 /**
1462  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1463  * @vsi: the vsi to configure
1464  * @new_mac: new mac filter to be added
1465  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1466  * @vlan_filters: the number of active VLAN filters
1467  * @trusted: flag if the VF is trusted
1468  *
1469  * Get new VLAN id based on current VLAN filters, trust, PVID
1470  * and vf-vlan-prune-disable flag.
1471  *
1472  * Returns the value of the new vlan filter or
1473  * the old value if no new filter is needed.
1474  */
1475 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1476 				struct i40e_new_mac_filter *new_mac,
1477 				struct i40e_mac_filter *f,
1478 				int vlan_filters,
1479 				bool trusted)
1480 {
1481 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1482 	struct i40e_pf *pf = vsi->back;
1483 	bool is_any;
1484 
1485 	if (new_mac)
1486 		f = new_mac->f;
1487 
1488 	if (pvid && f->vlan != pvid)
1489 		return pvid;
1490 
1491 	is_any = (trusted ||
1492 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1493 
1494 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1495 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1496 	    (is_any && !vlan_filters && f->vlan == 0)) {
1497 		if (is_any)
1498 			return I40E_VLAN_ANY;
1499 		else
1500 			return 0;
1501 	}
1502 
1503 	return f->vlan;
1504 }
1505 
1506 /**
1507  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1508  * @vsi: the vsi to configure
1509  * @tmp_add_list: list of filters ready to be added
1510  * @tmp_del_list: list of filters ready to be deleted
1511  * @vlan_filters: the number of active VLAN filters
1512  * @trusted: flag if the VF is trusted
1513  *
1514  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1515  * and vf-vlan-prune-disable flag.
1516  *
1517  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1518  *
1519  * This function is only expected to be called from within
1520  * i40e_sync_vsi_filters.
1521  *
1522  * NOTE: This function expects to be called while under the
1523  * mac_filter_hash_lock
1524  */
1525 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1526 					    struct hlist_head *tmp_add_list,
1527 					    struct hlist_head *tmp_del_list,
1528 					    int vlan_filters,
1529 					    bool trusted)
1530 {
1531 	struct i40e_mac_filter *f, *add_head;
1532 	struct i40e_new_mac_filter *new_mac;
1533 	struct hlist_node *h;
1534 	int bkt, new_vlan;
1535 
1536 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1537 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1538 							vlan_filters, trusted);
1539 	}
1540 
1541 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1542 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1543 						trusted);
1544 		if (new_vlan != f->vlan) {
1545 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1546 			if (!add_head)
1547 				return -ENOMEM;
1548 			/* Create a temporary i40e_new_mac_filter */
1549 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1550 			if (!new_mac)
1551 				return -ENOMEM;
1552 			new_mac->f = add_head;
1553 			new_mac->state = add_head->state;
1554 
1555 			/* Add the new filter to the tmp list */
1556 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1557 
1558 			/* Put the original filter into the delete list */
1559 			f->state = I40E_FILTER_REMOVE;
1560 			hash_del(&f->hlist);
1561 			hlist_add_head(&f->hlist, tmp_del_list);
1562 		}
1563 	}
1564 
1565 	vsi->has_vlan_filter = !!vlan_filters;
1566 	return 0;
1567 }
1568 
1569 /**
1570  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1571  * @vsi: the PF Main VSI - inappropriate for any other VSI
1572  * @macaddr: the MAC address
1573  *
1574  * Remove whatever filter the firmware set up so the driver can manage
1575  * its own filtering intelligently.
1576  **/
1577 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1578 {
1579 	struct i40e_aqc_remove_macvlan_element_data element;
1580 	struct i40e_pf *pf = vsi->back;
1581 
1582 	/* Only appropriate for the PF main VSI */
1583 	if (vsi->type != I40E_VSI_MAIN)
1584 		return;
1585 
1586 	memset(&element, 0, sizeof(element));
1587 	ether_addr_copy(element.mac_addr, macaddr);
1588 	element.vlan_tag = 0;
1589 	/* Ignore error returns, some firmware does it this way... */
1590 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1591 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1592 
1593 	memset(&element, 0, sizeof(element));
1594 	ether_addr_copy(element.mac_addr, macaddr);
1595 	element.vlan_tag = 0;
1596 	/* ...and some firmware does it this way. */
1597 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1598 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1599 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1600 }
1601 
1602 /**
1603  * i40e_add_filter - Add a mac/vlan filter to the VSI
1604  * @vsi: the VSI to be searched
1605  * @macaddr: the MAC address
1606  * @vlan: the vlan
1607  *
1608  * Returns ptr to the filter object or NULL when no memory available.
1609  *
1610  * NOTE: This function is expected to be called with mac_filter_hash_lock
1611  * being held.
1612  **/
1613 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1614 					const u8 *macaddr, s16 vlan)
1615 {
1616 	struct i40e_mac_filter *f;
1617 	u64 key;
1618 
1619 	if (!vsi || !macaddr)
1620 		return NULL;
1621 
1622 	f = i40e_find_filter(vsi, macaddr, vlan);
1623 	if (!f) {
1624 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1625 		if (!f)
1626 			return NULL;
1627 
1628 		/* Update the boolean indicating if we need to function in
1629 		 * VLAN mode.
1630 		 */
1631 		if (vlan >= 0)
1632 			vsi->has_vlan_filter = true;
1633 
1634 		ether_addr_copy(f->macaddr, macaddr);
1635 		f->vlan = vlan;
1636 		f->state = I40E_FILTER_NEW;
1637 		INIT_HLIST_NODE(&f->hlist);
1638 
1639 		key = i40e_addr_to_hkey(macaddr);
1640 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1641 
1642 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1643 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1644 	}
1645 
1646 	/* If we're asked to add a filter that has been marked for removal, it
1647 	 * is safe to simply restore it to active state. __i40e_del_filter
1648 	 * will have simply deleted any filters which were previously marked
1649 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1650 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1651 	 * task, just restore this filter to the ACTIVE state so that the
1652 	 * sync task leaves it in place
1653 	 */
1654 	if (f->state == I40E_FILTER_REMOVE)
1655 		f->state = I40E_FILTER_ACTIVE;
1656 
1657 	return f;
1658 }
1659 
1660 /**
1661  * __i40e_del_filter - Remove a specific filter from the VSI
1662  * @vsi: VSI to remove from
1663  * @f: the filter to remove from the list
1664  *
1665  * This function should be called instead of i40e_del_filter only if you know
1666  * the exact filter you will remove already, such as via i40e_find_filter or
1667  * i40e_find_mac.
1668  *
1669  * NOTE: This function is expected to be called with mac_filter_hash_lock
1670  * being held.
1671  * ANOTHER NOTE: This function MUST be called from within the context of
1672  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1673  * instead of list_for_each_entry().
1674  **/
1675 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1676 {
1677 	if (!f)
1678 		return;
1679 
1680 	/* If the filter was never added to firmware then we can just delete it
1681 	 * directly and we don't want to set the status to remove or else an
1682 	 * admin queue command will unnecessarily fire.
1683 	 */
1684 	if ((f->state == I40E_FILTER_FAILED) ||
1685 	    (f->state == I40E_FILTER_NEW)) {
1686 		hash_del(&f->hlist);
1687 		kfree(f);
1688 	} else {
1689 		f->state = I40E_FILTER_REMOVE;
1690 	}
1691 
1692 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1693 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1694 }
1695 
1696 /**
1697  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1698  * @vsi: the VSI to be searched
1699  * @macaddr: the MAC address
1700  * @vlan: the VLAN
1701  *
1702  * NOTE: This function is expected to be called with mac_filter_hash_lock
1703  * being held.
1704  * ANOTHER NOTE: This function MUST be called from within the context of
1705  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1706  * instead of list_for_each_entry().
1707  **/
1708 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1709 {
1710 	struct i40e_mac_filter *f;
1711 
1712 	if (!vsi || !macaddr)
1713 		return;
1714 
1715 	f = i40e_find_filter(vsi, macaddr, vlan);
1716 	__i40e_del_filter(vsi, f);
1717 }
1718 
1719 /**
1720  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1721  * @vsi: the VSI to be searched
1722  * @macaddr: the mac address to be filtered
1723  *
1724  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1725  * go through all the macvlan filters and add a macvlan filter for each
1726  * unique vlan that already exists. If a PVID has been assigned, instead only
1727  * add the macaddr to that VLAN.
1728  *
1729  * Returns last filter added on success, else NULL
1730  **/
1731 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1732 					    const u8 *macaddr)
1733 {
1734 	struct i40e_mac_filter *f, *add = NULL;
1735 	struct hlist_node *h;
1736 	int bkt;
1737 
1738 	if (vsi->info.pvid)
1739 		return i40e_add_filter(vsi, macaddr,
1740 				       le16_to_cpu(vsi->info.pvid));
1741 
1742 	if (!i40e_is_vsi_in_vlan(vsi))
1743 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1744 
1745 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1746 		if (f->state == I40E_FILTER_REMOVE)
1747 			continue;
1748 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1749 		if (!add)
1750 			return NULL;
1751 	}
1752 
1753 	return add;
1754 }
1755 
1756 /**
1757  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1758  * @vsi: the VSI to be searched
1759  * @macaddr: the mac address to be removed
1760  *
1761  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1762  * associated with.
1763  *
1764  * Returns 0 for success, or error
1765  **/
1766 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1767 {
1768 	struct i40e_mac_filter *f;
1769 	struct hlist_node *h;
1770 	bool found = false;
1771 	int bkt;
1772 
1773 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1774 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1775 		if (ether_addr_equal(macaddr, f->macaddr)) {
1776 			__i40e_del_filter(vsi, f);
1777 			found = true;
1778 		}
1779 	}
1780 
1781 	if (found)
1782 		return 0;
1783 	else
1784 		return -ENOENT;
1785 }
1786 
1787 /**
1788  * i40e_set_mac - NDO callback to set mac address
1789  * @netdev: network interface device structure
1790  * @p: pointer to an address structure
1791  *
1792  * Returns 0 on success, negative on failure
1793  **/
1794 static int i40e_set_mac(struct net_device *netdev, void *p)
1795 {
1796 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1797 	struct i40e_vsi *vsi = np->vsi;
1798 	struct i40e_pf *pf = vsi->back;
1799 	struct i40e_hw *hw = &pf->hw;
1800 	struct sockaddr *addr = p;
1801 
1802 	if (!is_valid_ether_addr(addr->sa_data))
1803 		return -EADDRNOTAVAIL;
1804 
1805 	if (test_bit(__I40E_DOWN, pf->state) ||
1806 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1807 		return -EADDRNOTAVAIL;
1808 
1809 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1810 		netdev_info(netdev, "returning to hw mac address %pM\n",
1811 			    hw->mac.addr);
1812 	else
1813 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1814 
1815 	/* Copy the address first, so that we avoid a possible race with
1816 	 * .set_rx_mode().
1817 	 * - Remove old address from MAC filter
1818 	 * - Copy new address
1819 	 * - Add new address to MAC filter
1820 	 */
1821 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1822 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1823 	eth_hw_addr_set(netdev, addr->sa_data);
1824 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1825 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1826 
1827 	if (vsi->type == I40E_VSI_MAIN) {
1828 		int ret;
1829 
1830 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1831 						addr->sa_data, NULL);
1832 		if (ret)
1833 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1834 				    ERR_PTR(ret),
1835 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1836 	}
1837 
1838 	/* schedule our worker thread which will take care of
1839 	 * applying the new filter changes
1840 	 */
1841 	i40e_service_event_schedule(pf);
1842 	return 0;
1843 }
1844 
1845 /**
1846  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1847  * @vsi: vsi structure
1848  * @seed: RSS hash seed
1849  * @lut: pointer to lookup table of lut_size
1850  * @lut_size: size of the lookup table
1851  **/
1852 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1853 			      u8 *lut, u16 lut_size)
1854 {
1855 	struct i40e_pf *pf = vsi->back;
1856 	struct i40e_hw *hw = &pf->hw;
1857 	int ret = 0;
1858 
1859 	if (seed) {
1860 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1861 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1862 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1863 		if (ret) {
1864 			dev_info(&pf->pdev->dev,
1865 				 "Cannot set RSS key, err %pe aq_err %s\n",
1866 				 ERR_PTR(ret),
1867 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1868 			return ret;
1869 		}
1870 	}
1871 	if (lut) {
1872 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1873 
1874 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1875 		if (ret) {
1876 			dev_info(&pf->pdev->dev,
1877 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1878 				 ERR_PTR(ret),
1879 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1880 			return ret;
1881 		}
1882 	}
1883 	return ret;
1884 }
1885 
1886 /**
1887  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1888  * @vsi: VSI structure
1889  **/
1890 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1891 {
1892 	struct i40e_pf *pf = vsi->back;
1893 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1894 	u8 *lut;
1895 	int ret;
1896 
1897 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1898 		return 0;
1899 	if (!vsi->rss_size)
1900 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1901 				      vsi->num_queue_pairs);
1902 	if (!vsi->rss_size)
1903 		return -EINVAL;
1904 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1905 	if (!lut)
1906 		return -ENOMEM;
1907 
1908 	/* Use the user configured hash keys and lookup table if there is one,
1909 	 * otherwise use default
1910 	 */
1911 	if (vsi->rss_lut_user)
1912 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1913 	else
1914 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1915 	if (vsi->rss_hkey_user)
1916 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1917 	else
1918 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1919 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1920 	kfree(lut);
1921 	return ret;
1922 }
1923 
1924 /**
1925  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1926  * @vsi: the VSI being configured,
1927  * @ctxt: VSI context structure
1928  * @enabled_tc: number of traffic classes to enable
1929  *
1930  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1931  **/
1932 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1933 					   struct i40e_vsi_context *ctxt,
1934 					   u8 enabled_tc)
1935 {
1936 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1937 	int i, override_q, pow, num_qps, ret;
1938 	u8 netdev_tc = 0, offset = 0;
1939 
1940 	if (vsi->type != I40E_VSI_MAIN)
1941 		return -EINVAL;
1942 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1943 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1944 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1945 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1946 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1947 
1948 	/* find the next higher power-of-2 of num queue pairs */
1949 	pow = ilog2(num_qps);
1950 	if (!is_power_of_2(num_qps))
1951 		pow++;
1952 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1953 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1954 
1955 	/* Setup queue offset/count for all TCs for given VSI */
1956 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1957 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1958 		/* See if the given TC is enabled for the given VSI */
1959 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1960 			offset = vsi->mqprio_qopt.qopt.offset[i];
1961 			qcount = vsi->mqprio_qopt.qopt.count[i];
1962 			if (qcount > max_qcount)
1963 				max_qcount = qcount;
1964 			vsi->tc_config.tc_info[i].qoffset = offset;
1965 			vsi->tc_config.tc_info[i].qcount = qcount;
1966 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1967 		} else {
1968 			/* TC is not enabled so set the offset to
1969 			 * default queue and allocate one queue
1970 			 * for the given TC.
1971 			 */
1972 			vsi->tc_config.tc_info[i].qoffset = 0;
1973 			vsi->tc_config.tc_info[i].qcount = 1;
1974 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1975 		}
1976 	}
1977 
1978 	/* Set actual Tx/Rx queue pairs */
1979 	vsi->num_queue_pairs = offset + qcount;
1980 
1981 	/* Setup queue TC[0].qmap for given VSI context */
1982 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1983 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1984 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1985 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1986 
1987 	/* Reconfigure RSS for main VSI with max queue count */
1988 	vsi->rss_size = max_qcount;
1989 	ret = i40e_vsi_config_rss(vsi);
1990 	if (ret) {
1991 		dev_info(&vsi->back->pdev->dev,
1992 			 "Failed to reconfig rss for num_queues (%u)\n",
1993 			 max_qcount);
1994 		return ret;
1995 	}
1996 	vsi->reconfig_rss = true;
1997 	dev_dbg(&vsi->back->pdev->dev,
1998 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1999 
2000 	/* Find queue count available for channel VSIs and starting offset
2001 	 * for channel VSIs
2002 	 */
2003 	override_q = vsi->mqprio_qopt.qopt.count[0];
2004 	if (override_q && override_q < vsi->num_queue_pairs) {
2005 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2006 		vsi->next_base_queue = override_q;
2007 	}
2008 	return 0;
2009 }
2010 
2011 /**
2012  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2013  * @vsi: the VSI being setup
2014  * @ctxt: VSI context structure
2015  * @enabled_tc: Enabled TCs bitmap
2016  * @is_add: True if called before Add VSI
2017  *
2018  * Setup VSI queue mapping for enabled traffic classes.
2019  **/
2020 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2021 				     struct i40e_vsi_context *ctxt,
2022 				     u8 enabled_tc,
2023 				     bool is_add)
2024 {
2025 	struct i40e_pf *pf = vsi->back;
2026 	u16 num_tc_qps = 0;
2027 	u16 sections = 0;
2028 	u8 netdev_tc = 0;
2029 	u16 numtc = 1;
2030 	u16 qcount;
2031 	u8 offset;
2032 	u16 qmap;
2033 	int i;
2034 
2035 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2036 	offset = 0;
2037 	/* zero out queue mapping, it will get updated on the end of the function */
2038 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2039 
2040 	if (vsi->type == I40E_VSI_MAIN) {
2041 		/* This code helps add more queue to the VSI if we have
2042 		 * more cores than RSS can support, the higher cores will
2043 		 * be served by ATR or other filters. Furthermore, the
2044 		 * non-zero req_queue_pairs says that user requested a new
2045 		 * queue count via ethtool's set_channels, so use this
2046 		 * value for queues distribution across traffic classes
2047 		 * We need at least one queue pair for the interface
2048 		 * to be usable as we see in else statement.
2049 		 */
2050 		if (vsi->req_queue_pairs > 0)
2051 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2052 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2053 			vsi->num_queue_pairs = pf->num_lan_msix;
2054 		else
2055 			vsi->num_queue_pairs = 1;
2056 	}
2057 
2058 	/* Number of queues per enabled TC */
2059 	if (vsi->type == I40E_VSI_MAIN ||
2060 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2061 		num_tc_qps = vsi->num_queue_pairs;
2062 	else
2063 		num_tc_qps = vsi->alloc_queue_pairs;
2064 
2065 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2066 		/* Find numtc from enabled TC bitmap */
2067 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2068 			if (enabled_tc & BIT(i)) /* TC is enabled */
2069 				numtc++;
2070 		}
2071 		if (!numtc) {
2072 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2073 			numtc = 1;
2074 		}
2075 		num_tc_qps = num_tc_qps / numtc;
2076 		num_tc_qps = min_t(int, num_tc_qps,
2077 				   i40e_pf_get_max_q_per_tc(pf));
2078 	}
2079 
2080 	vsi->tc_config.numtc = numtc;
2081 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2082 
2083 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2084 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2085 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2086 
2087 	/* Setup queue offset/count for all TCs for given VSI */
2088 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2089 		/* See if the given TC is enabled for the given VSI */
2090 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2091 			/* TC is enabled */
2092 			int pow, num_qps;
2093 
2094 			switch (vsi->type) {
2095 			case I40E_VSI_MAIN:
2096 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2097 					       pf->flags) &&
2098 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2099 					       pf->flags)) ||
2100 				    vsi->tc_config.enabled_tc != 1) {
2101 					qcount = min_t(int, pf->alloc_rss_size,
2102 						       num_tc_qps);
2103 					break;
2104 				}
2105 				fallthrough;
2106 			case I40E_VSI_FDIR:
2107 			case I40E_VSI_SRIOV:
2108 			case I40E_VSI_VMDQ2:
2109 			default:
2110 				qcount = num_tc_qps;
2111 				WARN_ON(i != 0);
2112 				break;
2113 			}
2114 			vsi->tc_config.tc_info[i].qoffset = offset;
2115 			vsi->tc_config.tc_info[i].qcount = qcount;
2116 
2117 			/* find the next higher power-of-2 of num queue pairs */
2118 			num_qps = qcount;
2119 			pow = 0;
2120 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2121 				pow++;
2122 				num_qps >>= 1;
2123 			}
2124 
2125 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2126 			qmap =
2127 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2128 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2129 
2130 			offset += qcount;
2131 		} else {
2132 			/* TC is not enabled so set the offset to
2133 			 * default queue and allocate one queue
2134 			 * for the given TC.
2135 			 */
2136 			vsi->tc_config.tc_info[i].qoffset = 0;
2137 			vsi->tc_config.tc_info[i].qcount = 1;
2138 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2139 
2140 			qmap = 0;
2141 		}
2142 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2143 	}
2144 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2145 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2146 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2147 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2148 		vsi->num_queue_pairs = offset;
2149 
2150 	/* Scheduler section valid can only be set for ADD VSI */
2151 	if (is_add) {
2152 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2153 
2154 		ctxt->info.up_enable_bits = enabled_tc;
2155 	}
2156 	if (vsi->type == I40E_VSI_SRIOV) {
2157 		ctxt->info.mapping_flags |=
2158 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2159 		for (i = 0; i < vsi->num_queue_pairs; i++)
2160 			ctxt->info.queue_mapping[i] =
2161 					       cpu_to_le16(vsi->base_queue + i);
2162 	} else {
2163 		ctxt->info.mapping_flags |=
2164 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2165 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2166 	}
2167 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2168 }
2169 
2170 /**
2171  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2172  * @netdev: the netdevice
2173  * @addr: address to add
2174  *
2175  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2176  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2177  */
2178 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2179 {
2180 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2181 	struct i40e_vsi *vsi = np->vsi;
2182 
2183 	if (i40e_add_mac_filter(vsi, addr))
2184 		return 0;
2185 	else
2186 		return -ENOMEM;
2187 }
2188 
2189 /**
2190  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2191  * @netdev: the netdevice
2192  * @addr: address to add
2193  *
2194  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2195  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2196  */
2197 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2198 {
2199 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2200 	struct i40e_vsi *vsi = np->vsi;
2201 
2202 	/* Under some circumstances, we might receive a request to delete
2203 	 * our own device address from our uc list. Because we store the
2204 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2205 	 * such requests and not delete our device address from this list.
2206 	 */
2207 	if (ether_addr_equal(addr, netdev->dev_addr))
2208 		return 0;
2209 
2210 	i40e_del_mac_filter(vsi, addr);
2211 
2212 	return 0;
2213 }
2214 
2215 /**
2216  * i40e_set_rx_mode - NDO callback to set the netdev filters
2217  * @netdev: network interface device structure
2218  **/
2219 static void i40e_set_rx_mode(struct net_device *netdev)
2220 {
2221 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2222 	struct i40e_vsi *vsi = np->vsi;
2223 
2224 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2225 
2226 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2227 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2228 
2229 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2230 
2231 	/* check for other flag changes */
2232 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2233 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2234 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2235 	}
2236 }
2237 
2238 /**
2239  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2240  * @vsi: Pointer to VSI struct
2241  * @from: Pointer to list which contains MAC filter entries - changes to
2242  *        those entries needs to be undone.
2243  *
2244  * MAC filter entries from this list were slated for deletion.
2245  **/
2246 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2247 					 struct hlist_head *from)
2248 {
2249 	struct i40e_mac_filter *f;
2250 	struct hlist_node *h;
2251 
2252 	hlist_for_each_entry_safe(f, h, from, hlist) {
2253 		u64 key = i40e_addr_to_hkey(f->macaddr);
2254 
2255 		/* Move the element back into MAC filter list*/
2256 		hlist_del(&f->hlist);
2257 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2258 	}
2259 }
2260 
2261 /**
2262  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2263  * @vsi: Pointer to vsi struct
2264  * @from: Pointer to list which contains MAC filter entries - changes to
2265  *        those entries needs to be undone.
2266  *
2267  * MAC filter entries from this list were slated for addition.
2268  **/
2269 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2270 					 struct hlist_head *from)
2271 {
2272 	struct i40e_new_mac_filter *new;
2273 	struct hlist_node *h;
2274 
2275 	hlist_for_each_entry_safe(new, h, from, hlist) {
2276 		/* We can simply free the wrapper structure */
2277 		hlist_del(&new->hlist);
2278 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2279 		kfree(new);
2280 	}
2281 }
2282 
2283 /**
2284  * i40e_next_filter - Get the next non-broadcast filter from a list
2285  * @next: pointer to filter in list
2286  *
2287  * Returns the next non-broadcast filter in the list. Required so that we
2288  * ignore broadcast filters within the list, since these are not handled via
2289  * the normal firmware update path.
2290  */
2291 static
2292 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2293 {
2294 	hlist_for_each_entry_continue(next, hlist) {
2295 		if (!is_broadcast_ether_addr(next->f->macaddr))
2296 			return next;
2297 	}
2298 
2299 	return NULL;
2300 }
2301 
2302 /**
2303  * i40e_update_filter_state - Update filter state based on return data
2304  * from firmware
2305  * @count: Number of filters added
2306  * @add_list: return data from fw
2307  * @add_head: pointer to first filter in current batch
2308  *
2309  * MAC filter entries from list were slated to be added to device. Returns
2310  * number of successful filters. Note that 0 does NOT mean success!
2311  **/
2312 static int
2313 i40e_update_filter_state(int count,
2314 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2315 			 struct i40e_new_mac_filter *add_head)
2316 {
2317 	int retval = 0;
2318 	int i;
2319 
2320 	for (i = 0; i < count; i++) {
2321 		/* Always check status of each filter. We don't need to check
2322 		 * the firmware return status because we pre-set the filter
2323 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2324 		 * request to the adminq. Thus, if it no longer matches then
2325 		 * we know the filter is active.
2326 		 */
2327 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2328 			add_head->state = I40E_FILTER_FAILED;
2329 		} else {
2330 			add_head->state = I40E_FILTER_ACTIVE;
2331 			retval++;
2332 		}
2333 
2334 		add_head = i40e_next_filter(add_head);
2335 		if (!add_head)
2336 			break;
2337 	}
2338 
2339 	return retval;
2340 }
2341 
2342 /**
2343  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2344  * @vsi: ptr to the VSI
2345  * @vsi_name: name to display in messages
2346  * @list: the list of filters to send to firmware
2347  * @num_del: the number of filters to delete
2348  * @retval: Set to -EIO on failure to delete
2349  *
2350  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2351  * *retval instead of a return value so that success does not force ret_val to
2352  * be set to 0. This ensures that a sequence of calls to this function
2353  * preserve the previous value of *retval on successful delete.
2354  */
2355 static
2356 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2357 			  struct i40e_aqc_remove_macvlan_element_data *list,
2358 			  int num_del, int *retval)
2359 {
2360 	struct i40e_hw *hw = &vsi->back->hw;
2361 	enum i40e_admin_queue_err aq_status;
2362 	int aq_ret;
2363 
2364 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2365 					   &aq_status);
2366 
2367 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2368 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2369 		*retval = -EIO;
2370 		dev_info(&vsi->back->pdev->dev,
2371 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2372 			 vsi_name, ERR_PTR(aq_ret),
2373 			 i40e_aq_str(hw, aq_status));
2374 	}
2375 }
2376 
2377 /**
2378  * i40e_aqc_add_filters - Request firmware to add a set of filters
2379  * @vsi: ptr to the VSI
2380  * @vsi_name: name to display in messages
2381  * @list: the list of filters to send to firmware
2382  * @add_head: Position in the add hlist
2383  * @num_add: the number of filters to add
2384  *
2385  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2386  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2387  * space for more filters.
2388  */
2389 static
2390 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2391 			  struct i40e_aqc_add_macvlan_element_data *list,
2392 			  struct i40e_new_mac_filter *add_head,
2393 			  int num_add)
2394 {
2395 	struct i40e_hw *hw = &vsi->back->hw;
2396 	enum i40e_admin_queue_err aq_status;
2397 	int fcnt;
2398 
2399 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2400 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2401 
2402 	if (fcnt != num_add) {
2403 		if (vsi->type == I40E_VSI_MAIN) {
2404 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2405 			dev_warn(&vsi->back->pdev->dev,
2406 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2407 				 i40e_aq_str(hw, aq_status), vsi_name);
2408 		} else if (vsi->type == I40E_VSI_SRIOV ||
2409 			   vsi->type == I40E_VSI_VMDQ1 ||
2410 			   vsi->type == I40E_VSI_VMDQ2) {
2411 			dev_warn(&vsi->back->pdev->dev,
2412 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2413 				 i40e_aq_str(hw, aq_status), vsi_name,
2414 					     vsi_name);
2415 		} else {
2416 			dev_warn(&vsi->back->pdev->dev,
2417 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2418 				 i40e_aq_str(hw, aq_status), vsi_name,
2419 					     vsi->type);
2420 		}
2421 	}
2422 }
2423 
2424 /**
2425  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2426  * @vsi: pointer to the VSI
2427  * @vsi_name: the VSI name
2428  * @f: filter data
2429  *
2430  * This function sets or clears the promiscuous broadcast flags for VLAN
2431  * filters in order to properly receive broadcast frames. Assumes that only
2432  * broadcast filters are passed.
2433  *
2434  * Returns status indicating success or failure;
2435  **/
2436 static int
2437 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2438 			  struct i40e_mac_filter *f)
2439 {
2440 	bool enable = f->state == I40E_FILTER_NEW;
2441 	struct i40e_hw *hw = &vsi->back->hw;
2442 	int aq_ret;
2443 
2444 	if (f->vlan == I40E_VLAN_ANY) {
2445 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2446 						   vsi->seid,
2447 						   enable,
2448 						   NULL);
2449 	} else {
2450 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2451 							    vsi->seid,
2452 							    enable,
2453 							    f->vlan,
2454 							    NULL);
2455 	}
2456 
2457 	if (aq_ret) {
2458 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2459 		dev_warn(&vsi->back->pdev->dev,
2460 			 "Error %s, forcing overflow promiscuous on %s\n",
2461 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2462 			 vsi_name);
2463 	}
2464 
2465 	return aq_ret;
2466 }
2467 
2468 /**
2469  * i40e_set_promiscuous - set promiscuous mode
2470  * @pf: board private structure
2471  * @promisc: promisc on or off
2472  *
2473  * There are different ways of setting promiscuous mode on a PF depending on
2474  * what state/environment we're in.  This identifies and sets it appropriately.
2475  * Returns 0 on success.
2476  **/
2477 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2478 {
2479 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2480 	struct i40e_hw *hw = &pf->hw;
2481 	int aq_ret;
2482 
2483 	if (vsi->type == I40E_VSI_MAIN &&
2484 	    pf->lan_veb != I40E_NO_VEB &&
2485 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2486 		/* set defport ON for Main VSI instead of true promisc
2487 		 * this way we will get all unicast/multicast and VLAN
2488 		 * promisc behavior but will not get VF or VMDq traffic
2489 		 * replicated on the Main VSI.
2490 		 */
2491 		if (promisc)
2492 			aq_ret = i40e_aq_set_default_vsi(hw,
2493 							 vsi->seid,
2494 							 NULL);
2495 		else
2496 			aq_ret = i40e_aq_clear_default_vsi(hw,
2497 							   vsi->seid,
2498 							   NULL);
2499 		if (aq_ret) {
2500 			dev_info(&pf->pdev->dev,
2501 				 "Set default VSI failed, err %pe, aq_err %s\n",
2502 				 ERR_PTR(aq_ret),
2503 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2504 		}
2505 	} else {
2506 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2507 						  hw,
2508 						  vsi->seid,
2509 						  promisc, NULL,
2510 						  true);
2511 		if (aq_ret) {
2512 			dev_info(&pf->pdev->dev,
2513 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2514 				 ERR_PTR(aq_ret),
2515 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2516 		}
2517 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2518 						  hw,
2519 						  vsi->seid,
2520 						  promisc, NULL);
2521 		if (aq_ret) {
2522 			dev_info(&pf->pdev->dev,
2523 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2524 				 ERR_PTR(aq_ret),
2525 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2526 		}
2527 	}
2528 
2529 	if (!aq_ret)
2530 		pf->cur_promisc = promisc;
2531 
2532 	return aq_ret;
2533 }
2534 
2535 /**
2536  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2537  * @vsi: ptr to the VSI
2538  *
2539  * Push any outstanding VSI filter changes through the AdminQ.
2540  *
2541  * Returns 0 or error value
2542  **/
2543 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2544 {
2545 	struct hlist_head tmp_add_list, tmp_del_list;
2546 	struct i40e_mac_filter *f;
2547 	struct i40e_new_mac_filter *new, *add_head = NULL;
2548 	struct i40e_hw *hw = &vsi->back->hw;
2549 	bool old_overflow, new_overflow;
2550 	unsigned int failed_filters = 0;
2551 	unsigned int vlan_filters = 0;
2552 	char vsi_name[16] = "PF";
2553 	int filter_list_len = 0;
2554 	u32 changed_flags = 0;
2555 	struct hlist_node *h;
2556 	struct i40e_pf *pf;
2557 	int num_add = 0;
2558 	int num_del = 0;
2559 	int aq_ret = 0;
2560 	int retval = 0;
2561 	u16 cmd_flags;
2562 	int list_size;
2563 	int bkt;
2564 
2565 	/* empty array typed pointers, kcalloc later */
2566 	struct i40e_aqc_add_macvlan_element_data *add_list;
2567 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2568 
2569 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2570 		usleep_range(1000, 2000);
2571 	pf = vsi->back;
2572 
2573 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2574 
2575 	if (vsi->netdev) {
2576 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2577 		vsi->current_netdev_flags = vsi->netdev->flags;
2578 	}
2579 
2580 	INIT_HLIST_HEAD(&tmp_add_list);
2581 	INIT_HLIST_HEAD(&tmp_del_list);
2582 
2583 	if (vsi->type == I40E_VSI_SRIOV)
2584 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2585 	else if (vsi->type != I40E_VSI_MAIN)
2586 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2587 
2588 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2589 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2590 
2591 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2592 		/* Create a list of filters to delete. */
2593 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2594 			if (f->state == I40E_FILTER_REMOVE) {
2595 				/* Move the element into temporary del_list */
2596 				hash_del(&f->hlist);
2597 				hlist_add_head(&f->hlist, &tmp_del_list);
2598 
2599 				/* Avoid counting removed filters */
2600 				continue;
2601 			}
2602 			if (f->state == I40E_FILTER_NEW) {
2603 				/* Create a temporary i40e_new_mac_filter */
2604 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2605 				if (!new)
2606 					goto err_no_memory_locked;
2607 
2608 				/* Store pointer to the real filter */
2609 				new->f = f;
2610 				new->state = f->state;
2611 
2612 				/* Add it to the hash list */
2613 				hlist_add_head(&new->hlist, &tmp_add_list);
2614 			}
2615 
2616 			/* Count the number of active (current and new) VLAN
2617 			 * filters we have now. Does not count filters which
2618 			 * are marked for deletion.
2619 			 */
2620 			if (f->vlan > 0)
2621 				vlan_filters++;
2622 		}
2623 
2624 		if (vsi->type != I40E_VSI_SRIOV)
2625 			retval = i40e_correct_mac_vlan_filters
2626 				(vsi, &tmp_add_list, &tmp_del_list,
2627 				 vlan_filters);
2628 		else if (pf->vf)
2629 			retval = i40e_correct_vf_mac_vlan_filters
2630 				(vsi, &tmp_add_list, &tmp_del_list,
2631 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2632 
2633 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2634 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2635 
2636 		if (retval)
2637 			goto err_no_memory_locked;
2638 
2639 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2640 	}
2641 
2642 	/* Now process 'del_list' outside the lock */
2643 	if (!hlist_empty(&tmp_del_list)) {
2644 		filter_list_len = hw->aq.asq_buf_size /
2645 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2646 		list_size = filter_list_len *
2647 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2648 		del_list = kzalloc(list_size, GFP_ATOMIC);
2649 		if (!del_list)
2650 			goto err_no_memory;
2651 
2652 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2653 			cmd_flags = 0;
2654 
2655 			/* handle broadcast filters by updating the broadcast
2656 			 * promiscuous flag and release filter list.
2657 			 */
2658 			if (is_broadcast_ether_addr(f->macaddr)) {
2659 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2660 
2661 				hlist_del(&f->hlist);
2662 				kfree(f);
2663 				continue;
2664 			}
2665 
2666 			/* add to delete list */
2667 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2668 			if (f->vlan == I40E_VLAN_ANY) {
2669 				del_list[num_del].vlan_tag = 0;
2670 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2671 			} else {
2672 				del_list[num_del].vlan_tag =
2673 					cpu_to_le16((u16)(f->vlan));
2674 			}
2675 
2676 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2677 			del_list[num_del].flags = cmd_flags;
2678 			num_del++;
2679 
2680 			/* flush a full buffer */
2681 			if (num_del == filter_list_len) {
2682 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2683 						     num_del, &retval);
2684 				memset(del_list, 0, list_size);
2685 				num_del = 0;
2686 			}
2687 			/* Release memory for MAC filter entries which were
2688 			 * synced up with HW.
2689 			 */
2690 			hlist_del(&f->hlist);
2691 			kfree(f);
2692 		}
2693 
2694 		if (num_del) {
2695 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2696 					     num_del, &retval);
2697 		}
2698 
2699 		kfree(del_list);
2700 		del_list = NULL;
2701 	}
2702 
2703 	if (!hlist_empty(&tmp_add_list)) {
2704 		/* Do all the adds now. */
2705 		filter_list_len = hw->aq.asq_buf_size /
2706 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2707 		list_size = filter_list_len *
2708 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2709 		add_list = kzalloc(list_size, GFP_ATOMIC);
2710 		if (!add_list)
2711 			goto err_no_memory;
2712 
2713 		num_add = 0;
2714 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2715 			/* handle broadcast filters by updating the broadcast
2716 			 * promiscuous flag instead of adding a MAC filter.
2717 			 */
2718 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2719 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2720 							      new->f))
2721 					new->state = I40E_FILTER_FAILED;
2722 				else
2723 					new->state = I40E_FILTER_ACTIVE;
2724 				continue;
2725 			}
2726 
2727 			/* add to add array */
2728 			if (num_add == 0)
2729 				add_head = new;
2730 			cmd_flags = 0;
2731 			ether_addr_copy(add_list[num_add].mac_addr,
2732 					new->f->macaddr);
2733 			if (new->f->vlan == I40E_VLAN_ANY) {
2734 				add_list[num_add].vlan_tag = 0;
2735 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2736 			} else {
2737 				add_list[num_add].vlan_tag =
2738 					cpu_to_le16((u16)(new->f->vlan));
2739 			}
2740 			add_list[num_add].queue_number = 0;
2741 			/* set invalid match method for later detection */
2742 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2743 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2744 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2745 			num_add++;
2746 
2747 			/* flush a full buffer */
2748 			if (num_add == filter_list_len) {
2749 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2750 						     add_head, num_add);
2751 				memset(add_list, 0, list_size);
2752 				num_add = 0;
2753 			}
2754 		}
2755 		if (num_add) {
2756 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2757 					     num_add);
2758 		}
2759 		/* Now move all of the filters from the temp add list back to
2760 		 * the VSI's list.
2761 		 */
2762 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2763 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2764 			/* Only update the state if we're still NEW */
2765 			if (new->f->state == I40E_FILTER_NEW)
2766 				new->f->state = new->state;
2767 			hlist_del(&new->hlist);
2768 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2769 			kfree(new);
2770 		}
2771 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2772 		kfree(add_list);
2773 		add_list = NULL;
2774 	}
2775 
2776 	/* Determine the number of active and failed filters. */
2777 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2778 	vsi->active_filters = 0;
2779 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2780 		if (f->state == I40E_FILTER_ACTIVE)
2781 			vsi->active_filters++;
2782 		else if (f->state == I40E_FILTER_FAILED)
2783 			failed_filters++;
2784 	}
2785 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2786 
2787 	/* Check if we are able to exit overflow promiscuous mode. We can
2788 	 * safely exit if we didn't just enter, we no longer have any failed
2789 	 * filters, and we have reduced filters below the threshold value.
2790 	 */
2791 	if (old_overflow && !failed_filters &&
2792 	    vsi->active_filters < vsi->promisc_threshold) {
2793 		dev_info(&pf->pdev->dev,
2794 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2795 			 vsi_name);
2796 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2797 		vsi->promisc_threshold = 0;
2798 	}
2799 
2800 	/* if the VF is not trusted do not do promisc */
2801 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2802 	    !pf->vf[vsi->vf_id].trusted) {
2803 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2804 		goto out;
2805 	}
2806 
2807 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2808 
2809 	/* If we are entering overflow promiscuous, we need to calculate a new
2810 	 * threshold for when we are safe to exit
2811 	 */
2812 	if (!old_overflow && new_overflow)
2813 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2814 
2815 	/* check for changes in promiscuous modes */
2816 	if (changed_flags & IFF_ALLMULTI) {
2817 		bool cur_multipromisc;
2818 
2819 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2820 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2821 							       vsi->seid,
2822 							       cur_multipromisc,
2823 							       NULL);
2824 		if (aq_ret) {
2825 			retval = i40e_aq_rc_to_posix(aq_ret,
2826 						     hw->aq.asq_last_status);
2827 			dev_info(&pf->pdev->dev,
2828 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2829 				 vsi_name,
2830 				 ERR_PTR(aq_ret),
2831 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2832 		} else {
2833 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2834 				 cur_multipromisc ? "entering" : "leaving");
2835 		}
2836 	}
2837 
2838 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2839 		bool cur_promisc;
2840 
2841 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2842 			       new_overflow);
2843 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2844 		if (aq_ret) {
2845 			retval = i40e_aq_rc_to_posix(aq_ret,
2846 						     hw->aq.asq_last_status);
2847 			dev_info(&pf->pdev->dev,
2848 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2849 				 cur_promisc ? "on" : "off",
2850 				 vsi_name,
2851 				 ERR_PTR(aq_ret),
2852 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2853 		}
2854 	}
2855 out:
2856 	/* if something went wrong then set the changed flag so we try again */
2857 	if (retval)
2858 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2859 
2860 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2861 	return retval;
2862 
2863 err_no_memory:
2864 	/* Restore elements on the temporary add and delete lists */
2865 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2866 err_no_memory_locked:
2867 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2868 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2869 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2870 
2871 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2872 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2873 	return -ENOMEM;
2874 }
2875 
2876 /**
2877  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2878  * @pf: board private structure
2879  **/
2880 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2881 {
2882 	int v;
2883 
2884 	if (!pf)
2885 		return;
2886 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2887 		return;
2888 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2889 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2890 		return;
2891 	}
2892 
2893 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2894 		if (pf->vsi[v] &&
2895 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2896 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2897 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2898 
2899 			if (ret) {
2900 				/* come back and try again later */
2901 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2902 					pf->state);
2903 				break;
2904 			}
2905 		}
2906 	}
2907 }
2908 
2909 /**
2910  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2911  *
2912  * @vsi: VSI to calculate rx_buf_len from
2913  */
2914 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2915 {
2916 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2917 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2918 
2919 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2920 }
2921 
2922 /**
2923  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2924  * @vsi: the vsi
2925  * @xdp_prog: XDP program
2926  **/
2927 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2928 				   struct bpf_prog *xdp_prog)
2929 {
2930 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2931 	u16 chain_len;
2932 
2933 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2934 		chain_len = 1;
2935 	else
2936 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2937 
2938 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2939 }
2940 
2941 /**
2942  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2943  * @netdev: network interface device structure
2944  * @new_mtu: new value for maximum frame size
2945  *
2946  * Returns 0 on success, negative on failure
2947  **/
2948 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2949 {
2950 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2951 	struct i40e_vsi *vsi = np->vsi;
2952 	struct i40e_pf *pf = vsi->back;
2953 	int frame_size;
2954 
2955 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2956 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2957 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2958 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2959 		return -EINVAL;
2960 	}
2961 
2962 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2963 		   netdev->mtu, new_mtu);
2964 	netdev->mtu = new_mtu;
2965 	if (netif_running(netdev))
2966 		i40e_vsi_reinit_locked(vsi);
2967 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2968 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2969 	return 0;
2970 }
2971 
2972 /**
2973  * i40e_ioctl - Access the hwtstamp interface
2974  * @netdev: network interface device structure
2975  * @ifr: interface request data
2976  * @cmd: ioctl command
2977  **/
2978 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2979 {
2980 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2981 	struct i40e_pf *pf = np->vsi->back;
2982 
2983 	switch (cmd) {
2984 	case SIOCGHWTSTAMP:
2985 		return i40e_ptp_get_ts_config(pf, ifr);
2986 	case SIOCSHWTSTAMP:
2987 		return i40e_ptp_set_ts_config(pf, ifr);
2988 	default:
2989 		return -EOPNOTSUPP;
2990 	}
2991 }
2992 
2993 /**
2994  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2995  * @vsi: the vsi being adjusted
2996  **/
2997 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2998 {
2999 	struct i40e_vsi_context ctxt;
3000 	int ret;
3001 
3002 	/* Don't modify stripping options if a port VLAN is active */
3003 	if (vsi->info.pvid)
3004 		return;
3005 
3006 	if ((vsi->info.valid_sections &
3007 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3008 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3009 		return;  /* already enabled */
3010 
3011 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3012 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3013 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3014 
3015 	ctxt.seid = vsi->seid;
3016 	ctxt.info = vsi->info;
3017 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3018 	if (ret) {
3019 		dev_info(&vsi->back->pdev->dev,
3020 			 "update vlan stripping failed, err %pe aq_err %s\n",
3021 			 ERR_PTR(ret),
3022 			 i40e_aq_str(&vsi->back->hw,
3023 				     vsi->back->hw.aq.asq_last_status));
3024 	}
3025 }
3026 
3027 /**
3028  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3029  * @vsi: the vsi being adjusted
3030  **/
3031 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3032 {
3033 	struct i40e_vsi_context ctxt;
3034 	int ret;
3035 
3036 	/* Don't modify stripping options if a port VLAN is active */
3037 	if (vsi->info.pvid)
3038 		return;
3039 
3040 	if ((vsi->info.valid_sections &
3041 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3042 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3043 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3044 		return;  /* already disabled */
3045 
3046 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3047 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3048 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3049 
3050 	ctxt.seid = vsi->seid;
3051 	ctxt.info = vsi->info;
3052 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3053 	if (ret) {
3054 		dev_info(&vsi->back->pdev->dev,
3055 			 "update vlan stripping failed, err %pe aq_err %s\n",
3056 			 ERR_PTR(ret),
3057 			 i40e_aq_str(&vsi->back->hw,
3058 				     vsi->back->hw.aq.asq_last_status));
3059 	}
3060 }
3061 
3062 /**
3063  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3064  * @vsi: the vsi being configured
3065  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3066  *
3067  * This is a helper function for adding a new MAC/VLAN filter with the
3068  * specified VLAN for each existing MAC address already in the hash table.
3069  * This function does *not* perform any accounting to update filters based on
3070  * VLAN mode.
3071  *
3072  * NOTE: this function expects to be called while under the
3073  * mac_filter_hash_lock
3074  **/
3075 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3076 {
3077 	struct i40e_mac_filter *f, *add_f;
3078 	struct hlist_node *h;
3079 	int bkt;
3080 
3081 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3082 		/* If we're asked to add a filter that has been marked for
3083 		 * removal, it is safe to simply restore it to active state.
3084 		 * __i40e_del_filter will have simply deleted any filters which
3085 		 * were previously marked NEW or FAILED, so if it is currently
3086 		 * marked REMOVE it must have previously been ACTIVE. Since we
3087 		 * haven't yet run the sync filters task, just restore this
3088 		 * filter to the ACTIVE state so that the sync task leaves it
3089 		 * in place.
3090 		 */
3091 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3092 			f->state = I40E_FILTER_ACTIVE;
3093 			continue;
3094 		} else if (f->state == I40E_FILTER_REMOVE) {
3095 			continue;
3096 		}
3097 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3098 		if (!add_f) {
3099 			dev_info(&vsi->back->pdev->dev,
3100 				 "Could not add vlan filter %d for %pM\n",
3101 				 vid, f->macaddr);
3102 			return -ENOMEM;
3103 		}
3104 	}
3105 
3106 	return 0;
3107 }
3108 
3109 /**
3110  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3111  * @vsi: the VSI being configured
3112  * @vid: VLAN id to be added
3113  **/
3114 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3115 {
3116 	int err;
3117 
3118 	if (vsi->info.pvid)
3119 		return -EINVAL;
3120 
3121 	/* The network stack will attempt to add VID=0, with the intention to
3122 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3123 	 * these packets by default when configured to receive untagged
3124 	 * packets, so we don't need to add a filter for this case.
3125 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3126 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3127 	 * Thus, we do not want to actually add a filter for VID=0
3128 	 */
3129 	if (!vid)
3130 		return 0;
3131 
3132 	/* Locked once because all functions invoked below iterates list*/
3133 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3134 	err = i40e_add_vlan_all_mac(vsi, vid);
3135 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3136 	if (err)
3137 		return err;
3138 
3139 	/* schedule our worker thread which will take care of
3140 	 * applying the new filter changes
3141 	 */
3142 	i40e_service_event_schedule(vsi->back);
3143 	return 0;
3144 }
3145 
3146 /**
3147  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3148  * @vsi: the vsi being configured
3149  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3150  *
3151  * This function should be used to remove all VLAN filters which match the
3152  * given VID. It does not schedule the service event and does not take the
3153  * mac_filter_hash_lock so it may be combined with other operations under
3154  * a single invocation of the mac_filter_hash_lock.
3155  *
3156  * NOTE: this function expects to be called while under the
3157  * mac_filter_hash_lock
3158  */
3159 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3160 {
3161 	struct i40e_mac_filter *f;
3162 	struct hlist_node *h;
3163 	int bkt;
3164 
3165 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3166 		if (f->vlan == vid)
3167 			__i40e_del_filter(vsi, f);
3168 	}
3169 }
3170 
3171 /**
3172  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3173  * @vsi: the VSI being configured
3174  * @vid: VLAN id to be removed
3175  **/
3176 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3177 {
3178 	if (!vid || vsi->info.pvid)
3179 		return;
3180 
3181 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3182 	i40e_rm_vlan_all_mac(vsi, vid);
3183 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3184 
3185 	/* schedule our worker thread which will take care of
3186 	 * applying the new filter changes
3187 	 */
3188 	i40e_service_event_schedule(vsi->back);
3189 }
3190 
3191 /**
3192  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3193  * @netdev: network interface to be adjusted
3194  * @proto: unused protocol value
3195  * @vid: vlan id to be added
3196  *
3197  * net_device_ops implementation for adding vlan ids
3198  **/
3199 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3200 				__always_unused __be16 proto, u16 vid)
3201 {
3202 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3203 	struct i40e_vsi *vsi = np->vsi;
3204 	int ret = 0;
3205 
3206 	if (vid >= VLAN_N_VID)
3207 		return -EINVAL;
3208 
3209 	ret = i40e_vsi_add_vlan(vsi, vid);
3210 	if (!ret)
3211 		set_bit(vid, vsi->active_vlans);
3212 
3213 	return ret;
3214 }
3215 
3216 /**
3217  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3218  * @netdev: network interface to be adjusted
3219  * @proto: unused protocol value
3220  * @vid: vlan id to be added
3221  **/
3222 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3223 				    __always_unused __be16 proto, u16 vid)
3224 {
3225 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3226 	struct i40e_vsi *vsi = np->vsi;
3227 
3228 	if (vid >= VLAN_N_VID)
3229 		return;
3230 	set_bit(vid, vsi->active_vlans);
3231 }
3232 
3233 /**
3234  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3235  * @netdev: network interface to be adjusted
3236  * @proto: unused protocol value
3237  * @vid: vlan id to be removed
3238  *
3239  * net_device_ops implementation for removing vlan ids
3240  **/
3241 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3242 				 __always_unused __be16 proto, u16 vid)
3243 {
3244 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3245 	struct i40e_vsi *vsi = np->vsi;
3246 
3247 	/* return code is ignored as there is nothing a user
3248 	 * can do about failure to remove and a log message was
3249 	 * already printed from the other function
3250 	 */
3251 	i40e_vsi_kill_vlan(vsi, vid);
3252 
3253 	clear_bit(vid, vsi->active_vlans);
3254 
3255 	return 0;
3256 }
3257 
3258 /**
3259  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3260  * @vsi: the vsi being brought back up
3261  **/
3262 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3263 {
3264 	u16 vid;
3265 
3266 	if (!vsi->netdev)
3267 		return;
3268 
3269 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3270 		i40e_vlan_stripping_enable(vsi);
3271 	else
3272 		i40e_vlan_stripping_disable(vsi);
3273 
3274 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3275 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3276 					vid);
3277 }
3278 
3279 /**
3280  * i40e_vsi_add_pvid - Add pvid for the VSI
3281  * @vsi: the vsi being adjusted
3282  * @vid: the vlan id to set as a PVID
3283  **/
3284 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3285 {
3286 	struct i40e_vsi_context ctxt;
3287 	int ret;
3288 
3289 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3290 	vsi->info.pvid = cpu_to_le16(vid);
3291 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3292 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3293 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3294 
3295 	ctxt.seid = vsi->seid;
3296 	ctxt.info = vsi->info;
3297 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3298 	if (ret) {
3299 		dev_info(&vsi->back->pdev->dev,
3300 			 "add pvid failed, err %pe aq_err %s\n",
3301 			 ERR_PTR(ret),
3302 			 i40e_aq_str(&vsi->back->hw,
3303 				     vsi->back->hw.aq.asq_last_status));
3304 		return -ENOENT;
3305 	}
3306 
3307 	return 0;
3308 }
3309 
3310 /**
3311  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3312  * @vsi: the vsi being adjusted
3313  *
3314  * Just use the vlan_rx_register() service to put it back to normal
3315  **/
3316 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3317 {
3318 	vsi->info.pvid = 0;
3319 
3320 	i40e_vlan_stripping_disable(vsi);
3321 }
3322 
3323 /**
3324  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3325  * @vsi: ptr to the VSI
3326  *
3327  * If this function returns with an error, then it's possible one or
3328  * more of the rings is populated (while the rest are not).  It is the
3329  * callers duty to clean those orphaned rings.
3330  *
3331  * Return 0 on success, negative on failure
3332  **/
3333 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3334 {
3335 	int i, err = 0;
3336 
3337 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3338 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3339 
3340 	if (!i40e_enabled_xdp_vsi(vsi))
3341 		return err;
3342 
3343 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3344 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3345 
3346 	return err;
3347 }
3348 
3349 /**
3350  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3351  * @vsi: ptr to the VSI
3352  *
3353  * Free VSI's transmit software resources
3354  **/
3355 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3356 {
3357 	int i;
3358 
3359 	if (vsi->tx_rings) {
3360 		for (i = 0; i < vsi->num_queue_pairs; i++)
3361 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3362 				i40e_free_tx_resources(vsi->tx_rings[i]);
3363 	}
3364 
3365 	if (vsi->xdp_rings) {
3366 		for (i = 0; i < vsi->num_queue_pairs; i++)
3367 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3368 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3369 	}
3370 }
3371 
3372 /**
3373  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3374  * @vsi: ptr to the VSI
3375  *
3376  * If this function returns with an error, then it's possible one or
3377  * more of the rings is populated (while the rest are not).  It is the
3378  * callers duty to clean those orphaned rings.
3379  *
3380  * Return 0 on success, negative on failure
3381  **/
3382 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3383 {
3384 	int i, err = 0;
3385 
3386 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3387 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3388 	return err;
3389 }
3390 
3391 /**
3392  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3393  * @vsi: ptr to the VSI
3394  *
3395  * Free all receive software resources
3396  **/
3397 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3398 {
3399 	int i;
3400 
3401 	if (!vsi->rx_rings)
3402 		return;
3403 
3404 	for (i = 0; i < vsi->num_queue_pairs; i++)
3405 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3406 			i40e_free_rx_resources(vsi->rx_rings[i]);
3407 }
3408 
3409 /**
3410  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3411  * @ring: The Tx ring to configure
3412  *
3413  * This enables/disables XPS for a given Tx descriptor ring
3414  * based on the TCs enabled for the VSI that ring belongs to.
3415  **/
3416 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3417 {
3418 	int cpu;
3419 
3420 	if (!ring->q_vector || !ring->netdev || ring->ch)
3421 		return;
3422 
3423 	/* We only initialize XPS once, so as not to overwrite user settings */
3424 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3425 		return;
3426 
3427 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3428 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3429 			    ring->queue_index);
3430 }
3431 
3432 /**
3433  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3434  * @ring: The Tx or Rx ring
3435  *
3436  * Returns the AF_XDP buffer pool or NULL.
3437  **/
3438 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3439 {
3440 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3441 	int qid = ring->queue_index;
3442 
3443 	if (ring_is_xdp(ring))
3444 		qid -= ring->vsi->alloc_queue_pairs;
3445 
3446 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3447 		return NULL;
3448 
3449 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3450 }
3451 
3452 /**
3453  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3454  * @ring: The Tx ring to configure
3455  *
3456  * Configure the Tx descriptor ring in the HMC context.
3457  **/
3458 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3459 {
3460 	struct i40e_vsi *vsi = ring->vsi;
3461 	u16 pf_q = vsi->base_queue + ring->queue_index;
3462 	struct i40e_hw *hw = &vsi->back->hw;
3463 	struct i40e_hmc_obj_txq tx_ctx;
3464 	u32 qtx_ctl = 0;
3465 	int err = 0;
3466 
3467 	if (ring_is_xdp(ring))
3468 		ring->xsk_pool = i40e_xsk_pool(ring);
3469 
3470 	/* some ATR related tx ring init */
3471 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3472 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3473 		ring->atr_count = 0;
3474 	} else {
3475 		ring->atr_sample_rate = 0;
3476 	}
3477 
3478 	/* configure XPS */
3479 	i40e_config_xps_tx_ring(ring);
3480 
3481 	/* clear the context structure first */
3482 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3483 
3484 	tx_ctx.new_context = 1;
3485 	tx_ctx.base = (ring->dma / 128);
3486 	tx_ctx.qlen = ring->count;
3487 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3488 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3489 		tx_ctx.fd_ena = 1;
3490 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3491 		tx_ctx.timesync_ena = 1;
3492 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3493 	if (vsi->type != I40E_VSI_FDIR)
3494 		tx_ctx.head_wb_ena = 1;
3495 	tx_ctx.head_wb_addr = ring->dma +
3496 			      (ring->count * sizeof(struct i40e_tx_desc));
3497 
3498 	/* As part of VSI creation/update, FW allocates certain
3499 	 * Tx arbitration queue sets for each TC enabled for
3500 	 * the VSI. The FW returns the handles to these queue
3501 	 * sets as part of the response buffer to Add VSI,
3502 	 * Update VSI, etc. AQ commands. It is expected that
3503 	 * these queue set handles be associated with the Tx
3504 	 * queues by the driver as part of the TX queue context
3505 	 * initialization. This has to be done regardless of
3506 	 * DCB as by default everything is mapped to TC0.
3507 	 */
3508 
3509 	if (ring->ch)
3510 		tx_ctx.rdylist =
3511 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3512 
3513 	else
3514 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3515 
3516 	tx_ctx.rdylist_act = 0;
3517 
3518 	/* clear the context in the HMC */
3519 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3520 	if (err) {
3521 		dev_info(&vsi->back->pdev->dev,
3522 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3523 			 ring->queue_index, pf_q, err);
3524 		return -ENOMEM;
3525 	}
3526 
3527 	/* set the context in the HMC */
3528 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3529 	if (err) {
3530 		dev_info(&vsi->back->pdev->dev,
3531 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3532 			 ring->queue_index, pf_q, err);
3533 		return -ENOMEM;
3534 	}
3535 
3536 	/* Now associate this queue with this PCI function */
3537 	if (ring->ch) {
3538 		if (ring->ch->type == I40E_VSI_VMDQ2)
3539 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3540 		else
3541 			return -EINVAL;
3542 
3543 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3544 				      ring->ch->vsi_number);
3545 	} else {
3546 		if (vsi->type == I40E_VSI_VMDQ2) {
3547 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3548 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3549 					      vsi->id);
3550 		} else {
3551 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3552 		}
3553 	}
3554 
3555 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3556 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3557 	i40e_flush(hw);
3558 
3559 	/* cache tail off for easier writes later */
3560 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3561 
3562 	return 0;
3563 }
3564 
3565 /**
3566  * i40e_rx_offset - Return expected offset into page to access data
3567  * @rx_ring: Ring we are requesting offset of
3568  *
3569  * Returns the offset value for ring into the data buffer.
3570  */
3571 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3572 {
3573 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3574 }
3575 
3576 /**
3577  * i40e_configure_rx_ring - Configure a receive ring context
3578  * @ring: The Rx ring to configure
3579  *
3580  * Configure the Rx descriptor ring in the HMC context.
3581  **/
3582 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3583 {
3584 	struct i40e_vsi *vsi = ring->vsi;
3585 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3586 	u16 pf_q = vsi->base_queue + ring->queue_index;
3587 	struct i40e_hw *hw = &vsi->back->hw;
3588 	struct i40e_hmc_obj_rxq rx_ctx;
3589 	int err = 0;
3590 	bool ok;
3591 
3592 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3593 
3594 	/* clear the context structure first */
3595 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3596 
3597 	ring->rx_buf_len = vsi->rx_buf_len;
3598 
3599 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3600 	if (ring->vsi->type != I40E_VSI_MAIN)
3601 		goto skip;
3602 
3603 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3604 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3605 					 ring->queue_index,
3606 					 ring->q_vector->napi.napi_id,
3607 					 ring->rx_buf_len);
3608 		if (err)
3609 			return err;
3610 	}
3611 
3612 	ring->xsk_pool = i40e_xsk_pool(ring);
3613 	if (ring->xsk_pool) {
3614 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3615 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3616 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3617 					 ring->queue_index,
3618 					 ring->q_vector->napi.napi_id,
3619 					 ring->rx_buf_len);
3620 		if (err)
3621 			return err;
3622 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3623 						 MEM_TYPE_XSK_BUFF_POOL,
3624 						 NULL);
3625 		if (err)
3626 			return err;
3627 		dev_info(&vsi->back->pdev->dev,
3628 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3629 			 ring->queue_index);
3630 
3631 	} else {
3632 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3633 						 MEM_TYPE_PAGE_SHARED,
3634 						 NULL);
3635 		if (err)
3636 			return err;
3637 	}
3638 
3639 skip:
3640 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3641 
3642 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3643 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3644 
3645 	rx_ctx.base = (ring->dma / 128);
3646 	rx_ctx.qlen = ring->count;
3647 
3648 	/* use 16 byte descriptors */
3649 	rx_ctx.dsize = 0;
3650 
3651 	/* descriptor type is always zero
3652 	 * rx_ctx.dtype = 0;
3653 	 */
3654 	rx_ctx.hsplit_0 = 0;
3655 
3656 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3657 	if (hw->revision_id == 0)
3658 		rx_ctx.lrxqthresh = 0;
3659 	else
3660 		rx_ctx.lrxqthresh = 1;
3661 	rx_ctx.crcstrip = 1;
3662 	rx_ctx.l2tsel = 1;
3663 	/* this controls whether VLAN is stripped from inner headers */
3664 	rx_ctx.showiv = 0;
3665 	/* set the prefena field to 1 because the manual says to */
3666 	rx_ctx.prefena = 1;
3667 
3668 	/* clear the context in the HMC */
3669 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3670 	if (err) {
3671 		dev_info(&vsi->back->pdev->dev,
3672 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3673 			 ring->queue_index, pf_q, err);
3674 		return -ENOMEM;
3675 	}
3676 
3677 	/* set the context in the HMC */
3678 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3679 	if (err) {
3680 		dev_info(&vsi->back->pdev->dev,
3681 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3682 			 ring->queue_index, pf_q, err);
3683 		return -ENOMEM;
3684 	}
3685 
3686 	/* configure Rx buffer alignment */
3687 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3688 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3689 			dev_info(&vsi->back->pdev->dev,
3690 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3691 			return -EOPNOTSUPP;
3692 		}
3693 		clear_ring_build_skb_enabled(ring);
3694 	} else {
3695 		set_ring_build_skb_enabled(ring);
3696 	}
3697 
3698 	ring->rx_offset = i40e_rx_offset(ring);
3699 
3700 	/* cache tail for quicker writes, and clear the reg before use */
3701 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3702 	writel(0, ring->tail);
3703 
3704 	if (ring->xsk_pool) {
3705 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3706 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3707 	} else {
3708 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3709 	}
3710 	if (!ok) {
3711 		/* Log this in case the user has forgotten to give the kernel
3712 		 * any buffers, even later in the application.
3713 		 */
3714 		dev_info(&vsi->back->pdev->dev,
3715 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3716 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3717 			 ring->queue_index, pf_q);
3718 	}
3719 
3720 	return 0;
3721 }
3722 
3723 /**
3724  * i40e_vsi_configure_tx - Configure the VSI for Tx
3725  * @vsi: VSI structure describing this set of rings and resources
3726  *
3727  * Configure the Tx VSI for operation.
3728  **/
3729 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3730 {
3731 	int err = 0;
3732 	u16 i;
3733 
3734 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3735 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3736 
3737 	if (err || !i40e_enabled_xdp_vsi(vsi))
3738 		return err;
3739 
3740 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3741 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3742 
3743 	return err;
3744 }
3745 
3746 /**
3747  * i40e_vsi_configure_rx - Configure the VSI for Rx
3748  * @vsi: the VSI being configured
3749  *
3750  * Configure the Rx VSI for operation.
3751  **/
3752 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3753 {
3754 	int err = 0;
3755 	u16 i;
3756 
3757 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3758 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3759 
3760 #if (PAGE_SIZE < 8192)
3761 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3762 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3763 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3764 		vsi->max_frame = vsi->rx_buf_len;
3765 	}
3766 #endif
3767 
3768 	/* set up individual rings */
3769 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3770 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3771 
3772 	return err;
3773 }
3774 
3775 /**
3776  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3777  * @vsi: ptr to the VSI
3778  **/
3779 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3780 {
3781 	struct i40e_ring *tx_ring, *rx_ring;
3782 	u16 qoffset, qcount;
3783 	int i, n;
3784 
3785 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3786 		/* Reset the TC information */
3787 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3788 			rx_ring = vsi->rx_rings[i];
3789 			tx_ring = vsi->tx_rings[i];
3790 			rx_ring->dcb_tc = 0;
3791 			tx_ring->dcb_tc = 0;
3792 		}
3793 		return;
3794 	}
3795 
3796 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3797 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3798 			continue;
3799 
3800 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3801 		qcount = vsi->tc_config.tc_info[n].qcount;
3802 		for (i = qoffset; i < (qoffset + qcount); i++) {
3803 			rx_ring = vsi->rx_rings[i];
3804 			tx_ring = vsi->tx_rings[i];
3805 			rx_ring->dcb_tc = n;
3806 			tx_ring->dcb_tc = n;
3807 		}
3808 	}
3809 }
3810 
3811 /**
3812  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3813  * @vsi: ptr to the VSI
3814  **/
3815 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3816 {
3817 	if (vsi->netdev)
3818 		i40e_set_rx_mode(vsi->netdev);
3819 }
3820 
3821 /**
3822  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3823  * @pf: Pointer to the targeted PF
3824  *
3825  * Set all flow director counters to 0.
3826  */
3827 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3828 {
3829 	pf->fd_tcp4_filter_cnt = 0;
3830 	pf->fd_udp4_filter_cnt = 0;
3831 	pf->fd_sctp4_filter_cnt = 0;
3832 	pf->fd_ip4_filter_cnt = 0;
3833 	pf->fd_tcp6_filter_cnt = 0;
3834 	pf->fd_udp6_filter_cnt = 0;
3835 	pf->fd_sctp6_filter_cnt = 0;
3836 	pf->fd_ip6_filter_cnt = 0;
3837 }
3838 
3839 /**
3840  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3841  * @vsi: Pointer to the targeted VSI
3842  *
3843  * This function replays the hlist on the hw where all the SB Flow Director
3844  * filters were saved.
3845  **/
3846 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3847 {
3848 	struct i40e_fdir_filter *filter;
3849 	struct i40e_pf *pf = vsi->back;
3850 	struct hlist_node *node;
3851 
3852 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3853 		return;
3854 
3855 	/* Reset FDir counters as we're replaying all existing filters */
3856 	i40e_reset_fdir_filter_cnt(pf);
3857 
3858 	hlist_for_each_entry_safe(filter, node,
3859 				  &pf->fdir_filter_list, fdir_node) {
3860 		i40e_add_del_fdir(vsi, filter, true);
3861 	}
3862 }
3863 
3864 /**
3865  * i40e_vsi_configure - Set up the VSI for action
3866  * @vsi: the VSI being configured
3867  **/
3868 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3869 {
3870 	int err;
3871 
3872 	i40e_set_vsi_rx_mode(vsi);
3873 	i40e_restore_vlan(vsi);
3874 	i40e_vsi_config_dcb_rings(vsi);
3875 	err = i40e_vsi_configure_tx(vsi);
3876 	if (!err)
3877 		err = i40e_vsi_configure_rx(vsi);
3878 
3879 	return err;
3880 }
3881 
3882 /**
3883  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3884  * @vsi: the VSI being configured
3885  **/
3886 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3887 {
3888 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3889 	struct i40e_pf *pf = vsi->back;
3890 	struct i40e_hw *hw = &pf->hw;
3891 	u16 vector;
3892 	int i, q;
3893 	u32 qp;
3894 
3895 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3896 	 * and PFINT_LNKLSTn registers, e.g.:
3897 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3898 	 */
3899 	qp = vsi->base_queue;
3900 	vector = vsi->base_vector;
3901 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3902 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3903 
3904 		q_vector->rx.next_update = jiffies + 1;
3905 		q_vector->rx.target_itr =
3906 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3907 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3908 		     q_vector->rx.target_itr >> 1);
3909 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3910 
3911 		q_vector->tx.next_update = jiffies + 1;
3912 		q_vector->tx.target_itr =
3913 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3914 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3915 		     q_vector->tx.target_itr >> 1);
3916 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3917 
3918 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3919 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3920 
3921 		/* begin of linked list for RX queue assigned to this vector */
3922 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3923 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3924 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3925 			u32 val;
3926 
3927 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3928 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3929 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3930 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3931 			      (I40E_QUEUE_TYPE_TX <<
3932 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3933 
3934 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3935 
3936 			if (has_xdp) {
3937 				/* TX queue with next queue set to TX */
3938 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3939 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3940 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3941 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3942 				      (I40E_QUEUE_TYPE_TX <<
3943 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3944 
3945 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3946 			}
3947 			/* TX queue with next RX or end of linked list */
3948 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3949 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3950 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3951 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3952 			      (I40E_QUEUE_TYPE_RX <<
3953 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3954 
3955 			/* Terminate the linked list */
3956 			if (q == (q_vector->num_ringpairs - 1))
3957 				val |= (I40E_QUEUE_END_OF_LIST <<
3958 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3959 
3960 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3961 			qp++;
3962 		}
3963 	}
3964 
3965 	i40e_flush(hw);
3966 }
3967 
3968 /**
3969  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3970  * @pf: pointer to private device data structure
3971  **/
3972 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3973 {
3974 	struct i40e_hw *hw = &pf->hw;
3975 	u32 val;
3976 
3977 	/* clear things first */
3978 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3979 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3980 
3981 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3982 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3983 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3984 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3985 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3986 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3987 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3988 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3989 
3990 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3991 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3992 
3993 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
3994 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3995 
3996 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3997 
3998 	/* SW_ITR_IDX = 0, but don't change INTENA */
3999 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
4000 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
4001 
4002 	/* OTHER_ITR_IDX = 0 */
4003 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
4004 }
4005 
4006 /**
4007  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
4008  * @vsi: the VSI being configured
4009  **/
4010 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
4011 {
4012 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
4013 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4014 	struct i40e_pf *pf = vsi->back;
4015 	struct i40e_hw *hw = &pf->hw;
4016 
4017 	/* set the ITR configuration */
4018 	q_vector->rx.next_update = jiffies + 1;
4019 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4020 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4021 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4022 	q_vector->tx.next_update = jiffies + 1;
4023 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4024 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4025 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4026 
4027 	i40e_enable_misc_int_causes(pf);
4028 
4029 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4030 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4031 
4032 	/* Associate the queue pair to the vector and enable the queue
4033 	 * interrupt RX queue in linked list with next queue set to TX
4034 	 */
4035 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4036 
4037 	if (i40e_enabled_xdp_vsi(vsi)) {
4038 		/* TX queue in linked list with next queue set to TX */
4039 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4040 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4041 	}
4042 
4043 	/* last TX queue so the next RX queue doesn't matter */
4044 	wr32(hw, I40E_QINT_TQCTL(0),
4045 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4046 	i40e_flush(hw);
4047 }
4048 
4049 /**
4050  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4051  * @pf: board private structure
4052  **/
4053 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4054 {
4055 	struct i40e_hw *hw = &pf->hw;
4056 
4057 	wr32(hw, I40E_PFINT_DYN_CTL0,
4058 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4059 	i40e_flush(hw);
4060 }
4061 
4062 /**
4063  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4064  * @pf: board private structure
4065  **/
4066 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4067 {
4068 	struct i40e_hw *hw = &pf->hw;
4069 	u32 val;
4070 
4071 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4072 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4073 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4074 
4075 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4076 	i40e_flush(hw);
4077 }
4078 
4079 /**
4080  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4081  * @irq: interrupt number
4082  * @data: pointer to a q_vector
4083  **/
4084 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4085 {
4086 	struct i40e_q_vector *q_vector = data;
4087 
4088 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4089 		return IRQ_HANDLED;
4090 
4091 	napi_schedule_irqoff(&q_vector->napi);
4092 
4093 	return IRQ_HANDLED;
4094 }
4095 
4096 /**
4097  * i40e_irq_affinity_notify - Callback for affinity changes
4098  * @notify: context as to what irq was changed
4099  * @mask: the new affinity mask
4100  *
4101  * This is a callback function used by the irq_set_affinity_notifier function
4102  * so that we may register to receive changes to the irq affinity masks.
4103  **/
4104 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4105 				     const cpumask_t *mask)
4106 {
4107 	struct i40e_q_vector *q_vector =
4108 		container_of(notify, struct i40e_q_vector, affinity_notify);
4109 
4110 	cpumask_copy(&q_vector->affinity_mask, mask);
4111 }
4112 
4113 /**
4114  * i40e_irq_affinity_release - Callback for affinity notifier release
4115  * @ref: internal core kernel usage
4116  *
4117  * This is a callback function used by the irq_set_affinity_notifier function
4118  * to inform the current notification subscriber that they will no longer
4119  * receive notifications.
4120  **/
4121 static void i40e_irq_affinity_release(struct kref *ref) {}
4122 
4123 /**
4124  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4125  * @vsi: the VSI being configured
4126  * @basename: name for the vector
4127  *
4128  * Allocates MSI-X vectors and requests interrupts from the kernel.
4129  **/
4130 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4131 {
4132 	int q_vectors = vsi->num_q_vectors;
4133 	struct i40e_pf *pf = vsi->back;
4134 	int base = vsi->base_vector;
4135 	int rx_int_idx = 0;
4136 	int tx_int_idx = 0;
4137 	int vector, err;
4138 	int irq_num;
4139 	int cpu;
4140 
4141 	for (vector = 0; vector < q_vectors; vector++) {
4142 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4143 
4144 		irq_num = pf->msix_entries[base + vector].vector;
4145 
4146 		if (q_vector->tx.ring && q_vector->rx.ring) {
4147 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4148 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4149 			tx_int_idx++;
4150 		} else if (q_vector->rx.ring) {
4151 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4152 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4153 		} else if (q_vector->tx.ring) {
4154 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4155 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4156 		} else {
4157 			/* skip this unused q_vector */
4158 			continue;
4159 		}
4160 		err = request_irq(irq_num,
4161 				  vsi->irq_handler,
4162 				  0,
4163 				  q_vector->name,
4164 				  q_vector);
4165 		if (err) {
4166 			dev_info(&pf->pdev->dev,
4167 				 "MSIX request_irq failed, error: %d\n", err);
4168 			goto free_queue_irqs;
4169 		}
4170 
4171 		/* register for affinity change notifications */
4172 		q_vector->irq_num = irq_num;
4173 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4174 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4175 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4176 		/* Spread affinity hints out across online CPUs.
4177 		 *
4178 		 * get_cpu_mask returns a static constant mask with
4179 		 * a permanent lifetime so it's ok to pass to
4180 		 * irq_update_affinity_hint without making a copy.
4181 		 */
4182 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4183 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4184 	}
4185 
4186 	vsi->irqs_ready = true;
4187 	return 0;
4188 
4189 free_queue_irqs:
4190 	while (vector) {
4191 		vector--;
4192 		irq_num = pf->msix_entries[base + vector].vector;
4193 		irq_set_affinity_notifier(irq_num, NULL);
4194 		irq_update_affinity_hint(irq_num, NULL);
4195 		free_irq(irq_num, &vsi->q_vectors[vector]);
4196 	}
4197 	return err;
4198 }
4199 
4200 /**
4201  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4202  * @vsi: the VSI being un-configured
4203  **/
4204 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4205 {
4206 	struct i40e_pf *pf = vsi->back;
4207 	struct i40e_hw *hw = &pf->hw;
4208 	int base = vsi->base_vector;
4209 	int i;
4210 
4211 	/* disable interrupt causation from each queue */
4212 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4213 		u32 val;
4214 
4215 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4216 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4217 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4218 
4219 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4220 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4221 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4222 
4223 		if (!i40e_enabled_xdp_vsi(vsi))
4224 			continue;
4225 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4226 	}
4227 
4228 	/* disable each interrupt */
4229 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4230 		for (i = vsi->base_vector;
4231 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4232 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4233 
4234 		i40e_flush(hw);
4235 		for (i = 0; i < vsi->num_q_vectors; i++)
4236 			synchronize_irq(pf->msix_entries[i + base].vector);
4237 	} else {
4238 		/* Legacy and MSI mode - this stops all interrupt handling */
4239 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4240 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4241 		i40e_flush(hw);
4242 		synchronize_irq(pf->pdev->irq);
4243 	}
4244 }
4245 
4246 /**
4247  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4248  * @vsi: the VSI being configured
4249  **/
4250 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4251 {
4252 	struct i40e_pf *pf = vsi->back;
4253 	int i;
4254 
4255 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4256 		for (i = 0; i < vsi->num_q_vectors; i++)
4257 			i40e_irq_dynamic_enable(vsi, i);
4258 	} else {
4259 		i40e_irq_dynamic_enable_icr0(pf);
4260 	}
4261 
4262 	i40e_flush(&pf->hw);
4263 	return 0;
4264 }
4265 
4266 /**
4267  * i40e_free_misc_vector - Free the vector that handles non-queue events
4268  * @pf: board private structure
4269  **/
4270 static void i40e_free_misc_vector(struct i40e_pf *pf)
4271 {
4272 	/* Disable ICR 0 */
4273 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4274 	i40e_flush(&pf->hw);
4275 
4276 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4277 		free_irq(pf->msix_entries[0].vector, pf);
4278 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4279 	}
4280 }
4281 
4282 /**
4283  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4284  * @irq: interrupt number
4285  * @data: pointer to a q_vector
4286  *
4287  * This is the handler used for all MSI/Legacy interrupts, and deals
4288  * with both queue and non-queue interrupts.  This is also used in
4289  * MSIX mode to handle the non-queue interrupts.
4290  **/
4291 static irqreturn_t i40e_intr(int irq, void *data)
4292 {
4293 	struct i40e_pf *pf = (struct i40e_pf *)data;
4294 	struct i40e_hw *hw = &pf->hw;
4295 	irqreturn_t ret = IRQ_NONE;
4296 	u32 icr0, icr0_remaining;
4297 	u32 val, ena_mask;
4298 
4299 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4300 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4301 
4302 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4303 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4304 		goto enable_intr;
4305 
4306 	/* if interrupt but no bits showing, must be SWINT */
4307 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4308 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4309 		pf->sw_int_count++;
4310 
4311 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4312 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4313 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4314 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4315 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4316 	}
4317 
4318 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4319 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4320 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4321 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4322 
4323 		/* We do not have a way to disarm Queue causes while leaving
4324 		 * interrupt enabled for all other causes, ideally
4325 		 * interrupt should be disabled while we are in NAPI but
4326 		 * this is not a performance path and napi_schedule()
4327 		 * can deal with rescheduling.
4328 		 */
4329 		if (!test_bit(__I40E_DOWN, pf->state))
4330 			napi_schedule_irqoff(&q_vector->napi);
4331 	}
4332 
4333 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4334 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4335 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4336 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4337 	}
4338 
4339 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4340 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4341 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4342 	}
4343 
4344 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4345 		/* disable any further VFLR event notifications */
4346 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4347 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4348 
4349 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4350 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4351 		} else {
4352 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4353 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4354 		}
4355 	}
4356 
4357 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4358 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4359 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4360 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4361 		val = rd32(hw, I40E_GLGEN_RSTAT);
4362 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4363 		if (val == I40E_RESET_CORER) {
4364 			pf->corer_count++;
4365 		} else if (val == I40E_RESET_GLOBR) {
4366 			pf->globr_count++;
4367 		} else if (val == I40E_RESET_EMPR) {
4368 			pf->empr_count++;
4369 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4370 		}
4371 	}
4372 
4373 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4374 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4375 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4376 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4377 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4378 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4379 	}
4380 
4381 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4382 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4383 
4384 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4385 			schedule_work(&pf->ptp_extts0_work);
4386 
4387 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4388 			i40e_ptp_tx_hwtstamp(pf);
4389 
4390 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4391 	}
4392 
4393 	/* If a critical error is pending we have no choice but to reset the
4394 	 * device.
4395 	 * Report and mask out any remaining unexpected interrupts.
4396 	 */
4397 	icr0_remaining = icr0 & ena_mask;
4398 	if (icr0_remaining) {
4399 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4400 			 icr0_remaining);
4401 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4402 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4403 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4404 			dev_info(&pf->pdev->dev, "device will be reset\n");
4405 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4406 			i40e_service_event_schedule(pf);
4407 		}
4408 		ena_mask &= ~icr0_remaining;
4409 	}
4410 	ret = IRQ_HANDLED;
4411 
4412 enable_intr:
4413 	/* re-enable interrupt causes */
4414 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4415 	if (!test_bit(__I40E_DOWN, pf->state) ||
4416 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4417 		i40e_service_event_schedule(pf);
4418 		i40e_irq_dynamic_enable_icr0(pf);
4419 	}
4420 
4421 	return ret;
4422 }
4423 
4424 /**
4425  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4426  * @tx_ring:  tx ring to clean
4427  * @budget:   how many cleans we're allowed
4428  *
4429  * Returns true if there's any budget left (e.g. the clean is finished)
4430  **/
4431 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4432 {
4433 	struct i40e_vsi *vsi = tx_ring->vsi;
4434 	u16 i = tx_ring->next_to_clean;
4435 	struct i40e_tx_buffer *tx_buf;
4436 	struct i40e_tx_desc *tx_desc;
4437 
4438 	tx_buf = &tx_ring->tx_bi[i];
4439 	tx_desc = I40E_TX_DESC(tx_ring, i);
4440 	i -= tx_ring->count;
4441 
4442 	do {
4443 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4444 
4445 		/* if next_to_watch is not set then there is no work pending */
4446 		if (!eop_desc)
4447 			break;
4448 
4449 		/* prevent any other reads prior to eop_desc */
4450 		smp_rmb();
4451 
4452 		/* if the descriptor isn't done, no work yet to do */
4453 		if (!(eop_desc->cmd_type_offset_bsz &
4454 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4455 			break;
4456 
4457 		/* clear next_to_watch to prevent false hangs */
4458 		tx_buf->next_to_watch = NULL;
4459 
4460 		tx_desc->buffer_addr = 0;
4461 		tx_desc->cmd_type_offset_bsz = 0;
4462 		/* move past filter desc */
4463 		tx_buf++;
4464 		tx_desc++;
4465 		i++;
4466 		if (unlikely(!i)) {
4467 			i -= tx_ring->count;
4468 			tx_buf = tx_ring->tx_bi;
4469 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4470 		}
4471 		/* unmap skb header data */
4472 		dma_unmap_single(tx_ring->dev,
4473 				 dma_unmap_addr(tx_buf, dma),
4474 				 dma_unmap_len(tx_buf, len),
4475 				 DMA_TO_DEVICE);
4476 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4477 			kfree(tx_buf->raw_buf);
4478 
4479 		tx_buf->raw_buf = NULL;
4480 		tx_buf->tx_flags = 0;
4481 		tx_buf->next_to_watch = NULL;
4482 		dma_unmap_len_set(tx_buf, len, 0);
4483 		tx_desc->buffer_addr = 0;
4484 		tx_desc->cmd_type_offset_bsz = 0;
4485 
4486 		/* move us past the eop_desc for start of next FD desc */
4487 		tx_buf++;
4488 		tx_desc++;
4489 		i++;
4490 		if (unlikely(!i)) {
4491 			i -= tx_ring->count;
4492 			tx_buf = tx_ring->tx_bi;
4493 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4494 		}
4495 
4496 		/* update budget accounting */
4497 		budget--;
4498 	} while (likely(budget));
4499 
4500 	i += tx_ring->count;
4501 	tx_ring->next_to_clean = i;
4502 
4503 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4504 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4505 
4506 	return budget > 0;
4507 }
4508 
4509 /**
4510  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4511  * @irq: interrupt number
4512  * @data: pointer to a q_vector
4513  **/
4514 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4515 {
4516 	struct i40e_q_vector *q_vector = data;
4517 	struct i40e_vsi *vsi;
4518 
4519 	if (!q_vector->tx.ring)
4520 		return IRQ_HANDLED;
4521 
4522 	vsi = q_vector->tx.ring->vsi;
4523 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4524 
4525 	return IRQ_HANDLED;
4526 }
4527 
4528 /**
4529  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4530  * @vsi: the VSI being configured
4531  * @v_idx: vector index
4532  * @qp_idx: queue pair index
4533  **/
4534 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4535 {
4536 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4537 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4538 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4539 
4540 	tx_ring->q_vector = q_vector;
4541 	tx_ring->next = q_vector->tx.ring;
4542 	q_vector->tx.ring = tx_ring;
4543 	q_vector->tx.count++;
4544 
4545 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4546 	if (i40e_enabled_xdp_vsi(vsi)) {
4547 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4548 
4549 		xdp_ring->q_vector = q_vector;
4550 		xdp_ring->next = q_vector->tx.ring;
4551 		q_vector->tx.ring = xdp_ring;
4552 		q_vector->tx.count++;
4553 	}
4554 
4555 	rx_ring->q_vector = q_vector;
4556 	rx_ring->next = q_vector->rx.ring;
4557 	q_vector->rx.ring = rx_ring;
4558 	q_vector->rx.count++;
4559 }
4560 
4561 /**
4562  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4563  * @vsi: the VSI being configured
4564  *
4565  * This function maps descriptor rings to the queue-specific vectors
4566  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4567  * one vector per queue pair, but on a constrained vector budget, we
4568  * group the queue pairs as "efficiently" as possible.
4569  **/
4570 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4571 {
4572 	int qp_remaining = vsi->num_queue_pairs;
4573 	int q_vectors = vsi->num_q_vectors;
4574 	int num_ringpairs;
4575 	int v_start = 0;
4576 	int qp_idx = 0;
4577 
4578 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4579 	 * group them so there are multiple queues per vector.
4580 	 * It is also important to go through all the vectors available to be
4581 	 * sure that if we don't use all the vectors, that the remaining vectors
4582 	 * are cleared. This is especially important when decreasing the
4583 	 * number of queues in use.
4584 	 */
4585 	for (; v_start < q_vectors; v_start++) {
4586 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4587 
4588 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4589 
4590 		q_vector->num_ringpairs = num_ringpairs;
4591 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4592 
4593 		q_vector->rx.count = 0;
4594 		q_vector->tx.count = 0;
4595 		q_vector->rx.ring = NULL;
4596 		q_vector->tx.ring = NULL;
4597 
4598 		while (num_ringpairs--) {
4599 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4600 			qp_idx++;
4601 			qp_remaining--;
4602 		}
4603 	}
4604 }
4605 
4606 /**
4607  * i40e_vsi_request_irq - Request IRQ from the OS
4608  * @vsi: the VSI being configured
4609  * @basename: name for the vector
4610  **/
4611 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4612 {
4613 	struct i40e_pf *pf = vsi->back;
4614 	int err;
4615 
4616 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4617 		err = i40e_vsi_request_irq_msix(vsi, basename);
4618 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4619 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4620 				  pf->int_name, pf);
4621 	else
4622 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4623 				  pf->int_name, pf);
4624 
4625 	if (err)
4626 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4627 
4628 	return err;
4629 }
4630 
4631 #ifdef CONFIG_NET_POLL_CONTROLLER
4632 /**
4633  * i40e_netpoll - A Polling 'interrupt' handler
4634  * @netdev: network interface device structure
4635  *
4636  * This is used by netconsole to send skbs without having to re-enable
4637  * interrupts.  It's not called while the normal interrupt routine is executing.
4638  **/
4639 static void i40e_netpoll(struct net_device *netdev)
4640 {
4641 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4642 	struct i40e_vsi *vsi = np->vsi;
4643 	struct i40e_pf *pf = vsi->back;
4644 	int i;
4645 
4646 	/* if interface is down do nothing */
4647 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4648 		return;
4649 
4650 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4651 		for (i = 0; i < vsi->num_q_vectors; i++)
4652 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4653 	} else {
4654 		i40e_intr(pf->pdev->irq, netdev);
4655 	}
4656 }
4657 #endif
4658 
4659 #define I40E_QTX_ENA_WAIT_COUNT 50
4660 
4661 /**
4662  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4663  * @pf: the PF being configured
4664  * @pf_q: the PF queue
4665  * @enable: enable or disable state of the queue
4666  *
4667  * This routine will wait for the given Tx queue of the PF to reach the
4668  * enabled or disabled state.
4669  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4670  * multiple retries; else will return 0 in case of success.
4671  **/
4672 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4673 {
4674 	int i;
4675 	u32 tx_reg;
4676 
4677 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4678 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4679 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4680 			break;
4681 
4682 		usleep_range(10, 20);
4683 	}
4684 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4685 		return -ETIMEDOUT;
4686 
4687 	return 0;
4688 }
4689 
4690 /**
4691  * i40e_control_tx_q - Start or stop a particular Tx queue
4692  * @pf: the PF structure
4693  * @pf_q: the PF queue to configure
4694  * @enable: start or stop the queue
4695  *
4696  * This function enables or disables a single queue. Note that any delay
4697  * required after the operation is expected to be handled by the caller of
4698  * this function.
4699  **/
4700 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4701 {
4702 	struct i40e_hw *hw = &pf->hw;
4703 	u32 tx_reg;
4704 	int i;
4705 
4706 	/* warn the TX unit of coming changes */
4707 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4708 	if (!enable)
4709 		usleep_range(10, 20);
4710 
4711 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4712 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4713 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4714 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4715 			break;
4716 		usleep_range(1000, 2000);
4717 	}
4718 
4719 	/* Skip if the queue is already in the requested state */
4720 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4721 		return;
4722 
4723 	/* turn on/off the queue */
4724 	if (enable) {
4725 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4726 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4727 	} else {
4728 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4729 	}
4730 
4731 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4732 }
4733 
4734 /**
4735  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4736  * @seid: VSI SEID
4737  * @pf: the PF structure
4738  * @pf_q: the PF queue to configure
4739  * @is_xdp: true if the queue is used for XDP
4740  * @enable: start or stop the queue
4741  **/
4742 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4743 			   bool is_xdp, bool enable)
4744 {
4745 	int ret;
4746 
4747 	i40e_control_tx_q(pf, pf_q, enable);
4748 
4749 	/* wait for the change to finish */
4750 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4751 	if (ret) {
4752 		dev_info(&pf->pdev->dev,
4753 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4754 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4755 			 (enable ? "en" : "dis"));
4756 	}
4757 
4758 	return ret;
4759 }
4760 
4761 /**
4762  * i40e_vsi_enable_tx - Start a VSI's rings
4763  * @vsi: the VSI being configured
4764  **/
4765 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4766 {
4767 	struct i40e_pf *pf = vsi->back;
4768 	int i, pf_q, ret = 0;
4769 
4770 	pf_q = vsi->base_queue;
4771 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4772 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4773 					     pf_q,
4774 					     false /*is xdp*/, true);
4775 		if (ret)
4776 			break;
4777 
4778 		if (!i40e_enabled_xdp_vsi(vsi))
4779 			continue;
4780 
4781 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4782 					     pf_q + vsi->alloc_queue_pairs,
4783 					     true /*is xdp*/, true);
4784 		if (ret)
4785 			break;
4786 	}
4787 	return ret;
4788 }
4789 
4790 /**
4791  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4792  * @pf: the PF being configured
4793  * @pf_q: the PF queue
4794  * @enable: enable or disable state of the queue
4795  *
4796  * This routine will wait for the given Rx queue of the PF to reach the
4797  * enabled or disabled state.
4798  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4799  * multiple retries; else will return 0 in case of success.
4800  **/
4801 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4802 {
4803 	int i;
4804 	u32 rx_reg;
4805 
4806 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4807 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4808 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4809 			break;
4810 
4811 		usleep_range(10, 20);
4812 	}
4813 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4814 		return -ETIMEDOUT;
4815 
4816 	return 0;
4817 }
4818 
4819 /**
4820  * i40e_control_rx_q - Start or stop a particular Rx queue
4821  * @pf: the PF structure
4822  * @pf_q: the PF queue to configure
4823  * @enable: start or stop the queue
4824  *
4825  * This function enables or disables a single queue. Note that
4826  * any delay required after the operation is expected to be
4827  * handled by the caller of this function.
4828  **/
4829 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4830 {
4831 	struct i40e_hw *hw = &pf->hw;
4832 	u32 rx_reg;
4833 	int i;
4834 
4835 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4836 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4837 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4838 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4839 			break;
4840 		usleep_range(1000, 2000);
4841 	}
4842 
4843 	/* Skip if the queue is already in the requested state */
4844 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4845 		return;
4846 
4847 	/* turn on/off the queue */
4848 	if (enable)
4849 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4850 	else
4851 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4852 
4853 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4854 }
4855 
4856 /**
4857  * i40e_control_wait_rx_q
4858  * @pf: the PF structure
4859  * @pf_q: queue being configured
4860  * @enable: start or stop the rings
4861  *
4862  * This function enables or disables a single queue along with waiting
4863  * for the change to finish. The caller of this function should handle
4864  * the delays needed in the case of disabling queues.
4865  **/
4866 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4867 {
4868 	int ret = 0;
4869 
4870 	i40e_control_rx_q(pf, pf_q, enable);
4871 
4872 	/* wait for the change to finish */
4873 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4874 	if (ret)
4875 		return ret;
4876 
4877 	return ret;
4878 }
4879 
4880 /**
4881  * i40e_vsi_enable_rx - Start a VSI's rings
4882  * @vsi: the VSI being configured
4883  **/
4884 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4885 {
4886 	struct i40e_pf *pf = vsi->back;
4887 	int i, pf_q, ret = 0;
4888 
4889 	pf_q = vsi->base_queue;
4890 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4891 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4892 		if (ret) {
4893 			dev_info(&pf->pdev->dev,
4894 				 "VSI seid %d Rx ring %d enable timeout\n",
4895 				 vsi->seid, pf_q);
4896 			break;
4897 		}
4898 	}
4899 
4900 	return ret;
4901 }
4902 
4903 /**
4904  * i40e_vsi_start_rings - Start a VSI's rings
4905  * @vsi: the VSI being configured
4906  **/
4907 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4908 {
4909 	int ret = 0;
4910 
4911 	/* do rx first for enable and last for disable */
4912 	ret = i40e_vsi_enable_rx(vsi);
4913 	if (ret)
4914 		return ret;
4915 	ret = i40e_vsi_enable_tx(vsi);
4916 
4917 	return ret;
4918 }
4919 
4920 #define I40E_DISABLE_TX_GAP_MSEC	50
4921 
4922 /**
4923  * i40e_vsi_stop_rings - Stop a VSI's rings
4924  * @vsi: the VSI being configured
4925  **/
4926 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4927 {
4928 	struct i40e_pf *pf = vsi->back;
4929 	int pf_q, err, q_end;
4930 
4931 	/* When port TX is suspended, don't wait */
4932 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4933 		return i40e_vsi_stop_rings_no_wait(vsi);
4934 
4935 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4936 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4937 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4938 
4939 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4940 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4941 		if (err)
4942 			dev_info(&pf->pdev->dev,
4943 				 "VSI seid %d Rx ring %d disable timeout\n",
4944 				 vsi->seid, pf_q);
4945 	}
4946 
4947 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4948 	pf_q = vsi->base_queue;
4949 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4950 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4951 
4952 	i40e_vsi_wait_queues_disabled(vsi);
4953 }
4954 
4955 /**
4956  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4957  * @vsi: the VSI being shutdown
4958  *
4959  * This function stops all the rings for a VSI but does not delay to verify
4960  * that rings have been disabled. It is expected that the caller is shutting
4961  * down multiple VSIs at once and will delay together for all the VSIs after
4962  * initiating the shutdown. This is particularly useful for shutting down lots
4963  * of VFs together. Otherwise, a large delay can be incurred while configuring
4964  * each VSI in serial.
4965  **/
4966 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4967 {
4968 	struct i40e_pf *pf = vsi->back;
4969 	int i, pf_q;
4970 
4971 	pf_q = vsi->base_queue;
4972 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4973 		i40e_control_tx_q(pf, pf_q, false);
4974 		i40e_control_rx_q(pf, pf_q, false);
4975 	}
4976 }
4977 
4978 /**
4979  * i40e_vsi_free_irq - Free the irq association with the OS
4980  * @vsi: the VSI being configured
4981  **/
4982 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4983 {
4984 	struct i40e_pf *pf = vsi->back;
4985 	struct i40e_hw *hw = &pf->hw;
4986 	int base = vsi->base_vector;
4987 	u32 val, qp;
4988 	int i;
4989 
4990 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4991 		if (!vsi->q_vectors)
4992 			return;
4993 
4994 		if (!vsi->irqs_ready)
4995 			return;
4996 
4997 		vsi->irqs_ready = false;
4998 		for (i = 0; i < vsi->num_q_vectors; i++) {
4999 			int irq_num;
5000 			u16 vector;
5001 
5002 			vector = i + base;
5003 			irq_num = pf->msix_entries[vector].vector;
5004 
5005 			/* free only the irqs that were actually requested */
5006 			if (!vsi->q_vectors[i] ||
5007 			    !vsi->q_vectors[i]->num_ringpairs)
5008 				continue;
5009 
5010 			/* clear the affinity notifier in the IRQ descriptor */
5011 			irq_set_affinity_notifier(irq_num, NULL);
5012 			/* remove our suggested affinity mask for this IRQ */
5013 			irq_update_affinity_hint(irq_num, NULL);
5014 			free_irq(irq_num, vsi->q_vectors[i]);
5015 
5016 			/* Tear down the interrupt queue link list
5017 			 *
5018 			 * We know that they come in pairs and always
5019 			 * the Rx first, then the Tx.  To clear the
5020 			 * link list, stick the EOL value into the
5021 			 * next_q field of the registers.
5022 			 */
5023 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5024 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
5025 				       val);
5026 			val |= I40E_QUEUE_END_OF_LIST
5027 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5028 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5029 
5030 			while (qp != I40E_QUEUE_END_OF_LIST) {
5031 				u32 next;
5032 
5033 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5034 
5035 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5036 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5037 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5038 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5039 
5040 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5041 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5042 
5043 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5044 
5045 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5046 
5047 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5048 						 val);
5049 
5050 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5051 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5052 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5053 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5054 
5055 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5056 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5057 
5058 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5059 				qp = next;
5060 			}
5061 		}
5062 	} else {
5063 		free_irq(pf->pdev->irq, pf);
5064 
5065 		val = rd32(hw, I40E_PFINT_LNKLST0);
5066 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5067 		val |= I40E_QUEUE_END_OF_LIST
5068 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5069 		wr32(hw, I40E_PFINT_LNKLST0, val);
5070 
5071 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5072 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5073 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5074 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5075 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5076 
5077 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5078 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5079 
5080 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5081 
5082 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5083 
5084 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5085 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5086 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5087 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5088 
5089 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5090 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5091 
5092 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5093 	}
5094 }
5095 
5096 /**
5097  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5098  * @vsi: the VSI being configured
5099  * @v_idx: Index of vector to be freed
5100  *
5101  * This function frees the memory allocated to the q_vector.  In addition if
5102  * NAPI is enabled it will delete any references to the NAPI struct prior
5103  * to freeing the q_vector.
5104  **/
5105 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5106 {
5107 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5108 	struct i40e_ring *ring;
5109 
5110 	if (!q_vector)
5111 		return;
5112 
5113 	/* disassociate q_vector from rings */
5114 	i40e_for_each_ring(ring, q_vector->tx)
5115 		ring->q_vector = NULL;
5116 
5117 	i40e_for_each_ring(ring, q_vector->rx)
5118 		ring->q_vector = NULL;
5119 
5120 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5121 	if (vsi->netdev)
5122 		netif_napi_del(&q_vector->napi);
5123 
5124 	vsi->q_vectors[v_idx] = NULL;
5125 
5126 	kfree_rcu(q_vector, rcu);
5127 }
5128 
5129 /**
5130  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5131  * @vsi: the VSI being un-configured
5132  *
5133  * This frees the memory allocated to the q_vectors and
5134  * deletes references to the NAPI struct.
5135  **/
5136 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5137 {
5138 	int v_idx;
5139 
5140 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5141 		i40e_free_q_vector(vsi, v_idx);
5142 }
5143 
5144 /**
5145  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5146  * @pf: board private structure
5147  **/
5148 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5149 {
5150 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5151 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5152 		pci_disable_msix(pf->pdev);
5153 		kfree(pf->msix_entries);
5154 		pf->msix_entries = NULL;
5155 		kfree(pf->irq_pile);
5156 		pf->irq_pile = NULL;
5157 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5158 		pci_disable_msi(pf->pdev);
5159 	}
5160 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5161 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5162 }
5163 
5164 /**
5165  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5166  * @pf: board private structure
5167  *
5168  * We go through and clear interrupt specific resources and reset the structure
5169  * to pre-load conditions
5170  **/
5171 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5172 {
5173 	int i;
5174 
5175 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5176 		i40e_free_misc_vector(pf);
5177 
5178 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5179 		      I40E_IWARP_IRQ_PILE_ID);
5180 
5181 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5182 	for (i = 0; i < pf->num_alloc_vsi; i++)
5183 		if (pf->vsi[i])
5184 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5185 	i40e_reset_interrupt_capability(pf);
5186 }
5187 
5188 /**
5189  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5190  * @vsi: the VSI being configured
5191  **/
5192 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5193 {
5194 	int q_idx;
5195 
5196 	if (!vsi->netdev)
5197 		return;
5198 
5199 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5200 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5201 
5202 		if (q_vector->rx.ring || q_vector->tx.ring)
5203 			napi_enable(&q_vector->napi);
5204 	}
5205 }
5206 
5207 /**
5208  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5209  * @vsi: the VSI being configured
5210  **/
5211 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5212 {
5213 	int q_idx;
5214 
5215 	if (!vsi->netdev)
5216 		return;
5217 
5218 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5219 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5220 
5221 		if (q_vector->rx.ring || q_vector->tx.ring)
5222 			napi_disable(&q_vector->napi);
5223 	}
5224 }
5225 
5226 /**
5227  * i40e_vsi_close - Shut down a VSI
5228  * @vsi: the vsi to be quelled
5229  **/
5230 static void i40e_vsi_close(struct i40e_vsi *vsi)
5231 {
5232 	struct i40e_pf *pf = vsi->back;
5233 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5234 		i40e_down(vsi);
5235 	i40e_vsi_free_irq(vsi);
5236 	i40e_vsi_free_tx_resources(vsi);
5237 	i40e_vsi_free_rx_resources(vsi);
5238 	vsi->current_netdev_flags = 0;
5239 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5240 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5241 		set_bit(__I40E_CLIENT_RESET, pf->state);
5242 }
5243 
5244 /**
5245  * i40e_quiesce_vsi - Pause a given VSI
5246  * @vsi: the VSI being paused
5247  **/
5248 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5249 {
5250 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5251 		return;
5252 
5253 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5254 	if (vsi->netdev && netif_running(vsi->netdev))
5255 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5256 	else
5257 		i40e_vsi_close(vsi);
5258 }
5259 
5260 /**
5261  * i40e_unquiesce_vsi - Resume a given VSI
5262  * @vsi: the VSI being resumed
5263  **/
5264 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5265 {
5266 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5267 		return;
5268 
5269 	if (vsi->netdev && netif_running(vsi->netdev))
5270 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5271 	else
5272 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5273 }
5274 
5275 /**
5276  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5277  * @pf: the PF
5278  **/
5279 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5280 {
5281 	int v;
5282 
5283 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5284 		if (pf->vsi[v])
5285 			i40e_quiesce_vsi(pf->vsi[v]);
5286 	}
5287 }
5288 
5289 /**
5290  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5291  * @pf: the PF
5292  **/
5293 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5294 {
5295 	int v;
5296 
5297 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5298 		if (pf->vsi[v])
5299 			i40e_unquiesce_vsi(pf->vsi[v]);
5300 	}
5301 }
5302 
5303 /**
5304  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5305  * @vsi: the VSI being configured
5306  *
5307  * Wait until all queues on a given VSI have been disabled.
5308  **/
5309 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5310 {
5311 	struct i40e_pf *pf = vsi->back;
5312 	int i, pf_q, ret;
5313 
5314 	pf_q = vsi->base_queue;
5315 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5316 		/* Check and wait for the Tx queue */
5317 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5318 		if (ret) {
5319 			dev_info(&pf->pdev->dev,
5320 				 "VSI seid %d Tx ring %d disable timeout\n",
5321 				 vsi->seid, pf_q);
5322 			return ret;
5323 		}
5324 
5325 		if (!i40e_enabled_xdp_vsi(vsi))
5326 			goto wait_rx;
5327 
5328 		/* Check and wait for the XDP Tx queue */
5329 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5330 				       false);
5331 		if (ret) {
5332 			dev_info(&pf->pdev->dev,
5333 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5334 				 vsi->seid, pf_q);
5335 			return ret;
5336 		}
5337 wait_rx:
5338 		/* Check and wait for the Rx queue */
5339 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5340 		if (ret) {
5341 			dev_info(&pf->pdev->dev,
5342 				 "VSI seid %d Rx ring %d disable timeout\n",
5343 				 vsi->seid, pf_q);
5344 			return ret;
5345 		}
5346 	}
5347 
5348 	return 0;
5349 }
5350 
5351 #ifdef CONFIG_I40E_DCB
5352 /**
5353  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5354  * @pf: the PF
5355  *
5356  * This function waits for the queues to be in disabled state for all the
5357  * VSIs that are managed by this PF.
5358  **/
5359 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5360 {
5361 	int v, ret = 0;
5362 
5363 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5364 		if (pf->vsi[v]) {
5365 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5366 			if (ret)
5367 				break;
5368 		}
5369 	}
5370 
5371 	return ret;
5372 }
5373 
5374 #endif
5375 
5376 /**
5377  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5378  * @pf: pointer to PF
5379  *
5380  * Get TC map for ISCSI PF type that will include iSCSI TC
5381  * and LAN TC.
5382  **/
5383 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5384 {
5385 	struct i40e_dcb_app_priority_table app;
5386 	struct i40e_hw *hw = &pf->hw;
5387 	u8 enabled_tc = 1; /* TC0 is always enabled */
5388 	u8 tc, i;
5389 	/* Get the iSCSI APP TLV */
5390 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5391 
5392 	for (i = 0; i < dcbcfg->numapps; i++) {
5393 		app = dcbcfg->app[i];
5394 		if (app.selector == I40E_APP_SEL_TCPIP &&
5395 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5396 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5397 			enabled_tc |= BIT(tc);
5398 			break;
5399 		}
5400 	}
5401 
5402 	return enabled_tc;
5403 }
5404 
5405 /**
5406  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5407  * @dcbcfg: the corresponding DCBx configuration structure
5408  *
5409  * Return the number of TCs from given DCBx configuration
5410  **/
5411 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5412 {
5413 	int i, tc_unused = 0;
5414 	u8 num_tc = 0;
5415 	u8 ret = 0;
5416 
5417 	/* Scan the ETS Config Priority Table to find
5418 	 * traffic class enabled for a given priority
5419 	 * and create a bitmask of enabled TCs
5420 	 */
5421 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5422 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5423 
5424 	/* Now scan the bitmask to check for
5425 	 * contiguous TCs starting with TC0
5426 	 */
5427 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5428 		if (num_tc & BIT(i)) {
5429 			if (!tc_unused) {
5430 				ret++;
5431 			} else {
5432 				pr_err("Non-contiguous TC - Disabling DCB\n");
5433 				return 1;
5434 			}
5435 		} else {
5436 			tc_unused = 1;
5437 		}
5438 	}
5439 
5440 	/* There is always at least TC0 */
5441 	if (!ret)
5442 		ret = 1;
5443 
5444 	return ret;
5445 }
5446 
5447 /**
5448  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5449  * @dcbcfg: the corresponding DCBx configuration structure
5450  *
5451  * Query the current DCB configuration and return the number of
5452  * traffic classes enabled from the given DCBX config
5453  **/
5454 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5455 {
5456 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5457 	u8 enabled_tc = 1;
5458 	u8 i;
5459 
5460 	for (i = 0; i < num_tc; i++)
5461 		enabled_tc |= BIT(i);
5462 
5463 	return enabled_tc;
5464 }
5465 
5466 /**
5467  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5468  * @pf: PF being queried
5469  *
5470  * Query the current MQPRIO configuration and return the number of
5471  * traffic classes enabled.
5472  **/
5473 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5474 {
5475 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5476 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5477 	u8 enabled_tc = 1, i;
5478 
5479 	for (i = 1; i < num_tc; i++)
5480 		enabled_tc |= BIT(i);
5481 	return enabled_tc;
5482 }
5483 
5484 /**
5485  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5486  * @pf: PF being queried
5487  *
5488  * Return number of traffic classes enabled for the given PF
5489  **/
5490 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5491 {
5492 	struct i40e_hw *hw = &pf->hw;
5493 	u8 i, enabled_tc = 1;
5494 	u8 num_tc = 0;
5495 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5496 
5497 	if (i40e_is_tc_mqprio_enabled(pf))
5498 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5499 
5500 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5501 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5502 		return 1;
5503 
5504 	/* SFP mode will be enabled for all TCs on port */
5505 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5506 		return i40e_dcb_get_num_tc(dcbcfg);
5507 
5508 	/* MFP mode return count of enabled TCs for this PF */
5509 	if (pf->hw.func_caps.iscsi)
5510 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5511 	else
5512 		return 1; /* Only TC0 */
5513 
5514 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5515 		if (enabled_tc & BIT(i))
5516 			num_tc++;
5517 	}
5518 	return num_tc;
5519 }
5520 
5521 /**
5522  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5523  * @pf: PF being queried
5524  *
5525  * Return a bitmap for enabled traffic classes for this PF.
5526  **/
5527 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5528 {
5529 	if (i40e_is_tc_mqprio_enabled(pf))
5530 		return i40e_mqprio_get_enabled_tc(pf);
5531 
5532 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5533 	 * default TC
5534 	 */
5535 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5536 		return I40E_DEFAULT_TRAFFIC_CLASS;
5537 
5538 	/* SFP mode we want PF to be enabled for all TCs */
5539 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5540 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5541 
5542 	/* MFP enabled and iSCSI PF type */
5543 	if (pf->hw.func_caps.iscsi)
5544 		return i40e_get_iscsi_tc_map(pf);
5545 	else
5546 		return I40E_DEFAULT_TRAFFIC_CLASS;
5547 }
5548 
5549 /**
5550  * i40e_vsi_get_bw_info - Query VSI BW Information
5551  * @vsi: the VSI being queried
5552  *
5553  * Returns 0 on success, negative value on failure
5554  **/
5555 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5556 {
5557 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5558 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5559 	struct i40e_pf *pf = vsi->back;
5560 	struct i40e_hw *hw = &pf->hw;
5561 	u32 tc_bw_max;
5562 	int ret;
5563 	int i;
5564 
5565 	/* Get the VSI level BW configuration */
5566 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5567 	if (ret) {
5568 		dev_info(&pf->pdev->dev,
5569 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5570 			 ERR_PTR(ret),
5571 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5572 		return -EINVAL;
5573 	}
5574 
5575 	/* Get the VSI level BW configuration per TC */
5576 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5577 					       NULL);
5578 	if (ret) {
5579 		dev_info(&pf->pdev->dev,
5580 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5581 			 ERR_PTR(ret),
5582 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5583 		return -EINVAL;
5584 	}
5585 
5586 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5587 		dev_info(&pf->pdev->dev,
5588 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5589 			 bw_config.tc_valid_bits,
5590 			 bw_ets_config.tc_valid_bits);
5591 		/* Still continuing */
5592 	}
5593 
5594 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5595 	vsi->bw_max_quanta = bw_config.max_bw;
5596 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5597 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5598 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5599 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5600 		vsi->bw_ets_limit_credits[i] =
5601 					le16_to_cpu(bw_ets_config.credits[i]);
5602 		/* 3 bits out of 4 for each TC */
5603 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5604 	}
5605 
5606 	return 0;
5607 }
5608 
5609 /**
5610  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5611  * @vsi: the VSI being configured
5612  * @enabled_tc: TC bitmap
5613  * @bw_share: BW shared credits per TC
5614  *
5615  * Returns 0 on success, negative value on failure
5616  **/
5617 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5618 				       u8 *bw_share)
5619 {
5620 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5621 	struct i40e_pf *pf = vsi->back;
5622 	int ret;
5623 	int i;
5624 
5625 	/* There is no need to reset BW when mqprio mode is on.  */
5626 	if (i40e_is_tc_mqprio_enabled(pf))
5627 		return 0;
5628 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5629 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5630 		if (ret)
5631 			dev_info(&pf->pdev->dev,
5632 				 "Failed to reset tx rate for vsi->seid %u\n",
5633 				 vsi->seid);
5634 		return ret;
5635 	}
5636 	memset(&bw_data, 0, sizeof(bw_data));
5637 	bw_data.tc_valid_bits = enabled_tc;
5638 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5639 		bw_data.tc_bw_credits[i] = bw_share[i];
5640 
5641 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5642 	if (ret) {
5643 		dev_info(&pf->pdev->dev,
5644 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5645 			 pf->hw.aq.asq_last_status);
5646 		return -EINVAL;
5647 	}
5648 
5649 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5650 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5651 
5652 	return 0;
5653 }
5654 
5655 /**
5656  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5657  * @vsi: the VSI being configured
5658  * @enabled_tc: TC map to be enabled
5659  *
5660  **/
5661 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5662 {
5663 	struct net_device *netdev = vsi->netdev;
5664 	struct i40e_pf *pf = vsi->back;
5665 	struct i40e_hw *hw = &pf->hw;
5666 	u8 netdev_tc = 0;
5667 	int i;
5668 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5669 
5670 	if (!netdev)
5671 		return;
5672 
5673 	if (!enabled_tc) {
5674 		netdev_reset_tc(netdev);
5675 		return;
5676 	}
5677 
5678 	/* Set up actual enabled TCs on the VSI */
5679 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5680 		return;
5681 
5682 	/* set per TC queues for the VSI */
5683 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5684 		/* Only set TC queues for enabled tcs
5685 		 *
5686 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5687 		 * enabled_tc bitmap would be 0x00001001; the driver
5688 		 * will set the numtc for netdev as 2 that will be
5689 		 * referenced by the netdev layer as TC 0 and 1.
5690 		 */
5691 		if (vsi->tc_config.enabled_tc & BIT(i))
5692 			netdev_set_tc_queue(netdev,
5693 					vsi->tc_config.tc_info[i].netdev_tc,
5694 					vsi->tc_config.tc_info[i].qcount,
5695 					vsi->tc_config.tc_info[i].qoffset);
5696 	}
5697 
5698 	if (i40e_is_tc_mqprio_enabled(pf))
5699 		return;
5700 
5701 	/* Assign UP2TC map for the VSI */
5702 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5703 		/* Get the actual TC# for the UP */
5704 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5705 		/* Get the mapped netdev TC# for the UP */
5706 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5707 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5708 	}
5709 }
5710 
5711 /**
5712  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5713  * @vsi: the VSI being configured
5714  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5715  **/
5716 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5717 				      struct i40e_vsi_context *ctxt)
5718 {
5719 	/* copy just the sections touched not the entire info
5720 	 * since not all sections are valid as returned by
5721 	 * update vsi params
5722 	 */
5723 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5724 	memcpy(&vsi->info.queue_mapping,
5725 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5726 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5727 	       sizeof(vsi->info.tc_mapping));
5728 }
5729 
5730 /**
5731  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5732  * @vsi: the VSI being reconfigured
5733  * @vsi_offset: offset from main VF VSI
5734  */
5735 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5736 {
5737 	struct i40e_vsi_context ctxt = {};
5738 	struct i40e_pf *pf;
5739 	struct i40e_hw *hw;
5740 	int ret;
5741 
5742 	if (!vsi)
5743 		return -EINVAL;
5744 	pf = vsi->back;
5745 	hw = &pf->hw;
5746 
5747 	ctxt.seid = vsi->seid;
5748 	ctxt.pf_num = hw->pf_id;
5749 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5750 	ctxt.uplink_seid = vsi->uplink_seid;
5751 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5752 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5753 	ctxt.info = vsi->info;
5754 
5755 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5756 				 false);
5757 	if (vsi->reconfig_rss) {
5758 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5759 				      vsi->num_queue_pairs);
5760 		ret = i40e_vsi_config_rss(vsi);
5761 		if (ret) {
5762 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5763 			return ret;
5764 		}
5765 		vsi->reconfig_rss = false;
5766 	}
5767 
5768 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5769 	if (ret) {
5770 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5771 			 ERR_PTR(ret),
5772 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5773 		return ret;
5774 	}
5775 	/* update the local VSI info with updated queue map */
5776 	i40e_vsi_update_queue_map(vsi, &ctxt);
5777 	vsi->info.valid_sections = 0;
5778 
5779 	return ret;
5780 }
5781 
5782 /**
5783  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5784  * @vsi: VSI to be configured
5785  * @enabled_tc: TC bitmap
5786  *
5787  * This configures a particular VSI for TCs that are mapped to the
5788  * given TC bitmap. It uses default bandwidth share for TCs across
5789  * VSIs to configure TC for a particular VSI.
5790  *
5791  * NOTE:
5792  * It is expected that the VSI queues have been quisced before calling
5793  * this function.
5794  **/
5795 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5796 {
5797 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5798 	struct i40e_pf *pf = vsi->back;
5799 	struct i40e_hw *hw = &pf->hw;
5800 	struct i40e_vsi_context ctxt;
5801 	int ret = 0;
5802 	int i;
5803 
5804 	/* Check if enabled_tc is same as existing or new TCs */
5805 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5806 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5807 		return ret;
5808 
5809 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5810 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5811 		if (enabled_tc & BIT(i))
5812 			bw_share[i] = 1;
5813 	}
5814 
5815 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5816 	if (ret) {
5817 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5818 
5819 		dev_info(&pf->pdev->dev,
5820 			 "Failed configuring TC map %d for VSI %d\n",
5821 			 enabled_tc, vsi->seid);
5822 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5823 						  &bw_config, NULL);
5824 		if (ret) {
5825 			dev_info(&pf->pdev->dev,
5826 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5827 				 ERR_PTR(ret),
5828 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5829 			goto out;
5830 		}
5831 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5832 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5833 
5834 			if (!valid_tc)
5835 				valid_tc = bw_config.tc_valid_bits;
5836 			/* Always enable TC0, no matter what */
5837 			valid_tc |= 1;
5838 			dev_info(&pf->pdev->dev,
5839 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5840 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5841 			enabled_tc = valid_tc;
5842 		}
5843 
5844 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5845 		if (ret) {
5846 			dev_err(&pf->pdev->dev,
5847 				"Unable to  configure TC map %d for VSI %d\n",
5848 				enabled_tc, vsi->seid);
5849 			goto out;
5850 		}
5851 	}
5852 
5853 	/* Update Queue Pairs Mapping for currently enabled UPs */
5854 	ctxt.seid = vsi->seid;
5855 	ctxt.pf_num = vsi->back->hw.pf_id;
5856 	ctxt.vf_num = 0;
5857 	ctxt.uplink_seid = vsi->uplink_seid;
5858 	ctxt.info = vsi->info;
5859 	if (i40e_is_tc_mqprio_enabled(pf)) {
5860 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5861 		if (ret)
5862 			goto out;
5863 	} else {
5864 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5865 	}
5866 
5867 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5868 	 * queues changed.
5869 	 */
5870 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5871 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5872 				      vsi->num_queue_pairs);
5873 		ret = i40e_vsi_config_rss(vsi);
5874 		if (ret) {
5875 			dev_info(&vsi->back->pdev->dev,
5876 				 "Failed to reconfig rss for num_queues\n");
5877 			return ret;
5878 		}
5879 		vsi->reconfig_rss = false;
5880 	}
5881 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5882 		ctxt.info.valid_sections |=
5883 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5884 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5885 	}
5886 
5887 	/* Update the VSI after updating the VSI queue-mapping
5888 	 * information
5889 	 */
5890 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5891 	if (ret) {
5892 		dev_info(&pf->pdev->dev,
5893 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5894 			 ERR_PTR(ret),
5895 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5896 		goto out;
5897 	}
5898 	/* update the local VSI info with updated queue map */
5899 	i40e_vsi_update_queue_map(vsi, &ctxt);
5900 	vsi->info.valid_sections = 0;
5901 
5902 	/* Update current VSI BW information */
5903 	ret = i40e_vsi_get_bw_info(vsi);
5904 	if (ret) {
5905 		dev_info(&pf->pdev->dev,
5906 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5907 			 ERR_PTR(ret),
5908 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5909 		goto out;
5910 	}
5911 
5912 	/* Update the netdev TC setup */
5913 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5914 out:
5915 	return ret;
5916 }
5917 
5918 /**
5919  * i40e_get_link_speed - Returns link speed for the interface
5920  * @vsi: VSI to be configured
5921  *
5922  **/
5923 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5924 {
5925 	struct i40e_pf *pf = vsi->back;
5926 
5927 	switch (pf->hw.phy.link_info.link_speed) {
5928 	case I40E_LINK_SPEED_40GB:
5929 		return 40000;
5930 	case I40E_LINK_SPEED_25GB:
5931 		return 25000;
5932 	case I40E_LINK_SPEED_20GB:
5933 		return 20000;
5934 	case I40E_LINK_SPEED_10GB:
5935 		return 10000;
5936 	case I40E_LINK_SPEED_1GB:
5937 		return 1000;
5938 	default:
5939 		return -EINVAL;
5940 	}
5941 }
5942 
5943 /**
5944  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5945  * @vsi: Pointer to vsi structure
5946  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5947  *
5948  * Helper function to convert units before send to set BW limit
5949  **/
5950 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5951 {
5952 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5953 		dev_warn(&vsi->back->pdev->dev,
5954 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5955 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5956 	} else {
5957 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5958 	}
5959 
5960 	return max_tx_rate;
5961 }
5962 
5963 /**
5964  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5965  * @vsi: VSI to be configured
5966  * @seid: seid of the channel/VSI
5967  * @max_tx_rate: max TX rate to be configured as BW limit
5968  *
5969  * Helper function to set BW limit for a given VSI
5970  **/
5971 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5972 {
5973 	struct i40e_pf *pf = vsi->back;
5974 	u64 credits = 0;
5975 	int speed = 0;
5976 	int ret = 0;
5977 
5978 	speed = i40e_get_link_speed(vsi);
5979 	if (max_tx_rate > speed) {
5980 		dev_err(&pf->pdev->dev,
5981 			"Invalid max tx rate %llu specified for VSI seid %d.",
5982 			max_tx_rate, seid);
5983 		return -EINVAL;
5984 	}
5985 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5986 		dev_warn(&pf->pdev->dev,
5987 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5988 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5989 	}
5990 
5991 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5992 	credits = max_tx_rate;
5993 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5994 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5995 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5996 	if (ret)
5997 		dev_err(&pf->pdev->dev,
5998 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5999 			max_tx_rate, seid, ERR_PTR(ret),
6000 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6001 	return ret;
6002 }
6003 
6004 /**
6005  * i40e_remove_queue_channels - Remove queue channels for the TCs
6006  * @vsi: VSI to be configured
6007  *
6008  * Remove queue channels for the TCs
6009  **/
6010 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6011 {
6012 	enum i40e_admin_queue_err last_aq_status;
6013 	struct i40e_cloud_filter *cfilter;
6014 	struct i40e_channel *ch, *ch_tmp;
6015 	struct i40e_pf *pf = vsi->back;
6016 	struct hlist_node *node;
6017 	int ret, i;
6018 
6019 	/* Reset rss size that was stored when reconfiguring rss for
6020 	 * channel VSIs with non-power-of-2 queue count.
6021 	 */
6022 	vsi->current_rss_size = 0;
6023 
6024 	/* perform cleanup for channels if they exist */
6025 	if (list_empty(&vsi->ch_list))
6026 		return;
6027 
6028 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6029 		struct i40e_vsi *p_vsi;
6030 
6031 		list_del(&ch->list);
6032 		p_vsi = ch->parent_vsi;
6033 		if (!p_vsi || !ch->initialized) {
6034 			kfree(ch);
6035 			continue;
6036 		}
6037 		/* Reset queue contexts */
6038 		for (i = 0; i < ch->num_queue_pairs; i++) {
6039 			struct i40e_ring *tx_ring, *rx_ring;
6040 			u16 pf_q;
6041 
6042 			pf_q = ch->base_queue + i;
6043 			tx_ring = vsi->tx_rings[pf_q];
6044 			tx_ring->ch = NULL;
6045 
6046 			rx_ring = vsi->rx_rings[pf_q];
6047 			rx_ring->ch = NULL;
6048 		}
6049 
6050 		/* Reset BW configured for this VSI via mqprio */
6051 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6052 		if (ret)
6053 			dev_info(&vsi->back->pdev->dev,
6054 				 "Failed to reset tx rate for ch->seid %u\n",
6055 				 ch->seid);
6056 
6057 		/* delete cloud filters associated with this channel */
6058 		hlist_for_each_entry_safe(cfilter, node,
6059 					  &pf->cloud_filter_list, cloud_node) {
6060 			if (cfilter->seid != ch->seid)
6061 				continue;
6062 
6063 			hash_del(&cfilter->cloud_node);
6064 			if (cfilter->dst_port)
6065 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6066 									cfilter,
6067 									false);
6068 			else
6069 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6070 								false);
6071 			last_aq_status = pf->hw.aq.asq_last_status;
6072 			if (ret)
6073 				dev_info(&pf->pdev->dev,
6074 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6075 					 ERR_PTR(ret),
6076 					 i40e_aq_str(&pf->hw, last_aq_status));
6077 			kfree(cfilter);
6078 		}
6079 
6080 		/* delete VSI from FW */
6081 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6082 					     NULL);
6083 		if (ret)
6084 			dev_err(&vsi->back->pdev->dev,
6085 				"unable to remove channel (%d) for parent VSI(%d)\n",
6086 				ch->seid, p_vsi->seid);
6087 		kfree(ch);
6088 	}
6089 	INIT_LIST_HEAD(&vsi->ch_list);
6090 }
6091 
6092 /**
6093  * i40e_get_max_queues_for_channel
6094  * @vsi: ptr to VSI to which channels are associated with
6095  *
6096  * Helper function which returns max value among the queue counts set on the
6097  * channels/TCs created.
6098  **/
6099 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6100 {
6101 	struct i40e_channel *ch, *ch_tmp;
6102 	int max = 0;
6103 
6104 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6105 		if (!ch->initialized)
6106 			continue;
6107 		if (ch->num_queue_pairs > max)
6108 			max = ch->num_queue_pairs;
6109 	}
6110 
6111 	return max;
6112 }
6113 
6114 /**
6115  * i40e_validate_num_queues - validate num_queues w.r.t channel
6116  * @pf: ptr to PF device
6117  * @num_queues: number of queues
6118  * @vsi: the parent VSI
6119  * @reconfig_rss: indicates should the RSS be reconfigured or not
6120  *
6121  * This function validates number of queues in the context of new channel
6122  * which is being established and determines if RSS should be reconfigured
6123  * or not for parent VSI.
6124  **/
6125 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6126 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6127 {
6128 	int max_ch_queues;
6129 
6130 	if (!reconfig_rss)
6131 		return -EINVAL;
6132 
6133 	*reconfig_rss = false;
6134 	if (vsi->current_rss_size) {
6135 		if (num_queues > vsi->current_rss_size) {
6136 			dev_dbg(&pf->pdev->dev,
6137 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6138 				num_queues, vsi->current_rss_size);
6139 			return -EINVAL;
6140 		} else if ((num_queues < vsi->current_rss_size) &&
6141 			   (!is_power_of_2(num_queues))) {
6142 			dev_dbg(&pf->pdev->dev,
6143 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6144 				num_queues, vsi->current_rss_size);
6145 			return -EINVAL;
6146 		}
6147 	}
6148 
6149 	if (!is_power_of_2(num_queues)) {
6150 		/* Find the max num_queues configured for channel if channel
6151 		 * exist.
6152 		 * if channel exist, then enforce 'num_queues' to be more than
6153 		 * max ever queues configured for channel.
6154 		 */
6155 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6156 		if (num_queues < max_ch_queues) {
6157 			dev_dbg(&pf->pdev->dev,
6158 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6159 				num_queues, max_ch_queues);
6160 			return -EINVAL;
6161 		}
6162 		*reconfig_rss = true;
6163 	}
6164 
6165 	return 0;
6166 }
6167 
6168 /**
6169  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6170  * @vsi: the VSI being setup
6171  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6172  *
6173  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6174  **/
6175 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6176 {
6177 	struct i40e_pf *pf = vsi->back;
6178 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6179 	struct i40e_hw *hw = &pf->hw;
6180 	int local_rss_size;
6181 	u8 *lut;
6182 	int ret;
6183 
6184 	if (!vsi->rss_size)
6185 		return -EINVAL;
6186 
6187 	if (rss_size > vsi->rss_size)
6188 		return -EINVAL;
6189 
6190 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6191 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6192 	if (!lut)
6193 		return -ENOMEM;
6194 
6195 	/* Ignoring user configured lut if there is one */
6196 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6197 
6198 	/* Use user configured hash key if there is one, otherwise
6199 	 * use default.
6200 	 */
6201 	if (vsi->rss_hkey_user)
6202 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6203 	else
6204 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6205 
6206 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6207 	if (ret) {
6208 		dev_info(&pf->pdev->dev,
6209 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6210 			 ERR_PTR(ret),
6211 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6212 		kfree(lut);
6213 		return ret;
6214 	}
6215 	kfree(lut);
6216 
6217 	/* Do the update w.r.t. storing rss_size */
6218 	if (!vsi->orig_rss_size)
6219 		vsi->orig_rss_size = vsi->rss_size;
6220 	vsi->current_rss_size = local_rss_size;
6221 
6222 	return ret;
6223 }
6224 
6225 /**
6226  * i40e_channel_setup_queue_map - Setup a channel queue map
6227  * @pf: ptr to PF device
6228  * @ctxt: VSI context structure
6229  * @ch: ptr to channel structure
6230  *
6231  * Setup queue map for a specific channel
6232  **/
6233 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6234 					 struct i40e_vsi_context *ctxt,
6235 					 struct i40e_channel *ch)
6236 {
6237 	u16 qcount, qmap, sections = 0;
6238 	u8 offset = 0;
6239 	int pow;
6240 
6241 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6242 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6243 
6244 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6245 	ch->num_queue_pairs = qcount;
6246 
6247 	/* find the next higher power-of-2 of num queue pairs */
6248 	pow = ilog2(qcount);
6249 	if (!is_power_of_2(qcount))
6250 		pow++;
6251 
6252 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6253 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6254 
6255 	/* Setup queue TC[0].qmap for given VSI context */
6256 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6257 
6258 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6259 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6260 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6261 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6262 }
6263 
6264 /**
6265  * i40e_add_channel - add a channel by adding VSI
6266  * @pf: ptr to PF device
6267  * @uplink_seid: underlying HW switching element (VEB) ID
6268  * @ch: ptr to channel structure
6269  *
6270  * Add a channel (VSI) using add_vsi and queue_map
6271  **/
6272 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6273 			    struct i40e_channel *ch)
6274 {
6275 	struct i40e_hw *hw = &pf->hw;
6276 	struct i40e_vsi_context ctxt;
6277 	u8 enabled_tc = 0x1; /* TC0 enabled */
6278 	int ret;
6279 
6280 	if (ch->type != I40E_VSI_VMDQ2) {
6281 		dev_info(&pf->pdev->dev,
6282 			 "add new vsi failed, ch->type %d\n", ch->type);
6283 		return -EINVAL;
6284 	}
6285 
6286 	memset(&ctxt, 0, sizeof(ctxt));
6287 	ctxt.pf_num = hw->pf_id;
6288 	ctxt.vf_num = 0;
6289 	ctxt.uplink_seid = uplink_seid;
6290 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6291 	if (ch->type == I40E_VSI_VMDQ2)
6292 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6293 
6294 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6295 		ctxt.info.valid_sections |=
6296 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6297 		ctxt.info.switch_id =
6298 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6299 	}
6300 
6301 	/* Set queue map for a given VSI context */
6302 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6303 
6304 	/* Now time to create VSI */
6305 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6306 	if (ret) {
6307 		dev_info(&pf->pdev->dev,
6308 			 "add new vsi failed, err %pe aq_err %s\n",
6309 			 ERR_PTR(ret),
6310 			 i40e_aq_str(&pf->hw,
6311 				     pf->hw.aq.asq_last_status));
6312 		return -ENOENT;
6313 	}
6314 
6315 	/* Success, update channel, set enabled_tc only if the channel
6316 	 * is not a macvlan
6317 	 */
6318 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6319 	ch->seid = ctxt.seid;
6320 	ch->vsi_number = ctxt.vsi_number;
6321 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6322 
6323 	/* copy just the sections touched not the entire info
6324 	 * since not all sections are valid as returned by
6325 	 * update vsi params
6326 	 */
6327 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6328 	memcpy(&ch->info.queue_mapping,
6329 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6330 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6331 	       sizeof(ctxt.info.tc_mapping));
6332 
6333 	return 0;
6334 }
6335 
6336 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6337 				  u8 *bw_share)
6338 {
6339 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6340 	int ret;
6341 	int i;
6342 
6343 	memset(&bw_data, 0, sizeof(bw_data));
6344 	bw_data.tc_valid_bits = ch->enabled_tc;
6345 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6346 		bw_data.tc_bw_credits[i] = bw_share[i];
6347 
6348 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6349 				       &bw_data, NULL);
6350 	if (ret) {
6351 		dev_info(&vsi->back->pdev->dev,
6352 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6353 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6354 		return -EINVAL;
6355 	}
6356 
6357 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6358 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6359 
6360 	return 0;
6361 }
6362 
6363 /**
6364  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6365  * @pf: ptr to PF device
6366  * @vsi: the VSI being setup
6367  * @ch: ptr to channel structure
6368  *
6369  * Configure TX rings associated with channel (VSI) since queues are being
6370  * from parent VSI.
6371  **/
6372 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6373 				       struct i40e_vsi *vsi,
6374 				       struct i40e_channel *ch)
6375 {
6376 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6377 	int ret;
6378 	int i;
6379 
6380 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6381 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6382 		if (ch->enabled_tc & BIT(i))
6383 			bw_share[i] = 1;
6384 	}
6385 
6386 	/* configure BW for new VSI */
6387 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6388 	if (ret) {
6389 		dev_info(&vsi->back->pdev->dev,
6390 			 "Failed configuring TC map %d for channel (seid %u)\n",
6391 			 ch->enabled_tc, ch->seid);
6392 		return ret;
6393 	}
6394 
6395 	for (i = 0; i < ch->num_queue_pairs; i++) {
6396 		struct i40e_ring *tx_ring, *rx_ring;
6397 		u16 pf_q;
6398 
6399 		pf_q = ch->base_queue + i;
6400 
6401 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6402 		 * context
6403 		 */
6404 		tx_ring = vsi->tx_rings[pf_q];
6405 		tx_ring->ch = ch;
6406 
6407 		/* Get the RX ring ptr */
6408 		rx_ring = vsi->rx_rings[pf_q];
6409 		rx_ring->ch = ch;
6410 	}
6411 
6412 	return 0;
6413 }
6414 
6415 /**
6416  * i40e_setup_hw_channel - setup new channel
6417  * @pf: ptr to PF device
6418  * @vsi: the VSI being setup
6419  * @ch: ptr to channel structure
6420  * @uplink_seid: underlying HW switching element (VEB) ID
6421  * @type: type of channel to be created (VMDq2/VF)
6422  *
6423  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6424  * and configures TX rings accordingly
6425  **/
6426 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6427 					struct i40e_vsi *vsi,
6428 					struct i40e_channel *ch,
6429 					u16 uplink_seid, u8 type)
6430 {
6431 	int ret;
6432 
6433 	ch->initialized = false;
6434 	ch->base_queue = vsi->next_base_queue;
6435 	ch->type = type;
6436 
6437 	/* Proceed with creation of channel (VMDq2) VSI */
6438 	ret = i40e_add_channel(pf, uplink_seid, ch);
6439 	if (ret) {
6440 		dev_info(&pf->pdev->dev,
6441 			 "failed to add_channel using uplink_seid %u\n",
6442 			 uplink_seid);
6443 		return ret;
6444 	}
6445 
6446 	/* Mark the successful creation of channel */
6447 	ch->initialized = true;
6448 
6449 	/* Reconfigure TX queues using QTX_CTL register */
6450 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6451 	if (ret) {
6452 		dev_info(&pf->pdev->dev,
6453 			 "failed to configure TX rings for channel %u\n",
6454 			 ch->seid);
6455 		return ret;
6456 	}
6457 
6458 	/* update 'next_base_queue' */
6459 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6460 	dev_dbg(&pf->pdev->dev,
6461 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6462 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6463 		ch->num_queue_pairs,
6464 		vsi->next_base_queue);
6465 	return ret;
6466 }
6467 
6468 /**
6469  * i40e_setup_channel - setup new channel using uplink element
6470  * @pf: ptr to PF device
6471  * @vsi: pointer to the VSI to set up the channel within
6472  * @ch: ptr to channel structure
6473  *
6474  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6475  * and uplink switching element (uplink_seid)
6476  **/
6477 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6478 			       struct i40e_channel *ch)
6479 {
6480 	u8 vsi_type;
6481 	u16 seid;
6482 	int ret;
6483 
6484 	if (vsi->type == I40E_VSI_MAIN) {
6485 		vsi_type = I40E_VSI_VMDQ2;
6486 	} else {
6487 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6488 			vsi->type);
6489 		return false;
6490 	}
6491 
6492 	/* underlying switching element */
6493 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6494 
6495 	/* create channel (VSI), configure TX rings */
6496 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6497 	if (ret) {
6498 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6499 		return false;
6500 	}
6501 
6502 	return ch->initialized ? true : false;
6503 }
6504 
6505 /**
6506  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6507  * @vsi: ptr to VSI which has PF backing
6508  *
6509  * Sets up switch mode correctly if it needs to be changed and perform
6510  * what are allowed modes.
6511  **/
6512 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6513 {
6514 	u8 mode;
6515 	struct i40e_pf *pf = vsi->back;
6516 	struct i40e_hw *hw = &pf->hw;
6517 	int ret;
6518 
6519 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6520 	if (ret)
6521 		return -EINVAL;
6522 
6523 	if (hw->dev_caps.switch_mode) {
6524 		/* if switch mode is set, support mode2 (non-tunneled for
6525 		 * cloud filter) for now
6526 		 */
6527 		u32 switch_mode = hw->dev_caps.switch_mode &
6528 				  I40E_SWITCH_MODE_MASK;
6529 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6530 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6531 				return 0;
6532 			dev_err(&pf->pdev->dev,
6533 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6534 				hw->dev_caps.switch_mode);
6535 			return -EINVAL;
6536 		}
6537 	}
6538 
6539 	/* Set Bit 7 to be valid */
6540 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6541 
6542 	/* Set L4type for TCP support */
6543 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6544 
6545 	/* Set cloud filter mode */
6546 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6547 
6548 	/* Prep mode field for set_switch_config */
6549 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6550 					pf->last_sw_conf_valid_flags,
6551 					mode, NULL);
6552 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6553 		dev_err(&pf->pdev->dev,
6554 			"couldn't set switch config bits, err %pe aq_err %s\n",
6555 			ERR_PTR(ret),
6556 			i40e_aq_str(hw,
6557 				    hw->aq.asq_last_status));
6558 
6559 	return ret;
6560 }
6561 
6562 /**
6563  * i40e_create_queue_channel - function to create channel
6564  * @vsi: VSI to be configured
6565  * @ch: ptr to channel (it contains channel specific params)
6566  *
6567  * This function creates channel (VSI) using num_queues specified by user,
6568  * reconfigs RSS if needed.
6569  **/
6570 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6571 			      struct i40e_channel *ch)
6572 {
6573 	struct i40e_pf *pf = vsi->back;
6574 	bool reconfig_rss;
6575 	int err;
6576 
6577 	if (!ch)
6578 		return -EINVAL;
6579 
6580 	if (!ch->num_queue_pairs) {
6581 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6582 			ch->num_queue_pairs);
6583 		return -EINVAL;
6584 	}
6585 
6586 	/* validate user requested num_queues for channel */
6587 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6588 				       &reconfig_rss);
6589 	if (err) {
6590 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6591 			 ch->num_queue_pairs);
6592 		return -EINVAL;
6593 	}
6594 
6595 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6596 	 * VSI to be added switch to VEB mode.
6597 	 */
6598 
6599 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6600 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6601 
6602 		if (vsi->type == I40E_VSI_MAIN) {
6603 			if (i40e_is_tc_mqprio_enabled(pf))
6604 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6605 			else
6606 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6607 		}
6608 		/* now onwards for main VSI, number of queues will be value
6609 		 * of TC0's queue count
6610 		 */
6611 	}
6612 
6613 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6614 	 * it should be more than num_queues
6615 	 */
6616 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6617 		dev_dbg(&pf->pdev->dev,
6618 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6619 			vsi->cnt_q_avail, ch->num_queue_pairs);
6620 		return -EINVAL;
6621 	}
6622 
6623 	/* reconfig_rss only if vsi type is MAIN_VSI */
6624 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6625 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6626 		if (err) {
6627 			dev_info(&pf->pdev->dev,
6628 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6629 				 ch->num_queue_pairs);
6630 			return -EINVAL;
6631 		}
6632 	}
6633 
6634 	if (!i40e_setup_channel(pf, vsi, ch)) {
6635 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6636 		return -EINVAL;
6637 	}
6638 
6639 	dev_info(&pf->pdev->dev,
6640 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6641 		 ch->seid, ch->num_queue_pairs);
6642 
6643 	/* configure VSI for BW limit */
6644 	if (ch->max_tx_rate) {
6645 		u64 credits = ch->max_tx_rate;
6646 
6647 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6648 			return -EINVAL;
6649 
6650 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6651 		dev_dbg(&pf->pdev->dev,
6652 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6653 			ch->max_tx_rate,
6654 			credits,
6655 			ch->seid);
6656 	}
6657 
6658 	/* in case of VF, this will be main SRIOV VSI */
6659 	ch->parent_vsi = vsi;
6660 
6661 	/* and update main_vsi's count for queue_available to use */
6662 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6663 
6664 	return 0;
6665 }
6666 
6667 /**
6668  * i40e_configure_queue_channels - Add queue channel for the given TCs
6669  * @vsi: VSI to be configured
6670  *
6671  * Configures queue channel mapping to the given TCs
6672  **/
6673 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6674 {
6675 	struct i40e_channel *ch;
6676 	u64 max_rate = 0;
6677 	int ret = 0, i;
6678 
6679 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6680 	vsi->tc_seid_map[0] = vsi->seid;
6681 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6682 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6683 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6684 			if (!ch) {
6685 				ret = -ENOMEM;
6686 				goto err_free;
6687 			}
6688 
6689 			INIT_LIST_HEAD(&ch->list);
6690 			ch->num_queue_pairs =
6691 				vsi->tc_config.tc_info[i].qcount;
6692 			ch->base_queue =
6693 				vsi->tc_config.tc_info[i].qoffset;
6694 
6695 			/* Bandwidth limit through tc interface is in bytes/s,
6696 			 * change to Mbit/s
6697 			 */
6698 			max_rate = vsi->mqprio_qopt.max_rate[i];
6699 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6700 			ch->max_tx_rate = max_rate;
6701 
6702 			list_add_tail(&ch->list, &vsi->ch_list);
6703 
6704 			ret = i40e_create_queue_channel(vsi, ch);
6705 			if (ret) {
6706 				dev_err(&vsi->back->pdev->dev,
6707 					"Failed creating queue channel with TC%d: queues %d\n",
6708 					i, ch->num_queue_pairs);
6709 				goto err_free;
6710 			}
6711 			vsi->tc_seid_map[i] = ch->seid;
6712 		}
6713 	}
6714 
6715 	/* reset to reconfigure TX queue contexts */
6716 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6717 	return ret;
6718 
6719 err_free:
6720 	i40e_remove_queue_channels(vsi);
6721 	return ret;
6722 }
6723 
6724 /**
6725  * i40e_veb_config_tc - Configure TCs for given VEB
6726  * @veb: given VEB
6727  * @enabled_tc: TC bitmap
6728  *
6729  * Configures given TC bitmap for VEB (switching) element
6730  **/
6731 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6732 {
6733 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6734 	struct i40e_pf *pf = veb->pf;
6735 	int ret = 0;
6736 	int i;
6737 
6738 	/* No TCs or already enabled TCs just return */
6739 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6740 		return ret;
6741 
6742 	bw_data.tc_valid_bits = enabled_tc;
6743 	/* bw_data.absolute_credits is not set (relative) */
6744 
6745 	/* Enable ETS TCs with equal BW Share for now */
6746 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6747 		if (enabled_tc & BIT(i))
6748 			bw_data.tc_bw_share_credits[i] = 1;
6749 	}
6750 
6751 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6752 						   &bw_data, NULL);
6753 	if (ret) {
6754 		dev_info(&pf->pdev->dev,
6755 			 "VEB bw config failed, err %pe aq_err %s\n",
6756 			 ERR_PTR(ret),
6757 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6758 		goto out;
6759 	}
6760 
6761 	/* Update the BW information */
6762 	ret = i40e_veb_get_bw_info(veb);
6763 	if (ret) {
6764 		dev_info(&pf->pdev->dev,
6765 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6766 			 ERR_PTR(ret),
6767 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6768 	}
6769 
6770 out:
6771 	return ret;
6772 }
6773 
6774 #ifdef CONFIG_I40E_DCB
6775 /**
6776  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6777  * @pf: PF struct
6778  *
6779  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6780  * the caller would've quiesce all the VSIs before calling
6781  * this function
6782  **/
6783 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6784 {
6785 	u8 tc_map = 0;
6786 	int ret;
6787 	u8 v;
6788 
6789 	/* Enable the TCs available on PF to all VEBs */
6790 	tc_map = i40e_pf_get_tc_map(pf);
6791 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6792 		return;
6793 
6794 	for (v = 0; v < I40E_MAX_VEB; v++) {
6795 		if (!pf->veb[v])
6796 			continue;
6797 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6798 		if (ret) {
6799 			dev_info(&pf->pdev->dev,
6800 				 "Failed configuring TC for VEB seid=%d\n",
6801 				 pf->veb[v]->seid);
6802 			/* Will try to configure as many components */
6803 		}
6804 	}
6805 
6806 	/* Update each VSI */
6807 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6808 		if (!pf->vsi[v])
6809 			continue;
6810 
6811 		/* - Enable all TCs for the LAN VSI
6812 		 * - For all others keep them at TC0 for now
6813 		 */
6814 		if (v == pf->lan_vsi)
6815 			tc_map = i40e_pf_get_tc_map(pf);
6816 		else
6817 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6818 
6819 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6820 		if (ret) {
6821 			dev_info(&pf->pdev->dev,
6822 				 "Failed configuring TC for VSI seid=%d\n",
6823 				 pf->vsi[v]->seid);
6824 			/* Will try to configure as many components */
6825 		} else {
6826 			/* Re-configure VSI vectors based on updated TC map */
6827 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6828 			if (pf->vsi[v]->netdev)
6829 				i40e_dcbnl_set_all(pf->vsi[v]);
6830 		}
6831 	}
6832 }
6833 
6834 /**
6835  * i40e_resume_port_tx - Resume port Tx
6836  * @pf: PF struct
6837  *
6838  * Resume a port's Tx and issue a PF reset in case of failure to
6839  * resume.
6840  **/
6841 static int i40e_resume_port_tx(struct i40e_pf *pf)
6842 {
6843 	struct i40e_hw *hw = &pf->hw;
6844 	int ret;
6845 
6846 	ret = i40e_aq_resume_port_tx(hw, NULL);
6847 	if (ret) {
6848 		dev_info(&pf->pdev->dev,
6849 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6850 			  ERR_PTR(ret),
6851 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6852 		/* Schedule PF reset to recover */
6853 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6854 		i40e_service_event_schedule(pf);
6855 	}
6856 
6857 	return ret;
6858 }
6859 
6860 /**
6861  * i40e_suspend_port_tx - Suspend port Tx
6862  * @pf: PF struct
6863  *
6864  * Suspend a port's Tx and issue a PF reset in case of failure.
6865  **/
6866 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6867 {
6868 	struct i40e_hw *hw = &pf->hw;
6869 	int ret;
6870 
6871 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6872 	if (ret) {
6873 		dev_info(&pf->pdev->dev,
6874 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6875 			 ERR_PTR(ret),
6876 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6877 		/* Schedule PF reset to recover */
6878 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6879 		i40e_service_event_schedule(pf);
6880 	}
6881 
6882 	return ret;
6883 }
6884 
6885 /**
6886  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6887  * @pf: PF being configured
6888  * @new_cfg: New DCBX configuration
6889  *
6890  * Program DCB settings into HW and reconfigure VEB/VSIs on
6891  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6892  **/
6893 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6894 				  struct i40e_dcbx_config *new_cfg)
6895 {
6896 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6897 	int ret;
6898 
6899 	/* Check if need reconfiguration */
6900 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6901 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6902 		return 0;
6903 	}
6904 
6905 	/* Config change disable all VSIs */
6906 	i40e_pf_quiesce_all_vsi(pf);
6907 
6908 	/* Copy the new config to the current config */
6909 	*old_cfg = *new_cfg;
6910 	old_cfg->etsrec = old_cfg->etscfg;
6911 	ret = i40e_set_dcb_config(&pf->hw);
6912 	if (ret) {
6913 		dev_info(&pf->pdev->dev,
6914 			 "Set DCB Config failed, err %pe aq_err %s\n",
6915 			 ERR_PTR(ret),
6916 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6917 		goto out;
6918 	}
6919 
6920 	/* Changes in configuration update VEB/VSI */
6921 	i40e_dcb_reconfigure(pf);
6922 out:
6923 	/* In case of reset do not try to resume anything */
6924 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6925 		/* Re-start the VSIs if disabled */
6926 		ret = i40e_resume_port_tx(pf);
6927 		/* In case of error no point in resuming VSIs */
6928 		if (ret)
6929 			goto err;
6930 		i40e_pf_unquiesce_all_vsi(pf);
6931 	}
6932 err:
6933 	return ret;
6934 }
6935 
6936 /**
6937  * i40e_hw_dcb_config - Program new DCBX settings into HW
6938  * @pf: PF being configured
6939  * @new_cfg: New DCBX configuration
6940  *
6941  * Program DCB settings into HW and reconfigure VEB/VSIs on
6942  * given PF
6943  **/
6944 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6945 {
6946 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6947 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6948 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6949 	struct i40e_dcbx_config *old_cfg;
6950 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6951 	struct i40e_rx_pb_config pb_cfg;
6952 	struct i40e_hw *hw = &pf->hw;
6953 	u8 num_ports = hw->num_ports;
6954 	bool need_reconfig;
6955 	int ret = -EINVAL;
6956 	u8 lltc_map = 0;
6957 	u8 tc_map = 0;
6958 	u8 new_numtc;
6959 	u8 i;
6960 
6961 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6962 	/* Un-pack information to Program ETS HW via shared API
6963 	 * numtc, tcmap
6964 	 * LLTC map
6965 	 * ETS/NON-ETS arbiter mode
6966 	 * max exponent (credit refills)
6967 	 * Total number of ports
6968 	 * PFC priority bit-map
6969 	 * Priority Table
6970 	 * BW % per TC
6971 	 * Arbiter mode between UPs sharing same TC
6972 	 * TSA table (ETS or non-ETS)
6973 	 * EEE enabled or not
6974 	 * MFS TC table
6975 	 */
6976 
6977 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6978 
6979 	memset(&ets_data, 0, sizeof(ets_data));
6980 	for (i = 0; i < new_numtc; i++) {
6981 		tc_map |= BIT(i);
6982 		switch (new_cfg->etscfg.tsatable[i]) {
6983 		case I40E_IEEE_TSA_ETS:
6984 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6985 			ets_data.tc_bw_share_credits[i] =
6986 					new_cfg->etscfg.tcbwtable[i];
6987 			break;
6988 		case I40E_IEEE_TSA_STRICT:
6989 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6990 			lltc_map |= BIT(i);
6991 			ets_data.tc_bw_share_credits[i] =
6992 					I40E_DCB_STRICT_PRIO_CREDITS;
6993 			break;
6994 		default:
6995 			/* Invalid TSA type */
6996 			need_reconfig = false;
6997 			goto out;
6998 		}
6999 	}
7000 
7001 	old_cfg = &hw->local_dcbx_config;
7002 	/* Check if need reconfiguration */
7003 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
7004 
7005 	/* If needed, enable/disable frame tagging, disable all VSIs
7006 	 * and suspend port tx
7007 	 */
7008 	if (need_reconfig) {
7009 		/* Enable DCB tagging only when more than one TC */
7010 		if (new_numtc > 1)
7011 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7012 		else
7013 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7014 
7015 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7016 		/* Reconfiguration needed quiesce all VSIs */
7017 		i40e_pf_quiesce_all_vsi(pf);
7018 		ret = i40e_suspend_port_tx(pf);
7019 		if (ret)
7020 			goto err;
7021 	}
7022 
7023 	/* Configure Port ETS Tx Scheduler */
7024 	ets_data.tc_valid_bits = tc_map;
7025 	ets_data.tc_strict_priority_flags = lltc_map;
7026 	ret = i40e_aq_config_switch_comp_ets
7027 		(hw, pf->mac_seid, &ets_data,
7028 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7029 	if (ret) {
7030 		dev_info(&pf->pdev->dev,
7031 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7032 			 ERR_PTR(ret),
7033 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7034 		goto out;
7035 	}
7036 
7037 	/* Configure Rx ETS HW */
7038 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7039 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7040 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7041 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7042 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7043 				   lltc_map);
7044 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7045 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7046 				     prio_type);
7047 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7048 			       new_cfg->etscfg.prioritytable);
7049 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7050 
7051 	/* Configure Rx Packet Buffers in HW */
7052 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7053 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7054 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7055 	}
7056 
7057 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7058 					 false, new_cfg->pfc.pfcenable,
7059 					 mfs_tc, &pb_cfg);
7060 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7061 
7062 	/* Update the local Rx Packet buffer config */
7063 	pf->pb_cfg = pb_cfg;
7064 
7065 	/* Inform the FW about changes to DCB configuration */
7066 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7067 	if (ret) {
7068 		dev_info(&pf->pdev->dev,
7069 			 "DCB Updated failed, err %pe aq_err %s\n",
7070 			 ERR_PTR(ret),
7071 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7072 		goto out;
7073 	}
7074 
7075 	/* Update the port DCBx configuration */
7076 	*old_cfg = *new_cfg;
7077 
7078 	/* Changes in configuration update VEB/VSI */
7079 	i40e_dcb_reconfigure(pf);
7080 out:
7081 	/* Re-start the VSIs if disabled */
7082 	if (need_reconfig) {
7083 		ret = i40e_resume_port_tx(pf);
7084 
7085 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7086 		/* In case of error no point in resuming VSIs */
7087 		if (ret)
7088 			goto err;
7089 
7090 		/* Wait for the PF's queues to be disabled */
7091 		ret = i40e_pf_wait_queues_disabled(pf);
7092 		if (ret) {
7093 			/* Schedule PF reset to recover */
7094 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7095 			i40e_service_event_schedule(pf);
7096 			goto err;
7097 		} else {
7098 			i40e_pf_unquiesce_all_vsi(pf);
7099 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7100 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7101 		}
7102 		/* registers are set, lets apply */
7103 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7104 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7105 	}
7106 
7107 err:
7108 	return ret;
7109 }
7110 
7111 /**
7112  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7113  * @pf: PF being queried
7114  *
7115  * Set default DCB configuration in case DCB is to be done in SW.
7116  **/
7117 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7118 {
7119 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7120 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7121 	struct i40e_hw *hw = &pf->hw;
7122 	int err;
7123 
7124 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7125 		/* Update the local cached instance with TC0 ETS */
7126 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7127 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7128 		pf->tmp_cfg.etscfg.maxtcs = 0;
7129 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7130 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7131 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7132 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7133 		/* FW needs one App to configure HW */
7134 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7135 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7136 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7137 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7138 
7139 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7140 	}
7141 
7142 	memset(&ets_data, 0, sizeof(ets_data));
7143 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7144 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7145 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7146 
7147 	/* Enable ETS on the Physical port */
7148 	err = i40e_aq_config_switch_comp_ets
7149 		(hw, pf->mac_seid, &ets_data,
7150 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7151 	if (err) {
7152 		dev_info(&pf->pdev->dev,
7153 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7154 			 ERR_PTR(err),
7155 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7156 		err = -ENOENT;
7157 		goto out;
7158 	}
7159 
7160 	/* Update the local cached instance with TC0 ETS */
7161 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7162 	dcb_cfg->etscfg.cbs = 0;
7163 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7164 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7165 
7166 out:
7167 	return err;
7168 }
7169 
7170 /**
7171  * i40e_init_pf_dcb - Initialize DCB configuration
7172  * @pf: PF being configured
7173  *
7174  * Query the current DCB configuration and cache it
7175  * in the hardware structure
7176  **/
7177 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7178 {
7179 	struct i40e_hw *hw = &pf->hw;
7180 	int err;
7181 
7182 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7183 	 * Also do not enable DCBx if FW LLDP agent is disabled
7184 	 */
7185 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7186 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7187 		err = -EOPNOTSUPP;
7188 		goto out;
7189 	}
7190 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7191 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7192 		err = i40e_dcb_sw_default_config(pf);
7193 		if (err) {
7194 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7195 			goto out;
7196 		}
7197 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7198 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7199 			       DCB_CAP_DCBX_VER_IEEE;
7200 		/* at init capable but disabled */
7201 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7202 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7203 		goto out;
7204 	}
7205 	err = i40e_init_dcb(hw, true);
7206 	if (!err) {
7207 		/* Device/Function is not DCBX capable */
7208 		if ((!hw->func_caps.dcb) ||
7209 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7210 			dev_info(&pf->pdev->dev,
7211 				 "DCBX offload is not supported or is disabled for this PF.\n");
7212 		} else {
7213 			/* When status is not DISABLED then DCBX in FW */
7214 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7215 				       DCB_CAP_DCBX_VER_IEEE;
7216 
7217 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7218 			/* Enable DCB tagging only when more than one TC
7219 			 * or explicitly disable if only one TC
7220 			 */
7221 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7222 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7223 			else
7224 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7225 			dev_dbg(&pf->pdev->dev,
7226 				"DCBX offload is supported for this PF.\n");
7227 		}
7228 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7229 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7230 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7231 	} else {
7232 		dev_info(&pf->pdev->dev,
7233 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7234 			 ERR_PTR(err),
7235 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7236 	}
7237 
7238 out:
7239 	return err;
7240 }
7241 #endif /* CONFIG_I40E_DCB */
7242 
7243 /**
7244  * i40e_print_link_message - print link up or down
7245  * @vsi: the VSI for which link needs a message
7246  * @isup: true of link is up, false otherwise
7247  */
7248 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7249 {
7250 	enum i40e_aq_link_speed new_speed;
7251 	struct i40e_pf *pf = vsi->back;
7252 	char *speed = "Unknown";
7253 	char *fc = "Unknown";
7254 	char *fec = "";
7255 	char *req_fec = "";
7256 	char *an = "";
7257 
7258 	if (isup)
7259 		new_speed = pf->hw.phy.link_info.link_speed;
7260 	else
7261 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7262 
7263 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7264 		return;
7265 	vsi->current_isup = isup;
7266 	vsi->current_speed = new_speed;
7267 	if (!isup) {
7268 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7269 		return;
7270 	}
7271 
7272 	/* Warn user if link speed on NPAR enabled partition is not at
7273 	 * least 10GB
7274 	 */
7275 	if (pf->hw.func_caps.npar_enable &&
7276 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7277 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7278 		netdev_warn(vsi->netdev,
7279 			    "The partition detected link speed that is less than 10Gbps\n");
7280 
7281 	switch (pf->hw.phy.link_info.link_speed) {
7282 	case I40E_LINK_SPEED_40GB:
7283 		speed = "40 G";
7284 		break;
7285 	case I40E_LINK_SPEED_20GB:
7286 		speed = "20 G";
7287 		break;
7288 	case I40E_LINK_SPEED_25GB:
7289 		speed = "25 G";
7290 		break;
7291 	case I40E_LINK_SPEED_10GB:
7292 		speed = "10 G";
7293 		break;
7294 	case I40E_LINK_SPEED_5GB:
7295 		speed = "5 G";
7296 		break;
7297 	case I40E_LINK_SPEED_2_5GB:
7298 		speed = "2.5 G";
7299 		break;
7300 	case I40E_LINK_SPEED_1GB:
7301 		speed = "1000 M";
7302 		break;
7303 	case I40E_LINK_SPEED_100MB:
7304 		speed = "100 M";
7305 		break;
7306 	default:
7307 		break;
7308 	}
7309 
7310 	switch (pf->hw.fc.current_mode) {
7311 	case I40E_FC_FULL:
7312 		fc = "RX/TX";
7313 		break;
7314 	case I40E_FC_TX_PAUSE:
7315 		fc = "TX";
7316 		break;
7317 	case I40E_FC_RX_PAUSE:
7318 		fc = "RX";
7319 		break;
7320 	default:
7321 		fc = "None";
7322 		break;
7323 	}
7324 
7325 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7326 		req_fec = "None";
7327 		fec = "None";
7328 		an = "False";
7329 
7330 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7331 			an = "True";
7332 
7333 		if (pf->hw.phy.link_info.fec_info &
7334 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7335 			fec = "CL74 FC-FEC/BASE-R";
7336 		else if (pf->hw.phy.link_info.fec_info &
7337 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7338 			fec = "CL108 RS-FEC";
7339 
7340 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7341 		 * both RS and FC are requested
7342 		 */
7343 		if (vsi->back->hw.phy.link_info.req_fec_info &
7344 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7345 			if (vsi->back->hw.phy.link_info.req_fec_info &
7346 			    I40E_AQ_REQUEST_FEC_RS)
7347 				req_fec = "CL108 RS-FEC";
7348 			else
7349 				req_fec = "CL74 FC-FEC/BASE-R";
7350 		}
7351 		netdev_info(vsi->netdev,
7352 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7353 			    speed, req_fec, fec, an, fc);
7354 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7355 		req_fec = "None";
7356 		fec = "None";
7357 		an = "False";
7358 
7359 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7360 			an = "True";
7361 
7362 		if (pf->hw.phy.link_info.fec_info &
7363 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7364 			fec = "CL74 FC-FEC/BASE-R";
7365 
7366 		if (pf->hw.phy.link_info.req_fec_info &
7367 		    I40E_AQ_REQUEST_FEC_KR)
7368 			req_fec = "CL74 FC-FEC/BASE-R";
7369 
7370 		netdev_info(vsi->netdev,
7371 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7372 			    speed, req_fec, fec, an, fc);
7373 	} else {
7374 		netdev_info(vsi->netdev,
7375 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7376 			    speed, fc);
7377 	}
7378 
7379 }
7380 
7381 /**
7382  * i40e_up_complete - Finish the last steps of bringing up a connection
7383  * @vsi: the VSI being configured
7384  **/
7385 static int i40e_up_complete(struct i40e_vsi *vsi)
7386 {
7387 	struct i40e_pf *pf = vsi->back;
7388 	int err;
7389 
7390 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7391 		i40e_vsi_configure_msix(vsi);
7392 	else
7393 		i40e_configure_msi_and_legacy(vsi);
7394 
7395 	/* start rings */
7396 	err = i40e_vsi_start_rings(vsi);
7397 	if (err)
7398 		return err;
7399 
7400 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7401 	i40e_napi_enable_all(vsi);
7402 	i40e_vsi_enable_irq(vsi);
7403 
7404 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7405 	    (vsi->netdev)) {
7406 		i40e_print_link_message(vsi, true);
7407 		netif_tx_start_all_queues(vsi->netdev);
7408 		netif_carrier_on(vsi->netdev);
7409 	}
7410 
7411 	/* replay FDIR SB filters */
7412 	if (vsi->type == I40E_VSI_FDIR) {
7413 		/* reset fd counters */
7414 		pf->fd_add_err = 0;
7415 		pf->fd_atr_cnt = 0;
7416 		i40e_fdir_filter_restore(vsi);
7417 	}
7418 
7419 	/* On the next run of the service_task, notify any clients of the new
7420 	 * opened netdev
7421 	 */
7422 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7423 	i40e_service_event_schedule(pf);
7424 
7425 	return 0;
7426 }
7427 
7428 /**
7429  * i40e_vsi_reinit_locked - Reset the VSI
7430  * @vsi: the VSI being configured
7431  *
7432  * Rebuild the ring structs after some configuration
7433  * has changed, e.g. MTU size.
7434  **/
7435 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7436 {
7437 	struct i40e_pf *pf = vsi->back;
7438 
7439 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7440 		usleep_range(1000, 2000);
7441 	i40e_down(vsi);
7442 
7443 	i40e_up(vsi);
7444 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7445 }
7446 
7447 /**
7448  * i40e_force_link_state - Force the link status
7449  * @pf: board private structure
7450  * @is_up: whether the link state should be forced up or down
7451  **/
7452 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7453 {
7454 	struct i40e_aq_get_phy_abilities_resp abilities;
7455 	struct i40e_aq_set_phy_config config = {0};
7456 	bool non_zero_phy_type = is_up;
7457 	struct i40e_hw *hw = &pf->hw;
7458 	u64 mask;
7459 	u8 speed;
7460 	int err;
7461 
7462 	/* Card might've been put in an unstable state by other drivers
7463 	 * and applications, which causes incorrect speed values being
7464 	 * set on startup. In order to clear speed registers, we call
7465 	 * get_phy_capabilities twice, once to get initial state of
7466 	 * available speeds, and once to get current PHY config.
7467 	 */
7468 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7469 					   NULL);
7470 	if (err) {
7471 		dev_err(&pf->pdev->dev,
7472 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7473 			ERR_PTR(err),
7474 			i40e_aq_str(hw, hw->aq.asq_last_status));
7475 		return err;
7476 	}
7477 	speed = abilities.link_speed;
7478 
7479 	/* Get the current phy config */
7480 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7481 					   NULL);
7482 	if (err) {
7483 		dev_err(&pf->pdev->dev,
7484 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7485 			ERR_PTR(err),
7486 			i40e_aq_str(hw, hw->aq.asq_last_status));
7487 		return err;
7488 	}
7489 
7490 	/* If link needs to go up, but was not forced to go down,
7491 	 * and its speed values are OK, no need for a flap
7492 	 * if non_zero_phy_type was set, still need to force up
7493 	 */
7494 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7495 		non_zero_phy_type = true;
7496 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7497 		return 0;
7498 
7499 	/* To force link we need to set bits for all supported PHY types,
7500 	 * but there are now more than 32, so we need to split the bitmap
7501 	 * across two fields.
7502 	 */
7503 	mask = I40E_PHY_TYPES_BITMASK;
7504 	config.phy_type =
7505 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7506 	config.phy_type_ext =
7507 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7508 	/* Copy the old settings, except of phy_type */
7509 	config.abilities = abilities.abilities;
7510 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7511 		if (is_up)
7512 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7513 		else
7514 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7515 	}
7516 	if (abilities.link_speed != 0)
7517 		config.link_speed = abilities.link_speed;
7518 	else
7519 		config.link_speed = speed;
7520 	config.eee_capability = abilities.eee_capability;
7521 	config.eeer = abilities.eeer_val;
7522 	config.low_power_ctrl = abilities.d3_lpan;
7523 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7524 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7525 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7526 
7527 	if (err) {
7528 		dev_err(&pf->pdev->dev,
7529 			"set phy config ret =  %pe last_status =  %s\n",
7530 			ERR_PTR(err),
7531 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7532 		return err;
7533 	}
7534 
7535 	/* Update the link info */
7536 	err = i40e_update_link_info(hw);
7537 	if (err) {
7538 		/* Wait a little bit (on 40G cards it sometimes takes a really
7539 		 * long time for link to come back from the atomic reset)
7540 		 * and try once more
7541 		 */
7542 		msleep(1000);
7543 		i40e_update_link_info(hw);
7544 	}
7545 
7546 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7547 
7548 	return 0;
7549 }
7550 
7551 /**
7552  * i40e_up - Bring the connection back up after being down
7553  * @vsi: the VSI being configured
7554  **/
7555 int i40e_up(struct i40e_vsi *vsi)
7556 {
7557 	int err;
7558 
7559 	if (vsi->type == I40E_VSI_MAIN &&
7560 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7561 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7562 		i40e_force_link_state(vsi->back, true);
7563 
7564 	err = i40e_vsi_configure(vsi);
7565 	if (!err)
7566 		err = i40e_up_complete(vsi);
7567 
7568 	return err;
7569 }
7570 
7571 /**
7572  * i40e_down - Shutdown the connection processing
7573  * @vsi: the VSI being stopped
7574  **/
7575 void i40e_down(struct i40e_vsi *vsi)
7576 {
7577 	int i;
7578 
7579 	/* It is assumed that the caller of this function
7580 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7581 	 */
7582 	if (vsi->netdev) {
7583 		netif_carrier_off(vsi->netdev);
7584 		netif_tx_disable(vsi->netdev);
7585 	}
7586 	i40e_vsi_disable_irq(vsi);
7587 	i40e_vsi_stop_rings(vsi);
7588 	if (vsi->type == I40E_VSI_MAIN &&
7589 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7590 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7591 		i40e_force_link_state(vsi->back, false);
7592 	i40e_napi_disable_all(vsi);
7593 
7594 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7595 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7596 		if (i40e_enabled_xdp_vsi(vsi)) {
7597 			/* Make sure that in-progress ndo_xdp_xmit and
7598 			 * ndo_xsk_wakeup calls are completed.
7599 			 */
7600 			synchronize_rcu();
7601 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7602 		}
7603 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7604 	}
7605 
7606 }
7607 
7608 /**
7609  * i40e_validate_mqprio_qopt- validate queue mapping info
7610  * @vsi: the VSI being configured
7611  * @mqprio_qopt: queue parametrs
7612  **/
7613 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7614 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7615 {
7616 	u64 sum_max_rate = 0;
7617 	u64 max_rate = 0;
7618 	int i;
7619 
7620 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7621 	    mqprio_qopt->qopt.num_tc < 1 ||
7622 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7623 		return -EINVAL;
7624 	for (i = 0; ; i++) {
7625 		if (!mqprio_qopt->qopt.count[i])
7626 			return -EINVAL;
7627 		if (mqprio_qopt->min_rate[i]) {
7628 			dev_err(&vsi->back->pdev->dev,
7629 				"Invalid min tx rate (greater than 0) specified\n");
7630 			return -EINVAL;
7631 		}
7632 		max_rate = mqprio_qopt->max_rate[i];
7633 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7634 		sum_max_rate += max_rate;
7635 
7636 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7637 			break;
7638 		if (mqprio_qopt->qopt.offset[i + 1] !=
7639 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7640 			return -EINVAL;
7641 	}
7642 	if (vsi->num_queue_pairs <
7643 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7644 		dev_err(&vsi->back->pdev->dev,
7645 			"Failed to create traffic channel, insufficient number of queues.\n");
7646 		return -EINVAL;
7647 	}
7648 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7649 		dev_err(&vsi->back->pdev->dev,
7650 			"Invalid max tx rate specified\n");
7651 		return -EINVAL;
7652 	}
7653 	return 0;
7654 }
7655 
7656 /**
7657  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7658  * @vsi: the VSI being configured
7659  **/
7660 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7661 {
7662 	u16 qcount;
7663 	int i;
7664 
7665 	/* Only TC0 is enabled */
7666 	vsi->tc_config.numtc = 1;
7667 	vsi->tc_config.enabled_tc = 1;
7668 	qcount = min_t(int, vsi->alloc_queue_pairs,
7669 		       i40e_pf_get_max_q_per_tc(vsi->back));
7670 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7671 		/* For the TC that is not enabled set the offset to default
7672 		 * queue and allocate one queue for the given TC.
7673 		 */
7674 		vsi->tc_config.tc_info[i].qoffset = 0;
7675 		if (i == 0)
7676 			vsi->tc_config.tc_info[i].qcount = qcount;
7677 		else
7678 			vsi->tc_config.tc_info[i].qcount = 1;
7679 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7680 	}
7681 }
7682 
7683 /**
7684  * i40e_del_macvlan_filter
7685  * @hw: pointer to the HW structure
7686  * @seid: seid of the channel VSI
7687  * @macaddr: the mac address to apply as a filter
7688  * @aq_err: store the admin Q error
7689  *
7690  * This function deletes a mac filter on the channel VSI which serves as the
7691  * macvlan. Returns 0 on success.
7692  **/
7693 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7694 				   const u8 *macaddr, int *aq_err)
7695 {
7696 	struct i40e_aqc_remove_macvlan_element_data element;
7697 	int status;
7698 
7699 	memset(&element, 0, sizeof(element));
7700 	ether_addr_copy(element.mac_addr, macaddr);
7701 	element.vlan_tag = 0;
7702 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7703 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7704 	*aq_err = hw->aq.asq_last_status;
7705 
7706 	return status;
7707 }
7708 
7709 /**
7710  * i40e_add_macvlan_filter
7711  * @hw: pointer to the HW structure
7712  * @seid: seid of the channel VSI
7713  * @macaddr: the mac address to apply as a filter
7714  * @aq_err: store the admin Q error
7715  *
7716  * This function adds a mac filter on the channel VSI which serves as the
7717  * macvlan. Returns 0 on success.
7718  **/
7719 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7720 				   const u8 *macaddr, int *aq_err)
7721 {
7722 	struct i40e_aqc_add_macvlan_element_data element;
7723 	u16 cmd_flags = 0;
7724 	int status;
7725 
7726 	ether_addr_copy(element.mac_addr, macaddr);
7727 	element.vlan_tag = 0;
7728 	element.queue_number = 0;
7729 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7730 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7731 	element.flags = cpu_to_le16(cmd_flags);
7732 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7733 	*aq_err = hw->aq.asq_last_status;
7734 
7735 	return status;
7736 }
7737 
7738 /**
7739  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7740  * @vsi: the VSI we want to access
7741  * @ch: the channel we want to access
7742  */
7743 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7744 {
7745 	struct i40e_ring *tx_ring, *rx_ring;
7746 	u16 pf_q;
7747 	int i;
7748 
7749 	for (i = 0; i < ch->num_queue_pairs; i++) {
7750 		pf_q = ch->base_queue + i;
7751 		tx_ring = vsi->tx_rings[pf_q];
7752 		tx_ring->ch = NULL;
7753 		rx_ring = vsi->rx_rings[pf_q];
7754 		rx_ring->ch = NULL;
7755 	}
7756 }
7757 
7758 /**
7759  * i40e_free_macvlan_channels
7760  * @vsi: the VSI we want to access
7761  *
7762  * This function frees the Qs of the channel VSI from
7763  * the stack and also deletes the channel VSIs which
7764  * serve as macvlans.
7765  */
7766 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7767 {
7768 	struct i40e_channel *ch, *ch_tmp;
7769 	int ret;
7770 
7771 	if (list_empty(&vsi->macvlan_list))
7772 		return;
7773 
7774 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7775 		struct i40e_vsi *parent_vsi;
7776 
7777 		if (i40e_is_channel_macvlan(ch)) {
7778 			i40e_reset_ch_rings(vsi, ch);
7779 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7780 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7781 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7782 			kfree(ch->fwd);
7783 			ch->fwd = NULL;
7784 		}
7785 
7786 		list_del(&ch->list);
7787 		parent_vsi = ch->parent_vsi;
7788 		if (!parent_vsi || !ch->initialized) {
7789 			kfree(ch);
7790 			continue;
7791 		}
7792 
7793 		/* remove the VSI */
7794 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7795 					     NULL);
7796 		if (ret)
7797 			dev_err(&vsi->back->pdev->dev,
7798 				"unable to remove channel (%d) for parent VSI(%d)\n",
7799 				ch->seid, parent_vsi->seid);
7800 		kfree(ch);
7801 	}
7802 	vsi->macvlan_cnt = 0;
7803 }
7804 
7805 /**
7806  * i40e_fwd_ring_up - bring the macvlan device up
7807  * @vsi: the VSI we want to access
7808  * @vdev: macvlan netdevice
7809  * @fwd: the private fwd structure
7810  */
7811 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7812 			    struct i40e_fwd_adapter *fwd)
7813 {
7814 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7815 	int ret = 0, num_tc = 1,  i, aq_err;
7816 	struct i40e_pf *pf = vsi->back;
7817 	struct i40e_hw *hw = &pf->hw;
7818 
7819 	/* Go through the list and find an available channel */
7820 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7821 		if (!i40e_is_channel_macvlan(iter)) {
7822 			iter->fwd = fwd;
7823 			/* record configuration for macvlan interface in vdev */
7824 			for (i = 0; i < num_tc; i++)
7825 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7826 							     i,
7827 							     iter->num_queue_pairs,
7828 							     iter->base_queue);
7829 			for (i = 0; i < iter->num_queue_pairs; i++) {
7830 				struct i40e_ring *tx_ring, *rx_ring;
7831 				u16 pf_q;
7832 
7833 				pf_q = iter->base_queue + i;
7834 
7835 				/* Get to TX ring ptr */
7836 				tx_ring = vsi->tx_rings[pf_q];
7837 				tx_ring->ch = iter;
7838 
7839 				/* Get the RX ring ptr */
7840 				rx_ring = vsi->rx_rings[pf_q];
7841 				rx_ring->ch = iter;
7842 			}
7843 			ch = iter;
7844 			break;
7845 		}
7846 	}
7847 
7848 	if (!ch)
7849 		return -EINVAL;
7850 
7851 	/* Guarantee all rings are updated before we update the
7852 	 * MAC address filter.
7853 	 */
7854 	wmb();
7855 
7856 	/* Add a mac filter */
7857 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7858 	if (ret) {
7859 		/* if we cannot add the MAC rule then disable the offload */
7860 		macvlan_release_l2fw_offload(vdev);
7861 		for (i = 0; i < ch->num_queue_pairs; i++) {
7862 			struct i40e_ring *rx_ring;
7863 			u16 pf_q;
7864 
7865 			pf_q = ch->base_queue + i;
7866 			rx_ring = vsi->rx_rings[pf_q];
7867 			rx_ring->netdev = NULL;
7868 		}
7869 		dev_info(&pf->pdev->dev,
7870 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7871 			  ERR_PTR(ret),
7872 			  i40e_aq_str(hw, aq_err));
7873 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7874 	}
7875 
7876 	return ret;
7877 }
7878 
7879 /**
7880  * i40e_setup_macvlans - create the channels which will be macvlans
7881  * @vsi: the VSI we want to access
7882  * @macvlan_cnt: no. of macvlans to be setup
7883  * @qcnt: no. of Qs per macvlan
7884  * @vdev: macvlan netdevice
7885  */
7886 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7887 			       struct net_device *vdev)
7888 {
7889 	struct i40e_pf *pf = vsi->back;
7890 	struct i40e_hw *hw = &pf->hw;
7891 	struct i40e_vsi_context ctxt;
7892 	u16 sections, qmap, num_qps;
7893 	struct i40e_channel *ch;
7894 	int i, pow, ret = 0;
7895 	u8 offset = 0;
7896 
7897 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7898 		return -EINVAL;
7899 
7900 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7901 
7902 	/* find the next higher power-of-2 of num queue pairs */
7903 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7904 
7905 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7906 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7907 
7908 	/* Setup context bits for the main VSI */
7909 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7910 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7911 	memset(&ctxt, 0, sizeof(ctxt));
7912 	ctxt.seid = vsi->seid;
7913 	ctxt.pf_num = vsi->back->hw.pf_id;
7914 	ctxt.vf_num = 0;
7915 	ctxt.uplink_seid = vsi->uplink_seid;
7916 	ctxt.info = vsi->info;
7917 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7918 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7919 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7920 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7921 
7922 	/* Reconfigure RSS for main VSI with new max queue count */
7923 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7924 	ret = i40e_vsi_config_rss(vsi);
7925 	if (ret) {
7926 		dev_info(&pf->pdev->dev,
7927 			 "Failed to reconfig RSS for num_queues (%u)\n",
7928 			 vsi->rss_size);
7929 		return ret;
7930 	}
7931 	vsi->reconfig_rss = true;
7932 	dev_dbg(&vsi->back->pdev->dev,
7933 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7934 	vsi->next_base_queue = num_qps;
7935 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7936 
7937 	/* Update the VSI after updating the VSI queue-mapping
7938 	 * information
7939 	 */
7940 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7941 	if (ret) {
7942 		dev_info(&pf->pdev->dev,
7943 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7944 			 ERR_PTR(ret),
7945 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7946 		return ret;
7947 	}
7948 	/* update the local VSI info with updated queue map */
7949 	i40e_vsi_update_queue_map(vsi, &ctxt);
7950 	vsi->info.valid_sections = 0;
7951 
7952 	/* Create channels for macvlans */
7953 	INIT_LIST_HEAD(&vsi->macvlan_list);
7954 	for (i = 0; i < macvlan_cnt; i++) {
7955 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7956 		if (!ch) {
7957 			ret = -ENOMEM;
7958 			goto err_free;
7959 		}
7960 		INIT_LIST_HEAD(&ch->list);
7961 		ch->num_queue_pairs = qcnt;
7962 		if (!i40e_setup_channel(pf, vsi, ch)) {
7963 			ret = -EINVAL;
7964 			kfree(ch);
7965 			goto err_free;
7966 		}
7967 		ch->parent_vsi = vsi;
7968 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7969 		vsi->macvlan_cnt++;
7970 		list_add_tail(&ch->list, &vsi->macvlan_list);
7971 	}
7972 
7973 	return ret;
7974 
7975 err_free:
7976 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7977 	i40e_free_macvlan_channels(vsi);
7978 
7979 	return ret;
7980 }
7981 
7982 /**
7983  * i40e_fwd_add - configure macvlans
7984  * @netdev: net device to configure
7985  * @vdev: macvlan netdevice
7986  **/
7987 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7988 {
7989 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7990 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7991 	struct i40e_vsi *vsi = np->vsi;
7992 	struct i40e_pf *pf = vsi->back;
7993 	struct i40e_fwd_adapter *fwd;
7994 	int avail_macvlan, ret;
7995 
7996 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
7997 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7998 		return ERR_PTR(-EINVAL);
7999 	}
8000 	if (i40e_is_tc_mqprio_enabled(pf)) {
8001 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8002 		return ERR_PTR(-EINVAL);
8003 	}
8004 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8005 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8006 		return ERR_PTR(-EINVAL);
8007 	}
8008 
8009 	/* The macvlan device has to be a single Q device so that the
8010 	 * tc_to_txq field can be reused to pick the tx queue.
8011 	 */
8012 	if (netif_is_multiqueue(vdev))
8013 		return ERR_PTR(-ERANGE);
8014 
8015 	if (!vsi->macvlan_cnt) {
8016 		/* reserve bit 0 for the pf device */
8017 		set_bit(0, vsi->fwd_bitmask);
8018 
8019 		/* Try to reserve as many queues as possible for macvlans. First
8020 		 * reserve 3/4th of max vectors, then half, then quarter and
8021 		 * calculate Qs per macvlan as you go
8022 		 */
8023 		vectors = pf->num_lan_msix;
8024 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8025 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8026 			q_per_macvlan = 4;
8027 			macvlan_cnt = (vectors - 32) / 4;
8028 		} else if (vectors <= 64 && vectors > 32) {
8029 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8030 			q_per_macvlan = 2;
8031 			macvlan_cnt = (vectors - 16) / 2;
8032 		} else if (vectors <= 32 && vectors > 16) {
8033 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8034 			q_per_macvlan = 1;
8035 			macvlan_cnt = vectors - 16;
8036 		} else if (vectors <= 16 && vectors > 8) {
8037 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8038 			q_per_macvlan = 1;
8039 			macvlan_cnt = vectors - 8;
8040 		} else {
8041 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8042 			q_per_macvlan = 1;
8043 			macvlan_cnt = vectors - 1;
8044 		}
8045 
8046 		if (macvlan_cnt == 0)
8047 			return ERR_PTR(-EBUSY);
8048 
8049 		/* Quiesce VSI queues */
8050 		i40e_quiesce_vsi(vsi);
8051 
8052 		/* sets up the macvlans but does not "enable" them */
8053 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8054 					  vdev);
8055 		if (ret)
8056 			return ERR_PTR(ret);
8057 
8058 		/* Unquiesce VSI */
8059 		i40e_unquiesce_vsi(vsi);
8060 	}
8061 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8062 					    vsi->macvlan_cnt);
8063 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8064 		return ERR_PTR(-EBUSY);
8065 
8066 	/* create the fwd struct */
8067 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8068 	if (!fwd)
8069 		return ERR_PTR(-ENOMEM);
8070 
8071 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8072 	fwd->bit_no = avail_macvlan;
8073 	netdev_set_sb_channel(vdev, avail_macvlan);
8074 	fwd->netdev = vdev;
8075 
8076 	if (!netif_running(netdev))
8077 		return fwd;
8078 
8079 	/* Set fwd ring up */
8080 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8081 	if (ret) {
8082 		/* unbind the queues and drop the subordinate channel config */
8083 		netdev_unbind_sb_channel(netdev, vdev);
8084 		netdev_set_sb_channel(vdev, 0);
8085 
8086 		kfree(fwd);
8087 		return ERR_PTR(-EINVAL);
8088 	}
8089 
8090 	return fwd;
8091 }
8092 
8093 /**
8094  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8095  * @vsi: the VSI we want to access
8096  */
8097 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8098 {
8099 	struct i40e_channel *ch, *ch_tmp;
8100 	struct i40e_pf *pf = vsi->back;
8101 	struct i40e_hw *hw = &pf->hw;
8102 	int aq_err, ret = 0;
8103 
8104 	if (list_empty(&vsi->macvlan_list))
8105 		return;
8106 
8107 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8108 		if (i40e_is_channel_macvlan(ch)) {
8109 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8110 						      i40e_channel_mac(ch),
8111 						      &aq_err);
8112 			if (!ret) {
8113 				/* Reset queue contexts */
8114 				i40e_reset_ch_rings(vsi, ch);
8115 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8116 				netdev_unbind_sb_channel(vsi->netdev,
8117 							 ch->fwd->netdev);
8118 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8119 				kfree(ch->fwd);
8120 				ch->fwd = NULL;
8121 			}
8122 		}
8123 	}
8124 }
8125 
8126 /**
8127  * i40e_fwd_del - delete macvlan interfaces
8128  * @netdev: net device to configure
8129  * @vdev: macvlan netdevice
8130  */
8131 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8132 {
8133 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8134 	struct i40e_fwd_adapter *fwd = vdev;
8135 	struct i40e_channel *ch, *ch_tmp;
8136 	struct i40e_vsi *vsi = np->vsi;
8137 	struct i40e_pf *pf = vsi->back;
8138 	struct i40e_hw *hw = &pf->hw;
8139 	int aq_err, ret = 0;
8140 
8141 	/* Find the channel associated with the macvlan and del mac filter */
8142 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8143 		if (i40e_is_channel_macvlan(ch) &&
8144 		    ether_addr_equal(i40e_channel_mac(ch),
8145 				     fwd->netdev->dev_addr)) {
8146 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8147 						      i40e_channel_mac(ch),
8148 						      &aq_err);
8149 			if (!ret) {
8150 				/* Reset queue contexts */
8151 				i40e_reset_ch_rings(vsi, ch);
8152 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8153 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8154 				netdev_set_sb_channel(fwd->netdev, 0);
8155 				kfree(ch->fwd);
8156 				ch->fwd = NULL;
8157 			} else {
8158 				dev_info(&pf->pdev->dev,
8159 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8160 					  ERR_PTR(ret),
8161 					  i40e_aq_str(hw, aq_err));
8162 			}
8163 			break;
8164 		}
8165 	}
8166 }
8167 
8168 /**
8169  * i40e_setup_tc - configure multiple traffic classes
8170  * @netdev: net device to configure
8171  * @type_data: tc offload data
8172  **/
8173 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8174 {
8175 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8176 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8177 	struct i40e_vsi *vsi = np->vsi;
8178 	struct i40e_pf *pf = vsi->back;
8179 	u8 enabled_tc = 0, num_tc, hw;
8180 	bool need_reset = false;
8181 	int old_queue_pairs;
8182 	int ret = -EINVAL;
8183 	u16 mode;
8184 	int i;
8185 
8186 	old_queue_pairs = vsi->num_queue_pairs;
8187 	num_tc = mqprio_qopt->qopt.num_tc;
8188 	hw = mqprio_qopt->qopt.hw;
8189 	mode = mqprio_qopt->mode;
8190 	if (!hw) {
8191 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8192 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8193 		goto config_tc;
8194 	}
8195 
8196 	/* Check if MFP enabled */
8197 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8198 		netdev_info(netdev,
8199 			    "Configuring TC not supported in MFP mode\n");
8200 		return ret;
8201 	}
8202 	switch (mode) {
8203 	case TC_MQPRIO_MODE_DCB:
8204 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8205 
8206 		/* Check if DCB enabled to continue */
8207 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8208 			netdev_info(netdev,
8209 				    "DCB is not enabled for adapter\n");
8210 			return ret;
8211 		}
8212 
8213 		/* Check whether tc count is within enabled limit */
8214 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8215 			netdev_info(netdev,
8216 				    "TC count greater than enabled on link for adapter\n");
8217 			return ret;
8218 		}
8219 		break;
8220 	case TC_MQPRIO_MODE_CHANNEL:
8221 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8222 			netdev_info(netdev,
8223 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8224 			return ret;
8225 		}
8226 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8227 			return ret;
8228 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8229 		if (ret)
8230 			return ret;
8231 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8232 		       sizeof(*mqprio_qopt));
8233 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8234 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8235 		break;
8236 	default:
8237 		return -EINVAL;
8238 	}
8239 
8240 config_tc:
8241 	/* Generate TC map for number of tc requested */
8242 	for (i = 0; i < num_tc; i++)
8243 		enabled_tc |= BIT(i);
8244 
8245 	/* Requesting same TC configuration as already enabled */
8246 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8247 	    mode != TC_MQPRIO_MODE_CHANNEL)
8248 		return 0;
8249 
8250 	/* Quiesce VSI queues */
8251 	i40e_quiesce_vsi(vsi);
8252 
8253 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8254 		i40e_remove_queue_channels(vsi);
8255 
8256 	/* Configure VSI for enabled TCs */
8257 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8258 	if (ret) {
8259 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8260 			    vsi->seid);
8261 		need_reset = true;
8262 		goto exit;
8263 	} else if (enabled_tc &&
8264 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8265 		netdev_info(netdev,
8266 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8267 			    vsi->tc_config.tc_info[0].qcount);
8268 		ret = -EINVAL;
8269 		need_reset = true;
8270 		goto exit;
8271 	}
8272 
8273 	dev_info(&vsi->back->pdev->dev,
8274 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8275 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8276 
8277 	if (i40e_is_tc_mqprio_enabled(pf)) {
8278 		if (vsi->mqprio_qopt.max_rate[0]) {
8279 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8280 						  vsi->mqprio_qopt.max_rate[0]);
8281 
8282 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8283 			if (!ret) {
8284 				u64 credits = max_tx_rate;
8285 
8286 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8287 				dev_dbg(&vsi->back->pdev->dev,
8288 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8289 					max_tx_rate,
8290 					credits,
8291 					vsi->seid);
8292 			} else {
8293 				need_reset = true;
8294 				goto exit;
8295 			}
8296 		}
8297 		ret = i40e_configure_queue_channels(vsi);
8298 		if (ret) {
8299 			vsi->num_queue_pairs = old_queue_pairs;
8300 			netdev_info(netdev,
8301 				    "Failed configuring queue channels\n");
8302 			need_reset = true;
8303 			goto exit;
8304 		}
8305 	}
8306 
8307 exit:
8308 	/* Reset the configuration data to defaults, only TC0 is enabled */
8309 	if (need_reset) {
8310 		i40e_vsi_set_default_tc_config(vsi);
8311 		need_reset = false;
8312 	}
8313 
8314 	/* Unquiesce VSI */
8315 	i40e_unquiesce_vsi(vsi);
8316 	return ret;
8317 }
8318 
8319 /**
8320  * i40e_set_cld_element - sets cloud filter element data
8321  * @filter: cloud filter rule
8322  * @cld: ptr to cloud filter element data
8323  *
8324  * This is helper function to copy data into cloud filter element
8325  **/
8326 static inline void
8327 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8328 		     struct i40e_aqc_cloud_filters_element_data *cld)
8329 {
8330 	u32 ipa;
8331 	int i;
8332 
8333 	memset(cld, 0, sizeof(*cld));
8334 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8335 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8336 
8337 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8338 		return;
8339 
8340 	if (filter->n_proto == ETH_P_IPV6) {
8341 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8342 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8343 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8344 
8345 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8346 		}
8347 	} else {
8348 		ipa = be32_to_cpu(filter->dst_ipv4);
8349 
8350 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8351 	}
8352 
8353 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8354 
8355 	/* tenant_id is not supported by FW now, once the support is enabled
8356 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8357 	 */
8358 	if (filter->tenant_id)
8359 		return;
8360 }
8361 
8362 /**
8363  * i40e_add_del_cloud_filter - Add/del cloud filter
8364  * @vsi: pointer to VSI
8365  * @filter: cloud filter rule
8366  * @add: if true, add, if false, delete
8367  *
8368  * Add or delete a cloud filter for a specific flow spec.
8369  * Returns 0 if the filter were successfully added.
8370  **/
8371 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8372 			      struct i40e_cloud_filter *filter, bool add)
8373 {
8374 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8375 	struct i40e_pf *pf = vsi->back;
8376 	int ret;
8377 	static const u16 flag_table[128] = {
8378 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8379 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8380 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8381 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8382 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8383 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8384 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8385 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8386 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8387 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8388 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8389 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8390 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8391 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8392 	};
8393 
8394 	if (filter->flags >= ARRAY_SIZE(flag_table))
8395 		return -EIO;
8396 
8397 	memset(&cld_filter, 0, sizeof(cld_filter));
8398 
8399 	/* copy element needed to add cloud filter from filter */
8400 	i40e_set_cld_element(filter, &cld_filter);
8401 
8402 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8403 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8404 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8405 
8406 	if (filter->n_proto == ETH_P_IPV6)
8407 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8408 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8409 	else
8410 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8411 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8412 
8413 	if (add)
8414 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8415 						&cld_filter, 1);
8416 	else
8417 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8418 						&cld_filter, 1);
8419 	if (ret)
8420 		dev_dbg(&pf->pdev->dev,
8421 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8422 			add ? "add" : "delete", filter->dst_port, ret,
8423 			pf->hw.aq.asq_last_status);
8424 	else
8425 		dev_info(&pf->pdev->dev,
8426 			 "%s cloud filter for VSI: %d\n",
8427 			 add ? "Added" : "Deleted", filter->seid);
8428 	return ret;
8429 }
8430 
8431 /**
8432  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8433  * @vsi: pointer to VSI
8434  * @filter: cloud filter rule
8435  * @add: if true, add, if false, delete
8436  *
8437  * Add or delete a cloud filter for a specific flow spec using big buffer.
8438  * Returns 0 if the filter were successfully added.
8439  **/
8440 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8441 				      struct i40e_cloud_filter *filter,
8442 				      bool add)
8443 {
8444 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8445 	struct i40e_pf *pf = vsi->back;
8446 	int ret;
8447 
8448 	/* Both (src/dst) valid mac_addr are not supported */
8449 	if ((is_valid_ether_addr(filter->dst_mac) &&
8450 	     is_valid_ether_addr(filter->src_mac)) ||
8451 	    (is_multicast_ether_addr(filter->dst_mac) &&
8452 	     is_multicast_ether_addr(filter->src_mac)))
8453 		return -EOPNOTSUPP;
8454 
8455 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8456 	 * ports are not supported via big buffer now.
8457 	 */
8458 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8459 		return -EOPNOTSUPP;
8460 
8461 	/* adding filter using src_port/src_ip is not supported at this stage */
8462 	if (filter->src_port ||
8463 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8464 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8465 		return -EOPNOTSUPP;
8466 
8467 	memset(&cld_filter, 0, sizeof(cld_filter));
8468 
8469 	/* copy element needed to add cloud filter from filter */
8470 	i40e_set_cld_element(filter, &cld_filter.element);
8471 
8472 	if (is_valid_ether_addr(filter->dst_mac) ||
8473 	    is_valid_ether_addr(filter->src_mac) ||
8474 	    is_multicast_ether_addr(filter->dst_mac) ||
8475 	    is_multicast_ether_addr(filter->src_mac)) {
8476 		/* MAC + IP : unsupported mode */
8477 		if (filter->dst_ipv4)
8478 			return -EOPNOTSUPP;
8479 
8480 		/* since we validated that L4 port must be valid before
8481 		 * we get here, start with respective "flags" value
8482 		 * and update if vlan is present or not
8483 		 */
8484 		cld_filter.element.flags =
8485 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8486 
8487 		if (filter->vlan_id) {
8488 			cld_filter.element.flags =
8489 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8490 		}
8491 
8492 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8493 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8494 		cld_filter.element.flags =
8495 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8496 		if (filter->n_proto == ETH_P_IPV6)
8497 			cld_filter.element.flags |=
8498 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8499 		else
8500 			cld_filter.element.flags |=
8501 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8502 	} else {
8503 		dev_err(&pf->pdev->dev,
8504 			"either mac or ip has to be valid for cloud filter\n");
8505 		return -EINVAL;
8506 	}
8507 
8508 	/* Now copy L4 port in Byte 6..7 in general fields */
8509 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8510 						be16_to_cpu(filter->dst_port);
8511 
8512 	if (add) {
8513 		/* Validate current device switch mode, change if necessary */
8514 		ret = i40e_validate_and_set_switch_mode(vsi);
8515 		if (ret) {
8516 			dev_err(&pf->pdev->dev,
8517 				"failed to set switch mode, ret %d\n",
8518 				ret);
8519 			return ret;
8520 		}
8521 
8522 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8523 						   &cld_filter, 1);
8524 	} else {
8525 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8526 						   &cld_filter, 1);
8527 	}
8528 
8529 	if (ret)
8530 		dev_dbg(&pf->pdev->dev,
8531 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8532 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8533 	else
8534 		dev_info(&pf->pdev->dev,
8535 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8536 			 add ? "add" : "delete", filter->seid,
8537 			 ntohs(filter->dst_port));
8538 	return ret;
8539 }
8540 
8541 /**
8542  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8543  * @vsi: Pointer to VSI
8544  * @f: Pointer to struct flow_cls_offload
8545  * @filter: Pointer to cloud filter structure
8546  *
8547  **/
8548 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8549 				 struct flow_cls_offload *f,
8550 				 struct i40e_cloud_filter *filter)
8551 {
8552 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8553 	struct flow_dissector *dissector = rule->match.dissector;
8554 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8555 	struct i40e_pf *pf = vsi->back;
8556 	u8 field_flags = 0;
8557 
8558 	if (dissector->used_keys &
8559 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8560 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8561 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8562 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8563 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8564 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8565 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8566 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8567 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8568 			dissector->used_keys);
8569 		return -EOPNOTSUPP;
8570 	}
8571 
8572 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8573 		struct flow_match_enc_keyid match;
8574 
8575 		flow_rule_match_enc_keyid(rule, &match);
8576 		if (match.mask->keyid != 0)
8577 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8578 
8579 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8580 	}
8581 
8582 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8583 		struct flow_match_basic match;
8584 
8585 		flow_rule_match_basic(rule, &match);
8586 		n_proto_key = ntohs(match.key->n_proto);
8587 		n_proto_mask = ntohs(match.mask->n_proto);
8588 
8589 		if (n_proto_key == ETH_P_ALL) {
8590 			n_proto_key = 0;
8591 			n_proto_mask = 0;
8592 		}
8593 		filter->n_proto = n_proto_key & n_proto_mask;
8594 		filter->ip_proto = match.key->ip_proto;
8595 	}
8596 
8597 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8598 		struct flow_match_eth_addrs match;
8599 
8600 		flow_rule_match_eth_addrs(rule, &match);
8601 
8602 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8603 		if (!is_zero_ether_addr(match.mask->dst)) {
8604 			if (is_broadcast_ether_addr(match.mask->dst)) {
8605 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8606 			} else {
8607 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8608 					match.mask->dst);
8609 				return -EIO;
8610 			}
8611 		}
8612 
8613 		if (!is_zero_ether_addr(match.mask->src)) {
8614 			if (is_broadcast_ether_addr(match.mask->src)) {
8615 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8616 			} else {
8617 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8618 					match.mask->src);
8619 				return -EIO;
8620 			}
8621 		}
8622 		ether_addr_copy(filter->dst_mac, match.key->dst);
8623 		ether_addr_copy(filter->src_mac, match.key->src);
8624 	}
8625 
8626 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8627 		struct flow_match_vlan match;
8628 
8629 		flow_rule_match_vlan(rule, &match);
8630 		if (match.mask->vlan_id) {
8631 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8632 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8633 
8634 			} else {
8635 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8636 					match.mask->vlan_id);
8637 				return -EIO;
8638 			}
8639 		}
8640 
8641 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8642 	}
8643 
8644 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8645 		struct flow_match_control match;
8646 
8647 		flow_rule_match_control(rule, &match);
8648 		addr_type = match.key->addr_type;
8649 	}
8650 
8651 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8652 		struct flow_match_ipv4_addrs match;
8653 
8654 		flow_rule_match_ipv4_addrs(rule, &match);
8655 		if (match.mask->dst) {
8656 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8657 				field_flags |= I40E_CLOUD_FIELD_IIP;
8658 			} else {
8659 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8660 					&match.mask->dst);
8661 				return -EIO;
8662 			}
8663 		}
8664 
8665 		if (match.mask->src) {
8666 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8667 				field_flags |= I40E_CLOUD_FIELD_IIP;
8668 			} else {
8669 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8670 					&match.mask->src);
8671 				return -EIO;
8672 			}
8673 		}
8674 
8675 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8676 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8677 			return -EIO;
8678 		}
8679 		filter->dst_ipv4 = match.key->dst;
8680 		filter->src_ipv4 = match.key->src;
8681 	}
8682 
8683 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8684 		struct flow_match_ipv6_addrs match;
8685 
8686 		flow_rule_match_ipv6_addrs(rule, &match);
8687 
8688 		/* src and dest IPV6 address should not be LOOPBACK
8689 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8690 		 */
8691 		if (ipv6_addr_loopback(&match.key->dst) ||
8692 		    ipv6_addr_loopback(&match.key->src)) {
8693 			dev_err(&pf->pdev->dev,
8694 				"Bad ipv6, addr is LOOPBACK\n");
8695 			return -EIO;
8696 		}
8697 		if (!ipv6_addr_any(&match.mask->dst) ||
8698 		    !ipv6_addr_any(&match.mask->src))
8699 			field_flags |= I40E_CLOUD_FIELD_IIP;
8700 
8701 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8702 		       sizeof(filter->src_ipv6));
8703 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8704 		       sizeof(filter->dst_ipv6));
8705 	}
8706 
8707 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8708 		struct flow_match_ports match;
8709 
8710 		flow_rule_match_ports(rule, &match);
8711 		if (match.mask->src) {
8712 			if (match.mask->src == cpu_to_be16(0xffff)) {
8713 				field_flags |= I40E_CLOUD_FIELD_IIP;
8714 			} else {
8715 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8716 					be16_to_cpu(match.mask->src));
8717 				return -EIO;
8718 			}
8719 		}
8720 
8721 		if (match.mask->dst) {
8722 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8723 				field_flags |= I40E_CLOUD_FIELD_IIP;
8724 			} else {
8725 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8726 					be16_to_cpu(match.mask->dst));
8727 				return -EIO;
8728 			}
8729 		}
8730 
8731 		filter->dst_port = match.key->dst;
8732 		filter->src_port = match.key->src;
8733 
8734 		switch (filter->ip_proto) {
8735 		case IPPROTO_TCP:
8736 		case IPPROTO_UDP:
8737 			break;
8738 		default:
8739 			dev_err(&pf->pdev->dev,
8740 				"Only UDP and TCP transport are supported\n");
8741 			return -EINVAL;
8742 		}
8743 	}
8744 	filter->flags = field_flags;
8745 	return 0;
8746 }
8747 
8748 /**
8749  * i40e_handle_tclass: Forward to a traffic class on the device
8750  * @vsi: Pointer to VSI
8751  * @tc: traffic class index on the device
8752  * @filter: Pointer to cloud filter structure
8753  *
8754  **/
8755 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8756 			      struct i40e_cloud_filter *filter)
8757 {
8758 	struct i40e_channel *ch, *ch_tmp;
8759 
8760 	/* direct to a traffic class on the same device */
8761 	if (tc == 0) {
8762 		filter->seid = vsi->seid;
8763 		return 0;
8764 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8765 		if (!filter->dst_port) {
8766 			dev_err(&vsi->back->pdev->dev,
8767 				"Specify destination port to direct to traffic class that is not default\n");
8768 			return -EINVAL;
8769 		}
8770 		if (list_empty(&vsi->ch_list))
8771 			return -EINVAL;
8772 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8773 					 list) {
8774 			if (ch->seid == vsi->tc_seid_map[tc])
8775 				filter->seid = ch->seid;
8776 		}
8777 		return 0;
8778 	}
8779 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8780 	return -EINVAL;
8781 }
8782 
8783 /**
8784  * i40e_configure_clsflower - Configure tc flower filters
8785  * @vsi: Pointer to VSI
8786  * @cls_flower: Pointer to struct flow_cls_offload
8787  *
8788  **/
8789 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8790 				    struct flow_cls_offload *cls_flower)
8791 {
8792 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8793 	struct i40e_cloud_filter *filter = NULL;
8794 	struct i40e_pf *pf = vsi->back;
8795 	int err = 0;
8796 
8797 	if (tc < 0) {
8798 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8799 		return -EOPNOTSUPP;
8800 	}
8801 
8802 	if (!tc) {
8803 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8804 		return -EINVAL;
8805 	}
8806 
8807 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8808 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8809 		return -EBUSY;
8810 
8811 	if (pf->fdir_pf_active_filters ||
8812 	    (!hlist_empty(&pf->fdir_filter_list))) {
8813 		dev_err(&vsi->back->pdev->dev,
8814 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8815 		return -EINVAL;
8816 	}
8817 
8818 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8819 		dev_err(&vsi->back->pdev->dev,
8820 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8821 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8822 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8823 	}
8824 
8825 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8826 	if (!filter)
8827 		return -ENOMEM;
8828 
8829 	filter->cookie = cls_flower->cookie;
8830 
8831 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8832 	if (err < 0)
8833 		goto err;
8834 
8835 	err = i40e_handle_tclass(vsi, tc, filter);
8836 	if (err < 0)
8837 		goto err;
8838 
8839 	/* Add cloud filter */
8840 	if (filter->dst_port)
8841 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8842 	else
8843 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8844 
8845 	if (err) {
8846 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8847 			err);
8848 		goto err;
8849 	}
8850 
8851 	/* add filter to the ordered list */
8852 	INIT_HLIST_NODE(&filter->cloud_node);
8853 
8854 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8855 
8856 	pf->num_cloud_filters++;
8857 
8858 	return err;
8859 err:
8860 	kfree(filter);
8861 	return err;
8862 }
8863 
8864 /**
8865  * i40e_find_cloud_filter - Find the could filter in the list
8866  * @vsi: Pointer to VSI
8867  * @cookie: filter specific cookie
8868  *
8869  **/
8870 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8871 							unsigned long *cookie)
8872 {
8873 	struct i40e_cloud_filter *filter = NULL;
8874 	struct hlist_node *node2;
8875 
8876 	hlist_for_each_entry_safe(filter, node2,
8877 				  &vsi->back->cloud_filter_list, cloud_node)
8878 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8879 			return filter;
8880 	return NULL;
8881 }
8882 
8883 /**
8884  * i40e_delete_clsflower - Remove tc flower filters
8885  * @vsi: Pointer to VSI
8886  * @cls_flower: Pointer to struct flow_cls_offload
8887  *
8888  **/
8889 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8890 				 struct flow_cls_offload *cls_flower)
8891 {
8892 	struct i40e_cloud_filter *filter = NULL;
8893 	struct i40e_pf *pf = vsi->back;
8894 	int err = 0;
8895 
8896 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8897 
8898 	if (!filter)
8899 		return -EINVAL;
8900 
8901 	hash_del(&filter->cloud_node);
8902 
8903 	if (filter->dst_port)
8904 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8905 	else
8906 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8907 
8908 	kfree(filter);
8909 	if (err) {
8910 		dev_err(&pf->pdev->dev,
8911 			"Failed to delete cloud filter, err %pe\n",
8912 			ERR_PTR(err));
8913 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8914 	}
8915 
8916 	pf->num_cloud_filters--;
8917 	if (!pf->num_cloud_filters)
8918 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8919 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8920 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8921 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8922 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8923 		}
8924 	return 0;
8925 }
8926 
8927 /**
8928  * i40e_setup_tc_cls_flower - flower classifier offloads
8929  * @np: net device to configure
8930  * @cls_flower: offload data
8931  **/
8932 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8933 				    struct flow_cls_offload *cls_flower)
8934 {
8935 	struct i40e_vsi *vsi = np->vsi;
8936 
8937 	switch (cls_flower->command) {
8938 	case FLOW_CLS_REPLACE:
8939 		return i40e_configure_clsflower(vsi, cls_flower);
8940 	case FLOW_CLS_DESTROY:
8941 		return i40e_delete_clsflower(vsi, cls_flower);
8942 	case FLOW_CLS_STATS:
8943 		return -EOPNOTSUPP;
8944 	default:
8945 		return -EOPNOTSUPP;
8946 	}
8947 }
8948 
8949 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8950 				  void *cb_priv)
8951 {
8952 	struct i40e_netdev_priv *np = cb_priv;
8953 
8954 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8955 		return -EOPNOTSUPP;
8956 
8957 	switch (type) {
8958 	case TC_SETUP_CLSFLOWER:
8959 		return i40e_setup_tc_cls_flower(np, type_data);
8960 
8961 	default:
8962 		return -EOPNOTSUPP;
8963 	}
8964 }
8965 
8966 static LIST_HEAD(i40e_block_cb_list);
8967 
8968 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8969 			   void *type_data)
8970 {
8971 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8972 
8973 	switch (type) {
8974 	case TC_SETUP_QDISC_MQPRIO:
8975 		return i40e_setup_tc(netdev, type_data);
8976 	case TC_SETUP_BLOCK:
8977 		return flow_block_cb_setup_simple(type_data,
8978 						  &i40e_block_cb_list,
8979 						  i40e_setup_tc_block_cb,
8980 						  np, np, true);
8981 	default:
8982 		return -EOPNOTSUPP;
8983 	}
8984 }
8985 
8986 /**
8987  * i40e_open - Called when a network interface is made active
8988  * @netdev: network interface device structure
8989  *
8990  * The open entry point is called when a network interface is made
8991  * active by the system (IFF_UP).  At this point all resources needed
8992  * for transmit and receive operations are allocated, the interrupt
8993  * handler is registered with the OS, the netdev watchdog subtask is
8994  * enabled, and the stack is notified that the interface is ready.
8995  *
8996  * Returns 0 on success, negative value on failure
8997  **/
8998 int i40e_open(struct net_device *netdev)
8999 {
9000 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9001 	struct i40e_vsi *vsi = np->vsi;
9002 	struct i40e_pf *pf = vsi->back;
9003 	int err;
9004 
9005 	/* disallow open during test or if eeprom is broken */
9006 	if (test_bit(__I40E_TESTING, pf->state) ||
9007 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9008 		return -EBUSY;
9009 
9010 	netif_carrier_off(netdev);
9011 
9012 	if (i40e_force_link_state(pf, true))
9013 		return -EAGAIN;
9014 
9015 	err = i40e_vsi_open(vsi);
9016 	if (err)
9017 		return err;
9018 
9019 	/* configure global TSO hardware offload settings */
9020 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9021 						       TCP_FLAG_FIN) >> 16);
9022 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9023 						       TCP_FLAG_FIN |
9024 						       TCP_FLAG_CWR) >> 16);
9025 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9026 	udp_tunnel_get_rx_info(netdev);
9027 
9028 	return 0;
9029 }
9030 
9031 /**
9032  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9033  * @vsi: vsi structure
9034  *
9035  * This updates netdev's number of tx/rx queues
9036  *
9037  * Returns status of setting tx/rx queues
9038  **/
9039 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9040 {
9041 	int ret;
9042 
9043 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9044 					   vsi->num_queue_pairs);
9045 	if (ret)
9046 		return ret;
9047 
9048 	return netif_set_real_num_tx_queues(vsi->netdev,
9049 					    vsi->num_queue_pairs);
9050 }
9051 
9052 /**
9053  * i40e_vsi_open -
9054  * @vsi: the VSI to open
9055  *
9056  * Finish initialization of the VSI.
9057  *
9058  * Returns 0 on success, negative value on failure
9059  *
9060  * Note: expects to be called while under rtnl_lock()
9061  **/
9062 int i40e_vsi_open(struct i40e_vsi *vsi)
9063 {
9064 	struct i40e_pf *pf = vsi->back;
9065 	char int_name[I40E_INT_NAME_STR_LEN];
9066 	int err;
9067 
9068 	/* allocate descriptors */
9069 	err = i40e_vsi_setup_tx_resources(vsi);
9070 	if (err)
9071 		goto err_setup_tx;
9072 	err = i40e_vsi_setup_rx_resources(vsi);
9073 	if (err)
9074 		goto err_setup_rx;
9075 
9076 	err = i40e_vsi_configure(vsi);
9077 	if (err)
9078 		goto err_setup_rx;
9079 
9080 	if (vsi->netdev) {
9081 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9082 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9083 		err = i40e_vsi_request_irq(vsi, int_name);
9084 		if (err)
9085 			goto err_setup_rx;
9086 
9087 		/* Notify the stack of the actual queue counts. */
9088 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9089 		if (err)
9090 			goto err_set_queues;
9091 
9092 	} else if (vsi->type == I40E_VSI_FDIR) {
9093 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9094 			 dev_driver_string(&pf->pdev->dev),
9095 			 dev_name(&pf->pdev->dev));
9096 		err = i40e_vsi_request_irq(vsi, int_name);
9097 		if (err)
9098 			goto err_setup_rx;
9099 
9100 	} else {
9101 		err = -EINVAL;
9102 		goto err_setup_rx;
9103 	}
9104 
9105 	err = i40e_up_complete(vsi);
9106 	if (err)
9107 		goto err_up_complete;
9108 
9109 	return 0;
9110 
9111 err_up_complete:
9112 	i40e_down(vsi);
9113 err_set_queues:
9114 	i40e_vsi_free_irq(vsi);
9115 err_setup_rx:
9116 	i40e_vsi_free_rx_resources(vsi);
9117 err_setup_tx:
9118 	i40e_vsi_free_tx_resources(vsi);
9119 	if (vsi == pf->vsi[pf->lan_vsi])
9120 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9121 
9122 	return err;
9123 }
9124 
9125 /**
9126  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9127  * @pf: Pointer to PF
9128  *
9129  * This function destroys the hlist where all the Flow Director
9130  * filters were saved.
9131  **/
9132 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9133 {
9134 	struct i40e_fdir_filter *filter;
9135 	struct i40e_flex_pit *pit_entry, *tmp;
9136 	struct hlist_node *node2;
9137 
9138 	hlist_for_each_entry_safe(filter, node2,
9139 				  &pf->fdir_filter_list, fdir_node) {
9140 		hlist_del(&filter->fdir_node);
9141 		kfree(filter);
9142 	}
9143 
9144 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9145 		list_del(&pit_entry->list);
9146 		kfree(pit_entry);
9147 	}
9148 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9149 
9150 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9151 		list_del(&pit_entry->list);
9152 		kfree(pit_entry);
9153 	}
9154 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9155 
9156 	pf->fdir_pf_active_filters = 0;
9157 	i40e_reset_fdir_filter_cnt(pf);
9158 
9159 	/* Reprogram the default input set for TCP/IPv4 */
9160 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9161 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9162 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9163 
9164 	/* Reprogram the default input set for TCP/IPv6 */
9165 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9166 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9167 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9168 
9169 	/* Reprogram the default input set for UDP/IPv4 */
9170 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9171 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9172 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9173 
9174 	/* Reprogram the default input set for UDP/IPv6 */
9175 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9176 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9177 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9178 
9179 	/* Reprogram the default input set for SCTP/IPv4 */
9180 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9181 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9182 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9183 
9184 	/* Reprogram the default input set for SCTP/IPv6 */
9185 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9186 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9187 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9188 
9189 	/* Reprogram the default input set for Other/IPv4 */
9190 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9191 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9192 
9193 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9194 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9195 
9196 	/* Reprogram the default input set for Other/IPv6 */
9197 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9198 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9199 
9200 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9201 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9202 }
9203 
9204 /**
9205  * i40e_cloud_filter_exit - Cleans up the cloud filters
9206  * @pf: Pointer to PF
9207  *
9208  * This function destroys the hlist where all the cloud filters
9209  * were saved.
9210  **/
9211 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9212 {
9213 	struct i40e_cloud_filter *cfilter;
9214 	struct hlist_node *node;
9215 
9216 	hlist_for_each_entry_safe(cfilter, node,
9217 				  &pf->cloud_filter_list, cloud_node) {
9218 		hlist_del(&cfilter->cloud_node);
9219 		kfree(cfilter);
9220 	}
9221 	pf->num_cloud_filters = 0;
9222 
9223 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9224 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9225 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9226 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9227 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9228 	}
9229 }
9230 
9231 /**
9232  * i40e_close - Disables a network interface
9233  * @netdev: network interface device structure
9234  *
9235  * The close entry point is called when an interface is de-activated
9236  * by the OS.  The hardware is still under the driver's control, but
9237  * this netdev interface is disabled.
9238  *
9239  * Returns 0, this is not allowed to fail
9240  **/
9241 int i40e_close(struct net_device *netdev)
9242 {
9243 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9244 	struct i40e_vsi *vsi = np->vsi;
9245 
9246 	i40e_vsi_close(vsi);
9247 
9248 	return 0;
9249 }
9250 
9251 /**
9252  * i40e_do_reset - Start a PF or Core Reset sequence
9253  * @pf: board private structure
9254  * @reset_flags: which reset is requested
9255  * @lock_acquired: indicates whether or not the lock has been acquired
9256  * before this function was called.
9257  *
9258  * The essential difference in resets is that the PF Reset
9259  * doesn't clear the packet buffers, doesn't reset the PE
9260  * firmware, and doesn't bother the other PFs on the chip.
9261  **/
9262 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9263 {
9264 	u32 val;
9265 
9266 	/* do the biggest reset indicated */
9267 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9268 
9269 		/* Request a Global Reset
9270 		 *
9271 		 * This will start the chip's countdown to the actual full
9272 		 * chip reset event, and a warning interrupt to be sent
9273 		 * to all PFs, including the requestor.  Our handler
9274 		 * for the warning interrupt will deal with the shutdown
9275 		 * and recovery of the switch setup.
9276 		 */
9277 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9278 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9279 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9280 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9281 
9282 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9283 
9284 		/* Request a Core Reset
9285 		 *
9286 		 * Same as Global Reset, except does *not* include the MAC/PHY
9287 		 */
9288 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9289 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9290 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9291 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9292 		i40e_flush(&pf->hw);
9293 
9294 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9295 
9296 		/* Request a PF Reset
9297 		 *
9298 		 * Resets only the PF-specific registers
9299 		 *
9300 		 * This goes directly to the tear-down and rebuild of
9301 		 * the switch, since we need to do all the recovery as
9302 		 * for the Core Reset.
9303 		 */
9304 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9305 		i40e_handle_reset_warning(pf, lock_acquired);
9306 
9307 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9308 		/* Request a PF Reset
9309 		 *
9310 		 * Resets PF and reinitializes PFs VSI.
9311 		 */
9312 		i40e_prep_for_reset(pf);
9313 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9314 		dev_info(&pf->pdev->dev,
9315 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9316 			 "FW LLDP is disabled\n" :
9317 			 "FW LLDP is enabled\n");
9318 
9319 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9320 		int v;
9321 
9322 		/* Find the VSI(s) that requested a re-init */
9323 		dev_info(&pf->pdev->dev,
9324 			 "VSI reinit requested\n");
9325 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9326 			struct i40e_vsi *vsi = pf->vsi[v];
9327 
9328 			if (vsi != NULL &&
9329 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9330 					       vsi->state))
9331 				i40e_vsi_reinit_locked(pf->vsi[v]);
9332 		}
9333 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9334 		int v;
9335 
9336 		/* Find the VSI(s) that needs to be brought down */
9337 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9338 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9339 			struct i40e_vsi *vsi = pf->vsi[v];
9340 
9341 			if (vsi != NULL &&
9342 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9343 					       vsi->state)) {
9344 				set_bit(__I40E_VSI_DOWN, vsi->state);
9345 				i40e_down(vsi);
9346 			}
9347 		}
9348 	} else {
9349 		dev_info(&pf->pdev->dev,
9350 			 "bad reset request 0x%08x\n", reset_flags);
9351 	}
9352 }
9353 
9354 #ifdef CONFIG_I40E_DCB
9355 /**
9356  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9357  * @pf: board private structure
9358  * @old_cfg: current DCB config
9359  * @new_cfg: new DCB config
9360  **/
9361 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9362 			    struct i40e_dcbx_config *old_cfg,
9363 			    struct i40e_dcbx_config *new_cfg)
9364 {
9365 	bool need_reconfig = false;
9366 
9367 	/* Check if ETS configuration has changed */
9368 	if (memcmp(&new_cfg->etscfg,
9369 		   &old_cfg->etscfg,
9370 		   sizeof(new_cfg->etscfg))) {
9371 		/* If Priority Table has changed reconfig is needed */
9372 		if (memcmp(&new_cfg->etscfg.prioritytable,
9373 			   &old_cfg->etscfg.prioritytable,
9374 			   sizeof(new_cfg->etscfg.prioritytable))) {
9375 			need_reconfig = true;
9376 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9377 		}
9378 
9379 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9380 			   &old_cfg->etscfg.tcbwtable,
9381 			   sizeof(new_cfg->etscfg.tcbwtable)))
9382 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9383 
9384 		if (memcmp(&new_cfg->etscfg.tsatable,
9385 			   &old_cfg->etscfg.tsatable,
9386 			   sizeof(new_cfg->etscfg.tsatable)))
9387 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9388 	}
9389 
9390 	/* Check if PFC configuration has changed */
9391 	if (memcmp(&new_cfg->pfc,
9392 		   &old_cfg->pfc,
9393 		   sizeof(new_cfg->pfc))) {
9394 		need_reconfig = true;
9395 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9396 	}
9397 
9398 	/* Check if APP Table has changed */
9399 	if (memcmp(&new_cfg->app,
9400 		   &old_cfg->app,
9401 		   sizeof(new_cfg->app))) {
9402 		need_reconfig = true;
9403 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9404 	}
9405 
9406 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9407 	return need_reconfig;
9408 }
9409 
9410 /**
9411  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9412  * @pf: board private structure
9413  * @e: event info posted on ARQ
9414  **/
9415 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9416 				  struct i40e_arq_event_info *e)
9417 {
9418 	struct i40e_aqc_lldp_get_mib *mib =
9419 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9420 	struct i40e_hw *hw = &pf->hw;
9421 	struct i40e_dcbx_config tmp_dcbx_cfg;
9422 	bool need_reconfig = false;
9423 	int ret = 0;
9424 	u8 type;
9425 
9426 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9427 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9428 	    (hw->phy.link_info.link_speed &
9429 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9430 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9431 		/* let firmware decide if the DCB should be disabled */
9432 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9433 
9434 	/* Not DCB capable or capability disabled */
9435 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9436 		return ret;
9437 
9438 	/* Ignore if event is not for Nearest Bridge */
9439 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9440 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9441 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9442 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9443 		return ret;
9444 
9445 	/* Check MIB Type and return if event for Remote MIB update */
9446 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9447 	dev_dbg(&pf->pdev->dev,
9448 		"LLDP event mib type %s\n", type ? "remote" : "local");
9449 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9450 		/* Update the remote cached instance and return */
9451 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9452 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9453 				&hw->remote_dcbx_config);
9454 		goto exit;
9455 	}
9456 
9457 	/* Store the old configuration */
9458 	tmp_dcbx_cfg = hw->local_dcbx_config;
9459 
9460 	/* Reset the old DCBx configuration data */
9461 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9462 	/* Get updated DCBX data from firmware */
9463 	ret = i40e_get_dcb_config(&pf->hw);
9464 	if (ret) {
9465 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9466 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9467 		    (hw->phy.link_info.link_speed &
9468 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9469 			dev_warn(&pf->pdev->dev,
9470 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9471 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9472 		} else {
9473 			dev_info(&pf->pdev->dev,
9474 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9475 				 ERR_PTR(ret),
9476 				 i40e_aq_str(&pf->hw,
9477 					     pf->hw.aq.asq_last_status));
9478 		}
9479 		goto exit;
9480 	}
9481 
9482 	/* No change detected in DCBX configs */
9483 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9484 		    sizeof(tmp_dcbx_cfg))) {
9485 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9486 		goto exit;
9487 	}
9488 
9489 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9490 					       &hw->local_dcbx_config);
9491 
9492 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9493 
9494 	if (!need_reconfig)
9495 		goto exit;
9496 
9497 	/* Enable DCB tagging only when more than one TC */
9498 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9499 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9500 	else
9501 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9502 
9503 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9504 	/* Reconfiguration needed quiesce all VSIs */
9505 	i40e_pf_quiesce_all_vsi(pf);
9506 
9507 	/* Changes in configuration update VEB/VSI */
9508 	i40e_dcb_reconfigure(pf);
9509 
9510 	ret = i40e_resume_port_tx(pf);
9511 
9512 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9513 	/* In case of error no point in resuming VSIs */
9514 	if (ret)
9515 		goto exit;
9516 
9517 	/* Wait for the PF's queues to be disabled */
9518 	ret = i40e_pf_wait_queues_disabled(pf);
9519 	if (ret) {
9520 		/* Schedule PF reset to recover */
9521 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9522 		i40e_service_event_schedule(pf);
9523 	} else {
9524 		i40e_pf_unquiesce_all_vsi(pf);
9525 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9526 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9527 	}
9528 
9529 exit:
9530 	return ret;
9531 }
9532 #endif /* CONFIG_I40E_DCB */
9533 
9534 /**
9535  * i40e_do_reset_safe - Protected reset path for userland calls.
9536  * @pf: board private structure
9537  * @reset_flags: which reset is requested
9538  *
9539  **/
9540 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9541 {
9542 	rtnl_lock();
9543 	i40e_do_reset(pf, reset_flags, true);
9544 	rtnl_unlock();
9545 }
9546 
9547 /**
9548  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9549  * @pf: board private structure
9550  * @e: event info posted on ARQ
9551  *
9552  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9553  * and VF queues
9554  **/
9555 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9556 					   struct i40e_arq_event_info *e)
9557 {
9558 	struct i40e_aqc_lan_overflow *data =
9559 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9560 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9561 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9562 	struct i40e_hw *hw = &pf->hw;
9563 	struct i40e_vf *vf;
9564 	u16 vf_id;
9565 
9566 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9567 		queue, qtx_ctl);
9568 
9569 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9570 	    I40E_QTX_CTL_VF_QUEUE)
9571 		return;
9572 
9573 	/* Queue belongs to VF, find the VF and issue VF reset */
9574 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9575 	vf_id -= hw->func_caps.vf_base_id;
9576 	vf = &pf->vf[vf_id];
9577 	i40e_vc_notify_vf_reset(vf);
9578 	/* Allow VF to process pending reset notification */
9579 	msleep(20);
9580 	i40e_reset_vf(vf, false);
9581 }
9582 
9583 /**
9584  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9585  * @pf: board private structure
9586  **/
9587 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9588 {
9589 	u32 val, fcnt_prog;
9590 
9591 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9592 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9593 	return fcnt_prog;
9594 }
9595 
9596 /**
9597  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9598  * @pf: board private structure
9599  **/
9600 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9601 {
9602 	u32 val, fcnt_prog;
9603 
9604 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9605 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9606 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9607 	return fcnt_prog;
9608 }
9609 
9610 /**
9611  * i40e_get_global_fd_count - Get total FD filters programmed on device
9612  * @pf: board private structure
9613  **/
9614 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9615 {
9616 	u32 val, fcnt_prog;
9617 
9618 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9619 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9620 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9621 	return fcnt_prog;
9622 }
9623 
9624 /**
9625  * i40e_reenable_fdir_sb - Restore FDir SB capability
9626  * @pf: board private structure
9627  **/
9628 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9629 {
9630 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9631 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9632 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9633 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9634 }
9635 
9636 /**
9637  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9638  * @pf: board private structure
9639  **/
9640 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9641 {
9642 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9643 		/* ATR uses the same filtering logic as SB rules. It only
9644 		 * functions properly if the input set mask is at the default
9645 		 * settings. It is safe to restore the default input set
9646 		 * because there are no active TCPv4 filter rules.
9647 		 */
9648 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9649 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9650 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9651 
9652 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9653 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9654 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9655 	}
9656 }
9657 
9658 /**
9659  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9660  * @pf: board private structure
9661  * @filter: FDir filter to remove
9662  */
9663 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9664 				       struct i40e_fdir_filter *filter)
9665 {
9666 	/* Update counters */
9667 	pf->fdir_pf_active_filters--;
9668 	pf->fd_inv = 0;
9669 
9670 	switch (filter->flow_type) {
9671 	case TCP_V4_FLOW:
9672 		pf->fd_tcp4_filter_cnt--;
9673 		break;
9674 	case UDP_V4_FLOW:
9675 		pf->fd_udp4_filter_cnt--;
9676 		break;
9677 	case SCTP_V4_FLOW:
9678 		pf->fd_sctp4_filter_cnt--;
9679 		break;
9680 	case TCP_V6_FLOW:
9681 		pf->fd_tcp6_filter_cnt--;
9682 		break;
9683 	case UDP_V6_FLOW:
9684 		pf->fd_udp6_filter_cnt--;
9685 		break;
9686 	case SCTP_V6_FLOW:
9687 		pf->fd_udp6_filter_cnt--;
9688 		break;
9689 	case IP_USER_FLOW:
9690 		switch (filter->ipl4_proto) {
9691 		case IPPROTO_TCP:
9692 			pf->fd_tcp4_filter_cnt--;
9693 			break;
9694 		case IPPROTO_UDP:
9695 			pf->fd_udp4_filter_cnt--;
9696 			break;
9697 		case IPPROTO_SCTP:
9698 			pf->fd_sctp4_filter_cnt--;
9699 			break;
9700 		case IPPROTO_IP:
9701 			pf->fd_ip4_filter_cnt--;
9702 			break;
9703 		}
9704 		break;
9705 	case IPV6_USER_FLOW:
9706 		switch (filter->ipl4_proto) {
9707 		case IPPROTO_TCP:
9708 			pf->fd_tcp6_filter_cnt--;
9709 			break;
9710 		case IPPROTO_UDP:
9711 			pf->fd_udp6_filter_cnt--;
9712 			break;
9713 		case IPPROTO_SCTP:
9714 			pf->fd_sctp6_filter_cnt--;
9715 			break;
9716 		case IPPROTO_IP:
9717 			pf->fd_ip6_filter_cnt--;
9718 			break;
9719 		}
9720 		break;
9721 	}
9722 
9723 	/* Remove the filter from the list and free memory */
9724 	hlist_del(&filter->fdir_node);
9725 	kfree(filter);
9726 }
9727 
9728 /**
9729  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9730  * @pf: board private structure
9731  **/
9732 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9733 {
9734 	struct i40e_fdir_filter *filter;
9735 	u32 fcnt_prog, fcnt_avail;
9736 	struct hlist_node *node;
9737 
9738 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9739 		return;
9740 
9741 	/* Check if we have enough room to re-enable FDir SB capability. */
9742 	fcnt_prog = i40e_get_global_fd_count(pf);
9743 	fcnt_avail = pf->fdir_pf_filter_count;
9744 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9745 	    (pf->fd_add_err == 0) ||
9746 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9747 		i40e_reenable_fdir_sb(pf);
9748 
9749 	/* We should wait for even more space before re-enabling ATR.
9750 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9751 	 * rules active.
9752 	 */
9753 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9754 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9755 		i40e_reenable_fdir_atr(pf);
9756 
9757 	/* if hw had a problem adding a filter, delete it */
9758 	if (pf->fd_inv > 0) {
9759 		hlist_for_each_entry_safe(filter, node,
9760 					  &pf->fdir_filter_list, fdir_node)
9761 			if (filter->fd_id == pf->fd_inv)
9762 				i40e_delete_invalid_filter(pf, filter);
9763 	}
9764 }
9765 
9766 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9767 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9768 /**
9769  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9770  * @pf: board private structure
9771  **/
9772 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9773 {
9774 	unsigned long min_flush_time;
9775 	int flush_wait_retry = 50;
9776 	bool disable_atr = false;
9777 	int fd_room;
9778 	int reg;
9779 
9780 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9781 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9782 		return;
9783 
9784 	/* If the flush is happening too quick and we have mostly SB rules we
9785 	 * should not re-enable ATR for some time.
9786 	 */
9787 	min_flush_time = pf->fd_flush_timestamp +
9788 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9789 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9790 
9791 	if (!(time_after(jiffies, min_flush_time)) &&
9792 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9793 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9794 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9795 		disable_atr = true;
9796 	}
9797 
9798 	pf->fd_flush_timestamp = jiffies;
9799 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9800 	/* flush all filters */
9801 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9802 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9803 	i40e_flush(&pf->hw);
9804 	pf->fd_flush_cnt++;
9805 	pf->fd_add_err = 0;
9806 	do {
9807 		/* Check FD flush status every 5-6msec */
9808 		usleep_range(5000, 6000);
9809 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9810 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9811 			break;
9812 	} while (flush_wait_retry--);
9813 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9814 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9815 	} else {
9816 		/* replay sideband filters */
9817 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9818 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9819 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9820 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9821 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9822 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9823 	}
9824 }
9825 
9826 /**
9827  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9828  * @pf: board private structure
9829  **/
9830 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9831 {
9832 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9833 }
9834 
9835 /**
9836  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9837  * @pf: board private structure
9838  **/
9839 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9840 {
9841 
9842 	/* if interface is down do nothing */
9843 	if (test_bit(__I40E_DOWN, pf->state))
9844 		return;
9845 
9846 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9847 		i40e_fdir_flush_and_replay(pf);
9848 
9849 	i40e_fdir_check_and_reenable(pf);
9850 
9851 }
9852 
9853 /**
9854  * i40e_vsi_link_event - notify VSI of a link event
9855  * @vsi: vsi to be notified
9856  * @link_up: link up or down
9857  **/
9858 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9859 {
9860 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9861 		return;
9862 
9863 	switch (vsi->type) {
9864 	case I40E_VSI_MAIN:
9865 		if (!vsi->netdev || !vsi->netdev_registered)
9866 			break;
9867 
9868 		if (link_up) {
9869 			netif_carrier_on(vsi->netdev);
9870 			netif_tx_wake_all_queues(vsi->netdev);
9871 		} else {
9872 			netif_carrier_off(vsi->netdev);
9873 			netif_tx_stop_all_queues(vsi->netdev);
9874 		}
9875 		break;
9876 
9877 	case I40E_VSI_SRIOV:
9878 	case I40E_VSI_VMDQ2:
9879 	case I40E_VSI_CTRL:
9880 	case I40E_VSI_IWARP:
9881 	case I40E_VSI_MIRROR:
9882 	default:
9883 		/* there is no notification for other VSIs */
9884 		break;
9885 	}
9886 }
9887 
9888 /**
9889  * i40e_veb_link_event - notify elements on the veb of a link event
9890  * @veb: veb to be notified
9891  * @link_up: link up or down
9892  **/
9893 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9894 {
9895 	struct i40e_pf *pf;
9896 	int i;
9897 
9898 	if (!veb || !veb->pf)
9899 		return;
9900 	pf = veb->pf;
9901 
9902 	/* depth first... */
9903 	for (i = 0; i < I40E_MAX_VEB; i++)
9904 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9905 			i40e_veb_link_event(pf->veb[i], link_up);
9906 
9907 	/* ... now the local VSIs */
9908 	for (i = 0; i < pf->num_alloc_vsi; i++)
9909 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9910 			i40e_vsi_link_event(pf->vsi[i], link_up);
9911 }
9912 
9913 /**
9914  * i40e_link_event - Update netif_carrier status
9915  * @pf: board private structure
9916  **/
9917 static void i40e_link_event(struct i40e_pf *pf)
9918 {
9919 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9920 	u8 new_link_speed, old_link_speed;
9921 	bool new_link, old_link;
9922 	int status;
9923 #ifdef CONFIG_I40E_DCB
9924 	int err;
9925 #endif /* CONFIG_I40E_DCB */
9926 
9927 	/* set this to force the get_link_status call to refresh state */
9928 	pf->hw.phy.get_link_info = true;
9929 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9930 	status = i40e_get_link_status(&pf->hw, &new_link);
9931 
9932 	/* On success, disable temp link polling */
9933 	if (status == 0) {
9934 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9935 	} else {
9936 		/* Enable link polling temporarily until i40e_get_link_status
9937 		 * returns 0
9938 		 */
9939 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9940 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9941 			status);
9942 		return;
9943 	}
9944 
9945 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9946 	new_link_speed = pf->hw.phy.link_info.link_speed;
9947 
9948 	if (new_link == old_link &&
9949 	    new_link_speed == old_link_speed &&
9950 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9951 	     new_link == netif_carrier_ok(vsi->netdev)))
9952 		return;
9953 
9954 	i40e_print_link_message(vsi, new_link);
9955 
9956 	/* Notify the base of the switch tree connected to
9957 	 * the link.  Floating VEBs are not notified.
9958 	 */
9959 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9960 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9961 	else
9962 		i40e_vsi_link_event(vsi, new_link);
9963 
9964 	if (pf->vf)
9965 		i40e_vc_notify_link_state(pf);
9966 
9967 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9968 		i40e_ptp_set_increment(pf);
9969 #ifdef CONFIG_I40E_DCB
9970 	if (new_link == old_link)
9971 		return;
9972 	/* Not SW DCB so firmware will take care of default settings */
9973 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9974 		return;
9975 
9976 	/* We cover here only link down, as after link up in case of SW DCB
9977 	 * SW LLDP agent will take care of setting it up
9978 	 */
9979 	if (!new_link) {
9980 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9981 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9982 		err = i40e_dcb_sw_default_config(pf);
9983 		if (err) {
9984 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9985 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9986 		} else {
9987 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9988 				       DCB_CAP_DCBX_VER_IEEE;
9989 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9990 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9991 		}
9992 	}
9993 #endif /* CONFIG_I40E_DCB */
9994 }
9995 
9996 /**
9997  * i40e_watchdog_subtask - periodic checks not using event driven response
9998  * @pf: board private structure
9999  **/
10000 static void i40e_watchdog_subtask(struct i40e_pf *pf)
10001 {
10002 	int i;
10003 
10004 	/* if interface is down do nothing */
10005 	if (test_bit(__I40E_DOWN, pf->state) ||
10006 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
10007 		return;
10008 
10009 	/* make sure we don't do these things too often */
10010 	if (time_before(jiffies, (pf->service_timer_previous +
10011 				  pf->service_timer_period)))
10012 		return;
10013 	pf->service_timer_previous = jiffies;
10014 
10015 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10016 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10017 		i40e_link_event(pf);
10018 
10019 	/* Update the stats for active netdevs so the network stack
10020 	 * can look at updated numbers whenever it cares to
10021 	 */
10022 	for (i = 0; i < pf->num_alloc_vsi; i++)
10023 		if (pf->vsi[i] && pf->vsi[i]->netdev)
10024 			i40e_update_stats(pf->vsi[i]);
10025 
10026 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10027 		/* Update the stats for the active switching components */
10028 		for (i = 0; i < I40E_MAX_VEB; i++)
10029 			if (pf->veb[i])
10030 				i40e_update_veb_stats(pf->veb[i]);
10031 	}
10032 
10033 	i40e_ptp_rx_hang(pf);
10034 	i40e_ptp_tx_hang(pf);
10035 }
10036 
10037 /**
10038  * i40e_reset_subtask - Set up for resetting the device and driver
10039  * @pf: board private structure
10040  **/
10041 static void i40e_reset_subtask(struct i40e_pf *pf)
10042 {
10043 	u32 reset_flags = 0;
10044 
10045 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10046 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10047 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10048 	}
10049 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10050 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10051 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10052 	}
10053 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10054 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10055 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10056 	}
10057 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10058 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10059 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10060 	}
10061 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10062 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10063 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10064 	}
10065 
10066 	/* If there's a recovery already waiting, it takes
10067 	 * precedence before starting a new reset sequence.
10068 	 */
10069 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10070 		i40e_prep_for_reset(pf);
10071 		i40e_reset(pf);
10072 		i40e_rebuild(pf, false, false);
10073 	}
10074 
10075 	/* If we're already down or resetting, just bail */
10076 	if (reset_flags &&
10077 	    !test_bit(__I40E_DOWN, pf->state) &&
10078 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10079 		i40e_do_reset(pf, reset_flags, false);
10080 	}
10081 }
10082 
10083 /**
10084  * i40e_handle_link_event - Handle link event
10085  * @pf: board private structure
10086  * @e: event info posted on ARQ
10087  **/
10088 static void i40e_handle_link_event(struct i40e_pf *pf,
10089 				   struct i40e_arq_event_info *e)
10090 {
10091 	struct i40e_aqc_get_link_status *status =
10092 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10093 
10094 	/* Do a new status request to re-enable LSE reporting
10095 	 * and load new status information into the hw struct
10096 	 * This completely ignores any state information
10097 	 * in the ARQ event info, instead choosing to always
10098 	 * issue the AQ update link status command.
10099 	 */
10100 	i40e_link_event(pf);
10101 
10102 	/* Check if module meets thermal requirements */
10103 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10104 		dev_err(&pf->pdev->dev,
10105 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10106 		dev_err(&pf->pdev->dev,
10107 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10108 	} else {
10109 		/* check for unqualified module, if link is down, suppress
10110 		 * the message if link was forced to be down.
10111 		 */
10112 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10113 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10114 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10115 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10116 			dev_err(&pf->pdev->dev,
10117 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10118 			dev_err(&pf->pdev->dev,
10119 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10120 		}
10121 	}
10122 }
10123 
10124 /**
10125  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10126  * @pf: board private structure
10127  **/
10128 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10129 {
10130 	struct i40e_arq_event_info event;
10131 	struct i40e_hw *hw = &pf->hw;
10132 	u16 pending, i = 0;
10133 	u16 opcode;
10134 	u32 oldval;
10135 	int ret;
10136 	u32 val;
10137 
10138 	/* Do not run clean AQ when PF reset fails */
10139 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10140 		return;
10141 
10142 	/* check for error indications */
10143 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10144 	oldval = val;
10145 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10146 		if (hw->debug_mask & I40E_DEBUG_AQ)
10147 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10148 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10149 	}
10150 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10151 		if (hw->debug_mask & I40E_DEBUG_AQ)
10152 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10153 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10154 		pf->arq_overflows++;
10155 	}
10156 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10157 		if (hw->debug_mask & I40E_DEBUG_AQ)
10158 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10159 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10160 	}
10161 	if (oldval != val)
10162 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10163 
10164 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10165 	oldval = val;
10166 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10167 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10168 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10169 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10170 	}
10171 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10172 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10173 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10174 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10175 	}
10176 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10177 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10178 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10179 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10180 	}
10181 	if (oldval != val)
10182 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10183 
10184 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10185 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10186 	if (!event.msg_buf)
10187 		return;
10188 
10189 	do {
10190 		ret = i40e_clean_arq_element(hw, &event, &pending);
10191 		if (ret == -EALREADY)
10192 			break;
10193 		else if (ret) {
10194 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10195 			break;
10196 		}
10197 
10198 		opcode = le16_to_cpu(event.desc.opcode);
10199 		switch (opcode) {
10200 
10201 		case i40e_aqc_opc_get_link_status:
10202 			rtnl_lock();
10203 			i40e_handle_link_event(pf, &event);
10204 			rtnl_unlock();
10205 			break;
10206 		case i40e_aqc_opc_send_msg_to_pf:
10207 			ret = i40e_vc_process_vf_msg(pf,
10208 					le16_to_cpu(event.desc.retval),
10209 					le32_to_cpu(event.desc.cookie_high),
10210 					le32_to_cpu(event.desc.cookie_low),
10211 					event.msg_buf,
10212 					event.msg_len);
10213 			break;
10214 		case i40e_aqc_opc_lldp_update_mib:
10215 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10216 #ifdef CONFIG_I40E_DCB
10217 			rtnl_lock();
10218 			i40e_handle_lldp_event(pf, &event);
10219 			rtnl_unlock();
10220 #endif /* CONFIG_I40E_DCB */
10221 			break;
10222 		case i40e_aqc_opc_event_lan_overflow:
10223 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10224 			i40e_handle_lan_overflow_event(pf, &event);
10225 			break;
10226 		case i40e_aqc_opc_send_msg_to_peer:
10227 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10228 			break;
10229 		case i40e_aqc_opc_nvm_erase:
10230 		case i40e_aqc_opc_nvm_update:
10231 		case i40e_aqc_opc_oem_post_update:
10232 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10233 				   "ARQ NVM operation 0x%04x completed\n",
10234 				   opcode);
10235 			break;
10236 		default:
10237 			dev_info(&pf->pdev->dev,
10238 				 "ARQ: Unknown event 0x%04x ignored\n",
10239 				 opcode);
10240 			break;
10241 		}
10242 	} while (i++ < I40E_AQ_WORK_LIMIT);
10243 
10244 	if (i < I40E_AQ_WORK_LIMIT)
10245 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10246 
10247 	/* re-enable Admin queue interrupt cause */
10248 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10249 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10250 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10251 	i40e_flush(hw);
10252 
10253 	kfree(event.msg_buf);
10254 }
10255 
10256 /**
10257  * i40e_verify_eeprom - make sure eeprom is good to use
10258  * @pf: board private structure
10259  **/
10260 static void i40e_verify_eeprom(struct i40e_pf *pf)
10261 {
10262 	int err;
10263 
10264 	err = i40e_diag_eeprom_test(&pf->hw);
10265 	if (err) {
10266 		/* retry in case of garbage read */
10267 		err = i40e_diag_eeprom_test(&pf->hw);
10268 		if (err) {
10269 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10270 				 err);
10271 			set_bit(__I40E_BAD_EEPROM, pf->state);
10272 		}
10273 	}
10274 
10275 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10276 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10277 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10278 	}
10279 }
10280 
10281 /**
10282  * i40e_enable_pf_switch_lb
10283  * @pf: pointer to the PF structure
10284  *
10285  * enable switch loop back or die - no point in a return value
10286  **/
10287 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10288 {
10289 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10290 	struct i40e_vsi_context ctxt;
10291 	int ret;
10292 
10293 	ctxt.seid = pf->main_vsi_seid;
10294 	ctxt.pf_num = pf->hw.pf_id;
10295 	ctxt.vf_num = 0;
10296 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10297 	if (ret) {
10298 		dev_info(&pf->pdev->dev,
10299 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10300 			 ERR_PTR(ret),
10301 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10302 		return;
10303 	}
10304 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10305 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10306 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10307 
10308 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10309 	if (ret) {
10310 		dev_info(&pf->pdev->dev,
10311 			 "update vsi switch failed, err %pe aq_err %s\n",
10312 			 ERR_PTR(ret),
10313 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10314 	}
10315 }
10316 
10317 /**
10318  * i40e_disable_pf_switch_lb
10319  * @pf: pointer to the PF structure
10320  *
10321  * disable switch loop back or die - no point in a return value
10322  **/
10323 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10324 {
10325 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10326 	struct i40e_vsi_context ctxt;
10327 	int ret;
10328 
10329 	ctxt.seid = pf->main_vsi_seid;
10330 	ctxt.pf_num = pf->hw.pf_id;
10331 	ctxt.vf_num = 0;
10332 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10333 	if (ret) {
10334 		dev_info(&pf->pdev->dev,
10335 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10336 			 ERR_PTR(ret),
10337 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10338 		return;
10339 	}
10340 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10341 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10342 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10343 
10344 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10345 	if (ret) {
10346 		dev_info(&pf->pdev->dev,
10347 			 "update vsi switch failed, err %pe aq_err %s\n",
10348 			 ERR_PTR(ret),
10349 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10350 	}
10351 }
10352 
10353 /**
10354  * i40e_config_bridge_mode - Configure the HW bridge mode
10355  * @veb: pointer to the bridge instance
10356  *
10357  * Configure the loop back mode for the LAN VSI that is downlink to the
10358  * specified HW bridge instance. It is expected this function is called
10359  * when a new HW bridge is instantiated.
10360  **/
10361 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10362 {
10363 	struct i40e_pf *pf = veb->pf;
10364 
10365 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10366 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10367 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10368 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10369 		i40e_disable_pf_switch_lb(pf);
10370 	else
10371 		i40e_enable_pf_switch_lb(pf);
10372 }
10373 
10374 /**
10375  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10376  * @veb: pointer to the VEB instance
10377  *
10378  * This is a recursive function that first builds the attached VSIs then
10379  * recurses in to build the next layer of VEB.  We track the connections
10380  * through our own index numbers because the seid's from the HW could
10381  * change across the reset.
10382  **/
10383 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10384 {
10385 	struct i40e_vsi *ctl_vsi = NULL;
10386 	struct i40e_pf *pf = veb->pf;
10387 	int v, veb_idx;
10388 	int ret;
10389 
10390 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10391 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10392 		if (pf->vsi[v] &&
10393 		    pf->vsi[v]->veb_idx == veb->idx &&
10394 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10395 			ctl_vsi = pf->vsi[v];
10396 			break;
10397 		}
10398 	}
10399 	if (!ctl_vsi) {
10400 		dev_info(&pf->pdev->dev,
10401 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10402 		ret = -ENOENT;
10403 		goto end_reconstitute;
10404 	}
10405 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10406 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10407 	ret = i40e_add_vsi(ctl_vsi);
10408 	if (ret) {
10409 		dev_info(&pf->pdev->dev,
10410 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10411 			 veb->idx, ret);
10412 		goto end_reconstitute;
10413 	}
10414 	i40e_vsi_reset_stats(ctl_vsi);
10415 
10416 	/* create the VEB in the switch and move the VSI onto the VEB */
10417 	ret = i40e_add_veb(veb, ctl_vsi);
10418 	if (ret)
10419 		goto end_reconstitute;
10420 
10421 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10422 		veb->bridge_mode = BRIDGE_MODE_VEB;
10423 	else
10424 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10425 	i40e_config_bridge_mode(veb);
10426 
10427 	/* create the remaining VSIs attached to this VEB */
10428 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10429 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10430 			continue;
10431 
10432 		if (pf->vsi[v]->veb_idx == veb->idx) {
10433 			struct i40e_vsi *vsi = pf->vsi[v];
10434 
10435 			vsi->uplink_seid = veb->seid;
10436 			ret = i40e_add_vsi(vsi);
10437 			if (ret) {
10438 				dev_info(&pf->pdev->dev,
10439 					 "rebuild of vsi_idx %d failed: %d\n",
10440 					 v, ret);
10441 				goto end_reconstitute;
10442 			}
10443 			i40e_vsi_reset_stats(vsi);
10444 		}
10445 	}
10446 
10447 	/* create any VEBs attached to this VEB - RECURSION */
10448 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10449 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10450 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10451 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10452 			if (ret)
10453 				break;
10454 		}
10455 	}
10456 
10457 end_reconstitute:
10458 	return ret;
10459 }
10460 
10461 /**
10462  * i40e_get_capabilities - get info about the HW
10463  * @pf: the PF struct
10464  * @list_type: AQ capability to be queried
10465  **/
10466 static int i40e_get_capabilities(struct i40e_pf *pf,
10467 				 enum i40e_admin_queue_opc list_type)
10468 {
10469 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10470 	u16 data_size;
10471 	int buf_len;
10472 	int err;
10473 
10474 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10475 	do {
10476 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10477 		if (!cap_buf)
10478 			return -ENOMEM;
10479 
10480 		/* this loads the data into the hw struct for us */
10481 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10482 						    &data_size, list_type,
10483 						    NULL);
10484 		/* data loaded, buffer no longer needed */
10485 		kfree(cap_buf);
10486 
10487 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10488 			/* retry with a larger buffer */
10489 			buf_len = data_size;
10490 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10491 			dev_info(&pf->pdev->dev,
10492 				 "capability discovery failed, err %pe aq_err %s\n",
10493 				 ERR_PTR(err),
10494 				 i40e_aq_str(&pf->hw,
10495 					     pf->hw.aq.asq_last_status));
10496 			return -ENODEV;
10497 		}
10498 	} while (err);
10499 
10500 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10501 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10502 			dev_info(&pf->pdev->dev,
10503 				 "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",
10504 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10505 				 pf->hw.func_caps.num_msix_vectors,
10506 				 pf->hw.func_caps.num_msix_vectors_vf,
10507 				 pf->hw.func_caps.fd_filters_guaranteed,
10508 				 pf->hw.func_caps.fd_filters_best_effort,
10509 				 pf->hw.func_caps.num_tx_qp,
10510 				 pf->hw.func_caps.num_vsis);
10511 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10512 			dev_info(&pf->pdev->dev,
10513 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10514 				 pf->hw.dev_caps.switch_mode,
10515 				 pf->hw.dev_caps.valid_functions);
10516 			dev_info(&pf->pdev->dev,
10517 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10518 				 pf->hw.dev_caps.sr_iov_1_1,
10519 				 pf->hw.dev_caps.num_vfs);
10520 			dev_info(&pf->pdev->dev,
10521 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10522 				 pf->hw.dev_caps.num_vsis,
10523 				 pf->hw.dev_caps.num_rx_qp,
10524 				 pf->hw.dev_caps.num_tx_qp);
10525 		}
10526 	}
10527 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10528 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10529 		       + pf->hw.func_caps.num_vfs)
10530 		if (pf->hw.revision_id == 0 &&
10531 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10532 			dev_info(&pf->pdev->dev,
10533 				 "got num_vsis %d, setting num_vsis to %d\n",
10534 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10535 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10536 		}
10537 	}
10538 	return 0;
10539 }
10540 
10541 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10542 
10543 /**
10544  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10545  * @pf: board private structure
10546  **/
10547 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10548 {
10549 	struct i40e_vsi *vsi;
10550 
10551 	/* quick workaround for an NVM issue that leaves a critical register
10552 	 * uninitialized
10553 	 */
10554 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10555 		static const u32 hkey[] = {
10556 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10557 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10558 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10559 			0x95b3a76d};
10560 		int i;
10561 
10562 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10563 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10564 	}
10565 
10566 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10567 		return;
10568 
10569 	/* find existing VSI and see if it needs configuring */
10570 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10571 
10572 	/* create a new VSI if none exists */
10573 	if (!vsi) {
10574 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10575 				     pf->vsi[pf->lan_vsi]->seid, 0);
10576 		if (!vsi) {
10577 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10578 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10579 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10580 			return;
10581 		}
10582 	}
10583 
10584 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10585 }
10586 
10587 /**
10588  * i40e_fdir_teardown - release the Flow Director resources
10589  * @pf: board private structure
10590  **/
10591 static void i40e_fdir_teardown(struct i40e_pf *pf)
10592 {
10593 	struct i40e_vsi *vsi;
10594 
10595 	i40e_fdir_filter_exit(pf);
10596 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10597 	if (vsi)
10598 		i40e_vsi_release(vsi);
10599 }
10600 
10601 /**
10602  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10603  * @vsi: PF main vsi
10604  * @seid: seid of main or channel VSIs
10605  *
10606  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10607  * existed before reset
10608  **/
10609 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10610 {
10611 	struct i40e_cloud_filter *cfilter;
10612 	struct i40e_pf *pf = vsi->back;
10613 	struct hlist_node *node;
10614 	int ret;
10615 
10616 	/* Add cloud filters back if they exist */
10617 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10618 				  cloud_node) {
10619 		if (cfilter->seid != seid)
10620 			continue;
10621 
10622 		if (cfilter->dst_port)
10623 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10624 								true);
10625 		else
10626 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10627 
10628 		if (ret) {
10629 			dev_dbg(&pf->pdev->dev,
10630 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10631 				ERR_PTR(ret),
10632 				i40e_aq_str(&pf->hw,
10633 					    pf->hw.aq.asq_last_status));
10634 			return ret;
10635 		}
10636 	}
10637 	return 0;
10638 }
10639 
10640 /**
10641  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10642  * @vsi: PF main vsi
10643  *
10644  * Rebuilds channel VSIs if they existed before reset
10645  **/
10646 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10647 {
10648 	struct i40e_channel *ch, *ch_tmp;
10649 	int ret;
10650 
10651 	if (list_empty(&vsi->ch_list))
10652 		return 0;
10653 
10654 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10655 		if (!ch->initialized)
10656 			break;
10657 		/* Proceed with creation of channel (VMDq2) VSI */
10658 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10659 		if (ret) {
10660 			dev_info(&vsi->back->pdev->dev,
10661 				 "failed to rebuild channels using uplink_seid %u\n",
10662 				 vsi->uplink_seid);
10663 			return ret;
10664 		}
10665 		/* Reconfigure TX queues using QTX_CTL register */
10666 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10667 		if (ret) {
10668 			dev_info(&vsi->back->pdev->dev,
10669 				 "failed to configure TX rings for channel %u\n",
10670 				 ch->seid);
10671 			return ret;
10672 		}
10673 		/* update 'next_base_queue' */
10674 		vsi->next_base_queue = vsi->next_base_queue +
10675 							ch->num_queue_pairs;
10676 		if (ch->max_tx_rate) {
10677 			u64 credits = ch->max_tx_rate;
10678 
10679 			if (i40e_set_bw_limit(vsi, ch->seid,
10680 					      ch->max_tx_rate))
10681 				return -EINVAL;
10682 
10683 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10684 			dev_dbg(&vsi->back->pdev->dev,
10685 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10686 				ch->max_tx_rate,
10687 				credits,
10688 				ch->seid);
10689 		}
10690 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10691 		if (ret) {
10692 			dev_dbg(&vsi->back->pdev->dev,
10693 				"Failed to rebuild cloud filters for channel VSI %u\n",
10694 				ch->seid);
10695 			return ret;
10696 		}
10697 	}
10698 	return 0;
10699 }
10700 
10701 /**
10702  * i40e_clean_xps_state - clean xps state for every tx_ring
10703  * @vsi: ptr to the VSI
10704  **/
10705 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10706 {
10707 	int i;
10708 
10709 	if (vsi->tx_rings)
10710 		for (i = 0; i < vsi->num_queue_pairs; i++)
10711 			if (vsi->tx_rings[i])
10712 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10713 					  vsi->tx_rings[i]->state);
10714 }
10715 
10716 /**
10717  * i40e_prep_for_reset - prep for the core to reset
10718  * @pf: board private structure
10719  *
10720  * Close up the VFs and other things in prep for PF Reset.
10721   **/
10722 static void i40e_prep_for_reset(struct i40e_pf *pf)
10723 {
10724 	struct i40e_hw *hw = &pf->hw;
10725 	int ret = 0;
10726 	u32 v;
10727 
10728 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10729 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10730 		return;
10731 	if (i40e_check_asq_alive(&pf->hw))
10732 		i40e_vc_notify_reset(pf);
10733 
10734 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10735 
10736 	/* quiesce the VSIs and their queues that are not already DOWN */
10737 	i40e_pf_quiesce_all_vsi(pf);
10738 
10739 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10740 		if (pf->vsi[v]) {
10741 			i40e_clean_xps_state(pf->vsi[v]);
10742 			pf->vsi[v]->seid = 0;
10743 		}
10744 	}
10745 
10746 	i40e_shutdown_adminq(&pf->hw);
10747 
10748 	/* call shutdown HMC */
10749 	if (hw->hmc.hmc_obj) {
10750 		ret = i40e_shutdown_lan_hmc(hw);
10751 		if (ret)
10752 			dev_warn(&pf->pdev->dev,
10753 				 "shutdown_lan_hmc failed: %d\n", ret);
10754 	}
10755 
10756 	/* Save the current PTP time so that we can restore the time after the
10757 	 * reset completes.
10758 	 */
10759 	i40e_ptp_save_hw_time(pf);
10760 }
10761 
10762 /**
10763  * i40e_send_version - update firmware with driver version
10764  * @pf: PF struct
10765  */
10766 static void i40e_send_version(struct i40e_pf *pf)
10767 {
10768 	struct i40e_driver_version dv;
10769 
10770 	dv.major_version = 0xff;
10771 	dv.minor_version = 0xff;
10772 	dv.build_version = 0xff;
10773 	dv.subbuild_version = 0;
10774 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10775 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10776 }
10777 
10778 /**
10779  * i40e_get_oem_version - get OEM specific version information
10780  * @hw: pointer to the hardware structure
10781  **/
10782 static void i40e_get_oem_version(struct i40e_hw *hw)
10783 {
10784 	u16 block_offset = 0xffff;
10785 	u16 block_length = 0;
10786 	u16 capabilities = 0;
10787 	u16 gen_snap = 0;
10788 	u16 release = 0;
10789 
10790 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10791 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10792 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10793 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10794 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10795 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10796 #define I40E_NVM_OEM_LENGTH			3
10797 
10798 	/* Check if pointer to OEM version block is valid. */
10799 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10800 	if (block_offset == 0xffff)
10801 		return;
10802 
10803 	/* Check if OEM version block has correct length. */
10804 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10805 			   &block_length);
10806 	if (block_length < I40E_NVM_OEM_LENGTH)
10807 		return;
10808 
10809 	/* Check if OEM version format is as expected. */
10810 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10811 			   &capabilities);
10812 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10813 		return;
10814 
10815 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10816 			   &gen_snap);
10817 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10818 			   &release);
10819 	hw->nvm.oem_ver =
10820 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10821 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10822 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10823 }
10824 
10825 /**
10826  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10827  * @pf: board private structure
10828  **/
10829 static int i40e_reset(struct i40e_pf *pf)
10830 {
10831 	struct i40e_hw *hw = &pf->hw;
10832 	int ret;
10833 
10834 	ret = i40e_pf_reset(hw);
10835 	if (ret) {
10836 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10837 		set_bit(__I40E_RESET_FAILED, pf->state);
10838 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10839 	} else {
10840 		pf->pfr_count++;
10841 	}
10842 	return ret;
10843 }
10844 
10845 /**
10846  * i40e_rebuild - rebuild using a saved config
10847  * @pf: board private structure
10848  * @reinit: if the Main VSI needs to re-initialized.
10849  * @lock_acquired: indicates whether or not the lock has been acquired
10850  * before this function was called.
10851  **/
10852 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10853 {
10854 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10855 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10856 	struct i40e_hw *hw = &pf->hw;
10857 	int ret;
10858 	u32 val;
10859 	int v;
10860 
10861 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10862 	    is_recovery_mode_reported)
10863 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10864 
10865 	if (test_bit(__I40E_DOWN, pf->state) &&
10866 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10867 		goto clear_recovery;
10868 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10869 
10870 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10871 	ret = i40e_init_adminq(&pf->hw);
10872 	if (ret) {
10873 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10874 			 ERR_PTR(ret),
10875 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10876 		goto clear_recovery;
10877 	}
10878 	i40e_get_oem_version(&pf->hw);
10879 
10880 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10881 		/* The following delay is necessary for firmware update. */
10882 		mdelay(1000);
10883 	}
10884 
10885 	/* re-verify the eeprom if we just had an EMP reset */
10886 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10887 		i40e_verify_eeprom(pf);
10888 
10889 	/* if we are going out of or into recovery mode we have to act
10890 	 * accordingly with regard to resources initialization
10891 	 * and deinitialization
10892 	 */
10893 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10894 		if (i40e_get_capabilities(pf,
10895 					  i40e_aqc_opc_list_func_capabilities))
10896 			goto end_unlock;
10897 
10898 		if (is_recovery_mode_reported) {
10899 			/* we're staying in recovery mode so we'll reinitialize
10900 			 * misc vector here
10901 			 */
10902 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10903 				goto end_unlock;
10904 		} else {
10905 			if (!lock_acquired)
10906 				rtnl_lock();
10907 			/* we're going out of recovery mode so we'll free
10908 			 * the IRQ allocated specifically for recovery mode
10909 			 * and restore the interrupt scheme
10910 			 */
10911 			free_irq(pf->pdev->irq, pf);
10912 			i40e_clear_interrupt_scheme(pf);
10913 			if (i40e_restore_interrupt_scheme(pf))
10914 				goto end_unlock;
10915 		}
10916 
10917 		/* tell the firmware that we're starting */
10918 		i40e_send_version(pf);
10919 
10920 		/* bail out in case recovery mode was detected, as there is
10921 		 * no need for further configuration.
10922 		 */
10923 		goto end_unlock;
10924 	}
10925 
10926 	i40e_clear_pxe_mode(hw);
10927 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10928 	if (ret)
10929 		goto end_core_reset;
10930 
10931 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10932 				hw->func_caps.num_rx_qp, 0, 0);
10933 	if (ret) {
10934 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10935 		goto end_core_reset;
10936 	}
10937 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10938 	if (ret) {
10939 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10940 		goto end_core_reset;
10941 	}
10942 
10943 #ifdef CONFIG_I40E_DCB
10944 	/* Enable FW to write a default DCB config on link-up
10945 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10946 	 * is not supported with new link speed
10947 	 */
10948 	if (i40e_is_tc_mqprio_enabled(pf)) {
10949 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10950 	} else {
10951 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10952 		    (hw->phy.link_info.link_speed &
10953 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10954 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10955 			dev_warn(&pf->pdev->dev,
10956 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10957 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10958 		} else {
10959 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10960 			ret = i40e_init_pf_dcb(pf);
10961 			if (ret) {
10962 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10963 					 ret);
10964 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10965 				/* Continue without DCB enabled */
10966 			}
10967 		}
10968 	}
10969 
10970 #endif /* CONFIG_I40E_DCB */
10971 	if (!lock_acquired)
10972 		rtnl_lock();
10973 	ret = i40e_setup_pf_switch(pf, reinit, true);
10974 	if (ret)
10975 		goto end_unlock;
10976 
10977 	/* The driver only wants link up/down and module qualification
10978 	 * reports from firmware.  Note the negative logic.
10979 	 */
10980 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10981 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10982 					 I40E_AQ_EVENT_MEDIA_NA |
10983 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10984 	if (ret)
10985 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10986 			 ERR_PTR(ret),
10987 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10988 
10989 	/* Rebuild the VSIs and VEBs that existed before reset.
10990 	 * They are still in our local switch element arrays, so only
10991 	 * need to rebuild the switch model in the HW.
10992 	 *
10993 	 * If there were VEBs but the reconstitution failed, we'll try
10994 	 * to recover minimal use by getting the basic PF VSI working.
10995 	 */
10996 	if (vsi->uplink_seid != pf->mac_seid) {
10997 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10998 		/* find the one VEB connected to the MAC, and find orphans */
10999 		for (v = 0; v < I40E_MAX_VEB; v++) {
11000 			if (!pf->veb[v])
11001 				continue;
11002 
11003 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
11004 			    pf->veb[v]->uplink_seid == 0) {
11005 				ret = i40e_reconstitute_veb(pf->veb[v]);
11006 
11007 				if (!ret)
11008 					continue;
11009 
11010 				/* If Main VEB failed, we're in deep doodoo,
11011 				 * so give up rebuilding the switch and set up
11012 				 * for minimal rebuild of PF VSI.
11013 				 * If orphan failed, we'll report the error
11014 				 * but try to keep going.
11015 				 */
11016 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
11017 					dev_info(&pf->pdev->dev,
11018 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
11019 						 ret);
11020 					vsi->uplink_seid = pf->mac_seid;
11021 					break;
11022 				} else if (pf->veb[v]->uplink_seid == 0) {
11023 					dev_info(&pf->pdev->dev,
11024 						 "rebuild of orphan VEB failed: %d\n",
11025 						 ret);
11026 				}
11027 			}
11028 		}
11029 	}
11030 
11031 	if (vsi->uplink_seid == pf->mac_seid) {
11032 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11033 		/* no VEB, so rebuild only the Main VSI */
11034 		ret = i40e_add_vsi(vsi);
11035 		if (ret) {
11036 			dev_info(&pf->pdev->dev,
11037 				 "rebuild of Main VSI failed: %d\n", ret);
11038 			goto end_unlock;
11039 		}
11040 	}
11041 
11042 	if (vsi->mqprio_qopt.max_rate[0]) {
11043 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11044 						  vsi->mqprio_qopt.max_rate[0]);
11045 		u64 credits = 0;
11046 
11047 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11048 		if (ret)
11049 			goto end_unlock;
11050 
11051 		credits = max_tx_rate;
11052 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11053 		dev_dbg(&vsi->back->pdev->dev,
11054 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11055 			max_tx_rate,
11056 			credits,
11057 			vsi->seid);
11058 	}
11059 
11060 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11061 	if (ret)
11062 		goto end_unlock;
11063 
11064 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11065 	 * for this main VSI if they exist
11066 	 */
11067 	ret = i40e_rebuild_channels(vsi);
11068 	if (ret)
11069 		goto end_unlock;
11070 
11071 	/* Reconfigure hardware for allowing smaller MSS in the case
11072 	 * of TSO, so that we avoid the MDD being fired and causing
11073 	 * a reset in the case of small MSS+TSO.
11074 	 */
11075 #define I40E_REG_MSS          0x000E64DC
11076 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11077 #define I40E_64BYTE_MSS       0x400000
11078 	val = rd32(hw, I40E_REG_MSS);
11079 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11080 		val &= ~I40E_REG_MSS_MIN_MASK;
11081 		val |= I40E_64BYTE_MSS;
11082 		wr32(hw, I40E_REG_MSS, val);
11083 	}
11084 
11085 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11086 		msleep(75);
11087 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11088 		if (ret)
11089 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11090 				 ERR_PTR(ret),
11091 				 i40e_aq_str(&pf->hw,
11092 					     pf->hw.aq.asq_last_status));
11093 	}
11094 	/* reinit the misc interrupt */
11095 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11096 		ret = i40e_setup_misc_vector(pf);
11097 		if (ret)
11098 			goto end_unlock;
11099 	}
11100 
11101 	/* Add a filter to drop all Flow control frames from any VSI from being
11102 	 * transmitted. By doing so we stop a malicious VF from sending out
11103 	 * PAUSE or PFC frames and potentially controlling traffic for other
11104 	 * PF/VF VSIs.
11105 	 * The FW can still send Flow control frames if enabled.
11106 	 */
11107 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11108 						       pf->main_vsi_seid);
11109 
11110 	/* restart the VSIs that were rebuilt and running before the reset */
11111 	i40e_pf_unquiesce_all_vsi(pf);
11112 
11113 	/* Release the RTNL lock before we start resetting VFs */
11114 	if (!lock_acquired)
11115 		rtnl_unlock();
11116 
11117 	/* Restore promiscuous settings */
11118 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11119 	if (ret)
11120 		dev_warn(&pf->pdev->dev,
11121 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11122 			 pf->cur_promisc ? "on" : "off",
11123 			 ERR_PTR(ret),
11124 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11125 
11126 	i40e_reset_all_vfs(pf, true);
11127 
11128 	/* tell the firmware that we're starting */
11129 	i40e_send_version(pf);
11130 
11131 	/* We've already released the lock, so don't do it again */
11132 	goto end_core_reset;
11133 
11134 end_unlock:
11135 	if (!lock_acquired)
11136 		rtnl_unlock();
11137 end_core_reset:
11138 	clear_bit(__I40E_RESET_FAILED, pf->state);
11139 clear_recovery:
11140 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11141 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11142 }
11143 
11144 /**
11145  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11146  * @pf: board private structure
11147  * @reinit: if the Main VSI needs to re-initialized.
11148  * @lock_acquired: indicates whether or not the lock has been acquired
11149  * before this function was called.
11150  **/
11151 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11152 				   bool lock_acquired)
11153 {
11154 	int ret;
11155 
11156 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11157 		return;
11158 	/* Now we wait for GRST to settle out.
11159 	 * We don't have to delete the VEBs or VSIs from the hw switch
11160 	 * because the reset will make them disappear.
11161 	 */
11162 	ret = i40e_reset(pf);
11163 	if (!ret)
11164 		i40e_rebuild(pf, reinit, lock_acquired);
11165 }
11166 
11167 /**
11168  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11169  * @pf: board private structure
11170  *
11171  * Close up the VFs and other things in prep for a Core Reset,
11172  * then get ready to rebuild the world.
11173  * @lock_acquired: indicates whether or not the lock has been acquired
11174  * before this function was called.
11175  **/
11176 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11177 {
11178 	i40e_prep_for_reset(pf);
11179 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11180 }
11181 
11182 /**
11183  * i40e_handle_mdd_event
11184  * @pf: pointer to the PF structure
11185  *
11186  * Called from the MDD irq handler to identify possibly malicious vfs
11187  **/
11188 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11189 {
11190 	struct i40e_hw *hw = &pf->hw;
11191 	bool mdd_detected = false;
11192 	struct i40e_vf *vf;
11193 	u32 reg;
11194 	int i;
11195 
11196 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11197 		return;
11198 
11199 	/* find what triggered the MDD event */
11200 	reg = rd32(hw, I40E_GL_MDET_TX);
11201 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11202 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11203 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11204 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11205 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11206 				pf->hw.func_caps.base_queue;
11207 		if (netif_msg_tx_err(pf))
11208 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11209 				 event, queue, pf_num, vf_num);
11210 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11211 		mdd_detected = true;
11212 	}
11213 	reg = rd32(hw, I40E_GL_MDET_RX);
11214 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11215 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11216 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11217 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11218 				pf->hw.func_caps.base_queue;
11219 		if (netif_msg_rx_err(pf))
11220 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11221 				 event, queue, func);
11222 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11223 		mdd_detected = true;
11224 	}
11225 
11226 	if (mdd_detected) {
11227 		reg = rd32(hw, I40E_PF_MDET_TX);
11228 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11229 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11230 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11231 		}
11232 		reg = rd32(hw, I40E_PF_MDET_RX);
11233 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11234 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11235 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11236 		}
11237 	}
11238 
11239 	/* see if one of the VFs needs its hand slapped */
11240 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11241 		vf = &(pf->vf[i]);
11242 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11243 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11244 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11245 			vf->num_mdd_events++;
11246 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11247 				 i);
11248 			dev_info(&pf->pdev->dev,
11249 				 "Use PF Control I/F to re-enable the VF\n");
11250 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11251 		}
11252 
11253 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11254 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11255 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11256 			vf->num_mdd_events++;
11257 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11258 				 i);
11259 			dev_info(&pf->pdev->dev,
11260 				 "Use PF Control I/F to re-enable the VF\n");
11261 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11262 		}
11263 	}
11264 
11265 	/* re-enable mdd interrupt cause */
11266 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11267 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11268 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11269 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11270 	i40e_flush(hw);
11271 }
11272 
11273 /**
11274  * i40e_service_task - Run the driver's async subtasks
11275  * @work: pointer to work_struct containing our data
11276  **/
11277 static void i40e_service_task(struct work_struct *work)
11278 {
11279 	struct i40e_pf *pf = container_of(work,
11280 					  struct i40e_pf,
11281 					  service_task);
11282 	unsigned long start_time = jiffies;
11283 
11284 	/* don't bother with service tasks if a reset is in progress */
11285 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11286 	    test_bit(__I40E_SUSPENDED, pf->state))
11287 		return;
11288 
11289 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11290 		return;
11291 
11292 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11293 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11294 		i40e_sync_filters_subtask(pf);
11295 		i40e_reset_subtask(pf);
11296 		i40e_handle_mdd_event(pf);
11297 		i40e_vc_process_vflr_event(pf);
11298 		i40e_watchdog_subtask(pf);
11299 		i40e_fdir_reinit_subtask(pf);
11300 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11301 			/* Client subtask will reopen next time through. */
11302 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11303 							   true);
11304 		} else {
11305 			i40e_client_subtask(pf);
11306 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11307 					       pf->state))
11308 				i40e_notify_client_of_l2_param_changes(
11309 								pf->vsi[pf->lan_vsi]);
11310 		}
11311 		i40e_sync_filters_subtask(pf);
11312 	} else {
11313 		i40e_reset_subtask(pf);
11314 	}
11315 
11316 	i40e_clean_adminq_subtask(pf);
11317 
11318 	/* flush memory to make sure state is correct before next watchdog */
11319 	smp_mb__before_atomic();
11320 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11321 
11322 	/* If the tasks have taken longer than one timer cycle or there
11323 	 * is more work to be done, reschedule the service task now
11324 	 * rather than wait for the timer to tick again.
11325 	 */
11326 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11327 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11328 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11329 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11330 		i40e_service_event_schedule(pf);
11331 }
11332 
11333 /**
11334  * i40e_service_timer - timer callback
11335  * @t: timer list pointer
11336  **/
11337 static void i40e_service_timer(struct timer_list *t)
11338 {
11339 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11340 
11341 	mod_timer(&pf->service_timer,
11342 		  round_jiffies(jiffies + pf->service_timer_period));
11343 	i40e_service_event_schedule(pf);
11344 }
11345 
11346 /**
11347  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11348  * @vsi: the VSI being configured
11349  **/
11350 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11351 {
11352 	struct i40e_pf *pf = vsi->back;
11353 
11354 	switch (vsi->type) {
11355 	case I40E_VSI_MAIN:
11356 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11357 		if (!vsi->num_tx_desc)
11358 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11359 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11360 		if (!vsi->num_rx_desc)
11361 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11362 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11363 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11364 			vsi->num_q_vectors = pf->num_lan_msix;
11365 		else
11366 			vsi->num_q_vectors = 1;
11367 
11368 		break;
11369 
11370 	case I40E_VSI_FDIR:
11371 		vsi->alloc_queue_pairs = 1;
11372 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11373 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11374 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11375 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11376 		vsi->num_q_vectors = pf->num_fdsb_msix;
11377 		break;
11378 
11379 	case I40E_VSI_VMDQ2:
11380 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11381 		if (!vsi->num_tx_desc)
11382 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11383 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11384 		if (!vsi->num_rx_desc)
11385 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11386 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11387 		vsi->num_q_vectors = pf->num_vmdq_msix;
11388 		break;
11389 
11390 	case I40E_VSI_SRIOV:
11391 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11392 		if (!vsi->num_tx_desc)
11393 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11394 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11395 		if (!vsi->num_rx_desc)
11396 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11397 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11398 		break;
11399 
11400 	default:
11401 		WARN_ON(1);
11402 		return -ENODATA;
11403 	}
11404 
11405 	if (is_kdump_kernel()) {
11406 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11407 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11408 	}
11409 
11410 	return 0;
11411 }
11412 
11413 /**
11414  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11415  * @vsi: VSI pointer
11416  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11417  *
11418  * On error: returns error code (negative)
11419  * On success: returns 0
11420  **/
11421 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11422 {
11423 	struct i40e_ring **next_rings;
11424 	int size;
11425 	int ret = 0;
11426 
11427 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11428 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11429 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11430 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11431 	if (!vsi->tx_rings)
11432 		return -ENOMEM;
11433 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11434 	if (i40e_enabled_xdp_vsi(vsi)) {
11435 		vsi->xdp_rings = next_rings;
11436 		next_rings += vsi->alloc_queue_pairs;
11437 	}
11438 	vsi->rx_rings = next_rings;
11439 
11440 	if (alloc_qvectors) {
11441 		/* allocate memory for q_vector pointers */
11442 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11443 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11444 		if (!vsi->q_vectors) {
11445 			ret = -ENOMEM;
11446 			goto err_vectors;
11447 		}
11448 	}
11449 	return ret;
11450 
11451 err_vectors:
11452 	kfree(vsi->tx_rings);
11453 	return ret;
11454 }
11455 
11456 /**
11457  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11458  * @pf: board private structure
11459  * @type: type of VSI
11460  *
11461  * On error: returns error code (negative)
11462  * On success: returns vsi index in PF (positive)
11463  **/
11464 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11465 {
11466 	int ret = -ENODEV;
11467 	struct i40e_vsi *vsi;
11468 	int vsi_idx;
11469 	int i;
11470 
11471 	/* Need to protect the allocation of the VSIs at the PF level */
11472 	mutex_lock(&pf->switch_mutex);
11473 
11474 	/* VSI list may be fragmented if VSI creation/destruction has
11475 	 * been happening.  We can afford to do a quick scan to look
11476 	 * for any free VSIs in the list.
11477 	 *
11478 	 * find next empty vsi slot, looping back around if necessary
11479 	 */
11480 	i = pf->next_vsi;
11481 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11482 		i++;
11483 	if (i >= pf->num_alloc_vsi) {
11484 		i = 0;
11485 		while (i < pf->next_vsi && pf->vsi[i])
11486 			i++;
11487 	}
11488 
11489 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11490 		vsi_idx = i;             /* Found one! */
11491 	} else {
11492 		ret = -ENODEV;
11493 		goto unlock_pf;  /* out of VSI slots! */
11494 	}
11495 	pf->next_vsi = ++i;
11496 
11497 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11498 	if (!vsi) {
11499 		ret = -ENOMEM;
11500 		goto unlock_pf;
11501 	}
11502 	vsi->type = type;
11503 	vsi->back = pf;
11504 	set_bit(__I40E_VSI_DOWN, vsi->state);
11505 	vsi->flags = 0;
11506 	vsi->idx = vsi_idx;
11507 	vsi->int_rate_limit = 0;
11508 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11509 				pf->rss_table_size : 64;
11510 	vsi->netdev_registered = false;
11511 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11512 	hash_init(vsi->mac_filter_hash);
11513 	vsi->irqs_ready = false;
11514 
11515 	if (type == I40E_VSI_MAIN) {
11516 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11517 		if (!vsi->af_xdp_zc_qps)
11518 			goto err_rings;
11519 	}
11520 
11521 	ret = i40e_set_num_rings_in_vsi(vsi);
11522 	if (ret)
11523 		goto err_rings;
11524 
11525 	ret = i40e_vsi_alloc_arrays(vsi, true);
11526 	if (ret)
11527 		goto err_rings;
11528 
11529 	/* Setup default MSIX irq handler for VSI */
11530 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11531 
11532 	/* Initialize VSI lock */
11533 	spin_lock_init(&vsi->mac_filter_hash_lock);
11534 	pf->vsi[vsi_idx] = vsi;
11535 	ret = vsi_idx;
11536 	goto unlock_pf;
11537 
11538 err_rings:
11539 	bitmap_free(vsi->af_xdp_zc_qps);
11540 	pf->next_vsi = i - 1;
11541 	kfree(vsi);
11542 unlock_pf:
11543 	mutex_unlock(&pf->switch_mutex);
11544 	return ret;
11545 }
11546 
11547 /**
11548  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11549  * @vsi: VSI pointer
11550  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11551  *
11552  * On error: returns error code (negative)
11553  * On success: returns 0
11554  **/
11555 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11556 {
11557 	/* free the ring and vector containers */
11558 	if (free_qvectors) {
11559 		kfree(vsi->q_vectors);
11560 		vsi->q_vectors = NULL;
11561 	}
11562 	kfree(vsi->tx_rings);
11563 	vsi->tx_rings = NULL;
11564 	vsi->rx_rings = NULL;
11565 	vsi->xdp_rings = NULL;
11566 }
11567 
11568 /**
11569  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11570  * and lookup table
11571  * @vsi: Pointer to VSI structure
11572  */
11573 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11574 {
11575 	if (!vsi)
11576 		return;
11577 
11578 	kfree(vsi->rss_hkey_user);
11579 	vsi->rss_hkey_user = NULL;
11580 
11581 	kfree(vsi->rss_lut_user);
11582 	vsi->rss_lut_user = NULL;
11583 }
11584 
11585 /**
11586  * i40e_vsi_clear - Deallocate the VSI provided
11587  * @vsi: the VSI being un-configured
11588  **/
11589 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11590 {
11591 	struct i40e_pf *pf;
11592 
11593 	if (!vsi)
11594 		return 0;
11595 
11596 	if (!vsi->back)
11597 		goto free_vsi;
11598 	pf = vsi->back;
11599 
11600 	mutex_lock(&pf->switch_mutex);
11601 	if (!pf->vsi[vsi->idx]) {
11602 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11603 			vsi->idx, vsi->idx, vsi->type);
11604 		goto unlock_vsi;
11605 	}
11606 
11607 	if (pf->vsi[vsi->idx] != vsi) {
11608 		dev_err(&pf->pdev->dev,
11609 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11610 			pf->vsi[vsi->idx]->idx,
11611 			pf->vsi[vsi->idx]->type,
11612 			vsi->idx, vsi->type);
11613 		goto unlock_vsi;
11614 	}
11615 
11616 	/* updates the PF for this cleared vsi */
11617 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11618 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11619 
11620 	bitmap_free(vsi->af_xdp_zc_qps);
11621 	i40e_vsi_free_arrays(vsi, true);
11622 	i40e_clear_rss_config_user(vsi);
11623 
11624 	pf->vsi[vsi->idx] = NULL;
11625 	if (vsi->idx < pf->next_vsi)
11626 		pf->next_vsi = vsi->idx;
11627 
11628 unlock_vsi:
11629 	mutex_unlock(&pf->switch_mutex);
11630 free_vsi:
11631 	kfree(vsi);
11632 
11633 	return 0;
11634 }
11635 
11636 /**
11637  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11638  * @vsi: the VSI being cleaned
11639  **/
11640 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11641 {
11642 	int i;
11643 
11644 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11645 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11646 			kfree_rcu(vsi->tx_rings[i], rcu);
11647 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11648 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11649 			if (vsi->xdp_rings)
11650 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11651 		}
11652 	}
11653 }
11654 
11655 /**
11656  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11657  * @vsi: the VSI being configured
11658  **/
11659 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11660 {
11661 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11662 	struct i40e_pf *pf = vsi->back;
11663 	struct i40e_ring *ring;
11664 
11665 	/* Set basic values in the rings to be used later during open() */
11666 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11667 		/* allocate space for both Tx and Rx in one shot */
11668 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11669 		if (!ring)
11670 			goto err_out;
11671 
11672 		ring->queue_index = i;
11673 		ring->reg_idx = vsi->base_queue + i;
11674 		ring->ring_active = false;
11675 		ring->vsi = vsi;
11676 		ring->netdev = vsi->netdev;
11677 		ring->dev = &pf->pdev->dev;
11678 		ring->count = vsi->num_tx_desc;
11679 		ring->size = 0;
11680 		ring->dcb_tc = 0;
11681 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11682 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11683 		ring->itr_setting = pf->tx_itr_default;
11684 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11685 
11686 		if (!i40e_enabled_xdp_vsi(vsi))
11687 			goto setup_rx;
11688 
11689 		ring->queue_index = vsi->alloc_queue_pairs + i;
11690 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11691 		ring->ring_active = false;
11692 		ring->vsi = vsi;
11693 		ring->netdev = NULL;
11694 		ring->dev = &pf->pdev->dev;
11695 		ring->count = vsi->num_tx_desc;
11696 		ring->size = 0;
11697 		ring->dcb_tc = 0;
11698 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11699 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11700 		set_ring_xdp(ring);
11701 		ring->itr_setting = pf->tx_itr_default;
11702 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11703 
11704 setup_rx:
11705 		ring->queue_index = i;
11706 		ring->reg_idx = vsi->base_queue + i;
11707 		ring->ring_active = false;
11708 		ring->vsi = vsi;
11709 		ring->netdev = vsi->netdev;
11710 		ring->dev = &pf->pdev->dev;
11711 		ring->count = vsi->num_rx_desc;
11712 		ring->size = 0;
11713 		ring->dcb_tc = 0;
11714 		ring->itr_setting = pf->rx_itr_default;
11715 		WRITE_ONCE(vsi->rx_rings[i], ring);
11716 	}
11717 
11718 	return 0;
11719 
11720 err_out:
11721 	i40e_vsi_clear_rings(vsi);
11722 	return -ENOMEM;
11723 }
11724 
11725 /**
11726  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11727  * @pf: board private structure
11728  * @vectors: the number of MSI-X vectors to request
11729  *
11730  * Returns the number of vectors reserved, or error
11731  **/
11732 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11733 {
11734 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11735 					I40E_MIN_MSIX, vectors);
11736 	if (vectors < 0) {
11737 		dev_info(&pf->pdev->dev,
11738 			 "MSI-X vector reservation failed: %d\n", vectors);
11739 		vectors = 0;
11740 	}
11741 
11742 	return vectors;
11743 }
11744 
11745 /**
11746  * i40e_init_msix - Setup the MSIX capability
11747  * @pf: board private structure
11748  *
11749  * Work with the OS to set up the MSIX vectors needed.
11750  *
11751  * Returns the number of vectors reserved or negative on failure
11752  **/
11753 static int i40e_init_msix(struct i40e_pf *pf)
11754 {
11755 	struct i40e_hw *hw = &pf->hw;
11756 	int cpus, extra_vectors;
11757 	int vectors_left;
11758 	int v_budget, i;
11759 	int v_actual;
11760 	int iwarp_requested = 0;
11761 
11762 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11763 		return -ENODEV;
11764 
11765 	/* The number of vectors we'll request will be comprised of:
11766 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11767 	 *   - The number of LAN queue pairs
11768 	 *	- Queues being used for RSS.
11769 	 *		We don't need as many as max_rss_size vectors.
11770 	 *		use rss_size instead in the calculation since that
11771 	 *		is governed by number of cpus in the system.
11772 	 *	- assumes symmetric Tx/Rx pairing
11773 	 *   - The number of VMDq pairs
11774 	 *   - The CPU count within the NUMA node if iWARP is enabled
11775 	 * Once we count this up, try the request.
11776 	 *
11777 	 * If we can't get what we want, we'll simplify to nearly nothing
11778 	 * and try again.  If that still fails, we punt.
11779 	 */
11780 	vectors_left = hw->func_caps.num_msix_vectors;
11781 	v_budget = 0;
11782 
11783 	/* reserve one vector for miscellaneous handler */
11784 	if (vectors_left) {
11785 		v_budget++;
11786 		vectors_left--;
11787 	}
11788 
11789 	/* reserve some vectors for the main PF traffic queues. Initially we
11790 	 * only reserve at most 50% of the available vectors, in the case that
11791 	 * the number of online CPUs is large. This ensures that we can enable
11792 	 * extra features as well. Once we've enabled the other features, we
11793 	 * will use any remaining vectors to reach as close as we can to the
11794 	 * number of online CPUs.
11795 	 */
11796 	cpus = num_online_cpus();
11797 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11798 	vectors_left -= pf->num_lan_msix;
11799 
11800 	/* reserve one vector for sideband flow director */
11801 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11802 		if (vectors_left) {
11803 			pf->num_fdsb_msix = 1;
11804 			v_budget++;
11805 			vectors_left--;
11806 		} else {
11807 			pf->num_fdsb_msix = 0;
11808 		}
11809 	}
11810 
11811 	/* can we reserve enough for iWARP? */
11812 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11813 		iwarp_requested = pf->num_iwarp_msix;
11814 
11815 		if (!vectors_left)
11816 			pf->num_iwarp_msix = 0;
11817 		else if (vectors_left < pf->num_iwarp_msix)
11818 			pf->num_iwarp_msix = 1;
11819 		v_budget += pf->num_iwarp_msix;
11820 		vectors_left -= pf->num_iwarp_msix;
11821 	}
11822 
11823 	/* any vectors left over go for VMDq support */
11824 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11825 		if (!vectors_left) {
11826 			pf->num_vmdq_msix = 0;
11827 			pf->num_vmdq_qps = 0;
11828 		} else {
11829 			int vmdq_vecs_wanted =
11830 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11831 			int vmdq_vecs =
11832 				min_t(int, vectors_left, vmdq_vecs_wanted);
11833 
11834 			/* if we're short on vectors for what's desired, we limit
11835 			 * the queues per vmdq.  If this is still more than are
11836 			 * available, the user will need to change the number of
11837 			 * queues/vectors used by the PF later with the ethtool
11838 			 * channels command
11839 			 */
11840 			if (vectors_left < vmdq_vecs_wanted) {
11841 				pf->num_vmdq_qps = 1;
11842 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11843 				vmdq_vecs = min_t(int,
11844 						  vectors_left,
11845 						  vmdq_vecs_wanted);
11846 			}
11847 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11848 
11849 			v_budget += vmdq_vecs;
11850 			vectors_left -= vmdq_vecs;
11851 		}
11852 	}
11853 
11854 	/* On systems with a large number of SMP cores, we previously limited
11855 	 * the number of vectors for num_lan_msix to be at most 50% of the
11856 	 * available vectors, to allow for other features. Now, we add back
11857 	 * the remaining vectors. However, we ensure that the total
11858 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11859 	 * calculate the number of vectors we can add without going over the
11860 	 * cap of CPUs. For systems with a small number of CPUs this will be
11861 	 * zero.
11862 	 */
11863 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11864 	pf->num_lan_msix += extra_vectors;
11865 	vectors_left -= extra_vectors;
11866 
11867 	WARN(vectors_left < 0,
11868 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11869 
11870 	v_budget += pf->num_lan_msix;
11871 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11872 				   GFP_KERNEL);
11873 	if (!pf->msix_entries)
11874 		return -ENOMEM;
11875 
11876 	for (i = 0; i < v_budget; i++)
11877 		pf->msix_entries[i].entry = i;
11878 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11879 
11880 	if (v_actual < I40E_MIN_MSIX) {
11881 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11882 		kfree(pf->msix_entries);
11883 		pf->msix_entries = NULL;
11884 		pci_disable_msix(pf->pdev);
11885 		return -ENODEV;
11886 
11887 	} else if (v_actual == I40E_MIN_MSIX) {
11888 		/* Adjust for minimal MSIX use */
11889 		pf->num_vmdq_vsis = 0;
11890 		pf->num_vmdq_qps = 0;
11891 		pf->num_lan_qps = 1;
11892 		pf->num_lan_msix = 1;
11893 
11894 	} else if (v_actual != v_budget) {
11895 		/* If we have limited resources, we will start with no vectors
11896 		 * for the special features and then allocate vectors to some
11897 		 * of these features based on the policy and at the end disable
11898 		 * the features that did not get any vectors.
11899 		 */
11900 		int vec;
11901 
11902 		dev_info(&pf->pdev->dev,
11903 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11904 			 v_actual, v_budget);
11905 		/* reserve the misc vector */
11906 		vec = v_actual - 1;
11907 
11908 		/* Scale vector usage down */
11909 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11910 		pf->num_vmdq_vsis = 1;
11911 		pf->num_vmdq_qps = 1;
11912 
11913 		/* partition out the remaining vectors */
11914 		switch (vec) {
11915 		case 2:
11916 			pf->num_lan_msix = 1;
11917 			break;
11918 		case 3:
11919 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11920 				pf->num_lan_msix = 1;
11921 				pf->num_iwarp_msix = 1;
11922 			} else {
11923 				pf->num_lan_msix = 2;
11924 			}
11925 			break;
11926 		default:
11927 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11928 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11929 						 iwarp_requested);
11930 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11931 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11932 			} else {
11933 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11934 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11935 			}
11936 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11937 				pf->num_fdsb_msix = 1;
11938 				vec--;
11939 			}
11940 			pf->num_lan_msix = min_t(int,
11941 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11942 							      pf->num_lan_msix);
11943 			pf->num_lan_qps = pf->num_lan_msix;
11944 			break;
11945 		}
11946 	}
11947 
11948 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
11949 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11950 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
11951 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
11952 	}
11953 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
11954 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11955 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
11956 	}
11957 
11958 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
11959 	    pf->num_iwarp_msix == 0) {
11960 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11961 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
11962 	}
11963 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11964 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11965 		   pf->num_lan_msix,
11966 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11967 		   pf->num_fdsb_msix,
11968 		   pf->num_iwarp_msix);
11969 
11970 	return v_actual;
11971 }
11972 
11973 /**
11974  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11975  * @vsi: the VSI being configured
11976  * @v_idx: index of the vector in the vsi struct
11977  *
11978  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11979  **/
11980 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11981 {
11982 	struct i40e_q_vector *q_vector;
11983 
11984 	/* allocate q_vector */
11985 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11986 	if (!q_vector)
11987 		return -ENOMEM;
11988 
11989 	q_vector->vsi = vsi;
11990 	q_vector->v_idx = v_idx;
11991 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11992 
11993 	if (vsi->netdev)
11994 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11995 
11996 	/* tie q_vector and vsi together */
11997 	vsi->q_vectors[v_idx] = q_vector;
11998 
11999 	return 0;
12000 }
12001 
12002 /**
12003  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12004  * @vsi: the VSI being configured
12005  *
12006  * We allocate one q_vector per queue interrupt.  If allocation fails we
12007  * return -ENOMEM.
12008  **/
12009 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12010 {
12011 	struct i40e_pf *pf = vsi->back;
12012 	int err, v_idx, num_q_vectors;
12013 
12014 	/* if not MSIX, give the one vector only to the LAN VSI */
12015 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12016 		num_q_vectors = vsi->num_q_vectors;
12017 	else if (vsi == pf->vsi[pf->lan_vsi])
12018 		num_q_vectors = 1;
12019 	else
12020 		return -EINVAL;
12021 
12022 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12023 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12024 		if (err)
12025 			goto err_out;
12026 	}
12027 
12028 	return 0;
12029 
12030 err_out:
12031 	while (v_idx--)
12032 		i40e_free_q_vector(vsi, v_idx);
12033 
12034 	return err;
12035 }
12036 
12037 /**
12038  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12039  * @pf: board private structure to initialize
12040  **/
12041 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12042 {
12043 	int vectors = 0;
12044 	ssize_t size;
12045 
12046 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12047 		vectors = i40e_init_msix(pf);
12048 		if (vectors < 0) {
12049 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12050 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12051 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12052 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12053 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12054 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12055 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12056 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12057 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12058 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12059 
12060 			/* rework the queue expectations without MSIX */
12061 			i40e_determine_queue_usage(pf);
12062 		}
12063 	}
12064 
12065 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12066 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12067 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12068 		vectors = pci_enable_msi(pf->pdev);
12069 		if (vectors < 0) {
12070 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12071 				 vectors);
12072 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12073 		}
12074 		vectors = 1;  /* one MSI or Legacy vector */
12075 	}
12076 
12077 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12078 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12079 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12080 
12081 	/* set up vector assignment tracking */
12082 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12083 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12084 	if (!pf->irq_pile)
12085 		return -ENOMEM;
12086 
12087 	pf->irq_pile->num_entries = vectors;
12088 
12089 	/* track first vector for misc interrupts, ignore return */
12090 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12091 
12092 	return 0;
12093 }
12094 
12095 /**
12096  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12097  * @pf: private board data structure
12098  *
12099  * Restore the interrupt scheme that was cleared when we suspended the
12100  * device. This should be called during resume to re-allocate the q_vectors
12101  * and reacquire IRQs.
12102  */
12103 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12104 {
12105 	int err, i;
12106 
12107 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12108 	 * scheme. We need to re-enabled them here in order to attempt to
12109 	 * re-acquire the MSI or MSI-X vectors
12110 	 */
12111 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12112 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12113 
12114 	err = i40e_init_interrupt_scheme(pf);
12115 	if (err)
12116 		return err;
12117 
12118 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12119 	 * rings together again.
12120 	 */
12121 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12122 		if (pf->vsi[i]) {
12123 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12124 			if (err)
12125 				goto err_unwind;
12126 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12127 		}
12128 	}
12129 
12130 	err = i40e_setup_misc_vector(pf);
12131 	if (err)
12132 		goto err_unwind;
12133 
12134 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12135 		i40e_client_update_msix_info(pf);
12136 
12137 	return 0;
12138 
12139 err_unwind:
12140 	while (i--) {
12141 		if (pf->vsi[i])
12142 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12143 	}
12144 
12145 	return err;
12146 }
12147 
12148 /**
12149  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12150  * non queue events in recovery mode
12151  * @pf: board private structure
12152  *
12153  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12154  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12155  * This is handled differently than in recovery mode since no Tx/Rx resources
12156  * are being allocated.
12157  **/
12158 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12159 {
12160 	int err;
12161 
12162 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12163 		err = i40e_setup_misc_vector(pf);
12164 
12165 		if (err) {
12166 			dev_info(&pf->pdev->dev,
12167 				 "MSI-X misc vector request failed, error %d\n",
12168 				 err);
12169 			return err;
12170 		}
12171 	} else {
12172 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12173 
12174 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12175 				  pf->int_name, pf);
12176 
12177 		if (err) {
12178 			dev_info(&pf->pdev->dev,
12179 				 "MSI/legacy misc vector request failed, error %d\n",
12180 				 err);
12181 			return err;
12182 		}
12183 		i40e_enable_misc_int_causes(pf);
12184 		i40e_irq_dynamic_enable_icr0(pf);
12185 	}
12186 
12187 	return 0;
12188 }
12189 
12190 /**
12191  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12192  * @pf: board private structure
12193  *
12194  * This sets up the handler for MSIX 0, which is used to manage the
12195  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12196  * when in MSI or Legacy interrupt mode.
12197  **/
12198 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12199 {
12200 	struct i40e_hw *hw = &pf->hw;
12201 	int err = 0;
12202 
12203 	/* Only request the IRQ once, the first time through. */
12204 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12205 		err = request_irq(pf->msix_entries[0].vector,
12206 				  i40e_intr, 0, pf->int_name, pf);
12207 		if (err) {
12208 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12209 			dev_info(&pf->pdev->dev,
12210 				 "request_irq for %s failed: %d\n",
12211 				 pf->int_name, err);
12212 			return -EFAULT;
12213 		}
12214 	}
12215 
12216 	i40e_enable_misc_int_causes(pf);
12217 
12218 	/* associate no queues to the misc vector */
12219 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12220 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12221 
12222 	i40e_flush(hw);
12223 
12224 	i40e_irq_dynamic_enable_icr0(pf);
12225 
12226 	return err;
12227 }
12228 
12229 /**
12230  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12231  * @vsi: Pointer to vsi structure
12232  * @seed: Buffter to store the hash keys
12233  * @lut: Buffer to store the lookup table entries
12234  * @lut_size: Size of buffer to store the lookup table entries
12235  *
12236  * Return 0 on success, negative on failure
12237  */
12238 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12239 			   u8 *lut, u16 lut_size)
12240 {
12241 	struct i40e_pf *pf = vsi->back;
12242 	struct i40e_hw *hw = &pf->hw;
12243 	int ret = 0;
12244 
12245 	if (seed) {
12246 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12247 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12248 		if (ret) {
12249 			dev_info(&pf->pdev->dev,
12250 				 "Cannot get RSS key, err %pe aq_err %s\n",
12251 				 ERR_PTR(ret),
12252 				 i40e_aq_str(&pf->hw,
12253 					     pf->hw.aq.asq_last_status));
12254 			return ret;
12255 		}
12256 	}
12257 
12258 	if (lut) {
12259 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12260 
12261 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12262 		if (ret) {
12263 			dev_info(&pf->pdev->dev,
12264 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12265 				 ERR_PTR(ret),
12266 				 i40e_aq_str(&pf->hw,
12267 					     pf->hw.aq.asq_last_status));
12268 			return ret;
12269 		}
12270 	}
12271 
12272 	return ret;
12273 }
12274 
12275 /**
12276  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12277  * @vsi: Pointer to vsi structure
12278  * @seed: RSS hash seed
12279  * @lut: Lookup table
12280  * @lut_size: Lookup table size
12281  *
12282  * Returns 0 on success, negative on failure
12283  **/
12284 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12285 			       const u8 *lut, u16 lut_size)
12286 {
12287 	struct i40e_pf *pf = vsi->back;
12288 	struct i40e_hw *hw = &pf->hw;
12289 	u16 vf_id = vsi->vf_id;
12290 	u8 i;
12291 
12292 	/* Fill out hash function seed */
12293 	if (seed) {
12294 		u32 *seed_dw = (u32 *)seed;
12295 
12296 		if (vsi->type == I40E_VSI_MAIN) {
12297 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12298 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12299 		} else if (vsi->type == I40E_VSI_SRIOV) {
12300 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12301 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12302 		} else {
12303 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12304 		}
12305 	}
12306 
12307 	if (lut) {
12308 		u32 *lut_dw = (u32 *)lut;
12309 
12310 		if (vsi->type == I40E_VSI_MAIN) {
12311 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12312 				return -EINVAL;
12313 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12314 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12315 		} else if (vsi->type == I40E_VSI_SRIOV) {
12316 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12317 				return -EINVAL;
12318 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12319 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12320 		} else {
12321 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12322 		}
12323 	}
12324 	i40e_flush(hw);
12325 
12326 	return 0;
12327 }
12328 
12329 /**
12330  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12331  * @vsi: Pointer to VSI structure
12332  * @seed: Buffer to store the keys
12333  * @lut: Buffer to store the lookup table entries
12334  * @lut_size: Size of buffer to store the lookup table entries
12335  *
12336  * Returns 0 on success, negative on failure
12337  */
12338 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12339 			    u8 *lut, u16 lut_size)
12340 {
12341 	struct i40e_pf *pf = vsi->back;
12342 	struct i40e_hw *hw = &pf->hw;
12343 	u16 i;
12344 
12345 	if (seed) {
12346 		u32 *seed_dw = (u32 *)seed;
12347 
12348 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12349 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12350 	}
12351 	if (lut) {
12352 		u32 *lut_dw = (u32 *)lut;
12353 
12354 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12355 			return -EINVAL;
12356 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12357 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12358 	}
12359 
12360 	return 0;
12361 }
12362 
12363 /**
12364  * i40e_config_rss - Configure RSS keys and lut
12365  * @vsi: Pointer to VSI structure
12366  * @seed: RSS hash seed
12367  * @lut: Lookup table
12368  * @lut_size: Lookup table size
12369  *
12370  * Returns 0 on success, negative on failure
12371  */
12372 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12373 {
12374 	struct i40e_pf *pf = vsi->back;
12375 
12376 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12377 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12378 	else
12379 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12380 }
12381 
12382 /**
12383  * i40e_get_rss - Get RSS keys and lut
12384  * @vsi: Pointer to VSI structure
12385  * @seed: Buffer to store the keys
12386  * @lut: Buffer to store the lookup table entries
12387  * @lut_size: Size of buffer to store the lookup table entries
12388  *
12389  * Returns 0 on success, negative on failure
12390  */
12391 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12392 {
12393 	struct i40e_pf *pf = vsi->back;
12394 
12395 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12396 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12397 	else
12398 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12399 }
12400 
12401 /**
12402  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12403  * @pf: Pointer to board private structure
12404  * @lut: Lookup table
12405  * @rss_table_size: Lookup table size
12406  * @rss_size: Range of queue number for hashing
12407  */
12408 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12409 		       u16 rss_table_size, u16 rss_size)
12410 {
12411 	u16 i;
12412 
12413 	for (i = 0; i < rss_table_size; i++)
12414 		lut[i] = i % rss_size;
12415 }
12416 
12417 /**
12418  * i40e_pf_config_rss - Prepare for RSS if used
12419  * @pf: board private structure
12420  **/
12421 static int i40e_pf_config_rss(struct i40e_pf *pf)
12422 {
12423 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12424 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12425 	u8 *lut;
12426 	struct i40e_hw *hw = &pf->hw;
12427 	u32 reg_val;
12428 	u64 hena;
12429 	int ret;
12430 
12431 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12432 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12433 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12434 	hena |= i40e_pf_get_default_rss_hena(pf);
12435 
12436 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12437 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12438 
12439 	/* Determine the RSS table size based on the hardware capabilities */
12440 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12441 	reg_val = (pf->rss_table_size == 512) ?
12442 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12443 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12444 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12445 
12446 	/* Determine the RSS size of the VSI */
12447 	if (!vsi->rss_size) {
12448 		u16 qcount;
12449 		/* If the firmware does something weird during VSI init, we
12450 		 * could end up with zero TCs. Check for that to avoid
12451 		 * divide-by-zero. It probably won't pass traffic, but it also
12452 		 * won't panic.
12453 		 */
12454 		qcount = vsi->num_queue_pairs /
12455 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12456 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12457 	}
12458 	if (!vsi->rss_size)
12459 		return -EINVAL;
12460 
12461 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12462 	if (!lut)
12463 		return -ENOMEM;
12464 
12465 	/* Use user configured lut if there is one, otherwise use default */
12466 	if (vsi->rss_lut_user)
12467 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12468 	else
12469 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12470 
12471 	/* Use user configured hash key if there is one, otherwise
12472 	 * use default.
12473 	 */
12474 	if (vsi->rss_hkey_user)
12475 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12476 	else
12477 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12478 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12479 	kfree(lut);
12480 
12481 	return ret;
12482 }
12483 
12484 /**
12485  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12486  * @pf: board private structure
12487  * @queue_count: the requested queue count for rss.
12488  *
12489  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12490  * count which may be different from the requested queue count.
12491  * Note: expects to be called while under rtnl_lock()
12492  **/
12493 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12494 {
12495 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12496 	int new_rss_size;
12497 
12498 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12499 		return 0;
12500 
12501 	queue_count = min_t(int, queue_count, num_online_cpus());
12502 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12503 
12504 	if (queue_count != vsi->num_queue_pairs) {
12505 		u16 qcount;
12506 
12507 		vsi->req_queue_pairs = queue_count;
12508 		i40e_prep_for_reset(pf);
12509 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12510 			return pf->alloc_rss_size;
12511 
12512 		pf->alloc_rss_size = new_rss_size;
12513 
12514 		i40e_reset_and_rebuild(pf, true, true);
12515 
12516 		/* Discard the user configured hash keys and lut, if less
12517 		 * queues are enabled.
12518 		 */
12519 		if (queue_count < vsi->rss_size) {
12520 			i40e_clear_rss_config_user(vsi);
12521 			dev_dbg(&pf->pdev->dev,
12522 				"discard user configured hash keys and lut\n");
12523 		}
12524 
12525 		/* Reset vsi->rss_size, as number of enabled queues changed */
12526 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12527 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12528 
12529 		i40e_pf_config_rss(pf);
12530 	}
12531 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12532 		 vsi->req_queue_pairs, pf->rss_size_max);
12533 	return pf->alloc_rss_size;
12534 }
12535 
12536 /**
12537  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12538  * @pf: board private structure
12539  **/
12540 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12541 {
12542 	bool min_valid, max_valid;
12543 	u32 max_bw, min_bw;
12544 	int status;
12545 
12546 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12547 					   &min_valid, &max_valid);
12548 
12549 	if (!status) {
12550 		if (min_valid)
12551 			pf->min_bw = min_bw;
12552 		if (max_valid)
12553 			pf->max_bw = max_bw;
12554 	}
12555 
12556 	return status;
12557 }
12558 
12559 /**
12560  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12561  * @pf: board private structure
12562  **/
12563 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12564 {
12565 	struct i40e_aqc_configure_partition_bw_data bw_data;
12566 	int status;
12567 
12568 	memset(&bw_data, 0, sizeof(bw_data));
12569 
12570 	/* Set the valid bit for this PF */
12571 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12572 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12573 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12574 
12575 	/* Set the new bandwidths */
12576 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12577 
12578 	return status;
12579 }
12580 
12581 /**
12582  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12583  * @pf: board private structure
12584  **/
12585 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12586 {
12587 	/* Commit temporary BW setting to permanent NVM image */
12588 	enum i40e_admin_queue_err last_aq_status;
12589 	u16 nvm_word;
12590 	int ret;
12591 
12592 	if (pf->hw.partition_id != 1) {
12593 		dev_info(&pf->pdev->dev,
12594 			 "Commit BW only works on partition 1! This is partition %d",
12595 			 pf->hw.partition_id);
12596 		ret = -EOPNOTSUPP;
12597 		goto bw_commit_out;
12598 	}
12599 
12600 	/* Acquire NVM for read access */
12601 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12602 	last_aq_status = pf->hw.aq.asq_last_status;
12603 	if (ret) {
12604 		dev_info(&pf->pdev->dev,
12605 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12606 			 ERR_PTR(ret),
12607 			 i40e_aq_str(&pf->hw, last_aq_status));
12608 		goto bw_commit_out;
12609 	}
12610 
12611 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12612 	ret = i40e_aq_read_nvm(&pf->hw,
12613 			       I40E_SR_NVM_CONTROL_WORD,
12614 			       0x10, sizeof(nvm_word), &nvm_word,
12615 			       false, NULL);
12616 	/* Save off last admin queue command status before releasing
12617 	 * the NVM
12618 	 */
12619 	last_aq_status = pf->hw.aq.asq_last_status;
12620 	i40e_release_nvm(&pf->hw);
12621 	if (ret) {
12622 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12623 			 ERR_PTR(ret),
12624 			 i40e_aq_str(&pf->hw, last_aq_status));
12625 		goto bw_commit_out;
12626 	}
12627 
12628 	/* Wait a bit for NVM release to complete */
12629 	msleep(50);
12630 
12631 	/* Acquire NVM for write access */
12632 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12633 	last_aq_status = pf->hw.aq.asq_last_status;
12634 	if (ret) {
12635 		dev_info(&pf->pdev->dev,
12636 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12637 			 ERR_PTR(ret),
12638 			 i40e_aq_str(&pf->hw, last_aq_status));
12639 		goto bw_commit_out;
12640 	}
12641 	/* Write it back out unchanged to initiate update NVM,
12642 	 * which will force a write of the shadow (alt) RAM to
12643 	 * the NVM - thus storing the bandwidth values permanently.
12644 	 */
12645 	ret = i40e_aq_update_nvm(&pf->hw,
12646 				 I40E_SR_NVM_CONTROL_WORD,
12647 				 0x10, sizeof(nvm_word),
12648 				 &nvm_word, true, 0, NULL);
12649 	/* Save off last admin queue command status before releasing
12650 	 * the NVM
12651 	 */
12652 	last_aq_status = pf->hw.aq.asq_last_status;
12653 	i40e_release_nvm(&pf->hw);
12654 	if (ret)
12655 		dev_info(&pf->pdev->dev,
12656 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12657 			 ERR_PTR(ret),
12658 			 i40e_aq_str(&pf->hw, last_aq_status));
12659 bw_commit_out:
12660 
12661 	return ret;
12662 }
12663 
12664 /**
12665  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12666  * if total port shutdown feature is enabled for this PF
12667  * @pf: board private structure
12668  **/
12669 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12670 {
12671 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12672 #define I40E_FEATURES_ENABLE_PTR		0x2A
12673 #define I40E_CURRENT_SETTING_PTR		0x2B
12674 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12675 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12676 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12677 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12678 	u16 sr_emp_sr_settings_ptr = 0;
12679 	u16 features_enable = 0;
12680 	u16 link_behavior = 0;
12681 	int read_status = 0;
12682 	bool ret = false;
12683 
12684 	read_status = i40e_read_nvm_word(&pf->hw,
12685 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12686 					 &sr_emp_sr_settings_ptr);
12687 	if (read_status)
12688 		goto err_nvm;
12689 	read_status = i40e_read_nvm_word(&pf->hw,
12690 					 sr_emp_sr_settings_ptr +
12691 					 I40E_FEATURES_ENABLE_PTR,
12692 					 &features_enable);
12693 	if (read_status)
12694 		goto err_nvm;
12695 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12696 		read_status = i40e_read_nvm_module_data(&pf->hw,
12697 							I40E_SR_EMP_SR_SETTINGS_PTR,
12698 							I40E_CURRENT_SETTING_PTR,
12699 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12700 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12701 							&link_behavior);
12702 		if (read_status)
12703 			goto err_nvm;
12704 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12705 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12706 	}
12707 	return ret;
12708 
12709 err_nvm:
12710 	dev_warn(&pf->pdev->dev,
12711 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12712 		 ERR_PTR(read_status));
12713 	return ret;
12714 }
12715 
12716 /**
12717  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12718  * @pf: board private structure to initialize
12719  *
12720  * i40e_sw_init initializes the Adapter private data structure.
12721  * Fields are initialized based on PCI device information and
12722  * OS network device settings (MTU size).
12723  **/
12724 static int i40e_sw_init(struct i40e_pf *pf)
12725 {
12726 	int err = 0;
12727 	int size;
12728 	u16 pow;
12729 
12730 	/* Set default capability flags */
12731 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12732 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12733 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12734 
12735 	/* Set default ITR */
12736 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12737 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12738 
12739 	/* Depending on PF configurations, it is possible that the RSS
12740 	 * maximum might end up larger than the available queues
12741 	 */
12742 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12743 	pf->alloc_rss_size = 1;
12744 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12745 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12746 				 pf->hw.func_caps.num_tx_qp);
12747 
12748 	/* find the next higher power-of-2 of num cpus */
12749 	pow = roundup_pow_of_two(num_online_cpus());
12750 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12751 
12752 	if (pf->hw.func_caps.rss) {
12753 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12754 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12755 					   num_online_cpus());
12756 	}
12757 
12758 	/* MFP mode enabled */
12759 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12760 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12761 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12762 		if (i40e_get_partition_bw_setting(pf)) {
12763 			dev_warn(&pf->pdev->dev,
12764 				 "Could not get partition bw settings\n");
12765 		} else {
12766 			dev_info(&pf->pdev->dev,
12767 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12768 				 pf->min_bw, pf->max_bw);
12769 
12770 			/* nudge the Tx scheduler */
12771 			i40e_set_partition_bw_setting(pf);
12772 		}
12773 	}
12774 
12775 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12776 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12777 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12778 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12779 		    pf->hw.num_partitions > 1)
12780 			dev_info(&pf->pdev->dev,
12781 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12782 		else
12783 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12784 		pf->fdir_pf_filter_count =
12785 				 pf->hw.func_caps.fd_filters_guaranteed;
12786 		pf->hw.fdir_shared_filter_count =
12787 				 pf->hw.func_caps.fd_filters_best_effort;
12788 	}
12789 
12790 	/* Enable HW ATR eviction if possible */
12791 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12792 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12793 
12794 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12795 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12796 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12797 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12798 	}
12799 
12800 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12801 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12802 		/* IWARP needs one extra vector for CQP just like MISC.*/
12803 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12804 	}
12805 	/* Stopping FW LLDP engine is supported on XL710 and X722
12806 	 * starting from FW versions determined in i40e_init_adminq.
12807 	 * Stopping the FW LLDP engine is not supported on XL710
12808 	 * if NPAR is functioning so unset this hw flag in this case.
12809 	 */
12810 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12811 	    pf->hw.func_caps.npar_enable)
12812 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12813 
12814 #ifdef CONFIG_PCI_IOV
12815 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12816 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12817 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12818 		pf->num_req_vfs = min_t(int,
12819 					pf->hw.func_caps.num_vfs,
12820 					I40E_MAX_VF_COUNT);
12821 	}
12822 #endif /* CONFIG_PCI_IOV */
12823 	pf->lan_veb = I40E_NO_VEB;
12824 	pf->lan_vsi = I40E_NO_VSI;
12825 
12826 	/* By default FW has this off for performance reasons */
12827 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12828 
12829 	/* set up queue assignment tracking */
12830 	size = sizeof(struct i40e_lump_tracking)
12831 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12832 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12833 	if (!pf->qp_pile) {
12834 		err = -ENOMEM;
12835 		goto sw_init_done;
12836 	}
12837 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12838 
12839 	pf->tx_timeout_recovery_level = 1;
12840 
12841 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12842 	    i40e_is_total_port_shutdown_enabled(pf)) {
12843 		/* Link down on close must be on when total port shutdown
12844 		 * is enabled for a given port
12845 		 */
12846 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12847 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12848 		dev_info(&pf->pdev->dev,
12849 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12850 	}
12851 	mutex_init(&pf->switch_mutex);
12852 
12853 sw_init_done:
12854 	return err;
12855 }
12856 
12857 /**
12858  * i40e_set_ntuple - set the ntuple feature flag and take action
12859  * @pf: board private structure to initialize
12860  * @features: the feature set that the stack is suggesting
12861  *
12862  * returns a bool to indicate if reset needs to happen
12863  **/
12864 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12865 {
12866 	bool need_reset = false;
12867 
12868 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12869 	 * the state changed, we need to reset.
12870 	 */
12871 	if (features & NETIF_F_NTUPLE) {
12872 		/* Enable filters and mark for reset */
12873 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12874 			need_reset = true;
12875 		/* enable FD_SB only if there is MSI-X vector and no cloud
12876 		 * filters exist
12877 		 */
12878 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12879 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12880 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12881 		}
12882 	} else {
12883 		/* turn off filters, mark for reset and clear SW filter list */
12884 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12885 			need_reset = true;
12886 			i40e_fdir_filter_exit(pf);
12887 		}
12888 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12889 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12890 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12891 
12892 		/* reset fd counters */
12893 		pf->fd_add_err = 0;
12894 		pf->fd_atr_cnt = 0;
12895 		/* if ATR was auto disabled it can be re-enabled. */
12896 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12897 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12898 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12899 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12900 	}
12901 	return need_reset;
12902 }
12903 
12904 /**
12905  * i40e_clear_rss_lut - clear the rx hash lookup table
12906  * @vsi: the VSI being configured
12907  **/
12908 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12909 {
12910 	struct i40e_pf *pf = vsi->back;
12911 	struct i40e_hw *hw = &pf->hw;
12912 	u16 vf_id = vsi->vf_id;
12913 	u8 i;
12914 
12915 	if (vsi->type == I40E_VSI_MAIN) {
12916 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12917 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12918 	} else if (vsi->type == I40E_VSI_SRIOV) {
12919 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12920 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12921 	} else {
12922 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12923 	}
12924 }
12925 
12926 /**
12927  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12928  * @vsi: ptr to VSI
12929  * @ena: flag to indicate the on/off setting
12930  */
12931 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12932 {
12933 	bool if_running = netif_running(vsi->netdev) &&
12934 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12935 	int ret;
12936 
12937 	if (if_running)
12938 		i40e_down(vsi);
12939 
12940 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12941 	if (ret)
12942 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12943 	if (if_running)
12944 		i40e_up(vsi);
12945 
12946 	return ret;
12947 }
12948 
12949 /**
12950  * i40e_set_features - set the netdev feature flags
12951  * @netdev: ptr to the netdev being adjusted
12952  * @features: the feature set that the stack is suggesting
12953  * Note: expects to be called while under rtnl_lock()
12954  **/
12955 static int i40e_set_features(struct net_device *netdev,
12956 			     netdev_features_t features)
12957 {
12958 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12959 	struct i40e_vsi *vsi = np->vsi;
12960 	struct i40e_pf *pf = vsi->back;
12961 	bool need_reset;
12962 
12963 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12964 		i40e_pf_config_rss(pf);
12965 	else if (!(features & NETIF_F_RXHASH) &&
12966 		 netdev->features & NETIF_F_RXHASH)
12967 		i40e_clear_rss_lut(vsi);
12968 
12969 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12970 		i40e_vlan_stripping_enable(vsi);
12971 	else
12972 		i40e_vlan_stripping_disable(vsi);
12973 
12974 	if (!(features & NETIF_F_HW_TC) &&
12975 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12976 		dev_err(&pf->pdev->dev,
12977 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12978 		return -EINVAL;
12979 	}
12980 
12981 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12982 		i40e_del_all_macvlans(vsi);
12983 
12984 	need_reset = i40e_set_ntuple(pf, features);
12985 
12986 	if (need_reset)
12987 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12988 
12989 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12990 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12991 
12992 	return 0;
12993 }
12994 
12995 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12996 				    unsigned int table, unsigned int idx,
12997 				    struct udp_tunnel_info *ti)
12998 {
12999 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13000 	struct i40e_hw *hw = &np->vsi->back->hw;
13001 	u8 type, filter_index;
13002 	int ret;
13003 
13004 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13005 						   I40E_AQC_TUNNEL_TYPE_NGE;
13006 
13007 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13008 				     NULL);
13009 	if (ret) {
13010 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13011 			    ERR_PTR(ret),
13012 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13013 		return -EIO;
13014 	}
13015 
13016 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13017 	return 0;
13018 }
13019 
13020 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13021 				      unsigned int table, unsigned int idx,
13022 				      struct udp_tunnel_info *ti)
13023 {
13024 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13025 	struct i40e_hw *hw = &np->vsi->back->hw;
13026 	int ret;
13027 
13028 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13029 	if (ret) {
13030 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13031 			    ERR_PTR(ret),
13032 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13033 		return -EIO;
13034 	}
13035 
13036 	return 0;
13037 }
13038 
13039 static int i40e_get_phys_port_id(struct net_device *netdev,
13040 				 struct netdev_phys_item_id *ppid)
13041 {
13042 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13043 	struct i40e_pf *pf = np->vsi->back;
13044 	struct i40e_hw *hw = &pf->hw;
13045 
13046 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13047 		return -EOPNOTSUPP;
13048 
13049 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13050 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13051 
13052 	return 0;
13053 }
13054 
13055 /**
13056  * i40e_ndo_fdb_add - add an entry to the hardware database
13057  * @ndm: the input from the stack
13058  * @tb: pointer to array of nladdr (unused)
13059  * @dev: the net device pointer
13060  * @addr: the MAC address entry being added
13061  * @vid: VLAN ID
13062  * @flags: instructions from stack about fdb operation
13063  * @extack: netlink extended ack, unused currently
13064  */
13065 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13066 			    struct net_device *dev,
13067 			    const unsigned char *addr, u16 vid,
13068 			    u16 flags,
13069 			    struct netlink_ext_ack *extack)
13070 {
13071 	struct i40e_netdev_priv *np = netdev_priv(dev);
13072 	struct i40e_pf *pf = np->vsi->back;
13073 	int err = 0;
13074 
13075 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13076 		return -EOPNOTSUPP;
13077 
13078 	if (vid) {
13079 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13080 		return -EINVAL;
13081 	}
13082 
13083 	/* Hardware does not support aging addresses so if a
13084 	 * ndm_state is given only allow permanent addresses
13085 	 */
13086 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13087 		netdev_info(dev, "FDB only supports static addresses\n");
13088 		return -EINVAL;
13089 	}
13090 
13091 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13092 		err = dev_uc_add_excl(dev, addr);
13093 	else if (is_multicast_ether_addr(addr))
13094 		err = dev_mc_add_excl(dev, addr);
13095 	else
13096 		err = -EINVAL;
13097 
13098 	/* Only return duplicate errors if NLM_F_EXCL is set */
13099 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13100 		err = 0;
13101 
13102 	return err;
13103 }
13104 
13105 /**
13106  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13107  * @dev: the netdev being configured
13108  * @nlh: RTNL message
13109  * @flags: bridge flags
13110  * @extack: netlink extended ack
13111  *
13112  * Inserts a new hardware bridge if not already created and
13113  * enables the bridging mode requested (VEB or VEPA). If the
13114  * hardware bridge has already been inserted and the request
13115  * is to change the mode then that requires a PF reset to
13116  * allow rebuild of the components with required hardware
13117  * bridge mode enabled.
13118  *
13119  * Note: expects to be called while under rtnl_lock()
13120  **/
13121 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13122 				   struct nlmsghdr *nlh,
13123 				   u16 flags,
13124 				   struct netlink_ext_ack *extack)
13125 {
13126 	struct i40e_netdev_priv *np = netdev_priv(dev);
13127 	struct i40e_vsi *vsi = np->vsi;
13128 	struct i40e_pf *pf = vsi->back;
13129 	struct i40e_veb *veb = NULL;
13130 	struct nlattr *attr, *br_spec;
13131 	int i, rem;
13132 
13133 	/* Only for PF VSI for now */
13134 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13135 		return -EOPNOTSUPP;
13136 
13137 	/* Find the HW bridge for PF VSI */
13138 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13139 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13140 			veb = pf->veb[i];
13141 	}
13142 
13143 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13144 	if (!br_spec)
13145 		return -EINVAL;
13146 
13147 	nla_for_each_nested(attr, br_spec, rem) {
13148 		__u16 mode;
13149 
13150 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13151 			continue;
13152 
13153 		mode = nla_get_u16(attr);
13154 		if ((mode != BRIDGE_MODE_VEPA) &&
13155 		    (mode != BRIDGE_MODE_VEB))
13156 			return -EINVAL;
13157 
13158 		/* Insert a new HW bridge */
13159 		if (!veb) {
13160 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13161 					     vsi->tc_config.enabled_tc);
13162 			if (veb) {
13163 				veb->bridge_mode = mode;
13164 				i40e_config_bridge_mode(veb);
13165 			} else {
13166 				/* No Bridge HW offload available */
13167 				return -ENOENT;
13168 			}
13169 			break;
13170 		} else if (mode != veb->bridge_mode) {
13171 			/* Existing HW bridge but different mode needs reset */
13172 			veb->bridge_mode = mode;
13173 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13174 			if (mode == BRIDGE_MODE_VEB)
13175 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13176 			else
13177 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13178 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13179 			break;
13180 		}
13181 	}
13182 
13183 	return 0;
13184 }
13185 
13186 /**
13187  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13188  * @skb: skb buff
13189  * @pid: process id
13190  * @seq: RTNL message seq #
13191  * @dev: the netdev being configured
13192  * @filter_mask: unused
13193  * @nlflags: netlink flags passed in
13194  *
13195  * Return the mode in which the hardware bridge is operating in
13196  * i.e VEB or VEPA.
13197  **/
13198 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13199 				   struct net_device *dev,
13200 				   u32 __always_unused filter_mask,
13201 				   int nlflags)
13202 {
13203 	struct i40e_netdev_priv *np = netdev_priv(dev);
13204 	struct i40e_vsi *vsi = np->vsi;
13205 	struct i40e_pf *pf = vsi->back;
13206 	struct i40e_veb *veb = NULL;
13207 	int i;
13208 
13209 	/* Only for PF VSI for now */
13210 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13211 		return -EOPNOTSUPP;
13212 
13213 	/* Find the HW bridge for the PF VSI */
13214 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13215 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13216 			veb = pf->veb[i];
13217 	}
13218 
13219 	if (!veb)
13220 		return 0;
13221 
13222 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13223 				       0, 0, nlflags, filter_mask, NULL);
13224 }
13225 
13226 /**
13227  * i40e_features_check - Validate encapsulated packet conforms to limits
13228  * @skb: skb buff
13229  * @dev: This physical port's netdev
13230  * @features: Offload features that the stack believes apply
13231  **/
13232 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13233 					     struct net_device *dev,
13234 					     netdev_features_t features)
13235 {
13236 	size_t len;
13237 
13238 	/* No point in doing any of this if neither checksum nor GSO are
13239 	 * being requested for this frame.  We can rule out both by just
13240 	 * checking for CHECKSUM_PARTIAL
13241 	 */
13242 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13243 		return features;
13244 
13245 	/* We cannot support GSO if the MSS is going to be less than
13246 	 * 64 bytes.  If it is then we need to drop support for GSO.
13247 	 */
13248 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13249 		features &= ~NETIF_F_GSO_MASK;
13250 
13251 	/* MACLEN can support at most 63 words */
13252 	len = skb_network_header(skb) - skb->data;
13253 	if (len & ~(63 * 2))
13254 		goto out_err;
13255 
13256 	/* IPLEN and EIPLEN can support at most 127 dwords */
13257 	len = skb_transport_header(skb) - skb_network_header(skb);
13258 	if (len & ~(127 * 4))
13259 		goto out_err;
13260 
13261 	if (skb->encapsulation) {
13262 		/* L4TUNLEN can support 127 words */
13263 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13264 		if (len & ~(127 * 2))
13265 			goto out_err;
13266 
13267 		/* IPLEN can support at most 127 dwords */
13268 		len = skb_inner_transport_header(skb) -
13269 		      skb_inner_network_header(skb);
13270 		if (len & ~(127 * 4))
13271 			goto out_err;
13272 	}
13273 
13274 	/* No need to validate L4LEN as TCP is the only protocol with a
13275 	 * flexible value and we support all possible values supported
13276 	 * by TCP, which is at most 15 dwords
13277 	 */
13278 
13279 	return features;
13280 out_err:
13281 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13282 }
13283 
13284 /**
13285  * i40e_xdp_setup - add/remove an XDP program
13286  * @vsi: VSI to changed
13287  * @prog: XDP program
13288  * @extack: netlink extended ack
13289  **/
13290 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13291 			  struct netlink_ext_ack *extack)
13292 {
13293 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13294 	struct i40e_pf *pf = vsi->back;
13295 	struct bpf_prog *old_prog;
13296 	bool need_reset;
13297 	int i;
13298 
13299 	/* Don't allow frames that span over multiple buffers */
13300 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13301 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13302 		return -EINVAL;
13303 	}
13304 
13305 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13306 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13307 
13308 	if (need_reset)
13309 		i40e_prep_for_reset(pf);
13310 
13311 	/* VSI shall be deleted in a moment, just return EINVAL */
13312 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13313 		return -EINVAL;
13314 
13315 	old_prog = xchg(&vsi->xdp_prog, prog);
13316 
13317 	if (need_reset) {
13318 		if (!prog) {
13319 			xdp_features_clear_redirect_target(vsi->netdev);
13320 			/* Wait until ndo_xsk_wakeup completes. */
13321 			synchronize_rcu();
13322 		}
13323 		i40e_reset_and_rebuild(pf, true, true);
13324 	}
13325 
13326 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13327 		if (i40e_realloc_rx_bi_zc(vsi, true))
13328 			return -ENOMEM;
13329 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13330 		if (i40e_realloc_rx_bi_zc(vsi, false))
13331 			return -ENOMEM;
13332 	}
13333 
13334 	for (i = 0; i < vsi->num_queue_pairs; i++)
13335 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13336 
13337 	if (old_prog)
13338 		bpf_prog_put(old_prog);
13339 
13340 	/* Kick start the NAPI context if there is an AF_XDP socket open
13341 	 * on that queue id. This so that receiving will start.
13342 	 */
13343 	if (need_reset && prog) {
13344 		for (i = 0; i < vsi->num_queue_pairs; i++)
13345 			if (vsi->xdp_rings[i]->xsk_pool)
13346 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13347 						      XDP_WAKEUP_RX);
13348 		xdp_features_set_redirect_target(vsi->netdev, true);
13349 	}
13350 
13351 	return 0;
13352 }
13353 
13354 /**
13355  * i40e_enter_busy_conf - Enters busy config state
13356  * @vsi: vsi
13357  *
13358  * Returns 0 on success, <0 for failure.
13359  **/
13360 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13361 {
13362 	struct i40e_pf *pf = vsi->back;
13363 	int timeout = 50;
13364 
13365 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13366 		timeout--;
13367 		if (!timeout)
13368 			return -EBUSY;
13369 		usleep_range(1000, 2000);
13370 	}
13371 
13372 	return 0;
13373 }
13374 
13375 /**
13376  * i40e_exit_busy_conf - Exits busy config state
13377  * @vsi: vsi
13378  **/
13379 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13380 {
13381 	struct i40e_pf *pf = vsi->back;
13382 
13383 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13384 }
13385 
13386 /**
13387  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13388  * @vsi: vsi
13389  * @queue_pair: queue pair
13390  **/
13391 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13392 {
13393 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13394 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13395 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13396 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13397 	if (i40e_enabled_xdp_vsi(vsi)) {
13398 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13399 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13400 	}
13401 }
13402 
13403 /**
13404  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13405  * @vsi: vsi
13406  * @queue_pair: queue pair
13407  **/
13408 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13409 {
13410 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13411 	if (i40e_enabled_xdp_vsi(vsi)) {
13412 		/* Make sure that in-progress ndo_xdp_xmit calls are
13413 		 * completed.
13414 		 */
13415 		synchronize_rcu();
13416 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13417 	}
13418 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13419 }
13420 
13421 /**
13422  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13423  * @vsi: vsi
13424  * @queue_pair: queue pair
13425  * @enable: true for enable, false for disable
13426  **/
13427 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13428 					bool enable)
13429 {
13430 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13431 	struct i40e_q_vector *q_vector = rxr->q_vector;
13432 
13433 	if (!vsi->netdev)
13434 		return;
13435 
13436 	/* All rings in a qp belong to the same qvector. */
13437 	if (q_vector->rx.ring || q_vector->tx.ring) {
13438 		if (enable)
13439 			napi_enable(&q_vector->napi);
13440 		else
13441 			napi_disable(&q_vector->napi);
13442 	}
13443 }
13444 
13445 /**
13446  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13447  * @vsi: vsi
13448  * @queue_pair: queue pair
13449  * @enable: true for enable, false for disable
13450  *
13451  * Returns 0 on success, <0 on failure.
13452  **/
13453 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13454 					bool enable)
13455 {
13456 	struct i40e_pf *pf = vsi->back;
13457 	int pf_q, ret = 0;
13458 
13459 	pf_q = vsi->base_queue + queue_pair;
13460 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13461 				     false /*is xdp*/, enable);
13462 	if (ret) {
13463 		dev_info(&pf->pdev->dev,
13464 			 "VSI seid %d Tx ring %d %sable timeout\n",
13465 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13466 		return ret;
13467 	}
13468 
13469 	i40e_control_rx_q(pf, pf_q, enable);
13470 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13471 	if (ret) {
13472 		dev_info(&pf->pdev->dev,
13473 			 "VSI seid %d Rx ring %d %sable timeout\n",
13474 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13475 		return ret;
13476 	}
13477 
13478 	/* Due to HW errata, on Rx disable only, the register can
13479 	 * indicate done before it really is. Needs 50ms to be sure
13480 	 */
13481 	if (!enable)
13482 		mdelay(50);
13483 
13484 	if (!i40e_enabled_xdp_vsi(vsi))
13485 		return ret;
13486 
13487 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13488 				     pf_q + vsi->alloc_queue_pairs,
13489 				     true /*is xdp*/, enable);
13490 	if (ret) {
13491 		dev_info(&pf->pdev->dev,
13492 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13493 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13494 	}
13495 
13496 	return ret;
13497 }
13498 
13499 /**
13500  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13501  * @vsi: vsi
13502  * @queue_pair: queue_pair
13503  **/
13504 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13505 {
13506 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13507 	struct i40e_pf *pf = vsi->back;
13508 	struct i40e_hw *hw = &pf->hw;
13509 
13510 	/* All rings in a qp belong to the same qvector. */
13511 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13512 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13513 	else
13514 		i40e_irq_dynamic_enable_icr0(pf);
13515 
13516 	i40e_flush(hw);
13517 }
13518 
13519 /**
13520  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13521  * @vsi: vsi
13522  * @queue_pair: queue_pair
13523  **/
13524 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13525 {
13526 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13527 	struct i40e_pf *pf = vsi->back;
13528 	struct i40e_hw *hw = &pf->hw;
13529 
13530 	/* For simplicity, instead of removing the qp interrupt causes
13531 	 * from the interrupt linked list, we simply disable the interrupt, and
13532 	 * leave the list intact.
13533 	 *
13534 	 * All rings in a qp belong to the same qvector.
13535 	 */
13536 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13537 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13538 
13539 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13540 		i40e_flush(hw);
13541 		synchronize_irq(pf->msix_entries[intpf].vector);
13542 	} else {
13543 		/* Legacy and MSI mode - this stops all interrupt handling */
13544 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13545 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13546 		i40e_flush(hw);
13547 		synchronize_irq(pf->pdev->irq);
13548 	}
13549 }
13550 
13551 /**
13552  * i40e_queue_pair_disable - Disables a queue pair
13553  * @vsi: vsi
13554  * @queue_pair: queue pair
13555  *
13556  * Returns 0 on success, <0 on failure.
13557  **/
13558 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13559 {
13560 	int err;
13561 
13562 	err = i40e_enter_busy_conf(vsi);
13563 	if (err)
13564 		return err;
13565 
13566 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13567 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13568 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13569 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13570 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13571 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13572 
13573 	return err;
13574 }
13575 
13576 /**
13577  * i40e_queue_pair_enable - Enables a queue pair
13578  * @vsi: vsi
13579  * @queue_pair: queue pair
13580  *
13581  * Returns 0 on success, <0 on failure.
13582  **/
13583 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13584 {
13585 	int err;
13586 
13587 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13588 	if (err)
13589 		return err;
13590 
13591 	if (i40e_enabled_xdp_vsi(vsi)) {
13592 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13593 		if (err)
13594 			return err;
13595 	}
13596 
13597 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13598 	if (err)
13599 		return err;
13600 
13601 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13602 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13603 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13604 
13605 	i40e_exit_busy_conf(vsi);
13606 
13607 	return err;
13608 }
13609 
13610 /**
13611  * i40e_xdp - implements ndo_bpf for i40e
13612  * @dev: netdevice
13613  * @xdp: XDP command
13614  **/
13615 static int i40e_xdp(struct net_device *dev,
13616 		    struct netdev_bpf *xdp)
13617 {
13618 	struct i40e_netdev_priv *np = netdev_priv(dev);
13619 	struct i40e_vsi *vsi = np->vsi;
13620 
13621 	if (vsi->type != I40E_VSI_MAIN)
13622 		return -EINVAL;
13623 
13624 	switch (xdp->command) {
13625 	case XDP_SETUP_PROG:
13626 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13627 	case XDP_SETUP_XSK_POOL:
13628 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13629 					   xdp->xsk.queue_id);
13630 	default:
13631 		return -EINVAL;
13632 	}
13633 }
13634 
13635 static const struct net_device_ops i40e_netdev_ops = {
13636 	.ndo_open		= i40e_open,
13637 	.ndo_stop		= i40e_close,
13638 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13639 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13640 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13641 	.ndo_validate_addr	= eth_validate_addr,
13642 	.ndo_set_mac_address	= i40e_set_mac,
13643 	.ndo_change_mtu		= i40e_change_mtu,
13644 	.ndo_eth_ioctl		= i40e_ioctl,
13645 	.ndo_tx_timeout		= i40e_tx_timeout,
13646 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13647 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13648 #ifdef CONFIG_NET_POLL_CONTROLLER
13649 	.ndo_poll_controller	= i40e_netpoll,
13650 #endif
13651 	.ndo_setup_tc		= __i40e_setup_tc,
13652 	.ndo_select_queue	= i40e_lan_select_queue,
13653 	.ndo_set_features	= i40e_set_features,
13654 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13655 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13656 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13657 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13658 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13659 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13660 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13661 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13662 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13663 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13664 	.ndo_features_check	= i40e_features_check,
13665 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13666 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13667 	.ndo_bpf		= i40e_xdp,
13668 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13669 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13670 	.ndo_dfwd_add_station	= i40e_fwd_add,
13671 	.ndo_dfwd_del_station	= i40e_fwd_del,
13672 };
13673 
13674 /**
13675  * i40e_config_netdev - Setup the netdev flags
13676  * @vsi: the VSI being configured
13677  *
13678  * Returns 0 on success, negative value on failure
13679  **/
13680 static int i40e_config_netdev(struct i40e_vsi *vsi)
13681 {
13682 	struct i40e_pf *pf = vsi->back;
13683 	struct i40e_hw *hw = &pf->hw;
13684 	struct i40e_netdev_priv *np;
13685 	struct net_device *netdev;
13686 	u8 broadcast[ETH_ALEN];
13687 	u8 mac_addr[ETH_ALEN];
13688 	int etherdev_size;
13689 	netdev_features_t hw_enc_features;
13690 	netdev_features_t hw_features;
13691 
13692 	etherdev_size = sizeof(struct i40e_netdev_priv);
13693 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13694 	if (!netdev)
13695 		return -ENOMEM;
13696 
13697 	vsi->netdev = netdev;
13698 	np = netdev_priv(netdev);
13699 	np->vsi = vsi;
13700 
13701 	hw_enc_features = NETIF_F_SG			|
13702 			  NETIF_F_HW_CSUM		|
13703 			  NETIF_F_HIGHDMA		|
13704 			  NETIF_F_SOFT_FEATURES		|
13705 			  NETIF_F_TSO			|
13706 			  NETIF_F_TSO_ECN		|
13707 			  NETIF_F_TSO6			|
13708 			  NETIF_F_GSO_GRE		|
13709 			  NETIF_F_GSO_GRE_CSUM		|
13710 			  NETIF_F_GSO_PARTIAL		|
13711 			  NETIF_F_GSO_IPXIP4		|
13712 			  NETIF_F_GSO_IPXIP6		|
13713 			  NETIF_F_GSO_UDP_TUNNEL	|
13714 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13715 			  NETIF_F_GSO_UDP_L4		|
13716 			  NETIF_F_SCTP_CRC		|
13717 			  NETIF_F_RXHASH		|
13718 			  NETIF_F_RXCSUM		|
13719 			  0;
13720 
13721 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13722 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13723 
13724 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13725 
13726 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13727 
13728 	netdev->hw_enc_features |= hw_enc_features;
13729 
13730 	/* record features VLANs can make use of */
13731 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13732 
13733 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13734 				   NETIF_F_GSO_GRE_CSUM |	\
13735 				   NETIF_F_GSO_IPXIP4 |		\
13736 				   NETIF_F_GSO_IPXIP6 |		\
13737 				   NETIF_F_GSO_UDP_TUNNEL |	\
13738 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13739 
13740 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13741 	netdev->features |= NETIF_F_GSO_PARTIAL |
13742 			    I40E_GSO_PARTIAL_FEATURES;
13743 
13744 	netdev->mpls_features |= NETIF_F_SG;
13745 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13746 	netdev->mpls_features |= NETIF_F_TSO;
13747 	netdev->mpls_features |= NETIF_F_TSO6;
13748 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13749 
13750 	/* enable macvlan offloads */
13751 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13752 
13753 	hw_features = hw_enc_features		|
13754 		      NETIF_F_HW_VLAN_CTAG_TX	|
13755 		      NETIF_F_HW_VLAN_CTAG_RX;
13756 
13757 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13758 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13759 
13760 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13761 
13762 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13763 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13764 
13765 	netdev->features &= ~NETIF_F_HW_TC;
13766 
13767 	if (vsi->type == I40E_VSI_MAIN) {
13768 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13769 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13770 		/* The following steps are necessary for two reasons. First,
13771 		 * some older NVM configurations load a default MAC-VLAN
13772 		 * filter that will accept any tagged packet, and we want to
13773 		 * replace this with a normal filter. Additionally, it is
13774 		 * possible our MAC address was provided by the platform using
13775 		 * Open Firmware or similar.
13776 		 *
13777 		 * Thus, we need to remove the default filter and install one
13778 		 * specific to the MAC address.
13779 		 */
13780 		i40e_rm_default_mac_filter(vsi, mac_addr);
13781 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13782 		i40e_add_mac_filter(vsi, mac_addr);
13783 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13784 
13785 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13786 				       NETDEV_XDP_ACT_REDIRECT |
13787 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13788 				       NETDEV_XDP_ACT_RX_SG;
13789 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13790 	} else {
13791 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13792 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13793 		 * the end, which is 4 bytes long, so force truncation of the
13794 		 * original name by IFNAMSIZ - 4
13795 		 */
13796 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13797 			 IFNAMSIZ - 4,
13798 			 pf->vsi[pf->lan_vsi]->netdev->name);
13799 		eth_random_addr(mac_addr);
13800 
13801 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13802 		i40e_add_mac_filter(vsi, mac_addr);
13803 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13804 	}
13805 
13806 	/* Add the broadcast filter so that we initially will receive
13807 	 * broadcast packets. Note that when a new VLAN is first added the
13808 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13809 	 * specific filters as part of transitioning into "vlan" operation.
13810 	 * When more VLANs are added, the driver will copy each existing MAC
13811 	 * filter and add it for the new VLAN.
13812 	 *
13813 	 * Broadcast filters are handled specially by
13814 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13815 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13816 	 * filter. The subtask will update the correct broadcast promiscuous
13817 	 * bits as VLANs become active or inactive.
13818 	 */
13819 	eth_broadcast_addr(broadcast);
13820 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13821 	i40e_add_mac_filter(vsi, broadcast);
13822 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13823 
13824 	eth_hw_addr_set(netdev, mac_addr);
13825 	ether_addr_copy(netdev->perm_addr, mac_addr);
13826 
13827 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13828 	netdev->neigh_priv_len = sizeof(u32) * 4;
13829 
13830 	netdev->priv_flags |= IFF_UNICAST_FLT;
13831 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13832 	/* Setup netdev TC information */
13833 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13834 
13835 	netdev->netdev_ops = &i40e_netdev_ops;
13836 	netdev->watchdog_timeo = 5 * HZ;
13837 	i40e_set_ethtool_ops(netdev);
13838 
13839 	/* MTU range: 68 - 9706 */
13840 	netdev->min_mtu = ETH_MIN_MTU;
13841 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13842 
13843 	return 0;
13844 }
13845 
13846 /**
13847  * i40e_vsi_delete - Delete a VSI from the switch
13848  * @vsi: the VSI being removed
13849  *
13850  * Returns 0 on success, negative value on failure
13851  **/
13852 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13853 {
13854 	/* remove default VSI is not allowed */
13855 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13856 		return;
13857 
13858 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13859 }
13860 
13861 /**
13862  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13863  * @vsi: the VSI being queried
13864  *
13865  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13866  **/
13867 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13868 {
13869 	struct i40e_veb *veb;
13870 	struct i40e_pf *pf = vsi->back;
13871 
13872 	/* Uplink is not a bridge so default to VEB */
13873 	if (vsi->veb_idx >= I40E_MAX_VEB)
13874 		return 1;
13875 
13876 	veb = pf->veb[vsi->veb_idx];
13877 	if (!veb) {
13878 		dev_info(&pf->pdev->dev,
13879 			 "There is no veb associated with the bridge\n");
13880 		return -ENOENT;
13881 	}
13882 
13883 	/* Uplink is a bridge in VEPA mode */
13884 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13885 		return 0;
13886 	} else {
13887 		/* Uplink is a bridge in VEB mode */
13888 		return 1;
13889 	}
13890 
13891 	/* VEPA is now default bridge, so return 0 */
13892 	return 0;
13893 }
13894 
13895 /**
13896  * i40e_add_vsi - Add a VSI to the switch
13897  * @vsi: the VSI being configured
13898  *
13899  * This initializes a VSI context depending on the VSI type to be added and
13900  * passes it down to the add_vsi aq command.
13901  **/
13902 static int i40e_add_vsi(struct i40e_vsi *vsi)
13903 {
13904 	int ret = -ENODEV;
13905 	struct i40e_pf *pf = vsi->back;
13906 	struct i40e_hw *hw = &pf->hw;
13907 	struct i40e_vsi_context ctxt;
13908 	struct i40e_mac_filter *f;
13909 	struct hlist_node *h;
13910 	int bkt;
13911 
13912 	u8 enabled_tc = 0x1; /* TC0 enabled */
13913 	int f_count = 0;
13914 
13915 	memset(&ctxt, 0, sizeof(ctxt));
13916 	switch (vsi->type) {
13917 	case I40E_VSI_MAIN:
13918 		/* The PF's main VSI is already setup as part of the
13919 		 * device initialization, so we'll not bother with
13920 		 * the add_vsi call, but we will retrieve the current
13921 		 * VSI context.
13922 		 */
13923 		ctxt.seid = pf->main_vsi_seid;
13924 		ctxt.pf_num = pf->hw.pf_id;
13925 		ctxt.vf_num = 0;
13926 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13927 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13928 		if (ret) {
13929 			dev_info(&pf->pdev->dev,
13930 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13931 				 ERR_PTR(ret),
13932 				 i40e_aq_str(&pf->hw,
13933 					     pf->hw.aq.asq_last_status));
13934 			return -ENOENT;
13935 		}
13936 		vsi->info = ctxt.info;
13937 		vsi->info.valid_sections = 0;
13938 
13939 		vsi->seid = ctxt.seid;
13940 		vsi->id = ctxt.vsi_number;
13941 
13942 		enabled_tc = i40e_pf_get_tc_map(pf);
13943 
13944 		/* Source pruning is enabled by default, so the flag is
13945 		 * negative logic - if it's set, we need to fiddle with
13946 		 * the VSI to disable source pruning.
13947 		 */
13948 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13949 			memset(&ctxt, 0, sizeof(ctxt));
13950 			ctxt.seid = pf->main_vsi_seid;
13951 			ctxt.pf_num = pf->hw.pf_id;
13952 			ctxt.vf_num = 0;
13953 			ctxt.info.valid_sections |=
13954 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13955 			ctxt.info.switch_id =
13956 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13957 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13958 			if (ret) {
13959 				dev_info(&pf->pdev->dev,
13960 					 "update vsi failed, err %d aq_err %s\n",
13961 					 ret,
13962 					 i40e_aq_str(&pf->hw,
13963 						     pf->hw.aq.asq_last_status));
13964 				ret = -ENOENT;
13965 				goto err;
13966 			}
13967 		}
13968 
13969 		/* MFP mode setup queue map and update VSI */
13970 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13971 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13972 			memset(&ctxt, 0, sizeof(ctxt));
13973 			ctxt.seid = pf->main_vsi_seid;
13974 			ctxt.pf_num = pf->hw.pf_id;
13975 			ctxt.vf_num = 0;
13976 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13977 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13978 			if (ret) {
13979 				dev_info(&pf->pdev->dev,
13980 					 "update vsi failed, err %pe aq_err %s\n",
13981 					 ERR_PTR(ret),
13982 					 i40e_aq_str(&pf->hw,
13983 						    pf->hw.aq.asq_last_status));
13984 				ret = -ENOENT;
13985 				goto err;
13986 			}
13987 			/* update the local VSI info queue map */
13988 			i40e_vsi_update_queue_map(vsi, &ctxt);
13989 			vsi->info.valid_sections = 0;
13990 		} else {
13991 			/* Default/Main VSI is only enabled for TC0
13992 			 * reconfigure it to enable all TCs that are
13993 			 * available on the port in SFP mode.
13994 			 * For MFP case the iSCSI PF would use this
13995 			 * flow to enable LAN+iSCSI TC.
13996 			 */
13997 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13998 			if (ret) {
13999 				/* Single TC condition is not fatal,
14000 				 * message and continue
14001 				 */
14002 				dev_info(&pf->pdev->dev,
14003 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14004 					 enabled_tc,
14005 					 ERR_PTR(ret),
14006 					 i40e_aq_str(&pf->hw,
14007 						    pf->hw.aq.asq_last_status));
14008 			}
14009 		}
14010 		break;
14011 
14012 	case I40E_VSI_FDIR:
14013 		ctxt.pf_num = hw->pf_id;
14014 		ctxt.vf_num = 0;
14015 		ctxt.uplink_seid = vsi->uplink_seid;
14016 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14017 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14018 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
14019 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14020 			ctxt.info.valid_sections |=
14021 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14022 			ctxt.info.switch_id =
14023 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14024 		}
14025 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14026 		break;
14027 
14028 	case I40E_VSI_VMDQ2:
14029 		ctxt.pf_num = hw->pf_id;
14030 		ctxt.vf_num = 0;
14031 		ctxt.uplink_seid = vsi->uplink_seid;
14032 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14033 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14034 
14035 		/* This VSI is connected to VEB so the switch_id
14036 		 * should be set to zero by default.
14037 		 */
14038 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14039 			ctxt.info.valid_sections |=
14040 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14041 			ctxt.info.switch_id =
14042 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14043 		}
14044 
14045 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14046 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14047 		break;
14048 
14049 	case I40E_VSI_SRIOV:
14050 		ctxt.pf_num = hw->pf_id;
14051 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14052 		ctxt.uplink_seid = vsi->uplink_seid;
14053 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14054 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14055 
14056 		/* This VSI is connected to VEB so the switch_id
14057 		 * should be set to zero by default.
14058 		 */
14059 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14060 			ctxt.info.valid_sections |=
14061 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14062 			ctxt.info.switch_id =
14063 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14064 		}
14065 
14066 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14067 			ctxt.info.valid_sections |=
14068 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14069 			ctxt.info.queueing_opt_flags |=
14070 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14071 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14072 		}
14073 
14074 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14075 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14076 		if (pf->vf[vsi->vf_id].spoofchk) {
14077 			ctxt.info.valid_sections |=
14078 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14079 			ctxt.info.sec_flags |=
14080 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14081 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14082 		}
14083 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14084 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14085 		break;
14086 
14087 	case I40E_VSI_IWARP:
14088 		/* send down message to iWARP */
14089 		break;
14090 
14091 	default:
14092 		return -ENODEV;
14093 	}
14094 
14095 	if (vsi->type != I40E_VSI_MAIN) {
14096 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14097 		if (ret) {
14098 			dev_info(&vsi->back->pdev->dev,
14099 				 "add vsi failed, err %pe aq_err %s\n",
14100 				 ERR_PTR(ret),
14101 				 i40e_aq_str(&pf->hw,
14102 					     pf->hw.aq.asq_last_status));
14103 			ret = -ENOENT;
14104 			goto err;
14105 		}
14106 		vsi->info = ctxt.info;
14107 		vsi->info.valid_sections = 0;
14108 		vsi->seid = ctxt.seid;
14109 		vsi->id = ctxt.vsi_number;
14110 	}
14111 
14112 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14113 	vsi->active_filters = 0;
14114 	/* If macvlan filters already exist, force them to get loaded */
14115 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14116 		f->state = I40E_FILTER_NEW;
14117 		f_count++;
14118 	}
14119 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14120 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14121 
14122 	if (f_count) {
14123 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14124 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14125 	}
14126 
14127 	/* Update VSI BW information */
14128 	ret = i40e_vsi_get_bw_info(vsi);
14129 	if (ret) {
14130 		dev_info(&pf->pdev->dev,
14131 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14132 			 ERR_PTR(ret),
14133 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14134 		/* VSI is already added so not tearing that up */
14135 		ret = 0;
14136 	}
14137 
14138 err:
14139 	return ret;
14140 }
14141 
14142 /**
14143  * i40e_vsi_release - Delete a VSI and free its resources
14144  * @vsi: the VSI being removed
14145  *
14146  * Returns 0 on success or < 0 on error
14147  **/
14148 int i40e_vsi_release(struct i40e_vsi *vsi)
14149 {
14150 	struct i40e_mac_filter *f;
14151 	struct hlist_node *h;
14152 	struct i40e_veb *veb = NULL;
14153 	struct i40e_pf *pf;
14154 	u16 uplink_seid;
14155 	int i, n, bkt;
14156 
14157 	pf = vsi->back;
14158 
14159 	/* release of a VEB-owner or last VSI is not allowed */
14160 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14161 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14162 			 vsi->seid, vsi->uplink_seid);
14163 		return -ENODEV;
14164 	}
14165 	if (vsi == pf->vsi[pf->lan_vsi] &&
14166 	    !test_bit(__I40E_DOWN, pf->state)) {
14167 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14168 		return -ENODEV;
14169 	}
14170 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14171 	uplink_seid = vsi->uplink_seid;
14172 
14173 	if (vsi->type != I40E_VSI_SRIOV) {
14174 		if (vsi->netdev_registered) {
14175 			vsi->netdev_registered = false;
14176 			if (vsi->netdev) {
14177 				/* results in a call to i40e_close() */
14178 				unregister_netdev(vsi->netdev);
14179 			}
14180 		} else {
14181 			i40e_vsi_close(vsi);
14182 		}
14183 		i40e_vsi_disable_irq(vsi);
14184 	}
14185 
14186 	if (vsi->type == I40E_VSI_MAIN)
14187 		i40e_devlink_destroy_port(pf);
14188 
14189 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14190 
14191 	/* clear the sync flag on all filters */
14192 	if (vsi->netdev) {
14193 		__dev_uc_unsync(vsi->netdev, NULL);
14194 		__dev_mc_unsync(vsi->netdev, NULL);
14195 	}
14196 
14197 	/* make sure any remaining filters are marked for deletion */
14198 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14199 		__i40e_del_filter(vsi, f);
14200 
14201 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14202 
14203 	i40e_sync_vsi_filters(vsi);
14204 
14205 	i40e_vsi_delete(vsi);
14206 	i40e_vsi_free_q_vectors(vsi);
14207 	if (vsi->netdev) {
14208 		free_netdev(vsi->netdev);
14209 		vsi->netdev = NULL;
14210 	}
14211 	i40e_vsi_clear_rings(vsi);
14212 	i40e_vsi_clear(vsi);
14213 
14214 	/* If this was the last thing on the VEB, except for the
14215 	 * controlling VSI, remove the VEB, which puts the controlling
14216 	 * VSI onto the next level down in the switch.
14217 	 *
14218 	 * Well, okay, there's one more exception here: don't remove
14219 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14220 	 * from up the network stack.
14221 	 */
14222 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14223 		if (pf->vsi[i] &&
14224 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14225 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14226 			n++;      /* count the VSIs */
14227 		}
14228 	}
14229 	for (i = 0; i < I40E_MAX_VEB; i++) {
14230 		if (!pf->veb[i])
14231 			continue;
14232 		if (pf->veb[i]->uplink_seid == uplink_seid)
14233 			n++;     /* count the VEBs */
14234 		if (pf->veb[i]->seid == uplink_seid)
14235 			veb = pf->veb[i];
14236 	}
14237 	if (n == 0 && veb && veb->uplink_seid != 0)
14238 		i40e_veb_release(veb);
14239 
14240 	return 0;
14241 }
14242 
14243 /**
14244  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14245  * @vsi: ptr to the VSI
14246  *
14247  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14248  * corresponding SW VSI structure and initializes num_queue_pairs for the
14249  * newly allocated VSI.
14250  *
14251  * Returns 0 on success or negative on failure
14252  **/
14253 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14254 {
14255 	int ret = -ENOENT;
14256 	struct i40e_pf *pf = vsi->back;
14257 
14258 	if (vsi->q_vectors[0]) {
14259 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14260 			 vsi->seid);
14261 		return -EEXIST;
14262 	}
14263 
14264 	if (vsi->base_vector) {
14265 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14266 			 vsi->seid, vsi->base_vector);
14267 		return -EEXIST;
14268 	}
14269 
14270 	ret = i40e_vsi_alloc_q_vectors(vsi);
14271 	if (ret) {
14272 		dev_info(&pf->pdev->dev,
14273 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14274 			 vsi->num_q_vectors, vsi->seid, ret);
14275 		vsi->num_q_vectors = 0;
14276 		goto vector_setup_out;
14277 	}
14278 
14279 	/* In Legacy mode, we do not have to get any other vector since we
14280 	 * piggyback on the misc/ICR0 for queue interrupts.
14281 	*/
14282 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14283 		return ret;
14284 	if (vsi->num_q_vectors)
14285 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14286 						 vsi->num_q_vectors, vsi->idx);
14287 	if (vsi->base_vector < 0) {
14288 		dev_info(&pf->pdev->dev,
14289 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14290 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14291 		i40e_vsi_free_q_vectors(vsi);
14292 		ret = -ENOENT;
14293 		goto vector_setup_out;
14294 	}
14295 
14296 vector_setup_out:
14297 	return ret;
14298 }
14299 
14300 /**
14301  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14302  * @vsi: pointer to the vsi.
14303  *
14304  * This re-allocates a vsi's queue resources.
14305  *
14306  * Returns pointer to the successfully allocated and configured VSI sw struct
14307  * on success, otherwise returns NULL on failure.
14308  **/
14309 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14310 {
14311 	u16 alloc_queue_pairs;
14312 	struct i40e_pf *pf;
14313 	u8 enabled_tc;
14314 	int ret;
14315 
14316 	if (!vsi)
14317 		return NULL;
14318 
14319 	pf = vsi->back;
14320 
14321 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14322 	i40e_vsi_clear_rings(vsi);
14323 
14324 	i40e_vsi_free_arrays(vsi, false);
14325 	i40e_set_num_rings_in_vsi(vsi);
14326 	ret = i40e_vsi_alloc_arrays(vsi, false);
14327 	if (ret)
14328 		goto err_vsi;
14329 
14330 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14331 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14332 
14333 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14334 	if (ret < 0) {
14335 		dev_info(&pf->pdev->dev,
14336 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14337 			 alloc_queue_pairs, vsi->seid, ret);
14338 		goto err_vsi;
14339 	}
14340 	vsi->base_queue = ret;
14341 
14342 	/* Update the FW view of the VSI. Force a reset of TC and queue
14343 	 * layout configurations.
14344 	 */
14345 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14346 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14347 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14348 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14349 	if (vsi->type == I40E_VSI_MAIN)
14350 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14351 
14352 	/* assign it some queues */
14353 	ret = i40e_alloc_rings(vsi);
14354 	if (ret)
14355 		goto err_rings;
14356 
14357 	/* map all of the rings to the q_vectors */
14358 	i40e_vsi_map_rings_to_vectors(vsi);
14359 	return vsi;
14360 
14361 err_rings:
14362 	i40e_vsi_free_q_vectors(vsi);
14363 	if (vsi->netdev_registered) {
14364 		vsi->netdev_registered = false;
14365 		unregister_netdev(vsi->netdev);
14366 		free_netdev(vsi->netdev);
14367 		vsi->netdev = NULL;
14368 	}
14369 	if (vsi->type == I40E_VSI_MAIN)
14370 		i40e_devlink_destroy_port(pf);
14371 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14372 err_vsi:
14373 	i40e_vsi_clear(vsi);
14374 	return NULL;
14375 }
14376 
14377 /**
14378  * i40e_vsi_setup - Set up a VSI by a given type
14379  * @pf: board private structure
14380  * @type: VSI type
14381  * @uplink_seid: the switch element to link to
14382  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14383  *
14384  * This allocates the sw VSI structure and its queue resources, then add a VSI
14385  * to the identified VEB.
14386  *
14387  * Returns pointer to the successfully allocated and configure VSI sw struct on
14388  * success, otherwise returns NULL on failure.
14389  **/
14390 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14391 				u16 uplink_seid, u32 param1)
14392 {
14393 	struct i40e_vsi *vsi = NULL;
14394 	struct i40e_veb *veb = NULL;
14395 	u16 alloc_queue_pairs;
14396 	int ret, i;
14397 	int v_idx;
14398 
14399 	/* The requested uplink_seid must be either
14400 	 *     - the PF's port seid
14401 	 *              no VEB is needed because this is the PF
14402 	 *              or this is a Flow Director special case VSI
14403 	 *     - seid of an existing VEB
14404 	 *     - seid of a VSI that owns an existing VEB
14405 	 *     - seid of a VSI that doesn't own a VEB
14406 	 *              a new VEB is created and the VSI becomes the owner
14407 	 *     - seid of the PF VSI, which is what creates the first VEB
14408 	 *              this is a special case of the previous
14409 	 *
14410 	 * Find which uplink_seid we were given and create a new VEB if needed
14411 	 */
14412 	for (i = 0; i < I40E_MAX_VEB; i++) {
14413 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14414 			veb = pf->veb[i];
14415 			break;
14416 		}
14417 	}
14418 
14419 	if (!veb && uplink_seid != pf->mac_seid) {
14420 
14421 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14422 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14423 				vsi = pf->vsi[i];
14424 				break;
14425 			}
14426 		}
14427 		if (!vsi) {
14428 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14429 				 uplink_seid);
14430 			return NULL;
14431 		}
14432 
14433 		if (vsi->uplink_seid == pf->mac_seid)
14434 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14435 					     vsi->tc_config.enabled_tc);
14436 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14437 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14438 					     vsi->tc_config.enabled_tc);
14439 		if (veb) {
14440 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14441 				dev_info(&vsi->back->pdev->dev,
14442 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14443 				return NULL;
14444 			}
14445 			/* We come up by default in VEPA mode if SRIOV is not
14446 			 * already enabled, in which case we can't force VEPA
14447 			 * mode.
14448 			 */
14449 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14450 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14451 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14452 			}
14453 			i40e_config_bridge_mode(veb);
14454 		}
14455 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14456 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14457 				veb = pf->veb[i];
14458 		}
14459 		if (!veb) {
14460 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14461 			return NULL;
14462 		}
14463 
14464 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14465 		uplink_seid = veb->seid;
14466 	}
14467 
14468 	/* get vsi sw struct */
14469 	v_idx = i40e_vsi_mem_alloc(pf, type);
14470 	if (v_idx < 0)
14471 		goto err_alloc;
14472 	vsi = pf->vsi[v_idx];
14473 	if (!vsi)
14474 		goto err_alloc;
14475 	vsi->type = type;
14476 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14477 
14478 	if (type == I40E_VSI_MAIN)
14479 		pf->lan_vsi = v_idx;
14480 	else if (type == I40E_VSI_SRIOV)
14481 		vsi->vf_id = param1;
14482 	/* assign it some queues */
14483 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14484 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14485 
14486 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14487 	if (ret < 0) {
14488 		dev_info(&pf->pdev->dev,
14489 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14490 			 alloc_queue_pairs, vsi->seid, ret);
14491 		goto err_vsi;
14492 	}
14493 	vsi->base_queue = ret;
14494 
14495 	/* get a VSI from the hardware */
14496 	vsi->uplink_seid = uplink_seid;
14497 	ret = i40e_add_vsi(vsi);
14498 	if (ret)
14499 		goto err_vsi;
14500 
14501 	switch (vsi->type) {
14502 	/* setup the netdev if needed */
14503 	case I40E_VSI_MAIN:
14504 	case I40E_VSI_VMDQ2:
14505 		ret = i40e_config_netdev(vsi);
14506 		if (ret)
14507 			goto err_netdev;
14508 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14509 		if (ret)
14510 			goto err_netdev;
14511 		if (vsi->type == I40E_VSI_MAIN) {
14512 			ret = i40e_devlink_create_port(pf);
14513 			if (ret)
14514 				goto err_netdev;
14515 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14516 		}
14517 		ret = register_netdev(vsi->netdev);
14518 		if (ret)
14519 			goto err_dl_port;
14520 		vsi->netdev_registered = true;
14521 		netif_carrier_off(vsi->netdev);
14522 #ifdef CONFIG_I40E_DCB
14523 		/* Setup DCB netlink interface */
14524 		i40e_dcbnl_setup(vsi);
14525 #endif /* CONFIG_I40E_DCB */
14526 		fallthrough;
14527 	case I40E_VSI_FDIR:
14528 		/* set up vectors and rings if needed */
14529 		ret = i40e_vsi_setup_vectors(vsi);
14530 		if (ret)
14531 			goto err_msix;
14532 
14533 		ret = i40e_alloc_rings(vsi);
14534 		if (ret)
14535 			goto err_rings;
14536 
14537 		/* map all of the rings to the q_vectors */
14538 		i40e_vsi_map_rings_to_vectors(vsi);
14539 
14540 		i40e_vsi_reset_stats(vsi);
14541 		break;
14542 	default:
14543 		/* no netdev or rings for the other VSI types */
14544 		break;
14545 	}
14546 
14547 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14548 	    vsi->type == I40E_VSI_VMDQ2) {
14549 		ret = i40e_vsi_config_rss(vsi);
14550 		if (ret)
14551 			goto err_config;
14552 	}
14553 	return vsi;
14554 
14555 err_config:
14556 	i40e_vsi_clear_rings(vsi);
14557 err_rings:
14558 	i40e_vsi_free_q_vectors(vsi);
14559 err_msix:
14560 	if (vsi->netdev_registered) {
14561 		vsi->netdev_registered = false;
14562 		unregister_netdev(vsi->netdev);
14563 		free_netdev(vsi->netdev);
14564 		vsi->netdev = NULL;
14565 	}
14566 err_dl_port:
14567 	if (vsi->type == I40E_VSI_MAIN)
14568 		i40e_devlink_destroy_port(pf);
14569 err_netdev:
14570 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14571 err_vsi:
14572 	i40e_vsi_clear(vsi);
14573 err_alloc:
14574 	return NULL;
14575 }
14576 
14577 /**
14578  * i40e_veb_get_bw_info - Query VEB BW information
14579  * @veb: the veb to query
14580  *
14581  * Query the Tx scheduler BW configuration data for given VEB
14582  **/
14583 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14584 {
14585 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14586 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14587 	struct i40e_pf *pf = veb->pf;
14588 	struct i40e_hw *hw = &pf->hw;
14589 	u32 tc_bw_max;
14590 	int ret = 0;
14591 	int i;
14592 
14593 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14594 						  &bw_data, NULL);
14595 	if (ret) {
14596 		dev_info(&pf->pdev->dev,
14597 			 "query veb bw config failed, err %pe aq_err %s\n",
14598 			 ERR_PTR(ret),
14599 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14600 		goto out;
14601 	}
14602 
14603 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14604 						   &ets_data, NULL);
14605 	if (ret) {
14606 		dev_info(&pf->pdev->dev,
14607 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14608 			 ERR_PTR(ret),
14609 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14610 		goto out;
14611 	}
14612 
14613 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14614 	veb->bw_max_quanta = ets_data.tc_bw_max;
14615 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14616 	veb->enabled_tc = ets_data.tc_valid_bits;
14617 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14618 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14619 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14620 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14621 		veb->bw_tc_limit_credits[i] =
14622 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14623 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14624 	}
14625 
14626 out:
14627 	return ret;
14628 }
14629 
14630 /**
14631  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14632  * @pf: board private structure
14633  *
14634  * On error: returns error code (negative)
14635  * On success: returns vsi index in PF (positive)
14636  **/
14637 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14638 {
14639 	int ret = -ENOENT;
14640 	struct i40e_veb *veb;
14641 	int i;
14642 
14643 	/* Need to protect the allocation of switch elements at the PF level */
14644 	mutex_lock(&pf->switch_mutex);
14645 
14646 	/* VEB list may be fragmented if VEB creation/destruction has
14647 	 * been happening.  We can afford to do a quick scan to look
14648 	 * for any free slots in the list.
14649 	 *
14650 	 * find next empty veb slot, looping back around if necessary
14651 	 */
14652 	i = 0;
14653 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14654 		i++;
14655 	if (i >= I40E_MAX_VEB) {
14656 		ret = -ENOMEM;
14657 		goto err_alloc_veb;  /* out of VEB slots! */
14658 	}
14659 
14660 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14661 	if (!veb) {
14662 		ret = -ENOMEM;
14663 		goto err_alloc_veb;
14664 	}
14665 	veb->pf = pf;
14666 	veb->idx = i;
14667 	veb->enabled_tc = 1;
14668 
14669 	pf->veb[i] = veb;
14670 	ret = i;
14671 err_alloc_veb:
14672 	mutex_unlock(&pf->switch_mutex);
14673 	return ret;
14674 }
14675 
14676 /**
14677  * i40e_switch_branch_release - Delete a branch of the switch tree
14678  * @branch: where to start deleting
14679  *
14680  * This uses recursion to find the tips of the branch to be
14681  * removed, deleting until we get back to and can delete this VEB.
14682  **/
14683 static void i40e_switch_branch_release(struct i40e_veb *branch)
14684 {
14685 	struct i40e_pf *pf = branch->pf;
14686 	u16 branch_seid = branch->seid;
14687 	u16 veb_idx = branch->idx;
14688 	int i;
14689 
14690 	/* release any VEBs on this VEB - RECURSION */
14691 	for (i = 0; i < I40E_MAX_VEB; i++) {
14692 		if (!pf->veb[i])
14693 			continue;
14694 		if (pf->veb[i]->uplink_seid == branch->seid)
14695 			i40e_switch_branch_release(pf->veb[i]);
14696 	}
14697 
14698 	/* Release the VSIs on this VEB, but not the owner VSI.
14699 	 *
14700 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14701 	 *       the VEB itself, so don't use (*branch) after this loop.
14702 	 */
14703 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14704 		if (!pf->vsi[i])
14705 			continue;
14706 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14707 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14708 			i40e_vsi_release(pf->vsi[i]);
14709 		}
14710 	}
14711 
14712 	/* There's one corner case where the VEB might not have been
14713 	 * removed, so double check it here and remove it if needed.
14714 	 * This case happens if the veb was created from the debugfs
14715 	 * commands and no VSIs were added to it.
14716 	 */
14717 	if (pf->veb[veb_idx])
14718 		i40e_veb_release(pf->veb[veb_idx]);
14719 }
14720 
14721 /**
14722  * i40e_veb_clear - remove veb struct
14723  * @veb: the veb to remove
14724  **/
14725 static void i40e_veb_clear(struct i40e_veb *veb)
14726 {
14727 	if (!veb)
14728 		return;
14729 
14730 	if (veb->pf) {
14731 		struct i40e_pf *pf = veb->pf;
14732 
14733 		mutex_lock(&pf->switch_mutex);
14734 		if (pf->veb[veb->idx] == veb)
14735 			pf->veb[veb->idx] = NULL;
14736 		mutex_unlock(&pf->switch_mutex);
14737 	}
14738 
14739 	kfree(veb);
14740 }
14741 
14742 /**
14743  * i40e_veb_release - Delete a VEB and free its resources
14744  * @veb: the VEB being removed
14745  **/
14746 void i40e_veb_release(struct i40e_veb *veb)
14747 {
14748 	struct i40e_vsi *vsi = NULL;
14749 	struct i40e_pf *pf;
14750 	int i, n = 0;
14751 
14752 	pf = veb->pf;
14753 
14754 	/* find the remaining VSI and check for extras */
14755 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14756 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14757 			n++;
14758 			vsi = pf->vsi[i];
14759 		}
14760 	}
14761 	if (n != 1) {
14762 		dev_info(&pf->pdev->dev,
14763 			 "can't remove VEB %d with %d VSIs left\n",
14764 			 veb->seid, n);
14765 		return;
14766 	}
14767 
14768 	/* move the remaining VSI to uplink veb */
14769 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14770 	if (veb->uplink_seid) {
14771 		vsi->uplink_seid = veb->uplink_seid;
14772 		if (veb->uplink_seid == pf->mac_seid)
14773 			vsi->veb_idx = I40E_NO_VEB;
14774 		else
14775 			vsi->veb_idx = veb->veb_idx;
14776 	} else {
14777 		/* floating VEB */
14778 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14779 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14780 	}
14781 
14782 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14783 	i40e_veb_clear(veb);
14784 }
14785 
14786 /**
14787  * i40e_add_veb - create the VEB in the switch
14788  * @veb: the VEB to be instantiated
14789  * @vsi: the controlling VSI
14790  **/
14791 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14792 {
14793 	struct i40e_pf *pf = veb->pf;
14794 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14795 	int ret;
14796 
14797 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14798 			      veb->enabled_tc, false,
14799 			      &veb->seid, enable_stats, NULL);
14800 
14801 	/* get a VEB from the hardware */
14802 	if (ret) {
14803 		dev_info(&pf->pdev->dev,
14804 			 "couldn't add VEB, err %pe aq_err %s\n",
14805 			 ERR_PTR(ret),
14806 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14807 		return -EPERM;
14808 	}
14809 
14810 	/* get statistics counter */
14811 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14812 					 &veb->stats_idx, NULL, NULL, NULL);
14813 	if (ret) {
14814 		dev_info(&pf->pdev->dev,
14815 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14816 			 ERR_PTR(ret),
14817 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14818 		return -EPERM;
14819 	}
14820 	ret = i40e_veb_get_bw_info(veb);
14821 	if (ret) {
14822 		dev_info(&pf->pdev->dev,
14823 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14824 			 ERR_PTR(ret),
14825 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14826 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14827 		return -ENOENT;
14828 	}
14829 
14830 	vsi->uplink_seid = veb->seid;
14831 	vsi->veb_idx = veb->idx;
14832 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14833 
14834 	return 0;
14835 }
14836 
14837 /**
14838  * i40e_veb_setup - Set up a VEB
14839  * @pf: board private structure
14840  * @flags: VEB setup flags
14841  * @uplink_seid: the switch element to link to
14842  * @vsi_seid: the initial VSI seid
14843  * @enabled_tc: Enabled TC bit-map
14844  *
14845  * This allocates the sw VEB structure and links it into the switch
14846  * It is possible and legal for this to be a duplicate of an already
14847  * existing VEB.  It is also possible for both uplink and vsi seids
14848  * to be zero, in order to create a floating VEB.
14849  *
14850  * Returns pointer to the successfully allocated VEB sw struct on
14851  * success, otherwise returns NULL on failure.
14852  **/
14853 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14854 				u16 uplink_seid, u16 vsi_seid,
14855 				u8 enabled_tc)
14856 {
14857 	struct i40e_veb *veb, *uplink_veb = NULL;
14858 	int vsi_idx, veb_idx;
14859 	int ret;
14860 
14861 	/* if one seid is 0, the other must be 0 to create a floating relay */
14862 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14863 	    (uplink_seid + vsi_seid != 0)) {
14864 		dev_info(&pf->pdev->dev,
14865 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14866 			 uplink_seid, vsi_seid);
14867 		return NULL;
14868 	}
14869 
14870 	/* make sure there is such a vsi and uplink */
14871 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14872 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14873 			break;
14874 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14875 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14876 			 vsi_seid);
14877 		return NULL;
14878 	}
14879 
14880 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14881 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14882 			if (pf->veb[veb_idx] &&
14883 			    pf->veb[veb_idx]->seid == uplink_seid) {
14884 				uplink_veb = pf->veb[veb_idx];
14885 				break;
14886 			}
14887 		}
14888 		if (!uplink_veb) {
14889 			dev_info(&pf->pdev->dev,
14890 				 "uplink seid %d not found\n", uplink_seid);
14891 			return NULL;
14892 		}
14893 	}
14894 
14895 	/* get veb sw struct */
14896 	veb_idx = i40e_veb_mem_alloc(pf);
14897 	if (veb_idx < 0)
14898 		goto err_alloc;
14899 	veb = pf->veb[veb_idx];
14900 	veb->flags = flags;
14901 	veb->uplink_seid = uplink_seid;
14902 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14903 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14904 
14905 	/* create the VEB in the switch */
14906 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14907 	if (ret)
14908 		goto err_veb;
14909 	if (vsi_idx == pf->lan_vsi)
14910 		pf->lan_veb = veb->idx;
14911 
14912 	return veb;
14913 
14914 err_veb:
14915 	i40e_veb_clear(veb);
14916 err_alloc:
14917 	return NULL;
14918 }
14919 
14920 /**
14921  * i40e_setup_pf_switch_element - set PF vars based on switch type
14922  * @pf: board private structure
14923  * @ele: element we are building info from
14924  * @num_reported: total number of elements
14925  * @printconfig: should we print the contents
14926  *
14927  * helper function to assist in extracting a few useful SEID values.
14928  **/
14929 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14930 				struct i40e_aqc_switch_config_element_resp *ele,
14931 				u16 num_reported, bool printconfig)
14932 {
14933 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14934 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14935 	u8 element_type = ele->element_type;
14936 	u16 seid = le16_to_cpu(ele->seid);
14937 
14938 	if (printconfig)
14939 		dev_info(&pf->pdev->dev,
14940 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14941 			 element_type, seid, uplink_seid, downlink_seid);
14942 
14943 	switch (element_type) {
14944 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14945 		pf->mac_seid = seid;
14946 		break;
14947 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14948 		/* Main VEB? */
14949 		if (uplink_seid != pf->mac_seid)
14950 			break;
14951 		if (pf->lan_veb >= I40E_MAX_VEB) {
14952 			int v;
14953 
14954 			/* find existing or else empty VEB */
14955 			for (v = 0; v < I40E_MAX_VEB; v++) {
14956 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14957 					pf->lan_veb = v;
14958 					break;
14959 				}
14960 			}
14961 			if (pf->lan_veb >= I40E_MAX_VEB) {
14962 				v = i40e_veb_mem_alloc(pf);
14963 				if (v < 0)
14964 					break;
14965 				pf->lan_veb = v;
14966 			}
14967 		}
14968 		if (pf->lan_veb >= I40E_MAX_VEB)
14969 			break;
14970 
14971 		pf->veb[pf->lan_veb]->seid = seid;
14972 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14973 		pf->veb[pf->lan_veb]->pf = pf;
14974 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14975 		break;
14976 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14977 		if (num_reported != 1)
14978 			break;
14979 		/* This is immediately after a reset so we can assume this is
14980 		 * the PF's VSI
14981 		 */
14982 		pf->mac_seid = uplink_seid;
14983 		pf->main_vsi_seid = seid;
14984 		if (printconfig)
14985 			dev_info(&pf->pdev->dev,
14986 				 "pf_seid=%d main_vsi_seid=%d\n",
14987 				 downlink_seid, pf->main_vsi_seid);
14988 		break;
14989 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14990 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14991 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14992 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14993 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14994 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14995 		/* ignore these for now */
14996 		break;
14997 	default:
14998 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14999 			 element_type, seid);
15000 		break;
15001 	}
15002 }
15003 
15004 /**
15005  * i40e_fetch_switch_configuration - Get switch config from firmware
15006  * @pf: board private structure
15007  * @printconfig: should we print the contents
15008  *
15009  * Get the current switch configuration from the device and
15010  * extract a few useful SEID values.
15011  **/
15012 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15013 {
15014 	struct i40e_aqc_get_switch_config_resp *sw_config;
15015 	u16 next_seid = 0;
15016 	int ret = 0;
15017 	u8 *aq_buf;
15018 	int i;
15019 
15020 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15021 	if (!aq_buf)
15022 		return -ENOMEM;
15023 
15024 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15025 	do {
15026 		u16 num_reported, num_total;
15027 
15028 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15029 						I40E_AQ_LARGE_BUF,
15030 						&next_seid, NULL);
15031 		if (ret) {
15032 			dev_info(&pf->pdev->dev,
15033 				 "get switch config failed err %d aq_err %s\n",
15034 				 ret,
15035 				 i40e_aq_str(&pf->hw,
15036 					     pf->hw.aq.asq_last_status));
15037 			kfree(aq_buf);
15038 			return -ENOENT;
15039 		}
15040 
15041 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15042 		num_total = le16_to_cpu(sw_config->header.num_total);
15043 
15044 		if (printconfig)
15045 			dev_info(&pf->pdev->dev,
15046 				 "header: %d reported %d total\n",
15047 				 num_reported, num_total);
15048 
15049 		for (i = 0; i < num_reported; i++) {
15050 			struct i40e_aqc_switch_config_element_resp *ele =
15051 				&sw_config->element[i];
15052 
15053 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15054 						     printconfig);
15055 		}
15056 	} while (next_seid != 0);
15057 
15058 	kfree(aq_buf);
15059 	return ret;
15060 }
15061 
15062 /**
15063  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15064  * @pf: board private structure
15065  * @reinit: if the Main VSI needs to re-initialized.
15066  * @lock_acquired: indicates whether or not the lock has been acquired
15067  *
15068  * Returns 0 on success, negative value on failure
15069  **/
15070 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15071 {
15072 	u16 flags = 0;
15073 	int ret;
15074 
15075 	/* find out what's out there already */
15076 	ret = i40e_fetch_switch_configuration(pf, false);
15077 	if (ret) {
15078 		dev_info(&pf->pdev->dev,
15079 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15080 			 ERR_PTR(ret),
15081 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15082 		return ret;
15083 	}
15084 	i40e_pf_reset_stats(pf);
15085 
15086 	/* set the switch config bit for the whole device to
15087 	 * support limited promisc or true promisc
15088 	 * when user requests promisc. The default is limited
15089 	 * promisc.
15090 	*/
15091 
15092 	if ((pf->hw.pf_id == 0) &&
15093 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
15094 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15095 		pf->last_sw_conf_flags = flags;
15096 	}
15097 
15098 	if (pf->hw.pf_id == 0) {
15099 		u16 valid_flags;
15100 
15101 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15102 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15103 						NULL);
15104 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15105 			dev_info(&pf->pdev->dev,
15106 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15107 				 ERR_PTR(ret),
15108 				 i40e_aq_str(&pf->hw,
15109 					     pf->hw.aq.asq_last_status));
15110 			/* not a fatal problem, just keep going */
15111 		}
15112 		pf->last_sw_conf_valid_flags = valid_flags;
15113 	}
15114 
15115 	/* first time setup */
15116 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15117 		struct i40e_vsi *vsi = NULL;
15118 		u16 uplink_seid;
15119 
15120 		/* Set up the PF VSI associated with the PF's main VSI
15121 		 * that is already in the HW switch
15122 		 */
15123 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15124 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15125 		else
15126 			uplink_seid = pf->mac_seid;
15127 		if (pf->lan_vsi == I40E_NO_VSI)
15128 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15129 		else if (reinit)
15130 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15131 		if (!vsi) {
15132 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15133 			i40e_cloud_filter_exit(pf);
15134 			i40e_fdir_teardown(pf);
15135 			return -EAGAIN;
15136 		}
15137 	} else {
15138 		/* force a reset of TC and queue layout configurations */
15139 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15140 
15141 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15142 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15143 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15144 	}
15145 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15146 
15147 	i40e_fdir_sb_setup(pf);
15148 
15149 	/* Setup static PF queue filter control settings */
15150 	ret = i40e_setup_pf_filter_control(pf);
15151 	if (ret) {
15152 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15153 			 ret);
15154 		/* Failure here should not stop continuing other steps */
15155 	}
15156 
15157 	/* enable RSS in the HW, even for only one queue, as the stack can use
15158 	 * the hash
15159 	 */
15160 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15161 		i40e_pf_config_rss(pf);
15162 
15163 	/* fill in link information and enable LSE reporting */
15164 	i40e_link_event(pf);
15165 
15166 	i40e_ptp_init(pf);
15167 
15168 	if (!lock_acquired)
15169 		rtnl_lock();
15170 
15171 	/* repopulate tunnel port filters */
15172 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15173 
15174 	if (!lock_acquired)
15175 		rtnl_unlock();
15176 
15177 	return ret;
15178 }
15179 
15180 /**
15181  * i40e_determine_queue_usage - Work out queue distribution
15182  * @pf: board private structure
15183  **/
15184 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15185 {
15186 	int queues_left;
15187 	int q_max;
15188 
15189 	pf->num_lan_qps = 0;
15190 
15191 	/* Find the max queues to be put into basic use.  We'll always be
15192 	 * using TC0, whether or not DCB is running, and TC0 will get the
15193 	 * big RSS set.
15194 	 */
15195 	queues_left = pf->hw.func_caps.num_tx_qp;
15196 
15197 	if ((queues_left == 1) ||
15198 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15199 		/* one qp for PF, no queues for anything else */
15200 		queues_left = 0;
15201 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15202 
15203 		/* make sure all the fancies are disabled */
15204 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15205 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15206 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15207 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15208 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15209 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15210 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15211 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15212 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15213 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15214 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15215 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15216 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15217 		/* one qp for PF */
15218 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15219 		queues_left -= pf->num_lan_qps;
15220 
15221 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15222 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15223 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15224 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15225 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15226 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15227 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15228 	} else {
15229 		/* Not enough queues for all TCs */
15230 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15231 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15232 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15233 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15234 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15235 		}
15236 
15237 		/* limit lan qps to the smaller of qps, cpus or msix */
15238 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15239 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15240 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15241 		pf->num_lan_qps = q_max;
15242 
15243 		queues_left -= pf->num_lan_qps;
15244 	}
15245 
15246 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15247 		if (queues_left > 1) {
15248 			queues_left -= 1; /* save 1 queue for FD */
15249 		} else {
15250 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15251 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15252 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15253 		}
15254 	}
15255 
15256 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15257 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15258 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15259 					(queues_left / pf->num_vf_qps));
15260 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15261 	}
15262 
15263 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15264 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15265 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15266 					  (queues_left / pf->num_vmdq_qps));
15267 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15268 	}
15269 
15270 	pf->queues_left = queues_left;
15271 	dev_dbg(&pf->pdev->dev,
15272 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15273 		pf->hw.func_caps.num_tx_qp,
15274 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15275 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15276 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15277 		queues_left);
15278 }
15279 
15280 /**
15281  * i40e_setup_pf_filter_control - Setup PF static filter control
15282  * @pf: PF to be setup
15283  *
15284  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15285  * settings. If PE/FCoE are enabled then it will also set the per PF
15286  * based filter sizes required for them. It also enables Flow director,
15287  * ethertype and macvlan type filter settings for the pf.
15288  *
15289  * Returns 0 on success, negative on failure
15290  **/
15291 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15292 {
15293 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15294 
15295 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15296 
15297 	/* Flow Director is enabled */
15298 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15299 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15300 		settings->enable_fdir = true;
15301 
15302 	/* Ethtype and MACVLAN filters enabled for PF */
15303 	settings->enable_ethtype = true;
15304 	settings->enable_macvlan = true;
15305 
15306 	if (i40e_set_filter_control(&pf->hw, settings))
15307 		return -ENOENT;
15308 
15309 	return 0;
15310 }
15311 
15312 #define INFO_STRING_LEN 255
15313 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15314 static void i40e_print_features(struct i40e_pf *pf)
15315 {
15316 	struct i40e_hw *hw = &pf->hw;
15317 	char *buf;
15318 	int i;
15319 
15320 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15321 	if (!buf)
15322 		return;
15323 
15324 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15325 #ifdef CONFIG_PCI_IOV
15326 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15327 #endif
15328 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15329 		      pf->hw.func_caps.num_vsis,
15330 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15331 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15332 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15333 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15334 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15335 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15336 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15337 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15338 	}
15339 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15340 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15341 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15342 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15343 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15344 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15345 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15346 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15347 	else
15348 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15349 
15350 	dev_info(&pf->pdev->dev, "%s\n", buf);
15351 	kfree(buf);
15352 	WARN_ON(i > INFO_STRING_LEN);
15353 }
15354 
15355 /**
15356  * i40e_get_platform_mac_addr - get platform-specific MAC address
15357  * @pdev: PCI device information struct
15358  * @pf: board private structure
15359  *
15360  * Look up the MAC address for the device. First we'll try
15361  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15362  * specific fallback. Otherwise, we'll default to the stored value in
15363  * firmware.
15364  **/
15365 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15366 {
15367 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15368 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15369 }
15370 
15371 /**
15372  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15373  * @fec_cfg: FEC option to set in flags
15374  * @flags: ptr to flags in which we set FEC option
15375  **/
15376 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15377 {
15378 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15379 		set_bit(I40E_FLAG_RS_FEC, flags);
15380 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15381 	}
15382 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15383 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15384 		set_bit(I40E_FLAG_RS_FEC, flags);
15385 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15386 	}
15387 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15388 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15389 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15390 		clear_bit(I40E_FLAG_RS_FEC, flags);
15391 	}
15392 	if (fec_cfg == 0) {
15393 		clear_bit(I40E_FLAG_RS_FEC, flags);
15394 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15395 	}
15396 }
15397 
15398 /**
15399  * i40e_check_recovery_mode - check if we are running transition firmware
15400  * @pf: board private structure
15401  *
15402  * Check registers indicating the firmware runs in recovery mode. Sets the
15403  * appropriate driver state.
15404  *
15405  * Returns true if the recovery mode was detected, false otherwise
15406  **/
15407 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15408 {
15409 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15410 
15411 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15412 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15413 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15414 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15415 
15416 		return true;
15417 	}
15418 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15419 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15420 
15421 	return false;
15422 }
15423 
15424 /**
15425  * i40e_pf_loop_reset - perform reset in a loop.
15426  * @pf: board private structure
15427  *
15428  * This function is useful when a NIC is about to enter recovery mode.
15429  * When a NIC's internal data structures are corrupted the NIC's
15430  * firmware is going to enter recovery mode.
15431  * Right after a POR it takes about 7 minutes for firmware to enter
15432  * recovery mode. Until that time a NIC is in some kind of intermediate
15433  * state. After that time period the NIC almost surely enters
15434  * recovery mode. The only way for a driver to detect intermediate
15435  * state is to issue a series of pf-resets and check a return value.
15436  * If a PF reset returns success then the firmware could be in recovery
15437  * mode so the caller of this code needs to check for recovery mode
15438  * if this function returns success. There is a little chance that
15439  * firmware will hang in intermediate state forever.
15440  * Since waiting 7 minutes is quite a lot of time this function waits
15441  * 10 seconds and then gives up by returning an error.
15442  *
15443  * Return 0 on success, negative on failure.
15444  **/
15445 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15446 {
15447 	/* wait max 10 seconds for PF reset to succeed */
15448 	const unsigned long time_end = jiffies + 10 * HZ;
15449 	struct i40e_hw *hw = &pf->hw;
15450 	int ret;
15451 
15452 	ret = i40e_pf_reset(hw);
15453 	while (ret != 0 && time_before(jiffies, time_end)) {
15454 		usleep_range(10000, 20000);
15455 		ret = i40e_pf_reset(hw);
15456 	}
15457 
15458 	if (ret == 0)
15459 		pf->pfr_count++;
15460 	else
15461 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15462 
15463 	return ret;
15464 }
15465 
15466 /**
15467  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15468  * @pf: board private structure
15469  *
15470  * Check FW registers to determine if FW issued unexpected EMP Reset.
15471  * Every time when unexpected EMP Reset occurs the FW increments
15472  * a counter of unexpected EMP Resets. When the counter reaches 10
15473  * the FW should enter the Recovery mode
15474  *
15475  * Returns true if FW issued unexpected EMP Reset
15476  **/
15477 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15478 {
15479 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15480 			   I40E_GL_FWSTS_FWS1B_MASK;
15481 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15482 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15483 }
15484 
15485 /**
15486  * i40e_handle_resets - handle EMP resets and PF resets
15487  * @pf: board private structure
15488  *
15489  * Handle both EMP resets and PF resets and conclude whether there are
15490  * any issues regarding these resets. If there are any issues then
15491  * generate log entry.
15492  *
15493  * Return 0 if NIC is healthy or negative value when there are issues
15494  * with resets
15495  **/
15496 static int i40e_handle_resets(struct i40e_pf *pf)
15497 {
15498 	const int pfr = i40e_pf_loop_reset(pf);
15499 	const bool is_empr = i40e_check_fw_empr(pf);
15500 
15501 	if (is_empr || pfr != 0)
15502 		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");
15503 
15504 	return is_empr ? -EIO : pfr;
15505 }
15506 
15507 /**
15508  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15509  * @pf: board private structure
15510  * @hw: ptr to the hardware info
15511  *
15512  * This function does a minimal setup of all subsystems needed for running
15513  * recovery mode.
15514  *
15515  * Returns 0 on success, negative on failure
15516  **/
15517 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15518 {
15519 	struct i40e_vsi *vsi;
15520 	int err;
15521 	int v_idx;
15522 
15523 	pci_set_drvdata(pf->pdev, pf);
15524 	pci_save_state(pf->pdev);
15525 
15526 	/* set up periodic task facility */
15527 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15528 	pf->service_timer_period = HZ;
15529 
15530 	INIT_WORK(&pf->service_task, i40e_service_task);
15531 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15532 
15533 	err = i40e_init_interrupt_scheme(pf);
15534 	if (err)
15535 		goto err_switch_setup;
15536 
15537 	/* The number of VSIs reported by the FW is the minimum guaranteed
15538 	 * to us; HW supports far more and we share the remaining pool with
15539 	 * the other PFs. We allocate space for more than the guarantee with
15540 	 * the understanding that we might not get them all later.
15541 	 */
15542 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15543 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15544 	else
15545 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15546 
15547 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15548 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15549 			  GFP_KERNEL);
15550 	if (!pf->vsi) {
15551 		err = -ENOMEM;
15552 		goto err_switch_setup;
15553 	}
15554 
15555 	/* We allocate one VSI which is needed as absolute minimum
15556 	 * in order to register the netdev
15557 	 */
15558 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15559 	if (v_idx < 0) {
15560 		err = v_idx;
15561 		goto err_switch_setup;
15562 	}
15563 	pf->lan_vsi = v_idx;
15564 	vsi = pf->vsi[v_idx];
15565 	if (!vsi) {
15566 		err = -EFAULT;
15567 		goto err_switch_setup;
15568 	}
15569 	vsi->alloc_queue_pairs = 1;
15570 	err = i40e_config_netdev(vsi);
15571 	if (err)
15572 		goto err_switch_setup;
15573 	err = register_netdev(vsi->netdev);
15574 	if (err)
15575 		goto err_switch_setup;
15576 	vsi->netdev_registered = true;
15577 	i40e_dbg_pf_init(pf);
15578 
15579 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15580 	if (err)
15581 		goto err_switch_setup;
15582 
15583 	/* tell the firmware that we're starting */
15584 	i40e_send_version(pf);
15585 
15586 	/* since everything's happy, start the service_task timer */
15587 	mod_timer(&pf->service_timer,
15588 		  round_jiffies(jiffies + pf->service_timer_period));
15589 
15590 	return 0;
15591 
15592 err_switch_setup:
15593 	i40e_reset_interrupt_capability(pf);
15594 	timer_shutdown_sync(&pf->service_timer);
15595 	i40e_shutdown_adminq(hw);
15596 	iounmap(hw->hw_addr);
15597 	pci_release_mem_regions(pf->pdev);
15598 	pci_disable_device(pf->pdev);
15599 	i40e_free_pf(pf);
15600 
15601 	return err;
15602 }
15603 
15604 /**
15605  * i40e_set_subsystem_device_id - set subsystem device id
15606  * @hw: pointer to the hardware info
15607  *
15608  * Set PCI subsystem device id either from a pci_dev structure or
15609  * a specific FW register.
15610  **/
15611 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15612 {
15613 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15614 
15615 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15616 		pf->pdev->subsystem_device :
15617 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15618 }
15619 
15620 /**
15621  * i40e_probe - Device initialization routine
15622  * @pdev: PCI device information struct
15623  * @ent: entry in i40e_pci_tbl
15624  *
15625  * i40e_probe initializes a PF identified by a pci_dev structure.
15626  * The OS initialization, configuring of the PF private structure,
15627  * and a hardware reset occur.
15628  *
15629  * Returns 0 on success, negative on failure
15630  **/
15631 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15632 {
15633 	struct i40e_aq_get_phy_abilities_resp abilities;
15634 #ifdef CONFIG_I40E_DCB
15635 	enum i40e_get_fw_lldp_status_resp lldp_status;
15636 #endif /* CONFIG_I40E_DCB */
15637 	struct i40e_pf *pf;
15638 	struct i40e_hw *hw;
15639 	u16 wol_nvm_bits;
15640 	char nvm_ver[32];
15641 	u16 link_status;
15642 #ifdef CONFIG_I40E_DCB
15643 	int status;
15644 #endif /* CONFIG_I40E_DCB */
15645 	int err;
15646 	u32 val;
15647 	u32 i;
15648 
15649 	err = pci_enable_device_mem(pdev);
15650 	if (err)
15651 		return err;
15652 
15653 	/* set up for high or low dma */
15654 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15655 	if (err) {
15656 		dev_err(&pdev->dev,
15657 			"DMA configuration failed: 0x%x\n", err);
15658 		goto err_dma;
15659 	}
15660 
15661 	/* set up pci connections */
15662 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15663 	if (err) {
15664 		dev_info(&pdev->dev,
15665 			 "pci_request_selected_regions failed %d\n", err);
15666 		goto err_pci_reg;
15667 	}
15668 
15669 	pci_set_master(pdev);
15670 
15671 	/* Now that we have a PCI connection, we need to do the
15672 	 * low level device setup.  This is primarily setting up
15673 	 * the Admin Queue structures and then querying for the
15674 	 * device's current profile information.
15675 	 */
15676 	pf = i40e_alloc_pf(&pdev->dev);
15677 	if (!pf) {
15678 		err = -ENOMEM;
15679 		goto err_pf_alloc;
15680 	}
15681 	pf->next_vsi = 0;
15682 	pf->pdev = pdev;
15683 	set_bit(__I40E_DOWN, pf->state);
15684 
15685 	hw = &pf->hw;
15686 
15687 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15688 				I40E_MAX_CSR_SPACE);
15689 	/* We believe that the highest register to read is
15690 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15691 	 * is not less than that before mapping to prevent a
15692 	 * kernel panic.
15693 	 */
15694 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15695 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15696 			pf->ioremap_len);
15697 		err = -ENOMEM;
15698 		goto err_ioremap;
15699 	}
15700 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15701 	if (!hw->hw_addr) {
15702 		err = -EIO;
15703 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15704 			 (unsigned int)pci_resource_start(pdev, 0),
15705 			 pf->ioremap_len, err);
15706 		goto err_ioremap;
15707 	}
15708 	hw->vendor_id = pdev->vendor;
15709 	hw->device_id = pdev->device;
15710 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15711 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15712 	i40e_set_subsystem_device_id(hw);
15713 	hw->bus.device = PCI_SLOT(pdev->devfn);
15714 	hw->bus.func = PCI_FUNC(pdev->devfn);
15715 	hw->bus.bus_id = pdev->bus->number;
15716 
15717 	/* Select something other than the 802.1ad ethertype for the
15718 	 * switch to use internally and drop on ingress.
15719 	 */
15720 	hw->switch_tag = 0xffff;
15721 	hw->first_tag = ETH_P_8021AD;
15722 	hw->second_tag = ETH_P_8021Q;
15723 
15724 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15725 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15726 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15727 
15728 	/* set up the locks for the AQ, do this only once in probe
15729 	 * and destroy them only once in remove
15730 	 */
15731 	mutex_init(&hw->aq.asq_mutex);
15732 	mutex_init(&hw->aq.arq_mutex);
15733 
15734 	pf->msg_enable = netif_msg_init(debug,
15735 					NETIF_MSG_DRV |
15736 					NETIF_MSG_PROBE |
15737 					NETIF_MSG_LINK);
15738 	if (debug < -1)
15739 		pf->hw.debug_mask = debug;
15740 
15741 	/* do a special CORER for clearing PXE mode once at init */
15742 	if (hw->revision_id == 0 &&
15743 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15744 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15745 		i40e_flush(hw);
15746 		msleep(200);
15747 		pf->corer_count++;
15748 
15749 		i40e_clear_pxe_mode(hw);
15750 	}
15751 
15752 	/* Reset here to make sure all is clean and to define PF 'n' */
15753 	i40e_clear_hw(hw);
15754 
15755 	err = i40e_set_mac_type(hw);
15756 	if (err) {
15757 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15758 			 err);
15759 		goto err_pf_reset;
15760 	}
15761 
15762 	err = i40e_handle_resets(pf);
15763 	if (err)
15764 		goto err_pf_reset;
15765 
15766 	i40e_check_recovery_mode(pf);
15767 
15768 	if (is_kdump_kernel()) {
15769 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15770 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15771 	} else {
15772 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15773 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15774 	}
15775 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15776 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15777 
15778 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15779 		 "%s-%s:misc",
15780 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15781 
15782 	err = i40e_init_shared_code(hw);
15783 	if (err) {
15784 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15785 			 err);
15786 		goto err_pf_reset;
15787 	}
15788 
15789 	/* set up a default setting for link flow control */
15790 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15791 
15792 	err = i40e_init_adminq(hw);
15793 	if (err) {
15794 		if (err == -EIO)
15795 			dev_info(&pdev->dev,
15796 				 "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",
15797 				 hw->aq.api_maj_ver,
15798 				 hw->aq.api_min_ver,
15799 				 I40E_FW_API_VERSION_MAJOR,
15800 				 I40E_FW_MINOR_VERSION(hw));
15801 		else
15802 			dev_info(&pdev->dev,
15803 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15804 
15805 		goto err_pf_reset;
15806 	}
15807 	i40e_get_oem_version(hw);
15808 	i40e_get_pba_string(hw);
15809 
15810 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15811 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15812 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15813 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15814 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15815 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15816 		 hw->subsystem_device_id);
15817 
15818 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15819 				  I40E_FW_MINOR_VERSION(hw) + 1))
15820 		dev_dbg(&pdev->dev,
15821 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15822 			 hw->aq.api_maj_ver,
15823 			 hw->aq.api_min_ver,
15824 			 I40E_FW_API_VERSION_MAJOR,
15825 			 I40E_FW_MINOR_VERSION(hw));
15826 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15827 		dev_info(&pdev->dev,
15828 			 "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",
15829 			 hw->aq.api_maj_ver,
15830 			 hw->aq.api_min_ver,
15831 			 I40E_FW_API_VERSION_MAJOR,
15832 			 I40E_FW_MINOR_VERSION(hw));
15833 
15834 	i40e_verify_eeprom(pf);
15835 
15836 	/* Rev 0 hardware was never productized */
15837 	if (hw->revision_id < 1)
15838 		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");
15839 
15840 	i40e_clear_pxe_mode(hw);
15841 
15842 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15843 	if (err)
15844 		goto err_adminq_setup;
15845 
15846 	err = i40e_sw_init(pf);
15847 	if (err) {
15848 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15849 		goto err_sw_init;
15850 	}
15851 
15852 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15853 		return i40e_init_recovery_mode(pf, hw);
15854 
15855 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15856 				hw->func_caps.num_rx_qp, 0, 0);
15857 	if (err) {
15858 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15859 		goto err_init_lan_hmc;
15860 	}
15861 
15862 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15863 	if (err) {
15864 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15865 		err = -ENOENT;
15866 		goto err_configure_lan_hmc;
15867 	}
15868 
15869 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15870 	 * Ignore error return codes because if it was already disabled via
15871 	 * hardware settings this will fail
15872 	 */
15873 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15874 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15875 		i40e_aq_stop_lldp(hw, true, false, NULL);
15876 	}
15877 
15878 	/* allow a platform config to override the HW addr */
15879 	i40e_get_platform_mac_addr(pdev, pf);
15880 
15881 	if (!is_valid_ether_addr(hw->mac.addr)) {
15882 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15883 		err = -EIO;
15884 		goto err_mac_addr;
15885 	}
15886 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15887 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15888 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15889 	if (is_valid_ether_addr(hw->mac.port_addr))
15890 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15891 
15892 	i40e_ptp_alloc_pins(pf);
15893 	pci_set_drvdata(pdev, pf);
15894 	pci_save_state(pdev);
15895 
15896 #ifdef CONFIG_I40E_DCB
15897 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15898 	(!status &&
15899 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15900 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15901 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15902 	dev_info(&pdev->dev,
15903 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15904 			"FW LLDP is disabled\n" :
15905 			"FW LLDP is enabled\n");
15906 
15907 	/* Enable FW to write default DCB config on link-up */
15908 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15909 
15910 	err = i40e_init_pf_dcb(pf);
15911 	if (err) {
15912 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15913 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15914 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15915 		/* Continue without DCB enabled */
15916 	}
15917 #endif /* CONFIG_I40E_DCB */
15918 
15919 	/* set up periodic task facility */
15920 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15921 	pf->service_timer_period = HZ;
15922 
15923 	INIT_WORK(&pf->service_task, i40e_service_task);
15924 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15925 
15926 	/* NVM bit on means WoL disabled for the port */
15927 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15928 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15929 		pf->wol_en = false;
15930 	else
15931 		pf->wol_en = true;
15932 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15933 
15934 	/* set up the main switch operations */
15935 	i40e_determine_queue_usage(pf);
15936 	err = i40e_init_interrupt_scheme(pf);
15937 	if (err)
15938 		goto err_switch_setup;
15939 
15940 	/* Reduce Tx and Rx pairs for kdump
15941 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15942 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15943 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15944 	 */
15945 	if (is_kdump_kernel())
15946 		pf->num_lan_msix = 1;
15947 
15948 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15949 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15950 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15951 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15952 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15953 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15954 						    UDP_TUNNEL_TYPE_GENEVE;
15955 
15956 	/* The number of VSIs reported by the FW is the minimum guaranteed
15957 	 * to us; HW supports far more and we share the remaining pool with
15958 	 * the other PFs. We allocate space for more than the guarantee with
15959 	 * the understanding that we might not get them all later.
15960 	 */
15961 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15962 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15963 	else
15964 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15965 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15966 		dev_warn(&pf->pdev->dev,
15967 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15968 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15969 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15970 	}
15971 
15972 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15973 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15974 			  GFP_KERNEL);
15975 	if (!pf->vsi) {
15976 		err = -ENOMEM;
15977 		goto err_switch_setup;
15978 	}
15979 
15980 #ifdef CONFIG_PCI_IOV
15981 	/* prep for VF support */
15982 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15983 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15984 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15985 		if (pci_num_vf(pdev))
15986 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15987 	}
15988 #endif
15989 	err = i40e_setup_pf_switch(pf, false, false);
15990 	if (err) {
15991 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15992 		goto err_vsis;
15993 	}
15994 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15995 
15996 	/* if FDIR VSI was set up, start it now */
15997 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15998 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15999 			i40e_vsi_open(pf->vsi[i]);
16000 			break;
16001 		}
16002 	}
16003 
16004 	/* The driver only wants link up/down and module qualification
16005 	 * reports from firmware.  Note the negative logic.
16006 	 */
16007 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16008 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16009 					 I40E_AQ_EVENT_MEDIA_NA |
16010 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16011 	if (err)
16012 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16013 			 ERR_PTR(err),
16014 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16015 
16016 	/* Reconfigure hardware for allowing smaller MSS in the case
16017 	 * of TSO, so that we avoid the MDD being fired and causing
16018 	 * a reset in the case of small MSS+TSO.
16019 	 */
16020 	val = rd32(hw, I40E_REG_MSS);
16021 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16022 		val &= ~I40E_REG_MSS_MIN_MASK;
16023 		val |= I40E_64BYTE_MSS;
16024 		wr32(hw, I40E_REG_MSS, val);
16025 	}
16026 
16027 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
16028 		msleep(75);
16029 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16030 		if (err)
16031 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16032 				 ERR_PTR(err),
16033 				 i40e_aq_str(&pf->hw,
16034 					     pf->hw.aq.asq_last_status));
16035 	}
16036 	/* The main driver is (mostly) up and happy. We need to set this state
16037 	 * before setting up the misc vector or we get a race and the vector
16038 	 * ends up disabled forever.
16039 	 */
16040 	clear_bit(__I40E_DOWN, pf->state);
16041 
16042 	/* In case of MSIX we are going to setup the misc vector right here
16043 	 * to handle admin queue events etc. In case of legacy and MSI
16044 	 * the misc functionality and queue processing is combined in
16045 	 * the same vector and that gets setup at open.
16046 	 */
16047 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
16048 		err = i40e_setup_misc_vector(pf);
16049 		if (err) {
16050 			dev_info(&pdev->dev,
16051 				 "setup of misc vector failed: %d\n", err);
16052 			i40e_cloud_filter_exit(pf);
16053 			i40e_fdir_teardown(pf);
16054 			goto err_vsis;
16055 		}
16056 	}
16057 
16058 #ifdef CONFIG_PCI_IOV
16059 	/* prep for VF support */
16060 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
16061 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
16062 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16063 		/* disable link interrupts for VFs */
16064 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16065 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16066 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16067 		i40e_flush(hw);
16068 
16069 		if (pci_num_vf(pdev)) {
16070 			dev_info(&pdev->dev,
16071 				 "Active VFs found, allocating resources.\n");
16072 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16073 			if (err)
16074 				dev_info(&pdev->dev,
16075 					 "Error %d allocating resources for existing VFs\n",
16076 					 err);
16077 		}
16078 	}
16079 #endif /* CONFIG_PCI_IOV */
16080 
16081 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16082 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16083 						      pf->num_iwarp_msix,
16084 						      I40E_IWARP_IRQ_PILE_ID);
16085 		if (pf->iwarp_base_vector < 0) {
16086 			dev_info(&pdev->dev,
16087 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16088 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16089 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
16090 		}
16091 	}
16092 
16093 	i40e_dbg_pf_init(pf);
16094 
16095 	/* tell the firmware that we're starting */
16096 	i40e_send_version(pf);
16097 
16098 	/* since everything's happy, start the service_task timer */
16099 	mod_timer(&pf->service_timer,
16100 		  round_jiffies(jiffies + pf->service_timer_period));
16101 
16102 	/* add this PF to client device list and launch a client service task */
16103 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16104 		err = i40e_lan_add_device(pf);
16105 		if (err)
16106 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16107 				 err);
16108 	}
16109 
16110 #define PCI_SPEED_SIZE 8
16111 #define PCI_WIDTH_SIZE 8
16112 	/* Devices on the IOSF bus do not have this information
16113 	 * and will report PCI Gen 1 x 1 by default so don't bother
16114 	 * checking them.
16115 	 */
16116 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16117 		char speed[PCI_SPEED_SIZE] = "Unknown";
16118 		char width[PCI_WIDTH_SIZE] = "Unknown";
16119 
16120 		/* Get the negotiated link width and speed from PCI config
16121 		 * space
16122 		 */
16123 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16124 					  &link_status);
16125 
16126 		i40e_set_pci_config_data(hw, link_status);
16127 
16128 		switch (hw->bus.speed) {
16129 		case i40e_bus_speed_8000:
16130 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16131 		case i40e_bus_speed_5000:
16132 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16133 		case i40e_bus_speed_2500:
16134 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16135 		default:
16136 			break;
16137 		}
16138 		switch (hw->bus.width) {
16139 		case i40e_bus_width_pcie_x8:
16140 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16141 		case i40e_bus_width_pcie_x4:
16142 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16143 		case i40e_bus_width_pcie_x2:
16144 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16145 		case i40e_bus_width_pcie_x1:
16146 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16147 		default:
16148 			break;
16149 		}
16150 
16151 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16152 			 speed, width);
16153 
16154 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16155 		    hw->bus.speed < i40e_bus_speed_8000) {
16156 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16157 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16158 		}
16159 	}
16160 
16161 	/* get the requested speeds from the fw */
16162 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16163 	if (err)
16164 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16165 			ERR_PTR(err),
16166 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16167 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16168 
16169 	/* set the FEC config due to the board capabilities */
16170 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16171 
16172 	/* get the supported phy types from the fw */
16173 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16174 	if (err)
16175 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16176 			ERR_PTR(err),
16177 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16178 
16179 	/* make sure the MFS hasn't been set lower than the default */
16180 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16181 	val = FIELD_GET(I40E_PRTGL_SAH_MFS_MASK,
16182 			rd32(&pf->hw, I40E_PRTGL_SAH));
16183 	if (val < MAX_FRAME_SIZE_DEFAULT)
16184 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16185 			 pf->hw.port, val);
16186 
16187 	/* Add a filter to drop all Flow control frames from any VSI from being
16188 	 * transmitted. By doing so we stop a malicious VF from sending out
16189 	 * PAUSE or PFC frames and potentially controlling traffic for other
16190 	 * PF/VF VSIs.
16191 	 * The FW can still send Flow control frames if enabled.
16192 	 */
16193 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16194 						       pf->main_vsi_seid);
16195 
16196 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16197 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16198 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16199 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16200 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16201 	/* print a string summarizing features */
16202 	i40e_print_features(pf);
16203 
16204 	i40e_devlink_register(pf);
16205 
16206 	return 0;
16207 
16208 	/* Unwind what we've done if something failed in the setup */
16209 err_vsis:
16210 	set_bit(__I40E_DOWN, pf->state);
16211 	i40e_clear_interrupt_scheme(pf);
16212 	kfree(pf->vsi);
16213 err_switch_setup:
16214 	i40e_reset_interrupt_capability(pf);
16215 	timer_shutdown_sync(&pf->service_timer);
16216 err_mac_addr:
16217 err_configure_lan_hmc:
16218 	(void)i40e_shutdown_lan_hmc(hw);
16219 err_init_lan_hmc:
16220 	kfree(pf->qp_pile);
16221 err_sw_init:
16222 err_adminq_setup:
16223 err_pf_reset:
16224 	iounmap(hw->hw_addr);
16225 err_ioremap:
16226 	i40e_free_pf(pf);
16227 err_pf_alloc:
16228 	pci_release_mem_regions(pdev);
16229 err_pci_reg:
16230 err_dma:
16231 	pci_disable_device(pdev);
16232 	return err;
16233 }
16234 
16235 /**
16236  * i40e_remove - Device removal routine
16237  * @pdev: PCI device information struct
16238  *
16239  * i40e_remove is called by the PCI subsystem to alert the driver
16240  * that is should release a PCI device.  This could be caused by a
16241  * Hot-Plug event, or because the driver is going to be removed from
16242  * memory.
16243  **/
16244 static void i40e_remove(struct pci_dev *pdev)
16245 {
16246 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16247 	struct i40e_hw *hw = &pf->hw;
16248 	int ret_code;
16249 	int i;
16250 
16251 	i40e_devlink_unregister(pf);
16252 
16253 	i40e_dbg_pf_exit(pf);
16254 
16255 	i40e_ptp_stop(pf);
16256 
16257 	/* Disable RSS in hw */
16258 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16259 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16260 
16261 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16262 	 * flags, once they are set, i40e_rebuild should not be called as
16263 	 * i40e_prep_for_reset always returns early.
16264 	 */
16265 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16266 		usleep_range(1000, 2000);
16267 	set_bit(__I40E_IN_REMOVE, pf->state);
16268 
16269 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16270 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16271 		i40e_free_vfs(pf);
16272 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16273 	}
16274 	/* no more scheduling of any task */
16275 	set_bit(__I40E_SUSPENDED, pf->state);
16276 	set_bit(__I40E_DOWN, pf->state);
16277 	if (pf->service_timer.function)
16278 		timer_shutdown_sync(&pf->service_timer);
16279 	if (pf->service_task.func)
16280 		cancel_work_sync(&pf->service_task);
16281 
16282 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16283 		struct i40e_vsi *vsi = pf->vsi[0];
16284 
16285 		/* We know that we have allocated only one vsi for this PF,
16286 		 * it was just for registering netdevice, so the interface
16287 		 * could be visible in the 'ifconfig' output
16288 		 */
16289 		unregister_netdev(vsi->netdev);
16290 		free_netdev(vsi->netdev);
16291 
16292 		goto unmap;
16293 	}
16294 
16295 	/* Client close must be called explicitly here because the timer
16296 	 * has been stopped.
16297 	 */
16298 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16299 
16300 	i40e_fdir_teardown(pf);
16301 
16302 	/* If there is a switch structure or any orphans, remove them.
16303 	 * This will leave only the PF's VSI remaining.
16304 	 */
16305 	for (i = 0; i < I40E_MAX_VEB; i++) {
16306 		if (!pf->veb[i])
16307 			continue;
16308 
16309 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16310 		    pf->veb[i]->uplink_seid == 0)
16311 			i40e_switch_branch_release(pf->veb[i]);
16312 	}
16313 
16314 	/* Now we can shutdown the PF's VSIs, just before we kill
16315 	 * adminq and hmc.
16316 	 */
16317 	for (i = pf->num_alloc_vsi; i--;)
16318 		if (pf->vsi[i]) {
16319 			i40e_vsi_close(pf->vsi[i]);
16320 			i40e_vsi_release(pf->vsi[i]);
16321 			pf->vsi[i] = NULL;
16322 		}
16323 
16324 	i40e_cloud_filter_exit(pf);
16325 
16326 	/* remove attached clients */
16327 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16328 		ret_code = i40e_lan_del_device(pf);
16329 		if (ret_code)
16330 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16331 				 ret_code);
16332 	}
16333 
16334 	/* shutdown and destroy the HMC */
16335 	if (hw->hmc.hmc_obj) {
16336 		ret_code = i40e_shutdown_lan_hmc(hw);
16337 		if (ret_code)
16338 			dev_warn(&pdev->dev,
16339 				 "Failed to destroy the HMC resources: %d\n",
16340 				 ret_code);
16341 	}
16342 
16343 unmap:
16344 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16345 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16346 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16347 		free_irq(pf->pdev->irq, pf);
16348 
16349 	/* shutdown the adminq */
16350 	i40e_shutdown_adminq(hw);
16351 
16352 	/* destroy the locks only once, here */
16353 	mutex_destroy(&hw->aq.arq_mutex);
16354 	mutex_destroy(&hw->aq.asq_mutex);
16355 
16356 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16357 	rtnl_lock();
16358 	i40e_clear_interrupt_scheme(pf);
16359 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16360 		if (pf->vsi[i]) {
16361 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16362 				i40e_vsi_clear_rings(pf->vsi[i]);
16363 			i40e_vsi_clear(pf->vsi[i]);
16364 			pf->vsi[i] = NULL;
16365 		}
16366 	}
16367 	rtnl_unlock();
16368 
16369 	for (i = 0; i < I40E_MAX_VEB; i++) {
16370 		kfree(pf->veb[i]);
16371 		pf->veb[i] = NULL;
16372 	}
16373 
16374 	kfree(pf->qp_pile);
16375 	kfree(pf->vsi);
16376 
16377 	iounmap(hw->hw_addr);
16378 	i40e_free_pf(pf);
16379 	pci_release_mem_regions(pdev);
16380 
16381 	pci_disable_device(pdev);
16382 }
16383 
16384 /**
16385  * i40e_pci_error_detected - warning that something funky happened in PCI land
16386  * @pdev: PCI device information struct
16387  * @error: the type of PCI error
16388  *
16389  * Called to warn that something happened and the error handling steps
16390  * are in progress.  Allows the driver to quiesce things, be ready for
16391  * remediation.
16392  **/
16393 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16394 						pci_channel_state_t error)
16395 {
16396 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16397 
16398 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16399 
16400 	if (!pf) {
16401 		dev_info(&pdev->dev,
16402 			 "Cannot recover - error happened during device probe\n");
16403 		return PCI_ERS_RESULT_DISCONNECT;
16404 	}
16405 
16406 	/* shutdown all operations */
16407 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16408 		i40e_prep_for_reset(pf);
16409 
16410 	/* Request a slot reset */
16411 	return PCI_ERS_RESULT_NEED_RESET;
16412 }
16413 
16414 /**
16415  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16416  * @pdev: PCI device information struct
16417  *
16418  * Called to find if the driver can work with the device now that
16419  * the pci slot has been reset.  If a basic connection seems good
16420  * (registers are readable and have sane content) then return a
16421  * happy little PCI_ERS_RESULT_xxx.
16422  **/
16423 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16424 {
16425 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16426 	pci_ers_result_t result;
16427 	u32 reg;
16428 
16429 	dev_dbg(&pdev->dev, "%s\n", __func__);
16430 	if (pci_enable_device_mem(pdev)) {
16431 		dev_info(&pdev->dev,
16432 			 "Cannot re-enable PCI device after reset.\n");
16433 		result = PCI_ERS_RESULT_DISCONNECT;
16434 	} else {
16435 		pci_set_master(pdev);
16436 		pci_restore_state(pdev);
16437 		pci_save_state(pdev);
16438 		pci_wake_from_d3(pdev, false);
16439 
16440 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16441 		if (reg == 0)
16442 			result = PCI_ERS_RESULT_RECOVERED;
16443 		else
16444 			result = PCI_ERS_RESULT_DISCONNECT;
16445 	}
16446 
16447 	return result;
16448 }
16449 
16450 /**
16451  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16452  * @pdev: PCI device information struct
16453  */
16454 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16455 {
16456 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16457 
16458 	i40e_prep_for_reset(pf);
16459 }
16460 
16461 /**
16462  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16463  * @pdev: PCI device information struct
16464  */
16465 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16466 {
16467 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16468 
16469 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16470 		return;
16471 
16472 	i40e_reset_and_rebuild(pf, false, false);
16473 #ifdef CONFIG_PCI_IOV
16474 	i40e_restore_all_vfs_msi_state(pdev);
16475 #endif /* CONFIG_PCI_IOV */
16476 }
16477 
16478 /**
16479  * i40e_pci_error_resume - restart operations after PCI error recovery
16480  * @pdev: PCI device information struct
16481  *
16482  * Called to allow the driver to bring things back up after PCI error
16483  * and/or reset recovery has finished.
16484  **/
16485 static void i40e_pci_error_resume(struct pci_dev *pdev)
16486 {
16487 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16488 
16489 	dev_dbg(&pdev->dev, "%s\n", __func__);
16490 	if (test_bit(__I40E_SUSPENDED, pf->state))
16491 		return;
16492 
16493 	i40e_handle_reset_warning(pf, false);
16494 }
16495 
16496 /**
16497  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16498  * using the mac_address_write admin q function
16499  * @pf: pointer to i40e_pf struct
16500  **/
16501 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16502 {
16503 	struct i40e_hw *hw = &pf->hw;
16504 	u8 mac_addr[6];
16505 	u16 flags = 0;
16506 	int ret;
16507 
16508 	/* Get current MAC address in case it's an LAA */
16509 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16510 		ether_addr_copy(mac_addr,
16511 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16512 	} else {
16513 		dev_err(&pf->pdev->dev,
16514 			"Failed to retrieve MAC address; using default\n");
16515 		ether_addr_copy(mac_addr, hw->mac.addr);
16516 	}
16517 
16518 	/* The FW expects the mac address write cmd to first be called with
16519 	 * one of these flags before calling it again with the multicast
16520 	 * enable flags.
16521 	 */
16522 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16523 
16524 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16525 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16526 
16527 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16528 	if (ret) {
16529 		dev_err(&pf->pdev->dev,
16530 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16531 		return;
16532 	}
16533 
16534 	flags = I40E_AQC_MC_MAG_EN
16535 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16536 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16537 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16538 	if (ret)
16539 		dev_err(&pf->pdev->dev,
16540 			"Failed to enable Multicast Magic Packet wake up\n");
16541 }
16542 
16543 /**
16544  * i40e_shutdown - PCI callback for shutting down
16545  * @pdev: PCI device information struct
16546  **/
16547 static void i40e_shutdown(struct pci_dev *pdev)
16548 {
16549 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16550 	struct i40e_hw *hw = &pf->hw;
16551 
16552 	set_bit(__I40E_SUSPENDED, pf->state);
16553 	set_bit(__I40E_DOWN, pf->state);
16554 
16555 	del_timer_sync(&pf->service_timer);
16556 	cancel_work_sync(&pf->service_task);
16557 	i40e_cloud_filter_exit(pf);
16558 	i40e_fdir_teardown(pf);
16559 
16560 	/* Client close must be called explicitly here because the timer
16561 	 * has been stopped.
16562 	 */
16563 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16564 
16565 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16566 	    pf->wol_en)
16567 		i40e_enable_mc_magic_wake(pf);
16568 
16569 	i40e_prep_for_reset(pf);
16570 
16571 	wr32(hw, I40E_PFPM_APM,
16572 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16573 	wr32(hw, I40E_PFPM_WUFC,
16574 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16575 
16576 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16577 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16578 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16579 		free_irq(pf->pdev->irq, pf);
16580 
16581 	/* Since we're going to destroy queues during the
16582 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16583 	 * whole section
16584 	 */
16585 	rtnl_lock();
16586 	i40e_clear_interrupt_scheme(pf);
16587 	rtnl_unlock();
16588 
16589 	if (system_state == SYSTEM_POWER_OFF) {
16590 		pci_wake_from_d3(pdev, pf->wol_en);
16591 		pci_set_power_state(pdev, PCI_D3hot);
16592 	}
16593 }
16594 
16595 /**
16596  * i40e_suspend - PM callback for moving to D3
16597  * @dev: generic device information structure
16598  **/
16599 static int __maybe_unused i40e_suspend(struct device *dev)
16600 {
16601 	struct i40e_pf *pf = dev_get_drvdata(dev);
16602 	struct i40e_hw *hw = &pf->hw;
16603 
16604 	/* If we're already suspended, then there is nothing to do */
16605 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16606 		return 0;
16607 
16608 	set_bit(__I40E_DOWN, pf->state);
16609 
16610 	/* Ensure service task will not be running */
16611 	del_timer_sync(&pf->service_timer);
16612 	cancel_work_sync(&pf->service_task);
16613 
16614 	/* Client close must be called explicitly here because the timer
16615 	 * has been stopped.
16616 	 */
16617 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16618 
16619 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16620 	    pf->wol_en)
16621 		i40e_enable_mc_magic_wake(pf);
16622 
16623 	/* Since we're going to destroy queues during the
16624 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16625 	 * whole section
16626 	 */
16627 	rtnl_lock();
16628 
16629 	i40e_prep_for_reset(pf);
16630 
16631 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16632 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16633 
16634 	/* Clear the interrupt scheme and release our IRQs so that the system
16635 	 * can safely hibernate even when there are a large number of CPUs.
16636 	 * Otherwise hibernation might fail when mapping all the vectors back
16637 	 * to CPU0.
16638 	 */
16639 	i40e_clear_interrupt_scheme(pf);
16640 
16641 	rtnl_unlock();
16642 
16643 	return 0;
16644 }
16645 
16646 /**
16647  * i40e_resume - PM callback for waking up from D3
16648  * @dev: generic device information structure
16649  **/
16650 static int __maybe_unused i40e_resume(struct device *dev)
16651 {
16652 	struct i40e_pf *pf = dev_get_drvdata(dev);
16653 	int err;
16654 
16655 	/* If we're not suspended, then there is nothing to do */
16656 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16657 		return 0;
16658 
16659 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16660 	 * since we're going to be restoring queues
16661 	 */
16662 	rtnl_lock();
16663 
16664 	/* We cleared the interrupt scheme when we suspended, so we need to
16665 	 * restore it now to resume device functionality.
16666 	 */
16667 	err = i40e_restore_interrupt_scheme(pf);
16668 	if (err) {
16669 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16670 			err);
16671 	}
16672 
16673 	clear_bit(__I40E_DOWN, pf->state);
16674 	i40e_reset_and_rebuild(pf, false, true);
16675 
16676 	rtnl_unlock();
16677 
16678 	/* Clear suspended state last after everything is recovered */
16679 	clear_bit(__I40E_SUSPENDED, pf->state);
16680 
16681 	/* Restart the service task */
16682 	mod_timer(&pf->service_timer,
16683 		  round_jiffies(jiffies + pf->service_timer_period));
16684 
16685 	return 0;
16686 }
16687 
16688 static const struct pci_error_handlers i40e_err_handler = {
16689 	.error_detected = i40e_pci_error_detected,
16690 	.slot_reset = i40e_pci_error_slot_reset,
16691 	.reset_prepare = i40e_pci_error_reset_prepare,
16692 	.reset_done = i40e_pci_error_reset_done,
16693 	.resume = i40e_pci_error_resume,
16694 };
16695 
16696 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16697 
16698 static struct pci_driver i40e_driver = {
16699 	.name     = i40e_driver_name,
16700 	.id_table = i40e_pci_tbl,
16701 	.probe    = i40e_probe,
16702 	.remove   = i40e_remove,
16703 	.driver   = {
16704 		.pm = &i40e_pm_ops,
16705 	},
16706 	.shutdown = i40e_shutdown,
16707 	.err_handler = &i40e_err_handler,
16708 	.sriov_configure = i40e_pci_sriov_configure,
16709 };
16710 
16711 /**
16712  * i40e_init_module - Driver registration routine
16713  *
16714  * i40e_init_module is the first routine called when the driver is
16715  * loaded. All it does is register with the PCI subsystem.
16716  **/
16717 static int __init i40e_init_module(void)
16718 {
16719 	int err;
16720 
16721 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16722 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16723 
16724 	/* There is no need to throttle the number of active tasks because
16725 	 * each device limits its own task using a state bit for scheduling
16726 	 * the service task, and the device tasks do not interfere with each
16727 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16728 	 * since we need to be able to guarantee forward progress even under
16729 	 * memory pressure.
16730 	 */
16731 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16732 	if (!i40e_wq) {
16733 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16734 		return -ENOMEM;
16735 	}
16736 
16737 	i40e_dbg_init();
16738 	err = pci_register_driver(&i40e_driver);
16739 	if (err) {
16740 		destroy_workqueue(i40e_wq);
16741 		i40e_dbg_exit();
16742 		return err;
16743 	}
16744 
16745 	return 0;
16746 }
16747 module_init(i40e_init_module);
16748 
16749 /**
16750  * i40e_exit_module - Driver exit cleanup routine
16751  *
16752  * i40e_exit_module is called just before the driver is removed
16753  * from memory.
16754  **/
16755 static void __exit i40e_exit_module(void)
16756 {
16757 	pci_unregister_driver(&i40e_driver);
16758 	destroy_workqueue(i40e_wq);
16759 	ida_destroy(&i40e_client_ida);
16760 	i40e_dbg_exit();
16761 }
16762 module_exit(i40e_exit_module);
16763