xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision af2d6148d2a159e1a0862bce5a2c88c1618a2b27)
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/net/intel/libie/pctype.h>
7 #include <linux/if_bridge.h>
8 #include <linux/if_macvlan.h>
9 #include <linux/module.h>
10 #include <net/pkt_cls.h>
11 #include <net/xdp_sock_drv.h>
12 
13 /* Local includes */
14 #include "i40e.h"
15 #include "i40e_devids.h"
16 #include "i40e_diag.h"
17 #include "i40e_lan_hmc.h"
18 #include "i40e_virtchnl_pf.h"
19 #include "i40e_xsk.h"
20 
21 /* All i40e tracepoints are defined by the include below, which
22  * must be included exactly once across the whole kernel with
23  * CREATE_TRACE_POINTS defined
24  */
25 #define CREATE_TRACE_POINTS
26 #include "i40e_trace.h"
27 
28 const char i40e_driver_name[] = "i40e";
29 static const char i40e_driver_string[] =
30 			"Intel(R) Ethernet Connection XL710 Network Driver";
31 
32 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
33 
34 /* a bit of forward declarations */
35 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
36 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
37 static int i40e_add_vsi(struct i40e_vsi *vsi);
38 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
39 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
40 static int i40e_setup_misc_vector(struct i40e_pf *pf);
41 static void i40e_determine_queue_usage(struct i40e_pf *pf);
42 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
43 static void i40e_prep_for_reset(struct i40e_pf *pf);
44 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
45 				   bool lock_acquired);
46 static int i40e_reset(struct i40e_pf *pf);
47 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
48 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
49 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
50 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
51 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
52 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
53 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
54 static int i40e_get_capabilities(struct i40e_pf *pf,
55 				 enum i40e_admin_queue_opc list_type);
56 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
57 
58 /* i40e_pci_tbl - PCI Device ID Table
59  *
60  * Last entry must be all 0s
61  *
62  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
63  *   Class, Class Mask, private data (not used) }
64  */
65 static const struct pci_device_id i40e_pci_tbl[] = {
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
91 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
92 	/* required last entry */
93 	{0, }
94 };
95 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
96 
97 #define I40E_MAX_VF_COUNT 128
98 static int debug = -1;
99 module_param(debug, uint, 0);
100 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
101 
102 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103 MODULE_IMPORT_NS("LIBIE");
104 MODULE_LICENSE("GPL v2");
105 
106 static struct workqueue_struct *i40e_wq;
107 
108 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
109 				  struct net_device *netdev, int delta)
110 {
111 	struct netdev_hw_addr_list *ha_list;
112 	struct netdev_hw_addr *ha;
113 
114 	if (!f || !netdev)
115 		return;
116 
117 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
118 		ha_list = &netdev->uc;
119 	else
120 		ha_list = &netdev->mc;
121 
122 	netdev_hw_addr_list_for_each(ha, ha_list) {
123 		if (ether_addr_equal(ha->addr, f->macaddr)) {
124 			ha->refcount += delta;
125 			if (ha->refcount <= 0)
126 				ha->refcount = 1;
127 			break;
128 		}
129 	}
130 }
131 
132 /**
133  * i40e_hw_to_dev - get device pointer from the hardware structure
134  * @hw: pointer to the device HW structure
135  **/
136 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
137 {
138 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
139 
140 	return &pf->pdev->dev;
141 }
142 
143 /**
144  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
145  * @hw:   pointer to the HW structure
146  * @mem:  ptr to mem struct to fill out
147  * @size: size of memory requested
148  * @alignment: what to align the allocation to
149  **/
150 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
151 			  u64 size, u32 alignment)
152 {
153 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
154 
155 	mem->size = ALIGN(size, alignment);
156 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
157 				     GFP_KERNEL);
158 	if (!mem->va)
159 		return -ENOMEM;
160 
161 	return 0;
162 }
163 
164 /**
165  * i40e_free_dma_mem - OS specific memory free for shared code
166  * @hw:   pointer to the HW structure
167  * @mem:  ptr to mem struct to free
168  **/
169 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
170 {
171 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
172 
173 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
174 	mem->va = NULL;
175 	mem->pa = 0;
176 	mem->size = 0;
177 
178 	return 0;
179 }
180 
181 /**
182  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
183  * @hw:   pointer to the HW structure
184  * @mem:  ptr to mem struct to fill out
185  * @size: size of memory requested
186  **/
187 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
188 			   u32 size)
189 {
190 	mem->size = size;
191 	mem->va = kzalloc(size, GFP_KERNEL);
192 
193 	if (!mem->va)
194 		return -ENOMEM;
195 
196 	return 0;
197 }
198 
199 /**
200  * i40e_free_virt_mem - OS specific memory free for shared code
201  * @hw:   pointer to the HW structure
202  * @mem:  ptr to mem struct to free
203  **/
204 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
205 {
206 	/* it's ok to kfree a NULL pointer */
207 	kfree(mem->va);
208 	mem->va = NULL;
209 	mem->size = 0;
210 
211 	return 0;
212 }
213 
214 /**
215  * i40e_get_lump - find a lump of free generic resource
216  * @pf: board private structure
217  * @pile: the pile of resource to search
218  * @needed: the number of items needed
219  * @id: an owner id to stick on the items assigned
220  *
221  * Returns the base item index of the lump, or negative for error
222  **/
223 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
224 			 u16 needed, u16 id)
225 {
226 	int ret = -ENOMEM;
227 	int i, j;
228 
229 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
230 		dev_info(&pf->pdev->dev,
231 			 "param err: pile=%s needed=%d id=0x%04x\n",
232 			 pile ? "<valid>" : "<null>", needed, id);
233 		return -EINVAL;
234 	}
235 
236 	/* Allocate last queue in the pile for FDIR VSI queue
237 	 * so it doesn't fragment the qp_pile
238 	 */
239 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
240 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
241 			dev_err(&pf->pdev->dev,
242 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
243 				pile->num_entries - 1);
244 			return -ENOMEM;
245 		}
246 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
247 		return pile->num_entries - 1;
248 	}
249 
250 	i = 0;
251 	while (i < pile->num_entries) {
252 		/* skip already allocated entries */
253 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
254 			i++;
255 			continue;
256 		}
257 
258 		/* do we have enough in this lump? */
259 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
260 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
261 				break;
262 		}
263 
264 		if (j == needed) {
265 			/* there was enough, so assign it to the requestor */
266 			for (j = 0; j < needed; j++)
267 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
268 			ret = i;
269 			break;
270 		}
271 
272 		/* not enough, so skip over it and continue looking */
273 		i += j;
274 	}
275 
276 	return ret;
277 }
278 
279 /**
280  * i40e_put_lump - return a lump of generic resource
281  * @pile: the pile of resource to search
282  * @index: the base item index
283  * @id: the owner id of the items assigned
284  *
285  * Returns the count of items in the lump
286  **/
287 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
288 {
289 	int valid_id = (id | I40E_PILE_VALID_BIT);
290 	int count = 0;
291 	u16 i;
292 
293 	if (!pile || index >= pile->num_entries)
294 		return -EINVAL;
295 
296 	for (i = index;
297 	     i < pile->num_entries && pile->list[i] == valid_id;
298 	     i++) {
299 		pile->list[i] = 0;
300 		count++;
301 	}
302 
303 
304 	return count;
305 }
306 
307 /**
308  * i40e_find_vsi_from_id - searches for the vsi with the given id
309  * @pf: the pf structure to search for the vsi
310  * @id: id of the vsi it is searching for
311  **/
312 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
313 {
314 	struct i40e_vsi *vsi;
315 	int i;
316 
317 	i40e_pf_for_each_vsi(pf, i, vsi)
318 		if (vsi->id == id)
319 			return vsi;
320 
321 	return NULL;
322 }
323 
324 /**
325  * i40e_service_event_schedule - Schedule the service task to wake up
326  * @pf: board private structure
327  *
328  * If not already scheduled, this puts the task into the work queue
329  **/
330 void i40e_service_event_schedule(struct i40e_pf *pf)
331 {
332 	if ((!test_bit(__I40E_DOWN, pf->state) &&
333 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
334 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
335 		queue_work(i40e_wq, &pf->service_task);
336 }
337 
338 /**
339  * i40e_tx_timeout - Respond to a Tx Hang
340  * @netdev: network interface device structure
341  * @txqueue: queue number timing out
342  *
343  * If any port has noticed a Tx timeout, it is likely that the whole
344  * device is munged, not just the one netdev port, so go for the full
345  * reset.
346  **/
347 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
348 {
349 	struct i40e_netdev_priv *np = netdev_priv(netdev);
350 	struct i40e_vsi *vsi = np->vsi;
351 	struct i40e_pf *pf = vsi->back;
352 	struct i40e_ring *tx_ring = NULL;
353 	unsigned int i;
354 	u32 head, val;
355 
356 	pf->tx_timeout_count++;
357 
358 	/* with txqueue index, find the tx_ring struct */
359 	for (i = 0; i < vsi->num_queue_pairs; i++) {
360 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
361 			if (txqueue ==
362 			    vsi->tx_rings[i]->queue_index) {
363 				tx_ring = vsi->tx_rings[i];
364 				break;
365 			}
366 		}
367 	}
368 
369 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
370 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
371 	else if (time_before(jiffies,
372 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
373 		return;   /* don't do any new action before the next timeout */
374 
375 	/* don't kick off another recovery if one is already pending */
376 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
377 		return;
378 
379 	if (tx_ring) {
380 		head = i40e_get_head(tx_ring);
381 		/* Read interrupt register */
382 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
383 			val = rd32(&pf->hw,
384 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
385 						tx_ring->vsi->base_vector - 1));
386 		else
387 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
388 
389 		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",
390 			    vsi->seid, txqueue, tx_ring->next_to_clean,
391 			    head, tx_ring->next_to_use,
392 			    readl(tx_ring->tail), val);
393 	}
394 
395 	pf->tx_timeout_last_recovery = jiffies;
396 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
397 		    pf->tx_timeout_recovery_level, txqueue);
398 
399 	switch (pf->tx_timeout_recovery_level) {
400 	case 1:
401 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
402 		break;
403 	case 2:
404 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
405 		break;
406 	case 3:
407 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
408 		break;
409 	default:
410 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
411 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
412 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
413 		break;
414 	}
415 
416 	i40e_service_event_schedule(pf);
417 	pf->tx_timeout_recovery_level++;
418 }
419 
420 /**
421  * i40e_get_vsi_stats_struct - Get System Network Statistics
422  * @vsi: the VSI we care about
423  *
424  * Returns the address of the device statistics structure.
425  * The statistics are actually updated from the service task.
426  **/
427 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
428 {
429 	return &vsi->net_stats;
430 }
431 
432 /**
433  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
434  * @ring: Tx ring to get statistics from
435  * @stats: statistics entry to be updated
436  **/
437 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
438 					    struct rtnl_link_stats64 *stats)
439 {
440 	u64 bytes, packets;
441 	unsigned int start;
442 
443 	do {
444 		start = u64_stats_fetch_begin(&ring->syncp);
445 		packets = ring->stats.packets;
446 		bytes   = ring->stats.bytes;
447 	} while (u64_stats_fetch_retry(&ring->syncp, start));
448 
449 	stats->tx_packets += packets;
450 	stats->tx_bytes   += bytes;
451 }
452 
453 /**
454  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
455  * @netdev: network interface device structure
456  * @stats: data structure to store statistics
457  *
458  * Returns the address of the device statistics structure.
459  * The statistics are actually updated from the service task.
460  **/
461 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
462 				  struct rtnl_link_stats64 *stats)
463 {
464 	struct i40e_netdev_priv *np = netdev_priv(netdev);
465 	struct i40e_vsi *vsi = np->vsi;
466 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
467 	struct i40e_ring *ring;
468 	int i;
469 
470 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
471 		return;
472 
473 	if (!vsi->tx_rings)
474 		return;
475 
476 	rcu_read_lock();
477 	for (i = 0; i < vsi->num_queue_pairs; i++) {
478 		u64 bytes, packets;
479 		unsigned int start;
480 
481 		ring = READ_ONCE(vsi->tx_rings[i]);
482 		if (!ring)
483 			continue;
484 		i40e_get_netdev_stats_struct_tx(ring, stats);
485 
486 		if (i40e_enabled_xdp_vsi(vsi)) {
487 			ring = READ_ONCE(vsi->xdp_rings[i]);
488 			if (!ring)
489 				continue;
490 			i40e_get_netdev_stats_struct_tx(ring, stats);
491 		}
492 
493 		ring = READ_ONCE(vsi->rx_rings[i]);
494 		if (!ring)
495 			continue;
496 		do {
497 			start   = u64_stats_fetch_begin(&ring->syncp);
498 			packets = ring->stats.packets;
499 			bytes   = ring->stats.bytes;
500 		} while (u64_stats_fetch_retry(&ring->syncp, start));
501 
502 		stats->rx_packets += packets;
503 		stats->rx_bytes   += bytes;
504 
505 	}
506 	rcu_read_unlock();
507 
508 	/* following stats updated by i40e_watchdog_subtask() */
509 	stats->multicast	= vsi_stats->multicast;
510 	stats->tx_errors	= vsi_stats->tx_errors;
511 	stats->tx_dropped	= vsi_stats->tx_dropped;
512 	stats->rx_errors	= vsi_stats->rx_errors;
513 	stats->rx_dropped	= vsi_stats->rx_dropped;
514 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
515 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
516 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
517 }
518 
519 /**
520  * i40e_vsi_reset_stats - Resets all stats of the given vsi
521  * @vsi: the VSI to have its stats reset
522  **/
523 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
524 {
525 	struct rtnl_link_stats64 *ns;
526 	int i;
527 
528 	if (!vsi)
529 		return;
530 
531 	ns = i40e_get_vsi_stats_struct(vsi);
532 	memset(ns, 0, sizeof(*ns));
533 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
534 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
535 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
536 	if (vsi->rx_rings && vsi->rx_rings[0]) {
537 		for (i = 0; i < vsi->num_queue_pairs; i++) {
538 			memset(&vsi->rx_rings[i]->stats, 0,
539 			       sizeof(vsi->rx_rings[i]->stats));
540 			memset(&vsi->rx_rings[i]->rx_stats, 0,
541 			       sizeof(vsi->rx_rings[i]->rx_stats));
542 			memset(&vsi->tx_rings[i]->stats, 0,
543 			       sizeof(vsi->tx_rings[i]->stats));
544 			memset(&vsi->tx_rings[i]->tx_stats, 0,
545 			       sizeof(vsi->tx_rings[i]->tx_stats));
546 		}
547 	}
548 	vsi->stat_offsets_loaded = false;
549 }
550 
551 /**
552  * i40e_pf_reset_stats - Reset all of the stats for the given PF
553  * @pf: the PF to be reset
554  **/
555 void i40e_pf_reset_stats(struct i40e_pf *pf)
556 {
557 	struct i40e_veb *veb;
558 	int i;
559 
560 	memset(&pf->stats, 0, sizeof(pf->stats));
561 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
562 	pf->stat_offsets_loaded = false;
563 
564 	i40e_pf_for_each_veb(pf, i, veb) {
565 		memset(&veb->stats, 0, sizeof(veb->stats));
566 		memset(&veb->stats_offsets, 0, sizeof(veb->stats_offsets));
567 		memset(&veb->tc_stats, 0, sizeof(veb->tc_stats));
568 		memset(&veb->tc_stats_offsets, 0, sizeof(veb->tc_stats_offsets));
569 		veb->stat_offsets_loaded = false;
570 	}
571 	pf->hw_csum_rx_error = 0;
572 }
573 
574 /**
575  * i40e_compute_pci_to_hw_id - compute index form PCI function.
576  * @vsi: ptr to the VSI to read from.
577  * @hw: ptr to the hardware info.
578  **/
579 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
580 {
581 	int pf_count = i40e_get_pf_count(hw);
582 
583 	if (vsi->type == I40E_VSI_SRIOV)
584 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
585 
586 	return hw->port + BIT(7);
587 }
588 
589 /**
590  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
591  * @hw: ptr to the hardware info.
592  * @hireg: the high 32 bit reg to read.
593  * @loreg: the low 32 bit reg to read.
594  * @offset_loaded: has the initial offset been loaded yet.
595  * @offset: ptr to current offset value.
596  * @stat: ptr to the stat.
597  *
598  * Since the device stats are not reset at PFReset, they will not
599  * be zeroed when the driver starts.  We'll save the first values read
600  * and use them as offsets to be subtracted from the raw values in order
601  * to report stats that count from zero.
602  **/
603 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
604 			       bool offset_loaded, u64 *offset, u64 *stat)
605 {
606 	u64 new_data;
607 
608 	new_data = rd64(hw, loreg);
609 
610 	if (!offset_loaded || new_data < *offset)
611 		*offset = new_data;
612 	*stat = new_data - *offset;
613 }
614 
615 /**
616  * i40e_stat_update48 - read and update a 48 bit stat from the chip
617  * @hw: ptr to the hardware info
618  * @hireg: the high 32 bit reg to read
619  * @loreg: the low 32 bit reg to read
620  * @offset_loaded: has the initial offset been loaded yet
621  * @offset: ptr to current offset value
622  * @stat: ptr to the stat
623  *
624  * Since the device stats are not reset at PFReset, they likely will not
625  * be zeroed when the driver starts.  We'll save the first values read
626  * and use them as offsets to be subtracted from the raw values in order
627  * to report stats that count from zero.  In the process, we also manage
628  * the potential roll-over.
629  **/
630 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
631 			       bool offset_loaded, u64 *offset, u64 *stat)
632 {
633 	u64 new_data;
634 
635 	if (hw->device_id == I40E_DEV_ID_QEMU) {
636 		new_data = rd32(hw, loreg);
637 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
638 	} else {
639 		new_data = rd64(hw, loreg);
640 	}
641 	if (!offset_loaded)
642 		*offset = new_data;
643 	if (likely(new_data >= *offset))
644 		*stat = new_data - *offset;
645 	else
646 		*stat = (new_data + BIT_ULL(48)) - *offset;
647 	*stat &= 0xFFFFFFFFFFFFULL;
648 }
649 
650 /**
651  * i40e_stat_update32 - read and update a 32 bit stat from the chip
652  * @hw: ptr to the hardware info
653  * @reg: the hw reg to read
654  * @offset_loaded: has the initial offset been loaded yet
655  * @offset: ptr to current offset value
656  * @stat: ptr to the stat
657  **/
658 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
659 			       bool offset_loaded, u64 *offset, u64 *stat)
660 {
661 	u32 new_data;
662 
663 	new_data = rd32(hw, reg);
664 	if (!offset_loaded)
665 		*offset = new_data;
666 	if (likely(new_data >= *offset))
667 		*stat = (u32)(new_data - *offset);
668 	else
669 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
670 }
671 
672 /**
673  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
674  * @hw: ptr to the hardware info
675  * @reg: the hw reg to read and clear
676  * @stat: ptr to the stat
677  **/
678 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
679 {
680 	u32 new_data = rd32(hw, reg);
681 
682 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
683 	*stat += new_data;
684 }
685 
686 /**
687  * i40e_stats_update_rx_discards - update rx_discards.
688  * @vsi: ptr to the VSI to be updated.
689  * @hw: ptr to the hardware info.
690  * @stat_idx: VSI's stat_counter_idx.
691  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
692  * @stat_offset: ptr to stat_offset to store first read of specific register.
693  * @stat: ptr to VSI's stat to be updated.
694  **/
695 static void
696 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
697 			      int stat_idx, bool offset_loaded,
698 			      struct i40e_eth_stats *stat_offset,
699 			      struct i40e_eth_stats *stat)
700 {
701 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
702 			   &stat_offset->rx_discards, &stat->rx_discards);
703 	i40e_stat_update64(hw,
704 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
705 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
706 			   offset_loaded, &stat_offset->rx_discards_other,
707 			   &stat->rx_discards_other);
708 }
709 
710 /**
711  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
712  * @vsi: the VSI to be updated
713  **/
714 void i40e_update_eth_stats(struct i40e_vsi *vsi)
715 {
716 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
717 	struct i40e_pf *pf = vsi->back;
718 	struct i40e_hw *hw = &pf->hw;
719 	struct i40e_eth_stats *oes;
720 	struct i40e_eth_stats *es;     /* device's eth stats */
721 
722 	es = &vsi->eth_stats;
723 	oes = &vsi->eth_stats_offsets;
724 
725 	/* Gather up the stats that the hw collects */
726 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
727 			   vsi->stat_offsets_loaded,
728 			   &oes->tx_errors, &es->tx_errors);
729 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
730 			   vsi->stat_offsets_loaded,
731 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
732 
733 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
734 			   I40E_GLV_GORCL(stat_idx),
735 			   vsi->stat_offsets_loaded,
736 			   &oes->rx_bytes, &es->rx_bytes);
737 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
738 			   I40E_GLV_UPRCL(stat_idx),
739 			   vsi->stat_offsets_loaded,
740 			   &oes->rx_unicast, &es->rx_unicast);
741 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
742 			   I40E_GLV_MPRCL(stat_idx),
743 			   vsi->stat_offsets_loaded,
744 			   &oes->rx_multicast, &es->rx_multicast);
745 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
746 			   I40E_GLV_BPRCL(stat_idx),
747 			   vsi->stat_offsets_loaded,
748 			   &oes->rx_broadcast, &es->rx_broadcast);
749 
750 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
751 			   I40E_GLV_GOTCL(stat_idx),
752 			   vsi->stat_offsets_loaded,
753 			   &oes->tx_bytes, &es->tx_bytes);
754 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
755 			   I40E_GLV_UPTCL(stat_idx),
756 			   vsi->stat_offsets_loaded,
757 			   &oes->tx_unicast, &es->tx_unicast);
758 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
759 			   I40E_GLV_MPTCL(stat_idx),
760 			   vsi->stat_offsets_loaded,
761 			   &oes->tx_multicast, &es->tx_multicast);
762 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
763 			   I40E_GLV_BPTCL(stat_idx),
764 			   vsi->stat_offsets_loaded,
765 			   &oes->tx_broadcast, &es->tx_broadcast);
766 
767 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
768 				      vsi->stat_offsets_loaded, oes, es);
769 
770 	vsi->stat_offsets_loaded = true;
771 }
772 
773 /**
774  * i40e_update_veb_stats - Update Switch component statistics
775  * @veb: the VEB being updated
776  **/
777 void i40e_update_veb_stats(struct i40e_veb *veb)
778 {
779 	struct i40e_pf *pf = veb->pf;
780 	struct i40e_hw *hw = &pf->hw;
781 	struct i40e_eth_stats *oes;
782 	struct i40e_eth_stats *es;     /* device's eth stats */
783 	struct i40e_veb_tc_stats *veb_oes;
784 	struct i40e_veb_tc_stats *veb_es;
785 	int i, idx = 0;
786 
787 	idx = veb->stats_idx;
788 	es = &veb->stats;
789 	oes = &veb->stats_offsets;
790 	veb_es = &veb->tc_stats;
791 	veb_oes = &veb->tc_stats_offsets;
792 
793 	/* Gather up the stats that the hw collects */
794 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
795 			   veb->stat_offsets_loaded,
796 			   &oes->tx_discards, &es->tx_discards);
797 	if (hw->revision_id > 0)
798 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
799 				   veb->stat_offsets_loaded,
800 				   &oes->rx_unknown_protocol,
801 				   &es->rx_unknown_protocol);
802 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
803 			   veb->stat_offsets_loaded,
804 			   &oes->rx_bytes, &es->rx_bytes);
805 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
806 			   veb->stat_offsets_loaded,
807 			   &oes->rx_unicast, &es->rx_unicast);
808 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
809 			   veb->stat_offsets_loaded,
810 			   &oes->rx_multicast, &es->rx_multicast);
811 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
812 			   veb->stat_offsets_loaded,
813 			   &oes->rx_broadcast, &es->rx_broadcast);
814 
815 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
816 			   veb->stat_offsets_loaded,
817 			   &oes->tx_bytes, &es->tx_bytes);
818 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
819 			   veb->stat_offsets_loaded,
820 			   &oes->tx_unicast, &es->tx_unicast);
821 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
822 			   veb->stat_offsets_loaded,
823 			   &oes->tx_multicast, &es->tx_multicast);
824 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
825 			   veb->stat_offsets_loaded,
826 			   &oes->tx_broadcast, &es->tx_broadcast);
827 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
828 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
829 				   I40E_GLVEBTC_RPCL(i, idx),
830 				   veb->stat_offsets_loaded,
831 				   &veb_oes->tc_rx_packets[i],
832 				   &veb_es->tc_rx_packets[i]);
833 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
834 				   I40E_GLVEBTC_RBCL(i, idx),
835 				   veb->stat_offsets_loaded,
836 				   &veb_oes->tc_rx_bytes[i],
837 				   &veb_es->tc_rx_bytes[i]);
838 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
839 				   I40E_GLVEBTC_TPCL(i, idx),
840 				   veb->stat_offsets_loaded,
841 				   &veb_oes->tc_tx_packets[i],
842 				   &veb_es->tc_tx_packets[i]);
843 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
844 				   I40E_GLVEBTC_TBCL(i, idx),
845 				   veb->stat_offsets_loaded,
846 				   &veb_oes->tc_tx_bytes[i],
847 				   &veb_es->tc_tx_bytes[i]);
848 	}
849 	veb->stat_offsets_loaded = true;
850 }
851 
852 /**
853  * i40e_update_vsi_stats - Update the vsi statistics counters.
854  * @vsi: the VSI to be updated
855  *
856  * There are a few instances where we store the same stat in a
857  * couple of different structs.  This is partly because we have
858  * the netdev stats that need to be filled out, which is slightly
859  * different from the "eth_stats" defined by the chip and used in
860  * VF communications.  We sort it out here.
861  **/
862 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
863 {
864 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
865 	struct i40e_pf *pf = vsi->back;
866 	struct rtnl_link_stats64 *ons;
867 	struct rtnl_link_stats64 *ns;   /* netdev stats */
868 	struct i40e_eth_stats *oes;
869 	struct i40e_eth_stats *es;     /* device's eth stats */
870 	u64 tx_restart, tx_busy;
871 	struct i40e_ring *p;
872 	u64 bytes, packets;
873 	unsigned int start;
874 	u64 tx_linearize;
875 	u64 tx_force_wb;
876 	u64 tx_stopped;
877 	u64 rx_p, rx_b;
878 	u64 tx_p, tx_b;
879 	u16 q;
880 
881 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
882 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
883 		return;
884 
885 	ns = i40e_get_vsi_stats_struct(vsi);
886 	ons = &vsi->net_stats_offsets;
887 	es = &vsi->eth_stats;
888 	oes = &vsi->eth_stats_offsets;
889 
890 	/* Gather up the netdev and vsi stats that the driver collects
891 	 * on the fly during packet processing
892 	 */
893 	rx_b = rx_p = 0;
894 	tx_b = tx_p = 0;
895 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
896 	tx_stopped = 0;
897 	rx_page = 0;
898 	rx_buf = 0;
899 	rx_reuse = 0;
900 	rx_alloc = 0;
901 	rx_waive = 0;
902 	rx_busy = 0;
903 	rcu_read_lock();
904 	for (q = 0; q < vsi->num_queue_pairs; q++) {
905 		/* locate Tx ring */
906 		p = READ_ONCE(vsi->tx_rings[q]);
907 		if (!p)
908 			continue;
909 
910 		do {
911 			start = u64_stats_fetch_begin(&p->syncp);
912 			packets = p->stats.packets;
913 			bytes = p->stats.bytes;
914 		} while (u64_stats_fetch_retry(&p->syncp, start));
915 		tx_b += bytes;
916 		tx_p += packets;
917 		tx_restart += p->tx_stats.restart_queue;
918 		tx_busy += p->tx_stats.tx_busy;
919 		tx_linearize += p->tx_stats.tx_linearize;
920 		tx_force_wb += p->tx_stats.tx_force_wb;
921 		tx_stopped += p->tx_stats.tx_stopped;
922 
923 		/* locate Rx ring */
924 		p = READ_ONCE(vsi->rx_rings[q]);
925 		if (!p)
926 			continue;
927 
928 		do {
929 			start = u64_stats_fetch_begin(&p->syncp);
930 			packets = p->stats.packets;
931 			bytes = p->stats.bytes;
932 		} while (u64_stats_fetch_retry(&p->syncp, start));
933 		rx_b += bytes;
934 		rx_p += packets;
935 		rx_buf += p->rx_stats.alloc_buff_failed;
936 		rx_page += p->rx_stats.alloc_page_failed;
937 		rx_reuse += p->rx_stats.page_reuse_count;
938 		rx_alloc += p->rx_stats.page_alloc_count;
939 		rx_waive += p->rx_stats.page_waive_count;
940 		rx_busy += p->rx_stats.page_busy_count;
941 
942 		if (i40e_enabled_xdp_vsi(vsi)) {
943 			/* locate XDP ring */
944 			p = READ_ONCE(vsi->xdp_rings[q]);
945 			if (!p)
946 				continue;
947 
948 			do {
949 				start = u64_stats_fetch_begin(&p->syncp);
950 				packets = p->stats.packets;
951 				bytes = p->stats.bytes;
952 			} while (u64_stats_fetch_retry(&p->syncp, start));
953 			tx_b += bytes;
954 			tx_p += packets;
955 			tx_restart += p->tx_stats.restart_queue;
956 			tx_busy += p->tx_stats.tx_busy;
957 			tx_linearize += p->tx_stats.tx_linearize;
958 			tx_force_wb += p->tx_stats.tx_force_wb;
959 		}
960 	}
961 	rcu_read_unlock();
962 	vsi->tx_restart = tx_restart;
963 	vsi->tx_busy = tx_busy;
964 	vsi->tx_linearize = tx_linearize;
965 	vsi->tx_force_wb = tx_force_wb;
966 	vsi->tx_stopped = tx_stopped;
967 	vsi->rx_page_failed = rx_page;
968 	vsi->rx_buf_failed = rx_buf;
969 	vsi->rx_page_reuse = rx_reuse;
970 	vsi->rx_page_alloc = rx_alloc;
971 	vsi->rx_page_waive = rx_waive;
972 	vsi->rx_page_busy = rx_busy;
973 
974 	ns->rx_packets = rx_p;
975 	ns->rx_bytes = rx_b;
976 	ns->tx_packets = tx_p;
977 	ns->tx_bytes = tx_b;
978 
979 	/* update netdev stats from eth stats */
980 	i40e_update_eth_stats(vsi);
981 	ons->tx_errors = oes->tx_errors;
982 	ns->tx_errors = es->tx_errors;
983 	ons->multicast = oes->rx_multicast;
984 	ns->multicast = es->rx_multicast;
985 	ons->rx_dropped = oes->rx_discards_other;
986 	ns->rx_dropped = es->rx_discards_other;
987 	ons->rx_missed_errors = oes->rx_discards;
988 	ns->rx_missed_errors = es->rx_discards;
989 	ons->tx_dropped = oes->tx_discards;
990 	ns->tx_dropped = es->tx_discards;
991 
992 	/* pull in a couple PF stats if this is the main vsi */
993 	if (vsi->type == I40E_VSI_MAIN) {
994 		ns->rx_crc_errors = pf->stats.crc_errors;
995 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
996 		ns->rx_length_errors = pf->stats.rx_length_errors;
997 	}
998 }
999 
1000 /**
1001  * i40e_update_pf_stats - Update the PF statistics counters.
1002  * @pf: the PF to be updated
1003  **/
1004 static void i40e_update_pf_stats(struct i40e_pf *pf)
1005 {
1006 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1007 	struct i40e_hw_port_stats *nsd = &pf->stats;
1008 	struct i40e_hw *hw = &pf->hw;
1009 	u32 val;
1010 	int i;
1011 
1012 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1013 			   I40E_GLPRT_GORCL(hw->port),
1014 			   pf->stat_offsets_loaded,
1015 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1016 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1017 			   I40E_GLPRT_GOTCL(hw->port),
1018 			   pf->stat_offsets_loaded,
1019 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1020 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->eth.rx_discards,
1023 			   &nsd->eth.rx_discards);
1024 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1025 			   I40E_GLPRT_UPRCL(hw->port),
1026 			   pf->stat_offsets_loaded,
1027 			   &osd->eth.rx_unicast,
1028 			   &nsd->eth.rx_unicast);
1029 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1030 			   I40E_GLPRT_MPRCL(hw->port),
1031 			   pf->stat_offsets_loaded,
1032 			   &osd->eth.rx_multicast,
1033 			   &nsd->eth.rx_multicast);
1034 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1035 			   I40E_GLPRT_BPRCL(hw->port),
1036 			   pf->stat_offsets_loaded,
1037 			   &osd->eth.rx_broadcast,
1038 			   &nsd->eth.rx_broadcast);
1039 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1040 			   I40E_GLPRT_UPTCL(hw->port),
1041 			   pf->stat_offsets_loaded,
1042 			   &osd->eth.tx_unicast,
1043 			   &nsd->eth.tx_unicast);
1044 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1045 			   I40E_GLPRT_MPTCL(hw->port),
1046 			   pf->stat_offsets_loaded,
1047 			   &osd->eth.tx_multicast,
1048 			   &nsd->eth.tx_multicast);
1049 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1050 			   I40E_GLPRT_BPTCL(hw->port),
1051 			   pf->stat_offsets_loaded,
1052 			   &osd->eth.tx_broadcast,
1053 			   &nsd->eth.tx_broadcast);
1054 
1055 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->tx_dropped_link_down,
1058 			   &nsd->tx_dropped_link_down);
1059 
1060 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1061 			   pf->stat_offsets_loaded,
1062 			   &osd->crc_errors, &nsd->crc_errors);
1063 
1064 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1067 
1068 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1069 			   pf->stat_offsets_loaded,
1070 			   &osd->mac_local_faults,
1071 			   &nsd->mac_local_faults);
1072 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->mac_remote_faults,
1075 			   &nsd->mac_remote_faults);
1076 
1077 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1078 			   pf->stat_offsets_loaded,
1079 			   &osd->rx_length_errors,
1080 			   &nsd->rx_length_errors);
1081 
1082 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1083 			   pf->stat_offsets_loaded,
1084 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1085 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1086 			   pf->stat_offsets_loaded,
1087 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1088 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1091 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1092 			   pf->stat_offsets_loaded,
1093 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1094 
1095 	for (i = 0; i < 8; i++) {
1096 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1097 				   pf->stat_offsets_loaded,
1098 				   &osd->priority_xoff_rx[i],
1099 				   &nsd->priority_xoff_rx[i]);
1100 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1101 				   pf->stat_offsets_loaded,
1102 				   &osd->priority_xon_rx[i],
1103 				   &nsd->priority_xon_rx[i]);
1104 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1105 				   pf->stat_offsets_loaded,
1106 				   &osd->priority_xon_tx[i],
1107 				   &nsd->priority_xon_tx[i]);
1108 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1109 				   pf->stat_offsets_loaded,
1110 				   &osd->priority_xoff_tx[i],
1111 				   &nsd->priority_xoff_tx[i]);
1112 		i40e_stat_update32(hw,
1113 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1114 				   pf->stat_offsets_loaded,
1115 				   &osd->priority_xon_2_xoff[i],
1116 				   &nsd->priority_xon_2_xoff[i]);
1117 	}
1118 
1119 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1120 			   I40E_GLPRT_PRC64L(hw->port),
1121 			   pf->stat_offsets_loaded,
1122 			   &osd->rx_size_64, &nsd->rx_size_64);
1123 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1124 			   I40E_GLPRT_PRC127L(hw->port),
1125 			   pf->stat_offsets_loaded,
1126 			   &osd->rx_size_127, &nsd->rx_size_127);
1127 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1128 			   I40E_GLPRT_PRC255L(hw->port),
1129 			   pf->stat_offsets_loaded,
1130 			   &osd->rx_size_255, &nsd->rx_size_255);
1131 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1132 			   I40E_GLPRT_PRC511L(hw->port),
1133 			   pf->stat_offsets_loaded,
1134 			   &osd->rx_size_511, &nsd->rx_size_511);
1135 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1136 			   I40E_GLPRT_PRC1023L(hw->port),
1137 			   pf->stat_offsets_loaded,
1138 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1139 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1140 			   I40E_GLPRT_PRC1522L(hw->port),
1141 			   pf->stat_offsets_loaded,
1142 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1143 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1144 			   I40E_GLPRT_PRC9522L(hw->port),
1145 			   pf->stat_offsets_loaded,
1146 			   &osd->rx_size_big, &nsd->rx_size_big);
1147 
1148 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1149 			   I40E_GLPRT_PTC64L(hw->port),
1150 			   pf->stat_offsets_loaded,
1151 			   &osd->tx_size_64, &nsd->tx_size_64);
1152 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1153 			   I40E_GLPRT_PTC127L(hw->port),
1154 			   pf->stat_offsets_loaded,
1155 			   &osd->tx_size_127, &nsd->tx_size_127);
1156 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1157 			   I40E_GLPRT_PTC255L(hw->port),
1158 			   pf->stat_offsets_loaded,
1159 			   &osd->tx_size_255, &nsd->tx_size_255);
1160 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1161 			   I40E_GLPRT_PTC511L(hw->port),
1162 			   pf->stat_offsets_loaded,
1163 			   &osd->tx_size_511, &nsd->tx_size_511);
1164 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1165 			   I40E_GLPRT_PTC1023L(hw->port),
1166 			   pf->stat_offsets_loaded,
1167 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1168 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1169 			   I40E_GLPRT_PTC1522L(hw->port),
1170 			   pf->stat_offsets_loaded,
1171 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1172 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1173 			   I40E_GLPRT_PTC9522L(hw->port),
1174 			   pf->stat_offsets_loaded,
1175 			   &osd->tx_size_big, &nsd->tx_size_big);
1176 
1177 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1178 			   pf->stat_offsets_loaded,
1179 			   &osd->rx_undersize, &nsd->rx_undersize);
1180 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1181 			   pf->stat_offsets_loaded,
1182 			   &osd->rx_fragments, &nsd->rx_fragments);
1183 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1184 			   pf->stat_offsets_loaded,
1185 			   &osd->rx_oversize, &nsd->rx_oversize);
1186 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1187 			   pf->stat_offsets_loaded,
1188 			   &osd->rx_jabber, &nsd->rx_jabber);
1189 
1190 	/* FDIR stats */
1191 	i40e_stat_update_and_clear32(hw,
1192 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1193 			&nsd->fd_atr_match);
1194 	i40e_stat_update_and_clear32(hw,
1195 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1196 			&nsd->fd_sb_match);
1197 	i40e_stat_update_and_clear32(hw,
1198 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1199 			&nsd->fd_atr_tunnel_match);
1200 
1201 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1202 	nsd->tx_lpi_status =
1203 		       FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
1204 	nsd->rx_lpi_status =
1205 		       FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
1206 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1207 			   pf->stat_offsets_loaded,
1208 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1209 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1210 			   pf->stat_offsets_loaded,
1211 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1212 
1213 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1214 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1215 		nsd->fd_sb_status = true;
1216 	else
1217 		nsd->fd_sb_status = false;
1218 
1219 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1220 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1221 		nsd->fd_atr_status = true;
1222 	else
1223 		nsd->fd_atr_status = false;
1224 
1225 	pf->stat_offsets_loaded = true;
1226 }
1227 
1228 /**
1229  * i40e_update_stats - Update the various statistics counters.
1230  * @vsi: the VSI to be updated
1231  *
1232  * Update the various stats for this VSI and its related entities.
1233  **/
1234 void i40e_update_stats(struct i40e_vsi *vsi)
1235 {
1236 	struct i40e_pf *pf = vsi->back;
1237 
1238 	if (vsi->type == I40E_VSI_MAIN)
1239 		i40e_update_pf_stats(pf);
1240 
1241 	i40e_update_vsi_stats(vsi);
1242 }
1243 
1244 /**
1245  * i40e_count_filters - counts VSI mac filters
1246  * @vsi: the VSI to be searched
1247  *
1248  * Returns count of mac filters
1249  **/
1250 int i40e_count_filters(struct i40e_vsi *vsi)
1251 {
1252 	struct i40e_mac_filter *f;
1253 	struct hlist_node *h;
1254 	int bkt;
1255 	int cnt = 0;
1256 
1257 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1258 		if (f->state == I40E_FILTER_NEW ||
1259 		    f->state == I40E_FILTER_NEW_SYNC ||
1260 		    f->state == I40E_FILTER_ACTIVE)
1261 			++cnt;
1262 	}
1263 
1264 	return cnt;
1265 }
1266 
1267 /**
1268  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1269  * @vsi: the VSI to be searched
1270  * @macaddr: the MAC address
1271  * @vlan: the vlan
1272  *
1273  * Returns ptr to the filter object or NULL
1274  **/
1275 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1276 						const u8 *macaddr, s16 vlan)
1277 {
1278 	struct i40e_mac_filter *f;
1279 	u64 key;
1280 
1281 	if (!vsi || !macaddr)
1282 		return NULL;
1283 
1284 	key = i40e_addr_to_hkey(macaddr);
1285 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1286 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1287 		    (vlan == f->vlan))
1288 			return f;
1289 	}
1290 	return NULL;
1291 }
1292 
1293 /**
1294  * i40e_find_mac - Find a mac addr in the macvlan filters list
1295  * @vsi: the VSI to be searched
1296  * @macaddr: the MAC address we are searching for
1297  *
1298  * Returns the first filter with the provided MAC address or NULL if
1299  * MAC address was not found
1300  **/
1301 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1302 {
1303 	struct i40e_mac_filter *f;
1304 	u64 key;
1305 
1306 	if (!vsi || !macaddr)
1307 		return NULL;
1308 
1309 	key = i40e_addr_to_hkey(macaddr);
1310 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1311 		if ((ether_addr_equal(macaddr, f->macaddr)))
1312 			return f;
1313 	}
1314 	return NULL;
1315 }
1316 
1317 /**
1318  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1319  * @vsi: the VSI to be searched
1320  *
1321  * Returns true if VSI is in vlan mode or false otherwise
1322  **/
1323 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1324 {
1325 	/* If we have a PVID, always operate in VLAN mode */
1326 	if (vsi->info.pvid)
1327 		return true;
1328 
1329 	/* We need to operate in VLAN mode whenever we have any filters with
1330 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1331 	 * time, incurring search cost repeatedly. However, we can notice two
1332 	 * things:
1333 	 *
1334 	 * 1) the only place where we can gain a VLAN filter is in
1335 	 *    i40e_add_filter.
1336 	 *
1337 	 * 2) the only place where filters are actually removed is in
1338 	 *    i40e_sync_filters_subtask.
1339 	 *
1340 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1341 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1342 	 * we have to perform the full search after deleting filters in
1343 	 * i40e_sync_filters_subtask, but we already have to search
1344 	 * filters here and can perform the check at the same time. This
1345 	 * results in avoiding embedding a loop for VLAN mode inside another
1346 	 * loop over all the filters, and should maintain correctness as noted
1347 	 * above.
1348 	 */
1349 	return vsi->has_vlan_filter;
1350 }
1351 
1352 /**
1353  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1354  * @vsi: the VSI to configure
1355  * @tmp_add_list: list of filters ready to be added
1356  * @tmp_del_list: list of filters ready to be deleted
1357  * @vlan_filters: the number of active VLAN filters
1358  *
1359  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1360  * behave as expected. If we have any active VLAN filters remaining or about
1361  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1362  * so that they only match against untagged traffic. If we no longer have any
1363  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1364  * so that they match against both tagged and untagged traffic. In this way,
1365  * we ensure that we correctly receive the desired traffic. This ensures that
1366  * when we have an active VLAN we will receive only untagged traffic and
1367  * traffic matching active VLANs. If we have no active VLANs then we will
1368  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1369  *
1370  * Finally, in a similar fashion, this function also corrects filters when
1371  * there is an active PVID assigned to this VSI.
1372  *
1373  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1374  *
1375  * This function is only expected to be called from within
1376  * i40e_sync_vsi_filters.
1377  *
1378  * NOTE: This function expects to be called while under the
1379  * mac_filter_hash_lock
1380  */
1381 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1382 					 struct hlist_head *tmp_add_list,
1383 					 struct hlist_head *tmp_del_list,
1384 					 int vlan_filters)
1385 {
1386 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1387 	struct i40e_mac_filter *f, *add_head;
1388 	struct i40e_new_mac_filter *new;
1389 	struct hlist_node *h;
1390 	int bkt, new_vlan;
1391 
1392 	/* To determine if a particular filter needs to be replaced we
1393 	 * have the three following conditions:
1394 	 *
1395 	 * a) if we have a PVID assigned, then all filters which are
1396 	 *    not marked as VLAN=PVID must be replaced with filters that
1397 	 *    are.
1398 	 * b) otherwise, if we have any active VLANS, all filters
1399 	 *    which are marked as VLAN=-1 must be replaced with
1400 	 *    filters marked as VLAN=0
1401 	 * c) finally, if we do not have any active VLANS, all filters
1402 	 *    which are marked as VLAN=0 must be replaced with filters
1403 	 *    marked as VLAN=-1
1404 	 */
1405 
1406 	/* Update the filters about to be added in place */
1407 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1408 		if (pvid && new->f->vlan != pvid)
1409 			new->f->vlan = pvid;
1410 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1411 			new->f->vlan = 0;
1412 		else if (!vlan_filters && new->f->vlan == 0)
1413 			new->f->vlan = I40E_VLAN_ANY;
1414 	}
1415 
1416 	/* Update the remaining active filters */
1417 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1418 		/* Combine the checks for whether a filter needs to be changed
1419 		 * and then determine the new VLAN inside the if block, in
1420 		 * order to avoid duplicating code for adding the new filter
1421 		 * then deleting the old filter.
1422 		 */
1423 		if ((pvid && f->vlan != pvid) ||
1424 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1425 		    (!vlan_filters && f->vlan == 0)) {
1426 			/* Determine the new vlan we will be adding */
1427 			if (pvid)
1428 				new_vlan = pvid;
1429 			else if (vlan_filters)
1430 				new_vlan = 0;
1431 			else
1432 				new_vlan = I40E_VLAN_ANY;
1433 
1434 			/* Create the new filter */
1435 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1436 			if (!add_head)
1437 				return -ENOMEM;
1438 
1439 			/* Create a temporary i40e_new_mac_filter */
1440 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1441 			if (!new)
1442 				return -ENOMEM;
1443 
1444 			new->f = add_head;
1445 			new->state = add_head->state;
1446 			if (add_head->state == I40E_FILTER_NEW)
1447 				add_head->state = I40E_FILTER_NEW_SYNC;
1448 
1449 			/* Add the new filter to the tmp list */
1450 			hlist_add_head(&new->hlist, tmp_add_list);
1451 
1452 			/* Put the original filter into the delete list */
1453 			f->state = I40E_FILTER_REMOVE;
1454 			hash_del(&f->hlist);
1455 			hlist_add_head(&f->hlist, tmp_del_list);
1456 		}
1457 	}
1458 
1459 	vsi->has_vlan_filter = !!vlan_filters;
1460 
1461 	return 0;
1462 }
1463 
1464 /**
1465  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1466  * @vsi: the vsi to configure
1467  * @new_mac: new mac filter to be added
1468  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1469  * @vlan_filters: the number of active VLAN filters
1470  * @trusted: flag if the VF is trusted
1471  *
1472  * Get new VLAN id based on current VLAN filters, trust, PVID
1473  * and vf-vlan-prune-disable flag.
1474  *
1475  * Returns the value of the new vlan filter or
1476  * the old value if no new filter is needed.
1477  */
1478 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1479 				struct i40e_new_mac_filter *new_mac,
1480 				struct i40e_mac_filter *f,
1481 				int vlan_filters,
1482 				bool trusted)
1483 {
1484 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1485 	struct i40e_pf *pf = vsi->back;
1486 	bool is_any;
1487 
1488 	if (new_mac)
1489 		f = new_mac->f;
1490 
1491 	if (pvid && f->vlan != pvid)
1492 		return pvid;
1493 
1494 	is_any = (trusted ||
1495 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1496 
1497 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1498 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1499 	    (is_any && !vlan_filters && f->vlan == 0)) {
1500 		if (is_any)
1501 			return I40E_VLAN_ANY;
1502 		else
1503 			return 0;
1504 	}
1505 
1506 	return f->vlan;
1507 }
1508 
1509 /**
1510  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1511  * @vsi: the vsi to configure
1512  * @tmp_add_list: list of filters ready to be added
1513  * @tmp_del_list: list of filters ready to be deleted
1514  * @vlan_filters: the number of active VLAN filters
1515  * @trusted: flag if the VF is trusted
1516  *
1517  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1518  * and vf-vlan-prune-disable flag.
1519  *
1520  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1521  *
1522  * This function is only expected to be called from within
1523  * i40e_sync_vsi_filters.
1524  *
1525  * NOTE: This function expects to be called while under the
1526  * mac_filter_hash_lock
1527  */
1528 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1529 					    struct hlist_head *tmp_add_list,
1530 					    struct hlist_head *tmp_del_list,
1531 					    int vlan_filters,
1532 					    bool trusted)
1533 {
1534 	struct i40e_mac_filter *f, *add_head;
1535 	struct i40e_new_mac_filter *new_mac;
1536 	struct hlist_node *h;
1537 	int bkt, new_vlan;
1538 
1539 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1540 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1541 							vlan_filters, trusted);
1542 	}
1543 
1544 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1545 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1546 						trusted);
1547 		if (new_vlan != f->vlan) {
1548 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1549 			if (!add_head)
1550 				return -ENOMEM;
1551 			/* Create a temporary i40e_new_mac_filter */
1552 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1553 			if (!new_mac)
1554 				return -ENOMEM;
1555 			new_mac->f = add_head;
1556 			new_mac->state = add_head->state;
1557 			if (add_head->state == I40E_FILTER_NEW)
1558 				add_head->state = I40E_FILTER_NEW_SYNC;
1559 
1560 			/* Add the new filter to the tmp list */
1561 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1562 
1563 			/* Put the original filter into the delete list */
1564 			f->state = I40E_FILTER_REMOVE;
1565 			hash_del(&f->hlist);
1566 			hlist_add_head(&f->hlist, tmp_del_list);
1567 		}
1568 	}
1569 
1570 	vsi->has_vlan_filter = !!vlan_filters;
1571 	return 0;
1572 }
1573 
1574 /**
1575  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1576  * @vsi: the PF Main VSI - inappropriate for any other VSI
1577  * @macaddr: the MAC address
1578  *
1579  * Remove whatever filter the firmware set up so the driver can manage
1580  * its own filtering intelligently.
1581  **/
1582 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1583 {
1584 	struct i40e_aqc_remove_macvlan_element_data element;
1585 	struct i40e_pf *pf = vsi->back;
1586 
1587 	/* Only appropriate for the PF main VSI */
1588 	if (vsi->type != I40E_VSI_MAIN)
1589 		return;
1590 
1591 	memset(&element, 0, sizeof(element));
1592 	ether_addr_copy(element.mac_addr, macaddr);
1593 	element.vlan_tag = 0;
1594 	/* Ignore error returns, some firmware does it this way... */
1595 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1596 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1597 
1598 	memset(&element, 0, sizeof(element));
1599 	ether_addr_copy(element.mac_addr, macaddr);
1600 	element.vlan_tag = 0;
1601 	/* ...and some firmware does it this way. */
1602 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1603 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1604 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1605 }
1606 
1607 /**
1608  * i40e_add_filter - Add a mac/vlan filter to the VSI
1609  * @vsi: the VSI to be searched
1610  * @macaddr: the MAC address
1611  * @vlan: the vlan
1612  *
1613  * Returns ptr to the filter object or NULL when no memory available.
1614  *
1615  * NOTE: This function is expected to be called with mac_filter_hash_lock
1616  * being held.
1617  **/
1618 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1619 					const u8 *macaddr, s16 vlan)
1620 {
1621 	struct i40e_mac_filter *f;
1622 	u64 key;
1623 
1624 	if (!vsi || !macaddr)
1625 		return NULL;
1626 
1627 	f = i40e_find_filter(vsi, macaddr, vlan);
1628 	if (!f) {
1629 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1630 		if (!f)
1631 			return NULL;
1632 
1633 		/* Update the boolean indicating if we need to function in
1634 		 * VLAN mode.
1635 		 */
1636 		if (vlan >= 0)
1637 			vsi->has_vlan_filter = true;
1638 
1639 		ether_addr_copy(f->macaddr, macaddr);
1640 		f->vlan = vlan;
1641 		f->state = I40E_FILTER_NEW;
1642 		INIT_HLIST_NODE(&f->hlist);
1643 
1644 		key = i40e_addr_to_hkey(macaddr);
1645 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1646 
1647 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1648 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1649 	}
1650 
1651 	/* If we're asked to add a filter that has been marked for removal, it
1652 	 * is safe to simply restore it to active state. __i40e_del_filter
1653 	 * will have simply deleted any filters which were previously marked
1654 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1655 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1656 	 * task, just restore this filter to the ACTIVE state so that the
1657 	 * sync task leaves it in place
1658 	 */
1659 	if (f->state == I40E_FILTER_REMOVE)
1660 		f->state = I40E_FILTER_ACTIVE;
1661 
1662 	return f;
1663 }
1664 
1665 /**
1666  * __i40e_del_filter - Remove a specific filter from the VSI
1667  * @vsi: VSI to remove from
1668  * @f: the filter to remove from the list
1669  *
1670  * This function requires you've found * the exact filter you will remove
1671  * already, such as via i40e_find_filter or i40e_find_mac.
1672  *
1673  * NOTE: This function is expected to be called with mac_filter_hash_lock
1674  * being held.
1675  * ANOTHER NOTE: This function MUST be called from within the context of
1676  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1677  * instead of list_for_each_entry().
1678  **/
1679 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1680 {
1681 	if (!f)
1682 		return;
1683 
1684 	/* If the filter was never added to firmware then we can just delete it
1685 	 * directly and we don't want to set the status to remove or else an
1686 	 * admin queue command will unnecessarily fire.
1687 	 */
1688 	if ((f->state == I40E_FILTER_FAILED) ||
1689 	    (f->state == I40E_FILTER_NEW)) {
1690 		hash_del(&f->hlist);
1691 		kfree(f);
1692 	} else {
1693 		f->state = I40E_FILTER_REMOVE;
1694 	}
1695 
1696 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1697 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1698 }
1699 
1700 /**
1701  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1702  * @vsi: the VSI to be searched
1703  * @macaddr: the mac address to be filtered
1704  *
1705  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1706  * go through all the macvlan filters and add a macvlan filter for each
1707  * unique vlan that already exists. If a PVID has been assigned, instead only
1708  * add the macaddr to that VLAN.
1709  *
1710  * Returns last filter added on success, else NULL
1711  **/
1712 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1713 					    const u8 *macaddr)
1714 {
1715 	struct i40e_mac_filter *f, *add = NULL;
1716 	struct hlist_node *h;
1717 	int bkt;
1718 
1719 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1720 	if (vsi->info.pvid)
1721 		return i40e_add_filter(vsi, macaddr,
1722 				       le16_to_cpu(vsi->info.pvid));
1723 
1724 	if (!i40e_is_vsi_in_vlan(vsi))
1725 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1726 
1727 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1728 		if (f->state == I40E_FILTER_REMOVE)
1729 			continue;
1730 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1731 		if (!add)
1732 			return NULL;
1733 	}
1734 
1735 	return add;
1736 }
1737 
1738 /**
1739  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1740  * @vsi: the VSI to be searched
1741  * @macaddr: the mac address to be removed
1742  *
1743  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1744  * associated with.
1745  *
1746  * Returns 0 for success, or error
1747  **/
1748 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1749 {
1750 	struct i40e_mac_filter *f;
1751 	struct hlist_node *h;
1752 	bool found = false;
1753 	int bkt;
1754 
1755 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1756 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1757 		if (ether_addr_equal(macaddr, f->macaddr)) {
1758 			__i40e_del_filter(vsi, f);
1759 			found = true;
1760 		}
1761 	}
1762 
1763 	if (found)
1764 		return 0;
1765 	else
1766 		return -ENOENT;
1767 }
1768 
1769 /**
1770  * i40e_set_mac - NDO callback to set mac address
1771  * @netdev: network interface device structure
1772  * @p: pointer to an address structure
1773  *
1774  * Returns 0 on success, negative on failure
1775  **/
1776 static int i40e_set_mac(struct net_device *netdev, void *p)
1777 {
1778 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1779 	struct i40e_vsi *vsi = np->vsi;
1780 	struct i40e_pf *pf = vsi->back;
1781 	struct i40e_hw *hw = &pf->hw;
1782 	struct sockaddr *addr = p;
1783 
1784 	if (!is_valid_ether_addr(addr->sa_data))
1785 		return -EADDRNOTAVAIL;
1786 
1787 	if (test_bit(__I40E_DOWN, pf->state) ||
1788 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1789 		return -EADDRNOTAVAIL;
1790 
1791 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1792 		netdev_info(netdev, "returning to hw mac address %pM\n",
1793 			    hw->mac.addr);
1794 	else
1795 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1796 
1797 	/* Copy the address first, so that we avoid a possible race with
1798 	 * .set_rx_mode().
1799 	 * - Remove old address from MAC filter
1800 	 * - Copy new address
1801 	 * - Add new address to MAC filter
1802 	 */
1803 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1804 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1805 	eth_hw_addr_set(netdev, addr->sa_data);
1806 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1807 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1808 
1809 	if (vsi->type == I40E_VSI_MAIN) {
1810 		int ret;
1811 
1812 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1813 						addr->sa_data, NULL);
1814 		if (ret)
1815 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1816 				    ERR_PTR(ret),
1817 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1818 	}
1819 
1820 	/* schedule our worker thread which will take care of
1821 	 * applying the new filter changes
1822 	 */
1823 	i40e_service_event_schedule(pf);
1824 	return 0;
1825 }
1826 
1827 /**
1828  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1829  * @vsi: vsi structure
1830  * @seed: RSS hash seed
1831  * @lut: pointer to lookup table of lut_size
1832  * @lut_size: size of the lookup table
1833  **/
1834 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1835 			      u8 *lut, u16 lut_size)
1836 {
1837 	struct i40e_pf *pf = vsi->back;
1838 	struct i40e_hw *hw = &pf->hw;
1839 	int ret = 0;
1840 
1841 	if (seed) {
1842 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1843 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1844 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1845 		if (ret) {
1846 			dev_info(&pf->pdev->dev,
1847 				 "Cannot set RSS key, err %pe aq_err %s\n",
1848 				 ERR_PTR(ret),
1849 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1850 			return ret;
1851 		}
1852 	}
1853 	if (lut) {
1854 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1855 
1856 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1857 		if (ret) {
1858 			dev_info(&pf->pdev->dev,
1859 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1860 				 ERR_PTR(ret),
1861 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1862 			return ret;
1863 		}
1864 	}
1865 	return ret;
1866 }
1867 
1868 /**
1869  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1870  * @vsi: VSI structure
1871  **/
1872 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1873 {
1874 	struct i40e_pf *pf = vsi->back;
1875 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1876 	u8 *lut;
1877 	int ret;
1878 
1879 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1880 		return 0;
1881 	if (!vsi->rss_size)
1882 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1883 				      vsi->num_queue_pairs);
1884 	if (!vsi->rss_size)
1885 		return -EINVAL;
1886 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1887 	if (!lut)
1888 		return -ENOMEM;
1889 
1890 	/* Use the user configured hash keys and lookup table if there is one,
1891 	 * otherwise use default
1892 	 */
1893 	if (vsi->rss_lut_user)
1894 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1895 	else
1896 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1897 	if (vsi->rss_hkey_user)
1898 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1899 	else
1900 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1901 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1902 	kfree(lut);
1903 	return ret;
1904 }
1905 
1906 /**
1907  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1908  * @vsi: the VSI being configured,
1909  * @ctxt: VSI context structure
1910  * @enabled_tc: number of traffic classes to enable
1911  *
1912  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1913  **/
1914 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1915 					   struct i40e_vsi_context *ctxt,
1916 					   u8 enabled_tc)
1917 {
1918 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1919 	int i, override_q, pow, num_qps, ret;
1920 	u8 netdev_tc = 0, offset = 0;
1921 
1922 	if (vsi->type != I40E_VSI_MAIN)
1923 		return -EINVAL;
1924 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1925 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1926 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1927 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1928 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1929 
1930 	/* find the next higher power-of-2 of num queue pairs */
1931 	pow = ilog2(num_qps);
1932 	if (!is_power_of_2(num_qps))
1933 		pow++;
1934 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1935 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1936 
1937 	/* Setup queue offset/count for all TCs for given VSI */
1938 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1939 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1940 		/* See if the given TC is enabled for the given VSI */
1941 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1942 			offset = vsi->mqprio_qopt.qopt.offset[i];
1943 			qcount = vsi->mqprio_qopt.qopt.count[i];
1944 			if (qcount > max_qcount)
1945 				max_qcount = qcount;
1946 			vsi->tc_config.tc_info[i].qoffset = offset;
1947 			vsi->tc_config.tc_info[i].qcount = qcount;
1948 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1949 		} else {
1950 			/* TC is not enabled so set the offset to
1951 			 * default queue and allocate one queue
1952 			 * for the given TC.
1953 			 */
1954 			vsi->tc_config.tc_info[i].qoffset = 0;
1955 			vsi->tc_config.tc_info[i].qcount = 1;
1956 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1957 		}
1958 	}
1959 
1960 	/* Set actual Tx/Rx queue pairs */
1961 	vsi->num_queue_pairs = offset + qcount;
1962 
1963 	/* Setup queue TC[0].qmap for given VSI context */
1964 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1965 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1966 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1967 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1968 
1969 	/* Reconfigure RSS for main VSI with max queue count */
1970 	vsi->rss_size = max_qcount;
1971 	ret = i40e_vsi_config_rss(vsi);
1972 	if (ret) {
1973 		dev_info(&vsi->back->pdev->dev,
1974 			 "Failed to reconfig rss for num_queues (%u)\n",
1975 			 max_qcount);
1976 		return ret;
1977 	}
1978 	vsi->reconfig_rss = true;
1979 	dev_dbg(&vsi->back->pdev->dev,
1980 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1981 
1982 	/* Find queue count available for channel VSIs and starting offset
1983 	 * for channel VSIs
1984 	 */
1985 	override_q = vsi->mqprio_qopt.qopt.count[0];
1986 	if (override_q && override_q < vsi->num_queue_pairs) {
1987 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1988 		vsi->next_base_queue = override_q;
1989 	}
1990 	return 0;
1991 }
1992 
1993 /**
1994  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1995  * @vsi: the VSI being setup
1996  * @ctxt: VSI context structure
1997  * @enabled_tc: Enabled TCs bitmap
1998  * @is_add: True if called before Add VSI
1999  *
2000  * Setup VSI queue mapping for enabled traffic classes.
2001  **/
2002 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2003 				     struct i40e_vsi_context *ctxt,
2004 				     u8 enabled_tc,
2005 				     bool is_add)
2006 {
2007 	struct i40e_pf *pf = vsi->back;
2008 	u16 num_tc_qps = 0;
2009 	u16 sections = 0;
2010 	u8 netdev_tc = 0;
2011 	u16 numtc = 1;
2012 	u16 qcount;
2013 	u8 offset;
2014 	u16 qmap;
2015 	int i;
2016 
2017 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2018 	offset = 0;
2019 	/* zero out queue mapping, it will get updated on the end of the function */
2020 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2021 
2022 	if (vsi->type == I40E_VSI_MAIN) {
2023 		/* This code helps add more queue to the VSI if we have
2024 		 * more cores than RSS can support, the higher cores will
2025 		 * be served by ATR or other filters. Furthermore, the
2026 		 * non-zero req_queue_pairs says that user requested a new
2027 		 * queue count via ethtool's set_channels, so use this
2028 		 * value for queues distribution across traffic classes
2029 		 * We need at least one queue pair for the interface
2030 		 * to be usable as we see in else statement.
2031 		 */
2032 		if (vsi->req_queue_pairs > 0)
2033 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2034 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2035 			vsi->num_queue_pairs = pf->num_lan_msix;
2036 		else
2037 			vsi->num_queue_pairs = 1;
2038 	}
2039 
2040 	/* Number of queues per enabled TC */
2041 	if (vsi->type == I40E_VSI_MAIN ||
2042 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2043 		num_tc_qps = vsi->num_queue_pairs;
2044 	else
2045 		num_tc_qps = vsi->alloc_queue_pairs;
2046 
2047 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2048 		/* Find numtc from enabled TC bitmap */
2049 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2050 			if (enabled_tc & BIT(i)) /* TC is enabled */
2051 				numtc++;
2052 		}
2053 		if (!numtc) {
2054 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2055 			numtc = 1;
2056 		}
2057 		num_tc_qps = num_tc_qps / numtc;
2058 		num_tc_qps = min_t(int, num_tc_qps,
2059 				   i40e_pf_get_max_q_per_tc(pf));
2060 	}
2061 
2062 	vsi->tc_config.numtc = numtc;
2063 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2064 
2065 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2066 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2067 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2068 
2069 	/* Setup queue offset/count for all TCs for given VSI */
2070 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2071 		/* See if the given TC is enabled for the given VSI */
2072 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2073 			/* TC is enabled */
2074 			int pow, num_qps;
2075 
2076 			switch (vsi->type) {
2077 			case I40E_VSI_MAIN:
2078 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2079 					       pf->flags) &&
2080 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2081 					       pf->flags)) ||
2082 				    vsi->tc_config.enabled_tc != 1) {
2083 					qcount = min_t(int, pf->alloc_rss_size,
2084 						       num_tc_qps);
2085 					break;
2086 				}
2087 				fallthrough;
2088 			case I40E_VSI_FDIR:
2089 			case I40E_VSI_SRIOV:
2090 			case I40E_VSI_VMDQ2:
2091 			default:
2092 				qcount = num_tc_qps;
2093 				WARN_ON(i != 0);
2094 				break;
2095 			}
2096 			vsi->tc_config.tc_info[i].qoffset = offset;
2097 			vsi->tc_config.tc_info[i].qcount = qcount;
2098 
2099 			/* find the next higher power-of-2 of num queue pairs */
2100 			num_qps = qcount;
2101 			pow = 0;
2102 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2103 				pow++;
2104 				num_qps >>= 1;
2105 			}
2106 
2107 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2108 			qmap =
2109 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2110 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2111 
2112 			offset += qcount;
2113 		} else {
2114 			/* TC is not enabled so set the offset to
2115 			 * default queue and allocate one queue
2116 			 * for the given TC.
2117 			 */
2118 			vsi->tc_config.tc_info[i].qoffset = 0;
2119 			vsi->tc_config.tc_info[i].qcount = 1;
2120 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2121 
2122 			qmap = 0;
2123 		}
2124 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2125 	}
2126 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2127 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2128 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2129 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2130 		vsi->num_queue_pairs = offset;
2131 
2132 	/* Scheduler section valid can only be set for ADD VSI */
2133 	if (is_add) {
2134 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2135 
2136 		ctxt->info.up_enable_bits = enabled_tc;
2137 	}
2138 	if (vsi->type == I40E_VSI_SRIOV) {
2139 		ctxt->info.mapping_flags |=
2140 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2141 		for (i = 0; i < vsi->num_queue_pairs; i++)
2142 			ctxt->info.queue_mapping[i] =
2143 					       cpu_to_le16(vsi->base_queue + i);
2144 	} else {
2145 		ctxt->info.mapping_flags |=
2146 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2147 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2148 	}
2149 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2150 }
2151 
2152 /**
2153  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2154  * @netdev: the netdevice
2155  * @addr: address to add
2156  *
2157  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2158  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2159  */
2160 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2161 {
2162 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2163 	struct i40e_vsi *vsi = np->vsi;
2164 
2165 	if (i40e_add_mac_filter(vsi, addr))
2166 		return 0;
2167 	else
2168 		return -ENOMEM;
2169 }
2170 
2171 /**
2172  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2173  * @netdev: the netdevice
2174  * @addr: address to add
2175  *
2176  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2177  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2178  */
2179 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2180 {
2181 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2182 	struct i40e_vsi *vsi = np->vsi;
2183 
2184 	/* Under some circumstances, we might receive a request to delete
2185 	 * our own device address from our uc list. Because we store the
2186 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2187 	 * such requests and not delete our device address from this list.
2188 	 */
2189 	if (ether_addr_equal(addr, netdev->dev_addr))
2190 		return 0;
2191 
2192 	i40e_del_mac_filter(vsi, addr);
2193 
2194 	return 0;
2195 }
2196 
2197 /**
2198  * i40e_set_rx_mode - NDO callback to set the netdev filters
2199  * @netdev: network interface device structure
2200  **/
2201 static void i40e_set_rx_mode(struct net_device *netdev)
2202 {
2203 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2204 	struct i40e_vsi *vsi = np->vsi;
2205 
2206 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2207 
2208 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2209 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2210 
2211 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2212 
2213 	/* check for other flag changes */
2214 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2215 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2216 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2217 	}
2218 }
2219 
2220 /**
2221  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2222  * @vsi: Pointer to VSI struct
2223  * @from: Pointer to list which contains MAC filter entries - changes to
2224  *        those entries needs to be undone.
2225  *
2226  * MAC filter entries from this list were slated for deletion.
2227  **/
2228 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2229 					 struct hlist_head *from)
2230 {
2231 	struct i40e_mac_filter *f;
2232 	struct hlist_node *h;
2233 
2234 	hlist_for_each_entry_safe(f, h, from, hlist) {
2235 		u64 key = i40e_addr_to_hkey(f->macaddr);
2236 
2237 		/* Move the element back into MAC filter list*/
2238 		hlist_del(&f->hlist);
2239 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2240 	}
2241 }
2242 
2243 /**
2244  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2245  * @vsi: Pointer to vsi struct
2246  * @from: Pointer to list which contains MAC filter entries - changes to
2247  *        those entries needs to be undone.
2248  *
2249  * MAC filter entries from this list were slated for addition.
2250  **/
2251 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2252 					 struct hlist_head *from)
2253 {
2254 	struct i40e_new_mac_filter *new;
2255 	struct hlist_node *h;
2256 
2257 	hlist_for_each_entry_safe(new, h, from, hlist) {
2258 		/* We can simply free the wrapper structure */
2259 		hlist_del(&new->hlist);
2260 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2261 		kfree(new);
2262 	}
2263 }
2264 
2265 /**
2266  * i40e_next_filter - Get the next non-broadcast filter from a list
2267  * @next: pointer to filter in list
2268  *
2269  * Returns the next non-broadcast filter in the list. Required so that we
2270  * ignore broadcast filters within the list, since these are not handled via
2271  * the normal firmware update path.
2272  */
2273 static
2274 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2275 {
2276 	hlist_for_each_entry_continue(next, hlist) {
2277 		if (!is_broadcast_ether_addr(next->f->macaddr))
2278 			return next;
2279 	}
2280 
2281 	return NULL;
2282 }
2283 
2284 /**
2285  * i40e_update_filter_state - Update filter state based on return data
2286  * from firmware
2287  * @count: Number of filters added
2288  * @add_list: return data from fw
2289  * @add_head: pointer to first filter in current batch
2290  *
2291  * MAC filter entries from list were slated to be added to device. Returns
2292  * number of successful filters. Note that 0 does NOT mean success!
2293  **/
2294 static int
2295 i40e_update_filter_state(int count,
2296 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2297 			 struct i40e_new_mac_filter *add_head)
2298 {
2299 	int retval = 0;
2300 	int i;
2301 
2302 	for (i = 0; i < count; i++) {
2303 		/* Always check status of each filter. We don't need to check
2304 		 * the firmware return status because we pre-set the filter
2305 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2306 		 * request to the adminq. Thus, if it no longer matches then
2307 		 * we know the filter is active.
2308 		 */
2309 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2310 			add_head->state = I40E_FILTER_FAILED;
2311 		} else {
2312 			add_head->state = I40E_FILTER_ACTIVE;
2313 			retval++;
2314 		}
2315 
2316 		add_head = i40e_next_filter(add_head);
2317 		if (!add_head)
2318 			break;
2319 	}
2320 
2321 	return retval;
2322 }
2323 
2324 /**
2325  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2326  * @vsi: ptr to the VSI
2327  * @vsi_name: name to display in messages
2328  * @list: the list of filters to send to firmware
2329  * @num_del: the number of filters to delete
2330  * @retval: Set to -EIO on failure to delete
2331  *
2332  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2333  * *retval instead of a return value so that success does not force ret_val to
2334  * be set to 0. This ensures that a sequence of calls to this function
2335  * preserve the previous value of *retval on successful delete.
2336  */
2337 static
2338 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2339 			  struct i40e_aqc_remove_macvlan_element_data *list,
2340 			  int num_del, int *retval)
2341 {
2342 	struct i40e_hw *hw = &vsi->back->hw;
2343 	enum i40e_admin_queue_err aq_status;
2344 	int aq_ret;
2345 
2346 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2347 					   &aq_status);
2348 
2349 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2350 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2351 		*retval = -EIO;
2352 		dev_info(&vsi->back->pdev->dev,
2353 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2354 			 vsi_name, ERR_PTR(aq_ret),
2355 			 i40e_aq_str(hw, aq_status));
2356 	}
2357 }
2358 
2359 /**
2360  * i40e_aqc_add_filters - Request firmware to add a set of filters
2361  * @vsi: ptr to the VSI
2362  * @vsi_name: name to display in messages
2363  * @list: the list of filters to send to firmware
2364  * @add_head: Position in the add hlist
2365  * @num_add: the number of filters to add
2366  *
2367  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2368  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2369  * space for more filters.
2370  */
2371 static
2372 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2373 			  struct i40e_aqc_add_macvlan_element_data *list,
2374 			  struct i40e_new_mac_filter *add_head,
2375 			  int num_add)
2376 {
2377 	struct i40e_hw *hw = &vsi->back->hw;
2378 	enum i40e_admin_queue_err aq_status;
2379 	int fcnt;
2380 
2381 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2382 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2383 
2384 	if (fcnt != num_add) {
2385 		if (vsi->type == I40E_VSI_MAIN) {
2386 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2387 			dev_warn(&vsi->back->pdev->dev,
2388 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2389 				 i40e_aq_str(hw, aq_status), vsi_name);
2390 		} else if (vsi->type == I40E_VSI_SRIOV ||
2391 			   vsi->type == I40E_VSI_VMDQ1 ||
2392 			   vsi->type == I40E_VSI_VMDQ2) {
2393 			dev_warn(&vsi->back->pdev->dev,
2394 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2395 				 i40e_aq_str(hw, aq_status), vsi_name,
2396 					     vsi_name);
2397 		} else {
2398 			dev_warn(&vsi->back->pdev->dev,
2399 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2400 				 i40e_aq_str(hw, aq_status), vsi_name,
2401 					     vsi->type);
2402 		}
2403 	}
2404 }
2405 
2406 /**
2407  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2408  * @vsi: pointer to the VSI
2409  * @vsi_name: the VSI name
2410  * @f: filter data
2411  *
2412  * This function sets or clears the promiscuous broadcast flags for VLAN
2413  * filters in order to properly receive broadcast frames. Assumes that only
2414  * broadcast filters are passed.
2415  *
2416  * Returns status indicating success or failure;
2417  **/
2418 static int
2419 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2420 			  struct i40e_mac_filter *f)
2421 {
2422 	bool enable = f->state == I40E_FILTER_NEW ||
2423 		      f->state == I40E_FILTER_NEW_SYNC;
2424 	struct i40e_hw *hw = &vsi->back->hw;
2425 	int aq_ret;
2426 
2427 	if (f->vlan == I40E_VLAN_ANY) {
2428 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2429 						   vsi->seid,
2430 						   enable,
2431 						   NULL);
2432 	} else {
2433 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2434 							    vsi->seid,
2435 							    enable,
2436 							    f->vlan,
2437 							    NULL);
2438 	}
2439 
2440 	if (aq_ret) {
2441 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2442 		dev_warn(&vsi->back->pdev->dev,
2443 			 "Error %s, forcing overflow promiscuous on %s\n",
2444 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2445 			 vsi_name);
2446 	}
2447 
2448 	return aq_ret;
2449 }
2450 
2451 /**
2452  * i40e_set_promiscuous - set promiscuous mode
2453  * @pf: board private structure
2454  * @promisc: promisc on or off
2455  *
2456  * There are different ways of setting promiscuous mode on a PF depending on
2457  * what state/environment we're in.  This identifies and sets it appropriately.
2458  * Returns 0 on success.
2459  **/
2460 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2461 {
2462 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
2463 	struct i40e_hw *hw = &pf->hw;
2464 	int aq_ret;
2465 
2466 	if (vsi->type == I40E_VSI_MAIN &&
2467 	    i40e_pf_get_main_veb(pf) &&
2468 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2469 		/* set defport ON for Main VSI instead of true promisc
2470 		 * this way we will get all unicast/multicast and VLAN
2471 		 * promisc behavior but will not get VF or VMDq traffic
2472 		 * replicated on the Main VSI.
2473 		 */
2474 		if (promisc)
2475 			aq_ret = i40e_aq_set_default_vsi(hw,
2476 							 vsi->seid,
2477 							 NULL);
2478 		else
2479 			aq_ret = i40e_aq_clear_default_vsi(hw,
2480 							   vsi->seid,
2481 							   NULL);
2482 		if (aq_ret) {
2483 			dev_info(&pf->pdev->dev,
2484 				 "Set default VSI failed, err %pe, aq_err %s\n",
2485 				 ERR_PTR(aq_ret),
2486 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2487 		}
2488 	} else {
2489 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2490 						  hw,
2491 						  vsi->seid,
2492 						  promisc, NULL,
2493 						  true);
2494 		if (aq_ret) {
2495 			dev_info(&pf->pdev->dev,
2496 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2497 				 ERR_PTR(aq_ret),
2498 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2499 		}
2500 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2501 						  hw,
2502 						  vsi->seid,
2503 						  promisc, NULL);
2504 		if (aq_ret) {
2505 			dev_info(&pf->pdev->dev,
2506 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2507 				 ERR_PTR(aq_ret),
2508 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2509 		}
2510 	}
2511 
2512 	if (!aq_ret)
2513 		pf->cur_promisc = promisc;
2514 
2515 	return aq_ret;
2516 }
2517 
2518 /**
2519  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2520  * @vsi: ptr to the VSI
2521  *
2522  * Push any outstanding VSI filter changes through the AdminQ.
2523  *
2524  * Returns 0 or error value
2525  **/
2526 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2527 {
2528 	struct hlist_head tmp_add_list, tmp_del_list;
2529 	struct i40e_mac_filter *f;
2530 	struct i40e_new_mac_filter *new, *add_head = NULL;
2531 	struct i40e_hw *hw = &vsi->back->hw;
2532 	bool old_overflow, new_overflow;
2533 	unsigned int failed_filters = 0;
2534 	unsigned int vlan_filters = 0;
2535 	char vsi_name[16] = "PF";
2536 	int filter_list_len = 0;
2537 	u32 changed_flags = 0;
2538 	struct hlist_node *h;
2539 	struct i40e_pf *pf;
2540 	int num_add = 0;
2541 	int num_del = 0;
2542 	int aq_ret = 0;
2543 	int retval = 0;
2544 	u16 cmd_flags;
2545 	int list_size;
2546 	int bkt;
2547 
2548 	/* empty array typed pointers, kcalloc later */
2549 	struct i40e_aqc_add_macvlan_element_data *add_list;
2550 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2551 
2552 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2553 		usleep_range(1000, 2000);
2554 	pf = vsi->back;
2555 
2556 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2557 
2558 	if (vsi->netdev) {
2559 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2560 		vsi->current_netdev_flags = vsi->netdev->flags;
2561 	}
2562 
2563 	INIT_HLIST_HEAD(&tmp_add_list);
2564 	INIT_HLIST_HEAD(&tmp_del_list);
2565 
2566 	if (vsi->type == I40E_VSI_SRIOV)
2567 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2568 	else if (vsi->type != I40E_VSI_MAIN)
2569 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2570 
2571 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2572 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2573 
2574 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2575 		/* Create a list of filters to delete. */
2576 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2577 			if (f->state == I40E_FILTER_REMOVE) {
2578 				/* Move the element into temporary del_list */
2579 				hash_del(&f->hlist);
2580 				hlist_add_head(&f->hlist, &tmp_del_list);
2581 
2582 				/* Avoid counting removed filters */
2583 				continue;
2584 			}
2585 			if (f->state == I40E_FILTER_NEW) {
2586 				/* Create a temporary i40e_new_mac_filter */
2587 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2588 				if (!new)
2589 					goto err_no_memory_locked;
2590 
2591 				/* Store pointer to the real filter */
2592 				new->f = f;
2593 				new->state = f->state;
2594 
2595 				/* Add it to the hash list */
2596 				hlist_add_head(&new->hlist, &tmp_add_list);
2597 				f->state = I40E_FILTER_NEW_SYNC;
2598 			}
2599 
2600 			/* Count the number of active (current and new) VLAN
2601 			 * filters we have now. Does not count filters which
2602 			 * are marked for deletion.
2603 			 */
2604 			if (f->vlan > 0)
2605 				vlan_filters++;
2606 		}
2607 
2608 		if (vsi->type != I40E_VSI_SRIOV)
2609 			retval = i40e_correct_mac_vlan_filters
2610 				(vsi, &tmp_add_list, &tmp_del_list,
2611 				 vlan_filters);
2612 		else if (pf->vf)
2613 			retval = i40e_correct_vf_mac_vlan_filters
2614 				(vsi, &tmp_add_list, &tmp_del_list,
2615 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2616 
2617 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2618 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2619 
2620 		if (retval)
2621 			goto err_no_memory_locked;
2622 
2623 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2624 	}
2625 
2626 	/* Now process 'del_list' outside the lock */
2627 	if (!hlist_empty(&tmp_del_list)) {
2628 		filter_list_len = hw->aq.asq_buf_size /
2629 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2630 		list_size = filter_list_len *
2631 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2632 		del_list = kzalloc(list_size, GFP_ATOMIC);
2633 		if (!del_list)
2634 			goto err_no_memory;
2635 
2636 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2637 			cmd_flags = 0;
2638 
2639 			/* handle broadcast filters by updating the broadcast
2640 			 * promiscuous flag and release filter list.
2641 			 */
2642 			if (is_broadcast_ether_addr(f->macaddr)) {
2643 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2644 
2645 				hlist_del(&f->hlist);
2646 				kfree(f);
2647 				continue;
2648 			}
2649 
2650 			/* add to delete list */
2651 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2652 			if (f->vlan == I40E_VLAN_ANY) {
2653 				del_list[num_del].vlan_tag = 0;
2654 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2655 			} else {
2656 				del_list[num_del].vlan_tag =
2657 					cpu_to_le16((u16)(f->vlan));
2658 			}
2659 
2660 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2661 			del_list[num_del].flags = cmd_flags;
2662 			num_del++;
2663 
2664 			/* flush a full buffer */
2665 			if (num_del == filter_list_len) {
2666 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2667 						     num_del, &retval);
2668 				memset(del_list, 0, list_size);
2669 				num_del = 0;
2670 			}
2671 			/* Release memory for MAC filter entries which were
2672 			 * synced up with HW.
2673 			 */
2674 			hlist_del(&f->hlist);
2675 			kfree(f);
2676 		}
2677 
2678 		if (num_del) {
2679 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2680 					     num_del, &retval);
2681 		}
2682 
2683 		kfree(del_list);
2684 		del_list = NULL;
2685 	}
2686 
2687 	if (!hlist_empty(&tmp_add_list)) {
2688 		/* Do all the adds now. */
2689 		filter_list_len = hw->aq.asq_buf_size /
2690 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2691 		list_size = filter_list_len *
2692 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2693 		add_list = kzalloc(list_size, GFP_ATOMIC);
2694 		if (!add_list)
2695 			goto err_no_memory;
2696 
2697 		num_add = 0;
2698 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2699 			/* handle broadcast filters by updating the broadcast
2700 			 * promiscuous flag instead of adding a MAC filter.
2701 			 */
2702 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2703 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2704 							      new->f))
2705 					new->state = I40E_FILTER_FAILED;
2706 				else
2707 					new->state = I40E_FILTER_ACTIVE;
2708 				continue;
2709 			}
2710 
2711 			/* add to add array */
2712 			if (num_add == 0)
2713 				add_head = new;
2714 			cmd_flags = 0;
2715 			ether_addr_copy(add_list[num_add].mac_addr,
2716 					new->f->macaddr);
2717 			if (new->f->vlan == I40E_VLAN_ANY) {
2718 				add_list[num_add].vlan_tag = 0;
2719 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2720 			} else {
2721 				add_list[num_add].vlan_tag =
2722 					cpu_to_le16((u16)(new->f->vlan));
2723 			}
2724 			add_list[num_add].queue_number = 0;
2725 			/* set invalid match method for later detection */
2726 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2727 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2728 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2729 			num_add++;
2730 
2731 			/* flush a full buffer */
2732 			if (num_add == filter_list_len) {
2733 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2734 						     add_head, num_add);
2735 				memset(add_list, 0, list_size);
2736 				num_add = 0;
2737 			}
2738 		}
2739 		if (num_add) {
2740 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2741 					     num_add);
2742 		}
2743 		/* Now move all of the filters from the temp add list back to
2744 		 * the VSI's list.
2745 		 */
2746 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2747 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2748 			/* Only update the state if we're still NEW */
2749 			if (new->f->state == I40E_FILTER_NEW ||
2750 			    new->f->state == I40E_FILTER_NEW_SYNC)
2751 				new->f->state = new->state;
2752 			hlist_del(&new->hlist);
2753 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2754 			kfree(new);
2755 		}
2756 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2757 		kfree(add_list);
2758 		add_list = NULL;
2759 	}
2760 
2761 	/* Determine the number of active and failed filters. */
2762 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2763 	vsi->active_filters = 0;
2764 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2765 		if (f->state == I40E_FILTER_ACTIVE)
2766 			vsi->active_filters++;
2767 		else if (f->state == I40E_FILTER_FAILED)
2768 			failed_filters++;
2769 	}
2770 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2771 
2772 	/* Check if we are able to exit overflow promiscuous mode. We can
2773 	 * safely exit if we didn't just enter, we no longer have any failed
2774 	 * filters, and we have reduced filters below the threshold value.
2775 	 */
2776 	if (old_overflow && !failed_filters &&
2777 	    vsi->active_filters < vsi->promisc_threshold) {
2778 		dev_info(&pf->pdev->dev,
2779 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2780 			 vsi_name);
2781 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2782 		vsi->promisc_threshold = 0;
2783 	}
2784 
2785 	/* if the VF is not trusted do not do promisc */
2786 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2787 	    !pf->vf[vsi->vf_id].trusted) {
2788 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2789 		goto out;
2790 	}
2791 
2792 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2793 
2794 	/* If we are entering overflow promiscuous, we need to calculate a new
2795 	 * threshold for when we are safe to exit
2796 	 */
2797 	if (!old_overflow && new_overflow)
2798 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2799 
2800 	/* check for changes in promiscuous modes */
2801 	if (changed_flags & IFF_ALLMULTI) {
2802 		bool cur_multipromisc;
2803 
2804 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2805 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2806 							       vsi->seid,
2807 							       cur_multipromisc,
2808 							       NULL);
2809 		if (aq_ret) {
2810 			retval = i40e_aq_rc_to_posix(aq_ret,
2811 						     hw->aq.asq_last_status);
2812 			dev_info(&pf->pdev->dev,
2813 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2814 				 vsi_name,
2815 				 ERR_PTR(aq_ret),
2816 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2817 		} else {
2818 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2819 				 cur_multipromisc ? "entering" : "leaving");
2820 		}
2821 	}
2822 
2823 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2824 		bool cur_promisc;
2825 
2826 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2827 			       new_overflow);
2828 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2829 		if (aq_ret) {
2830 			retval = i40e_aq_rc_to_posix(aq_ret,
2831 						     hw->aq.asq_last_status);
2832 			dev_info(&pf->pdev->dev,
2833 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2834 				 cur_promisc ? "on" : "off",
2835 				 vsi_name,
2836 				 ERR_PTR(aq_ret),
2837 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2838 		}
2839 	}
2840 out:
2841 	/* if something went wrong then set the changed flag so we try again */
2842 	if (retval)
2843 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2844 
2845 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2846 	return retval;
2847 
2848 err_no_memory:
2849 	/* Restore elements on the temporary add and delete lists */
2850 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2851 err_no_memory_locked:
2852 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2853 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2854 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2855 
2856 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2857 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2858 	return -ENOMEM;
2859 }
2860 
2861 /**
2862  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2863  * @pf: board private structure
2864  **/
2865 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2866 {
2867 	struct i40e_vsi *vsi;
2868 	int v;
2869 
2870 	if (!pf)
2871 		return;
2872 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2873 		return;
2874 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2875 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2876 		return;
2877 	}
2878 
2879 	i40e_pf_for_each_vsi(pf, v, vsi) {
2880 		if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2881 		    !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
2882 			int ret = i40e_sync_vsi_filters(vsi);
2883 
2884 			if (ret) {
2885 				/* come back and try again later */
2886 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2887 					pf->state);
2888 				break;
2889 			}
2890 		}
2891 	}
2892 }
2893 
2894 /**
2895  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2896  *
2897  * @vsi: VSI to calculate rx_buf_len from
2898  */
2899 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2900 {
2901 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2902 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2903 
2904 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2905 }
2906 
2907 /**
2908  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2909  * @vsi: the vsi
2910  * @xdp_prog: XDP program
2911  **/
2912 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2913 				   struct bpf_prog *xdp_prog)
2914 {
2915 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2916 	u16 chain_len;
2917 
2918 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2919 		chain_len = 1;
2920 	else
2921 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2922 
2923 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2924 }
2925 
2926 /**
2927  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2928  * @netdev: network interface device structure
2929  * @new_mtu: new value for maximum frame size
2930  *
2931  * Returns 0 on success, negative on failure
2932  **/
2933 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2934 {
2935 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2936 	struct i40e_vsi *vsi = np->vsi;
2937 	struct i40e_pf *pf = vsi->back;
2938 	int frame_size;
2939 
2940 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2941 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2942 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2943 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2944 		return -EINVAL;
2945 	}
2946 
2947 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2948 		   netdev->mtu, new_mtu);
2949 	WRITE_ONCE(netdev->mtu, new_mtu);
2950 	if (netif_running(netdev))
2951 		i40e_vsi_reinit_locked(vsi);
2952 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2953 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2954 	return 0;
2955 }
2956 
2957 /**
2958  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2959  * @vsi: the vsi being adjusted
2960  **/
2961 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2962 {
2963 	struct i40e_vsi_context ctxt;
2964 	int ret;
2965 
2966 	/* Don't modify stripping options if a port VLAN is active */
2967 	if (vsi->info.pvid)
2968 		return;
2969 
2970 	if ((vsi->info.valid_sections &
2971 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2972 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2973 		return;  /* already enabled */
2974 
2975 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2976 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2977 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2978 
2979 	ctxt.seid = vsi->seid;
2980 	ctxt.info = vsi->info;
2981 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2982 	if (ret) {
2983 		dev_info(&vsi->back->pdev->dev,
2984 			 "update vlan stripping failed, err %pe aq_err %s\n",
2985 			 ERR_PTR(ret),
2986 			 i40e_aq_str(&vsi->back->hw,
2987 				     vsi->back->hw.aq.asq_last_status));
2988 	}
2989 }
2990 
2991 /**
2992  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2993  * @vsi: the vsi being adjusted
2994  **/
2995 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
2996 {
2997 	struct i40e_vsi_context ctxt;
2998 	int ret;
2999 
3000 	/* Don't modify stripping options if a port VLAN is active */
3001 	if (vsi->info.pvid)
3002 		return;
3003 
3004 	if ((vsi->info.valid_sections &
3005 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3006 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3007 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3008 		return;  /* already disabled */
3009 
3010 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3011 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3012 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3013 
3014 	ctxt.seid = vsi->seid;
3015 	ctxt.info = vsi->info;
3016 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3017 	if (ret) {
3018 		dev_info(&vsi->back->pdev->dev,
3019 			 "update vlan stripping failed, err %pe aq_err %s\n",
3020 			 ERR_PTR(ret),
3021 			 i40e_aq_str(&vsi->back->hw,
3022 				     vsi->back->hw.aq.asq_last_status));
3023 	}
3024 }
3025 
3026 /**
3027  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3028  * @vsi: the vsi being configured
3029  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3030  *
3031  * This is a helper function for adding a new MAC/VLAN filter with the
3032  * specified VLAN for each existing MAC address already in the hash table.
3033  * This function does *not* perform any accounting to update filters based on
3034  * VLAN mode.
3035  *
3036  * NOTE: this function expects to be called while under the
3037  * mac_filter_hash_lock
3038  **/
3039 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3040 {
3041 	struct i40e_mac_filter *f, *add_f;
3042 	struct hlist_node *h;
3043 	int bkt;
3044 
3045 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3046 		/* If we're asked to add a filter that has been marked for
3047 		 * removal, it is safe to simply restore it to active state.
3048 		 * __i40e_del_filter will have simply deleted any filters which
3049 		 * were previously marked NEW or FAILED, so if it is currently
3050 		 * marked REMOVE it must have previously been ACTIVE. Since we
3051 		 * haven't yet run the sync filters task, just restore this
3052 		 * filter to the ACTIVE state so that the sync task leaves it
3053 		 * in place.
3054 		 */
3055 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3056 			f->state = I40E_FILTER_ACTIVE;
3057 			continue;
3058 		} else if (f->state == I40E_FILTER_REMOVE) {
3059 			continue;
3060 		}
3061 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3062 		if (!add_f) {
3063 			dev_info(&vsi->back->pdev->dev,
3064 				 "Could not add vlan filter %d for %pM\n",
3065 				 vid, f->macaddr);
3066 			return -ENOMEM;
3067 		}
3068 	}
3069 
3070 	return 0;
3071 }
3072 
3073 /**
3074  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3075  * @vsi: the VSI being configured
3076  * @vid: VLAN id to be added
3077  **/
3078 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3079 {
3080 	int err;
3081 
3082 	if (vsi->info.pvid)
3083 		return -EINVAL;
3084 
3085 	/* The network stack will attempt to add VID=0, with the intention to
3086 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3087 	 * these packets by default when configured to receive untagged
3088 	 * packets, so we don't need to add a filter for this case.
3089 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3090 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3091 	 * Thus, we do not want to actually add a filter for VID=0
3092 	 */
3093 	if (!vid)
3094 		return 0;
3095 
3096 	/* Locked once because all functions invoked below iterates list*/
3097 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3098 	err = i40e_add_vlan_all_mac(vsi, vid);
3099 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3100 	if (err)
3101 		return err;
3102 
3103 	/* schedule our worker thread which will take care of
3104 	 * applying the new filter changes
3105 	 */
3106 	i40e_service_event_schedule(vsi->back);
3107 	return 0;
3108 }
3109 
3110 /**
3111  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3112  * @vsi: the vsi being configured
3113  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3114  *
3115  * This function should be used to remove all VLAN filters which match the
3116  * given VID. It does not schedule the service event and does not take the
3117  * mac_filter_hash_lock so it may be combined with other operations under
3118  * a single invocation of the mac_filter_hash_lock.
3119  *
3120  * NOTE: this function expects to be called while under the
3121  * mac_filter_hash_lock
3122  */
3123 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3124 {
3125 	struct i40e_mac_filter *f;
3126 	struct hlist_node *h;
3127 	int bkt;
3128 
3129 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3130 		if (f->vlan == vid)
3131 			__i40e_del_filter(vsi, f);
3132 	}
3133 }
3134 
3135 /**
3136  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3137  * @vsi: the VSI being configured
3138  * @vid: VLAN id to be removed
3139  **/
3140 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3141 {
3142 	if (!vid || vsi->info.pvid)
3143 		return;
3144 
3145 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3146 	i40e_rm_vlan_all_mac(vsi, vid);
3147 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3148 
3149 	/* schedule our worker thread which will take care of
3150 	 * applying the new filter changes
3151 	 */
3152 	i40e_service_event_schedule(vsi->back);
3153 }
3154 
3155 /**
3156  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3157  * @netdev: network interface to be adjusted
3158  * @proto: unused protocol value
3159  * @vid: vlan id to be added
3160  *
3161  * net_device_ops implementation for adding vlan ids
3162  **/
3163 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3164 				__always_unused __be16 proto, u16 vid)
3165 {
3166 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3167 	struct i40e_vsi *vsi = np->vsi;
3168 	int ret = 0;
3169 
3170 	if (vid >= VLAN_N_VID)
3171 		return -EINVAL;
3172 
3173 	ret = i40e_vsi_add_vlan(vsi, vid);
3174 	if (!ret)
3175 		set_bit(vid, vsi->active_vlans);
3176 
3177 	return ret;
3178 }
3179 
3180 /**
3181  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3182  * @netdev: network interface to be adjusted
3183  * @proto: unused protocol value
3184  * @vid: vlan id to be added
3185  **/
3186 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3187 				    __always_unused __be16 proto, u16 vid)
3188 {
3189 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3190 	struct i40e_vsi *vsi = np->vsi;
3191 
3192 	if (vid >= VLAN_N_VID)
3193 		return;
3194 	set_bit(vid, vsi->active_vlans);
3195 }
3196 
3197 /**
3198  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3199  * @netdev: network interface to be adjusted
3200  * @proto: unused protocol value
3201  * @vid: vlan id to be removed
3202  *
3203  * net_device_ops implementation for removing vlan ids
3204  **/
3205 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3206 				 __always_unused __be16 proto, u16 vid)
3207 {
3208 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3209 	struct i40e_vsi *vsi = np->vsi;
3210 
3211 	/* return code is ignored as there is nothing a user
3212 	 * can do about failure to remove and a log message was
3213 	 * already printed from the other function
3214 	 */
3215 	i40e_vsi_kill_vlan(vsi, vid);
3216 
3217 	clear_bit(vid, vsi->active_vlans);
3218 
3219 	return 0;
3220 }
3221 
3222 /**
3223  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3224  * @vsi: the vsi being brought back up
3225  **/
3226 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3227 {
3228 	u16 vid;
3229 
3230 	if (!vsi->netdev)
3231 		return;
3232 
3233 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3234 		i40e_vlan_stripping_enable(vsi);
3235 	else
3236 		i40e_vlan_stripping_disable(vsi);
3237 
3238 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3239 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3240 					vid);
3241 }
3242 
3243 /**
3244  * i40e_vsi_add_pvid - Add pvid for the VSI
3245  * @vsi: the vsi being adjusted
3246  * @vid: the vlan id to set as a PVID
3247  **/
3248 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3249 {
3250 	struct i40e_vsi_context ctxt;
3251 	int ret;
3252 
3253 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3254 	vsi->info.pvid = cpu_to_le16(vid);
3255 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3256 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3257 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3258 
3259 	ctxt.seid = vsi->seid;
3260 	ctxt.info = vsi->info;
3261 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3262 	if (ret) {
3263 		dev_info(&vsi->back->pdev->dev,
3264 			 "add pvid failed, err %pe aq_err %s\n",
3265 			 ERR_PTR(ret),
3266 			 i40e_aq_str(&vsi->back->hw,
3267 				     vsi->back->hw.aq.asq_last_status));
3268 		return -ENOENT;
3269 	}
3270 
3271 	return 0;
3272 }
3273 
3274 /**
3275  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3276  * @vsi: the vsi being adjusted
3277  *
3278  * Just use the vlan_rx_register() service to put it back to normal
3279  **/
3280 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3281 {
3282 	vsi->info.pvid = 0;
3283 
3284 	i40e_vlan_stripping_disable(vsi);
3285 }
3286 
3287 /**
3288  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3289  * @vsi: ptr to the VSI
3290  *
3291  * If this function returns with an error, then it's possible one or
3292  * more of the rings is populated (while the rest are not).  It is the
3293  * callers duty to clean those orphaned rings.
3294  *
3295  * Return 0 on success, negative on failure
3296  **/
3297 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3298 {
3299 	int i, err = 0;
3300 
3301 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3302 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3303 
3304 	if (!i40e_enabled_xdp_vsi(vsi))
3305 		return err;
3306 
3307 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3308 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3309 
3310 	return err;
3311 }
3312 
3313 /**
3314  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3315  * @vsi: ptr to the VSI
3316  *
3317  * Free VSI's transmit software resources
3318  **/
3319 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3320 {
3321 	int i;
3322 
3323 	if (vsi->tx_rings) {
3324 		for (i = 0; i < vsi->num_queue_pairs; i++)
3325 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3326 				i40e_free_tx_resources(vsi->tx_rings[i]);
3327 	}
3328 
3329 	if (vsi->xdp_rings) {
3330 		for (i = 0; i < vsi->num_queue_pairs; i++)
3331 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3332 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3333 	}
3334 }
3335 
3336 /**
3337  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3338  * @vsi: ptr to the VSI
3339  *
3340  * If this function returns with an error, then it's possible one or
3341  * more of the rings is populated (while the rest are not).  It is the
3342  * callers duty to clean those orphaned rings.
3343  *
3344  * Return 0 on success, negative on failure
3345  **/
3346 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3347 {
3348 	int i, err = 0;
3349 
3350 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3351 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3352 	return err;
3353 }
3354 
3355 /**
3356  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3357  * @vsi: ptr to the VSI
3358  *
3359  * Free all receive software resources
3360  **/
3361 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3362 {
3363 	int i;
3364 
3365 	if (!vsi->rx_rings)
3366 		return;
3367 
3368 	for (i = 0; i < vsi->num_queue_pairs; i++)
3369 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3370 			i40e_free_rx_resources(vsi->rx_rings[i]);
3371 }
3372 
3373 /**
3374  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3375  * @ring: The Tx ring to configure
3376  *
3377  * This enables/disables XPS for a given Tx descriptor ring
3378  * based on the TCs enabled for the VSI that ring belongs to.
3379  **/
3380 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3381 {
3382 	int cpu;
3383 
3384 	if (!ring->q_vector || !ring->netdev || ring->ch)
3385 		return;
3386 
3387 	/* We only initialize XPS once, so as not to overwrite user settings */
3388 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3389 		return;
3390 
3391 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3392 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3393 			    ring->queue_index);
3394 }
3395 
3396 /**
3397  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3398  * @ring: The Tx or Rx ring
3399  *
3400  * Returns the AF_XDP buffer pool or NULL.
3401  **/
3402 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3403 {
3404 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3405 	int qid = ring->queue_index;
3406 
3407 	if (ring_is_xdp(ring))
3408 		qid -= ring->vsi->alloc_queue_pairs;
3409 
3410 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3411 		return NULL;
3412 
3413 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3414 }
3415 
3416 /**
3417  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3418  * @ring: The Tx ring to configure
3419  *
3420  * Configure the Tx descriptor ring in the HMC context.
3421  **/
3422 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3423 {
3424 	struct i40e_vsi *vsi = ring->vsi;
3425 	u16 pf_q = vsi->base_queue + ring->queue_index;
3426 	struct i40e_hw *hw = &vsi->back->hw;
3427 	struct i40e_hmc_obj_txq tx_ctx;
3428 	u32 qtx_ctl = 0;
3429 	int err = 0;
3430 
3431 	if (ring_is_xdp(ring))
3432 		ring->xsk_pool = i40e_xsk_pool(ring);
3433 
3434 	/* some ATR related tx ring init */
3435 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3436 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3437 		ring->atr_count = 0;
3438 	} else {
3439 		ring->atr_sample_rate = 0;
3440 	}
3441 
3442 	/* configure XPS */
3443 	i40e_config_xps_tx_ring(ring);
3444 
3445 	/* clear the context structure first */
3446 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3447 
3448 	tx_ctx.new_context = 1;
3449 	tx_ctx.base = (ring->dma / 128);
3450 	tx_ctx.qlen = ring->count;
3451 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3452 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3453 		tx_ctx.fd_ena = 1;
3454 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3455 		tx_ctx.timesync_ena = 1;
3456 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3457 	if (vsi->type != I40E_VSI_FDIR)
3458 		tx_ctx.head_wb_ena = 1;
3459 	tx_ctx.head_wb_addr = ring->dma +
3460 			      (ring->count * sizeof(struct i40e_tx_desc));
3461 
3462 	/* As part of VSI creation/update, FW allocates certain
3463 	 * Tx arbitration queue sets for each TC enabled for
3464 	 * the VSI. The FW returns the handles to these queue
3465 	 * sets as part of the response buffer to Add VSI,
3466 	 * Update VSI, etc. AQ commands. It is expected that
3467 	 * these queue set handles be associated with the Tx
3468 	 * queues by the driver as part of the TX queue context
3469 	 * initialization. This has to be done regardless of
3470 	 * DCB as by default everything is mapped to TC0.
3471 	 */
3472 
3473 	if (ring->ch)
3474 		tx_ctx.rdylist =
3475 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3476 
3477 	else
3478 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3479 
3480 	tx_ctx.rdylist_act = 0;
3481 
3482 	/* clear the context in the HMC */
3483 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3484 	if (err) {
3485 		dev_info(&vsi->back->pdev->dev,
3486 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3487 			 ring->queue_index, pf_q, err);
3488 		return -ENOMEM;
3489 	}
3490 
3491 	/* set the context in the HMC */
3492 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3493 	if (err) {
3494 		dev_info(&vsi->back->pdev->dev,
3495 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3496 			 ring->queue_index, pf_q, err);
3497 		return -ENOMEM;
3498 	}
3499 
3500 	/* Now associate this queue with this PCI function */
3501 	if (ring->ch) {
3502 		if (ring->ch->type == I40E_VSI_VMDQ2)
3503 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3504 		else
3505 			return -EINVAL;
3506 
3507 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3508 				      ring->ch->vsi_number);
3509 	} else {
3510 		if (vsi->type == I40E_VSI_VMDQ2) {
3511 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3512 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3513 					      vsi->id);
3514 		} else {
3515 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3516 		}
3517 	}
3518 
3519 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3520 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3521 	i40e_flush(hw);
3522 
3523 	/* cache tail off for easier writes later */
3524 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3525 
3526 	return 0;
3527 }
3528 
3529 /**
3530  * i40e_rx_offset - Return expected offset into page to access data
3531  * @rx_ring: Ring we are requesting offset of
3532  *
3533  * Returns the offset value for ring into the data buffer.
3534  */
3535 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3536 {
3537 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3538 }
3539 
3540 /**
3541  * i40e_configure_rx_ring - Configure a receive ring context
3542  * @ring: The Rx ring to configure
3543  *
3544  * Configure the Rx descriptor ring in the HMC context.
3545  **/
3546 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3547 {
3548 	struct i40e_vsi *vsi = ring->vsi;
3549 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3550 	u16 pf_q = vsi->base_queue + ring->queue_index;
3551 	struct i40e_hw *hw = &vsi->back->hw;
3552 	struct i40e_hmc_obj_rxq rx_ctx;
3553 	int err = 0;
3554 	bool ok;
3555 
3556 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3557 
3558 	/* clear the context structure first */
3559 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3560 
3561 	ring->rx_buf_len = vsi->rx_buf_len;
3562 
3563 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3564 	if (ring->vsi->type != I40E_VSI_MAIN)
3565 		goto skip;
3566 
3567 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3568 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3569 					 ring->queue_index,
3570 					 ring->q_vector->napi.napi_id,
3571 					 ring->rx_buf_len);
3572 		if (err)
3573 			return err;
3574 	}
3575 
3576 	ring->xsk_pool = i40e_xsk_pool(ring);
3577 	if (ring->xsk_pool) {
3578 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3579 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3580 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3581 					 ring->queue_index,
3582 					 ring->q_vector->napi.napi_id,
3583 					 ring->rx_buf_len);
3584 		if (err)
3585 			return err;
3586 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3587 						 MEM_TYPE_XSK_BUFF_POOL,
3588 						 NULL);
3589 		if (err)
3590 			return err;
3591 		dev_info(&vsi->back->pdev->dev,
3592 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3593 			 ring->queue_index);
3594 
3595 	} else {
3596 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3597 						 MEM_TYPE_PAGE_SHARED,
3598 						 NULL);
3599 		if (err)
3600 			return err;
3601 	}
3602 
3603 skip:
3604 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3605 
3606 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3607 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3608 
3609 	rx_ctx.base = (ring->dma / 128);
3610 	rx_ctx.qlen = ring->count;
3611 
3612 	/* use 16 byte descriptors */
3613 	rx_ctx.dsize = 0;
3614 
3615 	/* descriptor type is always zero
3616 	 * rx_ctx.dtype = 0;
3617 	 */
3618 	rx_ctx.hsplit_0 = 0;
3619 
3620 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3621 	if (hw->revision_id == 0)
3622 		rx_ctx.lrxqthresh = 0;
3623 	else
3624 		rx_ctx.lrxqthresh = 1;
3625 	rx_ctx.crcstrip = 1;
3626 	rx_ctx.l2tsel = 1;
3627 	/* this controls whether VLAN is stripped from inner headers */
3628 	rx_ctx.showiv = 0;
3629 	/* set the prefena field to 1 because the manual says to */
3630 	rx_ctx.prefena = 1;
3631 
3632 	/* clear the context in the HMC */
3633 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3634 	if (err) {
3635 		dev_info(&vsi->back->pdev->dev,
3636 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3637 			 ring->queue_index, pf_q, err);
3638 		return -ENOMEM;
3639 	}
3640 
3641 	/* set the context in the HMC */
3642 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3643 	if (err) {
3644 		dev_info(&vsi->back->pdev->dev,
3645 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3646 			 ring->queue_index, pf_q, err);
3647 		return -ENOMEM;
3648 	}
3649 
3650 	/* configure Rx buffer alignment */
3651 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3652 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3653 			dev_info(&vsi->back->pdev->dev,
3654 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3655 			return -EOPNOTSUPP;
3656 		}
3657 		clear_ring_build_skb_enabled(ring);
3658 	} else {
3659 		set_ring_build_skb_enabled(ring);
3660 	}
3661 
3662 	ring->rx_offset = i40e_rx_offset(ring);
3663 
3664 	/* cache tail for quicker writes, and clear the reg before use */
3665 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3666 	writel(0, ring->tail);
3667 
3668 	if (ring->xsk_pool) {
3669 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3670 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3671 	} else {
3672 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3673 	}
3674 	if (!ok) {
3675 		/* Log this in case the user has forgotten to give the kernel
3676 		 * any buffers, even later in the application.
3677 		 */
3678 		dev_info(&vsi->back->pdev->dev,
3679 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3680 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3681 			 ring->queue_index, pf_q);
3682 	}
3683 
3684 	return 0;
3685 }
3686 
3687 /**
3688  * i40e_vsi_configure_tx - Configure the VSI for Tx
3689  * @vsi: VSI structure describing this set of rings and resources
3690  *
3691  * Configure the Tx VSI for operation.
3692  **/
3693 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3694 {
3695 	int err = 0;
3696 	u16 i;
3697 
3698 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3699 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3700 
3701 	if (err || !i40e_enabled_xdp_vsi(vsi))
3702 		return err;
3703 
3704 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3705 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3706 
3707 	return err;
3708 }
3709 
3710 /**
3711  * i40e_vsi_configure_rx - Configure the VSI for Rx
3712  * @vsi: the VSI being configured
3713  *
3714  * Configure the Rx VSI for operation.
3715  **/
3716 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3717 {
3718 	int err = 0;
3719 	u16 i;
3720 
3721 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3722 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3723 
3724 #if (PAGE_SIZE < 8192)
3725 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3726 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3727 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3728 		vsi->max_frame = vsi->rx_buf_len;
3729 	}
3730 #endif
3731 
3732 	/* set up individual rings */
3733 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3734 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3735 
3736 	return err;
3737 }
3738 
3739 /**
3740  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3741  * @vsi: ptr to the VSI
3742  **/
3743 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3744 {
3745 	struct i40e_ring *tx_ring, *rx_ring;
3746 	u16 qoffset, qcount;
3747 	int i, n;
3748 
3749 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3750 		/* Reset the TC information */
3751 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3752 			rx_ring = vsi->rx_rings[i];
3753 			tx_ring = vsi->tx_rings[i];
3754 			rx_ring->dcb_tc = 0;
3755 			tx_ring->dcb_tc = 0;
3756 		}
3757 		return;
3758 	}
3759 
3760 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3761 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3762 			continue;
3763 
3764 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3765 		qcount = vsi->tc_config.tc_info[n].qcount;
3766 		for (i = qoffset; i < (qoffset + qcount); i++) {
3767 			rx_ring = vsi->rx_rings[i];
3768 			tx_ring = vsi->tx_rings[i];
3769 			rx_ring->dcb_tc = n;
3770 			tx_ring->dcb_tc = n;
3771 		}
3772 	}
3773 }
3774 
3775 /**
3776  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3777  * @vsi: ptr to the VSI
3778  **/
3779 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3780 {
3781 	if (vsi->netdev)
3782 		i40e_set_rx_mode(vsi->netdev);
3783 }
3784 
3785 /**
3786  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3787  * @pf: Pointer to the targeted PF
3788  *
3789  * Set all flow director counters to 0.
3790  */
3791 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3792 {
3793 	pf->fd_tcp4_filter_cnt = 0;
3794 	pf->fd_udp4_filter_cnt = 0;
3795 	pf->fd_sctp4_filter_cnt = 0;
3796 	pf->fd_ip4_filter_cnt = 0;
3797 	pf->fd_tcp6_filter_cnt = 0;
3798 	pf->fd_udp6_filter_cnt = 0;
3799 	pf->fd_sctp6_filter_cnt = 0;
3800 	pf->fd_ip6_filter_cnt = 0;
3801 }
3802 
3803 /**
3804  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3805  * @vsi: Pointer to the targeted VSI
3806  *
3807  * This function replays the hlist on the hw where all the SB Flow Director
3808  * filters were saved.
3809  **/
3810 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3811 {
3812 	struct i40e_fdir_filter *filter;
3813 	struct i40e_pf *pf = vsi->back;
3814 	struct hlist_node *node;
3815 
3816 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3817 		return;
3818 
3819 	/* Reset FDir counters as we're replaying all existing filters */
3820 	i40e_reset_fdir_filter_cnt(pf);
3821 
3822 	hlist_for_each_entry_safe(filter, node,
3823 				  &pf->fdir_filter_list, fdir_node) {
3824 		i40e_add_del_fdir(vsi, filter, true);
3825 	}
3826 }
3827 
3828 /**
3829  * i40e_vsi_configure - Set up the VSI for action
3830  * @vsi: the VSI being configured
3831  **/
3832 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3833 {
3834 	int err;
3835 
3836 	i40e_set_vsi_rx_mode(vsi);
3837 	i40e_restore_vlan(vsi);
3838 	i40e_vsi_config_dcb_rings(vsi);
3839 	err = i40e_vsi_configure_tx(vsi);
3840 	if (!err)
3841 		err = i40e_vsi_configure_rx(vsi);
3842 
3843 	return err;
3844 }
3845 
3846 /**
3847  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3848  * @vsi: the VSI being configured
3849  **/
3850 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3851 {
3852 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3853 	struct i40e_pf *pf = vsi->back;
3854 	struct i40e_hw *hw = &pf->hw;
3855 	u16 vector;
3856 	int i, q;
3857 	u32 qp;
3858 
3859 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3860 	 * and PFINT_LNKLSTn registers, e.g.:
3861 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3862 	 */
3863 	qp = vsi->base_queue;
3864 	vector = vsi->base_vector;
3865 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3866 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3867 
3868 		q_vector->rx.next_update = jiffies + 1;
3869 		q_vector->rx.target_itr =
3870 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3871 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3872 		     q_vector->rx.target_itr >> 1);
3873 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3874 
3875 		q_vector->tx.next_update = jiffies + 1;
3876 		q_vector->tx.target_itr =
3877 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3878 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3879 		     q_vector->tx.target_itr >> 1);
3880 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3881 
3882 		/* Set ITR for software interrupts triggered after exiting
3883 		 * busy-loop polling.
3884 		 */
3885 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3886 		     I40E_ITR_20K);
3887 
3888 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3889 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3890 
3891 		/* begin of linked list for RX queue assigned to this vector */
3892 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3893 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3894 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3895 			u32 val;
3896 
3897 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3898 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3899 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3900 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3901 			      (I40E_QUEUE_TYPE_TX <<
3902 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3903 
3904 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3905 
3906 			if (has_xdp) {
3907 				/* TX queue with next queue set to TX */
3908 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3909 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3910 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3911 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3912 				      (I40E_QUEUE_TYPE_TX <<
3913 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3914 
3915 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3916 			}
3917 			/* TX queue with next RX or end of linked list */
3918 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3919 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3920 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3921 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3922 			      (I40E_QUEUE_TYPE_RX <<
3923 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3924 
3925 			/* Terminate the linked list */
3926 			if (q == (q_vector->num_ringpairs - 1))
3927 				val |= (I40E_QUEUE_END_OF_LIST <<
3928 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3929 
3930 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3931 			qp++;
3932 		}
3933 	}
3934 
3935 	i40e_flush(hw);
3936 }
3937 
3938 /**
3939  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3940  * @pf: pointer to private device data structure
3941  **/
3942 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3943 {
3944 	struct i40e_hw *hw = &pf->hw;
3945 	u32 val;
3946 
3947 	/* clear things first */
3948 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3949 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3950 
3951 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3952 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3953 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3954 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3955 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3956 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3957 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3958 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3959 
3960 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3961 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3962 
3963 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
3964 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3965 
3966 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3967 
3968 	/* SW_ITR_IDX = 0, but don't change INTENA */
3969 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3970 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3971 
3972 	/* OTHER_ITR_IDX = 0 */
3973 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3974 }
3975 
3976 /**
3977  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3978  * @vsi: the VSI being configured
3979  **/
3980 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3981 {
3982 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3983 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3984 	struct i40e_pf *pf = vsi->back;
3985 	struct i40e_hw *hw = &pf->hw;
3986 
3987 	/* set the ITR configuration */
3988 	q_vector->rx.next_update = jiffies + 1;
3989 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3990 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3991 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3992 	q_vector->tx.next_update = jiffies + 1;
3993 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3994 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3995 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3996 
3997 	i40e_enable_misc_int_causes(pf);
3998 
3999 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4000 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4001 
4002 	/* Associate the queue pair to the vector and enable the queue
4003 	 * interrupt RX queue in linked list with next queue set to TX
4004 	 */
4005 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4006 
4007 	if (i40e_enabled_xdp_vsi(vsi)) {
4008 		/* TX queue in linked list with next queue set to TX */
4009 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4010 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4011 	}
4012 
4013 	/* last TX queue so the next RX queue doesn't matter */
4014 	wr32(hw, I40E_QINT_TQCTL(0),
4015 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4016 	i40e_flush(hw);
4017 }
4018 
4019 /**
4020  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4021  * @pf: board private structure
4022  **/
4023 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4024 {
4025 	struct i40e_hw *hw = &pf->hw;
4026 
4027 	wr32(hw, I40E_PFINT_DYN_CTL0,
4028 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4029 	i40e_flush(hw);
4030 }
4031 
4032 /**
4033  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4034  * @pf: board private structure
4035  **/
4036 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4037 {
4038 	struct i40e_hw *hw = &pf->hw;
4039 	u32 val;
4040 
4041 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4042 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4043 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4044 
4045 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4046 	i40e_flush(hw);
4047 }
4048 
4049 /**
4050  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4051  * @irq: interrupt number
4052  * @data: pointer to a q_vector
4053  **/
4054 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4055 {
4056 	struct i40e_q_vector *q_vector = data;
4057 
4058 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4059 		return IRQ_HANDLED;
4060 
4061 	napi_schedule_irqoff(&q_vector->napi);
4062 
4063 	return IRQ_HANDLED;
4064 }
4065 
4066 /**
4067  * i40e_irq_affinity_notify - Callback for affinity changes
4068  * @notify: context as to what irq was changed
4069  * @mask: the new affinity mask
4070  *
4071  * This is a callback function used by the irq_set_affinity_notifier function
4072  * so that we may register to receive changes to the irq affinity masks.
4073  **/
4074 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4075 				     const cpumask_t *mask)
4076 {
4077 	struct i40e_q_vector *q_vector =
4078 		container_of(notify, struct i40e_q_vector, affinity_notify);
4079 
4080 	cpumask_copy(&q_vector->affinity_mask, mask);
4081 }
4082 
4083 /**
4084  * i40e_irq_affinity_release - Callback for affinity notifier release
4085  * @ref: internal core kernel usage
4086  *
4087  * This is a callback function used by the irq_set_affinity_notifier function
4088  * to inform the current notification subscriber that they will no longer
4089  * receive notifications.
4090  **/
4091 static void i40e_irq_affinity_release(struct kref *ref) {}
4092 
4093 /**
4094  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4095  * @vsi: the VSI being configured
4096  * @basename: name for the vector
4097  *
4098  * Allocates MSI-X vectors and requests interrupts from the kernel.
4099  **/
4100 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4101 {
4102 	int q_vectors = vsi->num_q_vectors;
4103 	struct i40e_pf *pf = vsi->back;
4104 	int base = vsi->base_vector;
4105 	int rx_int_idx = 0;
4106 	int tx_int_idx = 0;
4107 	int vector, err;
4108 	int irq_num;
4109 	int cpu;
4110 
4111 	for (vector = 0; vector < q_vectors; vector++) {
4112 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4113 
4114 		irq_num = pf->msix_entries[base + vector].vector;
4115 
4116 		if (q_vector->tx.ring && q_vector->rx.ring) {
4117 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4118 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4119 			tx_int_idx++;
4120 		} else if (q_vector->rx.ring) {
4121 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4122 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4123 		} else if (q_vector->tx.ring) {
4124 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4125 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4126 		} else {
4127 			/* skip this unused q_vector */
4128 			continue;
4129 		}
4130 		err = request_irq(irq_num,
4131 				  vsi->irq_handler,
4132 				  0,
4133 				  q_vector->name,
4134 				  q_vector);
4135 		if (err) {
4136 			dev_info(&pf->pdev->dev,
4137 				 "MSIX request_irq failed, error: %d\n", err);
4138 			goto free_queue_irqs;
4139 		}
4140 
4141 		/* register for affinity change notifications */
4142 		q_vector->irq_num = irq_num;
4143 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4144 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4145 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4146 		/* Spread affinity hints out across online CPUs.
4147 		 *
4148 		 * get_cpu_mask returns a static constant mask with
4149 		 * a permanent lifetime so it's ok to pass to
4150 		 * irq_update_affinity_hint without making a copy.
4151 		 */
4152 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4153 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4154 	}
4155 
4156 	vsi->irqs_ready = true;
4157 	return 0;
4158 
4159 free_queue_irqs:
4160 	while (vector) {
4161 		vector--;
4162 		irq_num = pf->msix_entries[base + vector].vector;
4163 		irq_set_affinity_notifier(irq_num, NULL);
4164 		irq_update_affinity_hint(irq_num, NULL);
4165 		free_irq(irq_num, &vsi->q_vectors[vector]);
4166 	}
4167 	return err;
4168 }
4169 
4170 /**
4171  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4172  * @vsi: the VSI being un-configured
4173  **/
4174 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4175 {
4176 	struct i40e_pf *pf = vsi->back;
4177 	struct i40e_hw *hw = &pf->hw;
4178 	int base = vsi->base_vector;
4179 	int i;
4180 
4181 	/* disable interrupt causation from each queue */
4182 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4183 		u32 val;
4184 
4185 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4186 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4187 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4188 
4189 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4190 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4191 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4192 
4193 		if (!i40e_enabled_xdp_vsi(vsi))
4194 			continue;
4195 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4196 	}
4197 
4198 	/* disable each interrupt */
4199 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4200 		for (i = vsi->base_vector;
4201 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4202 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4203 
4204 		i40e_flush(hw);
4205 		for (i = 0; i < vsi->num_q_vectors; i++)
4206 			synchronize_irq(pf->msix_entries[i + base].vector);
4207 	} else {
4208 		/* Legacy and MSI mode - this stops all interrupt handling */
4209 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4210 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4211 		i40e_flush(hw);
4212 		synchronize_irq(pf->pdev->irq);
4213 	}
4214 }
4215 
4216 /**
4217  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4218  * @vsi: the VSI being configured
4219  **/
4220 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4221 {
4222 	struct i40e_pf *pf = vsi->back;
4223 	int i;
4224 
4225 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4226 		for (i = 0; i < vsi->num_q_vectors; i++)
4227 			i40e_irq_dynamic_enable(vsi, i);
4228 	} else {
4229 		i40e_irq_dynamic_enable_icr0(pf);
4230 	}
4231 
4232 	i40e_flush(&pf->hw);
4233 	return 0;
4234 }
4235 
4236 /**
4237  * i40e_free_misc_vector - Free the vector that handles non-queue events
4238  * @pf: board private structure
4239  **/
4240 static void i40e_free_misc_vector(struct i40e_pf *pf)
4241 {
4242 	/* Disable ICR 0 */
4243 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4244 	i40e_flush(&pf->hw);
4245 
4246 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4247 		free_irq(pf->msix_entries[0].vector, pf);
4248 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4249 	}
4250 }
4251 
4252 /**
4253  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4254  * @irq: interrupt number
4255  * @data: pointer to a q_vector
4256  *
4257  * This is the handler used for all MSI/Legacy interrupts, and deals
4258  * with both queue and non-queue interrupts.  This is also used in
4259  * MSIX mode to handle the non-queue interrupts.
4260  **/
4261 static irqreturn_t i40e_intr(int irq, void *data)
4262 {
4263 	struct i40e_pf *pf = (struct i40e_pf *)data;
4264 	struct i40e_hw *hw = &pf->hw;
4265 	irqreturn_t ret = IRQ_NONE;
4266 	u32 icr0, icr0_remaining;
4267 	u32 val, ena_mask;
4268 
4269 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4270 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4271 
4272 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4273 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4274 		goto enable_intr;
4275 
4276 	/* if interrupt but no bits showing, must be SWINT */
4277 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4278 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4279 		pf->sw_int_count++;
4280 
4281 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4282 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4283 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4284 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4285 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4286 	}
4287 
4288 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4289 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4290 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
4291 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4292 
4293 		/* We do not have a way to disarm Queue causes while leaving
4294 		 * interrupt enabled for all other causes, ideally
4295 		 * interrupt should be disabled while we are in NAPI but
4296 		 * this is not a performance path and napi_schedule()
4297 		 * can deal with rescheduling.
4298 		 */
4299 		if (!test_bit(__I40E_DOWN, pf->state))
4300 			napi_schedule_irqoff(&q_vector->napi);
4301 	}
4302 
4303 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4304 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4305 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4306 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4307 	}
4308 
4309 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4310 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4311 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4312 	}
4313 
4314 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4315 		/* disable any further VFLR event notifications */
4316 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4317 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4318 
4319 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4320 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4321 		} else {
4322 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4323 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4324 		}
4325 	}
4326 
4327 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4328 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4329 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4330 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4331 		val = rd32(hw, I40E_GLGEN_RSTAT);
4332 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4333 		if (val == I40E_RESET_CORER) {
4334 			pf->corer_count++;
4335 		} else if (val == I40E_RESET_GLOBR) {
4336 			pf->globr_count++;
4337 		} else if (val == I40E_RESET_EMPR) {
4338 			pf->empr_count++;
4339 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4340 		}
4341 	}
4342 
4343 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4344 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4345 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4346 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4347 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4348 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4349 	}
4350 
4351 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4352 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4353 
4354 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4355 			schedule_work(&pf->ptp_extts0_work);
4356 
4357 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4358 			i40e_ptp_tx_hwtstamp(pf);
4359 
4360 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4361 	}
4362 
4363 	/* If a critical error is pending we have no choice but to reset the
4364 	 * device.
4365 	 * Report and mask out any remaining unexpected interrupts.
4366 	 */
4367 	icr0_remaining = icr0 & ena_mask;
4368 	if (icr0_remaining) {
4369 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4370 			 icr0_remaining);
4371 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4372 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4373 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4374 			dev_info(&pf->pdev->dev, "device will be reset\n");
4375 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4376 			i40e_service_event_schedule(pf);
4377 		}
4378 		ena_mask &= ~icr0_remaining;
4379 	}
4380 	ret = IRQ_HANDLED;
4381 
4382 enable_intr:
4383 	/* re-enable interrupt causes */
4384 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4385 	if (!test_bit(__I40E_DOWN, pf->state) ||
4386 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4387 		i40e_service_event_schedule(pf);
4388 		i40e_irq_dynamic_enable_icr0(pf);
4389 	}
4390 
4391 	return ret;
4392 }
4393 
4394 /**
4395  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4396  * @tx_ring:  tx ring to clean
4397  * @budget:   how many cleans we're allowed
4398  *
4399  * Returns true if there's any budget left (e.g. the clean is finished)
4400  **/
4401 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4402 {
4403 	struct i40e_vsi *vsi = tx_ring->vsi;
4404 	u16 i = tx_ring->next_to_clean;
4405 	struct i40e_tx_buffer *tx_buf;
4406 	struct i40e_tx_desc *tx_desc;
4407 
4408 	tx_buf = &tx_ring->tx_bi[i];
4409 	tx_desc = I40E_TX_DESC(tx_ring, i);
4410 	i -= tx_ring->count;
4411 
4412 	do {
4413 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4414 
4415 		/* if next_to_watch is not set then there is no work pending */
4416 		if (!eop_desc)
4417 			break;
4418 
4419 		/* prevent any other reads prior to eop_desc */
4420 		smp_rmb();
4421 
4422 		/* if the descriptor isn't done, no work yet to do */
4423 		if (!(eop_desc->cmd_type_offset_bsz &
4424 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4425 			break;
4426 
4427 		/* clear next_to_watch to prevent false hangs */
4428 		tx_buf->next_to_watch = NULL;
4429 
4430 		tx_desc->buffer_addr = 0;
4431 		tx_desc->cmd_type_offset_bsz = 0;
4432 		/* move past filter desc */
4433 		tx_buf++;
4434 		tx_desc++;
4435 		i++;
4436 		if (unlikely(!i)) {
4437 			i -= tx_ring->count;
4438 			tx_buf = tx_ring->tx_bi;
4439 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4440 		}
4441 		/* unmap skb header data */
4442 		dma_unmap_single(tx_ring->dev,
4443 				 dma_unmap_addr(tx_buf, dma),
4444 				 dma_unmap_len(tx_buf, len),
4445 				 DMA_TO_DEVICE);
4446 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4447 			kfree(tx_buf->raw_buf);
4448 
4449 		tx_buf->raw_buf = NULL;
4450 		tx_buf->tx_flags = 0;
4451 		tx_buf->next_to_watch = NULL;
4452 		dma_unmap_len_set(tx_buf, len, 0);
4453 		tx_desc->buffer_addr = 0;
4454 		tx_desc->cmd_type_offset_bsz = 0;
4455 
4456 		/* move us past the eop_desc for start of next FD desc */
4457 		tx_buf++;
4458 		tx_desc++;
4459 		i++;
4460 		if (unlikely(!i)) {
4461 			i -= tx_ring->count;
4462 			tx_buf = tx_ring->tx_bi;
4463 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4464 		}
4465 
4466 		/* update budget accounting */
4467 		budget--;
4468 	} while (likely(budget));
4469 
4470 	i += tx_ring->count;
4471 	tx_ring->next_to_clean = i;
4472 
4473 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4474 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4475 
4476 	return budget > 0;
4477 }
4478 
4479 /**
4480  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4481  * @irq: interrupt number
4482  * @data: pointer to a q_vector
4483  **/
4484 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4485 {
4486 	struct i40e_q_vector *q_vector = data;
4487 	struct i40e_vsi *vsi;
4488 
4489 	if (!q_vector->tx.ring)
4490 		return IRQ_HANDLED;
4491 
4492 	vsi = q_vector->tx.ring->vsi;
4493 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4494 
4495 	return IRQ_HANDLED;
4496 }
4497 
4498 /**
4499  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4500  * @vsi: the VSI being configured
4501  * @v_idx: vector index
4502  * @qp_idx: queue pair index
4503  **/
4504 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4505 {
4506 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4507 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4508 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4509 
4510 	tx_ring->q_vector = q_vector;
4511 	tx_ring->next = q_vector->tx.ring;
4512 	q_vector->tx.ring = tx_ring;
4513 	q_vector->tx.count++;
4514 
4515 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4516 	if (i40e_enabled_xdp_vsi(vsi)) {
4517 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4518 
4519 		xdp_ring->q_vector = q_vector;
4520 		xdp_ring->next = q_vector->tx.ring;
4521 		q_vector->tx.ring = xdp_ring;
4522 		q_vector->tx.count++;
4523 	}
4524 
4525 	rx_ring->q_vector = q_vector;
4526 	rx_ring->next = q_vector->rx.ring;
4527 	q_vector->rx.ring = rx_ring;
4528 	q_vector->rx.count++;
4529 }
4530 
4531 /**
4532  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4533  * @vsi: the VSI being configured
4534  *
4535  * This function maps descriptor rings to the queue-specific vectors
4536  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4537  * one vector per queue pair, but on a constrained vector budget, we
4538  * group the queue pairs as "efficiently" as possible.
4539  **/
4540 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4541 {
4542 	int qp_remaining = vsi->num_queue_pairs;
4543 	int q_vectors = vsi->num_q_vectors;
4544 	int num_ringpairs;
4545 	int v_start = 0;
4546 	int qp_idx = 0;
4547 
4548 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4549 	 * group them so there are multiple queues per vector.
4550 	 * It is also important to go through all the vectors available to be
4551 	 * sure that if we don't use all the vectors, that the remaining vectors
4552 	 * are cleared. This is especially important when decreasing the
4553 	 * number of queues in use.
4554 	 */
4555 	for (; v_start < q_vectors; v_start++) {
4556 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4557 
4558 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4559 
4560 		q_vector->num_ringpairs = num_ringpairs;
4561 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4562 
4563 		q_vector->rx.count = 0;
4564 		q_vector->tx.count = 0;
4565 		q_vector->rx.ring = NULL;
4566 		q_vector->tx.ring = NULL;
4567 
4568 		while (num_ringpairs--) {
4569 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4570 			qp_idx++;
4571 			qp_remaining--;
4572 		}
4573 	}
4574 }
4575 
4576 /**
4577  * i40e_vsi_request_irq - Request IRQ from the OS
4578  * @vsi: the VSI being configured
4579  * @basename: name for the vector
4580  **/
4581 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4582 {
4583 	struct i40e_pf *pf = vsi->back;
4584 	int err;
4585 
4586 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4587 		err = i40e_vsi_request_irq_msix(vsi, basename);
4588 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4589 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4590 				  pf->int_name, pf);
4591 	else
4592 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4593 				  pf->int_name, pf);
4594 
4595 	if (err)
4596 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4597 
4598 	return err;
4599 }
4600 
4601 #ifdef CONFIG_NET_POLL_CONTROLLER
4602 /**
4603  * i40e_netpoll - A Polling 'interrupt' handler
4604  * @netdev: network interface device structure
4605  *
4606  * This is used by netconsole to send skbs without having to re-enable
4607  * interrupts.  It's not called while the normal interrupt routine is executing.
4608  **/
4609 static void i40e_netpoll(struct net_device *netdev)
4610 {
4611 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4612 	struct i40e_vsi *vsi = np->vsi;
4613 	struct i40e_pf *pf = vsi->back;
4614 	int i;
4615 
4616 	/* if interface is down do nothing */
4617 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4618 		return;
4619 
4620 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4621 		for (i = 0; i < vsi->num_q_vectors; i++)
4622 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4623 	} else {
4624 		i40e_intr(pf->pdev->irq, netdev);
4625 	}
4626 }
4627 #endif
4628 
4629 #define I40E_QTX_ENA_WAIT_COUNT 50
4630 
4631 /**
4632  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4633  * @pf: the PF being configured
4634  * @pf_q: the PF queue
4635  * @enable: enable or disable state of the queue
4636  *
4637  * This routine will wait for the given Tx queue of the PF to reach the
4638  * enabled or disabled state.
4639  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4640  * multiple retries; else will return 0 in case of success.
4641  **/
4642 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4643 {
4644 	int i;
4645 	u32 tx_reg;
4646 
4647 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4648 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4649 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4650 			break;
4651 
4652 		usleep_range(10, 20);
4653 	}
4654 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4655 		return -ETIMEDOUT;
4656 
4657 	return 0;
4658 }
4659 
4660 /**
4661  * i40e_control_tx_q - Start or stop a particular Tx queue
4662  * @pf: the PF structure
4663  * @pf_q: the PF queue to configure
4664  * @enable: start or stop the queue
4665  *
4666  * This function enables or disables a single queue. Note that any delay
4667  * required after the operation is expected to be handled by the caller of
4668  * this function.
4669  **/
4670 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4671 {
4672 	struct i40e_hw *hw = &pf->hw;
4673 	u32 tx_reg;
4674 	int i;
4675 
4676 	/* warn the TX unit of coming changes */
4677 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4678 	if (!enable)
4679 		usleep_range(10, 20);
4680 
4681 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4682 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4683 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4684 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4685 			break;
4686 		usleep_range(1000, 2000);
4687 	}
4688 
4689 	/* Skip if the queue is already in the requested state */
4690 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4691 		return;
4692 
4693 	/* turn on/off the queue */
4694 	if (enable) {
4695 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4696 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4697 	} else {
4698 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4699 	}
4700 
4701 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4702 }
4703 
4704 /**
4705  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4706  * @seid: VSI SEID
4707  * @pf: the PF structure
4708  * @pf_q: the PF queue to configure
4709  * @is_xdp: true if the queue is used for XDP
4710  * @enable: start or stop the queue
4711  **/
4712 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4713 			   bool is_xdp, bool enable)
4714 {
4715 	int ret;
4716 
4717 	i40e_control_tx_q(pf, pf_q, enable);
4718 
4719 	/* wait for the change to finish */
4720 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4721 	if (ret) {
4722 		dev_info(&pf->pdev->dev,
4723 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4724 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4725 			 (enable ? "en" : "dis"));
4726 	}
4727 
4728 	return ret;
4729 }
4730 
4731 /**
4732  * i40e_vsi_enable_tx - Start a VSI's rings
4733  * @vsi: the VSI being configured
4734  **/
4735 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4736 {
4737 	struct i40e_pf *pf = vsi->back;
4738 	int i, pf_q, ret = 0;
4739 
4740 	pf_q = vsi->base_queue;
4741 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4742 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4743 					     pf_q,
4744 					     false /*is xdp*/, true);
4745 		if (ret)
4746 			break;
4747 
4748 		if (!i40e_enabled_xdp_vsi(vsi))
4749 			continue;
4750 
4751 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4752 					     pf_q + vsi->alloc_queue_pairs,
4753 					     true /*is xdp*/, true);
4754 		if (ret)
4755 			break;
4756 	}
4757 	return ret;
4758 }
4759 
4760 /**
4761  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4762  * @pf: the PF being configured
4763  * @pf_q: the PF queue
4764  * @enable: enable or disable state of the queue
4765  *
4766  * This routine will wait for the given Rx queue of the PF to reach the
4767  * enabled or disabled state.
4768  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4769  * multiple retries; else will return 0 in case of success.
4770  **/
4771 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4772 {
4773 	int i;
4774 	u32 rx_reg;
4775 
4776 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4777 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4778 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4779 			break;
4780 
4781 		usleep_range(10, 20);
4782 	}
4783 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4784 		return -ETIMEDOUT;
4785 
4786 	return 0;
4787 }
4788 
4789 /**
4790  * i40e_control_rx_q - Start or stop a particular Rx queue
4791  * @pf: the PF structure
4792  * @pf_q: the PF queue to configure
4793  * @enable: start or stop the queue
4794  *
4795  * This function enables or disables a single queue. Note that
4796  * any delay required after the operation is expected to be
4797  * handled by the caller of this function.
4798  **/
4799 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4800 {
4801 	struct i40e_hw *hw = &pf->hw;
4802 	u32 rx_reg;
4803 	int i;
4804 
4805 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4806 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4807 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4808 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4809 			break;
4810 		usleep_range(1000, 2000);
4811 	}
4812 
4813 	/* Skip if the queue is already in the requested state */
4814 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4815 		return;
4816 
4817 	/* turn on/off the queue */
4818 	if (enable)
4819 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4820 	else
4821 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4822 
4823 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4824 }
4825 
4826 /**
4827  * i40e_control_wait_rx_q
4828  * @pf: the PF structure
4829  * @pf_q: queue being configured
4830  * @enable: start or stop the rings
4831  *
4832  * This function enables or disables a single queue along with waiting
4833  * for the change to finish. The caller of this function should handle
4834  * the delays needed in the case of disabling queues.
4835  **/
4836 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4837 {
4838 	int ret = 0;
4839 
4840 	i40e_control_rx_q(pf, pf_q, enable);
4841 
4842 	/* wait for the change to finish */
4843 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4844 	if (ret)
4845 		return ret;
4846 
4847 	return ret;
4848 }
4849 
4850 /**
4851  * i40e_vsi_enable_rx - Start a VSI's rings
4852  * @vsi: the VSI being configured
4853  **/
4854 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4855 {
4856 	struct i40e_pf *pf = vsi->back;
4857 	int i, pf_q, ret = 0;
4858 
4859 	pf_q = vsi->base_queue;
4860 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4861 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4862 		if (ret) {
4863 			dev_info(&pf->pdev->dev,
4864 				 "VSI seid %d Rx ring %d enable timeout\n",
4865 				 vsi->seid, pf_q);
4866 			break;
4867 		}
4868 	}
4869 
4870 	return ret;
4871 }
4872 
4873 /**
4874  * i40e_vsi_start_rings - Start a VSI's rings
4875  * @vsi: the VSI being configured
4876  **/
4877 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4878 {
4879 	int ret = 0;
4880 
4881 	/* do rx first for enable and last for disable */
4882 	ret = i40e_vsi_enable_rx(vsi);
4883 	if (ret)
4884 		return ret;
4885 	ret = i40e_vsi_enable_tx(vsi);
4886 
4887 	return ret;
4888 }
4889 
4890 #define I40E_DISABLE_TX_GAP_MSEC	50
4891 
4892 /**
4893  * i40e_vsi_stop_rings - Stop a VSI's rings
4894  * @vsi: the VSI being configured
4895  **/
4896 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4897 {
4898 	struct i40e_pf *pf = vsi->back;
4899 	u32 pf_q, tx_q_end, rx_q_end;
4900 
4901 	/* When port TX is suspended, don't wait */
4902 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4903 		return i40e_vsi_stop_rings_no_wait(vsi);
4904 
4905 	tx_q_end = vsi->base_queue +
4906 		vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
4907 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4908 		i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
4909 
4910 	rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
4911 	for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
4912 		i40e_control_rx_q(pf, pf_q, false);
4913 
4914 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4915 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4916 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4917 
4918 	i40e_vsi_wait_queues_disabled(vsi);
4919 }
4920 
4921 /**
4922  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4923  * @vsi: the VSI being shutdown
4924  *
4925  * This function stops all the rings for a VSI but does not delay to verify
4926  * that rings have been disabled. It is expected that the caller is shutting
4927  * down multiple VSIs at once and will delay together for all the VSIs after
4928  * initiating the shutdown. This is particularly useful for shutting down lots
4929  * of VFs together. Otherwise, a large delay can be incurred while configuring
4930  * each VSI in serial.
4931  **/
4932 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4933 {
4934 	struct i40e_pf *pf = vsi->back;
4935 	int i, pf_q;
4936 
4937 	pf_q = vsi->base_queue;
4938 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4939 		i40e_control_tx_q(pf, pf_q, false);
4940 		i40e_control_rx_q(pf, pf_q, false);
4941 	}
4942 }
4943 
4944 /**
4945  * i40e_vsi_free_irq - Free the irq association with the OS
4946  * @vsi: the VSI being configured
4947  **/
4948 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4949 {
4950 	struct i40e_pf *pf = vsi->back;
4951 	struct i40e_hw *hw = &pf->hw;
4952 	int base = vsi->base_vector;
4953 	u32 val, qp;
4954 	int i;
4955 
4956 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4957 		if (!vsi->q_vectors)
4958 			return;
4959 
4960 		if (!vsi->irqs_ready)
4961 			return;
4962 
4963 		vsi->irqs_ready = false;
4964 		for (i = 0; i < vsi->num_q_vectors; i++) {
4965 			int irq_num;
4966 			u16 vector;
4967 
4968 			vector = i + base;
4969 			irq_num = pf->msix_entries[vector].vector;
4970 
4971 			/* free only the irqs that were actually requested */
4972 			if (!vsi->q_vectors[i] ||
4973 			    !vsi->q_vectors[i]->num_ringpairs)
4974 				continue;
4975 
4976 			/* clear the affinity notifier in the IRQ descriptor */
4977 			irq_set_affinity_notifier(irq_num, NULL);
4978 			/* remove our suggested affinity mask for this IRQ */
4979 			irq_update_affinity_hint(irq_num, NULL);
4980 			free_irq(irq_num, vsi->q_vectors[i]);
4981 
4982 			/* Tear down the interrupt queue link list
4983 			 *
4984 			 * We know that they come in pairs and always
4985 			 * the Rx first, then the Tx.  To clear the
4986 			 * link list, stick the EOL value into the
4987 			 * next_q field of the registers.
4988 			 */
4989 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4990 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
4991 				       val);
4992 			val |= I40E_QUEUE_END_OF_LIST
4993 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4994 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4995 
4996 			while (qp != I40E_QUEUE_END_OF_LIST) {
4997 				u32 next;
4998 
4999 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5000 
5001 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5002 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5003 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5004 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5005 
5006 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5007 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5008 
5009 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5010 
5011 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5012 
5013 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5014 						 val);
5015 
5016 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5017 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5018 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5019 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5020 
5021 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5022 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5023 
5024 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5025 				qp = next;
5026 			}
5027 		}
5028 	} else {
5029 		free_irq(pf->pdev->irq, pf);
5030 
5031 		val = rd32(hw, I40E_PFINT_LNKLST0);
5032 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5033 		val |= I40E_QUEUE_END_OF_LIST
5034 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5035 		wr32(hw, I40E_PFINT_LNKLST0, val);
5036 
5037 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5038 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5039 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5040 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5041 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5042 
5043 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5044 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5045 
5046 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5047 
5048 		val = rd32(hw, I40E_QINT_TQCTL(qp));
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 	}
5060 }
5061 
5062 /**
5063  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5064  * @vsi: the VSI being configured
5065  * @v_idx: Index of vector to be freed
5066  *
5067  * This function frees the memory allocated to the q_vector.  In addition if
5068  * NAPI is enabled it will delete any references to the NAPI struct prior
5069  * to freeing the q_vector.
5070  **/
5071 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5072 {
5073 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5074 	struct i40e_ring *ring;
5075 
5076 	if (!q_vector)
5077 		return;
5078 
5079 	/* disassociate q_vector from rings */
5080 	i40e_for_each_ring(ring, q_vector->tx)
5081 		ring->q_vector = NULL;
5082 
5083 	i40e_for_each_ring(ring, q_vector->rx)
5084 		ring->q_vector = NULL;
5085 
5086 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5087 	if (vsi->netdev)
5088 		netif_napi_del(&q_vector->napi);
5089 
5090 	vsi->q_vectors[v_idx] = NULL;
5091 
5092 	kfree_rcu(q_vector, rcu);
5093 }
5094 
5095 /**
5096  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5097  * @vsi: the VSI being un-configured
5098  *
5099  * This frees the memory allocated to the q_vectors and
5100  * deletes references to the NAPI struct.
5101  **/
5102 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5103 {
5104 	int v_idx;
5105 
5106 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5107 		i40e_free_q_vector(vsi, v_idx);
5108 }
5109 
5110 /**
5111  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5112  * @pf: board private structure
5113  **/
5114 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5115 {
5116 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5117 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5118 		pci_disable_msix(pf->pdev);
5119 		kfree(pf->msix_entries);
5120 		pf->msix_entries = NULL;
5121 		kfree(pf->irq_pile);
5122 		pf->irq_pile = NULL;
5123 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5124 		pci_disable_msi(pf->pdev);
5125 	}
5126 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5127 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5128 }
5129 
5130 /**
5131  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5132  * @pf: board private structure
5133  *
5134  * We go through and clear interrupt specific resources and reset the structure
5135  * to pre-load conditions
5136  **/
5137 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5138 {
5139 	struct i40e_vsi *vsi;
5140 	int i;
5141 
5142 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5143 		i40e_free_misc_vector(pf);
5144 
5145 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5146 		      I40E_IWARP_IRQ_PILE_ID);
5147 
5148 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5149 
5150 	i40e_pf_for_each_vsi(pf, i, vsi)
5151 		i40e_vsi_free_q_vectors(vsi);
5152 
5153 	i40e_reset_interrupt_capability(pf);
5154 }
5155 
5156 /**
5157  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5158  * @vsi: the VSI being configured
5159  **/
5160 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5161 {
5162 	int q_idx;
5163 
5164 	if (!vsi->netdev)
5165 		return;
5166 
5167 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5168 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5169 
5170 		if (q_vector->rx.ring || q_vector->tx.ring)
5171 			napi_enable(&q_vector->napi);
5172 	}
5173 }
5174 
5175 /**
5176  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5177  * @vsi: the VSI being configured
5178  **/
5179 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5180 {
5181 	int q_idx;
5182 
5183 	if (!vsi->netdev)
5184 		return;
5185 
5186 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5187 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5188 
5189 		if (q_vector->rx.ring || q_vector->tx.ring)
5190 			napi_disable(&q_vector->napi);
5191 	}
5192 }
5193 
5194 /**
5195  * i40e_vsi_close - Shut down a VSI
5196  * @vsi: the vsi to be quelled
5197  **/
5198 static void i40e_vsi_close(struct i40e_vsi *vsi)
5199 {
5200 	struct i40e_pf *pf = vsi->back;
5201 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5202 		i40e_down(vsi);
5203 	i40e_vsi_free_irq(vsi);
5204 	i40e_vsi_free_tx_resources(vsi);
5205 	i40e_vsi_free_rx_resources(vsi);
5206 	vsi->current_netdev_flags = 0;
5207 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5208 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5209 		set_bit(__I40E_CLIENT_RESET, pf->state);
5210 }
5211 
5212 /**
5213  * i40e_quiesce_vsi - Pause a given VSI
5214  * @vsi: the VSI being paused
5215  **/
5216 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5217 {
5218 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5219 		return;
5220 
5221 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5222 	if (vsi->netdev && netif_running(vsi->netdev))
5223 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5224 	else
5225 		i40e_vsi_close(vsi);
5226 }
5227 
5228 /**
5229  * i40e_unquiesce_vsi - Resume a given VSI
5230  * @vsi: the VSI being resumed
5231  **/
5232 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5233 {
5234 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5235 		return;
5236 
5237 	if (vsi->netdev && netif_running(vsi->netdev))
5238 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5239 	else
5240 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5241 }
5242 
5243 /**
5244  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5245  * @pf: the PF
5246  **/
5247 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5248 {
5249 	struct i40e_vsi *vsi;
5250 	int v;
5251 
5252 	i40e_pf_for_each_vsi(pf, v, vsi)
5253 		i40e_quiesce_vsi(vsi);
5254 }
5255 
5256 /**
5257  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5258  * @pf: the PF
5259  **/
5260 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5261 {
5262 	struct i40e_vsi *vsi;
5263 	int v;
5264 
5265 	i40e_pf_for_each_vsi(pf, v, vsi)
5266 		i40e_unquiesce_vsi(vsi);
5267 }
5268 
5269 /**
5270  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5271  * @vsi: the VSI being configured
5272  *
5273  * Wait until all queues on a given VSI have been disabled.
5274  **/
5275 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5276 {
5277 	struct i40e_pf *pf = vsi->back;
5278 	int i, pf_q, ret;
5279 
5280 	pf_q = vsi->base_queue;
5281 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5282 		/* Check and wait for the Tx queue */
5283 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5284 		if (ret) {
5285 			dev_info(&pf->pdev->dev,
5286 				 "VSI seid %d Tx ring %d disable timeout\n",
5287 				 vsi->seid, pf_q);
5288 			return ret;
5289 		}
5290 
5291 		if (!i40e_enabled_xdp_vsi(vsi))
5292 			goto wait_rx;
5293 
5294 		/* Check and wait for the XDP Tx queue */
5295 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5296 				       false);
5297 		if (ret) {
5298 			dev_info(&pf->pdev->dev,
5299 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5300 				 vsi->seid, pf_q);
5301 			return ret;
5302 		}
5303 wait_rx:
5304 		/* Check and wait for the Rx queue */
5305 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5306 		if (ret) {
5307 			dev_info(&pf->pdev->dev,
5308 				 "VSI seid %d Rx ring %d disable timeout\n",
5309 				 vsi->seid, pf_q);
5310 			return ret;
5311 		}
5312 	}
5313 
5314 	return 0;
5315 }
5316 
5317 #ifdef CONFIG_I40E_DCB
5318 /**
5319  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5320  * @pf: the PF
5321  *
5322  * This function waits for the queues to be in disabled state for all the
5323  * VSIs that are managed by this PF.
5324  **/
5325 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5326 {
5327 	struct i40e_vsi *vsi;
5328 	int v, ret = 0;
5329 
5330 	i40e_pf_for_each_vsi(pf, v, vsi) {
5331 		ret = i40e_vsi_wait_queues_disabled(vsi);
5332 		if (ret)
5333 			break;
5334 	}
5335 
5336 	return ret;
5337 }
5338 
5339 #endif
5340 
5341 /**
5342  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5343  * @pf: pointer to PF
5344  *
5345  * Get TC map for ISCSI PF type that will include iSCSI TC
5346  * and LAN TC.
5347  **/
5348 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5349 {
5350 	struct i40e_dcb_app_priority_table app;
5351 	struct i40e_hw *hw = &pf->hw;
5352 	u8 enabled_tc = 1; /* TC0 is always enabled */
5353 	u8 tc, i;
5354 	/* Get the iSCSI APP TLV */
5355 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5356 
5357 	for (i = 0; i < dcbcfg->numapps; i++) {
5358 		app = dcbcfg->app[i];
5359 		if (app.selector == I40E_APP_SEL_TCPIP &&
5360 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5361 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5362 			enabled_tc |= BIT(tc);
5363 			break;
5364 		}
5365 	}
5366 
5367 	return enabled_tc;
5368 }
5369 
5370 /**
5371  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5372  * @dcbcfg: the corresponding DCBx configuration structure
5373  *
5374  * Return the number of TCs from given DCBx configuration
5375  **/
5376 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5377 {
5378 	int i, tc_unused = 0;
5379 	u8 num_tc = 0;
5380 	u8 ret = 0;
5381 
5382 	/* Scan the ETS Config Priority Table to find
5383 	 * traffic class enabled for a given priority
5384 	 * and create a bitmask of enabled TCs
5385 	 */
5386 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5387 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5388 
5389 	/* Now scan the bitmask to check for
5390 	 * contiguous TCs starting with TC0
5391 	 */
5392 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5393 		if (num_tc & BIT(i)) {
5394 			if (!tc_unused) {
5395 				ret++;
5396 			} else {
5397 				pr_err("Non-contiguous TC - Disabling DCB\n");
5398 				return 1;
5399 			}
5400 		} else {
5401 			tc_unused = 1;
5402 		}
5403 	}
5404 
5405 	/* There is always at least TC0 */
5406 	if (!ret)
5407 		ret = 1;
5408 
5409 	return ret;
5410 }
5411 
5412 /**
5413  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5414  * @dcbcfg: the corresponding DCBx configuration structure
5415  *
5416  * Query the current DCB configuration and return the number of
5417  * traffic classes enabled from the given DCBX config
5418  **/
5419 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5420 {
5421 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5422 	u8 enabled_tc = 1;
5423 	u8 i;
5424 
5425 	for (i = 0; i < num_tc; i++)
5426 		enabled_tc |= BIT(i);
5427 
5428 	return enabled_tc;
5429 }
5430 
5431 /**
5432  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5433  * @pf: PF being queried
5434  *
5435  * Query the current MQPRIO configuration and return the number of
5436  * traffic classes enabled.
5437  **/
5438 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5439 {
5440 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5441 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5442 	u8 enabled_tc = 1, i;
5443 
5444 	for (i = 1; i < num_tc; i++)
5445 		enabled_tc |= BIT(i);
5446 	return enabled_tc;
5447 }
5448 
5449 /**
5450  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5451  * @pf: PF being queried
5452  *
5453  * Return number of traffic classes enabled for the given PF
5454  **/
5455 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5456 {
5457 	u8 i, enabled_tc = 1;
5458 	u8 num_tc = 0;
5459 
5460 	if (i40e_is_tc_mqprio_enabled(pf)) {
5461 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5462 
5463 		return vsi->mqprio_qopt.qopt.num_tc;
5464 	}
5465 
5466 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5467 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5468 		return 1;
5469 
5470 	/* SFP mode will be enabled for all TCs on port */
5471 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5472 		return i40e_dcb_get_num_tc(&pf->hw.local_dcbx_config);
5473 
5474 	/* MFP mode return count of enabled TCs for this PF */
5475 	if (pf->hw.func_caps.iscsi)
5476 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5477 	else
5478 		return 1; /* Only TC0 */
5479 
5480 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5481 		if (enabled_tc & BIT(i))
5482 			num_tc++;
5483 	}
5484 	return num_tc;
5485 }
5486 
5487 /**
5488  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5489  * @pf: PF being queried
5490  *
5491  * Return a bitmap for enabled traffic classes for this PF.
5492  **/
5493 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5494 {
5495 	if (i40e_is_tc_mqprio_enabled(pf))
5496 		return i40e_mqprio_get_enabled_tc(pf);
5497 
5498 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5499 	 * default TC
5500 	 */
5501 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5502 		return I40E_DEFAULT_TRAFFIC_CLASS;
5503 
5504 	/* SFP mode we want PF to be enabled for all TCs */
5505 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5506 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5507 
5508 	/* MFP enabled and iSCSI PF type */
5509 	if (pf->hw.func_caps.iscsi)
5510 		return i40e_get_iscsi_tc_map(pf);
5511 	else
5512 		return I40E_DEFAULT_TRAFFIC_CLASS;
5513 }
5514 
5515 /**
5516  * i40e_vsi_get_bw_info - Query VSI BW Information
5517  * @vsi: the VSI being queried
5518  *
5519  * Returns 0 on success, negative value on failure
5520  **/
5521 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5522 {
5523 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5524 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5525 	struct i40e_pf *pf = vsi->back;
5526 	struct i40e_hw *hw = &pf->hw;
5527 	u32 tc_bw_max;
5528 	int ret;
5529 	int i;
5530 
5531 	/* Get the VSI level BW configuration */
5532 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5533 	if (ret) {
5534 		dev_info(&pf->pdev->dev,
5535 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5536 			 ERR_PTR(ret),
5537 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5538 		return -EINVAL;
5539 	}
5540 
5541 	/* Get the VSI level BW configuration per TC */
5542 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5543 					       NULL);
5544 	if (ret) {
5545 		dev_info(&pf->pdev->dev,
5546 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5547 			 ERR_PTR(ret),
5548 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5549 		return -EINVAL;
5550 	}
5551 
5552 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5553 		dev_info(&pf->pdev->dev,
5554 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5555 			 bw_config.tc_valid_bits,
5556 			 bw_ets_config.tc_valid_bits);
5557 		/* Still continuing */
5558 	}
5559 
5560 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5561 	vsi->bw_max_quanta = bw_config.max_bw;
5562 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5563 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5564 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5565 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5566 		vsi->bw_ets_limit_credits[i] =
5567 					le16_to_cpu(bw_ets_config.credits[i]);
5568 		/* 3 bits out of 4 for each TC */
5569 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5570 	}
5571 
5572 	return 0;
5573 }
5574 
5575 /**
5576  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5577  * @vsi: the VSI being configured
5578  * @enabled_tc: TC bitmap
5579  * @bw_share: BW shared credits per TC
5580  *
5581  * Returns 0 on success, negative value on failure
5582  **/
5583 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5584 				       u8 *bw_share)
5585 {
5586 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5587 	struct i40e_pf *pf = vsi->back;
5588 	int ret;
5589 	int i;
5590 
5591 	/* There is no need to reset BW when mqprio mode is on.  */
5592 	if (i40e_is_tc_mqprio_enabled(pf))
5593 		return 0;
5594 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5595 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5596 		if (ret)
5597 			dev_info(&pf->pdev->dev,
5598 				 "Failed to reset tx rate for vsi->seid %u\n",
5599 				 vsi->seid);
5600 		return ret;
5601 	}
5602 	memset(&bw_data, 0, sizeof(bw_data));
5603 	bw_data.tc_valid_bits = enabled_tc;
5604 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5605 		bw_data.tc_bw_credits[i] = bw_share[i];
5606 
5607 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5608 	if (ret) {
5609 		dev_info(&pf->pdev->dev,
5610 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5611 			 pf->hw.aq.asq_last_status);
5612 		return -EINVAL;
5613 	}
5614 
5615 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5616 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5617 
5618 	return 0;
5619 }
5620 
5621 /**
5622  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5623  * @vsi: the VSI being configured
5624  * @enabled_tc: TC map to be enabled
5625  *
5626  **/
5627 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5628 {
5629 	struct net_device *netdev = vsi->netdev;
5630 	struct i40e_pf *pf = vsi->back;
5631 	struct i40e_hw *hw = &pf->hw;
5632 	u8 netdev_tc = 0;
5633 	int i;
5634 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5635 
5636 	if (!netdev)
5637 		return;
5638 
5639 	if (!enabled_tc) {
5640 		netdev_reset_tc(netdev);
5641 		return;
5642 	}
5643 
5644 	/* Set up actual enabled TCs on the VSI */
5645 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5646 		return;
5647 
5648 	/* set per TC queues for the VSI */
5649 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5650 		/* Only set TC queues for enabled tcs
5651 		 *
5652 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5653 		 * enabled_tc bitmap would be 0x00001001; the driver
5654 		 * will set the numtc for netdev as 2 that will be
5655 		 * referenced by the netdev layer as TC 0 and 1.
5656 		 */
5657 		if (vsi->tc_config.enabled_tc & BIT(i))
5658 			netdev_set_tc_queue(netdev,
5659 					vsi->tc_config.tc_info[i].netdev_tc,
5660 					vsi->tc_config.tc_info[i].qcount,
5661 					vsi->tc_config.tc_info[i].qoffset);
5662 	}
5663 
5664 	if (i40e_is_tc_mqprio_enabled(pf))
5665 		return;
5666 
5667 	/* Assign UP2TC map for the VSI */
5668 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5669 		/* Get the actual TC# for the UP */
5670 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5671 		/* Get the mapped netdev TC# for the UP */
5672 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5673 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5674 	}
5675 }
5676 
5677 /**
5678  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5679  * @vsi: the VSI being configured
5680  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5681  **/
5682 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5683 				      struct i40e_vsi_context *ctxt)
5684 {
5685 	/* copy just the sections touched not the entire info
5686 	 * since not all sections are valid as returned by
5687 	 * update vsi params
5688 	 */
5689 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5690 	memcpy(&vsi->info.queue_mapping,
5691 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5692 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5693 	       sizeof(vsi->info.tc_mapping));
5694 }
5695 
5696 /**
5697  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5698  * @vsi: the VSI being reconfigured
5699  * @vsi_offset: offset from main VF VSI
5700  */
5701 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5702 {
5703 	struct i40e_vsi_context ctxt = {};
5704 	struct i40e_pf *pf;
5705 	struct i40e_hw *hw;
5706 	int ret;
5707 
5708 	if (!vsi)
5709 		return -EINVAL;
5710 	pf = vsi->back;
5711 	hw = &pf->hw;
5712 
5713 	ctxt.seid = vsi->seid;
5714 	ctxt.pf_num = hw->pf_id;
5715 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5716 	ctxt.uplink_seid = vsi->uplink_seid;
5717 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5718 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5719 	ctxt.info = vsi->info;
5720 
5721 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5722 				 false);
5723 	if (vsi->reconfig_rss) {
5724 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5725 				      vsi->num_queue_pairs);
5726 		ret = i40e_vsi_config_rss(vsi);
5727 		if (ret) {
5728 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5729 			return ret;
5730 		}
5731 		vsi->reconfig_rss = false;
5732 	}
5733 
5734 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5735 	if (ret) {
5736 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5737 			 ERR_PTR(ret),
5738 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5739 		return ret;
5740 	}
5741 	/* update the local VSI info with updated queue map */
5742 	i40e_vsi_update_queue_map(vsi, &ctxt);
5743 	vsi->info.valid_sections = 0;
5744 
5745 	return ret;
5746 }
5747 
5748 /**
5749  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5750  * @vsi: VSI to be configured
5751  * @enabled_tc: TC bitmap
5752  *
5753  * This configures a particular VSI for TCs that are mapped to the
5754  * given TC bitmap. It uses default bandwidth share for TCs across
5755  * VSIs to configure TC for a particular VSI.
5756  *
5757  * NOTE:
5758  * It is expected that the VSI queues have been quisced before calling
5759  * this function.
5760  **/
5761 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5762 {
5763 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5764 	struct i40e_pf *pf = vsi->back;
5765 	struct i40e_hw *hw = &pf->hw;
5766 	struct i40e_vsi_context ctxt;
5767 	int ret = 0;
5768 	int i;
5769 
5770 	/* Check if enabled_tc is same as existing or new TCs */
5771 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5772 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5773 		return ret;
5774 
5775 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5776 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5777 		if (enabled_tc & BIT(i))
5778 			bw_share[i] = 1;
5779 	}
5780 
5781 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5782 	if (ret) {
5783 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5784 
5785 		dev_info(&pf->pdev->dev,
5786 			 "Failed configuring TC map %d for VSI %d\n",
5787 			 enabled_tc, vsi->seid);
5788 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5789 						  &bw_config, NULL);
5790 		if (ret) {
5791 			dev_info(&pf->pdev->dev,
5792 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5793 				 ERR_PTR(ret),
5794 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5795 			goto out;
5796 		}
5797 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5798 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5799 
5800 			if (!valid_tc)
5801 				valid_tc = bw_config.tc_valid_bits;
5802 			/* Always enable TC0, no matter what */
5803 			valid_tc |= 1;
5804 			dev_info(&pf->pdev->dev,
5805 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5806 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5807 			enabled_tc = valid_tc;
5808 		}
5809 
5810 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5811 		if (ret) {
5812 			dev_err(&pf->pdev->dev,
5813 				"Unable to  configure TC map %d for VSI %d\n",
5814 				enabled_tc, vsi->seid);
5815 			goto out;
5816 		}
5817 	}
5818 
5819 	/* Update Queue Pairs Mapping for currently enabled UPs */
5820 	ctxt.seid = vsi->seid;
5821 	ctxt.pf_num = vsi->back->hw.pf_id;
5822 	ctxt.vf_num = 0;
5823 	ctxt.uplink_seid = vsi->uplink_seid;
5824 	ctxt.info = vsi->info;
5825 	if (i40e_is_tc_mqprio_enabled(pf)) {
5826 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5827 		if (ret)
5828 			goto out;
5829 	} else {
5830 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5831 	}
5832 
5833 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5834 	 * queues changed.
5835 	 */
5836 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5837 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5838 				      vsi->num_queue_pairs);
5839 		ret = i40e_vsi_config_rss(vsi);
5840 		if (ret) {
5841 			dev_info(&vsi->back->pdev->dev,
5842 				 "Failed to reconfig rss for num_queues\n");
5843 			return ret;
5844 		}
5845 		vsi->reconfig_rss = false;
5846 	}
5847 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5848 		ctxt.info.valid_sections |=
5849 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5850 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5851 	}
5852 
5853 	/* Update the VSI after updating the VSI queue-mapping
5854 	 * information
5855 	 */
5856 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5857 	if (ret) {
5858 		dev_info(&pf->pdev->dev,
5859 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5860 			 ERR_PTR(ret),
5861 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5862 		goto out;
5863 	}
5864 	/* update the local VSI info with updated queue map */
5865 	i40e_vsi_update_queue_map(vsi, &ctxt);
5866 	vsi->info.valid_sections = 0;
5867 
5868 	/* Update current VSI BW information */
5869 	ret = i40e_vsi_get_bw_info(vsi);
5870 	if (ret) {
5871 		dev_info(&pf->pdev->dev,
5872 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5873 			 ERR_PTR(ret),
5874 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5875 		goto out;
5876 	}
5877 
5878 	/* Update the netdev TC setup */
5879 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5880 out:
5881 	return ret;
5882 }
5883 
5884 /**
5885  * i40e_vsi_reconfig_tc - Reconfigure VSI Tx Scheduler for stored TC map
5886  * @vsi: VSI to be reconfigured
5887  *
5888  * This reconfigures a particular VSI for TCs that are mapped to the
5889  * TC bitmap stored previously for the VSI.
5890  *
5891  * Context: It is expected that the VSI queues have been quisced before
5892  *          calling this function.
5893  *
5894  * Return: 0 on success, negative value on failure
5895  **/
5896 static int i40e_vsi_reconfig_tc(struct i40e_vsi *vsi)
5897 {
5898 	u8 enabled_tc;
5899 
5900 	enabled_tc = vsi->tc_config.enabled_tc;
5901 	vsi->tc_config.enabled_tc = 0;
5902 
5903 	return i40e_vsi_config_tc(vsi, enabled_tc);
5904 }
5905 
5906 /**
5907  * i40e_get_link_speed - Returns link speed for the interface
5908  * @vsi: VSI to be configured
5909  *
5910  **/
5911 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5912 {
5913 	struct i40e_pf *pf = vsi->back;
5914 
5915 	switch (pf->hw.phy.link_info.link_speed) {
5916 	case I40E_LINK_SPEED_40GB:
5917 		return 40000;
5918 	case I40E_LINK_SPEED_25GB:
5919 		return 25000;
5920 	case I40E_LINK_SPEED_20GB:
5921 		return 20000;
5922 	case I40E_LINK_SPEED_10GB:
5923 		return 10000;
5924 	case I40E_LINK_SPEED_1GB:
5925 		return 1000;
5926 	default:
5927 		return -EINVAL;
5928 	}
5929 }
5930 
5931 /**
5932  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5933  * @vsi: Pointer to vsi structure
5934  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5935  *
5936  * Helper function to convert units before send to set BW limit
5937  **/
5938 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5939 {
5940 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5941 		dev_warn(&vsi->back->pdev->dev,
5942 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5943 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5944 	} else {
5945 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5946 	}
5947 
5948 	return max_tx_rate;
5949 }
5950 
5951 /**
5952  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5953  * @vsi: VSI to be configured
5954  * @seid: seid of the channel/VSI
5955  * @max_tx_rate: max TX rate to be configured as BW limit
5956  *
5957  * Helper function to set BW limit for a given VSI
5958  **/
5959 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5960 {
5961 	struct i40e_pf *pf = vsi->back;
5962 	u64 credits = 0;
5963 	int speed = 0;
5964 	int ret = 0;
5965 
5966 	speed = i40e_get_link_speed(vsi);
5967 	if (max_tx_rate > speed) {
5968 		dev_err(&pf->pdev->dev,
5969 			"Invalid max tx rate %llu specified for VSI seid %d.",
5970 			max_tx_rate, seid);
5971 		return -EINVAL;
5972 	}
5973 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5974 		dev_warn(&pf->pdev->dev,
5975 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5976 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5977 	}
5978 
5979 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5980 	credits = max_tx_rate;
5981 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5982 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5983 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5984 	if (ret)
5985 		dev_err(&pf->pdev->dev,
5986 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5987 			max_tx_rate, seid, ERR_PTR(ret),
5988 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5989 	return ret;
5990 }
5991 
5992 /**
5993  * i40e_remove_queue_channels - Remove queue channels for the TCs
5994  * @vsi: VSI to be configured
5995  *
5996  * Remove queue channels for the TCs
5997  **/
5998 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5999 {
6000 	enum i40e_admin_queue_err last_aq_status;
6001 	struct i40e_cloud_filter *cfilter;
6002 	struct i40e_channel *ch, *ch_tmp;
6003 	struct i40e_pf *pf = vsi->back;
6004 	struct hlist_node *node;
6005 	int ret, i;
6006 
6007 	/* Reset rss size that was stored when reconfiguring rss for
6008 	 * channel VSIs with non-power-of-2 queue count.
6009 	 */
6010 	vsi->current_rss_size = 0;
6011 
6012 	/* perform cleanup for channels if they exist */
6013 	if (list_empty(&vsi->ch_list))
6014 		return;
6015 
6016 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6017 		struct i40e_vsi *p_vsi;
6018 
6019 		list_del(&ch->list);
6020 		p_vsi = ch->parent_vsi;
6021 		if (!p_vsi || !ch->initialized) {
6022 			kfree(ch);
6023 			continue;
6024 		}
6025 		/* Reset queue contexts */
6026 		for (i = 0; i < ch->num_queue_pairs; i++) {
6027 			struct i40e_ring *tx_ring, *rx_ring;
6028 			u16 pf_q;
6029 
6030 			pf_q = ch->base_queue + i;
6031 			tx_ring = vsi->tx_rings[pf_q];
6032 			tx_ring->ch = NULL;
6033 
6034 			rx_ring = vsi->rx_rings[pf_q];
6035 			rx_ring->ch = NULL;
6036 		}
6037 
6038 		/* Reset BW configured for this VSI via mqprio */
6039 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6040 		if (ret)
6041 			dev_info(&vsi->back->pdev->dev,
6042 				 "Failed to reset tx rate for ch->seid %u\n",
6043 				 ch->seid);
6044 
6045 		/* delete cloud filters associated with this channel */
6046 		hlist_for_each_entry_safe(cfilter, node,
6047 					  &pf->cloud_filter_list, cloud_node) {
6048 			if (cfilter->seid != ch->seid)
6049 				continue;
6050 
6051 			hash_del(&cfilter->cloud_node);
6052 			if (cfilter->dst_port)
6053 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6054 									cfilter,
6055 									false);
6056 			else
6057 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6058 								false);
6059 			last_aq_status = pf->hw.aq.asq_last_status;
6060 			if (ret)
6061 				dev_info(&pf->pdev->dev,
6062 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6063 					 ERR_PTR(ret),
6064 					 i40e_aq_str(&pf->hw, last_aq_status));
6065 			kfree(cfilter);
6066 		}
6067 
6068 		/* delete VSI from FW */
6069 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6070 					     NULL);
6071 		if (ret)
6072 			dev_err(&vsi->back->pdev->dev,
6073 				"unable to remove channel (%d) for parent VSI(%d)\n",
6074 				ch->seid, p_vsi->seid);
6075 		kfree(ch);
6076 	}
6077 	INIT_LIST_HEAD(&vsi->ch_list);
6078 }
6079 
6080 /**
6081  * i40e_get_max_queues_for_channel
6082  * @vsi: ptr to VSI to which channels are associated with
6083  *
6084  * Helper function which returns max value among the queue counts set on the
6085  * channels/TCs created.
6086  **/
6087 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6088 {
6089 	struct i40e_channel *ch, *ch_tmp;
6090 	int max = 0;
6091 
6092 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6093 		if (!ch->initialized)
6094 			continue;
6095 		if (ch->num_queue_pairs > max)
6096 			max = ch->num_queue_pairs;
6097 	}
6098 
6099 	return max;
6100 }
6101 
6102 /**
6103  * i40e_validate_num_queues - validate num_queues w.r.t channel
6104  * @pf: ptr to PF device
6105  * @num_queues: number of queues
6106  * @vsi: the parent VSI
6107  * @reconfig_rss: indicates should the RSS be reconfigured or not
6108  *
6109  * This function validates number of queues in the context of new channel
6110  * which is being established and determines if RSS should be reconfigured
6111  * or not for parent VSI.
6112  **/
6113 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6114 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6115 {
6116 	int max_ch_queues;
6117 
6118 	if (!reconfig_rss)
6119 		return -EINVAL;
6120 
6121 	*reconfig_rss = false;
6122 	if (vsi->current_rss_size) {
6123 		if (num_queues > vsi->current_rss_size) {
6124 			dev_dbg(&pf->pdev->dev,
6125 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6126 				num_queues, vsi->current_rss_size);
6127 			return -EINVAL;
6128 		} else if ((num_queues < vsi->current_rss_size) &&
6129 			   (!is_power_of_2(num_queues))) {
6130 			dev_dbg(&pf->pdev->dev,
6131 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6132 				num_queues, vsi->current_rss_size);
6133 			return -EINVAL;
6134 		}
6135 	}
6136 
6137 	if (!is_power_of_2(num_queues)) {
6138 		/* Find the max num_queues configured for channel if channel
6139 		 * exist.
6140 		 * if channel exist, then enforce 'num_queues' to be more than
6141 		 * max ever queues configured for channel.
6142 		 */
6143 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6144 		if (num_queues < max_ch_queues) {
6145 			dev_dbg(&pf->pdev->dev,
6146 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6147 				num_queues, max_ch_queues);
6148 			return -EINVAL;
6149 		}
6150 		*reconfig_rss = true;
6151 	}
6152 
6153 	return 0;
6154 }
6155 
6156 /**
6157  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6158  * @vsi: the VSI being setup
6159  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6160  *
6161  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6162  **/
6163 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6164 {
6165 	struct i40e_pf *pf = vsi->back;
6166 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6167 	struct i40e_hw *hw = &pf->hw;
6168 	int local_rss_size;
6169 	u8 *lut;
6170 	int ret;
6171 
6172 	if (!vsi->rss_size)
6173 		return -EINVAL;
6174 
6175 	if (rss_size > vsi->rss_size)
6176 		return -EINVAL;
6177 
6178 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6179 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6180 	if (!lut)
6181 		return -ENOMEM;
6182 
6183 	/* Ignoring user configured lut if there is one */
6184 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6185 
6186 	/* Use user configured hash key if there is one, otherwise
6187 	 * use default.
6188 	 */
6189 	if (vsi->rss_hkey_user)
6190 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6191 	else
6192 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6193 
6194 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6195 	if (ret) {
6196 		dev_info(&pf->pdev->dev,
6197 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6198 			 ERR_PTR(ret),
6199 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6200 		kfree(lut);
6201 		return ret;
6202 	}
6203 	kfree(lut);
6204 
6205 	/* Do the update w.r.t. storing rss_size */
6206 	if (!vsi->orig_rss_size)
6207 		vsi->orig_rss_size = vsi->rss_size;
6208 	vsi->current_rss_size = local_rss_size;
6209 
6210 	return ret;
6211 }
6212 
6213 /**
6214  * i40e_channel_setup_queue_map - Setup a channel queue map
6215  * @pf: ptr to PF device
6216  * @ctxt: VSI context structure
6217  * @ch: ptr to channel structure
6218  *
6219  * Setup queue map for a specific channel
6220  **/
6221 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6222 					 struct i40e_vsi_context *ctxt,
6223 					 struct i40e_channel *ch)
6224 {
6225 	u16 qcount, qmap, sections = 0;
6226 	u8 offset = 0;
6227 	int pow;
6228 
6229 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6230 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6231 
6232 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6233 	ch->num_queue_pairs = qcount;
6234 
6235 	/* find the next higher power-of-2 of num queue pairs */
6236 	pow = ilog2(qcount);
6237 	if (!is_power_of_2(qcount))
6238 		pow++;
6239 
6240 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6241 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6242 
6243 	/* Setup queue TC[0].qmap for given VSI context */
6244 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6245 
6246 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6247 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6248 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6249 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6250 }
6251 
6252 /**
6253  * i40e_add_channel - add a channel by adding VSI
6254  * @pf: ptr to PF device
6255  * @uplink_seid: underlying HW switching element (VEB) ID
6256  * @ch: ptr to channel structure
6257  *
6258  * Add a channel (VSI) using add_vsi and queue_map
6259  **/
6260 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6261 			    struct i40e_channel *ch)
6262 {
6263 	struct i40e_hw *hw = &pf->hw;
6264 	struct i40e_vsi_context ctxt;
6265 	u8 enabled_tc = 0x1; /* TC0 enabled */
6266 	int ret;
6267 
6268 	if (ch->type != I40E_VSI_VMDQ2) {
6269 		dev_info(&pf->pdev->dev,
6270 			 "add new vsi failed, ch->type %d\n", ch->type);
6271 		return -EINVAL;
6272 	}
6273 
6274 	memset(&ctxt, 0, sizeof(ctxt));
6275 	ctxt.pf_num = hw->pf_id;
6276 	ctxt.vf_num = 0;
6277 	ctxt.uplink_seid = uplink_seid;
6278 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6279 	if (ch->type == I40E_VSI_VMDQ2)
6280 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6281 
6282 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6283 		ctxt.info.valid_sections |=
6284 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6285 		ctxt.info.switch_id =
6286 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6287 	}
6288 
6289 	/* Set queue map for a given VSI context */
6290 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6291 
6292 	/* Now time to create VSI */
6293 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6294 	if (ret) {
6295 		dev_info(&pf->pdev->dev,
6296 			 "add new vsi failed, err %pe aq_err %s\n",
6297 			 ERR_PTR(ret),
6298 			 i40e_aq_str(&pf->hw,
6299 				     pf->hw.aq.asq_last_status));
6300 		return -ENOENT;
6301 	}
6302 
6303 	/* Success, update channel, set enabled_tc only if the channel
6304 	 * is not a macvlan
6305 	 */
6306 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6307 	ch->seid = ctxt.seid;
6308 	ch->vsi_number = ctxt.vsi_number;
6309 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6310 
6311 	/* copy just the sections touched not the entire info
6312 	 * since not all sections are valid as returned by
6313 	 * update vsi params
6314 	 */
6315 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6316 	memcpy(&ch->info.queue_mapping,
6317 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6318 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6319 	       sizeof(ctxt.info.tc_mapping));
6320 
6321 	return 0;
6322 }
6323 
6324 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6325 				  u8 *bw_share)
6326 {
6327 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6328 	int ret;
6329 	int i;
6330 
6331 	memset(&bw_data, 0, sizeof(bw_data));
6332 	bw_data.tc_valid_bits = ch->enabled_tc;
6333 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6334 		bw_data.tc_bw_credits[i] = bw_share[i];
6335 
6336 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6337 				       &bw_data, NULL);
6338 	if (ret) {
6339 		dev_info(&vsi->back->pdev->dev,
6340 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6341 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6342 		return -EINVAL;
6343 	}
6344 
6345 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6346 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6347 
6348 	return 0;
6349 }
6350 
6351 /**
6352  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6353  * @pf: ptr to PF device
6354  * @vsi: the VSI being setup
6355  * @ch: ptr to channel structure
6356  *
6357  * Configure TX rings associated with channel (VSI) since queues are being
6358  * from parent VSI.
6359  **/
6360 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6361 				       struct i40e_vsi *vsi,
6362 				       struct i40e_channel *ch)
6363 {
6364 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6365 	int ret;
6366 	int i;
6367 
6368 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6369 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6370 		if (ch->enabled_tc & BIT(i))
6371 			bw_share[i] = 1;
6372 	}
6373 
6374 	/* configure BW for new VSI */
6375 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6376 	if (ret) {
6377 		dev_info(&vsi->back->pdev->dev,
6378 			 "Failed configuring TC map %d for channel (seid %u)\n",
6379 			 ch->enabled_tc, ch->seid);
6380 		return ret;
6381 	}
6382 
6383 	for (i = 0; i < ch->num_queue_pairs; i++) {
6384 		struct i40e_ring *tx_ring, *rx_ring;
6385 		u16 pf_q;
6386 
6387 		pf_q = ch->base_queue + i;
6388 
6389 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6390 		 * context
6391 		 */
6392 		tx_ring = vsi->tx_rings[pf_q];
6393 		tx_ring->ch = ch;
6394 
6395 		/* Get the RX ring ptr */
6396 		rx_ring = vsi->rx_rings[pf_q];
6397 		rx_ring->ch = ch;
6398 	}
6399 
6400 	return 0;
6401 }
6402 
6403 /**
6404  * i40e_setup_hw_channel - setup new channel
6405  * @pf: ptr to PF device
6406  * @vsi: the VSI being setup
6407  * @ch: ptr to channel structure
6408  * @uplink_seid: underlying HW switching element (VEB) ID
6409  * @type: type of channel to be created (VMDq2/VF)
6410  *
6411  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6412  * and configures TX rings accordingly
6413  **/
6414 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6415 					struct i40e_vsi *vsi,
6416 					struct i40e_channel *ch,
6417 					u16 uplink_seid, u8 type)
6418 {
6419 	int ret;
6420 
6421 	ch->initialized = false;
6422 	ch->base_queue = vsi->next_base_queue;
6423 	ch->type = type;
6424 
6425 	/* Proceed with creation of channel (VMDq2) VSI */
6426 	ret = i40e_add_channel(pf, uplink_seid, ch);
6427 	if (ret) {
6428 		dev_info(&pf->pdev->dev,
6429 			 "failed to add_channel using uplink_seid %u\n",
6430 			 uplink_seid);
6431 		return ret;
6432 	}
6433 
6434 	/* Mark the successful creation of channel */
6435 	ch->initialized = true;
6436 
6437 	/* Reconfigure TX queues using QTX_CTL register */
6438 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6439 	if (ret) {
6440 		dev_info(&pf->pdev->dev,
6441 			 "failed to configure TX rings for channel %u\n",
6442 			 ch->seid);
6443 		return ret;
6444 	}
6445 
6446 	/* update 'next_base_queue' */
6447 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6448 	dev_dbg(&pf->pdev->dev,
6449 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6450 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6451 		ch->num_queue_pairs,
6452 		vsi->next_base_queue);
6453 	return ret;
6454 }
6455 
6456 /**
6457  * i40e_setup_channel - setup new channel using uplink element
6458  * @pf: ptr to PF device
6459  * @vsi: pointer to the VSI to set up the channel within
6460  * @ch: ptr to channel structure
6461  *
6462  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6463  * and uplink switching element (uplink_seid)
6464  **/
6465 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6466 			       struct i40e_channel *ch)
6467 {
6468 	struct i40e_vsi *main_vsi;
6469 	u8 vsi_type;
6470 	u16 seid;
6471 	int ret;
6472 
6473 	if (vsi->type == I40E_VSI_MAIN) {
6474 		vsi_type = I40E_VSI_VMDQ2;
6475 	} else {
6476 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6477 			vsi->type);
6478 		return false;
6479 	}
6480 
6481 	/* underlying switching element */
6482 	main_vsi = i40e_pf_get_main_vsi(pf);
6483 	seid = main_vsi->uplink_seid;
6484 
6485 	/* create channel (VSI), configure TX rings */
6486 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6487 	if (ret) {
6488 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6489 		return false;
6490 	}
6491 
6492 	return ch->initialized ? true : false;
6493 }
6494 
6495 /**
6496  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6497  * @vsi: ptr to VSI which has PF backing
6498  *
6499  * Sets up switch mode correctly if it needs to be changed and perform
6500  * what are allowed modes.
6501  **/
6502 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6503 {
6504 	u8 mode;
6505 	struct i40e_pf *pf = vsi->back;
6506 	struct i40e_hw *hw = &pf->hw;
6507 	int ret;
6508 
6509 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6510 	if (ret)
6511 		return -EINVAL;
6512 
6513 	if (hw->dev_caps.switch_mode) {
6514 		/* if switch mode is set, support mode2 (non-tunneled for
6515 		 * cloud filter) for now
6516 		 */
6517 		u32 switch_mode = hw->dev_caps.switch_mode &
6518 				  I40E_SWITCH_MODE_MASK;
6519 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6520 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6521 				return 0;
6522 			dev_err(&pf->pdev->dev,
6523 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6524 				hw->dev_caps.switch_mode);
6525 			return -EINVAL;
6526 		}
6527 	}
6528 
6529 	/* Set Bit 7 to be valid */
6530 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6531 
6532 	/* Set L4type for TCP support */
6533 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6534 
6535 	/* Set cloud filter mode */
6536 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6537 
6538 	/* Prep mode field for set_switch_config */
6539 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6540 					pf->last_sw_conf_valid_flags,
6541 					mode, NULL);
6542 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6543 		dev_err(&pf->pdev->dev,
6544 			"couldn't set switch config bits, err %pe aq_err %s\n",
6545 			ERR_PTR(ret),
6546 			i40e_aq_str(hw,
6547 				    hw->aq.asq_last_status));
6548 
6549 	return ret;
6550 }
6551 
6552 /**
6553  * i40e_create_queue_channel - function to create channel
6554  * @vsi: VSI to be configured
6555  * @ch: ptr to channel (it contains channel specific params)
6556  *
6557  * This function creates channel (VSI) using num_queues specified by user,
6558  * reconfigs RSS if needed.
6559  **/
6560 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6561 			      struct i40e_channel *ch)
6562 {
6563 	struct i40e_pf *pf = vsi->back;
6564 	bool reconfig_rss;
6565 	int err;
6566 
6567 	if (!ch)
6568 		return -EINVAL;
6569 
6570 	if (!ch->num_queue_pairs) {
6571 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6572 			ch->num_queue_pairs);
6573 		return -EINVAL;
6574 	}
6575 
6576 	/* validate user requested num_queues for channel */
6577 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6578 				       &reconfig_rss);
6579 	if (err) {
6580 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6581 			 ch->num_queue_pairs);
6582 		return -EINVAL;
6583 	}
6584 
6585 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6586 	 * VSI to be added switch to VEB mode.
6587 	 */
6588 
6589 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6590 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6591 
6592 		if (vsi->type == I40E_VSI_MAIN) {
6593 			if (i40e_is_tc_mqprio_enabled(pf))
6594 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6595 			else
6596 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6597 		}
6598 		/* now onwards for main VSI, number of queues will be value
6599 		 * of TC0's queue count
6600 		 */
6601 	}
6602 
6603 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6604 	 * it should be more than num_queues
6605 	 */
6606 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6607 		dev_dbg(&pf->pdev->dev,
6608 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6609 			vsi->cnt_q_avail, ch->num_queue_pairs);
6610 		return -EINVAL;
6611 	}
6612 
6613 	/* reconfig_rss only if vsi type is MAIN_VSI */
6614 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6615 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6616 		if (err) {
6617 			dev_info(&pf->pdev->dev,
6618 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6619 				 ch->num_queue_pairs);
6620 			return -EINVAL;
6621 		}
6622 	}
6623 
6624 	if (!i40e_setup_channel(pf, vsi, ch)) {
6625 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6626 		return -EINVAL;
6627 	}
6628 
6629 	dev_info(&pf->pdev->dev,
6630 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6631 		 ch->seid, ch->num_queue_pairs);
6632 
6633 	/* configure VSI for BW limit */
6634 	if (ch->max_tx_rate) {
6635 		u64 credits = ch->max_tx_rate;
6636 
6637 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6638 			return -EINVAL;
6639 
6640 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6641 		dev_dbg(&pf->pdev->dev,
6642 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6643 			ch->max_tx_rate,
6644 			credits,
6645 			ch->seid);
6646 	}
6647 
6648 	/* in case of VF, this will be main SRIOV VSI */
6649 	ch->parent_vsi = vsi;
6650 
6651 	/* and update main_vsi's count for queue_available to use */
6652 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6653 
6654 	return 0;
6655 }
6656 
6657 /**
6658  * i40e_configure_queue_channels - Add queue channel for the given TCs
6659  * @vsi: VSI to be configured
6660  *
6661  * Configures queue channel mapping to the given TCs
6662  **/
6663 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6664 {
6665 	struct i40e_channel *ch;
6666 	u64 max_rate = 0;
6667 	int ret = 0, i;
6668 
6669 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6670 	vsi->tc_seid_map[0] = vsi->seid;
6671 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6672 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6673 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6674 			if (!ch) {
6675 				ret = -ENOMEM;
6676 				goto err_free;
6677 			}
6678 
6679 			INIT_LIST_HEAD(&ch->list);
6680 			ch->num_queue_pairs =
6681 				vsi->tc_config.tc_info[i].qcount;
6682 			ch->base_queue =
6683 				vsi->tc_config.tc_info[i].qoffset;
6684 
6685 			/* Bandwidth limit through tc interface is in bytes/s,
6686 			 * change to Mbit/s
6687 			 */
6688 			max_rate = vsi->mqprio_qopt.max_rate[i];
6689 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6690 			ch->max_tx_rate = max_rate;
6691 
6692 			list_add_tail(&ch->list, &vsi->ch_list);
6693 
6694 			ret = i40e_create_queue_channel(vsi, ch);
6695 			if (ret) {
6696 				dev_err(&vsi->back->pdev->dev,
6697 					"Failed creating queue channel with TC%d: queues %d\n",
6698 					i, ch->num_queue_pairs);
6699 				goto err_free;
6700 			}
6701 			vsi->tc_seid_map[i] = ch->seid;
6702 		}
6703 	}
6704 
6705 	/* reset to reconfigure TX queue contexts */
6706 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6707 	return ret;
6708 
6709 err_free:
6710 	i40e_remove_queue_channels(vsi);
6711 	return ret;
6712 }
6713 
6714 /**
6715  * i40e_veb_config_tc - Configure TCs for given VEB
6716  * @veb: given VEB
6717  * @enabled_tc: TC bitmap
6718  *
6719  * Configures given TC bitmap for VEB (switching) element
6720  **/
6721 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6722 {
6723 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6724 	struct i40e_pf *pf = veb->pf;
6725 	int ret = 0;
6726 	int i;
6727 
6728 	/* No TCs or already enabled TCs just return */
6729 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6730 		return ret;
6731 
6732 	bw_data.tc_valid_bits = enabled_tc;
6733 	/* bw_data.absolute_credits is not set (relative) */
6734 
6735 	/* Enable ETS TCs with equal BW Share for now */
6736 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6737 		if (enabled_tc & BIT(i))
6738 			bw_data.tc_bw_share_credits[i] = 1;
6739 	}
6740 
6741 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6742 						   &bw_data, NULL);
6743 	if (ret) {
6744 		dev_info(&pf->pdev->dev,
6745 			 "VEB bw config failed, err %pe aq_err %s\n",
6746 			 ERR_PTR(ret),
6747 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6748 		goto out;
6749 	}
6750 
6751 	/* Update the BW information */
6752 	ret = i40e_veb_get_bw_info(veb);
6753 	if (ret) {
6754 		dev_info(&pf->pdev->dev,
6755 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6756 			 ERR_PTR(ret),
6757 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6758 	}
6759 
6760 out:
6761 	return ret;
6762 }
6763 
6764 #ifdef CONFIG_I40E_DCB
6765 /**
6766  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6767  * @pf: PF struct
6768  *
6769  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6770  * the caller would've quiesce all the VSIs before calling
6771  * this function
6772  **/
6773 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6774 {
6775 	struct i40e_vsi *vsi;
6776 	struct i40e_veb *veb;
6777 	u8 tc_map = 0;
6778 	int ret;
6779 	int v;
6780 
6781 	/* Enable the TCs available on PF to all VEBs */
6782 	tc_map = i40e_pf_get_tc_map(pf);
6783 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6784 		return;
6785 
6786 	i40e_pf_for_each_veb(pf, v, veb) {
6787 		ret = i40e_veb_config_tc(veb, tc_map);
6788 		if (ret) {
6789 			dev_info(&pf->pdev->dev,
6790 				 "Failed configuring TC for VEB seid=%d\n",
6791 				 veb->seid);
6792 			/* Will try to configure as many components */
6793 		}
6794 	}
6795 
6796 	/* Update each VSI */
6797 	i40e_pf_for_each_vsi(pf, v, vsi) {
6798 		/* - Enable all TCs for the LAN VSI
6799 		 * - For all others keep them at TC0 for now
6800 		 */
6801 		if (vsi->type == I40E_VSI_MAIN)
6802 			tc_map = i40e_pf_get_tc_map(pf);
6803 		else
6804 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6805 
6806 		ret = i40e_vsi_config_tc(vsi, tc_map);
6807 		if (ret) {
6808 			dev_info(&pf->pdev->dev,
6809 				 "Failed configuring TC for VSI seid=%d\n",
6810 				 vsi->seid);
6811 			/* Will try to configure as many components */
6812 		} else {
6813 			/* Re-configure VSI vectors based on updated TC map */
6814 			i40e_vsi_map_rings_to_vectors(vsi);
6815 			if (vsi->netdev)
6816 				i40e_dcbnl_set_all(vsi);
6817 		}
6818 	}
6819 }
6820 
6821 /**
6822  * i40e_resume_port_tx - Resume port Tx
6823  * @pf: PF struct
6824  *
6825  * Resume a port's Tx and issue a PF reset in case of failure to
6826  * resume.
6827  **/
6828 static int i40e_resume_port_tx(struct i40e_pf *pf)
6829 {
6830 	struct i40e_hw *hw = &pf->hw;
6831 	int ret;
6832 
6833 	ret = i40e_aq_resume_port_tx(hw, NULL);
6834 	if (ret) {
6835 		dev_info(&pf->pdev->dev,
6836 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6837 			  ERR_PTR(ret),
6838 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6839 		/* Schedule PF reset to recover */
6840 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6841 		i40e_service_event_schedule(pf);
6842 	}
6843 
6844 	return ret;
6845 }
6846 
6847 /**
6848  * i40e_suspend_port_tx - Suspend port Tx
6849  * @pf: PF struct
6850  *
6851  * Suspend a port's Tx and issue a PF reset in case of failure.
6852  **/
6853 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6854 {
6855 	struct i40e_hw *hw = &pf->hw;
6856 	int ret;
6857 
6858 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6859 	if (ret) {
6860 		dev_info(&pf->pdev->dev,
6861 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6862 			 ERR_PTR(ret),
6863 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6864 		/* Schedule PF reset to recover */
6865 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6866 		i40e_service_event_schedule(pf);
6867 	}
6868 
6869 	return ret;
6870 }
6871 
6872 /**
6873  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6874  * @pf: PF being configured
6875  * @new_cfg: New DCBX configuration
6876  *
6877  * Program DCB settings into HW and reconfigure VEB/VSIs on
6878  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6879  **/
6880 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6881 				  struct i40e_dcbx_config *new_cfg)
6882 {
6883 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6884 	int ret;
6885 
6886 	/* Check if need reconfiguration */
6887 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6888 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6889 		return 0;
6890 	}
6891 
6892 	/* Config change disable all VSIs */
6893 	i40e_pf_quiesce_all_vsi(pf);
6894 
6895 	/* Copy the new config to the current config */
6896 	*old_cfg = *new_cfg;
6897 	old_cfg->etsrec = old_cfg->etscfg;
6898 	ret = i40e_set_dcb_config(&pf->hw);
6899 	if (ret) {
6900 		dev_info(&pf->pdev->dev,
6901 			 "Set DCB Config failed, err %pe aq_err %s\n",
6902 			 ERR_PTR(ret),
6903 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6904 		goto out;
6905 	}
6906 
6907 	/* Changes in configuration update VEB/VSI */
6908 	i40e_dcb_reconfigure(pf);
6909 out:
6910 	/* In case of reset do not try to resume anything */
6911 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6912 		/* Re-start the VSIs if disabled */
6913 		ret = i40e_resume_port_tx(pf);
6914 		/* In case of error no point in resuming VSIs */
6915 		if (ret)
6916 			goto err;
6917 		i40e_pf_unquiesce_all_vsi(pf);
6918 	}
6919 err:
6920 	return ret;
6921 }
6922 
6923 /**
6924  * i40e_hw_dcb_config - Program new DCBX settings into HW
6925  * @pf: PF being configured
6926  * @new_cfg: New DCBX configuration
6927  *
6928  * Program DCB settings into HW and reconfigure VEB/VSIs on
6929  * given PF
6930  **/
6931 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6932 {
6933 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6934 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6935 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6936 	struct i40e_dcbx_config *old_cfg;
6937 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6938 	struct i40e_rx_pb_config pb_cfg;
6939 	struct i40e_hw *hw = &pf->hw;
6940 	u8 num_ports = hw->num_ports;
6941 	bool need_reconfig;
6942 	int ret = -EINVAL;
6943 	u8 lltc_map = 0;
6944 	u8 tc_map = 0;
6945 	u8 new_numtc;
6946 	u8 i;
6947 
6948 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6949 	/* Un-pack information to Program ETS HW via shared API
6950 	 * numtc, tcmap
6951 	 * LLTC map
6952 	 * ETS/NON-ETS arbiter mode
6953 	 * max exponent (credit refills)
6954 	 * Total number of ports
6955 	 * PFC priority bit-map
6956 	 * Priority Table
6957 	 * BW % per TC
6958 	 * Arbiter mode between UPs sharing same TC
6959 	 * TSA table (ETS or non-ETS)
6960 	 * EEE enabled or not
6961 	 * MFS TC table
6962 	 */
6963 
6964 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6965 
6966 	memset(&ets_data, 0, sizeof(ets_data));
6967 	for (i = 0; i < new_numtc; i++) {
6968 		tc_map |= BIT(i);
6969 		switch (new_cfg->etscfg.tsatable[i]) {
6970 		case I40E_IEEE_TSA_ETS:
6971 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6972 			ets_data.tc_bw_share_credits[i] =
6973 					new_cfg->etscfg.tcbwtable[i];
6974 			break;
6975 		case I40E_IEEE_TSA_STRICT:
6976 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6977 			lltc_map |= BIT(i);
6978 			ets_data.tc_bw_share_credits[i] =
6979 					I40E_DCB_STRICT_PRIO_CREDITS;
6980 			break;
6981 		default:
6982 			/* Invalid TSA type */
6983 			need_reconfig = false;
6984 			goto out;
6985 		}
6986 	}
6987 
6988 	old_cfg = &hw->local_dcbx_config;
6989 	/* Check if need reconfiguration */
6990 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6991 
6992 	/* If needed, enable/disable frame tagging, disable all VSIs
6993 	 * and suspend port tx
6994 	 */
6995 	if (need_reconfig) {
6996 		/* Enable DCB tagging only when more than one TC */
6997 		if (new_numtc > 1)
6998 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
6999 		else
7000 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7001 
7002 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7003 		/* Reconfiguration needed quiesce all VSIs */
7004 		i40e_pf_quiesce_all_vsi(pf);
7005 		ret = i40e_suspend_port_tx(pf);
7006 		if (ret)
7007 			goto err;
7008 	}
7009 
7010 	/* Configure Port ETS Tx Scheduler */
7011 	ets_data.tc_valid_bits = tc_map;
7012 	ets_data.tc_strict_priority_flags = lltc_map;
7013 	ret = i40e_aq_config_switch_comp_ets
7014 		(hw, pf->mac_seid, &ets_data,
7015 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7016 	if (ret) {
7017 		dev_info(&pf->pdev->dev,
7018 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7019 			 ERR_PTR(ret),
7020 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7021 		goto out;
7022 	}
7023 
7024 	/* Configure Rx ETS HW */
7025 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7026 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7027 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7028 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7029 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7030 				   lltc_map);
7031 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7032 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7033 				     prio_type);
7034 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7035 			       new_cfg->etscfg.prioritytable);
7036 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7037 
7038 	/* Configure Rx Packet Buffers in HW */
7039 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7040 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
7041 
7042 		mfs_tc[i] = main_vsi->netdev->mtu;
7043 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7044 	}
7045 
7046 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7047 					 false, new_cfg->pfc.pfcenable,
7048 					 mfs_tc, &pb_cfg);
7049 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7050 
7051 	/* Update the local Rx Packet buffer config */
7052 	pf->pb_cfg = pb_cfg;
7053 
7054 	/* Inform the FW about changes to DCB configuration */
7055 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7056 	if (ret) {
7057 		dev_info(&pf->pdev->dev,
7058 			 "DCB Updated failed, err %pe aq_err %s\n",
7059 			 ERR_PTR(ret),
7060 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7061 		goto out;
7062 	}
7063 
7064 	/* Update the port DCBx configuration */
7065 	*old_cfg = *new_cfg;
7066 
7067 	/* Changes in configuration update VEB/VSI */
7068 	i40e_dcb_reconfigure(pf);
7069 out:
7070 	/* Re-start the VSIs if disabled */
7071 	if (need_reconfig) {
7072 		ret = i40e_resume_port_tx(pf);
7073 
7074 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7075 		/* In case of error no point in resuming VSIs */
7076 		if (ret)
7077 			goto err;
7078 
7079 		/* Wait for the PF's queues to be disabled */
7080 		ret = i40e_pf_wait_queues_disabled(pf);
7081 		if (ret) {
7082 			/* Schedule PF reset to recover */
7083 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7084 			i40e_service_event_schedule(pf);
7085 			goto err;
7086 		} else {
7087 			i40e_pf_unquiesce_all_vsi(pf);
7088 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7089 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7090 		}
7091 		/* registers are set, lets apply */
7092 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7093 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7094 	}
7095 
7096 err:
7097 	return ret;
7098 }
7099 
7100 /**
7101  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7102  * @pf: PF being queried
7103  *
7104  * Set default DCB configuration in case DCB is to be done in SW.
7105  **/
7106 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7107 {
7108 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7109 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7110 	struct i40e_hw *hw = &pf->hw;
7111 	int err;
7112 
7113 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7114 		/* Update the local cached instance with TC0 ETS */
7115 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7116 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7117 		pf->tmp_cfg.etscfg.maxtcs = 0;
7118 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7119 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7120 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7121 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7122 		/* FW needs one App to configure HW */
7123 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7124 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7125 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7126 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7127 
7128 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7129 	}
7130 
7131 	memset(&ets_data, 0, sizeof(ets_data));
7132 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7133 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7134 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7135 
7136 	/* Enable ETS on the Physical port */
7137 	err = i40e_aq_config_switch_comp_ets
7138 		(hw, pf->mac_seid, &ets_data,
7139 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7140 	if (err) {
7141 		dev_info(&pf->pdev->dev,
7142 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7143 			 ERR_PTR(err),
7144 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7145 		err = -ENOENT;
7146 		goto out;
7147 	}
7148 
7149 	/* Update the local cached instance with TC0 ETS */
7150 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7151 	dcb_cfg->etscfg.cbs = 0;
7152 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7153 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7154 
7155 out:
7156 	return err;
7157 }
7158 
7159 /**
7160  * i40e_init_pf_dcb - Initialize DCB configuration
7161  * @pf: PF being configured
7162  *
7163  * Query the current DCB configuration and cache it
7164  * in the hardware structure
7165  **/
7166 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7167 {
7168 	struct i40e_hw *hw = &pf->hw;
7169 	int err;
7170 
7171 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7172 	 * Also do not enable DCBx if FW LLDP agent is disabled
7173 	 */
7174 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7175 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7176 		err = -EOPNOTSUPP;
7177 		goto out;
7178 	}
7179 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7180 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7181 		err = i40e_dcb_sw_default_config(pf);
7182 		if (err) {
7183 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7184 			goto out;
7185 		}
7186 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7187 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7188 			       DCB_CAP_DCBX_VER_IEEE;
7189 		/* at init capable but disabled */
7190 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7191 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7192 		goto out;
7193 	}
7194 	err = i40e_init_dcb(hw, true);
7195 	if (!err) {
7196 		/* Device/Function is not DCBX capable */
7197 		if ((!hw->func_caps.dcb) ||
7198 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7199 			dev_info(&pf->pdev->dev,
7200 				 "DCBX offload is not supported or is disabled for this PF.\n");
7201 		} else {
7202 			/* When status is not DISABLED then DCBX in FW */
7203 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7204 				       DCB_CAP_DCBX_VER_IEEE;
7205 
7206 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7207 			/* Enable DCB tagging only when more than one TC
7208 			 * or explicitly disable if only one TC
7209 			 */
7210 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7211 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7212 			else
7213 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7214 			dev_dbg(&pf->pdev->dev,
7215 				"DCBX offload is supported for this PF.\n");
7216 		}
7217 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7218 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7219 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7220 	} else {
7221 		dev_info(&pf->pdev->dev,
7222 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7223 			 ERR_PTR(err),
7224 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7225 	}
7226 
7227 out:
7228 	return err;
7229 }
7230 #endif /* CONFIG_I40E_DCB */
7231 
7232 static void i40e_print_link_message_eee(struct i40e_vsi *vsi,
7233 					const char *speed, const char *fc)
7234 {
7235 	struct ethtool_keee kedata;
7236 
7237 	memzero_explicit(&kedata, sizeof(kedata));
7238 	if (vsi->netdev->ethtool_ops->get_eee)
7239 		vsi->netdev->ethtool_ops->get_eee(vsi->netdev, &kedata);
7240 
7241 	if (!linkmode_empty(kedata.supported))
7242 		netdev_info(vsi->netdev,
7243 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s, EEE: %s\n",
7244 			    speed, fc,
7245 			    kedata.eee_enabled ? "Enabled" : "Disabled");
7246 	else
7247 		netdev_info(vsi->netdev,
7248 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7249 			    speed, fc);
7250 }
7251 
7252 /**
7253  * i40e_print_link_message - print link up or down
7254  * @vsi: the VSI for which link needs a message
7255  * @isup: true of link is up, false otherwise
7256  */
7257 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7258 {
7259 	enum i40e_aq_link_speed new_speed;
7260 	struct i40e_pf *pf = vsi->back;
7261 	char *speed = "Unknown";
7262 	char *fc = "Unknown";
7263 	char *fec = "";
7264 	char *req_fec = "";
7265 	char *an = "";
7266 
7267 	if (isup)
7268 		new_speed = pf->hw.phy.link_info.link_speed;
7269 	else
7270 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7271 
7272 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7273 		return;
7274 	vsi->current_isup = isup;
7275 	vsi->current_speed = new_speed;
7276 	if (!isup) {
7277 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7278 		return;
7279 	}
7280 
7281 	/* Warn user if link speed on NPAR enabled partition is not at
7282 	 * least 10GB
7283 	 */
7284 	if (pf->hw.func_caps.npar_enable &&
7285 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7286 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7287 		netdev_warn(vsi->netdev,
7288 			    "The partition detected link speed that is less than 10Gbps\n");
7289 
7290 	switch (pf->hw.phy.link_info.link_speed) {
7291 	case I40E_LINK_SPEED_40GB:
7292 		speed = "40 G";
7293 		break;
7294 	case I40E_LINK_SPEED_20GB:
7295 		speed = "20 G";
7296 		break;
7297 	case I40E_LINK_SPEED_25GB:
7298 		speed = "25 G";
7299 		break;
7300 	case I40E_LINK_SPEED_10GB:
7301 		speed = "10 G";
7302 		break;
7303 	case I40E_LINK_SPEED_5GB:
7304 		speed = "5 G";
7305 		break;
7306 	case I40E_LINK_SPEED_2_5GB:
7307 		speed = "2.5 G";
7308 		break;
7309 	case I40E_LINK_SPEED_1GB:
7310 		speed = "1000 M";
7311 		break;
7312 	case I40E_LINK_SPEED_100MB:
7313 		speed = "100 M";
7314 		break;
7315 	default:
7316 		break;
7317 	}
7318 
7319 	switch (pf->hw.fc.current_mode) {
7320 	case I40E_FC_FULL:
7321 		fc = "RX/TX";
7322 		break;
7323 	case I40E_FC_TX_PAUSE:
7324 		fc = "TX";
7325 		break;
7326 	case I40E_FC_RX_PAUSE:
7327 		fc = "RX";
7328 		break;
7329 	default:
7330 		fc = "None";
7331 		break;
7332 	}
7333 
7334 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7335 		req_fec = "None";
7336 		fec = "None";
7337 		an = "False";
7338 
7339 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7340 			an = "True";
7341 
7342 		if (pf->hw.phy.link_info.fec_info &
7343 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7344 			fec = "CL74 FC-FEC/BASE-R";
7345 		else if (pf->hw.phy.link_info.fec_info &
7346 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7347 			fec = "CL108 RS-FEC";
7348 
7349 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7350 		 * both RS and FC are requested
7351 		 */
7352 		if (vsi->back->hw.phy.link_info.req_fec_info &
7353 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7354 			if (vsi->back->hw.phy.link_info.req_fec_info &
7355 			    I40E_AQ_REQUEST_FEC_RS)
7356 				req_fec = "CL108 RS-FEC";
7357 			else
7358 				req_fec = "CL74 FC-FEC/BASE-R";
7359 		}
7360 		netdev_info(vsi->netdev,
7361 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7362 			    speed, req_fec, fec, an, fc);
7363 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7364 		req_fec = "None";
7365 		fec = "None";
7366 		an = "False";
7367 
7368 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7369 			an = "True";
7370 
7371 		if (pf->hw.phy.link_info.fec_info &
7372 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7373 			fec = "CL74 FC-FEC/BASE-R";
7374 
7375 		if (pf->hw.phy.link_info.req_fec_info &
7376 		    I40E_AQ_REQUEST_FEC_KR)
7377 			req_fec = "CL74 FC-FEC/BASE-R";
7378 
7379 		netdev_info(vsi->netdev,
7380 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7381 			    speed, req_fec, fec, an, fc);
7382 	} else {
7383 		i40e_print_link_message_eee(vsi, speed, fc);
7384 	}
7385 
7386 }
7387 
7388 /**
7389  * i40e_up_complete - Finish the last steps of bringing up a connection
7390  * @vsi: the VSI being configured
7391  **/
7392 static int i40e_up_complete(struct i40e_vsi *vsi)
7393 {
7394 	struct i40e_pf *pf = vsi->back;
7395 	int err;
7396 
7397 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7398 		i40e_vsi_configure_msix(vsi);
7399 	else
7400 		i40e_configure_msi_and_legacy(vsi);
7401 
7402 	/* start rings */
7403 	err = i40e_vsi_start_rings(vsi);
7404 	if (err)
7405 		return err;
7406 
7407 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7408 	i40e_napi_enable_all(vsi);
7409 	i40e_vsi_enable_irq(vsi);
7410 
7411 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7412 	    (vsi->netdev)) {
7413 		i40e_print_link_message(vsi, true);
7414 		netif_tx_start_all_queues(vsi->netdev);
7415 		netif_carrier_on(vsi->netdev);
7416 	}
7417 
7418 	/* replay FDIR SB filters */
7419 	if (vsi->type == I40E_VSI_FDIR) {
7420 		/* reset fd counters */
7421 		pf->fd_add_err = 0;
7422 		pf->fd_atr_cnt = 0;
7423 		i40e_fdir_filter_restore(vsi);
7424 	}
7425 
7426 	/* On the next run of the service_task, notify any clients of the new
7427 	 * opened netdev
7428 	 */
7429 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7430 	i40e_service_event_schedule(pf);
7431 
7432 	return 0;
7433 }
7434 
7435 /**
7436  * i40e_vsi_reinit_locked - Reset the VSI
7437  * @vsi: the VSI being configured
7438  *
7439  * Rebuild the ring structs after some configuration
7440  * has changed, e.g. MTU size.
7441  **/
7442 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7443 {
7444 	struct i40e_pf *pf = vsi->back;
7445 
7446 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7447 		usleep_range(1000, 2000);
7448 	i40e_down(vsi);
7449 
7450 	i40e_up(vsi);
7451 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7452 }
7453 
7454 /**
7455  * i40e_force_link_state - Force the link status
7456  * @pf: board private structure
7457  * @is_up: whether the link state should be forced up or down
7458  **/
7459 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7460 {
7461 	struct i40e_aq_get_phy_abilities_resp abilities;
7462 	struct i40e_aq_set_phy_config config = {0};
7463 	bool non_zero_phy_type = is_up;
7464 	struct i40e_hw *hw = &pf->hw;
7465 	u64 mask;
7466 	u8 speed;
7467 	int err;
7468 
7469 	/* Card might've been put in an unstable state by other drivers
7470 	 * and applications, which causes incorrect speed values being
7471 	 * set on startup. In order to clear speed registers, we call
7472 	 * get_phy_capabilities twice, once to get initial state of
7473 	 * available speeds, and once to get current PHY config.
7474 	 */
7475 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7476 					   NULL);
7477 	if (err) {
7478 		dev_err(&pf->pdev->dev,
7479 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7480 			ERR_PTR(err),
7481 			i40e_aq_str(hw, hw->aq.asq_last_status));
7482 		return err;
7483 	}
7484 	speed = abilities.link_speed;
7485 
7486 	/* Get the current phy config */
7487 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7488 					   NULL);
7489 	if (err) {
7490 		dev_err(&pf->pdev->dev,
7491 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7492 			ERR_PTR(err),
7493 			i40e_aq_str(hw, hw->aq.asq_last_status));
7494 		return err;
7495 	}
7496 
7497 	/* If link needs to go up, but was not forced to go down,
7498 	 * and its speed values are OK, no need for a flap
7499 	 * if non_zero_phy_type was set, still need to force up
7500 	 */
7501 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7502 		non_zero_phy_type = true;
7503 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7504 		return 0;
7505 
7506 	/* To force link we need to set bits for all supported PHY types,
7507 	 * but there are now more than 32, so we need to split the bitmap
7508 	 * across two fields.
7509 	 */
7510 	mask = I40E_PHY_TYPES_BITMASK;
7511 	config.phy_type =
7512 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7513 	config.phy_type_ext =
7514 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7515 	/* Copy the old settings, except of phy_type */
7516 	config.abilities = abilities.abilities;
7517 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7518 		if (is_up)
7519 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7520 		else
7521 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7522 	}
7523 	if (abilities.link_speed != 0)
7524 		config.link_speed = abilities.link_speed;
7525 	else
7526 		config.link_speed = speed;
7527 	config.eee_capability = abilities.eee_capability;
7528 	config.eeer = abilities.eeer_val;
7529 	config.low_power_ctrl = abilities.d3_lpan;
7530 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7531 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7532 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7533 
7534 	if (err) {
7535 		dev_err(&pf->pdev->dev,
7536 			"set phy config ret =  %pe last_status =  %s\n",
7537 			ERR_PTR(err),
7538 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7539 		return err;
7540 	}
7541 
7542 	/* Update the link info */
7543 	err = i40e_update_link_info(hw);
7544 	if (err) {
7545 		/* Wait a little bit (on 40G cards it sometimes takes a really
7546 		 * long time for link to come back from the atomic reset)
7547 		 * and try once more
7548 		 */
7549 		msleep(1000);
7550 		i40e_update_link_info(hw);
7551 	}
7552 
7553 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7554 
7555 	return 0;
7556 }
7557 
7558 /**
7559  * i40e_up - Bring the connection back up after being down
7560  * @vsi: the VSI being configured
7561  **/
7562 int i40e_up(struct i40e_vsi *vsi)
7563 {
7564 	int err;
7565 
7566 	if (vsi->type == I40E_VSI_MAIN &&
7567 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7568 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7569 		i40e_force_link_state(vsi->back, true);
7570 
7571 	err = i40e_vsi_configure(vsi);
7572 	if (!err)
7573 		err = i40e_up_complete(vsi);
7574 
7575 	return err;
7576 }
7577 
7578 /**
7579  * i40e_down - Shutdown the connection processing
7580  * @vsi: the VSI being stopped
7581  **/
7582 void i40e_down(struct i40e_vsi *vsi)
7583 {
7584 	int i;
7585 
7586 	/* It is assumed that the caller of this function
7587 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7588 	 */
7589 	if (vsi->netdev) {
7590 		netif_carrier_off(vsi->netdev);
7591 		netif_tx_disable(vsi->netdev);
7592 	}
7593 	i40e_vsi_disable_irq(vsi);
7594 	i40e_vsi_stop_rings(vsi);
7595 	if (vsi->type == I40E_VSI_MAIN &&
7596 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7597 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7598 		i40e_force_link_state(vsi->back, false);
7599 	i40e_napi_disable_all(vsi);
7600 
7601 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7602 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7603 		if (i40e_enabled_xdp_vsi(vsi)) {
7604 			/* Make sure that in-progress ndo_xdp_xmit and
7605 			 * ndo_xsk_wakeup calls are completed.
7606 			 */
7607 			synchronize_rcu();
7608 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7609 		}
7610 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7611 	}
7612 
7613 }
7614 
7615 /**
7616  * i40e_validate_mqprio_qopt- validate queue mapping info
7617  * @vsi: the VSI being configured
7618  * @mqprio_qopt: queue parametrs
7619  **/
7620 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7621 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7622 {
7623 	u64 sum_max_rate = 0;
7624 	u64 max_rate = 0;
7625 	int i;
7626 
7627 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7628 	    mqprio_qopt->qopt.num_tc < 1 ||
7629 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7630 		return -EINVAL;
7631 	for (i = 0; ; i++) {
7632 		if (!mqprio_qopt->qopt.count[i])
7633 			return -EINVAL;
7634 		if (mqprio_qopt->min_rate[i]) {
7635 			dev_err(&vsi->back->pdev->dev,
7636 				"Invalid min tx rate (greater than 0) specified\n");
7637 			return -EINVAL;
7638 		}
7639 		max_rate = mqprio_qopt->max_rate[i];
7640 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7641 		sum_max_rate += max_rate;
7642 
7643 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7644 			break;
7645 		if (mqprio_qopt->qopt.offset[i + 1] !=
7646 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7647 			return -EINVAL;
7648 	}
7649 	if (vsi->num_queue_pairs <
7650 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7651 		dev_err(&vsi->back->pdev->dev,
7652 			"Failed to create traffic channel, insufficient number of queues.\n");
7653 		return -EINVAL;
7654 	}
7655 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7656 		dev_err(&vsi->back->pdev->dev,
7657 			"Invalid max tx rate specified\n");
7658 		return -EINVAL;
7659 	}
7660 	return 0;
7661 }
7662 
7663 /**
7664  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7665  * @vsi: the VSI being configured
7666  **/
7667 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7668 {
7669 	u16 qcount;
7670 	int i;
7671 
7672 	/* Only TC0 is enabled */
7673 	vsi->tc_config.numtc = 1;
7674 	vsi->tc_config.enabled_tc = 1;
7675 	qcount = min_t(int, vsi->alloc_queue_pairs,
7676 		       i40e_pf_get_max_q_per_tc(vsi->back));
7677 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7678 		/* For the TC that is not enabled set the offset to default
7679 		 * queue and allocate one queue for the given TC.
7680 		 */
7681 		vsi->tc_config.tc_info[i].qoffset = 0;
7682 		if (i == 0)
7683 			vsi->tc_config.tc_info[i].qcount = qcount;
7684 		else
7685 			vsi->tc_config.tc_info[i].qcount = 1;
7686 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7687 	}
7688 }
7689 
7690 /**
7691  * i40e_del_macvlan_filter
7692  * @hw: pointer to the HW structure
7693  * @seid: seid of the channel VSI
7694  * @macaddr: the mac address to apply as a filter
7695  * @aq_err: store the admin Q error
7696  *
7697  * This function deletes a mac filter on the channel VSI which serves as the
7698  * macvlan. Returns 0 on success.
7699  **/
7700 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7701 				   const u8 *macaddr, int *aq_err)
7702 {
7703 	struct i40e_aqc_remove_macvlan_element_data element;
7704 	int status;
7705 
7706 	memset(&element, 0, sizeof(element));
7707 	ether_addr_copy(element.mac_addr, macaddr);
7708 	element.vlan_tag = 0;
7709 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7710 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7711 	*aq_err = hw->aq.asq_last_status;
7712 
7713 	return status;
7714 }
7715 
7716 /**
7717  * i40e_add_macvlan_filter
7718  * @hw: pointer to the HW structure
7719  * @seid: seid of the channel VSI
7720  * @macaddr: the mac address to apply as a filter
7721  * @aq_err: store the admin Q error
7722  *
7723  * This function adds a mac filter on the channel VSI which serves as the
7724  * macvlan. Returns 0 on success.
7725  **/
7726 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7727 				   const u8 *macaddr, int *aq_err)
7728 {
7729 	struct i40e_aqc_add_macvlan_element_data element;
7730 	u16 cmd_flags = 0;
7731 	int status;
7732 
7733 	ether_addr_copy(element.mac_addr, macaddr);
7734 	element.vlan_tag = 0;
7735 	element.queue_number = 0;
7736 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7737 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7738 	element.flags = cpu_to_le16(cmd_flags);
7739 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7740 	*aq_err = hw->aq.asq_last_status;
7741 
7742 	return status;
7743 }
7744 
7745 /**
7746  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7747  * @vsi: the VSI we want to access
7748  * @ch: the channel we want to access
7749  */
7750 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7751 {
7752 	struct i40e_ring *tx_ring, *rx_ring;
7753 	u16 pf_q;
7754 	int i;
7755 
7756 	for (i = 0; i < ch->num_queue_pairs; i++) {
7757 		pf_q = ch->base_queue + i;
7758 		tx_ring = vsi->tx_rings[pf_q];
7759 		tx_ring->ch = NULL;
7760 		rx_ring = vsi->rx_rings[pf_q];
7761 		rx_ring->ch = NULL;
7762 	}
7763 }
7764 
7765 /**
7766  * i40e_free_macvlan_channels
7767  * @vsi: the VSI we want to access
7768  *
7769  * This function frees the Qs of the channel VSI from
7770  * the stack and also deletes the channel VSIs which
7771  * serve as macvlans.
7772  */
7773 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7774 {
7775 	struct i40e_channel *ch, *ch_tmp;
7776 	int ret;
7777 
7778 	if (list_empty(&vsi->macvlan_list))
7779 		return;
7780 
7781 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7782 		struct i40e_vsi *parent_vsi;
7783 
7784 		if (i40e_is_channel_macvlan(ch)) {
7785 			i40e_reset_ch_rings(vsi, ch);
7786 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7787 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7788 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7789 			kfree(ch->fwd);
7790 			ch->fwd = NULL;
7791 		}
7792 
7793 		list_del(&ch->list);
7794 		parent_vsi = ch->parent_vsi;
7795 		if (!parent_vsi || !ch->initialized) {
7796 			kfree(ch);
7797 			continue;
7798 		}
7799 
7800 		/* remove the VSI */
7801 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7802 					     NULL);
7803 		if (ret)
7804 			dev_err(&vsi->back->pdev->dev,
7805 				"unable to remove channel (%d) for parent VSI(%d)\n",
7806 				ch->seid, parent_vsi->seid);
7807 		kfree(ch);
7808 	}
7809 	vsi->macvlan_cnt = 0;
7810 }
7811 
7812 /**
7813  * i40e_fwd_ring_up - bring the macvlan device up
7814  * @vsi: the VSI we want to access
7815  * @vdev: macvlan netdevice
7816  * @fwd: the private fwd structure
7817  */
7818 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7819 			    struct i40e_fwd_adapter *fwd)
7820 {
7821 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7822 	int ret = 0, num_tc = 1,  i, aq_err;
7823 	struct i40e_pf *pf = vsi->back;
7824 	struct i40e_hw *hw = &pf->hw;
7825 
7826 	/* Go through the list and find an available channel */
7827 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7828 		if (!i40e_is_channel_macvlan(iter)) {
7829 			iter->fwd = fwd;
7830 			/* record configuration for macvlan interface in vdev */
7831 			for (i = 0; i < num_tc; i++)
7832 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7833 							     i,
7834 							     iter->num_queue_pairs,
7835 							     iter->base_queue);
7836 			for (i = 0; i < iter->num_queue_pairs; i++) {
7837 				struct i40e_ring *tx_ring, *rx_ring;
7838 				u16 pf_q;
7839 
7840 				pf_q = iter->base_queue + i;
7841 
7842 				/* Get to TX ring ptr */
7843 				tx_ring = vsi->tx_rings[pf_q];
7844 				tx_ring->ch = iter;
7845 
7846 				/* Get the RX ring ptr */
7847 				rx_ring = vsi->rx_rings[pf_q];
7848 				rx_ring->ch = iter;
7849 			}
7850 			ch = iter;
7851 			break;
7852 		}
7853 	}
7854 
7855 	if (!ch)
7856 		return -EINVAL;
7857 
7858 	/* Guarantee all rings are updated before we update the
7859 	 * MAC address filter.
7860 	 */
7861 	wmb();
7862 
7863 	/* Add a mac filter */
7864 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7865 	if (ret) {
7866 		/* if we cannot add the MAC rule then disable the offload */
7867 		macvlan_release_l2fw_offload(vdev);
7868 		for (i = 0; i < ch->num_queue_pairs; i++) {
7869 			struct i40e_ring *rx_ring;
7870 			u16 pf_q;
7871 
7872 			pf_q = ch->base_queue + i;
7873 			rx_ring = vsi->rx_rings[pf_q];
7874 			rx_ring->netdev = NULL;
7875 		}
7876 		dev_info(&pf->pdev->dev,
7877 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7878 			  ERR_PTR(ret),
7879 			  i40e_aq_str(hw, aq_err));
7880 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7881 	}
7882 
7883 	return ret;
7884 }
7885 
7886 /**
7887  * i40e_setup_macvlans - create the channels which will be macvlans
7888  * @vsi: the VSI we want to access
7889  * @macvlan_cnt: no. of macvlans to be setup
7890  * @qcnt: no. of Qs per macvlan
7891  * @vdev: macvlan netdevice
7892  */
7893 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7894 			       struct net_device *vdev)
7895 {
7896 	struct i40e_pf *pf = vsi->back;
7897 	struct i40e_hw *hw = &pf->hw;
7898 	struct i40e_vsi_context ctxt;
7899 	u16 sections, qmap, num_qps;
7900 	struct i40e_channel *ch;
7901 	int i, pow, ret = 0;
7902 	u8 offset = 0;
7903 
7904 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7905 		return -EINVAL;
7906 
7907 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7908 
7909 	/* find the next higher power-of-2 of num queue pairs */
7910 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7911 
7912 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7913 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7914 
7915 	/* Setup context bits for the main VSI */
7916 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7917 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7918 	memset(&ctxt, 0, sizeof(ctxt));
7919 	ctxt.seid = vsi->seid;
7920 	ctxt.pf_num = vsi->back->hw.pf_id;
7921 	ctxt.vf_num = 0;
7922 	ctxt.uplink_seid = vsi->uplink_seid;
7923 	ctxt.info = vsi->info;
7924 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7925 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7926 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7927 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7928 
7929 	/* Reconfigure RSS for main VSI with new max queue count */
7930 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7931 	ret = i40e_vsi_config_rss(vsi);
7932 	if (ret) {
7933 		dev_info(&pf->pdev->dev,
7934 			 "Failed to reconfig RSS for num_queues (%u)\n",
7935 			 vsi->rss_size);
7936 		return ret;
7937 	}
7938 	vsi->reconfig_rss = true;
7939 	dev_dbg(&vsi->back->pdev->dev,
7940 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7941 	vsi->next_base_queue = num_qps;
7942 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7943 
7944 	/* Update the VSI after updating the VSI queue-mapping
7945 	 * information
7946 	 */
7947 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7948 	if (ret) {
7949 		dev_info(&pf->pdev->dev,
7950 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7951 			 ERR_PTR(ret),
7952 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7953 		return ret;
7954 	}
7955 	/* update the local VSI info with updated queue map */
7956 	i40e_vsi_update_queue_map(vsi, &ctxt);
7957 	vsi->info.valid_sections = 0;
7958 
7959 	/* Create channels for macvlans */
7960 	INIT_LIST_HEAD(&vsi->macvlan_list);
7961 	for (i = 0; i < macvlan_cnt; i++) {
7962 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7963 		if (!ch) {
7964 			ret = -ENOMEM;
7965 			goto err_free;
7966 		}
7967 		INIT_LIST_HEAD(&ch->list);
7968 		ch->num_queue_pairs = qcnt;
7969 		if (!i40e_setup_channel(pf, vsi, ch)) {
7970 			ret = -EINVAL;
7971 			kfree(ch);
7972 			goto err_free;
7973 		}
7974 		ch->parent_vsi = vsi;
7975 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7976 		vsi->macvlan_cnt++;
7977 		list_add_tail(&ch->list, &vsi->macvlan_list);
7978 	}
7979 
7980 	return ret;
7981 
7982 err_free:
7983 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7984 	i40e_free_macvlan_channels(vsi);
7985 
7986 	return ret;
7987 }
7988 
7989 /**
7990  * i40e_fwd_add - configure macvlans
7991  * @netdev: net device to configure
7992  * @vdev: macvlan netdevice
7993  **/
7994 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7995 {
7996 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7997 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7998 	struct i40e_vsi *vsi = np->vsi;
7999 	struct i40e_pf *pf = vsi->back;
8000 	struct i40e_fwd_adapter *fwd;
8001 	int avail_macvlan, ret;
8002 
8003 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8004 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
8005 		return ERR_PTR(-EINVAL);
8006 	}
8007 	if (i40e_is_tc_mqprio_enabled(pf)) {
8008 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8009 		return ERR_PTR(-EINVAL);
8010 	}
8011 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8012 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8013 		return ERR_PTR(-EINVAL);
8014 	}
8015 
8016 	/* The macvlan device has to be a single Q device so that the
8017 	 * tc_to_txq field can be reused to pick the tx queue.
8018 	 */
8019 	if (netif_is_multiqueue(vdev))
8020 		return ERR_PTR(-ERANGE);
8021 
8022 	if (!vsi->macvlan_cnt) {
8023 		/* reserve bit 0 for the pf device */
8024 		set_bit(0, vsi->fwd_bitmask);
8025 
8026 		/* Try to reserve as many queues as possible for macvlans. First
8027 		 * reserve 3/4th of max vectors, then half, then quarter and
8028 		 * calculate Qs per macvlan as you go
8029 		 */
8030 		vectors = pf->num_lan_msix;
8031 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8032 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8033 			q_per_macvlan = 4;
8034 			macvlan_cnt = (vectors - 32) / 4;
8035 		} else if (vectors <= 64 && vectors > 32) {
8036 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8037 			q_per_macvlan = 2;
8038 			macvlan_cnt = (vectors - 16) / 2;
8039 		} else if (vectors <= 32 && vectors > 16) {
8040 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8041 			q_per_macvlan = 1;
8042 			macvlan_cnt = vectors - 16;
8043 		} else if (vectors <= 16 && vectors > 8) {
8044 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8045 			q_per_macvlan = 1;
8046 			macvlan_cnt = vectors - 8;
8047 		} else {
8048 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8049 			q_per_macvlan = 1;
8050 			macvlan_cnt = vectors - 1;
8051 		}
8052 
8053 		if (macvlan_cnt == 0)
8054 			return ERR_PTR(-EBUSY);
8055 
8056 		/* Quiesce VSI queues */
8057 		i40e_quiesce_vsi(vsi);
8058 
8059 		/* sets up the macvlans but does not "enable" them */
8060 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8061 					  vdev);
8062 		if (ret)
8063 			return ERR_PTR(ret);
8064 
8065 		/* Unquiesce VSI */
8066 		i40e_unquiesce_vsi(vsi);
8067 	}
8068 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8069 					    vsi->macvlan_cnt);
8070 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8071 		return ERR_PTR(-EBUSY);
8072 
8073 	/* create the fwd struct */
8074 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8075 	if (!fwd)
8076 		return ERR_PTR(-ENOMEM);
8077 
8078 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8079 	fwd->bit_no = avail_macvlan;
8080 	netdev_set_sb_channel(vdev, avail_macvlan);
8081 	fwd->netdev = vdev;
8082 
8083 	if (!netif_running(netdev))
8084 		return fwd;
8085 
8086 	/* Set fwd ring up */
8087 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8088 	if (ret) {
8089 		/* unbind the queues and drop the subordinate channel config */
8090 		netdev_unbind_sb_channel(netdev, vdev);
8091 		netdev_set_sb_channel(vdev, 0);
8092 
8093 		kfree(fwd);
8094 		return ERR_PTR(-EINVAL);
8095 	}
8096 
8097 	return fwd;
8098 }
8099 
8100 /**
8101  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8102  * @vsi: the VSI we want to access
8103  */
8104 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8105 {
8106 	struct i40e_channel *ch, *ch_tmp;
8107 	struct i40e_pf *pf = vsi->back;
8108 	struct i40e_hw *hw = &pf->hw;
8109 	int aq_err, ret = 0;
8110 
8111 	if (list_empty(&vsi->macvlan_list))
8112 		return;
8113 
8114 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8115 		if (i40e_is_channel_macvlan(ch)) {
8116 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8117 						      i40e_channel_mac(ch),
8118 						      &aq_err);
8119 			if (!ret) {
8120 				/* Reset queue contexts */
8121 				i40e_reset_ch_rings(vsi, ch);
8122 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8123 				netdev_unbind_sb_channel(vsi->netdev,
8124 							 ch->fwd->netdev);
8125 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8126 				kfree(ch->fwd);
8127 				ch->fwd = NULL;
8128 			}
8129 		}
8130 	}
8131 }
8132 
8133 /**
8134  * i40e_fwd_del - delete macvlan interfaces
8135  * @netdev: net device to configure
8136  * @vdev: macvlan netdevice
8137  */
8138 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8139 {
8140 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8141 	struct i40e_fwd_adapter *fwd = vdev;
8142 	struct i40e_channel *ch, *ch_tmp;
8143 	struct i40e_vsi *vsi = np->vsi;
8144 	struct i40e_pf *pf = vsi->back;
8145 	struct i40e_hw *hw = &pf->hw;
8146 	int aq_err, ret = 0;
8147 
8148 	/* Find the channel associated with the macvlan and del mac filter */
8149 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8150 		if (i40e_is_channel_macvlan(ch) &&
8151 		    ether_addr_equal(i40e_channel_mac(ch),
8152 				     fwd->netdev->dev_addr)) {
8153 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8154 						      i40e_channel_mac(ch),
8155 						      &aq_err);
8156 			if (!ret) {
8157 				/* Reset queue contexts */
8158 				i40e_reset_ch_rings(vsi, ch);
8159 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8160 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8161 				netdev_set_sb_channel(fwd->netdev, 0);
8162 				kfree(ch->fwd);
8163 				ch->fwd = NULL;
8164 			} else {
8165 				dev_info(&pf->pdev->dev,
8166 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8167 					  ERR_PTR(ret),
8168 					  i40e_aq_str(hw, aq_err));
8169 			}
8170 			break;
8171 		}
8172 	}
8173 }
8174 
8175 /**
8176  * i40e_setup_tc - configure multiple traffic classes
8177  * @netdev: net device to configure
8178  * @type_data: tc offload data
8179  **/
8180 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8181 {
8182 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8183 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8184 	struct i40e_vsi *vsi = np->vsi;
8185 	struct i40e_pf *pf = vsi->back;
8186 	u8 enabled_tc = 0, num_tc, hw;
8187 	bool need_reset = false;
8188 	int old_queue_pairs;
8189 	int ret = -EINVAL;
8190 	u16 mode;
8191 	int i;
8192 
8193 	old_queue_pairs = vsi->num_queue_pairs;
8194 	num_tc = mqprio_qopt->qopt.num_tc;
8195 	hw = mqprio_qopt->qopt.hw;
8196 	mode = mqprio_qopt->mode;
8197 	if (!hw) {
8198 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8199 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8200 		goto config_tc;
8201 	}
8202 
8203 	/* Check if MFP enabled */
8204 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8205 		netdev_info(netdev,
8206 			    "Configuring TC not supported in MFP mode\n");
8207 		return ret;
8208 	}
8209 	switch (mode) {
8210 	case TC_MQPRIO_MODE_DCB:
8211 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8212 
8213 		/* Check if DCB enabled to continue */
8214 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8215 			netdev_info(netdev,
8216 				    "DCB is not enabled for adapter\n");
8217 			return ret;
8218 		}
8219 
8220 		/* Check whether tc count is within enabled limit */
8221 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8222 			netdev_info(netdev,
8223 				    "TC count greater than enabled on link for adapter\n");
8224 			return ret;
8225 		}
8226 		break;
8227 	case TC_MQPRIO_MODE_CHANNEL:
8228 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8229 			netdev_info(netdev,
8230 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8231 			return ret;
8232 		}
8233 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8234 			return ret;
8235 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8236 		if (ret)
8237 			return ret;
8238 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8239 		       sizeof(*mqprio_qopt));
8240 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8241 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8242 		break;
8243 	default:
8244 		return -EINVAL;
8245 	}
8246 
8247 config_tc:
8248 	/* Generate TC map for number of tc requested */
8249 	for (i = 0; i < num_tc; i++)
8250 		enabled_tc |= BIT(i);
8251 
8252 	/* Requesting same TC configuration as already enabled */
8253 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8254 	    mode != TC_MQPRIO_MODE_CHANNEL)
8255 		return 0;
8256 
8257 	/* Quiesce VSI queues */
8258 	i40e_quiesce_vsi(vsi);
8259 
8260 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8261 		i40e_remove_queue_channels(vsi);
8262 
8263 	/* Configure VSI for enabled TCs */
8264 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8265 	if (ret) {
8266 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8267 			    vsi->seid);
8268 		need_reset = true;
8269 		goto exit;
8270 	} else if (enabled_tc &&
8271 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8272 		netdev_info(netdev,
8273 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8274 			    vsi->tc_config.tc_info[0].qcount);
8275 		ret = -EINVAL;
8276 		need_reset = true;
8277 		goto exit;
8278 	}
8279 
8280 	dev_info(&vsi->back->pdev->dev,
8281 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8282 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8283 
8284 	if (i40e_is_tc_mqprio_enabled(pf)) {
8285 		if (vsi->mqprio_qopt.max_rate[0]) {
8286 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8287 						  vsi->mqprio_qopt.max_rate[0]);
8288 
8289 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8290 			if (!ret) {
8291 				u64 credits = max_tx_rate;
8292 
8293 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8294 				dev_dbg(&vsi->back->pdev->dev,
8295 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8296 					max_tx_rate,
8297 					credits,
8298 					vsi->seid);
8299 			} else {
8300 				need_reset = true;
8301 				goto exit;
8302 			}
8303 		}
8304 		ret = i40e_configure_queue_channels(vsi);
8305 		if (ret) {
8306 			vsi->num_queue_pairs = old_queue_pairs;
8307 			netdev_info(netdev,
8308 				    "Failed configuring queue channels\n");
8309 			need_reset = true;
8310 			goto exit;
8311 		}
8312 	}
8313 
8314 exit:
8315 	/* Reset the configuration data to defaults, only TC0 is enabled */
8316 	if (need_reset) {
8317 		i40e_vsi_set_default_tc_config(vsi);
8318 		need_reset = false;
8319 	}
8320 
8321 	/* Unquiesce VSI */
8322 	i40e_unquiesce_vsi(vsi);
8323 	return ret;
8324 }
8325 
8326 /**
8327  * i40e_set_cld_element - sets cloud filter element data
8328  * @filter: cloud filter rule
8329  * @cld: ptr to cloud filter element data
8330  *
8331  * This is helper function to copy data into cloud filter element
8332  **/
8333 static inline void
8334 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8335 		     struct i40e_aqc_cloud_filters_element_data *cld)
8336 {
8337 	u32 ipa;
8338 	int i;
8339 
8340 	memset(cld, 0, sizeof(*cld));
8341 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8342 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8343 
8344 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8345 		return;
8346 
8347 	if (filter->n_proto == ETH_P_IPV6) {
8348 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8349 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8350 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8351 
8352 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8353 		}
8354 	} else {
8355 		ipa = be32_to_cpu(filter->dst_ipv4);
8356 
8357 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8358 	}
8359 
8360 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8361 
8362 	/* tenant_id is not supported by FW now, once the support is enabled
8363 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8364 	 */
8365 	if (filter->tenant_id)
8366 		return;
8367 }
8368 
8369 /**
8370  * i40e_add_del_cloud_filter - Add/del cloud filter
8371  * @vsi: pointer to VSI
8372  * @filter: cloud filter rule
8373  * @add: if true, add, if false, delete
8374  *
8375  * Add or delete a cloud filter for a specific flow spec.
8376  * Returns 0 if the filter were successfully added.
8377  **/
8378 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8379 			      struct i40e_cloud_filter *filter, bool add)
8380 {
8381 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8382 	struct i40e_pf *pf = vsi->back;
8383 	int ret;
8384 	static const u16 flag_table[128] = {
8385 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8386 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8387 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8388 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8389 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8390 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8391 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8392 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8393 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8394 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8395 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8396 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8397 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8398 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8399 	};
8400 
8401 	if (filter->flags >= ARRAY_SIZE(flag_table))
8402 		return -EIO;
8403 
8404 	memset(&cld_filter, 0, sizeof(cld_filter));
8405 
8406 	/* copy element needed to add cloud filter from filter */
8407 	i40e_set_cld_element(filter, &cld_filter);
8408 
8409 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8410 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8411 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8412 
8413 	if (filter->n_proto == ETH_P_IPV6)
8414 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8415 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8416 	else
8417 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8418 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8419 
8420 	if (add)
8421 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8422 						&cld_filter, 1);
8423 	else
8424 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8425 						&cld_filter, 1);
8426 	if (ret)
8427 		dev_dbg(&pf->pdev->dev,
8428 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8429 			add ? "add" : "delete", filter->dst_port, ret,
8430 			pf->hw.aq.asq_last_status);
8431 	else
8432 		dev_info(&pf->pdev->dev,
8433 			 "%s cloud filter for VSI: %d\n",
8434 			 add ? "Added" : "Deleted", filter->seid);
8435 	return ret;
8436 }
8437 
8438 /**
8439  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8440  * @vsi: pointer to VSI
8441  * @filter: cloud filter rule
8442  * @add: if true, add, if false, delete
8443  *
8444  * Add or delete a cloud filter for a specific flow spec using big buffer.
8445  * Returns 0 if the filter were successfully added.
8446  **/
8447 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8448 				      struct i40e_cloud_filter *filter,
8449 				      bool add)
8450 {
8451 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8452 	struct i40e_pf *pf = vsi->back;
8453 	int ret;
8454 
8455 	/* Both (src/dst) valid mac_addr are not supported */
8456 	if ((is_valid_ether_addr(filter->dst_mac) &&
8457 	     is_valid_ether_addr(filter->src_mac)) ||
8458 	    (is_multicast_ether_addr(filter->dst_mac) &&
8459 	     is_multicast_ether_addr(filter->src_mac)))
8460 		return -EOPNOTSUPP;
8461 
8462 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8463 	 * ports are not supported via big buffer now.
8464 	 */
8465 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8466 		return -EOPNOTSUPP;
8467 
8468 	/* adding filter using src_port/src_ip is not supported at this stage */
8469 	if (filter->src_port ||
8470 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8471 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8472 		return -EOPNOTSUPP;
8473 
8474 	memset(&cld_filter, 0, sizeof(cld_filter));
8475 
8476 	/* copy element needed to add cloud filter from filter */
8477 	i40e_set_cld_element(filter, &cld_filter.element);
8478 
8479 	if (is_valid_ether_addr(filter->dst_mac) ||
8480 	    is_valid_ether_addr(filter->src_mac) ||
8481 	    is_multicast_ether_addr(filter->dst_mac) ||
8482 	    is_multicast_ether_addr(filter->src_mac)) {
8483 		/* MAC + IP : unsupported mode */
8484 		if (filter->dst_ipv4)
8485 			return -EOPNOTSUPP;
8486 
8487 		/* since we validated that L4 port must be valid before
8488 		 * we get here, start with respective "flags" value
8489 		 * and update if vlan is present or not
8490 		 */
8491 		cld_filter.element.flags =
8492 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8493 
8494 		if (filter->vlan_id) {
8495 			cld_filter.element.flags =
8496 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8497 		}
8498 
8499 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8500 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8501 		cld_filter.element.flags =
8502 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8503 		if (filter->n_proto == ETH_P_IPV6)
8504 			cld_filter.element.flags |=
8505 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8506 		else
8507 			cld_filter.element.flags |=
8508 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8509 	} else {
8510 		dev_err(&pf->pdev->dev,
8511 			"either mac or ip has to be valid for cloud filter\n");
8512 		return -EINVAL;
8513 	}
8514 
8515 	/* Now copy L4 port in Byte 6..7 in general fields */
8516 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8517 						be16_to_cpu(filter->dst_port);
8518 
8519 	if (add) {
8520 		/* Validate current device switch mode, change if necessary */
8521 		ret = i40e_validate_and_set_switch_mode(vsi);
8522 		if (ret) {
8523 			dev_err(&pf->pdev->dev,
8524 				"failed to set switch mode, ret %d\n",
8525 				ret);
8526 			return ret;
8527 		}
8528 
8529 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8530 						   &cld_filter, 1);
8531 	} else {
8532 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8533 						   &cld_filter, 1);
8534 	}
8535 
8536 	if (ret)
8537 		dev_dbg(&pf->pdev->dev,
8538 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8539 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8540 	else
8541 		dev_info(&pf->pdev->dev,
8542 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8543 			 add ? "add" : "delete", filter->seid,
8544 			 ntohs(filter->dst_port));
8545 	return ret;
8546 }
8547 
8548 /**
8549  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8550  * @vsi: Pointer to VSI
8551  * @f: Pointer to struct flow_cls_offload
8552  * @filter: Pointer to cloud filter structure
8553  *
8554  **/
8555 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8556 				 struct flow_cls_offload *f,
8557 				 struct i40e_cloud_filter *filter)
8558 {
8559 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8560 	struct flow_dissector *dissector = rule->match.dissector;
8561 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8562 	struct i40e_pf *pf = vsi->back;
8563 	u8 field_flags = 0;
8564 
8565 	if (dissector->used_keys &
8566 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8567 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8568 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8569 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8570 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8571 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8572 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8573 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8574 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8575 			dissector->used_keys);
8576 		return -EOPNOTSUPP;
8577 	}
8578 
8579 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8580 		struct flow_match_enc_keyid match;
8581 
8582 		flow_rule_match_enc_keyid(rule, &match);
8583 		if (match.mask->keyid != 0)
8584 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8585 
8586 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8587 	}
8588 
8589 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8590 		struct flow_match_basic match;
8591 
8592 		flow_rule_match_basic(rule, &match);
8593 		n_proto_key = ntohs(match.key->n_proto);
8594 		n_proto_mask = ntohs(match.mask->n_proto);
8595 
8596 		if (n_proto_key == ETH_P_ALL) {
8597 			n_proto_key = 0;
8598 			n_proto_mask = 0;
8599 		}
8600 		filter->n_proto = n_proto_key & n_proto_mask;
8601 		filter->ip_proto = match.key->ip_proto;
8602 	}
8603 
8604 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8605 		struct flow_match_eth_addrs match;
8606 
8607 		flow_rule_match_eth_addrs(rule, &match);
8608 
8609 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8610 		if (!is_zero_ether_addr(match.mask->dst)) {
8611 			if (is_broadcast_ether_addr(match.mask->dst)) {
8612 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8613 			} else {
8614 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8615 					match.mask->dst);
8616 				return -EIO;
8617 			}
8618 		}
8619 
8620 		if (!is_zero_ether_addr(match.mask->src)) {
8621 			if (is_broadcast_ether_addr(match.mask->src)) {
8622 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8623 			} else {
8624 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8625 					match.mask->src);
8626 				return -EIO;
8627 			}
8628 		}
8629 		ether_addr_copy(filter->dst_mac, match.key->dst);
8630 		ether_addr_copy(filter->src_mac, match.key->src);
8631 	}
8632 
8633 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8634 		struct flow_match_vlan match;
8635 
8636 		flow_rule_match_vlan(rule, &match);
8637 		if (match.mask->vlan_id) {
8638 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8639 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8640 
8641 			} else {
8642 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8643 					match.mask->vlan_id);
8644 				return -EIO;
8645 			}
8646 		}
8647 
8648 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8649 	}
8650 
8651 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8652 		struct flow_match_control match;
8653 
8654 		flow_rule_match_control(rule, &match);
8655 		addr_type = match.key->addr_type;
8656 
8657 		if (flow_rule_has_control_flags(match.mask->flags,
8658 						f->common.extack))
8659 			return -EOPNOTSUPP;
8660 	}
8661 
8662 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8663 		struct flow_match_ipv4_addrs match;
8664 
8665 		flow_rule_match_ipv4_addrs(rule, &match);
8666 		if (match.mask->dst) {
8667 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8668 				field_flags |= I40E_CLOUD_FIELD_IIP;
8669 			} else {
8670 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8671 					&match.mask->dst);
8672 				return -EIO;
8673 			}
8674 		}
8675 
8676 		if (match.mask->src) {
8677 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8678 				field_flags |= I40E_CLOUD_FIELD_IIP;
8679 			} else {
8680 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8681 					&match.mask->src);
8682 				return -EIO;
8683 			}
8684 		}
8685 
8686 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8687 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8688 			return -EIO;
8689 		}
8690 		filter->dst_ipv4 = match.key->dst;
8691 		filter->src_ipv4 = match.key->src;
8692 	}
8693 
8694 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8695 		struct flow_match_ipv6_addrs match;
8696 
8697 		flow_rule_match_ipv6_addrs(rule, &match);
8698 
8699 		/* src and dest IPV6 address should not be LOOPBACK
8700 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8701 		 */
8702 		if (ipv6_addr_loopback(&match.key->dst) ||
8703 		    ipv6_addr_loopback(&match.key->src)) {
8704 			dev_err(&pf->pdev->dev,
8705 				"Bad ipv6, addr is LOOPBACK\n");
8706 			return -EIO;
8707 		}
8708 		if (!ipv6_addr_any(&match.mask->dst) ||
8709 		    !ipv6_addr_any(&match.mask->src))
8710 			field_flags |= I40E_CLOUD_FIELD_IIP;
8711 
8712 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8713 		       sizeof(filter->src_ipv6));
8714 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8715 		       sizeof(filter->dst_ipv6));
8716 	}
8717 
8718 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8719 		struct flow_match_ports match;
8720 
8721 		flow_rule_match_ports(rule, &match);
8722 		if (match.mask->src) {
8723 			if (match.mask->src == cpu_to_be16(0xffff)) {
8724 				field_flags |= I40E_CLOUD_FIELD_IIP;
8725 			} else {
8726 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8727 					be16_to_cpu(match.mask->src));
8728 				return -EIO;
8729 			}
8730 		}
8731 
8732 		if (match.mask->dst) {
8733 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8734 				field_flags |= I40E_CLOUD_FIELD_IIP;
8735 			} else {
8736 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8737 					be16_to_cpu(match.mask->dst));
8738 				return -EIO;
8739 			}
8740 		}
8741 
8742 		filter->dst_port = match.key->dst;
8743 		filter->src_port = match.key->src;
8744 
8745 		switch (filter->ip_proto) {
8746 		case IPPROTO_TCP:
8747 		case IPPROTO_UDP:
8748 			break;
8749 		default:
8750 			dev_err(&pf->pdev->dev,
8751 				"Only UDP and TCP transport are supported\n");
8752 			return -EINVAL;
8753 		}
8754 	}
8755 	filter->flags = field_flags;
8756 	return 0;
8757 }
8758 
8759 /**
8760  * i40e_handle_tclass: Forward to a traffic class on the device
8761  * @vsi: Pointer to VSI
8762  * @tc: traffic class index on the device
8763  * @filter: Pointer to cloud filter structure
8764  *
8765  **/
8766 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8767 			      struct i40e_cloud_filter *filter)
8768 {
8769 	struct i40e_channel *ch, *ch_tmp;
8770 
8771 	/* direct to a traffic class on the same device */
8772 	if (tc == 0) {
8773 		filter->seid = vsi->seid;
8774 		return 0;
8775 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8776 		if (!filter->dst_port) {
8777 			dev_err(&vsi->back->pdev->dev,
8778 				"Specify destination port to direct to traffic class that is not default\n");
8779 			return -EINVAL;
8780 		}
8781 		if (list_empty(&vsi->ch_list))
8782 			return -EINVAL;
8783 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8784 					 list) {
8785 			if (ch->seid == vsi->tc_seid_map[tc])
8786 				filter->seid = ch->seid;
8787 		}
8788 		return 0;
8789 	}
8790 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8791 	return -EINVAL;
8792 }
8793 
8794 /**
8795  * i40e_configure_clsflower - Configure tc flower filters
8796  * @vsi: Pointer to VSI
8797  * @cls_flower: Pointer to struct flow_cls_offload
8798  *
8799  **/
8800 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8801 				    struct flow_cls_offload *cls_flower)
8802 {
8803 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8804 	struct i40e_cloud_filter *filter = NULL;
8805 	struct i40e_pf *pf = vsi->back;
8806 	int err = 0;
8807 
8808 	if (tc < 0) {
8809 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8810 		return -EOPNOTSUPP;
8811 	}
8812 
8813 	if (!tc) {
8814 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8815 		return -EINVAL;
8816 	}
8817 
8818 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8819 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8820 		return -EBUSY;
8821 
8822 	if (pf->fdir_pf_active_filters ||
8823 	    (!hlist_empty(&pf->fdir_filter_list))) {
8824 		dev_err(&vsi->back->pdev->dev,
8825 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8826 		return -EINVAL;
8827 	}
8828 
8829 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8830 		dev_err(&vsi->back->pdev->dev,
8831 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8832 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8833 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8834 	}
8835 
8836 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8837 	if (!filter)
8838 		return -ENOMEM;
8839 
8840 	filter->cookie = cls_flower->cookie;
8841 
8842 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8843 	if (err < 0)
8844 		goto err;
8845 
8846 	err = i40e_handle_tclass(vsi, tc, filter);
8847 	if (err < 0)
8848 		goto err;
8849 
8850 	/* Add cloud filter */
8851 	if (filter->dst_port)
8852 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8853 	else
8854 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8855 
8856 	if (err) {
8857 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8858 			err);
8859 		goto err;
8860 	}
8861 
8862 	/* add filter to the ordered list */
8863 	INIT_HLIST_NODE(&filter->cloud_node);
8864 
8865 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8866 
8867 	pf->num_cloud_filters++;
8868 
8869 	return err;
8870 err:
8871 	kfree(filter);
8872 	return err;
8873 }
8874 
8875 /**
8876  * i40e_find_cloud_filter - Find the could filter in the list
8877  * @vsi: Pointer to VSI
8878  * @cookie: filter specific cookie
8879  *
8880  **/
8881 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8882 							unsigned long *cookie)
8883 {
8884 	struct i40e_cloud_filter *filter = NULL;
8885 	struct hlist_node *node2;
8886 
8887 	hlist_for_each_entry_safe(filter, node2,
8888 				  &vsi->back->cloud_filter_list, cloud_node)
8889 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8890 			return filter;
8891 	return NULL;
8892 }
8893 
8894 /**
8895  * i40e_delete_clsflower - Remove tc flower filters
8896  * @vsi: Pointer to VSI
8897  * @cls_flower: Pointer to struct flow_cls_offload
8898  *
8899  **/
8900 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8901 				 struct flow_cls_offload *cls_flower)
8902 {
8903 	struct i40e_cloud_filter *filter = NULL;
8904 	struct i40e_pf *pf = vsi->back;
8905 	int err = 0;
8906 
8907 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8908 
8909 	if (!filter)
8910 		return -EINVAL;
8911 
8912 	hash_del(&filter->cloud_node);
8913 
8914 	if (filter->dst_port)
8915 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8916 	else
8917 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8918 
8919 	kfree(filter);
8920 	if (err) {
8921 		dev_err(&pf->pdev->dev,
8922 			"Failed to delete cloud filter, err %pe\n",
8923 			ERR_PTR(err));
8924 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8925 	}
8926 
8927 	pf->num_cloud_filters--;
8928 	if (!pf->num_cloud_filters)
8929 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8930 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8931 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8932 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8933 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8934 		}
8935 	return 0;
8936 }
8937 
8938 /**
8939  * i40e_setup_tc_cls_flower - flower classifier offloads
8940  * @np: net device to configure
8941  * @cls_flower: offload data
8942  **/
8943 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8944 				    struct flow_cls_offload *cls_flower)
8945 {
8946 	struct i40e_vsi *vsi = np->vsi;
8947 
8948 	switch (cls_flower->command) {
8949 	case FLOW_CLS_REPLACE:
8950 		return i40e_configure_clsflower(vsi, cls_flower);
8951 	case FLOW_CLS_DESTROY:
8952 		return i40e_delete_clsflower(vsi, cls_flower);
8953 	case FLOW_CLS_STATS:
8954 		return -EOPNOTSUPP;
8955 	default:
8956 		return -EOPNOTSUPP;
8957 	}
8958 }
8959 
8960 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8961 				  void *cb_priv)
8962 {
8963 	struct i40e_netdev_priv *np = cb_priv;
8964 
8965 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8966 		return -EOPNOTSUPP;
8967 
8968 	switch (type) {
8969 	case TC_SETUP_CLSFLOWER:
8970 		return i40e_setup_tc_cls_flower(np, type_data);
8971 
8972 	default:
8973 		return -EOPNOTSUPP;
8974 	}
8975 }
8976 
8977 static LIST_HEAD(i40e_block_cb_list);
8978 
8979 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8980 			   void *type_data)
8981 {
8982 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8983 
8984 	switch (type) {
8985 	case TC_SETUP_QDISC_MQPRIO:
8986 		return i40e_setup_tc(netdev, type_data);
8987 	case TC_SETUP_BLOCK:
8988 		return flow_block_cb_setup_simple(type_data,
8989 						  &i40e_block_cb_list,
8990 						  i40e_setup_tc_block_cb,
8991 						  np, np, true);
8992 	default:
8993 		return -EOPNOTSUPP;
8994 	}
8995 }
8996 
8997 /**
8998  * i40e_open - Called when a network interface is made active
8999  * @netdev: network interface device structure
9000  *
9001  * The open entry point is called when a network interface is made
9002  * active by the system (IFF_UP).  At this point all resources needed
9003  * for transmit and receive operations are allocated, the interrupt
9004  * handler is registered with the OS, the netdev watchdog subtask is
9005  * enabled, and the stack is notified that the interface is ready.
9006  *
9007  * Returns 0 on success, negative value on failure
9008  **/
9009 int i40e_open(struct net_device *netdev)
9010 {
9011 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9012 	struct i40e_vsi *vsi = np->vsi;
9013 	struct i40e_pf *pf = vsi->back;
9014 	int err;
9015 
9016 	/* disallow open during test or if eeprom is broken */
9017 	if (test_bit(__I40E_TESTING, pf->state) ||
9018 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9019 		return -EBUSY;
9020 
9021 	netif_carrier_off(netdev);
9022 
9023 	if (i40e_force_link_state(pf, true))
9024 		return -EAGAIN;
9025 
9026 	err = i40e_vsi_open(vsi);
9027 	if (err)
9028 		return err;
9029 
9030 	/* configure global TSO hardware offload settings */
9031 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9032 						       TCP_FLAG_FIN) >> 16);
9033 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9034 						       TCP_FLAG_FIN |
9035 						       TCP_FLAG_CWR) >> 16);
9036 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9037 	udp_tunnel_get_rx_info(netdev);
9038 
9039 	return 0;
9040 }
9041 
9042 /**
9043  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9044  * @vsi: vsi structure
9045  *
9046  * This updates netdev's number of tx/rx queues
9047  *
9048  * Returns status of setting tx/rx queues
9049  **/
9050 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9051 {
9052 	int ret;
9053 
9054 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9055 					   vsi->num_queue_pairs);
9056 	if (ret)
9057 		return ret;
9058 
9059 	return netif_set_real_num_tx_queues(vsi->netdev,
9060 					    vsi->num_queue_pairs);
9061 }
9062 
9063 /**
9064  * i40e_vsi_open -
9065  * @vsi: the VSI to open
9066  *
9067  * Finish initialization of the VSI.
9068  *
9069  * Returns 0 on success, negative value on failure
9070  *
9071  * Note: expects to be called while under rtnl_lock()
9072  **/
9073 int i40e_vsi_open(struct i40e_vsi *vsi)
9074 {
9075 	struct i40e_pf *pf = vsi->back;
9076 	char int_name[I40E_INT_NAME_STR_LEN];
9077 	int err;
9078 
9079 	/* allocate descriptors */
9080 	err = i40e_vsi_setup_tx_resources(vsi);
9081 	if (err)
9082 		goto err_setup_tx;
9083 	err = i40e_vsi_setup_rx_resources(vsi);
9084 	if (err)
9085 		goto err_setup_rx;
9086 
9087 	err = i40e_vsi_configure(vsi);
9088 	if (err)
9089 		goto err_setup_rx;
9090 
9091 	if (vsi->netdev) {
9092 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9093 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9094 		err = i40e_vsi_request_irq(vsi, int_name);
9095 		if (err)
9096 			goto err_setup_rx;
9097 
9098 		/* Notify the stack of the actual queue counts. */
9099 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9100 		if (err)
9101 			goto err_set_queues;
9102 
9103 	} else if (vsi->type == I40E_VSI_FDIR) {
9104 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9105 			 dev_driver_string(&pf->pdev->dev),
9106 			 dev_name(&pf->pdev->dev));
9107 		err = i40e_vsi_request_irq(vsi, int_name);
9108 		if (err)
9109 			goto err_setup_rx;
9110 
9111 	} else {
9112 		err = -EINVAL;
9113 		goto err_setup_rx;
9114 	}
9115 
9116 	err = i40e_up_complete(vsi);
9117 	if (err)
9118 		goto err_up_complete;
9119 
9120 	return 0;
9121 
9122 err_up_complete:
9123 	i40e_down(vsi);
9124 err_set_queues:
9125 	i40e_vsi_free_irq(vsi);
9126 err_setup_rx:
9127 	i40e_vsi_free_rx_resources(vsi);
9128 err_setup_tx:
9129 	i40e_vsi_free_tx_resources(vsi);
9130 	if (vsi->type == I40E_VSI_MAIN)
9131 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9132 
9133 	return err;
9134 }
9135 
9136 /**
9137  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9138  * @pf: Pointer to PF
9139  *
9140  * This function destroys the hlist where all the Flow Director
9141  * filters were saved.
9142  **/
9143 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9144 {
9145 	struct i40e_fdir_filter *filter;
9146 	struct i40e_flex_pit *pit_entry, *tmp;
9147 	struct hlist_node *node2;
9148 
9149 	hlist_for_each_entry_safe(filter, node2,
9150 				  &pf->fdir_filter_list, fdir_node) {
9151 		hlist_del(&filter->fdir_node);
9152 		kfree(filter);
9153 	}
9154 
9155 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9156 		list_del(&pit_entry->list);
9157 		kfree(pit_entry);
9158 	}
9159 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9160 
9161 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9162 		list_del(&pit_entry->list);
9163 		kfree(pit_entry);
9164 	}
9165 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9166 
9167 	pf->fdir_pf_active_filters = 0;
9168 	i40e_reset_fdir_filter_cnt(pf);
9169 
9170 	/* Reprogram the default input set for TCP/IPv4 */
9171 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9172 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9173 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9174 
9175 	/* Reprogram the default input set for TCP/IPv6 */
9176 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_TCP,
9177 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9178 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9179 
9180 	/* Reprogram the default input set for UDP/IPv4 */
9181 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_UDP,
9182 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9183 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9184 
9185 	/* Reprogram the default input set for UDP/IPv6 */
9186 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_UDP,
9187 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9188 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9189 
9190 	/* Reprogram the default input set for SCTP/IPv4 */
9191 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_SCTP,
9192 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9193 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9194 
9195 	/* Reprogram the default input set for SCTP/IPv6 */
9196 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_SCTP,
9197 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9198 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9199 
9200 	/* Reprogram the default input set for Other/IPv4 */
9201 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_OTHER,
9202 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9203 
9204 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV4,
9205 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9206 
9207 	/* Reprogram the default input set for Other/IPv6 */
9208 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_OTHER,
9209 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9210 
9211 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV6,
9212 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9213 }
9214 
9215 /**
9216  * i40e_cloud_filter_exit - Cleans up the cloud filters
9217  * @pf: Pointer to PF
9218  *
9219  * This function destroys the hlist where all the cloud filters
9220  * were saved.
9221  **/
9222 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9223 {
9224 	struct i40e_cloud_filter *cfilter;
9225 	struct hlist_node *node;
9226 
9227 	hlist_for_each_entry_safe(cfilter, node,
9228 				  &pf->cloud_filter_list, cloud_node) {
9229 		hlist_del(&cfilter->cloud_node);
9230 		kfree(cfilter);
9231 	}
9232 	pf->num_cloud_filters = 0;
9233 
9234 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9235 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9236 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9237 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9238 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9239 	}
9240 }
9241 
9242 /**
9243  * i40e_close - Disables a network interface
9244  * @netdev: network interface device structure
9245  *
9246  * The close entry point is called when an interface is de-activated
9247  * by the OS.  The hardware is still under the driver's control, but
9248  * this netdev interface is disabled.
9249  *
9250  * Returns 0, this is not allowed to fail
9251  **/
9252 int i40e_close(struct net_device *netdev)
9253 {
9254 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9255 	struct i40e_vsi *vsi = np->vsi;
9256 
9257 	i40e_vsi_close(vsi);
9258 
9259 	return 0;
9260 }
9261 
9262 /**
9263  * i40e_do_reset - Start a PF or Core Reset sequence
9264  * @pf: board private structure
9265  * @reset_flags: which reset is requested
9266  * @lock_acquired: indicates whether or not the lock has been acquired
9267  * before this function was called.
9268  *
9269  * The essential difference in resets is that the PF Reset
9270  * doesn't clear the packet buffers, doesn't reset the PE
9271  * firmware, and doesn't bother the other PFs on the chip.
9272  **/
9273 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9274 {
9275 	struct i40e_vsi *vsi;
9276 	u32 val;
9277 	int i;
9278 
9279 	/* do the biggest reset indicated */
9280 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9281 
9282 		/* Request a Global Reset
9283 		 *
9284 		 * This will start the chip's countdown to the actual full
9285 		 * chip reset event, and a warning interrupt to be sent
9286 		 * to all PFs, including the requestor.  Our handler
9287 		 * for the warning interrupt will deal with the shutdown
9288 		 * and recovery of the switch setup.
9289 		 */
9290 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9291 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9292 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9293 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9294 
9295 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9296 
9297 		/* Request a Core Reset
9298 		 *
9299 		 * Same as Global Reset, except does *not* include the MAC/PHY
9300 		 */
9301 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9302 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9303 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9304 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9305 		i40e_flush(&pf->hw);
9306 
9307 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9308 
9309 		/* Request a PF Reset
9310 		 *
9311 		 * Resets only the PF-specific registers
9312 		 *
9313 		 * This goes directly to the tear-down and rebuild of
9314 		 * the switch, since we need to do all the recovery as
9315 		 * for the Core Reset.
9316 		 */
9317 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9318 		i40e_handle_reset_warning(pf, lock_acquired);
9319 
9320 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9321 		/* Request a PF Reset
9322 		 *
9323 		 * Resets PF and reinitializes PFs VSI.
9324 		 */
9325 		i40e_prep_for_reset(pf);
9326 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9327 		dev_info(&pf->pdev->dev,
9328 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9329 			 "FW LLDP is disabled\n" :
9330 			 "FW LLDP is enabled\n");
9331 
9332 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9333 		/* Find the VSI(s) that requested a re-init */
9334 		dev_info(&pf->pdev->dev, "VSI reinit requested\n");
9335 
9336 		i40e_pf_for_each_vsi(pf, i, vsi) {
9337 			if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9338 					       vsi->state))
9339 				i40e_vsi_reinit_locked(vsi);
9340 		}
9341 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9342 		/* Find the VSI(s) that needs to be brought down */
9343 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9344 
9345 		i40e_pf_for_each_vsi(pf, i, vsi) {
9346 			if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9347 					       vsi->state)) {
9348 				set_bit(__I40E_VSI_DOWN, vsi->state);
9349 				i40e_down(vsi);
9350 			}
9351 		}
9352 	} else {
9353 		dev_info(&pf->pdev->dev,
9354 			 "bad reset request 0x%08x\n", reset_flags);
9355 	}
9356 }
9357 
9358 #ifdef CONFIG_I40E_DCB
9359 /**
9360  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9361  * @pf: board private structure
9362  * @old_cfg: current DCB config
9363  * @new_cfg: new DCB config
9364  **/
9365 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9366 			    struct i40e_dcbx_config *old_cfg,
9367 			    struct i40e_dcbx_config *new_cfg)
9368 {
9369 	bool need_reconfig = false;
9370 
9371 	/* Check if ETS configuration has changed */
9372 	if (memcmp(&new_cfg->etscfg,
9373 		   &old_cfg->etscfg,
9374 		   sizeof(new_cfg->etscfg))) {
9375 		/* If Priority Table has changed reconfig is needed */
9376 		if (memcmp(&new_cfg->etscfg.prioritytable,
9377 			   &old_cfg->etscfg.prioritytable,
9378 			   sizeof(new_cfg->etscfg.prioritytable))) {
9379 			need_reconfig = true;
9380 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9381 		}
9382 
9383 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9384 			   &old_cfg->etscfg.tcbwtable,
9385 			   sizeof(new_cfg->etscfg.tcbwtable)))
9386 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9387 
9388 		if (memcmp(&new_cfg->etscfg.tsatable,
9389 			   &old_cfg->etscfg.tsatable,
9390 			   sizeof(new_cfg->etscfg.tsatable)))
9391 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9392 	}
9393 
9394 	/* Check if PFC configuration has changed */
9395 	if (memcmp(&new_cfg->pfc,
9396 		   &old_cfg->pfc,
9397 		   sizeof(new_cfg->pfc))) {
9398 		need_reconfig = true;
9399 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9400 	}
9401 
9402 	/* Check if APP Table has changed */
9403 	if (memcmp(&new_cfg->app,
9404 		   &old_cfg->app,
9405 		   sizeof(new_cfg->app))) {
9406 		need_reconfig = true;
9407 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9408 	}
9409 
9410 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9411 	return need_reconfig;
9412 }
9413 
9414 /**
9415  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9416  * @pf: board private structure
9417  * @e: event info posted on ARQ
9418  **/
9419 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9420 				  struct i40e_arq_event_info *e)
9421 {
9422 	struct i40e_aqc_lldp_get_mib *mib =
9423 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9424 	struct i40e_hw *hw = &pf->hw;
9425 	struct i40e_dcbx_config tmp_dcbx_cfg;
9426 	bool need_reconfig = false;
9427 	int ret = 0;
9428 	u8 type;
9429 
9430 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9431 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9432 	    (hw->phy.link_info.link_speed &
9433 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9434 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9435 		/* let firmware decide if the DCB should be disabled */
9436 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9437 
9438 	/* Not DCB capable or capability disabled */
9439 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9440 		return ret;
9441 
9442 	/* Ignore if event is not for Nearest Bridge */
9443 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9444 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9445 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9446 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9447 		return ret;
9448 
9449 	/* Check MIB Type and return if event for Remote MIB update */
9450 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9451 	dev_dbg(&pf->pdev->dev,
9452 		"LLDP event mib type %s\n", type ? "remote" : "local");
9453 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9454 		/* Update the remote cached instance and return */
9455 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9456 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9457 				&hw->remote_dcbx_config);
9458 		goto exit;
9459 	}
9460 
9461 	/* Store the old configuration */
9462 	tmp_dcbx_cfg = hw->local_dcbx_config;
9463 
9464 	/* Reset the old DCBx configuration data */
9465 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9466 	/* Get updated DCBX data from firmware */
9467 	ret = i40e_get_dcb_config(&pf->hw);
9468 	if (ret) {
9469 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9470 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9471 		    (hw->phy.link_info.link_speed &
9472 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9473 			dev_warn(&pf->pdev->dev,
9474 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9475 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9476 		} else {
9477 			dev_info(&pf->pdev->dev,
9478 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9479 				 ERR_PTR(ret),
9480 				 i40e_aq_str(&pf->hw,
9481 					     pf->hw.aq.asq_last_status));
9482 		}
9483 		goto exit;
9484 	}
9485 
9486 	/* No change detected in DCBX configs */
9487 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9488 		    sizeof(tmp_dcbx_cfg))) {
9489 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9490 		goto exit;
9491 	}
9492 
9493 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9494 					       &hw->local_dcbx_config);
9495 
9496 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9497 
9498 	if (!need_reconfig)
9499 		goto exit;
9500 
9501 	/* Enable DCB tagging only when more than one TC */
9502 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9503 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9504 	else
9505 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9506 
9507 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9508 	/* Reconfiguration needed quiesce all VSIs */
9509 	i40e_pf_quiesce_all_vsi(pf);
9510 
9511 	/* Changes in configuration update VEB/VSI */
9512 	i40e_dcb_reconfigure(pf);
9513 
9514 	ret = i40e_resume_port_tx(pf);
9515 
9516 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9517 	/* In case of error no point in resuming VSIs */
9518 	if (ret)
9519 		goto exit;
9520 
9521 	/* Wait for the PF's queues to be disabled */
9522 	ret = i40e_pf_wait_queues_disabled(pf);
9523 	if (ret) {
9524 		/* Schedule PF reset to recover */
9525 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9526 		i40e_service_event_schedule(pf);
9527 	} else {
9528 		i40e_pf_unquiesce_all_vsi(pf);
9529 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9530 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9531 	}
9532 
9533 exit:
9534 	return ret;
9535 }
9536 #endif /* CONFIG_I40E_DCB */
9537 
9538 /**
9539  * i40e_do_reset_safe - Protected reset path for userland calls.
9540  * @pf: board private structure
9541  * @reset_flags: which reset is requested
9542  *
9543  **/
9544 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9545 {
9546 	rtnl_lock();
9547 	i40e_do_reset(pf, reset_flags, true);
9548 	rtnl_unlock();
9549 }
9550 
9551 /**
9552  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9553  * @pf: board private structure
9554  * @e: event info posted on ARQ
9555  *
9556  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9557  * and VF queues
9558  **/
9559 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9560 					   struct i40e_arq_event_info *e)
9561 {
9562 	struct i40e_aqc_lan_overflow *data =
9563 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9564 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9565 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9566 	struct i40e_hw *hw = &pf->hw;
9567 	struct i40e_vf *vf;
9568 	u16 vf_id;
9569 
9570 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9571 		queue, qtx_ctl);
9572 
9573 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9574 	    I40E_QTX_CTL_VF_QUEUE)
9575 		return;
9576 
9577 	/* Queue belongs to VF, find the VF and issue VF reset */
9578 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9579 	vf_id -= hw->func_caps.vf_base_id;
9580 	vf = &pf->vf[vf_id];
9581 	i40e_vc_notify_vf_reset(vf);
9582 	/* Allow VF to process pending reset notification */
9583 	msleep(20);
9584 	i40e_reset_vf(vf, false);
9585 }
9586 
9587 /**
9588  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9589  * @pf: board private structure
9590  **/
9591 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9592 {
9593 	u32 val, fcnt_prog;
9594 
9595 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9596 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9597 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9598 	return fcnt_prog;
9599 }
9600 
9601 /**
9602  * i40e_get_global_fd_count - Get total FD filters programmed on device
9603  * @pf: board private structure
9604  **/
9605 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9606 {
9607 	u32 val, fcnt_prog;
9608 
9609 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9610 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9611 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9612 	return fcnt_prog;
9613 }
9614 
9615 /**
9616  * i40e_reenable_fdir_sb - Restore FDir SB capability
9617  * @pf: board private structure
9618  **/
9619 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9620 {
9621 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9622 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9623 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9624 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9625 }
9626 
9627 /**
9628  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9629  * @pf: board private structure
9630  **/
9631 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9632 {
9633 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9634 		/* ATR uses the same filtering logic as SB rules. It only
9635 		 * functions properly if the input set mask is at the default
9636 		 * settings. It is safe to restore the default input set
9637 		 * because there are no active TCPv4 filter rules.
9638 		 */
9639 		i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9640 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9641 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9642 
9643 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9644 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9645 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9646 	}
9647 }
9648 
9649 /**
9650  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9651  * @pf: board private structure
9652  * @filter: FDir filter to remove
9653  */
9654 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9655 				       struct i40e_fdir_filter *filter)
9656 {
9657 	/* Update counters */
9658 	pf->fdir_pf_active_filters--;
9659 	pf->fd_inv = 0;
9660 
9661 	switch (filter->flow_type) {
9662 	case TCP_V4_FLOW:
9663 		pf->fd_tcp4_filter_cnt--;
9664 		break;
9665 	case UDP_V4_FLOW:
9666 		pf->fd_udp4_filter_cnt--;
9667 		break;
9668 	case SCTP_V4_FLOW:
9669 		pf->fd_sctp4_filter_cnt--;
9670 		break;
9671 	case TCP_V6_FLOW:
9672 		pf->fd_tcp6_filter_cnt--;
9673 		break;
9674 	case UDP_V6_FLOW:
9675 		pf->fd_udp6_filter_cnt--;
9676 		break;
9677 	case SCTP_V6_FLOW:
9678 		pf->fd_udp6_filter_cnt--;
9679 		break;
9680 	case IP_USER_FLOW:
9681 		switch (filter->ipl4_proto) {
9682 		case IPPROTO_TCP:
9683 			pf->fd_tcp4_filter_cnt--;
9684 			break;
9685 		case IPPROTO_UDP:
9686 			pf->fd_udp4_filter_cnt--;
9687 			break;
9688 		case IPPROTO_SCTP:
9689 			pf->fd_sctp4_filter_cnt--;
9690 			break;
9691 		case IPPROTO_IP:
9692 			pf->fd_ip4_filter_cnt--;
9693 			break;
9694 		}
9695 		break;
9696 	case IPV6_USER_FLOW:
9697 		switch (filter->ipl4_proto) {
9698 		case IPPROTO_TCP:
9699 			pf->fd_tcp6_filter_cnt--;
9700 			break;
9701 		case IPPROTO_UDP:
9702 			pf->fd_udp6_filter_cnt--;
9703 			break;
9704 		case IPPROTO_SCTP:
9705 			pf->fd_sctp6_filter_cnt--;
9706 			break;
9707 		case IPPROTO_IP:
9708 			pf->fd_ip6_filter_cnt--;
9709 			break;
9710 		}
9711 		break;
9712 	}
9713 
9714 	/* Remove the filter from the list and free memory */
9715 	hlist_del(&filter->fdir_node);
9716 	kfree(filter);
9717 }
9718 
9719 /**
9720  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9721  * @pf: board private structure
9722  **/
9723 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9724 {
9725 	struct i40e_fdir_filter *filter;
9726 	u32 fcnt_prog, fcnt_avail;
9727 	struct hlist_node *node;
9728 
9729 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9730 		return;
9731 
9732 	/* Check if we have enough room to re-enable FDir SB capability. */
9733 	fcnt_prog = i40e_get_global_fd_count(pf);
9734 	fcnt_avail = pf->fdir_pf_filter_count;
9735 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9736 	    (pf->fd_add_err == 0) ||
9737 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9738 		i40e_reenable_fdir_sb(pf);
9739 
9740 	/* We should wait for even more space before re-enabling ATR.
9741 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9742 	 * rules active.
9743 	 */
9744 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9745 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9746 		i40e_reenable_fdir_atr(pf);
9747 
9748 	/* if hw had a problem adding a filter, delete it */
9749 	if (pf->fd_inv > 0) {
9750 		hlist_for_each_entry_safe(filter, node,
9751 					  &pf->fdir_filter_list, fdir_node)
9752 			if (filter->fd_id == pf->fd_inv)
9753 				i40e_delete_invalid_filter(pf, filter);
9754 	}
9755 }
9756 
9757 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9758 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9759 /**
9760  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9761  * @pf: board private structure
9762  **/
9763 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9764 {
9765 	unsigned long min_flush_time;
9766 	int flush_wait_retry = 50;
9767 	bool disable_atr = false;
9768 	int fd_room;
9769 	int reg;
9770 
9771 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9772 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9773 		return;
9774 
9775 	/* If the flush is happening too quick and we have mostly SB rules we
9776 	 * should not re-enable ATR for some time.
9777 	 */
9778 	min_flush_time = pf->fd_flush_timestamp +
9779 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9780 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9781 
9782 	if (!(time_after(jiffies, min_flush_time)) &&
9783 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9784 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9785 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9786 		disable_atr = true;
9787 	}
9788 
9789 	pf->fd_flush_timestamp = jiffies;
9790 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9791 	/* flush all filters */
9792 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9793 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9794 	i40e_flush(&pf->hw);
9795 	pf->fd_flush_cnt++;
9796 	pf->fd_add_err = 0;
9797 	do {
9798 		/* Check FD flush status every 5-6msec */
9799 		usleep_range(5000, 6000);
9800 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9801 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9802 			break;
9803 	} while (flush_wait_retry--);
9804 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9805 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9806 	} else {
9807 		/* replay sideband filters */
9808 		i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
9809 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9810 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9811 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9812 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9813 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9814 	}
9815 }
9816 
9817 /**
9818  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9819  * @pf: board private structure
9820  **/
9821 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9822 {
9823 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9824 }
9825 
9826 /**
9827  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9828  * @pf: board private structure
9829  **/
9830 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9831 {
9832 
9833 	/* if interface is down do nothing */
9834 	if (test_bit(__I40E_DOWN, pf->state))
9835 		return;
9836 
9837 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9838 		i40e_fdir_flush_and_replay(pf);
9839 
9840 	i40e_fdir_check_and_reenable(pf);
9841 
9842 }
9843 
9844 /**
9845  * i40e_vsi_link_event - notify VSI of a link event
9846  * @vsi: vsi to be notified
9847  * @link_up: link up or down
9848  **/
9849 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9850 {
9851 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9852 		return;
9853 
9854 	switch (vsi->type) {
9855 	case I40E_VSI_MAIN:
9856 		if (!vsi->netdev || !vsi->netdev_registered)
9857 			break;
9858 
9859 		if (link_up) {
9860 			netif_carrier_on(vsi->netdev);
9861 			netif_tx_wake_all_queues(vsi->netdev);
9862 		} else {
9863 			netif_carrier_off(vsi->netdev);
9864 			netif_tx_stop_all_queues(vsi->netdev);
9865 		}
9866 		break;
9867 
9868 	case I40E_VSI_SRIOV:
9869 	case I40E_VSI_VMDQ2:
9870 	case I40E_VSI_CTRL:
9871 	case I40E_VSI_IWARP:
9872 	case I40E_VSI_MIRROR:
9873 	default:
9874 		/* there is no notification for other VSIs */
9875 		break;
9876 	}
9877 }
9878 
9879 /**
9880  * i40e_veb_link_event - notify elements on the veb of a link event
9881  * @veb: veb to be notified
9882  * @link_up: link up or down
9883  **/
9884 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9885 {
9886 	struct i40e_vsi *vsi;
9887 	struct i40e_pf *pf;
9888 	int i;
9889 
9890 	if (!veb || !veb->pf)
9891 		return;
9892 	pf = veb->pf;
9893 
9894 	/* Send link event to contained VSIs */
9895 	i40e_pf_for_each_vsi(pf, i, vsi)
9896 		if (vsi->uplink_seid == veb->seid)
9897 			i40e_vsi_link_event(vsi, link_up);
9898 }
9899 
9900 /**
9901  * i40e_link_event - Update netif_carrier status
9902  * @pf: board private structure
9903  **/
9904 static void i40e_link_event(struct i40e_pf *pf)
9905 {
9906 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
9907 	struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
9908 	u8 new_link_speed, old_link_speed;
9909 	bool new_link, old_link;
9910 	int status;
9911 #ifdef CONFIG_I40E_DCB
9912 	int err;
9913 #endif /* CONFIG_I40E_DCB */
9914 
9915 	/* set this to force the get_link_status call to refresh state */
9916 	pf->hw.phy.get_link_info = true;
9917 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9918 	status = i40e_get_link_status(&pf->hw, &new_link);
9919 
9920 	/* On success, disable temp link polling */
9921 	if (status == 0) {
9922 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9923 	} else {
9924 		/* Enable link polling temporarily until i40e_get_link_status
9925 		 * returns 0
9926 		 */
9927 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9928 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9929 			status);
9930 		return;
9931 	}
9932 
9933 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9934 	new_link_speed = pf->hw.phy.link_info.link_speed;
9935 
9936 	if (new_link == old_link &&
9937 	    new_link_speed == old_link_speed &&
9938 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9939 	     new_link == netif_carrier_ok(vsi->netdev)))
9940 		return;
9941 
9942 	if (!new_link && old_link)
9943 		pf->link_down_events++;
9944 
9945 	i40e_print_link_message(vsi, new_link);
9946 
9947 	/* Notify the base of the switch tree connected to
9948 	 * the link.  Floating VEBs are not notified.
9949 	 */
9950 	if (veb)
9951 		i40e_veb_link_event(veb, new_link);
9952 	else
9953 		i40e_vsi_link_event(vsi, new_link);
9954 
9955 	if (pf->vf)
9956 		i40e_vc_notify_link_state(pf);
9957 
9958 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9959 		i40e_ptp_set_increment(pf);
9960 #ifdef CONFIG_I40E_DCB
9961 	if (new_link == old_link)
9962 		return;
9963 	/* Not SW DCB so firmware will take care of default settings */
9964 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9965 		return;
9966 
9967 	/* We cover here only link down, as after link up in case of SW DCB
9968 	 * SW LLDP agent will take care of setting it up
9969 	 */
9970 	if (!new_link) {
9971 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9972 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9973 		err = i40e_dcb_sw_default_config(pf);
9974 		if (err) {
9975 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9976 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9977 		} else {
9978 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9979 				       DCB_CAP_DCBX_VER_IEEE;
9980 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9981 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9982 		}
9983 	}
9984 #endif /* CONFIG_I40E_DCB */
9985 }
9986 
9987 /**
9988  * i40e_watchdog_subtask - periodic checks not using event driven response
9989  * @pf: board private structure
9990  **/
9991 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9992 {
9993 	struct i40e_vsi *vsi;
9994 	struct i40e_veb *veb;
9995 	int i;
9996 
9997 	/* if interface is down do nothing */
9998 	if (test_bit(__I40E_DOWN, pf->state) ||
9999 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
10000 		return;
10001 
10002 	/* make sure we don't do these things too often */
10003 	if (time_before(jiffies, (pf->service_timer_previous +
10004 				  pf->service_timer_period)))
10005 		return;
10006 	pf->service_timer_previous = jiffies;
10007 
10008 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10009 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10010 		i40e_link_event(pf);
10011 
10012 	/* Update the stats for active netdevs so the network stack
10013 	 * can look at updated numbers whenever it cares to
10014 	 */
10015 	i40e_pf_for_each_vsi(pf, i, vsi)
10016 		if (vsi->netdev)
10017 			i40e_update_stats(vsi);
10018 
10019 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10020 		/* Update the stats for the active switching components */
10021 		i40e_pf_for_each_veb(pf, i, veb)
10022 			i40e_update_veb_stats(veb);
10023 	}
10024 
10025 	i40e_ptp_rx_hang(pf);
10026 	i40e_ptp_tx_hang(pf);
10027 }
10028 
10029 /**
10030  * i40e_reset_subtask - Set up for resetting the device and driver
10031  * @pf: board private structure
10032  **/
10033 static void i40e_reset_subtask(struct i40e_pf *pf)
10034 {
10035 	u32 reset_flags = 0;
10036 
10037 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10038 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10039 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10040 	}
10041 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10042 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10043 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10044 	}
10045 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10046 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10047 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10048 	}
10049 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10050 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10051 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10052 	}
10053 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10054 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10055 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10056 	}
10057 
10058 	/* If there's a recovery already waiting, it takes
10059 	 * precedence before starting a new reset sequence.
10060 	 */
10061 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10062 		i40e_prep_for_reset(pf);
10063 		i40e_reset(pf);
10064 		i40e_rebuild(pf, false, false);
10065 	}
10066 
10067 	/* If we're already down or resetting, just bail */
10068 	if (reset_flags &&
10069 	    !test_bit(__I40E_DOWN, pf->state) &&
10070 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10071 		i40e_do_reset(pf, reset_flags, false);
10072 	}
10073 }
10074 
10075 /**
10076  * i40e_handle_link_event - Handle link event
10077  * @pf: board private structure
10078  * @e: event info posted on ARQ
10079  **/
10080 static void i40e_handle_link_event(struct i40e_pf *pf,
10081 				   struct i40e_arq_event_info *e)
10082 {
10083 	struct i40e_aqc_get_link_status *status =
10084 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10085 
10086 	/* Do a new status request to re-enable LSE reporting
10087 	 * and load new status information into the hw struct
10088 	 * This completely ignores any state information
10089 	 * in the ARQ event info, instead choosing to always
10090 	 * issue the AQ update link status command.
10091 	 */
10092 	i40e_link_event(pf);
10093 
10094 	/* Check if module meets thermal requirements */
10095 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10096 		dev_err(&pf->pdev->dev,
10097 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10098 		dev_err(&pf->pdev->dev,
10099 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10100 	} else {
10101 		/* check for unqualified module, if link is down, suppress
10102 		 * the message if link was forced to be down.
10103 		 */
10104 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10105 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10106 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10107 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10108 			dev_err(&pf->pdev->dev,
10109 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10110 			dev_err(&pf->pdev->dev,
10111 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10112 		}
10113 	}
10114 }
10115 
10116 /**
10117  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10118  * @pf: board private structure
10119  **/
10120 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10121 {
10122 	struct i40e_arq_event_info event;
10123 	struct i40e_hw *hw = &pf->hw;
10124 	u16 pending, i = 0;
10125 	u16 opcode;
10126 	u32 oldval;
10127 	int ret;
10128 	u32 val;
10129 
10130 	/* Do not run clean AQ when PF reset fails */
10131 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10132 		return;
10133 
10134 	/* check for error indications */
10135 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10136 	oldval = val;
10137 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10138 		if (hw->debug_mask & I40E_DEBUG_AQ)
10139 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10140 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10141 	}
10142 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10143 		if (hw->debug_mask & I40E_DEBUG_AQ)
10144 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10145 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10146 		pf->arq_overflows++;
10147 	}
10148 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10149 		if (hw->debug_mask & I40E_DEBUG_AQ)
10150 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10151 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10152 	}
10153 	if (oldval != val)
10154 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10155 
10156 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10157 	oldval = val;
10158 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10159 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10160 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10161 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10162 	}
10163 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10164 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10165 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10166 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10167 	}
10168 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10169 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10170 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10171 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10172 	}
10173 	if (oldval != val)
10174 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10175 
10176 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10177 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10178 	if (!event.msg_buf)
10179 		return;
10180 
10181 	do {
10182 		ret = i40e_clean_arq_element(hw, &event, &pending);
10183 		if (ret == -EALREADY)
10184 			break;
10185 		else if (ret) {
10186 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10187 			break;
10188 		}
10189 
10190 		opcode = le16_to_cpu(event.desc.opcode);
10191 		switch (opcode) {
10192 
10193 		case i40e_aqc_opc_get_link_status:
10194 			rtnl_lock();
10195 			i40e_handle_link_event(pf, &event);
10196 			rtnl_unlock();
10197 			break;
10198 		case i40e_aqc_opc_send_msg_to_pf:
10199 			ret = i40e_vc_process_vf_msg(pf,
10200 					le16_to_cpu(event.desc.retval),
10201 					le32_to_cpu(event.desc.cookie_high),
10202 					le32_to_cpu(event.desc.cookie_low),
10203 					event.msg_buf,
10204 					event.msg_len);
10205 			break;
10206 		case i40e_aqc_opc_lldp_update_mib:
10207 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10208 #ifdef CONFIG_I40E_DCB
10209 			rtnl_lock();
10210 			i40e_handle_lldp_event(pf, &event);
10211 			rtnl_unlock();
10212 #endif /* CONFIG_I40E_DCB */
10213 			break;
10214 		case i40e_aqc_opc_event_lan_overflow:
10215 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10216 			i40e_handle_lan_overflow_event(pf, &event);
10217 			break;
10218 		case i40e_aqc_opc_send_msg_to_peer:
10219 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10220 			break;
10221 		case i40e_aqc_opc_nvm_erase:
10222 		case i40e_aqc_opc_nvm_update:
10223 		case i40e_aqc_opc_oem_post_update:
10224 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10225 				   "ARQ NVM operation 0x%04x completed\n",
10226 				   opcode);
10227 			break;
10228 		default:
10229 			dev_info(&pf->pdev->dev,
10230 				 "ARQ: Unknown event 0x%04x ignored\n",
10231 				 opcode);
10232 			break;
10233 		}
10234 	} while (i++ < I40E_AQ_WORK_LIMIT);
10235 
10236 	if (i < I40E_AQ_WORK_LIMIT)
10237 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10238 
10239 	/* re-enable Admin queue interrupt cause */
10240 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10241 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10242 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10243 	i40e_flush(hw);
10244 
10245 	kfree(event.msg_buf);
10246 }
10247 
10248 /**
10249  * i40e_verify_eeprom - make sure eeprom is good to use
10250  * @pf: board private structure
10251  **/
10252 static void i40e_verify_eeprom(struct i40e_pf *pf)
10253 {
10254 	int err;
10255 
10256 	err = i40e_diag_eeprom_test(&pf->hw);
10257 	if (err) {
10258 		/* retry in case of garbage read */
10259 		err = i40e_diag_eeprom_test(&pf->hw);
10260 		if (err) {
10261 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10262 				 err);
10263 			set_bit(__I40E_BAD_EEPROM, pf->state);
10264 		}
10265 	}
10266 
10267 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10268 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10269 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10270 	}
10271 }
10272 
10273 /**
10274  * i40e_enable_pf_switch_lb
10275  * @pf: pointer to the PF structure
10276  *
10277  * enable switch loop back or die - no point in a return value
10278  **/
10279 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10280 {
10281 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10282 	struct i40e_vsi_context ctxt;
10283 	int ret;
10284 
10285 	ctxt.seid = pf->main_vsi_seid;
10286 	ctxt.pf_num = pf->hw.pf_id;
10287 	ctxt.vf_num = 0;
10288 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10289 	if (ret) {
10290 		dev_info(&pf->pdev->dev,
10291 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10292 			 ERR_PTR(ret),
10293 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10294 		return;
10295 	}
10296 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10297 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10298 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10299 
10300 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10301 	if (ret) {
10302 		dev_info(&pf->pdev->dev,
10303 			 "update vsi switch failed, err %pe aq_err %s\n",
10304 			 ERR_PTR(ret),
10305 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10306 	}
10307 }
10308 
10309 /**
10310  * i40e_disable_pf_switch_lb
10311  * @pf: pointer to the PF structure
10312  *
10313  * disable switch loop back or die - no point in a return value
10314  **/
10315 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10316 {
10317 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10318 	struct i40e_vsi_context ctxt;
10319 	int ret;
10320 
10321 	ctxt.seid = pf->main_vsi_seid;
10322 	ctxt.pf_num = pf->hw.pf_id;
10323 	ctxt.vf_num = 0;
10324 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10325 	if (ret) {
10326 		dev_info(&pf->pdev->dev,
10327 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10328 			 ERR_PTR(ret),
10329 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10330 		return;
10331 	}
10332 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10333 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10334 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10335 
10336 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10337 	if (ret) {
10338 		dev_info(&pf->pdev->dev,
10339 			 "update vsi switch failed, err %pe aq_err %s\n",
10340 			 ERR_PTR(ret),
10341 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10342 	}
10343 }
10344 
10345 /**
10346  * i40e_config_bridge_mode - Configure the HW bridge mode
10347  * @veb: pointer to the bridge instance
10348  *
10349  * Configure the loop back mode for the LAN VSI that is downlink to the
10350  * specified HW bridge instance. It is expected this function is called
10351  * when a new HW bridge is instantiated.
10352  **/
10353 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10354 {
10355 	struct i40e_pf *pf = veb->pf;
10356 
10357 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10358 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10359 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10360 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10361 		i40e_disable_pf_switch_lb(pf);
10362 	else
10363 		i40e_enable_pf_switch_lb(pf);
10364 }
10365 
10366 /**
10367  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10368  * @veb: pointer to the VEB instance
10369  *
10370  * This is a function that builds the attached VSIs. We track the connections
10371  * through our own index numbers because the seid's from the HW could change
10372  * across the reset.
10373  **/
10374 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10375 {
10376 	struct i40e_vsi *ctl_vsi = NULL;
10377 	struct i40e_pf *pf = veb->pf;
10378 	struct i40e_vsi *vsi;
10379 	int v, ret;
10380 
10381 	/* As we do not maintain PV (port virtualizer) switch element then
10382 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10383 	 * and its control VSI is the main one.
10384 	 */
10385 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10386 		dev_err(&pf->pdev->dev,
10387 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10388 		return -ENOENT;
10389 	}
10390 
10391 	if (veb->uplink_seid == pf->mac_seid) {
10392 		/* Check that the LAN VSI has VEB owning flag set */
10393 		ctl_vsi = i40e_pf_get_main_vsi(pf);
10394 
10395 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10396 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10397 			dev_err(&pf->pdev->dev,
10398 				"Invalid control VSI for VEB %d\n", veb->idx);
10399 			return -ENOENT;
10400 		}
10401 
10402 		/* Add the control VSI to switch */
10403 		ret = i40e_add_vsi(ctl_vsi);
10404 		if (ret) {
10405 			dev_err(&pf->pdev->dev,
10406 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10407 				veb->idx, ret);
10408 			return ret;
10409 		}
10410 
10411 		i40e_vsi_reset_stats(ctl_vsi);
10412 	}
10413 
10414 	/* create the VEB in the switch and move the VSI onto the VEB */
10415 	ret = i40e_add_veb(veb, ctl_vsi);
10416 	if (ret)
10417 		return ret;
10418 
10419 	if (veb->uplink_seid) {
10420 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10421 			veb->bridge_mode = BRIDGE_MODE_VEB;
10422 		else
10423 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10424 		i40e_config_bridge_mode(veb);
10425 	}
10426 
10427 	/* create the remaining VSIs attached to this VEB */
10428 	i40e_pf_for_each_vsi(pf, v, vsi) {
10429 		if (vsi == ctl_vsi)
10430 			continue;
10431 
10432 		if (vsi->veb_idx == veb->idx) {
10433 			vsi->uplink_seid = veb->seid;
10434 			ret = i40e_add_vsi(vsi);
10435 			if (ret) {
10436 				dev_info(&pf->pdev->dev,
10437 					 "rebuild of vsi_idx %d failed: %d\n",
10438 					 v, ret);
10439 				return ret;
10440 			}
10441 			i40e_vsi_reset_stats(vsi);
10442 		}
10443 	}
10444 
10445 	return ret;
10446 }
10447 
10448 /**
10449  * i40e_get_capabilities - get info about the HW
10450  * @pf: the PF struct
10451  * @list_type: AQ capability to be queried
10452  **/
10453 static int i40e_get_capabilities(struct i40e_pf *pf,
10454 				 enum i40e_admin_queue_opc list_type)
10455 {
10456 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10457 	u16 data_size;
10458 	int buf_len;
10459 	int err;
10460 
10461 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10462 	do {
10463 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10464 		if (!cap_buf)
10465 			return -ENOMEM;
10466 
10467 		/* this loads the data into the hw struct for us */
10468 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10469 						    &data_size, list_type,
10470 						    NULL);
10471 		/* data loaded, buffer no longer needed */
10472 		kfree(cap_buf);
10473 
10474 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10475 			/* retry with a larger buffer */
10476 			buf_len = data_size;
10477 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10478 			dev_info(&pf->pdev->dev,
10479 				 "capability discovery failed, err %pe aq_err %s\n",
10480 				 ERR_PTR(err),
10481 				 i40e_aq_str(&pf->hw,
10482 					     pf->hw.aq.asq_last_status));
10483 			return -ENODEV;
10484 		}
10485 	} while (err);
10486 
10487 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10488 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10489 			dev_info(&pf->pdev->dev,
10490 				 "pf=%d, num_vfs=%d, msix_pf=%d, msix_vf=%d, fd_g=%d, fd_b=%d, pf_max_q=%d num_vsi=%d\n",
10491 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10492 				 pf->hw.func_caps.num_msix_vectors,
10493 				 pf->hw.func_caps.num_msix_vectors_vf,
10494 				 pf->hw.func_caps.fd_filters_guaranteed,
10495 				 pf->hw.func_caps.fd_filters_best_effort,
10496 				 pf->hw.func_caps.num_tx_qp,
10497 				 pf->hw.func_caps.num_vsis);
10498 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10499 			dev_info(&pf->pdev->dev,
10500 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10501 				 pf->hw.dev_caps.switch_mode,
10502 				 pf->hw.dev_caps.valid_functions);
10503 			dev_info(&pf->pdev->dev,
10504 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10505 				 pf->hw.dev_caps.sr_iov_1_1,
10506 				 pf->hw.dev_caps.num_vfs);
10507 			dev_info(&pf->pdev->dev,
10508 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10509 				 pf->hw.dev_caps.num_vsis,
10510 				 pf->hw.dev_caps.num_rx_qp,
10511 				 pf->hw.dev_caps.num_tx_qp);
10512 		}
10513 	}
10514 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10515 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10516 		       + pf->hw.func_caps.num_vfs)
10517 		if (pf->hw.revision_id == 0 &&
10518 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10519 			dev_info(&pf->pdev->dev,
10520 				 "got num_vsis %d, setting num_vsis to %d\n",
10521 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10522 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10523 		}
10524 	}
10525 	return 0;
10526 }
10527 
10528 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10529 
10530 /**
10531  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10532  * @pf: board private structure
10533  **/
10534 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10535 {
10536 	struct i40e_vsi *main_vsi, *vsi;
10537 
10538 	/* quick workaround for an NVM issue that leaves a critical register
10539 	 * uninitialized
10540 	 */
10541 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10542 		static const u32 hkey[] = {
10543 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10544 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10545 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10546 			0x95b3a76d};
10547 		int i;
10548 
10549 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10550 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10551 	}
10552 
10553 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10554 		return;
10555 
10556 	/* find existing VSI and see if it needs configuring */
10557 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10558 
10559 	/* create a new VSI if none exists */
10560 	if (!vsi) {
10561 		main_vsi = i40e_pf_get_main_vsi(pf);
10562 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
10563 		if (!vsi) {
10564 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10565 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10566 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10567 			return;
10568 		}
10569 	}
10570 
10571 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10572 }
10573 
10574 /**
10575  * i40e_fdir_teardown - release the Flow Director resources
10576  * @pf: board private structure
10577  **/
10578 static void i40e_fdir_teardown(struct i40e_pf *pf)
10579 {
10580 	struct i40e_vsi *vsi;
10581 
10582 	i40e_fdir_filter_exit(pf);
10583 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10584 	if (vsi)
10585 		i40e_vsi_release(vsi);
10586 }
10587 
10588 /**
10589  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10590  * @vsi: PF main vsi
10591  * @seid: seid of main or channel VSIs
10592  *
10593  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10594  * existed before reset
10595  **/
10596 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10597 {
10598 	struct i40e_cloud_filter *cfilter;
10599 	struct i40e_pf *pf = vsi->back;
10600 	struct hlist_node *node;
10601 	int ret;
10602 
10603 	/* Add cloud filters back if they exist */
10604 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10605 				  cloud_node) {
10606 		if (cfilter->seid != seid)
10607 			continue;
10608 
10609 		if (cfilter->dst_port)
10610 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10611 								true);
10612 		else
10613 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10614 
10615 		if (ret) {
10616 			dev_dbg(&pf->pdev->dev,
10617 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10618 				ERR_PTR(ret),
10619 				i40e_aq_str(&pf->hw,
10620 					    pf->hw.aq.asq_last_status));
10621 			return ret;
10622 		}
10623 	}
10624 	return 0;
10625 }
10626 
10627 /**
10628  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10629  * @vsi: PF main vsi
10630  *
10631  * Rebuilds channel VSIs if they existed before reset
10632  **/
10633 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10634 {
10635 	struct i40e_channel *ch, *ch_tmp;
10636 	int ret;
10637 
10638 	if (list_empty(&vsi->ch_list))
10639 		return 0;
10640 
10641 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10642 		if (!ch->initialized)
10643 			break;
10644 		/* Proceed with creation of channel (VMDq2) VSI */
10645 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10646 		if (ret) {
10647 			dev_info(&vsi->back->pdev->dev,
10648 				 "failed to rebuild channels using uplink_seid %u\n",
10649 				 vsi->uplink_seid);
10650 			return ret;
10651 		}
10652 		/* Reconfigure TX queues using QTX_CTL register */
10653 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10654 		if (ret) {
10655 			dev_info(&vsi->back->pdev->dev,
10656 				 "failed to configure TX rings for channel %u\n",
10657 				 ch->seid);
10658 			return ret;
10659 		}
10660 		/* update 'next_base_queue' */
10661 		vsi->next_base_queue = vsi->next_base_queue +
10662 							ch->num_queue_pairs;
10663 		if (ch->max_tx_rate) {
10664 			u64 credits = ch->max_tx_rate;
10665 
10666 			if (i40e_set_bw_limit(vsi, ch->seid,
10667 					      ch->max_tx_rate))
10668 				return -EINVAL;
10669 
10670 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10671 			dev_dbg(&vsi->back->pdev->dev,
10672 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10673 				ch->max_tx_rate,
10674 				credits,
10675 				ch->seid);
10676 		}
10677 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10678 		if (ret) {
10679 			dev_dbg(&vsi->back->pdev->dev,
10680 				"Failed to rebuild cloud filters for channel VSI %u\n",
10681 				ch->seid);
10682 			return ret;
10683 		}
10684 	}
10685 	return 0;
10686 }
10687 
10688 /**
10689  * i40e_clean_xps_state - clean xps state for every tx_ring
10690  * @vsi: ptr to the VSI
10691  **/
10692 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10693 {
10694 	int i;
10695 
10696 	if (vsi->tx_rings)
10697 		for (i = 0; i < vsi->num_queue_pairs; i++)
10698 			if (vsi->tx_rings[i])
10699 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10700 					  vsi->tx_rings[i]->state);
10701 }
10702 
10703 /**
10704  * i40e_prep_for_reset - prep for the core to reset
10705  * @pf: board private structure
10706  *
10707  * Close up the VFs and other things in prep for PF Reset.
10708   **/
10709 static void i40e_prep_for_reset(struct i40e_pf *pf)
10710 {
10711 	struct i40e_hw *hw = &pf->hw;
10712 	struct i40e_vsi *vsi;
10713 	int ret = 0;
10714 	u32 v;
10715 
10716 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10717 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10718 		return;
10719 	if (i40e_check_asq_alive(&pf->hw))
10720 		i40e_vc_notify_reset(pf);
10721 
10722 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10723 
10724 	/* quiesce the VSIs and their queues that are not already DOWN */
10725 	i40e_pf_quiesce_all_vsi(pf);
10726 
10727 	i40e_pf_for_each_vsi(pf, v, vsi) {
10728 		i40e_clean_xps_state(vsi);
10729 		vsi->seid = 0;
10730 	}
10731 
10732 	i40e_shutdown_adminq(&pf->hw);
10733 
10734 	/* call shutdown HMC */
10735 	if (hw->hmc.hmc_obj) {
10736 		ret = i40e_shutdown_lan_hmc(hw);
10737 		if (ret)
10738 			dev_warn(&pf->pdev->dev,
10739 				 "shutdown_lan_hmc failed: %d\n", ret);
10740 	}
10741 
10742 	/* Save the current PTP time so that we can restore the time after the
10743 	 * reset completes.
10744 	 */
10745 	i40e_ptp_save_hw_time(pf);
10746 }
10747 
10748 /**
10749  * i40e_send_version - update firmware with driver version
10750  * @pf: PF struct
10751  */
10752 static void i40e_send_version(struct i40e_pf *pf)
10753 {
10754 	struct i40e_driver_version dv;
10755 
10756 	dv.major_version = 0xff;
10757 	dv.minor_version = 0xff;
10758 	dv.build_version = 0xff;
10759 	dv.subbuild_version = 0;
10760 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10761 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10762 }
10763 
10764 /**
10765  * i40e_get_oem_version - get OEM specific version information
10766  * @hw: pointer to the hardware structure
10767  **/
10768 static void i40e_get_oem_version(struct i40e_hw *hw)
10769 {
10770 	u16 block_offset = 0xffff;
10771 	u16 block_length = 0;
10772 	u16 capabilities = 0;
10773 	u16 gen_snap = 0;
10774 	u16 release = 0;
10775 
10776 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10777 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10778 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10779 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10780 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10781 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10782 #define I40E_NVM_OEM_LENGTH			3
10783 
10784 	/* Check if pointer to OEM version block is valid. */
10785 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10786 	if (block_offset == 0xffff)
10787 		return;
10788 
10789 	/* Check if OEM version block has correct length. */
10790 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10791 			   &block_length);
10792 	if (block_length < I40E_NVM_OEM_LENGTH)
10793 		return;
10794 
10795 	/* Check if OEM version format is as expected. */
10796 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10797 			   &capabilities);
10798 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10799 		return;
10800 
10801 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10802 			   &gen_snap);
10803 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10804 			   &release);
10805 	hw->nvm.oem_ver =
10806 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10807 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10808 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10809 }
10810 
10811 /**
10812  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10813  * @pf: board private structure
10814  **/
10815 static int i40e_reset(struct i40e_pf *pf)
10816 {
10817 	struct i40e_hw *hw = &pf->hw;
10818 	int ret;
10819 
10820 	ret = i40e_pf_reset(hw);
10821 	if (ret) {
10822 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10823 		set_bit(__I40E_RESET_FAILED, pf->state);
10824 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10825 	} else {
10826 		pf->pfr_count++;
10827 	}
10828 	return ret;
10829 }
10830 
10831 /**
10832  * i40e_rebuild - rebuild using a saved config
10833  * @pf: board private structure
10834  * @reinit: if the Main VSI needs to re-initialized.
10835  * @lock_acquired: indicates whether or not the lock has been acquired
10836  * before this function was called.
10837  **/
10838 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10839 {
10840 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10841 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10842 	struct i40e_hw *hw = &pf->hw;
10843 	struct i40e_veb *veb;
10844 	int ret;
10845 	u32 val;
10846 	int v;
10847 
10848 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10849 	    is_recovery_mode_reported)
10850 		i40e_set_ethtool_ops(vsi->netdev);
10851 
10852 	if (test_bit(__I40E_DOWN, pf->state) &&
10853 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10854 		goto clear_recovery;
10855 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10856 
10857 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10858 	ret = i40e_init_adminq(&pf->hw);
10859 	if (ret) {
10860 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10861 			 ERR_PTR(ret),
10862 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10863 		goto clear_recovery;
10864 	}
10865 	i40e_get_oem_version(&pf->hw);
10866 
10867 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10868 		/* The following delay is necessary for firmware update. */
10869 		mdelay(1000);
10870 	}
10871 
10872 	/* re-verify the eeprom if we just had an EMP reset */
10873 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10874 		i40e_verify_eeprom(pf);
10875 
10876 	/* if we are going out of or into recovery mode we have to act
10877 	 * accordingly with regard to resources initialization
10878 	 * and deinitialization
10879 	 */
10880 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10881 		if (i40e_get_capabilities(pf,
10882 					  i40e_aqc_opc_list_func_capabilities))
10883 			goto end_unlock;
10884 
10885 		if (is_recovery_mode_reported) {
10886 			/* we're staying in recovery mode so we'll reinitialize
10887 			 * misc vector here
10888 			 */
10889 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10890 				goto end_unlock;
10891 		} else {
10892 			if (!lock_acquired)
10893 				rtnl_lock();
10894 			/* we're going out of recovery mode so we'll free
10895 			 * the IRQ allocated specifically for recovery mode
10896 			 * and restore the interrupt scheme
10897 			 */
10898 			free_irq(pf->pdev->irq, pf);
10899 			i40e_clear_interrupt_scheme(pf);
10900 			if (i40e_restore_interrupt_scheme(pf))
10901 				goto end_unlock;
10902 		}
10903 
10904 		/* tell the firmware that we're starting */
10905 		i40e_send_version(pf);
10906 
10907 		/* bail out in case recovery mode was detected, as there is
10908 		 * no need for further configuration.
10909 		 */
10910 		goto end_unlock;
10911 	}
10912 
10913 	i40e_clear_pxe_mode(hw);
10914 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10915 	if (ret)
10916 		goto end_core_reset;
10917 
10918 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10919 				hw->func_caps.num_rx_qp, 0, 0);
10920 	if (ret) {
10921 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10922 		goto end_core_reset;
10923 	}
10924 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10925 	if (ret) {
10926 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10927 		goto end_core_reset;
10928 	}
10929 
10930 #ifdef CONFIG_I40E_DCB
10931 	/* Enable FW to write a default DCB config on link-up
10932 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10933 	 * is not supported with new link speed
10934 	 */
10935 	if (i40e_is_tc_mqprio_enabled(pf)) {
10936 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10937 	} else {
10938 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10939 		    (hw->phy.link_info.link_speed &
10940 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10941 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10942 			dev_warn(&pf->pdev->dev,
10943 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10944 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10945 		} else {
10946 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10947 			ret = i40e_init_pf_dcb(pf);
10948 			if (ret) {
10949 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10950 					 ret);
10951 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10952 				/* Continue without DCB enabled */
10953 			}
10954 		}
10955 	}
10956 
10957 #endif /* CONFIG_I40E_DCB */
10958 	if (!lock_acquired)
10959 		rtnl_lock();
10960 	ret = i40e_setup_pf_switch(pf, reinit, true);
10961 	if (ret)
10962 		goto end_unlock;
10963 
10964 	/* The driver only wants link up/down and module qualification
10965 	 * reports from firmware.  Note the negative logic.
10966 	 */
10967 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10968 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10969 					 I40E_AQ_EVENT_MEDIA_NA |
10970 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10971 	if (ret)
10972 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10973 			 ERR_PTR(ret),
10974 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10975 
10976 	/* Rebuild the VSIs and VEBs that existed before reset.
10977 	 * They are still in our local switch element arrays, so only
10978 	 * need to rebuild the switch model in the HW.
10979 	 *
10980 	 * If there were VEBs but the reconstitution failed, we'll try
10981 	 * to recover minimal use by getting the basic PF VSI working.
10982 	 */
10983 	if (vsi->uplink_seid != pf->mac_seid) {
10984 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10985 
10986 		/* Rebuild VEBs */
10987 		i40e_pf_for_each_veb(pf, v, veb) {
10988 			ret = i40e_reconstitute_veb(veb);
10989 			if (!ret)
10990 				continue;
10991 
10992 			/* If Main VEB failed, we're in deep doodoo,
10993 			 * so give up rebuilding the switch and set up
10994 			 * for minimal rebuild of PF VSI.
10995 			 * If orphan failed, we'll report the error
10996 			 * but try to keep going.
10997 			 */
10998 			if (veb->uplink_seid == pf->mac_seid) {
10999 				dev_info(&pf->pdev->dev,
11000 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
11001 					 ret);
11002 				vsi->uplink_seid = pf->mac_seid;
11003 				break;
11004 			} else if (veb->uplink_seid == 0) {
11005 				dev_info(&pf->pdev->dev,
11006 					 "rebuild of orphan VEB failed: %d\n",
11007 					 ret);
11008 			}
11009 		}
11010 	}
11011 
11012 	if (vsi->uplink_seid == pf->mac_seid) {
11013 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11014 		/* no VEB, so rebuild only the Main VSI */
11015 		ret = i40e_add_vsi(vsi);
11016 		if (ret) {
11017 			dev_info(&pf->pdev->dev,
11018 				 "rebuild of Main VSI failed: %d\n", ret);
11019 			goto end_unlock;
11020 		}
11021 	}
11022 
11023 	if (vsi->mqprio_qopt.max_rate[0]) {
11024 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11025 						  vsi->mqprio_qopt.max_rate[0]);
11026 		u64 credits = 0;
11027 
11028 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11029 		if (ret)
11030 			goto end_unlock;
11031 
11032 		credits = max_tx_rate;
11033 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11034 		dev_dbg(&vsi->back->pdev->dev,
11035 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11036 			max_tx_rate,
11037 			credits,
11038 			vsi->seid);
11039 	}
11040 
11041 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11042 	if (ret)
11043 		goto end_unlock;
11044 
11045 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11046 	 * for this main VSI if they exist
11047 	 */
11048 	ret = i40e_rebuild_channels(vsi);
11049 	if (ret)
11050 		goto end_unlock;
11051 
11052 	/* Reconfigure hardware for allowing smaller MSS in the case
11053 	 * of TSO, so that we avoid the MDD being fired and causing
11054 	 * a reset in the case of small MSS+TSO.
11055 	 */
11056 #define I40E_REG_MSS          0x000E64DC
11057 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11058 #define I40E_64BYTE_MSS       0x400000
11059 	val = rd32(hw, I40E_REG_MSS);
11060 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11061 		val &= ~I40E_REG_MSS_MIN_MASK;
11062 		val |= I40E_64BYTE_MSS;
11063 		wr32(hw, I40E_REG_MSS, val);
11064 	}
11065 
11066 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11067 		msleep(75);
11068 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11069 		if (ret)
11070 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11071 				 ERR_PTR(ret),
11072 				 i40e_aq_str(&pf->hw,
11073 					     pf->hw.aq.asq_last_status));
11074 	}
11075 	/* reinit the misc interrupt */
11076 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11077 		ret = i40e_setup_misc_vector(pf);
11078 		if (ret)
11079 			goto end_unlock;
11080 	}
11081 
11082 	/* Add a filter to drop all Flow control frames from any VSI from being
11083 	 * transmitted. By doing so we stop a malicious VF from sending out
11084 	 * PAUSE or PFC frames and potentially controlling traffic for other
11085 	 * PF/VF VSIs.
11086 	 * The FW can still send Flow control frames if enabled.
11087 	 */
11088 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11089 						       pf->main_vsi_seid);
11090 
11091 	/* restart the VSIs that were rebuilt and running before the reset */
11092 	i40e_pf_unquiesce_all_vsi(pf);
11093 
11094 	/* Release the RTNL lock before we start resetting VFs */
11095 	if (!lock_acquired)
11096 		rtnl_unlock();
11097 
11098 	/* Restore promiscuous settings */
11099 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11100 	if (ret)
11101 		dev_warn(&pf->pdev->dev,
11102 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11103 			 pf->cur_promisc ? "on" : "off",
11104 			 ERR_PTR(ret),
11105 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11106 
11107 	i40e_reset_all_vfs(pf, true);
11108 
11109 	/* tell the firmware that we're starting */
11110 	i40e_send_version(pf);
11111 
11112 	/* We've already released the lock, so don't do it again */
11113 	goto end_core_reset;
11114 
11115 end_unlock:
11116 	if (!lock_acquired)
11117 		rtnl_unlock();
11118 end_core_reset:
11119 	clear_bit(__I40E_RESET_FAILED, pf->state);
11120 clear_recovery:
11121 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11122 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11123 }
11124 
11125 /**
11126  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11127  * @pf: board private structure
11128  * @reinit: if the Main VSI needs to re-initialized.
11129  * @lock_acquired: indicates whether or not the lock has been acquired
11130  * before this function was called.
11131  **/
11132 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11133 				   bool lock_acquired)
11134 {
11135 	int ret;
11136 
11137 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11138 		return;
11139 	/* Now we wait for GRST to settle out.
11140 	 * We don't have to delete the VEBs or VSIs from the hw switch
11141 	 * because the reset will make them disappear.
11142 	 */
11143 	ret = i40e_reset(pf);
11144 	if (!ret)
11145 		i40e_rebuild(pf, reinit, lock_acquired);
11146 	else
11147 		dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
11148 }
11149 
11150 /**
11151  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11152  * @pf: board private structure
11153  *
11154  * Close up the VFs and other things in prep for a Core Reset,
11155  * then get ready to rebuild the world.
11156  * @lock_acquired: indicates whether or not the lock has been acquired
11157  * before this function was called.
11158  **/
11159 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11160 {
11161 	i40e_prep_for_reset(pf);
11162 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11163 }
11164 
11165 /**
11166  * i40e_print_vf_mdd_event - print VF Tx/Rx malicious driver detect event
11167  * @pf: board private structure
11168  * @vf: pointer to the VF structure
11169  * @is_tx: true - for Tx event, false - for  Rx
11170  */
11171 static void i40e_print_vf_mdd_event(struct i40e_pf *pf, struct i40e_vf *vf,
11172 				    bool is_tx)
11173 {
11174 	dev_err(&pf->pdev->dev, is_tx ?
11175 		"%lld Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n" :
11176 		"%lld Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n",
11177 		is_tx ? vf->mdd_tx_events.count : vf->mdd_rx_events.count,
11178 		pf->hw.pf_id,
11179 		vf->vf_id,
11180 		vf->default_lan_addr.addr,
11181 		str_on_off(test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)));
11182 }
11183 
11184 /**
11185  * i40e_print_vfs_mdd_events - print VFs malicious driver detect event
11186  * @pf: pointer to the PF structure
11187  *
11188  * Called from i40e_handle_mdd_event to rate limit and print VFs MDD events.
11189  */
11190 static void i40e_print_vfs_mdd_events(struct i40e_pf *pf)
11191 {
11192 	unsigned int i;
11193 
11194 	/* check that there are pending MDD events to print */
11195 	if (!test_and_clear_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state))
11196 		return;
11197 
11198 	if (!__ratelimit(&pf->mdd_message_rate_limit))
11199 		return;
11200 
11201 	for (i = 0; i < pf->num_alloc_vfs; i++) {
11202 		struct i40e_vf *vf = &pf->vf[i];
11203 		bool is_printed = false;
11204 
11205 		/* only print Rx MDD event message if there are new events */
11206 		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
11207 			vf->mdd_rx_events.last_printed = vf->mdd_rx_events.count;
11208 			i40e_print_vf_mdd_event(pf, vf, false);
11209 			is_printed = true;
11210 		}
11211 
11212 		/* only print Tx MDD event message if there are new events */
11213 		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
11214 			vf->mdd_tx_events.last_printed = vf->mdd_tx_events.count;
11215 			i40e_print_vf_mdd_event(pf, vf, true);
11216 			is_printed = true;
11217 		}
11218 
11219 		if (is_printed && !test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags))
11220 			dev_info(&pf->pdev->dev,
11221 				 "Use PF Control I/F to re-enable the VF #%d\n",
11222 				 i);
11223 	}
11224 }
11225 
11226 /**
11227  * i40e_handle_mdd_event
11228  * @pf: pointer to the PF structure
11229  *
11230  * Called from the MDD irq handler to identify possibly malicious vfs
11231  **/
11232 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11233 {
11234 	struct i40e_hw *hw = &pf->hw;
11235 	bool mdd_detected = false;
11236 	struct i40e_vf *vf;
11237 	u32 reg;
11238 	int i;
11239 
11240 	if (!test_and_clear_bit(__I40E_MDD_EVENT_PENDING, pf->state)) {
11241 		/* Since the VF MDD event logging is rate limited, check if
11242 		 * there are pending MDD events.
11243 		 */
11244 		i40e_print_vfs_mdd_events(pf);
11245 		return;
11246 	}
11247 
11248 	/* find what triggered the MDD event */
11249 	reg = rd32(hw, I40E_GL_MDET_TX);
11250 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11251 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11252 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11253 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11254 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11255 				pf->hw.func_caps.base_queue;
11256 		if (netif_msg_tx_err(pf))
11257 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11258 				 event, queue, pf_num, vf_num);
11259 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11260 		mdd_detected = true;
11261 	}
11262 	reg = rd32(hw, I40E_GL_MDET_RX);
11263 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11264 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11265 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11266 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11267 				pf->hw.func_caps.base_queue;
11268 		if (netif_msg_rx_err(pf))
11269 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11270 				 event, queue, func);
11271 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11272 		mdd_detected = true;
11273 	}
11274 
11275 	if (mdd_detected) {
11276 		reg = rd32(hw, I40E_PF_MDET_TX);
11277 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11278 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11279 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11280 		}
11281 		reg = rd32(hw, I40E_PF_MDET_RX);
11282 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11283 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11284 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11285 		}
11286 	}
11287 
11288 	/* see if one of the VFs needs its hand slapped */
11289 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11290 		bool is_mdd_on_tx = false;
11291 		bool is_mdd_on_rx = false;
11292 
11293 		vf = &(pf->vf[i]);
11294 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11295 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11296 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11297 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11298 			vf->mdd_tx_events.count++;
11299 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11300 			is_mdd_on_tx = true;
11301 		}
11302 
11303 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11304 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11305 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11306 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11307 			vf->mdd_rx_events.count++;
11308 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11309 			is_mdd_on_rx = true;
11310 		}
11311 
11312 		if ((is_mdd_on_tx || is_mdd_on_rx) &&
11313 		    test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
11314 			/* VF MDD event counters will be cleared by
11315 			 * reset, so print the event prior to reset.
11316 			 */
11317 			if (is_mdd_on_rx)
11318 				i40e_print_vf_mdd_event(pf, vf, false);
11319 			if (is_mdd_on_tx)
11320 				i40e_print_vf_mdd_event(pf, vf, true);
11321 
11322 			i40e_vc_reset_vf(vf, true);
11323 		}
11324 	}
11325 
11326 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11327 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11328 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11329 	i40e_flush(hw);
11330 
11331 	i40e_print_vfs_mdd_events(pf);
11332 }
11333 
11334 /**
11335  * i40e_service_task - Run the driver's async subtasks
11336  * @work: pointer to work_struct containing our data
11337  **/
11338 static void i40e_service_task(struct work_struct *work)
11339 {
11340 	struct i40e_pf *pf = container_of(work,
11341 					  struct i40e_pf,
11342 					  service_task);
11343 	unsigned long start_time = jiffies;
11344 
11345 	/* don't bother with service tasks if a reset is in progress */
11346 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11347 	    test_bit(__I40E_SUSPENDED, pf->state))
11348 		return;
11349 
11350 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11351 		return;
11352 
11353 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11354 		i40e_detect_recover_hung(pf);
11355 		i40e_sync_filters_subtask(pf);
11356 		i40e_reset_subtask(pf);
11357 		i40e_handle_mdd_event(pf);
11358 		i40e_vc_process_vflr_event(pf);
11359 		i40e_watchdog_subtask(pf);
11360 		i40e_fdir_reinit_subtask(pf);
11361 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11362 			/* Client subtask will reopen next time through. */
11363 			i40e_notify_client_of_netdev_close(pf, true);
11364 		} else {
11365 			i40e_client_subtask(pf);
11366 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11367 					       pf->state))
11368 				i40e_notify_client_of_l2_param_changes(pf);
11369 		}
11370 		i40e_sync_filters_subtask(pf);
11371 	} else {
11372 		i40e_reset_subtask(pf);
11373 	}
11374 
11375 	i40e_clean_adminq_subtask(pf);
11376 
11377 	/* flush memory to make sure state is correct before next watchdog */
11378 	smp_mb__before_atomic();
11379 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11380 
11381 	/* If the tasks have taken longer than one timer cycle or there
11382 	 * is more work to be done, reschedule the service task now
11383 	 * rather than wait for the timer to tick again.
11384 	 */
11385 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11386 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11387 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11388 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11389 		i40e_service_event_schedule(pf);
11390 }
11391 
11392 /**
11393  * i40e_service_timer - timer callback
11394  * @t: timer list pointer
11395  **/
11396 static void i40e_service_timer(struct timer_list *t)
11397 {
11398 	struct i40e_pf *pf = timer_container_of(pf, t, service_timer);
11399 
11400 	mod_timer(&pf->service_timer,
11401 		  round_jiffies(jiffies + pf->service_timer_period));
11402 	i40e_service_event_schedule(pf);
11403 }
11404 
11405 /**
11406  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11407  * @vsi: the VSI being configured
11408  **/
11409 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11410 {
11411 	struct i40e_pf *pf = vsi->back;
11412 
11413 	switch (vsi->type) {
11414 	case I40E_VSI_MAIN:
11415 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11416 		if (!vsi->num_tx_desc)
11417 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11418 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11419 		if (!vsi->num_rx_desc)
11420 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11421 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11422 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11423 			vsi->num_q_vectors = pf->num_lan_msix;
11424 		else
11425 			vsi->num_q_vectors = 1;
11426 
11427 		break;
11428 
11429 	case I40E_VSI_FDIR:
11430 		vsi->alloc_queue_pairs = 1;
11431 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11432 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11433 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11434 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11435 		vsi->num_q_vectors = pf->num_fdsb_msix;
11436 		break;
11437 
11438 	case I40E_VSI_VMDQ2:
11439 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11440 		if (!vsi->num_tx_desc)
11441 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11442 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11443 		if (!vsi->num_rx_desc)
11444 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11445 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11446 		vsi->num_q_vectors = pf->num_vmdq_msix;
11447 		break;
11448 
11449 	case I40E_VSI_SRIOV:
11450 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11451 		if (!vsi->num_tx_desc)
11452 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11453 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11454 		if (!vsi->num_rx_desc)
11455 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11456 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11457 		break;
11458 
11459 	default:
11460 		WARN_ON(1);
11461 		return -ENODATA;
11462 	}
11463 
11464 	if (is_kdump_kernel()) {
11465 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11466 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11467 	}
11468 
11469 	return 0;
11470 }
11471 
11472 /**
11473  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11474  * @vsi: VSI pointer
11475  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11476  *
11477  * On error: returns error code (negative)
11478  * On success: returns 0
11479  **/
11480 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11481 {
11482 	struct i40e_ring **next_rings;
11483 	int size;
11484 	int ret = 0;
11485 
11486 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11487 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11488 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11489 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11490 	if (!vsi->tx_rings)
11491 		return -ENOMEM;
11492 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11493 	if (i40e_enabled_xdp_vsi(vsi)) {
11494 		vsi->xdp_rings = next_rings;
11495 		next_rings += vsi->alloc_queue_pairs;
11496 	}
11497 	vsi->rx_rings = next_rings;
11498 
11499 	if (alloc_qvectors) {
11500 		/* allocate memory for q_vector pointers */
11501 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11502 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11503 		if (!vsi->q_vectors) {
11504 			ret = -ENOMEM;
11505 			goto err_vectors;
11506 		}
11507 	}
11508 	return ret;
11509 
11510 err_vectors:
11511 	kfree(vsi->tx_rings);
11512 	return ret;
11513 }
11514 
11515 /**
11516  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11517  * @pf: board private structure
11518  * @type: type of VSI
11519  *
11520  * On error: returns error code (negative)
11521  * On success: returns vsi index in PF (positive)
11522  **/
11523 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11524 {
11525 	int ret = -ENODEV;
11526 	struct i40e_vsi *vsi;
11527 	int vsi_idx;
11528 	int i;
11529 
11530 	/* Need to protect the allocation of the VSIs at the PF level */
11531 	mutex_lock(&pf->switch_mutex);
11532 
11533 	/* VSI list may be fragmented if VSI creation/destruction has
11534 	 * been happening.  We can afford to do a quick scan to look
11535 	 * for any free VSIs in the list.
11536 	 *
11537 	 * find next empty vsi slot, looping back around if necessary
11538 	 */
11539 	i = pf->next_vsi;
11540 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11541 		i++;
11542 	if (i >= pf->num_alloc_vsi) {
11543 		i = 0;
11544 		while (i < pf->next_vsi && pf->vsi[i])
11545 			i++;
11546 	}
11547 
11548 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11549 		vsi_idx = i;             /* Found one! */
11550 	} else {
11551 		ret = -ENODEV;
11552 		goto unlock_pf;  /* out of VSI slots! */
11553 	}
11554 	pf->next_vsi = ++i;
11555 
11556 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11557 	if (!vsi) {
11558 		ret = -ENOMEM;
11559 		goto unlock_pf;
11560 	}
11561 	vsi->type = type;
11562 	vsi->back = pf;
11563 	set_bit(__I40E_VSI_DOWN, vsi->state);
11564 	vsi->flags = 0;
11565 	vsi->idx = vsi_idx;
11566 	vsi->int_rate_limit = 0;
11567 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11568 				pf->rss_table_size : 64;
11569 	vsi->netdev_registered = false;
11570 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11571 	hash_init(vsi->mac_filter_hash);
11572 	vsi->irqs_ready = false;
11573 
11574 	if (type == I40E_VSI_MAIN) {
11575 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11576 		if (!vsi->af_xdp_zc_qps)
11577 			goto err_rings;
11578 	}
11579 
11580 	ret = i40e_set_num_rings_in_vsi(vsi);
11581 	if (ret)
11582 		goto err_rings;
11583 
11584 	ret = i40e_vsi_alloc_arrays(vsi, true);
11585 	if (ret)
11586 		goto err_rings;
11587 
11588 	/* Setup default MSIX irq handler for VSI */
11589 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11590 
11591 	/* Initialize VSI lock */
11592 	spin_lock_init(&vsi->mac_filter_hash_lock);
11593 	pf->vsi[vsi_idx] = vsi;
11594 	ret = vsi_idx;
11595 	goto unlock_pf;
11596 
11597 err_rings:
11598 	bitmap_free(vsi->af_xdp_zc_qps);
11599 	pf->next_vsi = i - 1;
11600 	kfree(vsi);
11601 unlock_pf:
11602 	mutex_unlock(&pf->switch_mutex);
11603 	return ret;
11604 }
11605 
11606 /**
11607  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11608  * @vsi: VSI pointer
11609  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11610  *
11611  * On error: returns error code (negative)
11612  * On success: returns 0
11613  **/
11614 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11615 {
11616 	/* free the ring and vector containers */
11617 	if (free_qvectors) {
11618 		kfree(vsi->q_vectors);
11619 		vsi->q_vectors = NULL;
11620 	}
11621 	kfree(vsi->tx_rings);
11622 	vsi->tx_rings = NULL;
11623 	vsi->rx_rings = NULL;
11624 	vsi->xdp_rings = NULL;
11625 }
11626 
11627 /**
11628  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11629  * and lookup table
11630  * @vsi: Pointer to VSI structure
11631  */
11632 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11633 {
11634 	if (!vsi)
11635 		return;
11636 
11637 	kfree(vsi->rss_hkey_user);
11638 	vsi->rss_hkey_user = NULL;
11639 
11640 	kfree(vsi->rss_lut_user);
11641 	vsi->rss_lut_user = NULL;
11642 }
11643 
11644 /**
11645  * i40e_vsi_clear - Deallocate the VSI provided
11646  * @vsi: the VSI being un-configured
11647  **/
11648 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11649 {
11650 	struct i40e_pf *pf;
11651 
11652 	if (!vsi)
11653 		return 0;
11654 
11655 	if (!vsi->back)
11656 		goto free_vsi;
11657 	pf = vsi->back;
11658 
11659 	mutex_lock(&pf->switch_mutex);
11660 	if (!pf->vsi[vsi->idx]) {
11661 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11662 			vsi->idx, vsi->idx, vsi->type);
11663 		goto unlock_vsi;
11664 	}
11665 
11666 	if (pf->vsi[vsi->idx] != vsi) {
11667 		dev_err(&pf->pdev->dev,
11668 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11669 			pf->vsi[vsi->idx]->idx,
11670 			pf->vsi[vsi->idx]->type,
11671 			vsi->idx, vsi->type);
11672 		goto unlock_vsi;
11673 	}
11674 
11675 	/* updates the PF for this cleared vsi */
11676 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11677 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11678 
11679 	bitmap_free(vsi->af_xdp_zc_qps);
11680 	i40e_vsi_free_arrays(vsi, true);
11681 	i40e_clear_rss_config_user(vsi);
11682 
11683 	pf->vsi[vsi->idx] = NULL;
11684 	if (vsi->idx < pf->next_vsi)
11685 		pf->next_vsi = vsi->idx;
11686 
11687 unlock_vsi:
11688 	mutex_unlock(&pf->switch_mutex);
11689 free_vsi:
11690 	kfree(vsi);
11691 
11692 	return 0;
11693 }
11694 
11695 /**
11696  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11697  * @vsi: the VSI being cleaned
11698  **/
11699 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11700 {
11701 	int i;
11702 
11703 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11704 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11705 			kfree_rcu(vsi->tx_rings[i], rcu);
11706 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11707 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11708 			if (vsi->xdp_rings)
11709 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11710 		}
11711 	}
11712 }
11713 
11714 /**
11715  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11716  * @vsi: the VSI being configured
11717  **/
11718 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11719 {
11720 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11721 	struct i40e_pf *pf = vsi->back;
11722 	struct i40e_ring *ring;
11723 
11724 	/* Set basic values in the rings to be used later during open() */
11725 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11726 		/* allocate space for both Tx and Rx in one shot */
11727 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11728 		if (!ring)
11729 			goto err_out;
11730 
11731 		ring->queue_index = i;
11732 		ring->reg_idx = vsi->base_queue + i;
11733 		ring->ring_active = false;
11734 		ring->vsi = vsi;
11735 		ring->netdev = vsi->netdev;
11736 		ring->dev = &pf->pdev->dev;
11737 		ring->count = vsi->num_tx_desc;
11738 		ring->size = 0;
11739 		ring->dcb_tc = 0;
11740 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11741 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11742 		ring->itr_setting = pf->tx_itr_default;
11743 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11744 
11745 		if (!i40e_enabled_xdp_vsi(vsi))
11746 			goto setup_rx;
11747 
11748 		ring->queue_index = vsi->alloc_queue_pairs + i;
11749 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11750 		ring->ring_active = false;
11751 		ring->vsi = vsi;
11752 		ring->netdev = NULL;
11753 		ring->dev = &pf->pdev->dev;
11754 		ring->count = vsi->num_tx_desc;
11755 		ring->size = 0;
11756 		ring->dcb_tc = 0;
11757 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11758 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11759 		set_ring_xdp(ring);
11760 		ring->itr_setting = pf->tx_itr_default;
11761 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11762 
11763 setup_rx:
11764 		ring->queue_index = i;
11765 		ring->reg_idx = vsi->base_queue + i;
11766 		ring->ring_active = false;
11767 		ring->vsi = vsi;
11768 		ring->netdev = vsi->netdev;
11769 		ring->dev = &pf->pdev->dev;
11770 		ring->count = vsi->num_rx_desc;
11771 		ring->size = 0;
11772 		ring->dcb_tc = 0;
11773 		ring->itr_setting = pf->rx_itr_default;
11774 		WRITE_ONCE(vsi->rx_rings[i], ring);
11775 	}
11776 
11777 	return 0;
11778 
11779 err_out:
11780 	i40e_vsi_clear_rings(vsi);
11781 	return -ENOMEM;
11782 }
11783 
11784 /**
11785  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11786  * @pf: board private structure
11787  * @vectors: the number of MSI-X vectors to request
11788  *
11789  * Returns the number of vectors reserved, or error
11790  **/
11791 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11792 {
11793 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11794 					I40E_MIN_MSIX, vectors);
11795 	if (vectors < 0) {
11796 		dev_info(&pf->pdev->dev,
11797 			 "MSI-X vector reservation failed: %d\n", vectors);
11798 		vectors = 0;
11799 	}
11800 
11801 	return vectors;
11802 }
11803 
11804 /**
11805  * i40e_init_msix - Setup the MSIX capability
11806  * @pf: board private structure
11807  *
11808  * Work with the OS to set up the MSIX vectors needed.
11809  *
11810  * Returns the number of vectors reserved or negative on failure
11811  **/
11812 static int i40e_init_msix(struct i40e_pf *pf)
11813 {
11814 	struct i40e_hw *hw = &pf->hw;
11815 	int cpus, extra_vectors;
11816 	int vectors_left;
11817 	int v_budget, i;
11818 	int v_actual;
11819 	int iwarp_requested = 0;
11820 
11821 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11822 		return -ENODEV;
11823 
11824 	/* The number of vectors we'll request will be comprised of:
11825 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11826 	 *   - The number of LAN queue pairs
11827 	 *	- Queues being used for RSS.
11828 	 *		We don't need as many as max_rss_size vectors.
11829 	 *		use rss_size instead in the calculation since that
11830 	 *		is governed by number of cpus in the system.
11831 	 *	- assumes symmetric Tx/Rx pairing
11832 	 *   - The number of VMDq pairs
11833 	 *   - The CPU count within the NUMA node if iWARP is enabled
11834 	 * Once we count this up, try the request.
11835 	 *
11836 	 * If we can't get what we want, we'll simplify to nearly nothing
11837 	 * and try again.  If that still fails, we punt.
11838 	 */
11839 	vectors_left = hw->func_caps.num_msix_vectors;
11840 	v_budget = 0;
11841 
11842 	/* reserve one vector for miscellaneous handler */
11843 	if (vectors_left) {
11844 		v_budget++;
11845 		vectors_left--;
11846 	}
11847 
11848 	/* reserve some vectors for the main PF traffic queues. Initially we
11849 	 * only reserve at most 50% of the available vectors, in the case that
11850 	 * the number of online CPUs is large. This ensures that we can enable
11851 	 * extra features as well. Once we've enabled the other features, we
11852 	 * will use any remaining vectors to reach as close as we can to the
11853 	 * number of online CPUs.
11854 	 */
11855 	cpus = num_online_cpus();
11856 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11857 	vectors_left -= pf->num_lan_msix;
11858 
11859 	/* reserve one vector for sideband flow director */
11860 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11861 		if (vectors_left) {
11862 			pf->num_fdsb_msix = 1;
11863 			v_budget++;
11864 			vectors_left--;
11865 		} else {
11866 			pf->num_fdsb_msix = 0;
11867 		}
11868 	}
11869 
11870 	/* can we reserve enough for iWARP? */
11871 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11872 		iwarp_requested = pf->num_iwarp_msix;
11873 
11874 		if (!vectors_left)
11875 			pf->num_iwarp_msix = 0;
11876 		else if (vectors_left < pf->num_iwarp_msix)
11877 			pf->num_iwarp_msix = 1;
11878 		v_budget += pf->num_iwarp_msix;
11879 		vectors_left -= pf->num_iwarp_msix;
11880 	}
11881 
11882 	/* any vectors left over go for VMDq support */
11883 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11884 		if (!vectors_left) {
11885 			pf->num_vmdq_msix = 0;
11886 			pf->num_vmdq_qps = 0;
11887 		} else {
11888 			int vmdq_vecs_wanted =
11889 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11890 			int vmdq_vecs =
11891 				min_t(int, vectors_left, vmdq_vecs_wanted);
11892 
11893 			/* if we're short on vectors for what's desired, we limit
11894 			 * the queues per vmdq.  If this is still more than are
11895 			 * available, the user will need to change the number of
11896 			 * queues/vectors used by the PF later with the ethtool
11897 			 * channels command
11898 			 */
11899 			if (vectors_left < vmdq_vecs_wanted) {
11900 				pf->num_vmdq_qps = 1;
11901 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11902 				vmdq_vecs = min_t(int,
11903 						  vectors_left,
11904 						  vmdq_vecs_wanted);
11905 			}
11906 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11907 
11908 			v_budget += vmdq_vecs;
11909 			vectors_left -= vmdq_vecs;
11910 		}
11911 	}
11912 
11913 	/* On systems with a large number of SMP cores, we previously limited
11914 	 * the number of vectors for num_lan_msix to be at most 50% of the
11915 	 * available vectors, to allow for other features. Now, we add back
11916 	 * the remaining vectors. However, we ensure that the total
11917 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11918 	 * calculate the number of vectors we can add without going over the
11919 	 * cap of CPUs. For systems with a small number of CPUs this will be
11920 	 * zero.
11921 	 */
11922 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11923 	pf->num_lan_msix += extra_vectors;
11924 	vectors_left -= extra_vectors;
11925 
11926 	WARN(vectors_left < 0,
11927 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11928 
11929 	v_budget += pf->num_lan_msix;
11930 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11931 				   GFP_KERNEL);
11932 	if (!pf->msix_entries)
11933 		return -ENOMEM;
11934 
11935 	for (i = 0; i < v_budget; i++)
11936 		pf->msix_entries[i].entry = i;
11937 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11938 
11939 	if (v_actual < I40E_MIN_MSIX) {
11940 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11941 		kfree(pf->msix_entries);
11942 		pf->msix_entries = NULL;
11943 		pci_disable_msix(pf->pdev);
11944 		return -ENODEV;
11945 
11946 	} else if (v_actual == I40E_MIN_MSIX) {
11947 		/* Adjust for minimal MSIX use */
11948 		pf->num_vmdq_vsis = 0;
11949 		pf->num_vmdq_qps = 0;
11950 		pf->num_lan_qps = 1;
11951 		pf->num_lan_msix = 1;
11952 
11953 	} else if (v_actual != v_budget) {
11954 		/* If we have limited resources, we will start with no vectors
11955 		 * for the special features and then allocate vectors to some
11956 		 * of these features based on the policy and at the end disable
11957 		 * the features that did not get any vectors.
11958 		 */
11959 		int vec;
11960 
11961 		dev_info(&pf->pdev->dev,
11962 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11963 			 v_actual, v_budget);
11964 		/* reserve the misc vector */
11965 		vec = v_actual - 1;
11966 
11967 		/* Scale vector usage down */
11968 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11969 		pf->num_vmdq_vsis = 1;
11970 		pf->num_vmdq_qps = 1;
11971 
11972 		/* partition out the remaining vectors */
11973 		switch (vec) {
11974 		case 2:
11975 			pf->num_lan_msix = 1;
11976 			break;
11977 		case 3:
11978 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11979 				pf->num_lan_msix = 1;
11980 				pf->num_iwarp_msix = 1;
11981 			} else {
11982 				pf->num_lan_msix = 2;
11983 			}
11984 			break;
11985 		default:
11986 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11987 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11988 						 iwarp_requested);
11989 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11990 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11991 			} else {
11992 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11993 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11994 			}
11995 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11996 				pf->num_fdsb_msix = 1;
11997 				vec--;
11998 			}
11999 			pf->num_lan_msix = min_t(int,
12000 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
12001 							      pf->num_lan_msix);
12002 			pf->num_lan_qps = pf->num_lan_msix;
12003 			break;
12004 		}
12005 	}
12006 
12007 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
12008 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
12009 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12010 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12011 	}
12012 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
12013 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
12014 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12015 	}
12016 
12017 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
12018 	    pf->num_iwarp_msix == 0) {
12019 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
12020 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12021 	}
12022 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
12023 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
12024 		   pf->num_lan_msix,
12025 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
12026 		   pf->num_fdsb_msix,
12027 		   pf->num_iwarp_msix);
12028 
12029 	return v_actual;
12030 }
12031 
12032 /**
12033  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12034  * @vsi: the VSI being configured
12035  * @v_idx: index of the vector in the vsi struct
12036  *
12037  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12038  **/
12039 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12040 {
12041 	struct i40e_q_vector *q_vector;
12042 
12043 	/* allocate q_vector */
12044 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
12045 	if (!q_vector)
12046 		return -ENOMEM;
12047 
12048 	q_vector->vsi = vsi;
12049 	q_vector->v_idx = v_idx;
12050 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12051 
12052 	if (vsi->netdev)
12053 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12054 
12055 	/* tie q_vector and vsi together */
12056 	vsi->q_vectors[v_idx] = q_vector;
12057 
12058 	return 0;
12059 }
12060 
12061 /**
12062  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12063  * @vsi: the VSI being configured
12064  *
12065  * We allocate one q_vector per queue interrupt.  If allocation fails we
12066  * return -ENOMEM.
12067  **/
12068 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12069 {
12070 	struct i40e_pf *pf = vsi->back;
12071 	int err, v_idx, num_q_vectors;
12072 
12073 	/* if not MSIX, give the one vector only to the LAN VSI */
12074 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12075 		num_q_vectors = vsi->num_q_vectors;
12076 	else if (vsi->type == I40E_VSI_MAIN)
12077 		num_q_vectors = 1;
12078 	else
12079 		return -EINVAL;
12080 
12081 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12082 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12083 		if (err)
12084 			goto err_out;
12085 	}
12086 
12087 	return 0;
12088 
12089 err_out:
12090 	while (v_idx--)
12091 		i40e_free_q_vector(vsi, v_idx);
12092 
12093 	return err;
12094 }
12095 
12096 /**
12097  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12098  * @pf: board private structure to initialize
12099  **/
12100 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12101 {
12102 	int vectors = 0;
12103 	ssize_t size;
12104 
12105 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12106 		vectors = i40e_init_msix(pf);
12107 		if (vectors < 0) {
12108 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12109 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12110 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12111 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12112 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12113 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12114 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12115 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12116 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12117 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12118 
12119 			/* rework the queue expectations without MSIX */
12120 			i40e_determine_queue_usage(pf);
12121 		}
12122 	}
12123 
12124 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12125 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12126 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12127 		vectors = pci_enable_msi(pf->pdev);
12128 		if (vectors < 0) {
12129 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12130 				 vectors);
12131 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12132 		}
12133 		vectors = 1;  /* one MSI or Legacy vector */
12134 	}
12135 
12136 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12137 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12138 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12139 
12140 	/* set up vector assignment tracking */
12141 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12142 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12143 	if (!pf->irq_pile)
12144 		return -ENOMEM;
12145 
12146 	pf->irq_pile->num_entries = vectors;
12147 
12148 	/* track first vector for misc interrupts, ignore return */
12149 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12150 
12151 	return 0;
12152 }
12153 
12154 /**
12155  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12156  * @pf: private board data structure
12157  *
12158  * Restore the interrupt scheme that was cleared when we suspended the
12159  * device. This should be called during resume to re-allocate the q_vectors
12160  * and reacquire IRQs.
12161  */
12162 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12163 {
12164 	struct i40e_vsi *vsi;
12165 	int err, i;
12166 
12167 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12168 	 * scheme. We need to re-enabled them here in order to attempt to
12169 	 * re-acquire the MSI or MSI-X vectors
12170 	 */
12171 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12172 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12173 
12174 	err = i40e_init_interrupt_scheme(pf);
12175 	if (err)
12176 		return err;
12177 
12178 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12179 	 * rings together again.
12180 	 */
12181 	i40e_pf_for_each_vsi(pf, i, vsi) {
12182 		err = i40e_vsi_alloc_q_vectors(vsi);
12183 		if (err)
12184 			goto err_unwind;
12185 
12186 		i40e_vsi_map_rings_to_vectors(vsi);
12187 	}
12188 
12189 	err = i40e_setup_misc_vector(pf);
12190 	if (err)
12191 		goto err_unwind;
12192 
12193 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12194 		i40e_client_update_msix_info(pf);
12195 
12196 	return 0;
12197 
12198 err_unwind:
12199 	while (i--) {
12200 		if (pf->vsi[i])
12201 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12202 	}
12203 
12204 	return err;
12205 }
12206 
12207 /**
12208  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12209  * non queue events in recovery mode
12210  * @pf: board private structure
12211  *
12212  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12213  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12214  * This is handled differently than in recovery mode since no Tx/Rx resources
12215  * are being allocated.
12216  **/
12217 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12218 {
12219 	int err;
12220 
12221 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12222 		err = i40e_setup_misc_vector(pf);
12223 
12224 		if (err) {
12225 			dev_info(&pf->pdev->dev,
12226 				 "MSI-X misc vector request failed, error %d\n",
12227 				 err);
12228 			return err;
12229 		}
12230 	} else {
12231 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12232 
12233 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12234 				  pf->int_name, pf);
12235 
12236 		if (err) {
12237 			dev_info(&pf->pdev->dev,
12238 				 "MSI/legacy misc vector request failed, error %d\n",
12239 				 err);
12240 			return err;
12241 		}
12242 		i40e_enable_misc_int_causes(pf);
12243 		i40e_irq_dynamic_enable_icr0(pf);
12244 	}
12245 
12246 	return 0;
12247 }
12248 
12249 /**
12250  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12251  * @pf: board private structure
12252  *
12253  * This sets up the handler for MSIX 0, which is used to manage the
12254  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12255  * when in MSI or Legacy interrupt mode.
12256  **/
12257 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12258 {
12259 	struct i40e_hw *hw = &pf->hw;
12260 	int err = 0;
12261 
12262 	/* Only request the IRQ once, the first time through. */
12263 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12264 		err = request_irq(pf->msix_entries[0].vector,
12265 				  i40e_intr, 0, pf->int_name, pf);
12266 		if (err) {
12267 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12268 			dev_info(&pf->pdev->dev,
12269 				 "request_irq for %s failed: %d\n",
12270 				 pf->int_name, err);
12271 			return -EFAULT;
12272 		}
12273 	}
12274 
12275 	i40e_enable_misc_int_causes(pf);
12276 
12277 	/* associate no queues to the misc vector */
12278 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12279 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12280 
12281 	i40e_flush(hw);
12282 
12283 	i40e_irq_dynamic_enable_icr0(pf);
12284 
12285 	return err;
12286 }
12287 
12288 /**
12289  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12290  * @vsi: Pointer to vsi structure
12291  * @seed: Buffter to store the hash keys
12292  * @lut: Buffer to store the lookup table entries
12293  * @lut_size: Size of buffer to store the lookup table entries
12294  *
12295  * Return 0 on success, negative on failure
12296  */
12297 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12298 			   u8 *lut, u16 lut_size)
12299 {
12300 	struct i40e_pf *pf = vsi->back;
12301 	struct i40e_hw *hw = &pf->hw;
12302 	int ret = 0;
12303 
12304 	if (seed) {
12305 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12306 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12307 		if (ret) {
12308 			dev_info(&pf->pdev->dev,
12309 				 "Cannot get RSS key, err %pe aq_err %s\n",
12310 				 ERR_PTR(ret),
12311 				 i40e_aq_str(&pf->hw,
12312 					     pf->hw.aq.asq_last_status));
12313 			return ret;
12314 		}
12315 	}
12316 
12317 	if (lut) {
12318 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12319 
12320 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12321 		if (ret) {
12322 			dev_info(&pf->pdev->dev,
12323 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12324 				 ERR_PTR(ret),
12325 				 i40e_aq_str(&pf->hw,
12326 					     pf->hw.aq.asq_last_status));
12327 			return ret;
12328 		}
12329 	}
12330 
12331 	return ret;
12332 }
12333 
12334 /**
12335  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12336  * @vsi: Pointer to vsi structure
12337  * @seed: RSS hash seed
12338  * @lut: Lookup table
12339  * @lut_size: Lookup table size
12340  *
12341  * Returns 0 on success, negative on failure
12342  **/
12343 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12344 			       const u8 *lut, u16 lut_size)
12345 {
12346 	struct i40e_pf *pf = vsi->back;
12347 	struct i40e_hw *hw = &pf->hw;
12348 	u16 vf_id = vsi->vf_id;
12349 	u8 i;
12350 
12351 	/* Fill out hash function seed */
12352 	if (seed) {
12353 		u32 *seed_dw = (u32 *)seed;
12354 
12355 		if (vsi->type == I40E_VSI_MAIN) {
12356 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12357 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12358 		} else if (vsi->type == I40E_VSI_SRIOV) {
12359 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12360 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12361 		} else {
12362 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12363 		}
12364 	}
12365 
12366 	if (lut) {
12367 		u32 *lut_dw = (u32 *)lut;
12368 
12369 		if (vsi->type == I40E_VSI_MAIN) {
12370 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12371 				return -EINVAL;
12372 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12373 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12374 		} else if (vsi->type == I40E_VSI_SRIOV) {
12375 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12376 				return -EINVAL;
12377 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12378 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12379 		} else {
12380 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12381 		}
12382 	}
12383 	i40e_flush(hw);
12384 
12385 	return 0;
12386 }
12387 
12388 /**
12389  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12390  * @vsi: Pointer to VSI structure
12391  * @seed: Buffer to store the keys
12392  * @lut: Buffer to store the lookup table entries
12393  * @lut_size: Size of buffer to store the lookup table entries
12394  *
12395  * Returns 0 on success, negative on failure
12396  */
12397 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12398 			    u8 *lut, u16 lut_size)
12399 {
12400 	struct i40e_pf *pf = vsi->back;
12401 	struct i40e_hw *hw = &pf->hw;
12402 	u16 i;
12403 
12404 	if (seed) {
12405 		u32 *seed_dw = (u32 *)seed;
12406 
12407 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12408 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12409 	}
12410 	if (lut) {
12411 		u32 *lut_dw = (u32 *)lut;
12412 
12413 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12414 			return -EINVAL;
12415 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12416 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12417 	}
12418 
12419 	return 0;
12420 }
12421 
12422 /**
12423  * i40e_config_rss - Configure RSS keys and lut
12424  * @vsi: Pointer to VSI structure
12425  * @seed: RSS hash seed
12426  * @lut: Lookup table
12427  * @lut_size: Lookup table size
12428  *
12429  * Returns 0 on success, negative on failure
12430  */
12431 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12432 {
12433 	struct i40e_pf *pf = vsi->back;
12434 
12435 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12436 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12437 	else
12438 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12439 }
12440 
12441 /**
12442  * i40e_get_rss - Get RSS keys and lut
12443  * @vsi: Pointer to VSI structure
12444  * @seed: Buffer to store the keys
12445  * @lut: Buffer to store the lookup table entries
12446  * @lut_size: Size of buffer to store the lookup table entries
12447  *
12448  * Returns 0 on success, negative on failure
12449  */
12450 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12451 {
12452 	struct i40e_pf *pf = vsi->back;
12453 
12454 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12455 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12456 	else
12457 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12458 }
12459 
12460 /**
12461  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12462  * @pf: Pointer to board private structure
12463  * @lut: Lookup table
12464  * @rss_table_size: Lookup table size
12465  * @rss_size: Range of queue number for hashing
12466  */
12467 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12468 		       u16 rss_table_size, u16 rss_size)
12469 {
12470 	u16 i;
12471 
12472 	for (i = 0; i < rss_table_size; i++)
12473 		lut[i] = i % rss_size;
12474 }
12475 
12476 /**
12477  * i40e_pf_config_rss - Prepare for RSS if used
12478  * @pf: board private structure
12479  **/
12480 static int i40e_pf_config_rss(struct i40e_pf *pf)
12481 {
12482 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12483 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12484 	u8 *lut;
12485 	struct i40e_hw *hw = &pf->hw;
12486 	u32 reg_val;
12487 	u64 hena;
12488 	int ret;
12489 
12490 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12491 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12492 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12493 	hena |= i40e_pf_get_default_rss_hashcfg(pf);
12494 
12495 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12496 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12497 
12498 	/* Determine the RSS table size based on the hardware capabilities */
12499 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12500 	reg_val = (pf->rss_table_size == 512) ?
12501 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12502 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12503 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12504 
12505 	/* Determine the RSS size of the VSI */
12506 	if (!vsi->rss_size) {
12507 		u16 qcount;
12508 		/* If the firmware does something weird during VSI init, we
12509 		 * could end up with zero TCs. Check for that to avoid
12510 		 * divide-by-zero. It probably won't pass traffic, but it also
12511 		 * won't panic.
12512 		 */
12513 		qcount = vsi->num_queue_pairs /
12514 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12515 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12516 	}
12517 	if (!vsi->rss_size)
12518 		return -EINVAL;
12519 
12520 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12521 	if (!lut)
12522 		return -ENOMEM;
12523 
12524 	/* Use user configured lut if there is one, otherwise use default */
12525 	if (vsi->rss_lut_user)
12526 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12527 	else
12528 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12529 
12530 	/* Use user configured hash key if there is one, otherwise
12531 	 * use default.
12532 	 */
12533 	if (vsi->rss_hkey_user)
12534 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12535 	else
12536 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12537 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12538 	kfree(lut);
12539 
12540 	return ret;
12541 }
12542 
12543 /**
12544  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12545  * @pf: board private structure
12546  * @queue_count: the requested queue count for rss.
12547  *
12548  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12549  * count which may be different from the requested queue count.
12550  * Note: expects to be called while under rtnl_lock()
12551  **/
12552 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12553 {
12554 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12555 	int new_rss_size;
12556 
12557 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12558 		return 0;
12559 
12560 	queue_count = min_t(int, queue_count, num_online_cpus());
12561 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12562 
12563 	if (queue_count != vsi->num_queue_pairs) {
12564 		u16 qcount;
12565 
12566 		vsi->req_queue_pairs = queue_count;
12567 		i40e_prep_for_reset(pf);
12568 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12569 			return pf->alloc_rss_size;
12570 
12571 		pf->alloc_rss_size = new_rss_size;
12572 
12573 		i40e_reset_and_rebuild(pf, true, true);
12574 
12575 		/* Discard the user configured hash keys and lut, if less
12576 		 * queues are enabled.
12577 		 */
12578 		if (queue_count < vsi->rss_size) {
12579 			i40e_clear_rss_config_user(vsi);
12580 			dev_dbg(&pf->pdev->dev,
12581 				"discard user configured hash keys and lut\n");
12582 		}
12583 
12584 		/* Reset vsi->rss_size, as number of enabled queues changed */
12585 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12586 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12587 
12588 		i40e_pf_config_rss(pf);
12589 	}
12590 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12591 		 vsi->req_queue_pairs, pf->rss_size_max);
12592 	return pf->alloc_rss_size;
12593 }
12594 
12595 /**
12596  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12597  * @pf: board private structure
12598  **/
12599 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12600 {
12601 	bool min_valid, max_valid;
12602 	u32 max_bw, min_bw;
12603 	int status;
12604 
12605 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12606 					   &min_valid, &max_valid);
12607 
12608 	if (!status) {
12609 		if (min_valid)
12610 			pf->min_bw = min_bw;
12611 		if (max_valid)
12612 			pf->max_bw = max_bw;
12613 	}
12614 
12615 	return status;
12616 }
12617 
12618 /**
12619  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12620  * @pf: board private structure
12621  **/
12622 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12623 {
12624 	struct i40e_aqc_configure_partition_bw_data bw_data;
12625 	int status;
12626 
12627 	memset(&bw_data, 0, sizeof(bw_data));
12628 
12629 	/* Set the valid bit for this PF */
12630 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12631 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12632 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12633 
12634 	/* Set the new bandwidths */
12635 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12636 
12637 	return status;
12638 }
12639 
12640 /**
12641  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12642  * if total port shutdown feature is enabled for this PF
12643  * @pf: board private structure
12644  **/
12645 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12646 {
12647 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12648 #define I40E_FEATURES_ENABLE_PTR		0x2A
12649 #define I40E_CURRENT_SETTING_PTR		0x2B
12650 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12651 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12652 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12653 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12654 	u16 sr_emp_sr_settings_ptr = 0;
12655 	u16 features_enable = 0;
12656 	u16 link_behavior = 0;
12657 	int read_status = 0;
12658 	bool ret = false;
12659 
12660 	read_status = i40e_read_nvm_word(&pf->hw,
12661 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12662 					 &sr_emp_sr_settings_ptr);
12663 	if (read_status)
12664 		goto err_nvm;
12665 	read_status = i40e_read_nvm_word(&pf->hw,
12666 					 sr_emp_sr_settings_ptr +
12667 					 I40E_FEATURES_ENABLE_PTR,
12668 					 &features_enable);
12669 	if (read_status)
12670 		goto err_nvm;
12671 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12672 		read_status = i40e_read_nvm_module_data(&pf->hw,
12673 							I40E_SR_EMP_SR_SETTINGS_PTR,
12674 							I40E_CURRENT_SETTING_PTR,
12675 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12676 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12677 							&link_behavior);
12678 		if (read_status)
12679 			goto err_nvm;
12680 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12681 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12682 	}
12683 	return ret;
12684 
12685 err_nvm:
12686 	dev_warn(&pf->pdev->dev,
12687 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12688 		 ERR_PTR(read_status));
12689 	return ret;
12690 }
12691 
12692 /**
12693  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12694  * @pf: board private structure to initialize
12695  *
12696  * i40e_sw_init initializes the Adapter private data structure.
12697  * Fields are initialized based on PCI device information and
12698  * OS network device settings (MTU size).
12699  **/
12700 static int i40e_sw_init(struct i40e_pf *pf)
12701 {
12702 	int err = 0;
12703 	int size;
12704 	u16 pow;
12705 
12706 	/* Set default capability flags */
12707 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12708 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12709 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12710 
12711 	/* Set default ITR */
12712 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12713 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12714 
12715 	/* Depending on PF configurations, it is possible that the RSS
12716 	 * maximum might end up larger than the available queues
12717 	 */
12718 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12719 	pf->alloc_rss_size = 1;
12720 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12721 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12722 				 pf->hw.func_caps.num_tx_qp);
12723 
12724 	/* find the next higher power-of-2 of num cpus */
12725 	pow = roundup_pow_of_two(num_online_cpus());
12726 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12727 
12728 	if (pf->hw.func_caps.rss) {
12729 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12730 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12731 					   num_online_cpus());
12732 	}
12733 
12734 	/* MFP mode enabled */
12735 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12736 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12737 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12738 		if (i40e_get_partition_bw_setting(pf)) {
12739 			dev_warn(&pf->pdev->dev,
12740 				 "Could not get partition bw settings\n");
12741 		} else {
12742 			dev_info(&pf->pdev->dev,
12743 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12744 				 pf->min_bw, pf->max_bw);
12745 
12746 			/* nudge the Tx scheduler */
12747 			i40e_set_partition_bw_setting(pf);
12748 		}
12749 	}
12750 
12751 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12752 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12753 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12754 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12755 		    pf->hw.num_partitions > 1)
12756 			dev_info(&pf->pdev->dev,
12757 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12758 		else
12759 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12760 		pf->fdir_pf_filter_count =
12761 				 pf->hw.func_caps.fd_filters_guaranteed;
12762 		pf->hw.fdir_shared_filter_count =
12763 				 pf->hw.func_caps.fd_filters_best_effort;
12764 	}
12765 
12766 	/* Enable HW ATR eviction if possible */
12767 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12768 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12769 
12770 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12771 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12772 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12773 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12774 	}
12775 
12776 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12777 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12778 		/* IWARP needs one extra vector for CQP just like MISC.*/
12779 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12780 	}
12781 	/* Stopping FW LLDP engine is supported on XL710 and X722
12782 	 * starting from FW versions determined in i40e_init_adminq.
12783 	 * Stopping the FW LLDP engine is not supported on XL710
12784 	 * if NPAR is functioning so unset this hw flag in this case.
12785 	 */
12786 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12787 	    pf->hw.func_caps.npar_enable)
12788 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12789 
12790 #ifdef CONFIG_PCI_IOV
12791 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12792 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12793 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12794 		pf->num_req_vfs = min_t(int,
12795 					pf->hw.func_caps.num_vfs,
12796 					I40E_MAX_VF_COUNT);
12797 	}
12798 #endif /* CONFIG_PCI_IOV */
12799 	pf->lan_veb = I40E_NO_VEB;
12800 	pf->lan_vsi = I40E_NO_VSI;
12801 
12802 	/* By default FW has this off for performance reasons */
12803 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12804 
12805 	/* set up queue assignment tracking */
12806 	size = sizeof(struct i40e_lump_tracking)
12807 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12808 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12809 	if (!pf->qp_pile) {
12810 		err = -ENOMEM;
12811 		goto sw_init_done;
12812 	}
12813 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12814 
12815 	pf->tx_timeout_recovery_level = 1;
12816 
12817 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12818 	    i40e_is_total_port_shutdown_enabled(pf)) {
12819 		/* Link down on close must be on when total port shutdown
12820 		 * is enabled for a given port
12821 		 */
12822 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12823 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12824 		dev_info(&pf->pdev->dev,
12825 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12826 	}
12827 	mutex_init(&pf->switch_mutex);
12828 
12829 sw_init_done:
12830 	return err;
12831 }
12832 
12833 /**
12834  * i40e_set_ntuple - set the ntuple feature flag and take action
12835  * @pf: board private structure to initialize
12836  * @features: the feature set that the stack is suggesting
12837  *
12838  * returns a bool to indicate if reset needs to happen
12839  **/
12840 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12841 {
12842 	bool need_reset = false;
12843 
12844 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12845 	 * the state changed, we need to reset.
12846 	 */
12847 	if (features & NETIF_F_NTUPLE) {
12848 		/* Enable filters and mark for reset */
12849 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12850 			need_reset = true;
12851 		/* enable FD_SB only if there is MSI-X vector and no cloud
12852 		 * filters exist
12853 		 */
12854 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12855 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12856 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12857 		}
12858 	} else {
12859 		/* turn off filters, mark for reset and clear SW filter list */
12860 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12861 			need_reset = true;
12862 			i40e_fdir_filter_exit(pf);
12863 		}
12864 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12865 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12866 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12867 
12868 		/* reset fd counters */
12869 		pf->fd_add_err = 0;
12870 		pf->fd_atr_cnt = 0;
12871 		/* if ATR was auto disabled it can be re-enabled. */
12872 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12873 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12874 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12875 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12876 	}
12877 	return need_reset;
12878 }
12879 
12880 /**
12881  * i40e_clear_rss_lut - clear the rx hash lookup table
12882  * @vsi: the VSI being configured
12883  **/
12884 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12885 {
12886 	struct i40e_pf *pf = vsi->back;
12887 	struct i40e_hw *hw = &pf->hw;
12888 	u16 vf_id = vsi->vf_id;
12889 	u8 i;
12890 
12891 	if (vsi->type == I40E_VSI_MAIN) {
12892 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12893 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12894 	} else if (vsi->type == I40E_VSI_SRIOV) {
12895 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12896 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12897 	} else {
12898 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12899 	}
12900 }
12901 
12902 /**
12903  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12904  * @vsi: ptr to VSI
12905  * @ena: flag to indicate the on/off setting
12906  */
12907 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12908 {
12909 	bool if_running = netif_running(vsi->netdev) &&
12910 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12911 	int ret;
12912 
12913 	if (if_running)
12914 		i40e_down(vsi);
12915 
12916 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12917 	if (ret)
12918 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12919 	if (if_running)
12920 		i40e_up(vsi);
12921 
12922 	return ret;
12923 }
12924 
12925 /**
12926  * i40e_set_features - set the netdev feature flags
12927  * @netdev: ptr to the netdev being adjusted
12928  * @features: the feature set that the stack is suggesting
12929  * Note: expects to be called while under rtnl_lock()
12930  **/
12931 static int i40e_set_features(struct net_device *netdev,
12932 			     netdev_features_t features)
12933 {
12934 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12935 	struct i40e_vsi *vsi = np->vsi;
12936 	struct i40e_pf *pf = vsi->back;
12937 	bool need_reset;
12938 
12939 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12940 		i40e_pf_config_rss(pf);
12941 	else if (!(features & NETIF_F_RXHASH) &&
12942 		 netdev->features & NETIF_F_RXHASH)
12943 		i40e_clear_rss_lut(vsi);
12944 
12945 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12946 		i40e_vlan_stripping_enable(vsi);
12947 	else
12948 		i40e_vlan_stripping_disable(vsi);
12949 
12950 	if (!(features & NETIF_F_HW_TC) &&
12951 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12952 		dev_err(&pf->pdev->dev,
12953 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12954 		return -EINVAL;
12955 	}
12956 
12957 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12958 		i40e_del_all_macvlans(vsi);
12959 
12960 	need_reset = i40e_set_ntuple(pf, features);
12961 
12962 	if (need_reset)
12963 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12964 
12965 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12966 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12967 
12968 	return 0;
12969 }
12970 
12971 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12972 				    unsigned int table, unsigned int idx,
12973 				    struct udp_tunnel_info *ti)
12974 {
12975 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12976 	struct i40e_hw *hw = &np->vsi->back->hw;
12977 	u8 type, filter_index;
12978 	int ret;
12979 
12980 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12981 						   I40E_AQC_TUNNEL_TYPE_NGE;
12982 
12983 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12984 				     NULL);
12985 	if (ret) {
12986 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
12987 			    ERR_PTR(ret),
12988 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12989 		return -EIO;
12990 	}
12991 
12992 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12993 	return 0;
12994 }
12995 
12996 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12997 				      unsigned int table, unsigned int idx,
12998 				      struct udp_tunnel_info *ti)
12999 {
13000 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13001 	struct i40e_hw *hw = &np->vsi->back->hw;
13002 	int ret;
13003 
13004 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13005 	if (ret) {
13006 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13007 			    ERR_PTR(ret),
13008 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13009 		return -EIO;
13010 	}
13011 
13012 	return 0;
13013 }
13014 
13015 static int i40e_get_phys_port_id(struct net_device *netdev,
13016 				 struct netdev_phys_item_id *ppid)
13017 {
13018 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13019 	struct i40e_pf *pf = np->vsi->back;
13020 	struct i40e_hw *hw = &pf->hw;
13021 
13022 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13023 		return -EOPNOTSUPP;
13024 
13025 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13026 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13027 
13028 	return 0;
13029 }
13030 
13031 /**
13032  * i40e_ndo_fdb_add - add an entry to the hardware database
13033  * @ndm: the input from the stack
13034  * @tb: pointer to array of nladdr (unused)
13035  * @dev: the net device pointer
13036  * @addr: the MAC address entry being added
13037  * @vid: VLAN ID
13038  * @flags: instructions from stack about fdb operation
13039  * @notified: whether notification was emitted
13040  * @extack: netlink extended ack, unused currently
13041  */
13042 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13043 			    struct net_device *dev,
13044 			    const unsigned char *addr, u16 vid,
13045 			    u16 flags, bool *notified,
13046 			    struct netlink_ext_ack *extack)
13047 {
13048 	struct i40e_netdev_priv *np = netdev_priv(dev);
13049 	struct i40e_pf *pf = np->vsi->back;
13050 	int err = 0;
13051 
13052 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13053 		return -EOPNOTSUPP;
13054 
13055 	if (vid) {
13056 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13057 		return -EINVAL;
13058 	}
13059 
13060 	/* Hardware does not support aging addresses so if a
13061 	 * ndm_state is given only allow permanent addresses
13062 	 */
13063 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13064 		netdev_info(dev, "FDB only supports static addresses\n");
13065 		return -EINVAL;
13066 	}
13067 
13068 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13069 		err = dev_uc_add_excl(dev, addr);
13070 	else if (is_multicast_ether_addr(addr))
13071 		err = dev_mc_add_excl(dev, addr);
13072 	else
13073 		err = -EINVAL;
13074 
13075 	/* Only return duplicate errors if NLM_F_EXCL is set */
13076 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13077 		err = 0;
13078 
13079 	return err;
13080 }
13081 
13082 /**
13083  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13084  * @dev: the netdev being configured
13085  * @nlh: RTNL message
13086  * @flags: bridge flags
13087  * @extack: netlink extended ack
13088  *
13089  * Inserts a new hardware bridge if not already created and
13090  * enables the bridging mode requested (VEB or VEPA). If the
13091  * hardware bridge has already been inserted and the request
13092  * is to change the mode then that requires a PF reset to
13093  * allow rebuild of the components with required hardware
13094  * bridge mode enabled.
13095  *
13096  * Note: expects to be called while under rtnl_lock()
13097  **/
13098 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13099 				   struct nlmsghdr *nlh,
13100 				   u16 flags,
13101 				   struct netlink_ext_ack *extack)
13102 {
13103 	struct i40e_netdev_priv *np = netdev_priv(dev);
13104 	struct i40e_vsi *vsi = np->vsi;
13105 	struct i40e_pf *pf = vsi->back;
13106 	struct nlattr *attr, *br_spec;
13107 	struct i40e_veb *veb;
13108 	int rem;
13109 
13110 	/* Only for PF VSI for now */
13111 	if (vsi->type != I40E_VSI_MAIN)
13112 		return -EOPNOTSUPP;
13113 
13114 	/* Find the HW bridge for PF VSI */
13115 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13116 
13117 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13118 	if (!br_spec)
13119 		return -EINVAL;
13120 
13121 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
13122 		__u16 mode = nla_get_u16(attr);
13123 
13124 		if ((mode != BRIDGE_MODE_VEPA) &&
13125 		    (mode != BRIDGE_MODE_VEB))
13126 			return -EINVAL;
13127 
13128 		/* Insert a new HW bridge */
13129 		if (!veb) {
13130 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
13131 					     vsi->tc_config.enabled_tc);
13132 			if (veb) {
13133 				veb->bridge_mode = mode;
13134 				i40e_config_bridge_mode(veb);
13135 			} else {
13136 				/* No Bridge HW offload available */
13137 				return -ENOENT;
13138 			}
13139 			break;
13140 		} else if (mode != veb->bridge_mode) {
13141 			/* Existing HW bridge but different mode needs reset */
13142 			veb->bridge_mode = mode;
13143 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13144 			if (mode == BRIDGE_MODE_VEB)
13145 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13146 			else
13147 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13148 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13149 			break;
13150 		}
13151 	}
13152 
13153 	return 0;
13154 }
13155 
13156 /**
13157  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13158  * @skb: skb buff
13159  * @pid: process id
13160  * @seq: RTNL message seq #
13161  * @dev: the netdev being configured
13162  * @filter_mask: unused
13163  * @nlflags: netlink flags passed in
13164  *
13165  * Return the mode in which the hardware bridge is operating in
13166  * i.e VEB or VEPA.
13167  **/
13168 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13169 				   struct net_device *dev,
13170 				   u32 __always_unused filter_mask,
13171 				   int nlflags)
13172 {
13173 	struct i40e_netdev_priv *np = netdev_priv(dev);
13174 	struct i40e_vsi *vsi = np->vsi;
13175 	struct i40e_pf *pf = vsi->back;
13176 	struct i40e_veb *veb;
13177 
13178 	/* Only for PF VSI for now */
13179 	if (vsi->type != I40E_VSI_MAIN)
13180 		return -EOPNOTSUPP;
13181 
13182 	/* Find the HW bridge for the PF VSI */
13183 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13184 	if (!veb)
13185 		return 0;
13186 
13187 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13188 				       0, 0, nlflags, filter_mask, NULL);
13189 }
13190 
13191 /**
13192  * i40e_features_check - Validate encapsulated packet conforms to limits
13193  * @skb: skb buff
13194  * @dev: This physical port's netdev
13195  * @features: Offload features that the stack believes apply
13196  **/
13197 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13198 					     struct net_device *dev,
13199 					     netdev_features_t features)
13200 {
13201 	size_t len;
13202 
13203 	/* No point in doing any of this if neither checksum nor GSO are
13204 	 * being requested for this frame.  We can rule out both by just
13205 	 * checking for CHECKSUM_PARTIAL
13206 	 */
13207 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13208 		return features;
13209 
13210 	/* We cannot support GSO if the MSS is going to be less than
13211 	 * 64 bytes.  If it is then we need to drop support for GSO.
13212 	 */
13213 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13214 		features &= ~NETIF_F_GSO_MASK;
13215 
13216 	/* MACLEN can support at most 63 words */
13217 	len = skb_network_offset(skb);
13218 	if (len & ~(63 * 2))
13219 		goto out_err;
13220 
13221 	/* IPLEN and EIPLEN can support at most 127 dwords */
13222 	len = skb_network_header_len(skb);
13223 	if (len & ~(127 * 4))
13224 		goto out_err;
13225 
13226 	if (skb->encapsulation) {
13227 		/* L4TUNLEN can support 127 words */
13228 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13229 		if (len & ~(127 * 2))
13230 			goto out_err;
13231 
13232 		/* IPLEN can support at most 127 dwords */
13233 		len = skb_inner_transport_header(skb) -
13234 		      skb_inner_network_header(skb);
13235 		if (len & ~(127 * 4))
13236 			goto out_err;
13237 	}
13238 
13239 	/* No need to validate L4LEN as TCP is the only protocol with a
13240 	 * flexible value and we support all possible values supported
13241 	 * by TCP, which is at most 15 dwords
13242 	 */
13243 
13244 	return features;
13245 out_err:
13246 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13247 }
13248 
13249 /**
13250  * i40e_xdp_setup - add/remove an XDP program
13251  * @vsi: VSI to changed
13252  * @prog: XDP program
13253  * @extack: netlink extended ack
13254  **/
13255 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13256 			  struct netlink_ext_ack *extack)
13257 {
13258 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13259 	struct i40e_pf *pf = vsi->back;
13260 	struct bpf_prog *old_prog;
13261 	bool need_reset;
13262 	int i;
13263 
13264 	/* VSI shall be deleted in a moment, block loading new programs */
13265 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13266 		return -EINVAL;
13267 
13268 	/* Don't allow frames that span over multiple buffers */
13269 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13270 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13271 		return -EINVAL;
13272 	}
13273 
13274 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13275 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13276 	if (need_reset)
13277 		i40e_prep_for_reset(pf);
13278 
13279 	old_prog = xchg(&vsi->xdp_prog, prog);
13280 
13281 	if (need_reset) {
13282 		if (!prog) {
13283 			xdp_features_clear_redirect_target(vsi->netdev);
13284 			/* Wait until ndo_xsk_wakeup completes. */
13285 			synchronize_rcu();
13286 		}
13287 		i40e_reset_and_rebuild(pf, true, true);
13288 	}
13289 
13290 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13291 		if (i40e_realloc_rx_bi_zc(vsi, true))
13292 			return -ENOMEM;
13293 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13294 		if (i40e_realloc_rx_bi_zc(vsi, false))
13295 			return -ENOMEM;
13296 	}
13297 
13298 	for (i = 0; i < vsi->num_queue_pairs; i++)
13299 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13300 
13301 	if (old_prog)
13302 		bpf_prog_put(old_prog);
13303 
13304 	/* Kick start the NAPI context if there is an AF_XDP socket open
13305 	 * on that queue id. This so that receiving will start.
13306 	 */
13307 	if (need_reset && prog) {
13308 		for (i = 0; i < vsi->num_queue_pairs; i++)
13309 			if (vsi->xdp_rings[i]->xsk_pool)
13310 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13311 						      XDP_WAKEUP_RX);
13312 		xdp_features_set_redirect_target(vsi->netdev, true);
13313 	}
13314 
13315 	return 0;
13316 }
13317 
13318 /**
13319  * i40e_enter_busy_conf - Enters busy config state
13320  * @vsi: vsi
13321  *
13322  * Returns 0 on success, <0 for failure.
13323  **/
13324 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13325 {
13326 	struct i40e_pf *pf = vsi->back;
13327 	int timeout = 50;
13328 
13329 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13330 		timeout--;
13331 		if (!timeout)
13332 			return -EBUSY;
13333 		usleep_range(1000, 2000);
13334 	}
13335 
13336 	return 0;
13337 }
13338 
13339 /**
13340  * i40e_exit_busy_conf - Exits busy config state
13341  * @vsi: vsi
13342  **/
13343 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13344 {
13345 	struct i40e_pf *pf = vsi->back;
13346 
13347 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13348 }
13349 
13350 /**
13351  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13352  * @vsi: vsi
13353  * @queue_pair: queue pair
13354  **/
13355 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13356 {
13357 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13358 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13359 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13360 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13361 	if (i40e_enabled_xdp_vsi(vsi)) {
13362 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13363 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13364 	}
13365 }
13366 
13367 /**
13368  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13369  * @vsi: vsi
13370  * @queue_pair: queue pair
13371  **/
13372 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13373 {
13374 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13375 	if (i40e_enabled_xdp_vsi(vsi)) {
13376 		/* Make sure that in-progress ndo_xdp_xmit calls are
13377 		 * completed.
13378 		 */
13379 		synchronize_rcu();
13380 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13381 	}
13382 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13383 }
13384 
13385 /**
13386  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13387  * @vsi: vsi
13388  * @queue_pair: queue pair
13389  * @enable: true for enable, false for disable
13390  **/
13391 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13392 					bool enable)
13393 {
13394 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13395 	struct i40e_q_vector *q_vector = rxr->q_vector;
13396 
13397 	if (!vsi->netdev)
13398 		return;
13399 
13400 	/* All rings in a qp belong to the same qvector. */
13401 	if (q_vector->rx.ring || q_vector->tx.ring) {
13402 		if (enable)
13403 			napi_enable(&q_vector->napi);
13404 		else
13405 			napi_disable(&q_vector->napi);
13406 	}
13407 }
13408 
13409 /**
13410  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13411  * @vsi: vsi
13412  * @queue_pair: queue pair
13413  * @enable: true for enable, false for disable
13414  *
13415  * Returns 0 on success, <0 on failure.
13416  **/
13417 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13418 					bool enable)
13419 {
13420 	struct i40e_pf *pf = vsi->back;
13421 	int pf_q, ret = 0;
13422 
13423 	pf_q = vsi->base_queue + queue_pair;
13424 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13425 				     false /*is xdp*/, enable);
13426 	if (ret) {
13427 		dev_info(&pf->pdev->dev,
13428 			 "VSI seid %d Tx ring %d %sable timeout\n",
13429 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13430 		return ret;
13431 	}
13432 
13433 	i40e_control_rx_q(pf, pf_q, enable);
13434 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13435 	if (ret) {
13436 		dev_info(&pf->pdev->dev,
13437 			 "VSI seid %d Rx ring %d %sable timeout\n",
13438 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13439 		return ret;
13440 	}
13441 
13442 	/* Due to HW errata, on Rx disable only, the register can
13443 	 * indicate done before it really is. Needs 50ms to be sure
13444 	 */
13445 	if (!enable)
13446 		mdelay(50);
13447 
13448 	if (!i40e_enabled_xdp_vsi(vsi))
13449 		return ret;
13450 
13451 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13452 				     pf_q + vsi->alloc_queue_pairs,
13453 				     true /*is xdp*/, enable);
13454 	if (ret) {
13455 		dev_info(&pf->pdev->dev,
13456 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13457 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13458 	}
13459 
13460 	return ret;
13461 }
13462 
13463 /**
13464  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13465  * @vsi: vsi
13466  * @queue_pair: queue_pair
13467  **/
13468 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13469 {
13470 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13471 	struct i40e_pf *pf = vsi->back;
13472 	struct i40e_hw *hw = &pf->hw;
13473 
13474 	/* All rings in a qp belong to the same qvector. */
13475 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13476 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13477 	else
13478 		i40e_irq_dynamic_enable_icr0(pf);
13479 
13480 	i40e_flush(hw);
13481 }
13482 
13483 /**
13484  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13485  * @vsi: vsi
13486  * @queue_pair: queue_pair
13487  **/
13488 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13489 {
13490 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13491 	struct i40e_pf *pf = vsi->back;
13492 	struct i40e_hw *hw = &pf->hw;
13493 
13494 	/* For simplicity, instead of removing the qp interrupt causes
13495 	 * from the interrupt linked list, we simply disable the interrupt, and
13496 	 * leave the list intact.
13497 	 *
13498 	 * All rings in a qp belong to the same qvector.
13499 	 */
13500 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13501 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13502 
13503 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13504 		i40e_flush(hw);
13505 		synchronize_irq(pf->msix_entries[intpf].vector);
13506 	} else {
13507 		/* Legacy and MSI mode - this stops all interrupt handling */
13508 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13509 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13510 		i40e_flush(hw);
13511 		synchronize_irq(pf->pdev->irq);
13512 	}
13513 }
13514 
13515 /**
13516  * i40e_queue_pair_disable - Disables a queue pair
13517  * @vsi: vsi
13518  * @queue_pair: queue pair
13519  *
13520  * Returns 0 on success, <0 on failure.
13521  **/
13522 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13523 {
13524 	int err;
13525 
13526 	err = i40e_enter_busy_conf(vsi);
13527 	if (err)
13528 		return err;
13529 
13530 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13531 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13532 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13533 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13534 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13535 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13536 
13537 	return err;
13538 }
13539 
13540 /**
13541  * i40e_queue_pair_enable - Enables a queue pair
13542  * @vsi: vsi
13543  * @queue_pair: queue pair
13544  *
13545  * Returns 0 on success, <0 on failure.
13546  **/
13547 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13548 {
13549 	int err;
13550 
13551 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13552 	if (err)
13553 		return err;
13554 
13555 	if (i40e_enabled_xdp_vsi(vsi)) {
13556 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13557 		if (err)
13558 			return err;
13559 	}
13560 
13561 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13562 	if (err)
13563 		return err;
13564 
13565 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13566 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13567 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13568 
13569 	i40e_exit_busy_conf(vsi);
13570 
13571 	return err;
13572 }
13573 
13574 /**
13575  * i40e_xdp - implements ndo_bpf for i40e
13576  * @dev: netdevice
13577  * @xdp: XDP command
13578  **/
13579 static int i40e_xdp(struct net_device *dev,
13580 		    struct netdev_bpf *xdp)
13581 {
13582 	struct i40e_netdev_priv *np = netdev_priv(dev);
13583 	struct i40e_vsi *vsi = np->vsi;
13584 
13585 	if (vsi->type != I40E_VSI_MAIN)
13586 		return -EINVAL;
13587 
13588 	switch (xdp->command) {
13589 	case XDP_SETUP_PROG:
13590 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13591 	case XDP_SETUP_XSK_POOL:
13592 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13593 					   xdp->xsk.queue_id);
13594 	default:
13595 		return -EINVAL;
13596 	}
13597 }
13598 
13599 static const struct net_device_ops i40e_netdev_ops = {
13600 	.ndo_open		= i40e_open,
13601 	.ndo_stop		= i40e_close,
13602 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13603 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13604 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13605 	.ndo_validate_addr	= eth_validate_addr,
13606 	.ndo_set_mac_address	= i40e_set_mac,
13607 	.ndo_change_mtu		= i40e_change_mtu,
13608 	.ndo_tx_timeout		= i40e_tx_timeout,
13609 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13610 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13611 #ifdef CONFIG_NET_POLL_CONTROLLER
13612 	.ndo_poll_controller	= i40e_netpoll,
13613 #endif
13614 	.ndo_setup_tc		= __i40e_setup_tc,
13615 	.ndo_select_queue	= i40e_lan_select_queue,
13616 	.ndo_set_features	= i40e_set_features,
13617 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13618 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13619 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13620 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13621 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13622 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13623 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13624 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13625 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13626 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13627 	.ndo_features_check	= i40e_features_check,
13628 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13629 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13630 	.ndo_bpf		= i40e_xdp,
13631 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13632 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13633 	.ndo_dfwd_add_station	= i40e_fwd_add,
13634 	.ndo_dfwd_del_station	= i40e_fwd_del,
13635 	.ndo_hwtstamp_get	= i40e_ptp_hwtstamp_get,
13636 	.ndo_hwtstamp_set	= i40e_ptp_hwtstamp_set,
13637 };
13638 
13639 /**
13640  * i40e_config_netdev - Setup the netdev flags
13641  * @vsi: the VSI being configured
13642  *
13643  * Returns 0 on success, negative value on failure
13644  **/
13645 static int i40e_config_netdev(struct i40e_vsi *vsi)
13646 {
13647 	struct i40e_pf *pf = vsi->back;
13648 	struct i40e_hw *hw = &pf->hw;
13649 	struct i40e_netdev_priv *np;
13650 	struct net_device *netdev;
13651 	u8 broadcast[ETH_ALEN];
13652 	u8 mac_addr[ETH_ALEN];
13653 	int etherdev_size;
13654 	netdev_features_t hw_enc_features;
13655 	netdev_features_t hw_features;
13656 
13657 	etherdev_size = sizeof(struct i40e_netdev_priv);
13658 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13659 	if (!netdev)
13660 		return -ENOMEM;
13661 
13662 	vsi->netdev = netdev;
13663 	np = netdev_priv(netdev);
13664 	np->vsi = vsi;
13665 
13666 	hw_enc_features = NETIF_F_SG			|
13667 			  NETIF_F_HW_CSUM		|
13668 			  NETIF_F_HIGHDMA		|
13669 			  NETIF_F_SOFT_FEATURES		|
13670 			  NETIF_F_TSO			|
13671 			  NETIF_F_TSO_ECN		|
13672 			  NETIF_F_TSO6			|
13673 			  NETIF_F_GSO_GRE		|
13674 			  NETIF_F_GSO_GRE_CSUM		|
13675 			  NETIF_F_GSO_PARTIAL		|
13676 			  NETIF_F_GSO_IPXIP4		|
13677 			  NETIF_F_GSO_IPXIP6		|
13678 			  NETIF_F_GSO_UDP_TUNNEL	|
13679 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13680 			  NETIF_F_GSO_UDP_L4		|
13681 			  NETIF_F_SCTP_CRC		|
13682 			  NETIF_F_RXHASH		|
13683 			  NETIF_F_RXCSUM		|
13684 			  0;
13685 
13686 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13687 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13688 
13689 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13690 
13691 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13692 
13693 	netdev->hw_enc_features |= hw_enc_features;
13694 
13695 	/* record features VLANs can make use of */
13696 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13697 
13698 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13699 				   NETIF_F_GSO_GRE_CSUM |	\
13700 				   NETIF_F_GSO_IPXIP4 |		\
13701 				   NETIF_F_GSO_IPXIP6 |		\
13702 				   NETIF_F_GSO_UDP_TUNNEL |	\
13703 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13704 
13705 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13706 	netdev->features |= NETIF_F_GSO_PARTIAL |
13707 			    I40E_GSO_PARTIAL_FEATURES;
13708 
13709 	netdev->mpls_features |= NETIF_F_SG;
13710 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13711 	netdev->mpls_features |= NETIF_F_TSO;
13712 	netdev->mpls_features |= NETIF_F_TSO6;
13713 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13714 
13715 	/* enable macvlan offloads */
13716 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13717 
13718 	hw_features = hw_enc_features		|
13719 		      NETIF_F_HW_VLAN_CTAG_TX	|
13720 		      NETIF_F_HW_VLAN_CTAG_RX;
13721 
13722 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13723 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13724 
13725 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13726 
13727 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13728 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13729 
13730 	netdev->features &= ~NETIF_F_HW_TC;
13731 
13732 	if (vsi->type == I40E_VSI_MAIN) {
13733 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13734 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13735 		/* The following steps are necessary for two reasons. First,
13736 		 * some older NVM configurations load a default MAC-VLAN
13737 		 * filter that will accept any tagged packet, and we want to
13738 		 * replace this with a normal filter. Additionally, it is
13739 		 * possible our MAC address was provided by the platform using
13740 		 * Open Firmware or similar.
13741 		 *
13742 		 * Thus, we need to remove the default filter and install one
13743 		 * specific to the MAC address.
13744 		 */
13745 		i40e_rm_default_mac_filter(vsi, mac_addr);
13746 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13747 		i40e_add_mac_filter(vsi, mac_addr);
13748 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13749 
13750 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13751 				       NETDEV_XDP_ACT_REDIRECT |
13752 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13753 				       NETDEV_XDP_ACT_RX_SG;
13754 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13755 	} else {
13756 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13757 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13758 		 * the end, which is 4 bytes long, so force truncation of the
13759 		 * original name by IFNAMSIZ - 4
13760 		 */
13761 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
13762 
13763 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
13764 			 main_vsi->netdev->name);
13765 		eth_random_addr(mac_addr);
13766 
13767 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13768 		i40e_add_mac_filter(vsi, mac_addr);
13769 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13770 	}
13771 
13772 	/* Add the broadcast filter so that we initially will receive
13773 	 * broadcast packets. Note that when a new VLAN is first added the
13774 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13775 	 * specific filters as part of transitioning into "vlan" operation.
13776 	 * When more VLANs are added, the driver will copy each existing MAC
13777 	 * filter and add it for the new VLAN.
13778 	 *
13779 	 * Broadcast filters are handled specially by
13780 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13781 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13782 	 * filter. The subtask will update the correct broadcast promiscuous
13783 	 * bits as VLANs become active or inactive.
13784 	 */
13785 	eth_broadcast_addr(broadcast);
13786 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13787 	i40e_add_mac_filter(vsi, broadcast);
13788 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13789 
13790 	eth_hw_addr_set(netdev, mac_addr);
13791 	ether_addr_copy(netdev->perm_addr, mac_addr);
13792 
13793 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13794 	netdev->neigh_priv_len = sizeof(u32) * 4;
13795 
13796 	netdev->priv_flags |= IFF_UNICAST_FLT;
13797 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13798 	/* Setup netdev TC information */
13799 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13800 
13801 	netdev->netdev_ops = &i40e_netdev_ops;
13802 	netdev->watchdog_timeo = 5 * HZ;
13803 	i40e_set_ethtool_ops(netdev);
13804 
13805 	/* MTU range: 68 - 9706 */
13806 	netdev->min_mtu = ETH_MIN_MTU;
13807 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13808 
13809 	return 0;
13810 }
13811 
13812 /**
13813  * i40e_vsi_delete - Delete a VSI from the switch
13814  * @vsi: the VSI being removed
13815  *
13816  * Returns 0 on success, negative value on failure
13817  **/
13818 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13819 {
13820 	/* remove default VSI is not allowed */
13821 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13822 		return;
13823 
13824 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13825 }
13826 
13827 /**
13828  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13829  * @vsi: the VSI being queried
13830  *
13831  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13832  **/
13833 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13834 {
13835 	struct i40e_veb *veb;
13836 	struct i40e_pf *pf = vsi->back;
13837 
13838 	/* Uplink is not a bridge so default to VEB */
13839 	if (vsi->veb_idx >= I40E_MAX_VEB)
13840 		return 1;
13841 
13842 	veb = pf->veb[vsi->veb_idx];
13843 	if (!veb) {
13844 		dev_info(&pf->pdev->dev,
13845 			 "There is no veb associated with the bridge\n");
13846 		return -ENOENT;
13847 	}
13848 
13849 	/* Uplink is a bridge in VEPA mode */
13850 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13851 		return 0;
13852 	} else {
13853 		/* Uplink is a bridge in VEB mode */
13854 		return 1;
13855 	}
13856 
13857 	/* VEPA is now default bridge, so return 0 */
13858 	return 0;
13859 }
13860 
13861 /**
13862  * i40e_add_vsi - Add a VSI to the switch
13863  * @vsi: the VSI being configured
13864  *
13865  * This initializes a VSI context depending on the VSI type to be added and
13866  * passes it down to the add_vsi aq command.
13867  **/
13868 static int i40e_add_vsi(struct i40e_vsi *vsi)
13869 {
13870 	int ret = -ENODEV;
13871 	struct i40e_pf *pf = vsi->back;
13872 	struct i40e_hw *hw = &pf->hw;
13873 	struct i40e_vsi_context ctxt;
13874 	struct i40e_mac_filter *f;
13875 	struct hlist_node *h;
13876 	int bkt;
13877 
13878 	u8 enabled_tc = 0x1; /* TC0 enabled */
13879 	int f_count = 0;
13880 
13881 	memset(&ctxt, 0, sizeof(ctxt));
13882 	switch (vsi->type) {
13883 	case I40E_VSI_MAIN:
13884 		/* The PF's main VSI is already setup as part of the
13885 		 * device initialization, so we'll not bother with
13886 		 * the add_vsi call, but we will retrieve the current
13887 		 * VSI context.
13888 		 */
13889 		ctxt.seid = pf->main_vsi_seid;
13890 		ctxt.pf_num = pf->hw.pf_id;
13891 		ctxt.vf_num = 0;
13892 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13893 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13894 		if (ret) {
13895 			dev_info(&pf->pdev->dev,
13896 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13897 				 ERR_PTR(ret),
13898 				 i40e_aq_str(&pf->hw,
13899 					     pf->hw.aq.asq_last_status));
13900 			return -ENOENT;
13901 		}
13902 		vsi->info = ctxt.info;
13903 		vsi->info.valid_sections = 0;
13904 
13905 		vsi->seid = ctxt.seid;
13906 		vsi->id = ctxt.vsi_number;
13907 
13908 		enabled_tc = i40e_pf_get_tc_map(pf);
13909 
13910 		/* Source pruning is enabled by default, so the flag is
13911 		 * negative logic - if it's set, we need to fiddle with
13912 		 * the VSI to disable source pruning.
13913 		 */
13914 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13915 			memset(&ctxt, 0, sizeof(ctxt));
13916 			ctxt.seid = pf->main_vsi_seid;
13917 			ctxt.pf_num = pf->hw.pf_id;
13918 			ctxt.vf_num = 0;
13919 			ctxt.info.valid_sections |=
13920 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13921 			ctxt.info.switch_id =
13922 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13923 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13924 			if (ret) {
13925 				dev_info(&pf->pdev->dev,
13926 					 "update vsi failed, err %d aq_err %s\n",
13927 					 ret,
13928 					 i40e_aq_str(&pf->hw,
13929 						     pf->hw.aq.asq_last_status));
13930 				ret = -ENOENT;
13931 				goto err;
13932 			}
13933 		}
13934 
13935 		/* MFP mode setup queue map and update VSI */
13936 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13937 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13938 			memset(&ctxt, 0, sizeof(ctxt));
13939 			ctxt.seid = pf->main_vsi_seid;
13940 			ctxt.pf_num = pf->hw.pf_id;
13941 			ctxt.vf_num = 0;
13942 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13943 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13944 			if (ret) {
13945 				dev_info(&pf->pdev->dev,
13946 					 "update vsi failed, err %pe aq_err %s\n",
13947 					 ERR_PTR(ret),
13948 					 i40e_aq_str(&pf->hw,
13949 						    pf->hw.aq.asq_last_status));
13950 				ret = -ENOENT;
13951 				goto err;
13952 			}
13953 			/* update the local VSI info queue map */
13954 			i40e_vsi_update_queue_map(vsi, &ctxt);
13955 			vsi->info.valid_sections = 0;
13956 		} else {
13957 			/* Default/Main VSI is only enabled for TC0
13958 			 * reconfigure it to enable all TCs that are
13959 			 * available on the port in SFP mode.
13960 			 * For MFP case the iSCSI PF would use this
13961 			 * flow to enable LAN+iSCSI TC.
13962 			 */
13963 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13964 			if (ret) {
13965 				/* Single TC condition is not fatal,
13966 				 * message and continue
13967 				 */
13968 				dev_info(&pf->pdev->dev,
13969 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
13970 					 enabled_tc,
13971 					 ERR_PTR(ret),
13972 					 i40e_aq_str(&pf->hw,
13973 						    pf->hw.aq.asq_last_status));
13974 			}
13975 		}
13976 		break;
13977 
13978 	case I40E_VSI_FDIR:
13979 		ctxt.pf_num = hw->pf_id;
13980 		ctxt.vf_num = 0;
13981 		ctxt.uplink_seid = vsi->uplink_seid;
13982 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13983 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13984 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
13985 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13986 			ctxt.info.valid_sections |=
13987 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13988 			ctxt.info.switch_id =
13989 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13990 		}
13991 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13992 		break;
13993 
13994 	case I40E_VSI_VMDQ2:
13995 		ctxt.pf_num = hw->pf_id;
13996 		ctxt.vf_num = 0;
13997 		ctxt.uplink_seid = vsi->uplink_seid;
13998 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13999 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14000 
14001 		/* This VSI is connected to VEB so the switch_id
14002 		 * should be set to zero by default.
14003 		 */
14004 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14005 			ctxt.info.valid_sections |=
14006 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14007 			ctxt.info.switch_id =
14008 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14009 		}
14010 
14011 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14012 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14013 		break;
14014 
14015 	case I40E_VSI_SRIOV:
14016 		ctxt.pf_num = hw->pf_id;
14017 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14018 		ctxt.uplink_seid = vsi->uplink_seid;
14019 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14020 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14021 
14022 		/* This VSI is connected to VEB so the switch_id
14023 		 * should be set to zero by default.
14024 		 */
14025 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14026 			ctxt.info.valid_sections |=
14027 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14028 			ctxt.info.switch_id =
14029 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14030 		}
14031 
14032 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14033 			ctxt.info.valid_sections |=
14034 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14035 			ctxt.info.queueing_opt_flags |=
14036 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14037 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14038 		}
14039 
14040 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14041 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14042 		if (pf->vf[vsi->vf_id].spoofchk) {
14043 			ctxt.info.valid_sections |=
14044 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14045 			ctxt.info.sec_flags |=
14046 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14047 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14048 		}
14049 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14050 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14051 		break;
14052 
14053 	case I40E_VSI_IWARP:
14054 		/* send down message to iWARP */
14055 		break;
14056 
14057 	default:
14058 		return -ENODEV;
14059 	}
14060 
14061 	if (vsi->type != I40E_VSI_MAIN) {
14062 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14063 		if (ret) {
14064 			dev_info(&vsi->back->pdev->dev,
14065 				 "add vsi failed, err %pe aq_err %s\n",
14066 				 ERR_PTR(ret),
14067 				 i40e_aq_str(&pf->hw,
14068 					     pf->hw.aq.asq_last_status));
14069 			ret = -ENOENT;
14070 			goto err;
14071 		}
14072 		vsi->info = ctxt.info;
14073 		vsi->info.valid_sections = 0;
14074 		vsi->seid = ctxt.seid;
14075 		vsi->id = ctxt.vsi_number;
14076 	}
14077 
14078 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14079 	vsi->active_filters = 0;
14080 	/* If macvlan filters already exist, force them to get loaded */
14081 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14082 		f->state = I40E_FILTER_NEW;
14083 		f_count++;
14084 	}
14085 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14086 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14087 
14088 	if (f_count) {
14089 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14090 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14091 	}
14092 
14093 	/* Update VSI BW information */
14094 	ret = i40e_vsi_get_bw_info(vsi);
14095 	if (ret) {
14096 		dev_info(&pf->pdev->dev,
14097 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14098 			 ERR_PTR(ret),
14099 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14100 		/* VSI is already added so not tearing that up */
14101 		ret = 0;
14102 	}
14103 
14104 err:
14105 	return ret;
14106 }
14107 
14108 /**
14109  * i40e_vsi_release - Delete a VSI and free its resources
14110  * @vsi: the VSI being removed
14111  *
14112  * Returns 0 on success or < 0 on error
14113  **/
14114 int i40e_vsi_release(struct i40e_vsi *vsi)
14115 {
14116 	struct i40e_mac_filter *f;
14117 	struct hlist_node *h;
14118 	struct i40e_veb *veb;
14119 	struct i40e_pf *pf;
14120 	u16 uplink_seid;
14121 	int i, n, bkt;
14122 
14123 	pf = vsi->back;
14124 
14125 	/* release of a VEB-owner or last VSI is not allowed */
14126 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14127 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14128 			 vsi->seid, vsi->uplink_seid);
14129 		return -ENODEV;
14130 	}
14131 	if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
14132 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14133 		return -ENODEV;
14134 	}
14135 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14136 	uplink_seid = vsi->uplink_seid;
14137 
14138 	if (vsi->type != I40E_VSI_SRIOV) {
14139 		if (vsi->netdev_registered) {
14140 			vsi->netdev_registered = false;
14141 			if (vsi->netdev) {
14142 				/* results in a call to i40e_close() */
14143 				unregister_netdev(vsi->netdev);
14144 			}
14145 		} else {
14146 			i40e_vsi_close(vsi);
14147 		}
14148 		i40e_vsi_disable_irq(vsi);
14149 	}
14150 
14151 	if (vsi->type == I40E_VSI_MAIN)
14152 		i40e_devlink_destroy_port(pf);
14153 
14154 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14155 
14156 	/* clear the sync flag on all filters */
14157 	if (vsi->netdev) {
14158 		__dev_uc_unsync(vsi->netdev, NULL);
14159 		__dev_mc_unsync(vsi->netdev, NULL);
14160 	}
14161 
14162 	/* make sure any remaining filters are marked for deletion */
14163 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14164 		__i40e_del_filter(vsi, f);
14165 
14166 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14167 
14168 	i40e_sync_vsi_filters(vsi);
14169 
14170 	i40e_vsi_delete(vsi);
14171 	i40e_vsi_free_q_vectors(vsi);
14172 	if (vsi->netdev) {
14173 		free_netdev(vsi->netdev);
14174 		vsi->netdev = NULL;
14175 	}
14176 	i40e_vsi_clear_rings(vsi);
14177 	i40e_vsi_clear(vsi);
14178 
14179 	/* If this was the last thing on the VEB, except for the
14180 	 * controlling VSI, remove the VEB, which puts the controlling
14181 	 * VSI onto the uplink port.
14182 	 *
14183 	 * Well, okay, there's one more exception here: don't remove
14184 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14185 	 * from up the network stack.
14186 	 */
14187 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14188 	if (veb && veb->uplink_seid) {
14189 		n = 0;
14190 
14191 		/* Count non-controlling VSIs present on  the VEB */
14192 		i40e_pf_for_each_vsi(pf, i, vsi)
14193 			if (vsi->uplink_seid == uplink_seid &&
14194 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14195 				n++;
14196 
14197 		/* If there is no VSI except the control one then release
14198 		 * the VEB and put the control VSI onto VEB uplink.
14199 		 */
14200 		if (!n)
14201 			i40e_veb_release(veb);
14202 	}
14203 
14204 	return 0;
14205 }
14206 
14207 /**
14208  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14209  * @vsi: ptr to the VSI
14210  *
14211  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14212  * corresponding SW VSI structure and initializes num_queue_pairs for the
14213  * newly allocated VSI.
14214  *
14215  * Returns 0 on success or negative on failure
14216  **/
14217 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14218 {
14219 	int ret = -ENOENT;
14220 	struct i40e_pf *pf = vsi->back;
14221 
14222 	if (vsi->q_vectors[0]) {
14223 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14224 			 vsi->seid);
14225 		return -EEXIST;
14226 	}
14227 
14228 	if (vsi->base_vector) {
14229 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14230 			 vsi->seid, vsi->base_vector);
14231 		return -EEXIST;
14232 	}
14233 
14234 	ret = i40e_vsi_alloc_q_vectors(vsi);
14235 	if (ret) {
14236 		dev_info(&pf->pdev->dev,
14237 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14238 			 vsi->num_q_vectors, vsi->seid, ret);
14239 		vsi->num_q_vectors = 0;
14240 		goto vector_setup_out;
14241 	}
14242 
14243 	/* In Legacy mode, we do not have to get any other vector since we
14244 	 * piggyback on the misc/ICR0 for queue interrupts.
14245 	*/
14246 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14247 		return ret;
14248 	if (vsi->num_q_vectors)
14249 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14250 						 vsi->num_q_vectors, vsi->idx);
14251 	if (vsi->base_vector < 0) {
14252 		dev_info(&pf->pdev->dev,
14253 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14254 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14255 		i40e_vsi_free_q_vectors(vsi);
14256 		ret = -ENOENT;
14257 		goto vector_setup_out;
14258 	}
14259 
14260 vector_setup_out:
14261 	return ret;
14262 }
14263 
14264 /**
14265  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14266  * @vsi: pointer to the vsi.
14267  *
14268  * This re-allocates a vsi's queue resources.
14269  *
14270  * Returns pointer to the successfully allocated and configured VSI sw struct
14271  * on success, otherwise returns NULL on failure.
14272  **/
14273 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14274 {
14275 	struct i40e_vsi *main_vsi;
14276 	u16 alloc_queue_pairs;
14277 	struct i40e_pf *pf;
14278 	int ret;
14279 
14280 	if (!vsi)
14281 		return NULL;
14282 
14283 	pf = vsi->back;
14284 
14285 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14286 	i40e_vsi_clear_rings(vsi);
14287 
14288 	i40e_vsi_free_arrays(vsi, false);
14289 	i40e_set_num_rings_in_vsi(vsi);
14290 	ret = i40e_vsi_alloc_arrays(vsi, false);
14291 	if (ret)
14292 		goto err_vsi;
14293 
14294 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14295 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14296 
14297 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14298 	if (ret < 0) {
14299 		dev_info(&pf->pdev->dev,
14300 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14301 			 alloc_queue_pairs, vsi->seid, ret);
14302 		goto err_vsi;
14303 	}
14304 	vsi->base_queue = ret;
14305 
14306 	/* Update the FW view of the VSI. Force a reset of TC and queue
14307 	 * layout configurations.
14308 	 */
14309 	main_vsi = i40e_pf_get_main_vsi(pf);
14310 	main_vsi->seid = pf->main_vsi_seid;
14311 	i40e_vsi_reconfig_tc(main_vsi);
14312 
14313 	if (vsi->type == I40E_VSI_MAIN)
14314 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14315 
14316 	/* assign it some queues */
14317 	ret = i40e_alloc_rings(vsi);
14318 	if (ret)
14319 		goto err_rings;
14320 
14321 	/* map all of the rings to the q_vectors */
14322 	i40e_vsi_map_rings_to_vectors(vsi);
14323 	return vsi;
14324 
14325 err_rings:
14326 	i40e_vsi_free_q_vectors(vsi);
14327 	if (vsi->netdev_registered) {
14328 		vsi->netdev_registered = false;
14329 		unregister_netdev(vsi->netdev);
14330 		free_netdev(vsi->netdev);
14331 		vsi->netdev = NULL;
14332 	}
14333 	if (vsi->type == I40E_VSI_MAIN)
14334 		i40e_devlink_destroy_port(pf);
14335 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14336 err_vsi:
14337 	i40e_vsi_clear(vsi);
14338 	return NULL;
14339 }
14340 
14341 /**
14342  * i40e_vsi_setup - Set up a VSI by a given type
14343  * @pf: board private structure
14344  * @type: VSI type
14345  * @uplink_seid: the switch element to link to
14346  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14347  *
14348  * This allocates the sw VSI structure and its queue resources, then add a VSI
14349  * to the identified VEB.
14350  *
14351  * Returns pointer to the successfully allocated and configure VSI sw struct on
14352  * success, otherwise returns NULL on failure.
14353  **/
14354 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14355 				u16 uplink_seid, u32 param1)
14356 {
14357 	struct i40e_vsi *vsi = NULL;
14358 	struct i40e_veb *veb = NULL;
14359 	u16 alloc_queue_pairs;
14360 	int v_idx;
14361 	int ret;
14362 
14363 	/* The requested uplink_seid must be either
14364 	 *     - the PF's port seid
14365 	 *              no VEB is needed because this is the PF
14366 	 *              or this is a Flow Director special case VSI
14367 	 *     - seid of an existing VEB
14368 	 *     - seid of a VSI that owns an existing VEB
14369 	 *     - seid of a VSI that doesn't own a VEB
14370 	 *              a new VEB is created and the VSI becomes the owner
14371 	 *     - seid of the PF VSI, which is what creates the first VEB
14372 	 *              this is a special case of the previous
14373 	 *
14374 	 * Find which uplink_seid we were given and create a new VEB if needed
14375 	 */
14376 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14377 	if (!veb && uplink_seid != pf->mac_seid) {
14378 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14379 		if (!vsi) {
14380 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14381 				 uplink_seid);
14382 			return NULL;
14383 		}
14384 
14385 		if (vsi->uplink_seid == pf->mac_seid)
14386 			veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
14387 					     vsi->tc_config.enabled_tc);
14388 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14389 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
14390 					     vsi->tc_config.enabled_tc);
14391 		if (veb) {
14392 			if (vsi->type != I40E_VSI_MAIN) {
14393 				dev_info(&vsi->back->pdev->dev,
14394 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14395 				return NULL;
14396 			}
14397 			/* We come up by default in VEPA mode if SRIOV is not
14398 			 * already enabled, in which case we can't force VEPA
14399 			 * mode.
14400 			 */
14401 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14402 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14403 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14404 			}
14405 			i40e_config_bridge_mode(veb);
14406 		}
14407 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14408 		if (!veb) {
14409 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14410 			return NULL;
14411 		}
14412 
14413 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14414 		uplink_seid = veb->seid;
14415 	}
14416 
14417 	/* get vsi sw struct */
14418 	v_idx = i40e_vsi_mem_alloc(pf, type);
14419 	if (v_idx < 0)
14420 		goto err_alloc;
14421 	vsi = pf->vsi[v_idx];
14422 	if (!vsi)
14423 		goto err_alloc;
14424 	vsi->type = type;
14425 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14426 
14427 	if (type == I40E_VSI_MAIN)
14428 		pf->lan_vsi = v_idx;
14429 	else if (type == I40E_VSI_SRIOV)
14430 		vsi->vf_id = param1;
14431 	/* assign it some queues */
14432 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14433 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14434 
14435 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14436 	if (ret < 0) {
14437 		dev_info(&pf->pdev->dev,
14438 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14439 			 alloc_queue_pairs, vsi->seid, ret);
14440 		goto err_vsi;
14441 	}
14442 	vsi->base_queue = ret;
14443 
14444 	/* get a VSI from the hardware */
14445 	vsi->uplink_seid = uplink_seid;
14446 	ret = i40e_add_vsi(vsi);
14447 	if (ret)
14448 		goto err_vsi;
14449 
14450 	switch (vsi->type) {
14451 	/* setup the netdev if needed */
14452 	case I40E_VSI_MAIN:
14453 	case I40E_VSI_VMDQ2:
14454 		ret = i40e_config_netdev(vsi);
14455 		if (ret)
14456 			goto err_netdev;
14457 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14458 		if (ret)
14459 			goto err_netdev;
14460 		if (vsi->type == I40E_VSI_MAIN) {
14461 			ret = i40e_devlink_create_port(pf);
14462 			if (ret)
14463 				goto err_netdev;
14464 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14465 		}
14466 		ret = register_netdev(vsi->netdev);
14467 		if (ret)
14468 			goto err_dl_port;
14469 		vsi->netdev_registered = true;
14470 		netif_carrier_off(vsi->netdev);
14471 #ifdef CONFIG_I40E_DCB
14472 		/* Setup DCB netlink interface */
14473 		i40e_dcbnl_setup(vsi);
14474 #endif /* CONFIG_I40E_DCB */
14475 		fallthrough;
14476 	case I40E_VSI_FDIR:
14477 		/* set up vectors and rings if needed */
14478 		ret = i40e_vsi_setup_vectors(vsi);
14479 		if (ret)
14480 			goto err_msix;
14481 
14482 		ret = i40e_alloc_rings(vsi);
14483 		if (ret)
14484 			goto err_rings;
14485 
14486 		/* map all of the rings to the q_vectors */
14487 		i40e_vsi_map_rings_to_vectors(vsi);
14488 
14489 		i40e_vsi_reset_stats(vsi);
14490 		break;
14491 	default:
14492 		/* no netdev or rings for the other VSI types */
14493 		break;
14494 	}
14495 
14496 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14497 	    vsi->type == I40E_VSI_VMDQ2) {
14498 		ret = i40e_vsi_config_rss(vsi);
14499 		if (ret)
14500 			goto err_config;
14501 	}
14502 	return vsi;
14503 
14504 err_config:
14505 	i40e_vsi_clear_rings(vsi);
14506 err_rings:
14507 	i40e_vsi_free_q_vectors(vsi);
14508 err_msix:
14509 	if (vsi->netdev_registered) {
14510 		vsi->netdev_registered = false;
14511 		unregister_netdev(vsi->netdev);
14512 		free_netdev(vsi->netdev);
14513 		vsi->netdev = NULL;
14514 	}
14515 err_dl_port:
14516 	if (vsi->type == I40E_VSI_MAIN)
14517 		i40e_devlink_destroy_port(pf);
14518 err_netdev:
14519 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14520 err_vsi:
14521 	i40e_vsi_clear(vsi);
14522 err_alloc:
14523 	return NULL;
14524 }
14525 
14526 /**
14527  * i40e_veb_get_bw_info - Query VEB BW information
14528  * @veb: the veb to query
14529  *
14530  * Query the Tx scheduler BW configuration data for given VEB
14531  **/
14532 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14533 {
14534 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14535 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14536 	struct i40e_pf *pf = veb->pf;
14537 	struct i40e_hw *hw = &pf->hw;
14538 	u32 tc_bw_max;
14539 	int ret = 0;
14540 	int i;
14541 
14542 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14543 						  &bw_data, NULL);
14544 	if (ret) {
14545 		dev_info(&pf->pdev->dev,
14546 			 "query veb bw config failed, err %pe aq_err %s\n",
14547 			 ERR_PTR(ret),
14548 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14549 		goto out;
14550 	}
14551 
14552 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14553 						   &ets_data, NULL);
14554 	if (ret) {
14555 		dev_info(&pf->pdev->dev,
14556 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14557 			 ERR_PTR(ret),
14558 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14559 		goto out;
14560 	}
14561 
14562 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14563 	veb->bw_max_quanta = ets_data.tc_bw_max;
14564 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14565 	veb->enabled_tc = ets_data.tc_valid_bits;
14566 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14567 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14568 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14569 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14570 		veb->bw_tc_limit_credits[i] =
14571 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14572 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14573 	}
14574 
14575 out:
14576 	return ret;
14577 }
14578 
14579 /**
14580  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14581  * @pf: board private structure
14582  *
14583  * On error: returns error code (negative)
14584  * On success: returns vsi index in PF (positive)
14585  **/
14586 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14587 {
14588 	int ret = -ENOENT;
14589 	struct i40e_veb *veb;
14590 	int i;
14591 
14592 	/* Need to protect the allocation of switch elements at the PF level */
14593 	mutex_lock(&pf->switch_mutex);
14594 
14595 	/* VEB list may be fragmented if VEB creation/destruction has
14596 	 * been happening.  We can afford to do a quick scan to look
14597 	 * for any free slots in the list.
14598 	 *
14599 	 * find next empty veb slot, looping back around if necessary
14600 	 */
14601 	i = 0;
14602 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14603 		i++;
14604 	if (i >= I40E_MAX_VEB) {
14605 		ret = -ENOMEM;
14606 		goto err_alloc_veb;  /* out of VEB slots! */
14607 	}
14608 
14609 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14610 	if (!veb) {
14611 		ret = -ENOMEM;
14612 		goto err_alloc_veb;
14613 	}
14614 	veb->pf = pf;
14615 	veb->idx = i;
14616 	veb->enabled_tc = 1;
14617 
14618 	pf->veb[i] = veb;
14619 	ret = i;
14620 err_alloc_veb:
14621 	mutex_unlock(&pf->switch_mutex);
14622 	return ret;
14623 }
14624 
14625 /**
14626  * i40e_switch_branch_release - Delete a branch of the switch tree
14627  * @branch: where to start deleting
14628  *
14629  * This uses recursion to find the tips of the branch to be
14630  * removed, deleting until we get back to and can delete this VEB.
14631  **/
14632 static void i40e_switch_branch_release(struct i40e_veb *branch)
14633 {
14634 	struct i40e_pf *pf = branch->pf;
14635 	u16 branch_seid = branch->seid;
14636 	u16 veb_idx = branch->idx;
14637 	struct i40e_vsi *vsi;
14638 	struct i40e_veb *veb;
14639 	int i;
14640 
14641 	/* release any VEBs on this VEB - RECURSION */
14642 	i40e_pf_for_each_veb(pf, i, veb)
14643 		if (veb->uplink_seid == branch->seid)
14644 			i40e_switch_branch_release(veb);
14645 
14646 	/* Release the VSIs on this VEB, but not the owner VSI.
14647 	 *
14648 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14649 	 *       the VEB itself, so don't use (*branch) after this loop.
14650 	 */
14651 	i40e_pf_for_each_vsi(pf, i, vsi)
14652 		if (vsi->uplink_seid == branch_seid &&
14653 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14654 			i40e_vsi_release(vsi);
14655 
14656 	/* There's one corner case where the VEB might not have been
14657 	 * removed, so double check it here and remove it if needed.
14658 	 * This case happens if the veb was created from the debugfs
14659 	 * commands and no VSIs were added to it.
14660 	 */
14661 	if (pf->veb[veb_idx])
14662 		i40e_veb_release(pf->veb[veb_idx]);
14663 }
14664 
14665 /**
14666  * i40e_veb_clear - remove veb struct
14667  * @veb: the veb to remove
14668  **/
14669 static void i40e_veb_clear(struct i40e_veb *veb)
14670 {
14671 	if (!veb)
14672 		return;
14673 
14674 	if (veb->pf) {
14675 		struct i40e_pf *pf = veb->pf;
14676 
14677 		mutex_lock(&pf->switch_mutex);
14678 		if (pf->veb[veb->idx] == veb)
14679 			pf->veb[veb->idx] = NULL;
14680 		mutex_unlock(&pf->switch_mutex);
14681 	}
14682 
14683 	kfree(veb);
14684 }
14685 
14686 /**
14687  * i40e_veb_release - Delete a VEB and free its resources
14688  * @veb: the VEB being removed
14689  **/
14690 void i40e_veb_release(struct i40e_veb *veb)
14691 {
14692 	struct i40e_vsi *vsi, *vsi_it;
14693 	struct i40e_pf *pf;
14694 	int i, n = 0;
14695 
14696 	pf = veb->pf;
14697 
14698 	/* find the remaining VSI and check for extras */
14699 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14700 		if (vsi_it->uplink_seid == veb->seid) {
14701 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14702 				vsi = vsi_it;
14703 			n++;
14704 		}
14705 
14706 	/* Floating VEB has to be empty and regular one must have
14707 	 * single owner VSI.
14708 	 */
14709 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14710 		dev_info(&pf->pdev->dev,
14711 			 "can't remove VEB %d with %d VSIs left\n",
14712 			 veb->seid, n);
14713 		return;
14714 	}
14715 
14716 	/* For regular VEB move the owner VSI to uplink port */
14717 	if (veb->uplink_seid) {
14718 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14719 		vsi->uplink_seid = veb->uplink_seid;
14720 		vsi->veb_idx = I40E_NO_VEB;
14721 	}
14722 
14723 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14724 	i40e_veb_clear(veb);
14725 }
14726 
14727 /**
14728  * i40e_add_veb - create the VEB in the switch
14729  * @veb: the VEB to be instantiated
14730  * @vsi: the controlling VSI
14731  **/
14732 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14733 {
14734 	struct i40e_pf *pf = veb->pf;
14735 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14736 	int ret;
14737 
14738 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14739 			      veb->enabled_tc, vsi ? false : true,
14740 			      &veb->seid, enable_stats, NULL);
14741 
14742 	/* get a VEB from the hardware */
14743 	if (ret) {
14744 		dev_info(&pf->pdev->dev,
14745 			 "couldn't add VEB, err %pe aq_err %s\n",
14746 			 ERR_PTR(ret),
14747 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14748 		return -EPERM;
14749 	}
14750 
14751 	/* get statistics counter */
14752 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14753 					 &veb->stats_idx, NULL, NULL, NULL);
14754 	if (ret) {
14755 		dev_info(&pf->pdev->dev,
14756 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14757 			 ERR_PTR(ret),
14758 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14759 		return -EPERM;
14760 	}
14761 	ret = i40e_veb_get_bw_info(veb);
14762 	if (ret) {
14763 		dev_info(&pf->pdev->dev,
14764 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14765 			 ERR_PTR(ret),
14766 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14767 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14768 		return -ENOENT;
14769 	}
14770 
14771 	if (vsi) {
14772 		vsi->uplink_seid = veb->seid;
14773 		vsi->veb_idx = veb->idx;
14774 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14775 	}
14776 
14777 	return 0;
14778 }
14779 
14780 /**
14781  * i40e_veb_setup - Set up a VEB
14782  * @pf: board private structure
14783  * @uplink_seid: the switch element to link to
14784  * @vsi_seid: the initial VSI seid
14785  * @enabled_tc: Enabled TC bit-map
14786  *
14787  * This allocates the sw VEB structure and links it into the switch
14788  * It is possible and legal for this to be a duplicate of an already
14789  * existing VEB.  It is also possible for both uplink and vsi seids
14790  * to be zero, in order to create a floating VEB.
14791  *
14792  * Returns pointer to the successfully allocated VEB sw struct on
14793  * success, otherwise returns NULL on failure.
14794  **/
14795 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
14796 				u16 vsi_seid, u8 enabled_tc)
14797 {
14798 	struct i40e_vsi *vsi = NULL;
14799 	struct i40e_veb *veb;
14800 	int veb_idx;
14801 	int ret;
14802 
14803 	/* if one seid is 0, the other must be 0 to create a floating relay */
14804 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14805 	    (uplink_seid + vsi_seid != 0)) {
14806 		dev_info(&pf->pdev->dev,
14807 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14808 			 uplink_seid, vsi_seid);
14809 		return NULL;
14810 	}
14811 
14812 	/* make sure there is such a vsi and uplink */
14813 	if (vsi_seid) {
14814 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14815 		if (!vsi) {
14816 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14817 				vsi_seid);
14818 			return NULL;
14819 		}
14820 	}
14821 
14822 	/* get veb sw struct */
14823 	veb_idx = i40e_veb_mem_alloc(pf);
14824 	if (veb_idx < 0)
14825 		goto err_alloc;
14826 	veb = pf->veb[veb_idx];
14827 	veb->uplink_seid = uplink_seid;
14828 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14829 
14830 	/* create the VEB in the switch */
14831 	ret = i40e_add_veb(veb, vsi);
14832 	if (ret)
14833 		goto err_veb;
14834 
14835 	if (vsi && vsi->idx == pf->lan_vsi)
14836 		pf->lan_veb = veb->idx;
14837 
14838 	return veb;
14839 
14840 err_veb:
14841 	i40e_veb_clear(veb);
14842 err_alloc:
14843 	return NULL;
14844 }
14845 
14846 /**
14847  * i40e_setup_pf_switch_element - set PF vars based on switch type
14848  * @pf: board private structure
14849  * @ele: element we are building info from
14850  * @num_reported: total number of elements
14851  * @printconfig: should we print the contents
14852  *
14853  * helper function to assist in extracting a few useful SEID values.
14854  **/
14855 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14856 				struct i40e_aqc_switch_config_element_resp *ele,
14857 				u16 num_reported, bool printconfig)
14858 {
14859 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14860 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14861 	u8 element_type = ele->element_type;
14862 	u16 seid = le16_to_cpu(ele->seid);
14863 	struct i40e_veb *veb;
14864 
14865 	if (printconfig)
14866 		dev_info(&pf->pdev->dev,
14867 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14868 			 element_type, seid, uplink_seid, downlink_seid);
14869 
14870 	switch (element_type) {
14871 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14872 		pf->mac_seid = seid;
14873 		break;
14874 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14875 		/* Main VEB? */
14876 		if (uplink_seid != pf->mac_seid)
14877 			break;
14878 		veb = i40e_pf_get_main_veb(pf);
14879 		if (!veb) {
14880 			int v;
14881 
14882 			/* find existing or else empty VEB */
14883 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14884 			if (veb) {
14885 				pf->lan_veb = veb->idx;
14886 			} else {
14887 				v = i40e_veb_mem_alloc(pf);
14888 				if (v < 0)
14889 					break;
14890 				pf->lan_veb = v;
14891 			}
14892 		}
14893 
14894 		/* Try to get again main VEB as pf->lan_veb may have changed */
14895 		veb = i40e_pf_get_main_veb(pf);
14896 		if (!veb)
14897 			break;
14898 
14899 		veb->seid = seid;
14900 		veb->uplink_seid = pf->mac_seid;
14901 		veb->pf = pf;
14902 		break;
14903 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14904 		if (num_reported != 1)
14905 			break;
14906 		/* This is immediately after a reset so we can assume this is
14907 		 * the PF's VSI
14908 		 */
14909 		pf->mac_seid = uplink_seid;
14910 		pf->main_vsi_seid = seid;
14911 		if (printconfig)
14912 			dev_info(&pf->pdev->dev,
14913 				 "pf_seid=%d main_vsi_seid=%d\n",
14914 				 downlink_seid, pf->main_vsi_seid);
14915 		break;
14916 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14917 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14918 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14919 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14920 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14921 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14922 		/* ignore these for now */
14923 		break;
14924 	default:
14925 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14926 			 element_type, seid);
14927 		break;
14928 	}
14929 }
14930 
14931 /**
14932  * i40e_fetch_switch_configuration - Get switch config from firmware
14933  * @pf: board private structure
14934  * @printconfig: should we print the contents
14935  *
14936  * Get the current switch configuration from the device and
14937  * extract a few useful SEID values.
14938  **/
14939 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14940 {
14941 	struct i40e_aqc_get_switch_config_resp *sw_config;
14942 	u16 next_seid = 0;
14943 	int ret = 0;
14944 	u8 *aq_buf;
14945 	int i;
14946 
14947 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14948 	if (!aq_buf)
14949 		return -ENOMEM;
14950 
14951 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14952 	do {
14953 		u16 num_reported, num_total;
14954 
14955 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14956 						I40E_AQ_LARGE_BUF,
14957 						&next_seid, NULL);
14958 		if (ret) {
14959 			dev_info(&pf->pdev->dev,
14960 				 "get switch config failed err %d aq_err %s\n",
14961 				 ret,
14962 				 i40e_aq_str(&pf->hw,
14963 					     pf->hw.aq.asq_last_status));
14964 			kfree(aq_buf);
14965 			return -ENOENT;
14966 		}
14967 
14968 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14969 		num_total = le16_to_cpu(sw_config->header.num_total);
14970 
14971 		if (printconfig)
14972 			dev_info(&pf->pdev->dev,
14973 				 "header: %d reported %d total\n",
14974 				 num_reported, num_total);
14975 
14976 		for (i = 0; i < num_reported; i++) {
14977 			struct i40e_aqc_switch_config_element_resp *ele =
14978 				&sw_config->element[i];
14979 
14980 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14981 						     printconfig);
14982 		}
14983 	} while (next_seid != 0);
14984 
14985 	kfree(aq_buf);
14986 	return ret;
14987 }
14988 
14989 /**
14990  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14991  * @pf: board private structure
14992  * @reinit: if the Main VSI needs to re-initialized.
14993  * @lock_acquired: indicates whether or not the lock has been acquired
14994  *
14995  * Returns 0 on success, negative value on failure
14996  **/
14997 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14998 {
14999 	struct i40e_vsi *main_vsi;
15000 	u16 flags = 0;
15001 	int ret;
15002 
15003 	/* find out what's out there already */
15004 	ret = i40e_fetch_switch_configuration(pf, false);
15005 	if (ret) {
15006 		dev_info(&pf->pdev->dev,
15007 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15008 			 ERR_PTR(ret),
15009 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15010 		return ret;
15011 	}
15012 	i40e_pf_reset_stats(pf);
15013 
15014 	/* set the switch config bit for the whole device to
15015 	 * support limited promisc or true promisc
15016 	 * when user requests promisc. The default is limited
15017 	 * promisc.
15018 	*/
15019 
15020 	if ((pf->hw.pf_id == 0) &&
15021 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
15022 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15023 		pf->last_sw_conf_flags = flags;
15024 	}
15025 
15026 	if (pf->hw.pf_id == 0) {
15027 		u16 valid_flags;
15028 
15029 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15030 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15031 						NULL);
15032 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15033 			dev_info(&pf->pdev->dev,
15034 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15035 				 ERR_PTR(ret),
15036 				 i40e_aq_str(&pf->hw,
15037 					     pf->hw.aq.asq_last_status));
15038 			/* not a fatal problem, just keep going */
15039 		}
15040 		pf->last_sw_conf_valid_flags = valid_flags;
15041 	}
15042 
15043 	/* first time setup */
15044 	main_vsi = i40e_pf_get_main_vsi(pf);
15045 	if (!main_vsi || reinit) {
15046 		struct i40e_veb *veb;
15047 		u16 uplink_seid;
15048 
15049 		/* Set up the PF VSI associated with the PF's main VSI
15050 		 * that is already in the HW switch
15051 		 */
15052 		veb = i40e_pf_get_main_veb(pf);
15053 		if (veb)
15054 			uplink_seid = veb->seid;
15055 		else
15056 			uplink_seid = pf->mac_seid;
15057 		if (!main_vsi)
15058 			main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
15059 						  uplink_seid, 0);
15060 		else if (reinit)
15061 			main_vsi = i40e_vsi_reinit_setup(main_vsi);
15062 		if (!main_vsi) {
15063 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15064 			i40e_cloud_filter_exit(pf);
15065 			i40e_fdir_teardown(pf);
15066 			return -EAGAIN;
15067 		}
15068 	} else {
15069 		/* force a reset of TC and queue layout configurations */
15070 		main_vsi->seid = pf->main_vsi_seid;
15071 		i40e_vsi_reconfig_tc(main_vsi);
15072 	}
15073 	i40e_vlan_stripping_disable(main_vsi);
15074 
15075 	i40e_fdir_sb_setup(pf);
15076 
15077 	/* Setup static PF queue filter control settings */
15078 	ret = i40e_setup_pf_filter_control(pf);
15079 	if (ret) {
15080 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15081 			 ret);
15082 		/* Failure here should not stop continuing other steps */
15083 	}
15084 
15085 	/* enable RSS in the HW, even for only one queue, as the stack can use
15086 	 * the hash
15087 	 */
15088 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15089 		i40e_pf_config_rss(pf);
15090 
15091 	/* fill in link information and enable LSE reporting */
15092 	i40e_link_event(pf);
15093 
15094 	i40e_ptp_init(pf);
15095 
15096 	if (!lock_acquired)
15097 		rtnl_lock();
15098 
15099 	/* repopulate tunnel port filters */
15100 	udp_tunnel_nic_reset_ntf(main_vsi->netdev);
15101 
15102 	if (!lock_acquired)
15103 		rtnl_unlock();
15104 
15105 	return ret;
15106 }
15107 
15108 /**
15109  * i40e_determine_queue_usage - Work out queue distribution
15110  * @pf: board private structure
15111  **/
15112 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15113 {
15114 	int queues_left;
15115 	int q_max;
15116 
15117 	pf->num_lan_qps = 0;
15118 
15119 	/* Find the max queues to be put into basic use.  We'll always be
15120 	 * using TC0, whether or not DCB is running, and TC0 will get the
15121 	 * big RSS set.
15122 	 */
15123 	queues_left = pf->hw.func_caps.num_tx_qp;
15124 
15125 	if ((queues_left == 1) ||
15126 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15127 		/* one qp for PF, no queues for anything else */
15128 		queues_left = 0;
15129 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15130 
15131 		/* make sure all the fancies are disabled */
15132 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15133 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15134 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15135 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15136 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15137 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15138 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15139 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15140 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15141 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15142 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15143 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15144 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15145 		/* one qp for PF */
15146 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15147 		queues_left -= pf->num_lan_qps;
15148 
15149 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15150 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15151 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15152 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15153 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15154 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15155 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15156 	} else {
15157 		/* Not enough queues for all TCs */
15158 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15159 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15160 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15161 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15162 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15163 		}
15164 
15165 		/* limit lan qps to the smaller of qps, cpus or msix */
15166 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15167 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15168 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15169 		pf->num_lan_qps = q_max;
15170 
15171 		queues_left -= pf->num_lan_qps;
15172 	}
15173 
15174 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15175 		if (queues_left > 1) {
15176 			queues_left -= 1; /* save 1 queue for FD */
15177 		} else {
15178 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15179 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15180 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15181 		}
15182 	}
15183 
15184 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15185 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15186 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15187 					(queues_left / pf->num_vf_qps));
15188 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15189 	}
15190 
15191 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15192 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15193 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15194 					  (queues_left / pf->num_vmdq_qps));
15195 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15196 	}
15197 
15198 	pf->queues_left = queues_left;
15199 	dev_dbg(&pf->pdev->dev,
15200 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15201 		pf->hw.func_caps.num_tx_qp,
15202 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15203 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15204 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15205 		queues_left);
15206 }
15207 
15208 /**
15209  * i40e_setup_pf_filter_control - Setup PF static filter control
15210  * @pf: PF to be setup
15211  *
15212  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15213  * settings. If PE/FCoE are enabled then it will also set the per PF
15214  * based filter sizes required for them. It also enables Flow director,
15215  * ethertype and macvlan type filter settings for the pf.
15216  *
15217  * Returns 0 on success, negative on failure
15218  **/
15219 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15220 {
15221 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15222 
15223 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15224 
15225 	/* Flow Director is enabled */
15226 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15227 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15228 		settings->enable_fdir = true;
15229 
15230 	/* Ethtype and MACVLAN filters enabled for PF */
15231 	settings->enable_ethtype = true;
15232 	settings->enable_macvlan = true;
15233 
15234 	if (i40e_set_filter_control(&pf->hw, settings))
15235 		return -ENOENT;
15236 
15237 	return 0;
15238 }
15239 
15240 #define INFO_STRING_LEN 255
15241 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15242 static void i40e_print_features(struct i40e_pf *pf)
15243 {
15244 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
15245 	struct i40e_hw *hw = &pf->hw;
15246 	char *buf;
15247 	int i;
15248 
15249 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15250 	if (!buf)
15251 		return;
15252 
15253 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15254 #ifdef CONFIG_PCI_IOV
15255 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15256 #endif
15257 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15258 		       pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
15259 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15260 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15261 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15262 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15263 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15264 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15265 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15266 	}
15267 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15268 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15269 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15270 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15271 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15272 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15273 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15274 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15275 	else
15276 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15277 
15278 	dev_info(&pf->pdev->dev, "%s\n", buf);
15279 	kfree(buf);
15280 	WARN_ON(i > INFO_STRING_LEN);
15281 }
15282 
15283 /**
15284  * i40e_get_platform_mac_addr - get platform-specific MAC address
15285  * @pdev: PCI device information struct
15286  * @pf: board private structure
15287  *
15288  * Look up the MAC address for the device. First we'll try
15289  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15290  * specific fallback. Otherwise, we'll default to the stored value in
15291  * firmware.
15292  **/
15293 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15294 {
15295 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15296 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15297 }
15298 
15299 /**
15300  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15301  * @fec_cfg: FEC option to set in flags
15302  * @flags: ptr to flags in which we set FEC option
15303  **/
15304 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15305 {
15306 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15307 		set_bit(I40E_FLAG_RS_FEC, flags);
15308 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15309 	}
15310 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15311 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15312 		set_bit(I40E_FLAG_RS_FEC, flags);
15313 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15314 	}
15315 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15316 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15317 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15318 		clear_bit(I40E_FLAG_RS_FEC, flags);
15319 	}
15320 	if (fec_cfg == 0) {
15321 		clear_bit(I40E_FLAG_RS_FEC, flags);
15322 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15323 	}
15324 }
15325 
15326 /**
15327  * i40e_check_recovery_mode - check if we are running transition firmware
15328  * @pf: board private structure
15329  *
15330  * Check registers indicating the firmware runs in recovery mode. Sets the
15331  * appropriate driver state.
15332  *
15333  * Returns true if the recovery mode was detected, false otherwise
15334  **/
15335 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15336 {
15337 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15338 
15339 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15340 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15341 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15342 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15343 
15344 		return true;
15345 	}
15346 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15347 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15348 
15349 	return false;
15350 }
15351 
15352 /**
15353  * i40e_pf_loop_reset - perform reset in a loop.
15354  * @pf: board private structure
15355  *
15356  * This function is useful when a NIC is about to enter recovery mode.
15357  * When a NIC's internal data structures are corrupted the NIC's
15358  * firmware is going to enter recovery mode.
15359  * Right after a POR it takes about 7 minutes for firmware to enter
15360  * recovery mode. Until that time a NIC is in some kind of intermediate
15361  * state. After that time period the NIC almost surely enters
15362  * recovery mode. The only way for a driver to detect intermediate
15363  * state is to issue a series of pf-resets and check a return value.
15364  * If a PF reset returns success then the firmware could be in recovery
15365  * mode so the caller of this code needs to check for recovery mode
15366  * if this function returns success. There is a little chance that
15367  * firmware will hang in intermediate state forever.
15368  * Since waiting 7 minutes is quite a lot of time this function waits
15369  * 10 seconds and then gives up by returning an error.
15370  *
15371  * Return 0 on success, negative on failure.
15372  **/
15373 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15374 {
15375 	/* wait max 10 seconds for PF reset to succeed */
15376 	const unsigned long time_end = jiffies + 10 * HZ;
15377 	struct i40e_hw *hw = &pf->hw;
15378 	int ret;
15379 
15380 	ret = i40e_pf_reset(hw);
15381 	while (ret != 0 && time_before(jiffies, time_end)) {
15382 		usleep_range(10000, 20000);
15383 		ret = i40e_pf_reset(hw);
15384 	}
15385 
15386 	if (ret == 0)
15387 		pf->pfr_count++;
15388 	else
15389 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15390 
15391 	return ret;
15392 }
15393 
15394 /**
15395  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15396  * @pf: board private structure
15397  *
15398  * Check FW registers to determine if FW issued unexpected EMP Reset.
15399  * Every time when unexpected EMP Reset occurs the FW increments
15400  * a counter of unexpected EMP Resets. When the counter reaches 10
15401  * the FW should enter the Recovery mode
15402  *
15403  * Returns true if FW issued unexpected EMP Reset
15404  **/
15405 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15406 {
15407 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15408 			   I40E_GL_FWSTS_FWS1B_MASK;
15409 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15410 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15411 }
15412 
15413 /**
15414  * i40e_handle_resets - handle EMP resets and PF resets
15415  * @pf: board private structure
15416  *
15417  * Handle both EMP resets and PF resets and conclude whether there are
15418  * any issues regarding these resets. If there are any issues then
15419  * generate log entry.
15420  *
15421  * Return 0 if NIC is healthy or negative value when there are issues
15422  * with resets
15423  **/
15424 static int i40e_handle_resets(struct i40e_pf *pf)
15425 {
15426 	const int pfr = i40e_pf_loop_reset(pf);
15427 	const bool is_empr = i40e_check_fw_empr(pf);
15428 
15429 	if (is_empr || pfr != 0)
15430 		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");
15431 
15432 	return is_empr ? -EIO : pfr;
15433 }
15434 
15435 /**
15436  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15437  * @pf: board private structure
15438  * @hw: ptr to the hardware info
15439  *
15440  * This function does a minimal setup of all subsystems needed for running
15441  * recovery mode.
15442  *
15443  * Returns 0 on success, negative on failure
15444  **/
15445 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15446 {
15447 	struct i40e_vsi *vsi;
15448 	int err;
15449 	int v_idx;
15450 
15451 	pci_set_drvdata(pf->pdev, pf);
15452 	pci_save_state(pf->pdev);
15453 
15454 	/* set up periodic task facility */
15455 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15456 	pf->service_timer_period = HZ;
15457 
15458 	INIT_WORK(&pf->service_task, i40e_service_task);
15459 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15460 
15461 	err = i40e_init_interrupt_scheme(pf);
15462 	if (err)
15463 		goto err_switch_setup;
15464 
15465 	/* The number of VSIs reported by the FW is the minimum guaranteed
15466 	 * to us; HW supports far more and we share the remaining pool with
15467 	 * the other PFs. We allocate space for more than the guarantee with
15468 	 * the understanding that we might not get them all later.
15469 	 */
15470 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15471 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15472 	else
15473 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15474 
15475 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15476 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15477 			  GFP_KERNEL);
15478 	if (!pf->vsi) {
15479 		err = -ENOMEM;
15480 		goto err_switch_setup;
15481 	}
15482 
15483 	/* We allocate one VSI which is needed as absolute minimum
15484 	 * in order to register the netdev
15485 	 */
15486 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15487 	if (v_idx < 0) {
15488 		err = v_idx;
15489 		goto err_switch_setup;
15490 	}
15491 	pf->lan_vsi = v_idx;
15492 	vsi = pf->vsi[v_idx];
15493 	if (!vsi) {
15494 		err = -EFAULT;
15495 		goto err_switch_setup;
15496 	}
15497 	vsi->alloc_queue_pairs = 1;
15498 	err = i40e_config_netdev(vsi);
15499 	if (err)
15500 		goto err_switch_setup;
15501 	err = register_netdev(vsi->netdev);
15502 	if (err)
15503 		goto err_switch_setup;
15504 	vsi->netdev_registered = true;
15505 	i40e_dbg_pf_init(pf);
15506 
15507 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15508 	if (err)
15509 		goto err_switch_setup;
15510 
15511 	/* tell the firmware that we're starting */
15512 	i40e_send_version(pf);
15513 
15514 	/* since everything's happy, start the service_task timer */
15515 	mod_timer(&pf->service_timer,
15516 		  round_jiffies(jiffies + pf->service_timer_period));
15517 
15518 	return 0;
15519 
15520 err_switch_setup:
15521 	i40e_reset_interrupt_capability(pf);
15522 	timer_shutdown_sync(&pf->service_timer);
15523 	i40e_shutdown_adminq(hw);
15524 	iounmap(hw->hw_addr);
15525 	pci_release_mem_regions(pf->pdev);
15526 	pci_disable_device(pf->pdev);
15527 	i40e_free_pf(pf);
15528 
15529 	return err;
15530 }
15531 
15532 /**
15533  * i40e_set_subsystem_device_id - set subsystem device id
15534  * @hw: pointer to the hardware info
15535  *
15536  * Set PCI subsystem device id either from a pci_dev structure or
15537  * a specific FW register.
15538  **/
15539 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15540 {
15541 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15542 
15543 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15544 		pf->pdev->subsystem_device :
15545 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15546 }
15547 
15548 /**
15549  * i40e_probe - Device initialization routine
15550  * @pdev: PCI device information struct
15551  * @ent: entry in i40e_pci_tbl
15552  *
15553  * i40e_probe initializes a PF identified by a pci_dev structure.
15554  * The OS initialization, configuring of the PF private structure,
15555  * and a hardware reset occur.
15556  *
15557  * Returns 0 on success, negative on failure
15558  **/
15559 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15560 {
15561 	struct i40e_aq_get_phy_abilities_resp abilities;
15562 #ifdef CONFIG_I40E_DCB
15563 	enum i40e_get_fw_lldp_status_resp lldp_status;
15564 #endif /* CONFIG_I40E_DCB */
15565 	struct i40e_vsi *vsi;
15566 	struct i40e_pf *pf;
15567 	struct i40e_hw *hw;
15568 	u16 wol_nvm_bits;
15569 	char nvm_ver[32];
15570 	u16 link_status;
15571 #ifdef CONFIG_I40E_DCB
15572 	int status;
15573 #endif /* CONFIG_I40E_DCB */
15574 	int err;
15575 	u32 val;
15576 
15577 	err = pci_enable_device_mem(pdev);
15578 	if (err)
15579 		return err;
15580 
15581 	/* set up for high or low dma */
15582 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15583 	if (err) {
15584 		dev_err(&pdev->dev,
15585 			"DMA configuration failed: 0x%x\n", err);
15586 		goto err_dma;
15587 	}
15588 
15589 	/* set up pci connections */
15590 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15591 	if (err) {
15592 		dev_info(&pdev->dev,
15593 			 "pci_request_selected_regions failed %d\n", err);
15594 		goto err_pci_reg;
15595 	}
15596 
15597 	pci_set_master(pdev);
15598 
15599 	/* Now that we have a PCI connection, we need to do the
15600 	 * low level device setup.  This is primarily setting up
15601 	 * the Admin Queue structures and then querying for the
15602 	 * device's current profile information.
15603 	 */
15604 	pf = i40e_alloc_pf(&pdev->dev);
15605 	if (!pf) {
15606 		err = -ENOMEM;
15607 		goto err_pf_alloc;
15608 	}
15609 	pf->next_vsi = 0;
15610 	pf->pdev = pdev;
15611 	set_bit(__I40E_DOWN, pf->state);
15612 
15613 	hw = &pf->hw;
15614 
15615 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15616 				I40E_MAX_CSR_SPACE);
15617 	/* We believe that the highest register to read is
15618 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15619 	 * is not less than that before mapping to prevent a
15620 	 * kernel panic.
15621 	 */
15622 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15623 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15624 			pf->ioremap_len);
15625 		err = -ENOMEM;
15626 		goto err_ioremap;
15627 	}
15628 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15629 	if (!hw->hw_addr) {
15630 		err = -EIO;
15631 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15632 			 (unsigned int)pci_resource_start(pdev, 0),
15633 			 pf->ioremap_len, err);
15634 		goto err_ioremap;
15635 	}
15636 	hw->vendor_id = pdev->vendor;
15637 	hw->device_id = pdev->device;
15638 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15639 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15640 	i40e_set_subsystem_device_id(hw);
15641 	hw->bus.device = PCI_SLOT(pdev->devfn);
15642 	hw->bus.func = PCI_FUNC(pdev->devfn);
15643 	hw->bus.bus_id = pdev->bus->number;
15644 
15645 	/* Select something other than the 802.1ad ethertype for the
15646 	 * switch to use internally and drop on ingress.
15647 	 */
15648 	hw->switch_tag = 0xffff;
15649 	hw->first_tag = ETH_P_8021AD;
15650 	hw->second_tag = ETH_P_8021Q;
15651 
15652 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15653 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15654 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15655 
15656 	/* set up the locks for the AQ, do this only once in probe
15657 	 * and destroy them only once in remove
15658 	 */
15659 	mutex_init(&hw->aq.asq_mutex);
15660 	mutex_init(&hw->aq.arq_mutex);
15661 
15662 	pf->msg_enable = netif_msg_init(debug,
15663 					NETIF_MSG_DRV |
15664 					NETIF_MSG_PROBE |
15665 					NETIF_MSG_LINK);
15666 	if (debug < -1)
15667 		pf->hw.debug_mask = debug;
15668 
15669 	/* do a special CORER for clearing PXE mode once at init */
15670 	if (hw->revision_id == 0 &&
15671 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15672 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15673 		i40e_flush(hw);
15674 		msleep(200);
15675 		pf->corer_count++;
15676 
15677 		i40e_clear_pxe_mode(hw);
15678 	}
15679 
15680 	/* Reset here to make sure all is clean and to define PF 'n' */
15681 	i40e_clear_hw(hw);
15682 
15683 	err = i40e_set_mac_type(hw);
15684 	if (err) {
15685 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15686 			 err);
15687 		goto err_pf_reset;
15688 	}
15689 
15690 	err = i40e_handle_resets(pf);
15691 	if (err)
15692 		goto err_pf_reset;
15693 
15694 	i40e_check_recovery_mode(pf);
15695 
15696 	if (is_kdump_kernel()) {
15697 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15698 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15699 	} else {
15700 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15701 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15702 	}
15703 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15704 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15705 
15706 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15707 		 "%s-%s:misc",
15708 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15709 
15710 	err = i40e_init_shared_code(hw);
15711 	if (err) {
15712 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15713 			 err);
15714 		goto err_pf_reset;
15715 	}
15716 
15717 	/* set up a default setting for link flow control */
15718 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15719 
15720 	err = i40e_init_adminq(hw);
15721 	if (err) {
15722 		if (err == -EIO)
15723 			dev_info(&pdev->dev,
15724 				 "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",
15725 				 hw->aq.api_maj_ver,
15726 				 hw->aq.api_min_ver,
15727 				 I40E_FW_API_VERSION_MAJOR,
15728 				 I40E_FW_MINOR_VERSION(hw));
15729 		else
15730 			dev_info(&pdev->dev,
15731 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15732 
15733 		goto err_pf_reset;
15734 	}
15735 	i40e_get_oem_version(hw);
15736 	i40e_get_pba_string(hw);
15737 
15738 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15739 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15740 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15741 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15742 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15743 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15744 		 hw->subsystem_device_id);
15745 
15746 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15747 				  I40E_FW_MINOR_VERSION(hw) + 1))
15748 		dev_dbg(&pdev->dev,
15749 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15750 			 hw->aq.api_maj_ver,
15751 			 hw->aq.api_min_ver,
15752 			 I40E_FW_API_VERSION_MAJOR,
15753 			 I40E_FW_MINOR_VERSION(hw));
15754 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15755 		dev_info(&pdev->dev,
15756 			 "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",
15757 			 hw->aq.api_maj_ver,
15758 			 hw->aq.api_min_ver,
15759 			 I40E_FW_API_VERSION_MAJOR,
15760 			 I40E_FW_MINOR_VERSION(hw));
15761 
15762 	i40e_verify_eeprom(pf);
15763 
15764 	/* Rev 0 hardware was never productized */
15765 	if (hw->revision_id < 1)
15766 		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");
15767 
15768 	i40e_clear_pxe_mode(hw);
15769 
15770 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15771 	if (err)
15772 		goto err_adminq_setup;
15773 
15774 	err = i40e_sw_init(pf);
15775 	if (err) {
15776 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15777 		goto err_sw_init;
15778 	}
15779 
15780 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15781 		return i40e_init_recovery_mode(pf, hw);
15782 
15783 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15784 				hw->func_caps.num_rx_qp, 0, 0);
15785 	if (err) {
15786 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15787 		goto err_init_lan_hmc;
15788 	}
15789 
15790 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15791 	if (err) {
15792 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15793 		err = -ENOENT;
15794 		goto err_configure_lan_hmc;
15795 	}
15796 
15797 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15798 	 * Ignore error return codes because if it was already disabled via
15799 	 * hardware settings this will fail
15800 	 */
15801 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15802 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15803 		i40e_aq_stop_lldp(hw, true, false, NULL);
15804 	}
15805 
15806 	/* allow a platform config to override the HW addr */
15807 	i40e_get_platform_mac_addr(pdev, pf);
15808 
15809 	if (!is_valid_ether_addr(hw->mac.addr)) {
15810 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15811 		err = -EIO;
15812 		goto err_mac_addr;
15813 	}
15814 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15815 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15816 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15817 	if (is_valid_ether_addr(hw->mac.port_addr))
15818 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15819 
15820 	i40e_ptp_alloc_pins(pf);
15821 	pci_set_drvdata(pdev, pf);
15822 	pci_save_state(pdev);
15823 
15824 #ifdef CONFIG_I40E_DCB
15825 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15826 	(!status &&
15827 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15828 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15829 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15830 	dev_info(&pdev->dev,
15831 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15832 			"FW LLDP is disabled\n" :
15833 			"FW LLDP is enabled\n");
15834 
15835 	/* Enable FW to write default DCB config on link-up */
15836 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15837 
15838 	err = i40e_init_pf_dcb(pf);
15839 	if (err) {
15840 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15841 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15842 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15843 		/* Continue without DCB enabled */
15844 	}
15845 #endif /* CONFIG_I40E_DCB */
15846 
15847 	/* set up periodic task facility */
15848 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15849 	pf->service_timer_period = HZ;
15850 
15851 	INIT_WORK(&pf->service_task, i40e_service_task);
15852 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15853 
15854 	/* NVM bit on means WoL disabled for the port */
15855 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15856 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15857 		pf->wol_en = false;
15858 	else
15859 		pf->wol_en = true;
15860 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15861 
15862 	/* set up the main switch operations */
15863 	i40e_determine_queue_usage(pf);
15864 	err = i40e_init_interrupt_scheme(pf);
15865 	if (err)
15866 		goto err_switch_setup;
15867 
15868 	/* Reduce Tx and Rx pairs for kdump
15869 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15870 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15871 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15872 	 */
15873 	if (is_kdump_kernel())
15874 		pf->num_lan_msix = 1;
15875 
15876 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15877 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15878 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15879 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15880 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15881 						    UDP_TUNNEL_TYPE_GENEVE;
15882 
15883 	/* The number of VSIs reported by the FW is the minimum guaranteed
15884 	 * to us; HW supports far more and we share the remaining pool with
15885 	 * the other PFs. We allocate space for more than the guarantee with
15886 	 * the understanding that we might not get them all later.
15887 	 */
15888 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15889 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15890 	else
15891 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15892 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15893 		dev_warn(&pf->pdev->dev,
15894 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15895 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15896 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15897 	}
15898 
15899 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15900 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15901 			  GFP_KERNEL);
15902 	if (!pf->vsi) {
15903 		err = -ENOMEM;
15904 		goto err_switch_setup;
15905 	}
15906 
15907 #ifdef CONFIG_PCI_IOV
15908 	/* prep for VF support */
15909 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15910 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15911 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15912 		if (pci_num_vf(pdev))
15913 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15914 	}
15915 #endif
15916 	err = i40e_setup_pf_switch(pf, false, false);
15917 	if (err) {
15918 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15919 		goto err_vsis;
15920 	}
15921 
15922 	vsi = i40e_pf_get_main_vsi(pf);
15923 	INIT_LIST_HEAD(&vsi->ch_list);
15924 
15925 	/* if FDIR VSI was set up, start it now */
15926 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
15927 	if (vsi)
15928 		i40e_vsi_open(vsi);
15929 
15930 	/* The driver only wants link up/down and module qualification
15931 	 * reports from firmware.  Note the negative logic.
15932 	 */
15933 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15934 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15935 					 I40E_AQ_EVENT_MEDIA_NA |
15936 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15937 	if (err)
15938 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15939 			 ERR_PTR(err),
15940 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15941 
15942 	/* VF MDD event logs are rate limited to one second intervals */
15943 	ratelimit_state_init(&pf->mdd_message_rate_limit, 1 * HZ, 1);
15944 
15945 	/* Reconfigure hardware for allowing smaller MSS in the case
15946 	 * of TSO, so that we avoid the MDD being fired and causing
15947 	 * a reset in the case of small MSS+TSO.
15948 	 */
15949 	val = rd32(hw, I40E_REG_MSS);
15950 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15951 		val &= ~I40E_REG_MSS_MIN_MASK;
15952 		val |= I40E_64BYTE_MSS;
15953 		wr32(hw, I40E_REG_MSS, val);
15954 	}
15955 
15956 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
15957 		msleep(75);
15958 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15959 		if (err)
15960 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
15961 				 ERR_PTR(err),
15962 				 i40e_aq_str(&pf->hw,
15963 					     pf->hw.aq.asq_last_status));
15964 	}
15965 	/* The main driver is (mostly) up and happy. We need to set this state
15966 	 * before setting up the misc vector or we get a race and the vector
15967 	 * ends up disabled forever.
15968 	 */
15969 	clear_bit(__I40E_DOWN, pf->state);
15970 
15971 	/* In case of MSIX we are going to setup the misc vector right here
15972 	 * to handle admin queue events etc. In case of legacy and MSI
15973 	 * the misc functionality and queue processing is combined in
15974 	 * the same vector and that gets setup at open.
15975 	 */
15976 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15977 		err = i40e_setup_misc_vector(pf);
15978 		if (err) {
15979 			dev_info(&pdev->dev,
15980 				 "setup of misc vector failed: %d\n", err);
15981 			i40e_cloud_filter_exit(pf);
15982 			i40e_fdir_teardown(pf);
15983 			goto err_vsis;
15984 		}
15985 	}
15986 
15987 #ifdef CONFIG_PCI_IOV
15988 	/* prep for VF support */
15989 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15990 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15991 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15992 		/* disable link interrupts for VFs */
15993 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15994 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15995 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15996 		i40e_flush(hw);
15997 
15998 		if (pci_num_vf(pdev)) {
15999 			dev_info(&pdev->dev,
16000 				 "Active VFs found, allocating resources.\n");
16001 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16002 			if (err)
16003 				dev_info(&pdev->dev,
16004 					 "Error %d allocating resources for existing VFs\n",
16005 					 err);
16006 		}
16007 	}
16008 #endif /* CONFIG_PCI_IOV */
16009 
16010 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16011 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16012 						      pf->num_iwarp_msix,
16013 						      I40E_IWARP_IRQ_PILE_ID);
16014 		if (pf->iwarp_base_vector < 0) {
16015 			dev_info(&pdev->dev,
16016 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16017 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16018 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
16019 		}
16020 	}
16021 
16022 	i40e_dbg_pf_init(pf);
16023 
16024 	/* tell the firmware that we're starting */
16025 	i40e_send_version(pf);
16026 
16027 	/* since everything's happy, start the service_task timer */
16028 	mod_timer(&pf->service_timer,
16029 		  round_jiffies(jiffies + pf->service_timer_period));
16030 
16031 	/* add this PF to client device list and launch a client service task */
16032 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16033 		err = i40e_lan_add_device(pf);
16034 		if (err)
16035 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16036 				 err);
16037 	}
16038 
16039 #define PCI_SPEED_SIZE 8
16040 #define PCI_WIDTH_SIZE 8
16041 	/* Devices on the IOSF bus do not have this information
16042 	 * and will report PCI Gen 1 x 1 by default so don't bother
16043 	 * checking them.
16044 	 */
16045 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16046 		char speed[PCI_SPEED_SIZE] = "Unknown";
16047 		char width[PCI_WIDTH_SIZE] = "Unknown";
16048 
16049 		/* Get the negotiated link width and speed from PCI config
16050 		 * space
16051 		 */
16052 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16053 					  &link_status);
16054 
16055 		i40e_set_pci_config_data(hw, link_status);
16056 
16057 		switch (hw->bus.speed) {
16058 		case i40e_bus_speed_8000:
16059 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16060 		case i40e_bus_speed_5000:
16061 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16062 		case i40e_bus_speed_2500:
16063 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16064 		default:
16065 			break;
16066 		}
16067 		switch (hw->bus.width) {
16068 		case i40e_bus_width_pcie_x8:
16069 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16070 		case i40e_bus_width_pcie_x4:
16071 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16072 		case i40e_bus_width_pcie_x2:
16073 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16074 		case i40e_bus_width_pcie_x1:
16075 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16076 		default:
16077 			break;
16078 		}
16079 
16080 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16081 			 speed, width);
16082 
16083 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16084 		    hw->bus.speed < i40e_bus_speed_8000) {
16085 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16086 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16087 		}
16088 	}
16089 
16090 	/* get the requested speeds from the fw */
16091 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16092 	if (err)
16093 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16094 			ERR_PTR(err),
16095 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16096 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16097 
16098 	/* set the FEC config due to the board capabilities */
16099 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16100 
16101 	/* get the supported phy types from the fw */
16102 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16103 	if (err)
16104 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16105 			ERR_PTR(err),
16106 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16107 
16108 	/* make sure the MFS hasn't been set lower than the default */
16109 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16110 	val = FIELD_GET(I40E_PRTGL_SAH_MFS_MASK,
16111 			rd32(&pf->hw, I40E_PRTGL_SAH));
16112 	if (val < MAX_FRAME_SIZE_DEFAULT)
16113 		dev_warn(&pdev->dev, "MFS for port %x (%d) has been set below the default (%d)\n",
16114 			 pf->hw.port, val, MAX_FRAME_SIZE_DEFAULT);
16115 
16116 	/* Add a filter to drop all Flow control frames from any VSI from being
16117 	 * transmitted. By doing so we stop a malicious VF from sending out
16118 	 * PAUSE or PFC frames and potentially controlling traffic for other
16119 	 * PF/VF VSIs.
16120 	 * The FW can still send Flow control frames if enabled.
16121 	 */
16122 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16123 						       pf->main_vsi_seid);
16124 
16125 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16126 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16127 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16128 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16129 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16130 	/* print a string summarizing features */
16131 	i40e_print_features(pf);
16132 
16133 	i40e_devlink_register(pf);
16134 
16135 	return 0;
16136 
16137 	/* Unwind what we've done if something failed in the setup */
16138 err_vsis:
16139 	set_bit(__I40E_DOWN, pf->state);
16140 	i40e_clear_interrupt_scheme(pf);
16141 	kfree(pf->vsi);
16142 err_switch_setup:
16143 	i40e_reset_interrupt_capability(pf);
16144 	timer_shutdown_sync(&pf->service_timer);
16145 err_mac_addr:
16146 err_configure_lan_hmc:
16147 	(void)i40e_shutdown_lan_hmc(hw);
16148 err_init_lan_hmc:
16149 	kfree(pf->qp_pile);
16150 err_sw_init:
16151 err_adminq_setup:
16152 err_pf_reset:
16153 	iounmap(hw->hw_addr);
16154 err_ioremap:
16155 	i40e_free_pf(pf);
16156 err_pf_alloc:
16157 	pci_release_mem_regions(pdev);
16158 err_pci_reg:
16159 err_dma:
16160 	pci_disable_device(pdev);
16161 	return err;
16162 }
16163 
16164 /**
16165  * i40e_remove - Device removal routine
16166  * @pdev: PCI device information struct
16167  *
16168  * i40e_remove is called by the PCI subsystem to alert the driver
16169  * that is should release a PCI device.  This could be caused by a
16170  * Hot-Plug event, or because the driver is going to be removed from
16171  * memory.
16172  **/
16173 static void i40e_remove(struct pci_dev *pdev)
16174 {
16175 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16176 	struct i40e_hw *hw = &pf->hw;
16177 	struct i40e_vsi *vsi;
16178 	struct i40e_veb *veb;
16179 	int ret_code;
16180 	int i;
16181 
16182 	i40e_devlink_unregister(pf);
16183 
16184 	i40e_dbg_pf_exit(pf);
16185 
16186 	i40e_ptp_stop(pf);
16187 
16188 	/* Disable RSS in hw */
16189 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16190 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16191 
16192 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16193 	 * flags, once they are set, i40e_rebuild should not be called as
16194 	 * i40e_prep_for_reset always returns early.
16195 	 */
16196 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16197 		usleep_range(1000, 2000);
16198 	set_bit(__I40E_IN_REMOVE, pf->state);
16199 
16200 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16201 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16202 		i40e_free_vfs(pf);
16203 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16204 	}
16205 	/* no more scheduling of any task */
16206 	set_bit(__I40E_SUSPENDED, pf->state);
16207 	set_bit(__I40E_DOWN, pf->state);
16208 	if (pf->service_timer.function)
16209 		timer_shutdown_sync(&pf->service_timer);
16210 	if (pf->service_task.func)
16211 		cancel_work_sync(&pf->service_task);
16212 
16213 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16214 		struct i40e_vsi *vsi = pf->vsi[0];
16215 
16216 		/* We know that we have allocated only one vsi for this PF,
16217 		 * it was just for registering netdevice, so the interface
16218 		 * could be visible in the 'ifconfig' output
16219 		 */
16220 		unregister_netdev(vsi->netdev);
16221 		free_netdev(vsi->netdev);
16222 
16223 		goto unmap;
16224 	}
16225 
16226 	/* Client close must be called explicitly here because the timer
16227 	 * has been stopped.
16228 	 */
16229 	i40e_notify_client_of_netdev_close(pf, false);
16230 
16231 	i40e_fdir_teardown(pf);
16232 
16233 	/* If there is a switch structure or any orphans, remove them.
16234 	 * This will leave only the PF's VSI remaining.
16235 	 */
16236 	i40e_pf_for_each_veb(pf, i, veb)
16237 		if (veb->uplink_seid == pf->mac_seid ||
16238 		    veb->uplink_seid == 0)
16239 			i40e_switch_branch_release(veb);
16240 
16241 	/* Now we can shutdown the PF's VSIs, just before we kill
16242 	 * adminq and hmc.
16243 	 */
16244 	i40e_pf_for_each_vsi(pf, i, vsi) {
16245 		i40e_vsi_close(vsi);
16246 		i40e_vsi_release(vsi);
16247 		pf->vsi[i] = NULL;
16248 	}
16249 
16250 	i40e_cloud_filter_exit(pf);
16251 
16252 	/* remove attached clients */
16253 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16254 		ret_code = i40e_lan_del_device(pf);
16255 		if (ret_code)
16256 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16257 				 ret_code);
16258 	}
16259 
16260 	/* shutdown and destroy the HMC */
16261 	if (hw->hmc.hmc_obj) {
16262 		ret_code = i40e_shutdown_lan_hmc(hw);
16263 		if (ret_code)
16264 			dev_warn(&pdev->dev,
16265 				 "Failed to destroy the HMC resources: %d\n",
16266 				 ret_code);
16267 	}
16268 
16269 unmap:
16270 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16271 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16272 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16273 		free_irq(pf->pdev->irq, pf);
16274 
16275 	/* shutdown the adminq */
16276 	i40e_shutdown_adminq(hw);
16277 
16278 	/* destroy the locks only once, here */
16279 	mutex_destroy(&hw->aq.arq_mutex);
16280 	mutex_destroy(&hw->aq.asq_mutex);
16281 
16282 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16283 	rtnl_lock();
16284 	i40e_clear_interrupt_scheme(pf);
16285 	i40e_pf_for_each_vsi(pf, i, vsi) {
16286 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16287 			i40e_vsi_clear_rings(vsi);
16288 
16289 		i40e_vsi_clear(vsi);
16290 		pf->vsi[i] = NULL;
16291 	}
16292 	rtnl_unlock();
16293 
16294 	i40e_pf_for_each_veb(pf, i, veb) {
16295 		kfree(veb);
16296 		pf->veb[i] = NULL;
16297 	}
16298 
16299 	kfree(pf->qp_pile);
16300 	kfree(pf->vsi);
16301 
16302 	iounmap(hw->hw_addr);
16303 	i40e_free_pf(pf);
16304 	pci_release_mem_regions(pdev);
16305 
16306 	pci_disable_device(pdev);
16307 }
16308 
16309 /**
16310  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16311  * using the mac_address_write admin q function
16312  * @pf: pointer to i40e_pf struct
16313  **/
16314 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16315 {
16316 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
16317 	struct i40e_hw *hw = &pf->hw;
16318 	u8 mac_addr[6];
16319 	u16 flags = 0;
16320 	int ret;
16321 
16322 	/* Get current MAC address in case it's an LAA */
16323 	if (main_vsi && main_vsi->netdev) {
16324 		ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
16325 	} else {
16326 		dev_err(&pf->pdev->dev,
16327 			"Failed to retrieve MAC address; using default\n");
16328 		ether_addr_copy(mac_addr, hw->mac.addr);
16329 	}
16330 
16331 	/* The FW expects the mac address write cmd to first be called with
16332 	 * one of these flags before calling it again with the multicast
16333 	 * enable flags.
16334 	 */
16335 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16336 
16337 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16338 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16339 
16340 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16341 	if (ret) {
16342 		dev_err(&pf->pdev->dev,
16343 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16344 		return;
16345 	}
16346 
16347 	flags = I40E_AQC_MC_MAG_EN
16348 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16349 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16350 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16351 	if (ret)
16352 		dev_err(&pf->pdev->dev,
16353 			"Failed to enable Multicast Magic Packet wake up\n");
16354 }
16355 
16356 /**
16357  * i40e_io_suspend - suspend all IO operations
16358  * @pf: pointer to i40e_pf struct
16359  *
16360  **/
16361 static int i40e_io_suspend(struct i40e_pf *pf)
16362 {
16363 	struct i40e_hw *hw = &pf->hw;
16364 
16365 	set_bit(__I40E_DOWN, pf->state);
16366 
16367 	/* Ensure service task will not be running */
16368 	timer_delete_sync(&pf->service_timer);
16369 	cancel_work_sync(&pf->service_task);
16370 
16371 	/* Client close must be called explicitly here because the timer
16372 	 * has been stopped.
16373 	 */
16374 	i40e_notify_client_of_netdev_close(pf, false);
16375 
16376 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16377 	    pf->wol_en)
16378 		i40e_enable_mc_magic_wake(pf);
16379 
16380 	/* Since we're going to destroy queues during the
16381 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16382 	 * whole section
16383 	 */
16384 	rtnl_lock();
16385 
16386 	i40e_prep_for_reset(pf);
16387 
16388 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16389 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16390 
16391 	/* Clear the interrupt scheme and release our IRQs so that the system
16392 	 * can safely hibernate even when there are a large number of CPUs.
16393 	 * Otherwise hibernation might fail when mapping all the vectors back
16394 	 * to CPU0.
16395 	 */
16396 	i40e_clear_interrupt_scheme(pf);
16397 
16398 	rtnl_unlock();
16399 
16400 	return 0;
16401 }
16402 
16403 /**
16404  * i40e_io_resume - resume IO operations
16405  * @pf: pointer to i40e_pf struct
16406  *
16407  **/
16408 static int i40e_io_resume(struct i40e_pf *pf)
16409 {
16410 	struct device *dev = &pf->pdev->dev;
16411 	int err;
16412 
16413 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16414 	 * since we're going to be restoring queues
16415 	 */
16416 	rtnl_lock();
16417 
16418 	/* We cleared the interrupt scheme when we suspended, so we need to
16419 	 * restore it now to resume device functionality.
16420 	 */
16421 	err = i40e_restore_interrupt_scheme(pf);
16422 	if (err) {
16423 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16424 			err);
16425 	}
16426 
16427 	clear_bit(__I40E_DOWN, pf->state);
16428 	i40e_reset_and_rebuild(pf, false, true);
16429 
16430 	rtnl_unlock();
16431 
16432 	/* Clear suspended state last after everything is recovered */
16433 	clear_bit(__I40E_SUSPENDED, pf->state);
16434 
16435 	/* Restart the service task */
16436 	mod_timer(&pf->service_timer,
16437 		  round_jiffies(jiffies + pf->service_timer_period));
16438 
16439 	return 0;
16440 }
16441 
16442 /**
16443  * i40e_pci_error_detected - warning that something funky happened in PCI land
16444  * @pdev: PCI device information struct
16445  * @error: the type of PCI error
16446  *
16447  * Called to warn that something happened and the error handling steps
16448  * are in progress.  Allows the driver to quiesce things, be ready for
16449  * remediation.
16450  **/
16451 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16452 						pci_channel_state_t error)
16453 {
16454 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16455 
16456 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16457 
16458 	if (!pf) {
16459 		dev_info(&pdev->dev,
16460 			 "Cannot recover - error happened during device probe\n");
16461 		return PCI_ERS_RESULT_DISCONNECT;
16462 	}
16463 
16464 	/* shutdown all operations */
16465 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16466 		i40e_io_suspend(pf);
16467 
16468 	/* Request a slot reset */
16469 	return PCI_ERS_RESULT_NEED_RESET;
16470 }
16471 
16472 /**
16473  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16474  * @pdev: PCI device information struct
16475  *
16476  * Called to find if the driver can work with the device now that
16477  * the pci slot has been reset.  If a basic connection seems good
16478  * (registers are readable and have sane content) then return a
16479  * happy little PCI_ERS_RESULT_xxx.
16480  **/
16481 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16482 {
16483 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16484 	pci_ers_result_t result;
16485 	u32 reg;
16486 
16487 	dev_dbg(&pdev->dev, "%s\n", __func__);
16488 	/* enable I/O and memory of the device  */
16489 	if (pci_enable_device(pdev)) {
16490 		dev_info(&pdev->dev,
16491 			 "Cannot re-enable PCI device after reset.\n");
16492 		result = PCI_ERS_RESULT_DISCONNECT;
16493 	} else {
16494 		pci_set_master(pdev);
16495 		pci_restore_state(pdev);
16496 		pci_save_state(pdev);
16497 		pci_wake_from_d3(pdev, false);
16498 
16499 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16500 		if (reg == 0)
16501 			result = PCI_ERS_RESULT_RECOVERED;
16502 		else
16503 			result = PCI_ERS_RESULT_DISCONNECT;
16504 	}
16505 
16506 	return result;
16507 }
16508 
16509 /**
16510  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16511  * @pdev: PCI device information struct
16512  */
16513 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16514 {
16515 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16516 
16517 	i40e_prep_for_reset(pf);
16518 }
16519 
16520 /**
16521  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16522  * @pdev: PCI device information struct
16523  */
16524 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16525 {
16526 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16527 
16528 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16529 		return;
16530 
16531 	i40e_reset_and_rebuild(pf, false, false);
16532 #ifdef CONFIG_PCI_IOV
16533 	i40e_restore_all_vfs_msi_state(pdev);
16534 #endif /* CONFIG_PCI_IOV */
16535 }
16536 
16537 /**
16538  * i40e_pci_error_resume - restart operations after PCI error recovery
16539  * @pdev: PCI device information struct
16540  *
16541  * Called to allow the driver to bring things back up after PCI error
16542  * and/or reset recovery has finished.
16543  **/
16544 static void i40e_pci_error_resume(struct pci_dev *pdev)
16545 {
16546 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16547 
16548 	dev_dbg(&pdev->dev, "%s\n", __func__);
16549 	if (test_bit(__I40E_SUSPENDED, pf->state))
16550 		return;
16551 
16552 	i40e_io_resume(pf);
16553 }
16554 
16555 /**
16556  * i40e_shutdown - PCI callback for shutting down
16557  * @pdev: PCI device information struct
16558  **/
16559 static void i40e_shutdown(struct pci_dev *pdev)
16560 {
16561 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16562 	struct i40e_hw *hw = &pf->hw;
16563 
16564 	set_bit(__I40E_SUSPENDED, pf->state);
16565 	set_bit(__I40E_DOWN, pf->state);
16566 
16567 	timer_delete_sync(&pf->service_timer);
16568 	cancel_work_sync(&pf->service_task);
16569 	i40e_cloud_filter_exit(pf);
16570 	i40e_fdir_teardown(pf);
16571 
16572 	/* Client close must be called explicitly here because the timer
16573 	 * has been stopped.
16574 	 */
16575 	i40e_notify_client_of_netdev_close(pf, false);
16576 
16577 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16578 	    pf->wol_en)
16579 		i40e_enable_mc_magic_wake(pf);
16580 
16581 	i40e_prep_for_reset(pf);
16582 
16583 	wr32(hw, I40E_PFPM_APM,
16584 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16585 	wr32(hw, I40E_PFPM_WUFC,
16586 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16587 
16588 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16589 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16590 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16591 		free_irq(pf->pdev->irq, pf);
16592 
16593 	/* Since we're going to destroy queues during the
16594 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16595 	 * whole section
16596 	 */
16597 	rtnl_lock();
16598 	i40e_clear_interrupt_scheme(pf);
16599 	rtnl_unlock();
16600 
16601 	if (system_state == SYSTEM_POWER_OFF) {
16602 		pci_wake_from_d3(pdev, pf->wol_en);
16603 		pci_set_power_state(pdev, PCI_D3hot);
16604 	}
16605 }
16606 
16607 /**
16608  * i40e_suspend - PM callback for moving to D3
16609  * @dev: generic device information structure
16610  **/
16611 static int i40e_suspend(struct device *dev)
16612 {
16613 	struct i40e_pf *pf = dev_get_drvdata(dev);
16614 
16615 	/* If we're already suspended, then there is nothing to do */
16616 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16617 		return 0;
16618 	return i40e_io_suspend(pf);
16619 }
16620 
16621 /**
16622  * i40e_resume - PM callback for waking up from D3
16623  * @dev: generic device information structure
16624  **/
16625 static int i40e_resume(struct device *dev)
16626 {
16627 	struct i40e_pf *pf = dev_get_drvdata(dev);
16628 
16629 	/* If we're not suspended, then there is nothing to do */
16630 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16631 		return 0;
16632 	return i40e_io_resume(pf);
16633 }
16634 
16635 static const struct pci_error_handlers i40e_err_handler = {
16636 	.error_detected = i40e_pci_error_detected,
16637 	.slot_reset = i40e_pci_error_slot_reset,
16638 	.reset_prepare = i40e_pci_error_reset_prepare,
16639 	.reset_done = i40e_pci_error_reset_done,
16640 	.resume = i40e_pci_error_resume,
16641 };
16642 
16643 static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16644 
16645 static struct pci_driver i40e_driver = {
16646 	.name     = i40e_driver_name,
16647 	.id_table = i40e_pci_tbl,
16648 	.probe    = i40e_probe,
16649 	.remove   = i40e_remove,
16650 	.driver.pm = pm_sleep_ptr(&i40e_pm_ops),
16651 	.shutdown = i40e_shutdown,
16652 	.err_handler = &i40e_err_handler,
16653 	.sriov_configure = i40e_pci_sriov_configure,
16654 };
16655 
16656 /**
16657  * i40e_init_module - Driver registration routine
16658  *
16659  * i40e_init_module is the first routine called when the driver is
16660  * loaded. All it does is register with the PCI subsystem.
16661  **/
16662 static int __init i40e_init_module(void)
16663 {
16664 	int err;
16665 
16666 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16667 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16668 
16669 	/* There is no need to throttle the number of active tasks because
16670 	 * each device limits its own task using a state bit for scheduling
16671 	 * the service task, and the device tasks do not interfere with each
16672 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16673 	 * since we need to be able to guarantee forward progress even under
16674 	 * memory pressure.
16675 	 */
16676 	i40e_wq = alloc_workqueue("%s", 0, 0, i40e_driver_name);
16677 	if (!i40e_wq) {
16678 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16679 		return -ENOMEM;
16680 	}
16681 
16682 	i40e_dbg_init();
16683 	err = pci_register_driver(&i40e_driver);
16684 	if (err) {
16685 		destroy_workqueue(i40e_wq);
16686 		i40e_dbg_exit();
16687 		return err;
16688 	}
16689 
16690 	return 0;
16691 }
16692 module_init(i40e_init_module);
16693 
16694 /**
16695  * i40e_exit_module - Driver exit cleanup routine
16696  *
16697  * i40e_exit_module is called just before the driver is removed
16698  * from memory.
16699  **/
16700 static void __exit i40e_exit_module(void)
16701 {
16702 	pci_unregister_driver(&i40e_driver);
16703 	destroy_workqueue(i40e_wq);
16704 	ida_destroy(&i40e_client_ida);
16705 	i40e_dbg_exit();
16706 }
16707 module_exit(i40e_exit_module);
16708