xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 5feaa7a07b85ebbef418ba4b80e4e0d23dc379f5)
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 libie_aq_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 == LIBIE_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 libie_aq_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 	struct i40e_cloud_filter *cfilter;
6001 	enum libie_aq_err last_aq_status;
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 != LIBIE_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 == LIBIE_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 = libie_aq_raw(&e->desc);
9423 	struct i40e_hw *hw = &pf->hw;
9424 	struct i40e_dcbx_config tmp_dcbx_cfg;
9425 	bool need_reconfig = false;
9426 	int ret = 0;
9427 	u8 type;
9428 
9429 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9430 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9431 	    (hw->phy.link_info.link_speed &
9432 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9433 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9434 		/* let firmware decide if the DCB should be disabled */
9435 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9436 
9437 	/* Not DCB capable or capability disabled */
9438 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9439 		return ret;
9440 
9441 	/* Ignore if event is not for Nearest Bridge */
9442 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9443 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9444 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9445 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9446 		return ret;
9447 
9448 	/* Check MIB Type and return if event for Remote MIB update */
9449 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9450 	dev_dbg(&pf->pdev->dev,
9451 		"LLDP event mib type %s\n", type ? "remote" : "local");
9452 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9453 		/* Update the remote cached instance and return */
9454 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9455 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9456 				&hw->remote_dcbx_config);
9457 		goto exit;
9458 	}
9459 
9460 	/* Store the old configuration */
9461 	tmp_dcbx_cfg = hw->local_dcbx_config;
9462 
9463 	/* Reset the old DCBx configuration data */
9464 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9465 	/* Get updated DCBX data from firmware */
9466 	ret = i40e_get_dcb_config(&pf->hw);
9467 	if (ret) {
9468 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9469 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9470 		    (hw->phy.link_info.link_speed &
9471 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9472 			dev_warn(&pf->pdev->dev,
9473 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9474 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9475 		} else {
9476 			dev_info(&pf->pdev->dev,
9477 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9478 				 ERR_PTR(ret),
9479 				 i40e_aq_str(&pf->hw,
9480 					     pf->hw.aq.asq_last_status));
9481 		}
9482 		goto exit;
9483 	}
9484 
9485 	/* No change detected in DCBX configs */
9486 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9487 		    sizeof(tmp_dcbx_cfg))) {
9488 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9489 		goto exit;
9490 	}
9491 
9492 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9493 					       &hw->local_dcbx_config);
9494 
9495 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9496 
9497 	if (!need_reconfig)
9498 		goto exit;
9499 
9500 	/* Enable DCB tagging only when more than one TC */
9501 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9502 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9503 	else
9504 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9505 
9506 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9507 	/* Reconfiguration needed quiesce all VSIs */
9508 	i40e_pf_quiesce_all_vsi(pf);
9509 
9510 	/* Changes in configuration update VEB/VSI */
9511 	i40e_dcb_reconfigure(pf);
9512 
9513 	ret = i40e_resume_port_tx(pf);
9514 
9515 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9516 	/* In case of error no point in resuming VSIs */
9517 	if (ret)
9518 		goto exit;
9519 
9520 	/* Wait for the PF's queues to be disabled */
9521 	ret = i40e_pf_wait_queues_disabled(pf);
9522 	if (ret) {
9523 		/* Schedule PF reset to recover */
9524 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9525 		i40e_service_event_schedule(pf);
9526 	} else {
9527 		i40e_pf_unquiesce_all_vsi(pf);
9528 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9529 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9530 	}
9531 
9532 exit:
9533 	return ret;
9534 }
9535 #endif /* CONFIG_I40E_DCB */
9536 
9537 /**
9538  * i40e_do_reset_safe - Protected reset path for userland calls.
9539  * @pf: board private structure
9540  * @reset_flags: which reset is requested
9541  *
9542  **/
9543 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9544 {
9545 	rtnl_lock();
9546 	i40e_do_reset(pf, reset_flags, true);
9547 	rtnl_unlock();
9548 }
9549 
9550 /**
9551  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9552  * @pf: board private structure
9553  * @e: event info posted on ARQ
9554  *
9555  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9556  * and VF queues
9557  **/
9558 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9559 					   struct i40e_arq_event_info *e)
9560 {
9561 	struct i40e_aqc_lan_overflow *data = libie_aq_raw(&e->desc);
9562 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9563 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9564 	struct i40e_hw *hw = &pf->hw;
9565 	struct i40e_vf *vf;
9566 	u16 vf_id;
9567 
9568 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9569 		queue, qtx_ctl);
9570 
9571 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9572 	    I40E_QTX_CTL_VF_QUEUE)
9573 		return;
9574 
9575 	/* Queue belongs to VF, find the VF and issue VF reset */
9576 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9577 	vf_id -= hw->func_caps.vf_base_id;
9578 	vf = &pf->vf[vf_id];
9579 	i40e_vc_notify_vf_reset(vf);
9580 	/* Allow VF to process pending reset notification */
9581 	msleep(20);
9582 	i40e_reset_vf(vf, false);
9583 }
9584 
9585 /**
9586  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9587  * @pf: board private structure
9588  **/
9589 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9590 {
9591 	u32 val, fcnt_prog;
9592 
9593 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9594 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9595 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9596 	return fcnt_prog;
9597 }
9598 
9599 /**
9600  * i40e_get_global_fd_count - Get total FD filters programmed on device
9601  * @pf: board private structure
9602  **/
9603 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9604 {
9605 	u32 val, fcnt_prog;
9606 
9607 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9608 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9609 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9610 	return fcnt_prog;
9611 }
9612 
9613 /**
9614  * i40e_reenable_fdir_sb - Restore FDir SB capability
9615  * @pf: board private structure
9616  **/
9617 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9618 {
9619 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9620 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9621 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9622 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9623 }
9624 
9625 /**
9626  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9627  * @pf: board private structure
9628  **/
9629 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9630 {
9631 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9632 		/* ATR uses the same filtering logic as SB rules. It only
9633 		 * functions properly if the input set mask is at the default
9634 		 * settings. It is safe to restore the default input set
9635 		 * because there are no active TCPv4 filter rules.
9636 		 */
9637 		i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9638 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9639 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9640 
9641 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9642 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9643 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9644 	}
9645 }
9646 
9647 /**
9648  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9649  * @pf: board private structure
9650  * @filter: FDir filter to remove
9651  */
9652 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9653 				       struct i40e_fdir_filter *filter)
9654 {
9655 	/* Update counters */
9656 	pf->fdir_pf_active_filters--;
9657 	pf->fd_inv = 0;
9658 
9659 	switch (filter->flow_type) {
9660 	case TCP_V4_FLOW:
9661 		pf->fd_tcp4_filter_cnt--;
9662 		break;
9663 	case UDP_V4_FLOW:
9664 		pf->fd_udp4_filter_cnt--;
9665 		break;
9666 	case SCTP_V4_FLOW:
9667 		pf->fd_sctp4_filter_cnt--;
9668 		break;
9669 	case TCP_V6_FLOW:
9670 		pf->fd_tcp6_filter_cnt--;
9671 		break;
9672 	case UDP_V6_FLOW:
9673 		pf->fd_udp6_filter_cnt--;
9674 		break;
9675 	case SCTP_V6_FLOW:
9676 		pf->fd_udp6_filter_cnt--;
9677 		break;
9678 	case IP_USER_FLOW:
9679 		switch (filter->ipl4_proto) {
9680 		case IPPROTO_TCP:
9681 			pf->fd_tcp4_filter_cnt--;
9682 			break;
9683 		case IPPROTO_UDP:
9684 			pf->fd_udp4_filter_cnt--;
9685 			break;
9686 		case IPPROTO_SCTP:
9687 			pf->fd_sctp4_filter_cnt--;
9688 			break;
9689 		case IPPROTO_IP:
9690 			pf->fd_ip4_filter_cnt--;
9691 			break;
9692 		}
9693 		break;
9694 	case IPV6_USER_FLOW:
9695 		switch (filter->ipl4_proto) {
9696 		case IPPROTO_TCP:
9697 			pf->fd_tcp6_filter_cnt--;
9698 			break;
9699 		case IPPROTO_UDP:
9700 			pf->fd_udp6_filter_cnt--;
9701 			break;
9702 		case IPPROTO_SCTP:
9703 			pf->fd_sctp6_filter_cnt--;
9704 			break;
9705 		case IPPROTO_IP:
9706 			pf->fd_ip6_filter_cnt--;
9707 			break;
9708 		}
9709 		break;
9710 	}
9711 
9712 	/* Remove the filter from the list and free memory */
9713 	hlist_del(&filter->fdir_node);
9714 	kfree(filter);
9715 }
9716 
9717 /**
9718  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9719  * @pf: board private structure
9720  **/
9721 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9722 {
9723 	struct i40e_fdir_filter *filter;
9724 	u32 fcnt_prog, fcnt_avail;
9725 	struct hlist_node *node;
9726 
9727 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9728 		return;
9729 
9730 	/* Check if we have enough room to re-enable FDir SB capability. */
9731 	fcnt_prog = i40e_get_global_fd_count(pf);
9732 	fcnt_avail = pf->fdir_pf_filter_count;
9733 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9734 	    (pf->fd_add_err == 0) ||
9735 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9736 		i40e_reenable_fdir_sb(pf);
9737 
9738 	/* We should wait for even more space before re-enabling ATR.
9739 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9740 	 * rules active.
9741 	 */
9742 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9743 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9744 		i40e_reenable_fdir_atr(pf);
9745 
9746 	/* if hw had a problem adding a filter, delete it */
9747 	if (pf->fd_inv > 0) {
9748 		hlist_for_each_entry_safe(filter, node,
9749 					  &pf->fdir_filter_list, fdir_node)
9750 			if (filter->fd_id == pf->fd_inv)
9751 				i40e_delete_invalid_filter(pf, filter);
9752 	}
9753 }
9754 
9755 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9756 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9757 /**
9758  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9759  * @pf: board private structure
9760  **/
9761 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9762 {
9763 	unsigned long min_flush_time;
9764 	int flush_wait_retry = 50;
9765 	bool disable_atr = false;
9766 	int fd_room;
9767 	int reg;
9768 
9769 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9770 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9771 		return;
9772 
9773 	/* If the flush is happening too quick and we have mostly SB rules we
9774 	 * should not re-enable ATR for some time.
9775 	 */
9776 	min_flush_time = pf->fd_flush_timestamp +
9777 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9778 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9779 
9780 	if (!(time_after(jiffies, min_flush_time)) &&
9781 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9782 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9783 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9784 		disable_atr = true;
9785 	}
9786 
9787 	pf->fd_flush_timestamp = jiffies;
9788 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9789 	/* flush all filters */
9790 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9791 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9792 	i40e_flush(&pf->hw);
9793 	pf->fd_flush_cnt++;
9794 	pf->fd_add_err = 0;
9795 	do {
9796 		/* Check FD flush status every 5-6msec */
9797 		usleep_range(5000, 6000);
9798 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9799 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9800 			break;
9801 	} while (flush_wait_retry--);
9802 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9803 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9804 	} else {
9805 		/* replay sideband filters */
9806 		i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
9807 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9808 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9809 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9810 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9811 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9812 	}
9813 }
9814 
9815 /**
9816  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9817  * @pf: board private structure
9818  **/
9819 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9820 {
9821 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9822 }
9823 
9824 /**
9825  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9826  * @pf: board private structure
9827  **/
9828 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9829 {
9830 
9831 	/* if interface is down do nothing */
9832 	if (test_bit(__I40E_DOWN, pf->state))
9833 		return;
9834 
9835 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9836 		i40e_fdir_flush_and_replay(pf);
9837 
9838 	i40e_fdir_check_and_reenable(pf);
9839 
9840 }
9841 
9842 /**
9843  * i40e_vsi_link_event - notify VSI of a link event
9844  * @vsi: vsi to be notified
9845  * @link_up: link up or down
9846  **/
9847 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9848 {
9849 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9850 		return;
9851 
9852 	switch (vsi->type) {
9853 	case I40E_VSI_MAIN:
9854 		if (!vsi->netdev || !vsi->netdev_registered)
9855 			break;
9856 
9857 		if (link_up) {
9858 			netif_carrier_on(vsi->netdev);
9859 			netif_tx_wake_all_queues(vsi->netdev);
9860 		} else {
9861 			netif_carrier_off(vsi->netdev);
9862 			netif_tx_stop_all_queues(vsi->netdev);
9863 		}
9864 		break;
9865 
9866 	case I40E_VSI_SRIOV:
9867 	case I40E_VSI_VMDQ2:
9868 	case I40E_VSI_CTRL:
9869 	case I40E_VSI_IWARP:
9870 	case I40E_VSI_MIRROR:
9871 	default:
9872 		/* there is no notification for other VSIs */
9873 		break;
9874 	}
9875 }
9876 
9877 /**
9878  * i40e_veb_link_event - notify elements on the veb of a link event
9879  * @veb: veb to be notified
9880  * @link_up: link up or down
9881  **/
9882 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9883 {
9884 	struct i40e_vsi *vsi;
9885 	struct i40e_pf *pf;
9886 	int i;
9887 
9888 	if (!veb || !veb->pf)
9889 		return;
9890 	pf = veb->pf;
9891 
9892 	/* Send link event to contained VSIs */
9893 	i40e_pf_for_each_vsi(pf, i, vsi)
9894 		if (vsi->uplink_seid == veb->seid)
9895 			i40e_vsi_link_event(vsi, link_up);
9896 }
9897 
9898 /**
9899  * i40e_link_event - Update netif_carrier status
9900  * @pf: board private structure
9901  **/
9902 static void i40e_link_event(struct i40e_pf *pf)
9903 {
9904 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
9905 	struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
9906 	u8 new_link_speed, old_link_speed;
9907 	bool new_link, old_link;
9908 	int status;
9909 #ifdef CONFIG_I40E_DCB
9910 	int err;
9911 #endif /* CONFIG_I40E_DCB */
9912 
9913 	/* set this to force the get_link_status call to refresh state */
9914 	pf->hw.phy.get_link_info = true;
9915 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9916 	status = i40e_get_link_status(&pf->hw, &new_link);
9917 
9918 	/* On success, disable temp link polling */
9919 	if (status == 0) {
9920 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9921 	} else {
9922 		/* Enable link polling temporarily until i40e_get_link_status
9923 		 * returns 0
9924 		 */
9925 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9926 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9927 			status);
9928 		return;
9929 	}
9930 
9931 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9932 	new_link_speed = pf->hw.phy.link_info.link_speed;
9933 
9934 	if (new_link == old_link &&
9935 	    new_link_speed == old_link_speed &&
9936 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9937 	     new_link == netif_carrier_ok(vsi->netdev)))
9938 		return;
9939 
9940 	if (!new_link && old_link)
9941 		pf->link_down_events++;
9942 
9943 	i40e_print_link_message(vsi, new_link);
9944 
9945 	/* Notify the base of the switch tree connected to
9946 	 * the link.  Floating VEBs are not notified.
9947 	 */
9948 	if (veb)
9949 		i40e_veb_link_event(veb, new_link);
9950 	else
9951 		i40e_vsi_link_event(vsi, new_link);
9952 
9953 	if (pf->vf)
9954 		i40e_vc_notify_link_state(pf);
9955 
9956 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9957 		i40e_ptp_set_increment(pf);
9958 #ifdef CONFIG_I40E_DCB
9959 	if (new_link == old_link)
9960 		return;
9961 	/* Not SW DCB so firmware will take care of default settings */
9962 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9963 		return;
9964 
9965 	/* We cover here only link down, as after link up in case of SW DCB
9966 	 * SW LLDP agent will take care of setting it up
9967 	 */
9968 	if (!new_link) {
9969 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9970 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9971 		err = i40e_dcb_sw_default_config(pf);
9972 		if (err) {
9973 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9974 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9975 		} else {
9976 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9977 				       DCB_CAP_DCBX_VER_IEEE;
9978 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9979 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9980 		}
9981 	}
9982 #endif /* CONFIG_I40E_DCB */
9983 }
9984 
9985 /**
9986  * i40e_watchdog_subtask - periodic checks not using event driven response
9987  * @pf: board private structure
9988  **/
9989 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9990 {
9991 	struct i40e_vsi *vsi;
9992 	struct i40e_veb *veb;
9993 	int i;
9994 
9995 	/* if interface is down do nothing */
9996 	if (test_bit(__I40E_DOWN, pf->state) ||
9997 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9998 		return;
9999 
10000 	/* make sure we don't do these things too often */
10001 	if (time_before(jiffies, (pf->service_timer_previous +
10002 				  pf->service_timer_period)))
10003 		return;
10004 	pf->service_timer_previous = jiffies;
10005 
10006 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10007 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10008 		i40e_link_event(pf);
10009 
10010 	/* Update the stats for active netdevs so the network stack
10011 	 * can look at updated numbers whenever it cares to
10012 	 */
10013 	i40e_pf_for_each_vsi(pf, i, vsi)
10014 		if (vsi->netdev)
10015 			i40e_update_stats(vsi);
10016 
10017 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10018 		/* Update the stats for the active switching components */
10019 		i40e_pf_for_each_veb(pf, i, veb)
10020 			i40e_update_veb_stats(veb);
10021 	}
10022 
10023 	i40e_ptp_rx_hang(pf);
10024 	i40e_ptp_tx_hang(pf);
10025 }
10026 
10027 /**
10028  * i40e_reset_subtask - Set up for resetting the device and driver
10029  * @pf: board private structure
10030  **/
10031 static void i40e_reset_subtask(struct i40e_pf *pf)
10032 {
10033 	u32 reset_flags = 0;
10034 
10035 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10036 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10037 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10038 	}
10039 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10040 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10041 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10042 	}
10043 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10044 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10045 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10046 	}
10047 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10048 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10049 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10050 	}
10051 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10052 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10053 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10054 	}
10055 
10056 	/* If there's a recovery already waiting, it takes
10057 	 * precedence before starting a new reset sequence.
10058 	 */
10059 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10060 		i40e_prep_for_reset(pf);
10061 		i40e_reset(pf);
10062 		i40e_rebuild(pf, false, false);
10063 	}
10064 
10065 	/* If we're already down or resetting, just bail */
10066 	if (reset_flags &&
10067 	    !test_bit(__I40E_DOWN, pf->state) &&
10068 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10069 		i40e_do_reset(pf, reset_flags, false);
10070 	}
10071 }
10072 
10073 /**
10074  * i40e_handle_link_event - Handle link event
10075  * @pf: board private structure
10076  * @e: event info posted on ARQ
10077  **/
10078 static void i40e_handle_link_event(struct i40e_pf *pf,
10079 				   struct i40e_arq_event_info *e)
10080 {
10081 	struct i40e_aqc_get_link_status *status = libie_aq_raw(&e->desc);
10082 
10083 	/* Do a new status request to re-enable LSE reporting
10084 	 * and load new status information into the hw struct
10085 	 * This completely ignores any state information
10086 	 * in the ARQ event info, instead choosing to always
10087 	 * issue the AQ update link status command.
10088 	 */
10089 	i40e_link_event(pf);
10090 
10091 	/* Check if module meets thermal requirements */
10092 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10093 		dev_err(&pf->pdev->dev,
10094 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10095 		dev_err(&pf->pdev->dev,
10096 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10097 	} else {
10098 		/* check for unqualified module, if link is down, suppress
10099 		 * the message if link was forced to be down.
10100 		 */
10101 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10102 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10103 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10104 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10105 			dev_err(&pf->pdev->dev,
10106 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10107 			dev_err(&pf->pdev->dev,
10108 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10109 		}
10110 	}
10111 }
10112 
10113 /**
10114  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10115  * @pf: board private structure
10116  **/
10117 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10118 {
10119 	struct i40e_arq_event_info event;
10120 	struct i40e_hw *hw = &pf->hw;
10121 	u16 pending, i = 0;
10122 	u16 opcode;
10123 	u32 oldval;
10124 	int ret;
10125 	u32 val;
10126 
10127 	/* Do not run clean AQ when PF reset fails */
10128 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10129 		return;
10130 
10131 	/* check for error indications */
10132 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10133 	oldval = val;
10134 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10135 		if (hw->debug_mask & I40E_DEBUG_AQ)
10136 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10137 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10138 	}
10139 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10140 		if (hw->debug_mask & I40E_DEBUG_AQ)
10141 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10142 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10143 		pf->arq_overflows++;
10144 	}
10145 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10146 		if (hw->debug_mask & I40E_DEBUG_AQ)
10147 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10148 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10149 	}
10150 	if (oldval != val)
10151 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10152 
10153 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10154 	oldval = val;
10155 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10156 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10157 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10158 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10159 	}
10160 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10161 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10162 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10163 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10164 	}
10165 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10166 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10167 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10168 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10169 	}
10170 	if (oldval != val)
10171 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10172 
10173 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10174 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10175 	if (!event.msg_buf)
10176 		return;
10177 
10178 	do {
10179 		ret = i40e_clean_arq_element(hw, &event, &pending);
10180 		if (ret == -EALREADY)
10181 			break;
10182 		else if (ret) {
10183 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10184 			break;
10185 		}
10186 
10187 		opcode = le16_to_cpu(event.desc.opcode);
10188 		switch (opcode) {
10189 
10190 		case i40e_aqc_opc_get_link_status:
10191 			rtnl_lock();
10192 			i40e_handle_link_event(pf, &event);
10193 			rtnl_unlock();
10194 			break;
10195 		case i40e_aqc_opc_send_msg_to_pf:
10196 			ret = i40e_vc_process_vf_msg(pf,
10197 					le16_to_cpu(event.desc.retval),
10198 					le32_to_cpu(event.desc.cookie_high),
10199 					le32_to_cpu(event.desc.cookie_low),
10200 					event.msg_buf,
10201 					event.msg_len);
10202 			break;
10203 		case i40e_aqc_opc_lldp_update_mib:
10204 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10205 #ifdef CONFIG_I40E_DCB
10206 			rtnl_lock();
10207 			i40e_handle_lldp_event(pf, &event);
10208 			rtnl_unlock();
10209 #endif /* CONFIG_I40E_DCB */
10210 			break;
10211 		case i40e_aqc_opc_event_lan_overflow:
10212 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10213 			i40e_handle_lan_overflow_event(pf, &event);
10214 			break;
10215 		case i40e_aqc_opc_send_msg_to_peer:
10216 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10217 			break;
10218 		case i40e_aqc_opc_nvm_erase:
10219 		case i40e_aqc_opc_nvm_update:
10220 		case i40e_aqc_opc_oem_post_update:
10221 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10222 				   "ARQ NVM operation 0x%04x completed\n",
10223 				   opcode);
10224 			break;
10225 		default:
10226 			dev_info(&pf->pdev->dev,
10227 				 "ARQ: Unknown event 0x%04x ignored\n",
10228 				 opcode);
10229 			break;
10230 		}
10231 	} while (i++ < I40E_AQ_WORK_LIMIT);
10232 
10233 	if (i < I40E_AQ_WORK_LIMIT)
10234 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10235 
10236 	/* re-enable Admin queue interrupt cause */
10237 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10238 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10239 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10240 	i40e_flush(hw);
10241 
10242 	kfree(event.msg_buf);
10243 }
10244 
10245 /**
10246  * i40e_verify_eeprom - make sure eeprom is good to use
10247  * @pf: board private structure
10248  **/
10249 static void i40e_verify_eeprom(struct i40e_pf *pf)
10250 {
10251 	int err;
10252 
10253 	err = i40e_diag_eeprom_test(&pf->hw);
10254 	if (err) {
10255 		/* retry in case of garbage read */
10256 		err = i40e_diag_eeprom_test(&pf->hw);
10257 		if (err) {
10258 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10259 				 err);
10260 			set_bit(__I40E_BAD_EEPROM, pf->state);
10261 		}
10262 	}
10263 
10264 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10265 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10266 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10267 	}
10268 }
10269 
10270 /**
10271  * i40e_enable_pf_switch_lb
10272  * @pf: pointer to the PF structure
10273  *
10274  * enable switch loop back or die - no point in a return value
10275  **/
10276 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10277 {
10278 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10279 	struct i40e_vsi_context ctxt;
10280 	int ret;
10281 
10282 	ctxt.seid = pf->main_vsi_seid;
10283 	ctxt.pf_num = pf->hw.pf_id;
10284 	ctxt.vf_num = 0;
10285 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10286 	if (ret) {
10287 		dev_info(&pf->pdev->dev,
10288 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10289 			 ERR_PTR(ret),
10290 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10291 		return;
10292 	}
10293 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10294 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10295 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10296 
10297 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10298 	if (ret) {
10299 		dev_info(&pf->pdev->dev,
10300 			 "update vsi switch failed, err %pe aq_err %s\n",
10301 			 ERR_PTR(ret),
10302 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10303 	}
10304 }
10305 
10306 /**
10307  * i40e_disable_pf_switch_lb
10308  * @pf: pointer to the PF structure
10309  *
10310  * disable switch loop back or die - no point in a return value
10311  **/
10312 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10313 {
10314 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10315 	struct i40e_vsi_context ctxt;
10316 	int ret;
10317 
10318 	ctxt.seid = pf->main_vsi_seid;
10319 	ctxt.pf_num = pf->hw.pf_id;
10320 	ctxt.vf_num = 0;
10321 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10322 	if (ret) {
10323 		dev_info(&pf->pdev->dev,
10324 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10325 			 ERR_PTR(ret),
10326 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10327 		return;
10328 	}
10329 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10330 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10331 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10332 
10333 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10334 	if (ret) {
10335 		dev_info(&pf->pdev->dev,
10336 			 "update vsi switch failed, err %pe aq_err %s\n",
10337 			 ERR_PTR(ret),
10338 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10339 	}
10340 }
10341 
10342 /**
10343  * i40e_config_bridge_mode - Configure the HW bridge mode
10344  * @veb: pointer to the bridge instance
10345  *
10346  * Configure the loop back mode for the LAN VSI that is downlink to the
10347  * specified HW bridge instance. It is expected this function is called
10348  * when a new HW bridge is instantiated.
10349  **/
10350 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10351 {
10352 	struct i40e_pf *pf = veb->pf;
10353 
10354 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10355 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10356 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10357 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10358 		i40e_disable_pf_switch_lb(pf);
10359 	else
10360 		i40e_enable_pf_switch_lb(pf);
10361 }
10362 
10363 /**
10364  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10365  * @veb: pointer to the VEB instance
10366  *
10367  * This is a function that builds the attached VSIs. We track the connections
10368  * through our own index numbers because the seid's from the HW could change
10369  * across the reset.
10370  **/
10371 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10372 {
10373 	struct i40e_vsi *ctl_vsi = NULL;
10374 	struct i40e_pf *pf = veb->pf;
10375 	struct i40e_vsi *vsi;
10376 	int v, ret;
10377 
10378 	/* As we do not maintain PV (port virtualizer) switch element then
10379 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10380 	 * and its control VSI is the main one.
10381 	 */
10382 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10383 		dev_err(&pf->pdev->dev,
10384 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10385 		return -ENOENT;
10386 	}
10387 
10388 	if (veb->uplink_seid == pf->mac_seid) {
10389 		/* Check that the LAN VSI has VEB owning flag set */
10390 		ctl_vsi = i40e_pf_get_main_vsi(pf);
10391 
10392 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10393 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10394 			dev_err(&pf->pdev->dev,
10395 				"Invalid control VSI for VEB %d\n", veb->idx);
10396 			return -ENOENT;
10397 		}
10398 
10399 		/* Add the control VSI to switch */
10400 		ret = i40e_add_vsi(ctl_vsi);
10401 		if (ret) {
10402 			dev_err(&pf->pdev->dev,
10403 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10404 				veb->idx, ret);
10405 			return ret;
10406 		}
10407 
10408 		i40e_vsi_reset_stats(ctl_vsi);
10409 	}
10410 
10411 	/* create the VEB in the switch and move the VSI onto the VEB */
10412 	ret = i40e_add_veb(veb, ctl_vsi);
10413 	if (ret)
10414 		return ret;
10415 
10416 	if (veb->uplink_seid) {
10417 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10418 			veb->bridge_mode = BRIDGE_MODE_VEB;
10419 		else
10420 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10421 		i40e_config_bridge_mode(veb);
10422 	}
10423 
10424 	/* create the remaining VSIs attached to this VEB */
10425 	i40e_pf_for_each_vsi(pf, v, vsi) {
10426 		if (vsi == ctl_vsi)
10427 			continue;
10428 
10429 		if (vsi->veb_idx == veb->idx) {
10430 			vsi->uplink_seid = veb->seid;
10431 			ret = i40e_add_vsi(vsi);
10432 			if (ret) {
10433 				dev_info(&pf->pdev->dev,
10434 					 "rebuild of vsi_idx %d failed: %d\n",
10435 					 v, ret);
10436 				return ret;
10437 			}
10438 			i40e_vsi_reset_stats(vsi);
10439 		}
10440 	}
10441 
10442 	return ret;
10443 }
10444 
10445 /**
10446  * i40e_get_capabilities - get info about the HW
10447  * @pf: the PF struct
10448  * @list_type: AQ capability to be queried
10449  **/
10450 static int i40e_get_capabilities(struct i40e_pf *pf,
10451 				 enum i40e_admin_queue_opc list_type)
10452 {
10453 	struct libie_aqc_list_caps_elem *cap_buf;
10454 	u16 data_size;
10455 	int buf_len;
10456 	int err;
10457 
10458 	buf_len = 40 * sizeof(struct libie_aqc_list_caps_elem);
10459 	do {
10460 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10461 		if (!cap_buf)
10462 			return -ENOMEM;
10463 
10464 		/* this loads the data into the hw struct for us */
10465 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10466 						    &data_size, list_type,
10467 						    NULL);
10468 		/* data loaded, buffer no longer needed */
10469 		kfree(cap_buf);
10470 
10471 		if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_ENOMEM) {
10472 			/* retry with a larger buffer */
10473 			buf_len = data_size;
10474 		} else if (pf->hw.aq.asq_last_status != LIBIE_AQ_RC_OK || err) {
10475 			dev_info(&pf->pdev->dev,
10476 				 "capability discovery failed, err %pe aq_err %s\n",
10477 				 ERR_PTR(err),
10478 				 i40e_aq_str(&pf->hw,
10479 					     pf->hw.aq.asq_last_status));
10480 			return -ENODEV;
10481 		}
10482 	} while (err);
10483 
10484 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10485 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10486 			dev_info(&pf->pdev->dev,
10487 				 "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",
10488 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10489 				 pf->hw.func_caps.num_msix_vectors,
10490 				 pf->hw.func_caps.num_msix_vectors_vf,
10491 				 pf->hw.func_caps.fd_filters_guaranteed,
10492 				 pf->hw.func_caps.fd_filters_best_effort,
10493 				 pf->hw.func_caps.num_tx_qp,
10494 				 pf->hw.func_caps.num_vsis);
10495 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10496 			dev_info(&pf->pdev->dev,
10497 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10498 				 pf->hw.dev_caps.switch_mode,
10499 				 pf->hw.dev_caps.valid_functions);
10500 			dev_info(&pf->pdev->dev,
10501 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10502 				 pf->hw.dev_caps.sr_iov_1_1,
10503 				 pf->hw.dev_caps.num_vfs);
10504 			dev_info(&pf->pdev->dev,
10505 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10506 				 pf->hw.dev_caps.num_vsis,
10507 				 pf->hw.dev_caps.num_rx_qp,
10508 				 pf->hw.dev_caps.num_tx_qp);
10509 		}
10510 	}
10511 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10512 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10513 		       + pf->hw.func_caps.num_vfs)
10514 		if (pf->hw.revision_id == 0 &&
10515 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10516 			dev_info(&pf->pdev->dev,
10517 				 "got num_vsis %d, setting num_vsis to %d\n",
10518 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10519 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10520 		}
10521 	}
10522 	return 0;
10523 }
10524 
10525 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10526 
10527 /**
10528  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10529  * @pf: board private structure
10530  **/
10531 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10532 {
10533 	struct i40e_vsi *main_vsi, *vsi;
10534 
10535 	/* quick workaround for an NVM issue that leaves a critical register
10536 	 * uninitialized
10537 	 */
10538 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10539 		static const u32 hkey[] = {
10540 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10541 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10542 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10543 			0x95b3a76d};
10544 		int i;
10545 
10546 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10547 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10548 	}
10549 
10550 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10551 		return;
10552 
10553 	/* find existing VSI and see if it needs configuring */
10554 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10555 
10556 	/* create a new VSI if none exists */
10557 	if (!vsi) {
10558 		main_vsi = i40e_pf_get_main_vsi(pf);
10559 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
10560 		if (!vsi) {
10561 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10562 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10563 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10564 			return;
10565 		}
10566 	}
10567 
10568 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10569 }
10570 
10571 /**
10572  * i40e_fdir_teardown - release the Flow Director resources
10573  * @pf: board private structure
10574  **/
10575 static void i40e_fdir_teardown(struct i40e_pf *pf)
10576 {
10577 	struct i40e_vsi *vsi;
10578 
10579 	i40e_fdir_filter_exit(pf);
10580 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10581 	if (vsi)
10582 		i40e_vsi_release(vsi);
10583 }
10584 
10585 /**
10586  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10587  * @vsi: PF main vsi
10588  * @seid: seid of main or channel VSIs
10589  *
10590  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10591  * existed before reset
10592  **/
10593 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10594 {
10595 	struct i40e_cloud_filter *cfilter;
10596 	struct i40e_pf *pf = vsi->back;
10597 	struct hlist_node *node;
10598 	int ret;
10599 
10600 	/* Add cloud filters back if they exist */
10601 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10602 				  cloud_node) {
10603 		if (cfilter->seid != seid)
10604 			continue;
10605 
10606 		if (cfilter->dst_port)
10607 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10608 								true);
10609 		else
10610 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10611 
10612 		if (ret) {
10613 			dev_dbg(&pf->pdev->dev,
10614 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10615 				ERR_PTR(ret),
10616 				i40e_aq_str(&pf->hw,
10617 					    pf->hw.aq.asq_last_status));
10618 			return ret;
10619 		}
10620 	}
10621 	return 0;
10622 }
10623 
10624 /**
10625  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10626  * @vsi: PF main vsi
10627  *
10628  * Rebuilds channel VSIs if they existed before reset
10629  **/
10630 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10631 {
10632 	struct i40e_channel *ch, *ch_tmp;
10633 	int ret;
10634 
10635 	if (list_empty(&vsi->ch_list))
10636 		return 0;
10637 
10638 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10639 		if (!ch->initialized)
10640 			break;
10641 		/* Proceed with creation of channel (VMDq2) VSI */
10642 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10643 		if (ret) {
10644 			dev_info(&vsi->back->pdev->dev,
10645 				 "failed to rebuild channels using uplink_seid %u\n",
10646 				 vsi->uplink_seid);
10647 			return ret;
10648 		}
10649 		/* Reconfigure TX queues using QTX_CTL register */
10650 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10651 		if (ret) {
10652 			dev_info(&vsi->back->pdev->dev,
10653 				 "failed to configure TX rings for channel %u\n",
10654 				 ch->seid);
10655 			return ret;
10656 		}
10657 		/* update 'next_base_queue' */
10658 		vsi->next_base_queue = vsi->next_base_queue +
10659 							ch->num_queue_pairs;
10660 		if (ch->max_tx_rate) {
10661 			u64 credits = ch->max_tx_rate;
10662 
10663 			if (i40e_set_bw_limit(vsi, ch->seid,
10664 					      ch->max_tx_rate))
10665 				return -EINVAL;
10666 
10667 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10668 			dev_dbg(&vsi->back->pdev->dev,
10669 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10670 				ch->max_tx_rate,
10671 				credits,
10672 				ch->seid);
10673 		}
10674 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10675 		if (ret) {
10676 			dev_dbg(&vsi->back->pdev->dev,
10677 				"Failed to rebuild cloud filters for channel VSI %u\n",
10678 				ch->seid);
10679 			return ret;
10680 		}
10681 	}
10682 	return 0;
10683 }
10684 
10685 /**
10686  * i40e_clean_xps_state - clean xps state for every tx_ring
10687  * @vsi: ptr to the VSI
10688  **/
10689 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10690 {
10691 	int i;
10692 
10693 	if (vsi->tx_rings)
10694 		for (i = 0; i < vsi->num_queue_pairs; i++)
10695 			if (vsi->tx_rings[i])
10696 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10697 					  vsi->tx_rings[i]->state);
10698 }
10699 
10700 /**
10701  * i40e_prep_for_reset - prep for the core to reset
10702  * @pf: board private structure
10703  *
10704  * Close up the VFs and other things in prep for PF Reset.
10705   **/
10706 static void i40e_prep_for_reset(struct i40e_pf *pf)
10707 {
10708 	struct i40e_hw *hw = &pf->hw;
10709 	struct i40e_vsi *vsi;
10710 	int ret = 0;
10711 	u32 v;
10712 
10713 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10714 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10715 		return;
10716 	if (i40e_check_asq_alive(&pf->hw))
10717 		i40e_vc_notify_reset(pf);
10718 
10719 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10720 
10721 	/* quiesce the VSIs and their queues that are not already DOWN */
10722 	i40e_pf_quiesce_all_vsi(pf);
10723 
10724 	i40e_pf_for_each_vsi(pf, v, vsi) {
10725 		i40e_clean_xps_state(vsi);
10726 		vsi->seid = 0;
10727 	}
10728 
10729 	i40e_shutdown_adminq(&pf->hw);
10730 
10731 	/* call shutdown HMC */
10732 	if (hw->hmc.hmc_obj) {
10733 		ret = i40e_shutdown_lan_hmc(hw);
10734 		if (ret)
10735 			dev_warn(&pf->pdev->dev,
10736 				 "shutdown_lan_hmc failed: %d\n", ret);
10737 	}
10738 
10739 	/* Save the current PTP time so that we can restore the time after the
10740 	 * reset completes.
10741 	 */
10742 	i40e_ptp_save_hw_time(pf);
10743 }
10744 
10745 /**
10746  * i40e_send_version - update firmware with driver version
10747  * @pf: PF struct
10748  */
10749 static void i40e_send_version(struct i40e_pf *pf)
10750 {
10751 	struct i40e_driver_version dv;
10752 
10753 	dv.major_version = 0xff;
10754 	dv.minor_version = 0xff;
10755 	dv.build_version = 0xff;
10756 	dv.subbuild_version = 0;
10757 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10758 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10759 }
10760 
10761 /**
10762  * i40e_get_oem_version - get OEM specific version information
10763  * @hw: pointer to the hardware structure
10764  **/
10765 static void i40e_get_oem_version(struct i40e_hw *hw)
10766 {
10767 	u16 block_offset = 0xffff;
10768 	u16 block_length = 0;
10769 	u16 capabilities = 0;
10770 	u16 gen_snap = 0;
10771 	u16 release = 0;
10772 
10773 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10774 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10775 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10776 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10777 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10778 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10779 #define I40E_NVM_OEM_LENGTH			3
10780 
10781 	/* Check if pointer to OEM version block is valid. */
10782 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10783 	if (block_offset == 0xffff)
10784 		return;
10785 
10786 	/* Check if OEM version block has correct length. */
10787 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10788 			   &block_length);
10789 	if (block_length < I40E_NVM_OEM_LENGTH)
10790 		return;
10791 
10792 	/* Check if OEM version format is as expected. */
10793 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10794 			   &capabilities);
10795 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10796 		return;
10797 
10798 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10799 			   &gen_snap);
10800 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10801 			   &release);
10802 	hw->nvm.oem_ver =
10803 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10804 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10805 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10806 }
10807 
10808 /**
10809  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10810  * @pf: board private structure
10811  **/
10812 static int i40e_reset(struct i40e_pf *pf)
10813 {
10814 	struct i40e_hw *hw = &pf->hw;
10815 	int ret;
10816 
10817 	ret = i40e_pf_reset(hw);
10818 	if (ret) {
10819 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10820 		set_bit(__I40E_RESET_FAILED, pf->state);
10821 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10822 	} else {
10823 		pf->pfr_count++;
10824 	}
10825 	return ret;
10826 }
10827 
10828 /**
10829  * i40e_rebuild - rebuild using a saved config
10830  * @pf: board private structure
10831  * @reinit: if the Main VSI needs to re-initialized.
10832  * @lock_acquired: indicates whether or not the lock has been acquired
10833  * before this function was called.
10834  **/
10835 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10836 {
10837 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10838 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10839 	struct i40e_hw *hw = &pf->hw;
10840 	struct i40e_veb *veb;
10841 	int ret;
10842 	u32 val;
10843 	int v;
10844 
10845 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10846 	    is_recovery_mode_reported)
10847 		i40e_set_ethtool_ops(vsi->netdev);
10848 
10849 	if (test_bit(__I40E_DOWN, pf->state) &&
10850 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10851 		goto clear_recovery;
10852 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10853 
10854 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10855 	ret = i40e_init_adminq(&pf->hw);
10856 	if (ret) {
10857 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10858 			 ERR_PTR(ret),
10859 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10860 		goto clear_recovery;
10861 	}
10862 	i40e_get_oem_version(&pf->hw);
10863 
10864 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10865 		/* The following delay is necessary for firmware update. */
10866 		mdelay(1000);
10867 	}
10868 
10869 	/* re-verify the eeprom if we just had an EMP reset */
10870 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10871 		i40e_verify_eeprom(pf);
10872 
10873 	/* if we are going out of or into recovery mode we have to act
10874 	 * accordingly with regard to resources initialization
10875 	 * and deinitialization
10876 	 */
10877 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10878 		if (i40e_get_capabilities(pf,
10879 					  i40e_aqc_opc_list_func_capabilities))
10880 			goto end_unlock;
10881 
10882 		if (is_recovery_mode_reported) {
10883 			/* we're staying in recovery mode so we'll reinitialize
10884 			 * misc vector here
10885 			 */
10886 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10887 				goto end_unlock;
10888 		} else {
10889 			if (!lock_acquired)
10890 				rtnl_lock();
10891 			/* we're going out of recovery mode so we'll free
10892 			 * the IRQ allocated specifically for recovery mode
10893 			 * and restore the interrupt scheme
10894 			 */
10895 			free_irq(pf->pdev->irq, pf);
10896 			i40e_clear_interrupt_scheme(pf);
10897 			if (i40e_restore_interrupt_scheme(pf))
10898 				goto end_unlock;
10899 		}
10900 
10901 		/* tell the firmware that we're starting */
10902 		i40e_send_version(pf);
10903 
10904 		/* bail out in case recovery mode was detected, as there is
10905 		 * no need for further configuration.
10906 		 */
10907 		goto end_unlock;
10908 	}
10909 
10910 	i40e_clear_pxe_mode(hw);
10911 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10912 	if (ret)
10913 		goto end_core_reset;
10914 
10915 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10916 				hw->func_caps.num_rx_qp, 0, 0);
10917 	if (ret) {
10918 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10919 		goto end_core_reset;
10920 	}
10921 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10922 	if (ret) {
10923 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10924 		goto end_core_reset;
10925 	}
10926 
10927 #ifdef CONFIG_I40E_DCB
10928 	/* Enable FW to write a default DCB config on link-up
10929 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10930 	 * is not supported with new link speed
10931 	 */
10932 	if (i40e_is_tc_mqprio_enabled(pf)) {
10933 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10934 	} else {
10935 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10936 		    (hw->phy.link_info.link_speed &
10937 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10938 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10939 			dev_warn(&pf->pdev->dev,
10940 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10941 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10942 		} else {
10943 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10944 			ret = i40e_init_pf_dcb(pf);
10945 			if (ret) {
10946 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10947 					 ret);
10948 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10949 				/* Continue without DCB enabled */
10950 			}
10951 		}
10952 	}
10953 
10954 #endif /* CONFIG_I40E_DCB */
10955 	if (!lock_acquired)
10956 		rtnl_lock();
10957 	ret = i40e_setup_pf_switch(pf, reinit, true);
10958 	if (ret)
10959 		goto end_unlock;
10960 
10961 	/* The driver only wants link up/down and module qualification
10962 	 * reports from firmware.  Note the negative logic.
10963 	 */
10964 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10965 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10966 					 I40E_AQ_EVENT_MEDIA_NA |
10967 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10968 	if (ret)
10969 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10970 			 ERR_PTR(ret),
10971 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10972 
10973 	/* Rebuild the VSIs and VEBs that existed before reset.
10974 	 * They are still in our local switch element arrays, so only
10975 	 * need to rebuild the switch model in the HW.
10976 	 *
10977 	 * If there were VEBs but the reconstitution failed, we'll try
10978 	 * to recover minimal use by getting the basic PF VSI working.
10979 	 */
10980 	if (vsi->uplink_seid != pf->mac_seid) {
10981 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10982 
10983 		/* Rebuild VEBs */
10984 		i40e_pf_for_each_veb(pf, v, veb) {
10985 			ret = i40e_reconstitute_veb(veb);
10986 			if (!ret)
10987 				continue;
10988 
10989 			/* If Main VEB failed, we're in deep doodoo,
10990 			 * so give up rebuilding the switch and set up
10991 			 * for minimal rebuild of PF VSI.
10992 			 * If orphan failed, we'll report the error
10993 			 * but try to keep going.
10994 			 */
10995 			if (veb->uplink_seid == pf->mac_seid) {
10996 				dev_info(&pf->pdev->dev,
10997 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10998 					 ret);
10999 				vsi->uplink_seid = pf->mac_seid;
11000 				break;
11001 			} else if (veb->uplink_seid == 0) {
11002 				dev_info(&pf->pdev->dev,
11003 					 "rebuild of orphan VEB failed: %d\n",
11004 					 ret);
11005 			}
11006 		}
11007 	}
11008 
11009 	if (vsi->uplink_seid == pf->mac_seid) {
11010 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11011 		/* no VEB, so rebuild only the Main VSI */
11012 		ret = i40e_add_vsi(vsi);
11013 		if (ret) {
11014 			dev_info(&pf->pdev->dev,
11015 				 "rebuild of Main VSI failed: %d\n", ret);
11016 			goto end_unlock;
11017 		}
11018 	}
11019 
11020 	if (vsi->mqprio_qopt.max_rate[0]) {
11021 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11022 						  vsi->mqprio_qopt.max_rate[0]);
11023 		u64 credits = 0;
11024 
11025 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11026 		if (ret)
11027 			goto end_unlock;
11028 
11029 		credits = max_tx_rate;
11030 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11031 		dev_dbg(&vsi->back->pdev->dev,
11032 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11033 			max_tx_rate,
11034 			credits,
11035 			vsi->seid);
11036 	}
11037 
11038 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11039 	if (ret)
11040 		goto end_unlock;
11041 
11042 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11043 	 * for this main VSI if they exist
11044 	 */
11045 	ret = i40e_rebuild_channels(vsi);
11046 	if (ret)
11047 		goto end_unlock;
11048 
11049 	/* Reconfigure hardware for allowing smaller MSS in the case
11050 	 * of TSO, so that we avoid the MDD being fired and causing
11051 	 * a reset in the case of small MSS+TSO.
11052 	 */
11053 #define I40E_REG_MSS          0x000E64DC
11054 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11055 #define I40E_64BYTE_MSS       0x400000
11056 	val = rd32(hw, I40E_REG_MSS);
11057 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11058 		val &= ~I40E_REG_MSS_MIN_MASK;
11059 		val |= I40E_64BYTE_MSS;
11060 		wr32(hw, I40E_REG_MSS, val);
11061 	}
11062 
11063 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11064 		msleep(75);
11065 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11066 		if (ret)
11067 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11068 				 ERR_PTR(ret),
11069 				 i40e_aq_str(&pf->hw,
11070 					     pf->hw.aq.asq_last_status));
11071 	}
11072 	/* reinit the misc interrupt */
11073 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11074 		ret = i40e_setup_misc_vector(pf);
11075 		if (ret)
11076 			goto end_unlock;
11077 	}
11078 
11079 	/* Add a filter to drop all Flow control frames from any VSI from being
11080 	 * transmitted. By doing so we stop a malicious VF from sending out
11081 	 * PAUSE or PFC frames and potentially controlling traffic for other
11082 	 * PF/VF VSIs.
11083 	 * The FW can still send Flow control frames if enabled.
11084 	 */
11085 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11086 						       pf->main_vsi_seid);
11087 
11088 	/* restart the VSIs that were rebuilt and running before the reset */
11089 	i40e_pf_unquiesce_all_vsi(pf);
11090 
11091 	/* Release the RTNL lock before we start resetting VFs */
11092 	if (!lock_acquired)
11093 		rtnl_unlock();
11094 
11095 	/* Restore promiscuous settings */
11096 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11097 	if (ret)
11098 		dev_warn(&pf->pdev->dev,
11099 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11100 			 pf->cur_promisc ? "on" : "off",
11101 			 ERR_PTR(ret),
11102 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11103 
11104 	i40e_reset_all_vfs(pf, true);
11105 
11106 	/* tell the firmware that we're starting */
11107 	i40e_send_version(pf);
11108 
11109 	/* We've already released the lock, so don't do it again */
11110 	goto end_core_reset;
11111 
11112 end_unlock:
11113 	if (!lock_acquired)
11114 		rtnl_unlock();
11115 end_core_reset:
11116 	clear_bit(__I40E_RESET_FAILED, pf->state);
11117 clear_recovery:
11118 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11119 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11120 }
11121 
11122 /**
11123  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11124  * @pf: board private structure
11125  * @reinit: if the Main VSI needs to re-initialized.
11126  * @lock_acquired: indicates whether or not the lock has been acquired
11127  * before this function was called.
11128  **/
11129 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11130 				   bool lock_acquired)
11131 {
11132 	int ret;
11133 
11134 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11135 		return;
11136 	/* Now we wait for GRST to settle out.
11137 	 * We don't have to delete the VEBs or VSIs from the hw switch
11138 	 * because the reset will make them disappear.
11139 	 */
11140 	ret = i40e_reset(pf);
11141 	if (!ret)
11142 		i40e_rebuild(pf, reinit, lock_acquired);
11143 	else
11144 		dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
11145 }
11146 
11147 /**
11148  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11149  * @pf: board private structure
11150  *
11151  * Close up the VFs and other things in prep for a Core Reset,
11152  * then get ready to rebuild the world.
11153  * @lock_acquired: indicates whether or not the lock has been acquired
11154  * before this function was called.
11155  **/
11156 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11157 {
11158 	i40e_prep_for_reset(pf);
11159 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11160 }
11161 
11162 /**
11163  * i40e_print_vf_mdd_event - print VF Tx/Rx malicious driver detect event
11164  * @pf: board private structure
11165  * @vf: pointer to the VF structure
11166  * @is_tx: true - for Tx event, false - for  Rx
11167  */
11168 static void i40e_print_vf_mdd_event(struct i40e_pf *pf, struct i40e_vf *vf,
11169 				    bool is_tx)
11170 {
11171 	dev_err(&pf->pdev->dev, is_tx ?
11172 		"%lld Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n" :
11173 		"%lld Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n",
11174 		is_tx ? vf->mdd_tx_events.count : vf->mdd_rx_events.count,
11175 		pf->hw.pf_id,
11176 		vf->vf_id,
11177 		vf->default_lan_addr.addr,
11178 		str_on_off(test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)));
11179 }
11180 
11181 /**
11182  * i40e_print_vfs_mdd_events - print VFs malicious driver detect event
11183  * @pf: pointer to the PF structure
11184  *
11185  * Called from i40e_handle_mdd_event to rate limit and print VFs MDD events.
11186  */
11187 static void i40e_print_vfs_mdd_events(struct i40e_pf *pf)
11188 {
11189 	unsigned int i;
11190 
11191 	/* check that there are pending MDD events to print */
11192 	if (!test_and_clear_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state))
11193 		return;
11194 
11195 	if (!__ratelimit(&pf->mdd_message_rate_limit))
11196 		return;
11197 
11198 	for (i = 0; i < pf->num_alloc_vfs; i++) {
11199 		struct i40e_vf *vf = &pf->vf[i];
11200 		bool is_printed = false;
11201 
11202 		/* only print Rx MDD event message if there are new events */
11203 		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
11204 			vf->mdd_rx_events.last_printed = vf->mdd_rx_events.count;
11205 			i40e_print_vf_mdd_event(pf, vf, false);
11206 			is_printed = true;
11207 		}
11208 
11209 		/* only print Tx MDD event message if there are new events */
11210 		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
11211 			vf->mdd_tx_events.last_printed = vf->mdd_tx_events.count;
11212 			i40e_print_vf_mdd_event(pf, vf, true);
11213 			is_printed = true;
11214 		}
11215 
11216 		if (is_printed && !test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags))
11217 			dev_info(&pf->pdev->dev,
11218 				 "Use PF Control I/F to re-enable the VF #%d\n",
11219 				 i);
11220 	}
11221 }
11222 
11223 /**
11224  * i40e_handle_mdd_event
11225  * @pf: pointer to the PF structure
11226  *
11227  * Called from the MDD irq handler to identify possibly malicious vfs
11228  **/
11229 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11230 {
11231 	struct i40e_hw *hw = &pf->hw;
11232 	bool mdd_detected = false;
11233 	struct i40e_vf *vf;
11234 	u32 reg;
11235 	int i;
11236 
11237 	if (!test_and_clear_bit(__I40E_MDD_EVENT_PENDING, pf->state)) {
11238 		/* Since the VF MDD event logging is rate limited, check if
11239 		 * there are pending MDD events.
11240 		 */
11241 		i40e_print_vfs_mdd_events(pf);
11242 		return;
11243 	}
11244 
11245 	/* find what triggered the MDD event */
11246 	reg = rd32(hw, I40E_GL_MDET_TX);
11247 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11248 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11249 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11250 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11251 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11252 				pf->hw.func_caps.base_queue;
11253 		if (netif_msg_tx_err(pf))
11254 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11255 				 event, queue, pf_num, vf_num);
11256 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11257 		mdd_detected = true;
11258 	}
11259 	reg = rd32(hw, I40E_GL_MDET_RX);
11260 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11261 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11262 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11263 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11264 				pf->hw.func_caps.base_queue;
11265 		if (netif_msg_rx_err(pf))
11266 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11267 				 event, queue, func);
11268 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11269 		mdd_detected = true;
11270 	}
11271 
11272 	if (mdd_detected) {
11273 		reg = rd32(hw, I40E_PF_MDET_TX);
11274 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11275 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11276 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11277 		}
11278 		reg = rd32(hw, I40E_PF_MDET_RX);
11279 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11280 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11281 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11282 		}
11283 	}
11284 
11285 	/* see if one of the VFs needs its hand slapped */
11286 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11287 		bool is_mdd_on_tx = false;
11288 		bool is_mdd_on_rx = false;
11289 
11290 		vf = &(pf->vf[i]);
11291 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11292 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11293 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11294 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11295 			vf->mdd_tx_events.count++;
11296 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11297 			is_mdd_on_tx = true;
11298 		}
11299 
11300 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11301 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11302 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11303 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11304 			vf->mdd_rx_events.count++;
11305 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11306 			is_mdd_on_rx = true;
11307 		}
11308 
11309 		if ((is_mdd_on_tx || is_mdd_on_rx) &&
11310 		    test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
11311 			/* VF MDD event counters will be cleared by
11312 			 * reset, so print the event prior to reset.
11313 			 */
11314 			if (is_mdd_on_rx)
11315 				i40e_print_vf_mdd_event(pf, vf, false);
11316 			if (is_mdd_on_tx)
11317 				i40e_print_vf_mdd_event(pf, vf, true);
11318 
11319 			i40e_vc_reset_vf(vf, true);
11320 		}
11321 	}
11322 
11323 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11324 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11325 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11326 	i40e_flush(hw);
11327 
11328 	i40e_print_vfs_mdd_events(pf);
11329 }
11330 
11331 /**
11332  * i40e_service_task - Run the driver's async subtasks
11333  * @work: pointer to work_struct containing our data
11334  **/
11335 static void i40e_service_task(struct work_struct *work)
11336 {
11337 	struct i40e_pf *pf = container_of(work,
11338 					  struct i40e_pf,
11339 					  service_task);
11340 	unsigned long start_time = jiffies;
11341 
11342 	/* don't bother with service tasks if a reset is in progress */
11343 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11344 	    test_bit(__I40E_SUSPENDED, pf->state))
11345 		return;
11346 
11347 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11348 		return;
11349 
11350 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11351 		i40e_detect_recover_hung(pf);
11352 		i40e_sync_filters_subtask(pf);
11353 		i40e_reset_subtask(pf);
11354 		i40e_handle_mdd_event(pf);
11355 		i40e_vc_process_vflr_event(pf);
11356 		i40e_watchdog_subtask(pf);
11357 		i40e_fdir_reinit_subtask(pf);
11358 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11359 			/* Client subtask will reopen next time through. */
11360 			i40e_notify_client_of_netdev_close(pf, true);
11361 		} else {
11362 			i40e_client_subtask(pf);
11363 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11364 					       pf->state))
11365 				i40e_notify_client_of_l2_param_changes(pf);
11366 		}
11367 		i40e_sync_filters_subtask(pf);
11368 	} else {
11369 		i40e_reset_subtask(pf);
11370 	}
11371 
11372 	i40e_clean_adminq_subtask(pf);
11373 
11374 	/* flush memory to make sure state is correct before next watchdog */
11375 	smp_mb__before_atomic();
11376 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11377 
11378 	/* If the tasks have taken longer than one timer cycle or there
11379 	 * is more work to be done, reschedule the service task now
11380 	 * rather than wait for the timer to tick again.
11381 	 */
11382 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11383 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11384 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11385 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11386 		i40e_service_event_schedule(pf);
11387 }
11388 
11389 /**
11390  * i40e_service_timer - timer callback
11391  * @t: timer list pointer
11392  **/
11393 static void i40e_service_timer(struct timer_list *t)
11394 {
11395 	struct i40e_pf *pf = timer_container_of(pf, t, service_timer);
11396 
11397 	mod_timer(&pf->service_timer,
11398 		  round_jiffies(jiffies + pf->service_timer_period));
11399 	i40e_service_event_schedule(pf);
11400 }
11401 
11402 /**
11403  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11404  * @vsi: the VSI being configured
11405  **/
11406 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11407 {
11408 	struct i40e_pf *pf = vsi->back;
11409 
11410 	switch (vsi->type) {
11411 	case I40E_VSI_MAIN:
11412 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11413 		if (!vsi->num_tx_desc)
11414 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11415 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11416 		if (!vsi->num_rx_desc)
11417 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11418 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11419 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11420 			vsi->num_q_vectors = pf->num_lan_msix;
11421 		else
11422 			vsi->num_q_vectors = 1;
11423 
11424 		break;
11425 
11426 	case I40E_VSI_FDIR:
11427 		vsi->alloc_queue_pairs = 1;
11428 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11429 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11430 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11431 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11432 		vsi->num_q_vectors = pf->num_fdsb_msix;
11433 		break;
11434 
11435 	case I40E_VSI_VMDQ2:
11436 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11437 		if (!vsi->num_tx_desc)
11438 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11439 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11440 		if (!vsi->num_rx_desc)
11441 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11442 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11443 		vsi->num_q_vectors = pf->num_vmdq_msix;
11444 		break;
11445 
11446 	case I40E_VSI_SRIOV:
11447 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11448 		if (!vsi->num_tx_desc)
11449 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11450 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11451 		if (!vsi->num_rx_desc)
11452 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11453 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11454 		break;
11455 
11456 	default:
11457 		WARN_ON(1);
11458 		return -ENODATA;
11459 	}
11460 
11461 	if (is_kdump_kernel()) {
11462 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11463 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11464 	}
11465 
11466 	return 0;
11467 }
11468 
11469 /**
11470  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11471  * @vsi: VSI pointer
11472  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11473  *
11474  * On error: returns error code (negative)
11475  * On success: returns 0
11476  **/
11477 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11478 {
11479 	struct i40e_ring **next_rings;
11480 	int size;
11481 	int ret = 0;
11482 
11483 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11484 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11485 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11486 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11487 	if (!vsi->tx_rings)
11488 		return -ENOMEM;
11489 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11490 	if (i40e_enabled_xdp_vsi(vsi)) {
11491 		vsi->xdp_rings = next_rings;
11492 		next_rings += vsi->alloc_queue_pairs;
11493 	}
11494 	vsi->rx_rings = next_rings;
11495 
11496 	if (alloc_qvectors) {
11497 		/* allocate memory for q_vector pointers */
11498 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11499 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11500 		if (!vsi->q_vectors) {
11501 			ret = -ENOMEM;
11502 			goto err_vectors;
11503 		}
11504 	}
11505 	return ret;
11506 
11507 err_vectors:
11508 	kfree(vsi->tx_rings);
11509 	return ret;
11510 }
11511 
11512 /**
11513  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11514  * @pf: board private structure
11515  * @type: type of VSI
11516  *
11517  * On error: returns error code (negative)
11518  * On success: returns vsi index in PF (positive)
11519  **/
11520 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11521 {
11522 	int ret = -ENODEV;
11523 	struct i40e_vsi *vsi;
11524 	int vsi_idx;
11525 	int i;
11526 
11527 	/* Need to protect the allocation of the VSIs at the PF level */
11528 	mutex_lock(&pf->switch_mutex);
11529 
11530 	/* VSI list may be fragmented if VSI creation/destruction has
11531 	 * been happening.  We can afford to do a quick scan to look
11532 	 * for any free VSIs in the list.
11533 	 *
11534 	 * find next empty vsi slot, looping back around if necessary
11535 	 */
11536 	i = pf->next_vsi;
11537 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11538 		i++;
11539 	if (i >= pf->num_alloc_vsi) {
11540 		i = 0;
11541 		while (i < pf->next_vsi && pf->vsi[i])
11542 			i++;
11543 	}
11544 
11545 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11546 		vsi_idx = i;             /* Found one! */
11547 	} else {
11548 		ret = -ENODEV;
11549 		goto unlock_pf;  /* out of VSI slots! */
11550 	}
11551 	pf->next_vsi = ++i;
11552 
11553 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11554 	if (!vsi) {
11555 		ret = -ENOMEM;
11556 		goto unlock_pf;
11557 	}
11558 	vsi->type = type;
11559 	vsi->back = pf;
11560 	set_bit(__I40E_VSI_DOWN, vsi->state);
11561 	vsi->flags = 0;
11562 	vsi->idx = vsi_idx;
11563 	vsi->int_rate_limit = 0;
11564 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11565 				pf->rss_table_size : 64;
11566 	vsi->netdev_registered = false;
11567 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11568 	hash_init(vsi->mac_filter_hash);
11569 	vsi->irqs_ready = false;
11570 
11571 	if (type == I40E_VSI_MAIN) {
11572 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11573 		if (!vsi->af_xdp_zc_qps)
11574 			goto err_rings;
11575 	}
11576 
11577 	ret = i40e_set_num_rings_in_vsi(vsi);
11578 	if (ret)
11579 		goto err_rings;
11580 
11581 	ret = i40e_vsi_alloc_arrays(vsi, true);
11582 	if (ret)
11583 		goto err_rings;
11584 
11585 	/* Setup default MSIX irq handler for VSI */
11586 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11587 
11588 	/* Initialize VSI lock */
11589 	spin_lock_init(&vsi->mac_filter_hash_lock);
11590 	pf->vsi[vsi_idx] = vsi;
11591 	ret = vsi_idx;
11592 	goto unlock_pf;
11593 
11594 err_rings:
11595 	bitmap_free(vsi->af_xdp_zc_qps);
11596 	pf->next_vsi = i - 1;
11597 	kfree(vsi);
11598 unlock_pf:
11599 	mutex_unlock(&pf->switch_mutex);
11600 	return ret;
11601 }
11602 
11603 /**
11604  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11605  * @vsi: VSI pointer
11606  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11607  *
11608  * On error: returns error code (negative)
11609  * On success: returns 0
11610  **/
11611 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11612 {
11613 	/* free the ring and vector containers */
11614 	if (free_qvectors) {
11615 		kfree(vsi->q_vectors);
11616 		vsi->q_vectors = NULL;
11617 	}
11618 	kfree(vsi->tx_rings);
11619 	vsi->tx_rings = NULL;
11620 	vsi->rx_rings = NULL;
11621 	vsi->xdp_rings = NULL;
11622 }
11623 
11624 /**
11625  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11626  * and lookup table
11627  * @vsi: Pointer to VSI structure
11628  */
11629 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11630 {
11631 	if (!vsi)
11632 		return;
11633 
11634 	kfree(vsi->rss_hkey_user);
11635 	vsi->rss_hkey_user = NULL;
11636 
11637 	kfree(vsi->rss_lut_user);
11638 	vsi->rss_lut_user = NULL;
11639 }
11640 
11641 /**
11642  * i40e_vsi_clear - Deallocate the VSI provided
11643  * @vsi: the VSI being un-configured
11644  **/
11645 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11646 {
11647 	struct i40e_pf *pf;
11648 
11649 	if (!vsi)
11650 		return 0;
11651 
11652 	if (!vsi->back)
11653 		goto free_vsi;
11654 	pf = vsi->back;
11655 
11656 	mutex_lock(&pf->switch_mutex);
11657 	if (!pf->vsi[vsi->idx]) {
11658 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11659 			vsi->idx, vsi->idx, vsi->type);
11660 		goto unlock_vsi;
11661 	}
11662 
11663 	if (pf->vsi[vsi->idx] != vsi) {
11664 		dev_err(&pf->pdev->dev,
11665 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11666 			pf->vsi[vsi->idx]->idx,
11667 			pf->vsi[vsi->idx]->type,
11668 			vsi->idx, vsi->type);
11669 		goto unlock_vsi;
11670 	}
11671 
11672 	/* updates the PF for this cleared vsi */
11673 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11674 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11675 
11676 	bitmap_free(vsi->af_xdp_zc_qps);
11677 	i40e_vsi_free_arrays(vsi, true);
11678 	i40e_clear_rss_config_user(vsi);
11679 
11680 	pf->vsi[vsi->idx] = NULL;
11681 	if (vsi->idx < pf->next_vsi)
11682 		pf->next_vsi = vsi->idx;
11683 
11684 unlock_vsi:
11685 	mutex_unlock(&pf->switch_mutex);
11686 free_vsi:
11687 	kfree(vsi);
11688 
11689 	return 0;
11690 }
11691 
11692 /**
11693  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11694  * @vsi: the VSI being cleaned
11695  **/
11696 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11697 {
11698 	int i;
11699 
11700 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11701 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11702 			kfree_rcu(vsi->tx_rings[i], rcu);
11703 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11704 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11705 			if (vsi->xdp_rings)
11706 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11707 		}
11708 	}
11709 }
11710 
11711 /**
11712  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11713  * @vsi: the VSI being configured
11714  **/
11715 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11716 {
11717 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11718 	struct i40e_pf *pf = vsi->back;
11719 	struct i40e_ring *ring;
11720 
11721 	/* Set basic values in the rings to be used later during open() */
11722 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11723 		/* allocate space for both Tx and Rx in one shot */
11724 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11725 		if (!ring)
11726 			goto err_out;
11727 
11728 		ring->queue_index = i;
11729 		ring->reg_idx = vsi->base_queue + i;
11730 		ring->ring_active = false;
11731 		ring->vsi = vsi;
11732 		ring->netdev = vsi->netdev;
11733 		ring->dev = &pf->pdev->dev;
11734 		ring->count = vsi->num_tx_desc;
11735 		ring->size = 0;
11736 		ring->dcb_tc = 0;
11737 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11738 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11739 		ring->itr_setting = pf->tx_itr_default;
11740 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11741 
11742 		if (!i40e_enabled_xdp_vsi(vsi))
11743 			goto setup_rx;
11744 
11745 		ring->queue_index = vsi->alloc_queue_pairs + i;
11746 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11747 		ring->ring_active = false;
11748 		ring->vsi = vsi;
11749 		ring->netdev = NULL;
11750 		ring->dev = &pf->pdev->dev;
11751 		ring->count = vsi->num_tx_desc;
11752 		ring->size = 0;
11753 		ring->dcb_tc = 0;
11754 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11755 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11756 		set_ring_xdp(ring);
11757 		ring->itr_setting = pf->tx_itr_default;
11758 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11759 
11760 setup_rx:
11761 		ring->queue_index = i;
11762 		ring->reg_idx = vsi->base_queue + i;
11763 		ring->ring_active = false;
11764 		ring->vsi = vsi;
11765 		ring->netdev = vsi->netdev;
11766 		ring->dev = &pf->pdev->dev;
11767 		ring->count = vsi->num_rx_desc;
11768 		ring->size = 0;
11769 		ring->dcb_tc = 0;
11770 		ring->itr_setting = pf->rx_itr_default;
11771 		WRITE_ONCE(vsi->rx_rings[i], ring);
11772 	}
11773 
11774 	return 0;
11775 
11776 err_out:
11777 	i40e_vsi_clear_rings(vsi);
11778 	return -ENOMEM;
11779 }
11780 
11781 /**
11782  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11783  * @pf: board private structure
11784  * @vectors: the number of MSI-X vectors to request
11785  *
11786  * Returns the number of vectors reserved, or error
11787  **/
11788 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11789 {
11790 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11791 					I40E_MIN_MSIX, vectors);
11792 	if (vectors < 0) {
11793 		dev_info(&pf->pdev->dev,
11794 			 "MSI-X vector reservation failed: %d\n", vectors);
11795 		vectors = 0;
11796 	}
11797 
11798 	return vectors;
11799 }
11800 
11801 /**
11802  * i40e_init_msix - Setup the MSIX capability
11803  * @pf: board private structure
11804  *
11805  * Work with the OS to set up the MSIX vectors needed.
11806  *
11807  * Returns the number of vectors reserved or negative on failure
11808  **/
11809 static int i40e_init_msix(struct i40e_pf *pf)
11810 {
11811 	struct i40e_hw *hw = &pf->hw;
11812 	int cpus, extra_vectors;
11813 	int vectors_left;
11814 	int v_budget, i;
11815 	int v_actual;
11816 	int iwarp_requested = 0;
11817 
11818 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11819 		return -ENODEV;
11820 
11821 	/* The number of vectors we'll request will be comprised of:
11822 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11823 	 *   - The number of LAN queue pairs
11824 	 *	- Queues being used for RSS.
11825 	 *		We don't need as many as max_rss_size vectors.
11826 	 *		use rss_size instead in the calculation since that
11827 	 *		is governed by number of cpus in the system.
11828 	 *	- assumes symmetric Tx/Rx pairing
11829 	 *   - The number of VMDq pairs
11830 	 *   - The CPU count within the NUMA node if iWARP is enabled
11831 	 * Once we count this up, try the request.
11832 	 *
11833 	 * If we can't get what we want, we'll simplify to nearly nothing
11834 	 * and try again.  If that still fails, we punt.
11835 	 */
11836 	vectors_left = hw->func_caps.num_msix_vectors;
11837 	v_budget = 0;
11838 
11839 	/* reserve one vector for miscellaneous handler */
11840 	if (vectors_left) {
11841 		v_budget++;
11842 		vectors_left--;
11843 	}
11844 
11845 	/* reserve some vectors for the main PF traffic queues. Initially we
11846 	 * only reserve at most 50% of the available vectors, in the case that
11847 	 * the number of online CPUs is large. This ensures that we can enable
11848 	 * extra features as well. Once we've enabled the other features, we
11849 	 * will use any remaining vectors to reach as close as we can to the
11850 	 * number of online CPUs.
11851 	 */
11852 	cpus = num_online_cpus();
11853 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11854 	vectors_left -= pf->num_lan_msix;
11855 
11856 	/* reserve one vector for sideband flow director */
11857 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11858 		if (vectors_left) {
11859 			pf->num_fdsb_msix = 1;
11860 			v_budget++;
11861 			vectors_left--;
11862 		} else {
11863 			pf->num_fdsb_msix = 0;
11864 		}
11865 	}
11866 
11867 	/* can we reserve enough for iWARP? */
11868 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11869 		iwarp_requested = pf->num_iwarp_msix;
11870 
11871 		if (!vectors_left)
11872 			pf->num_iwarp_msix = 0;
11873 		else if (vectors_left < pf->num_iwarp_msix)
11874 			pf->num_iwarp_msix = 1;
11875 		v_budget += pf->num_iwarp_msix;
11876 		vectors_left -= pf->num_iwarp_msix;
11877 	}
11878 
11879 	/* any vectors left over go for VMDq support */
11880 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11881 		if (!vectors_left) {
11882 			pf->num_vmdq_msix = 0;
11883 			pf->num_vmdq_qps = 0;
11884 		} else {
11885 			int vmdq_vecs_wanted =
11886 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11887 			int vmdq_vecs =
11888 				min_t(int, vectors_left, vmdq_vecs_wanted);
11889 
11890 			/* if we're short on vectors for what's desired, we limit
11891 			 * the queues per vmdq.  If this is still more than are
11892 			 * available, the user will need to change the number of
11893 			 * queues/vectors used by the PF later with the ethtool
11894 			 * channels command
11895 			 */
11896 			if (vectors_left < vmdq_vecs_wanted) {
11897 				pf->num_vmdq_qps = 1;
11898 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11899 				vmdq_vecs = min_t(int,
11900 						  vectors_left,
11901 						  vmdq_vecs_wanted);
11902 			}
11903 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11904 
11905 			v_budget += vmdq_vecs;
11906 			vectors_left -= vmdq_vecs;
11907 		}
11908 	}
11909 
11910 	/* On systems with a large number of SMP cores, we previously limited
11911 	 * the number of vectors for num_lan_msix to be at most 50% of the
11912 	 * available vectors, to allow for other features. Now, we add back
11913 	 * the remaining vectors. However, we ensure that the total
11914 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11915 	 * calculate the number of vectors we can add without going over the
11916 	 * cap of CPUs. For systems with a small number of CPUs this will be
11917 	 * zero.
11918 	 */
11919 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11920 	pf->num_lan_msix += extra_vectors;
11921 	vectors_left -= extra_vectors;
11922 
11923 	WARN(vectors_left < 0,
11924 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11925 
11926 	v_budget += pf->num_lan_msix;
11927 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11928 				   GFP_KERNEL);
11929 	if (!pf->msix_entries)
11930 		return -ENOMEM;
11931 
11932 	for (i = 0; i < v_budget; i++)
11933 		pf->msix_entries[i].entry = i;
11934 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11935 
11936 	if (v_actual < I40E_MIN_MSIX) {
11937 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11938 		kfree(pf->msix_entries);
11939 		pf->msix_entries = NULL;
11940 		pci_disable_msix(pf->pdev);
11941 		return -ENODEV;
11942 
11943 	} else if (v_actual == I40E_MIN_MSIX) {
11944 		/* Adjust for minimal MSIX use */
11945 		pf->num_vmdq_vsis = 0;
11946 		pf->num_vmdq_qps = 0;
11947 		pf->num_lan_qps = 1;
11948 		pf->num_lan_msix = 1;
11949 
11950 	} else if (v_actual != v_budget) {
11951 		/* If we have limited resources, we will start with no vectors
11952 		 * for the special features and then allocate vectors to some
11953 		 * of these features based on the policy and at the end disable
11954 		 * the features that did not get any vectors.
11955 		 */
11956 		int vec;
11957 
11958 		dev_info(&pf->pdev->dev,
11959 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11960 			 v_actual, v_budget);
11961 		/* reserve the misc vector */
11962 		vec = v_actual - 1;
11963 
11964 		/* Scale vector usage down */
11965 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11966 		pf->num_vmdq_vsis = 1;
11967 		pf->num_vmdq_qps = 1;
11968 
11969 		/* partition out the remaining vectors */
11970 		switch (vec) {
11971 		case 2:
11972 			pf->num_lan_msix = 1;
11973 			break;
11974 		case 3:
11975 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11976 				pf->num_lan_msix = 1;
11977 				pf->num_iwarp_msix = 1;
11978 			} else {
11979 				pf->num_lan_msix = 2;
11980 			}
11981 			break;
11982 		default:
11983 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11984 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11985 						 iwarp_requested);
11986 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11987 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11988 			} else {
11989 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11990 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11991 			}
11992 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11993 				pf->num_fdsb_msix = 1;
11994 				vec--;
11995 			}
11996 			pf->num_lan_msix = min_t(int,
11997 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11998 							      pf->num_lan_msix);
11999 			pf->num_lan_qps = pf->num_lan_msix;
12000 			break;
12001 		}
12002 	}
12003 
12004 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
12005 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
12006 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12007 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12008 	}
12009 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
12010 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
12011 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12012 	}
12013 
12014 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
12015 	    pf->num_iwarp_msix == 0) {
12016 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
12017 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12018 	}
12019 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
12020 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
12021 		   pf->num_lan_msix,
12022 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
12023 		   pf->num_fdsb_msix,
12024 		   pf->num_iwarp_msix);
12025 
12026 	return v_actual;
12027 }
12028 
12029 /**
12030  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12031  * @vsi: the VSI being configured
12032  * @v_idx: index of the vector in the vsi struct
12033  *
12034  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12035  **/
12036 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12037 {
12038 	struct i40e_q_vector *q_vector;
12039 
12040 	/* allocate q_vector */
12041 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
12042 	if (!q_vector)
12043 		return -ENOMEM;
12044 
12045 	q_vector->vsi = vsi;
12046 	q_vector->v_idx = v_idx;
12047 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12048 
12049 	if (vsi->netdev)
12050 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12051 
12052 	/* tie q_vector and vsi together */
12053 	vsi->q_vectors[v_idx] = q_vector;
12054 
12055 	return 0;
12056 }
12057 
12058 /**
12059  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12060  * @vsi: the VSI being configured
12061  *
12062  * We allocate one q_vector per queue interrupt.  If allocation fails we
12063  * return -ENOMEM.
12064  **/
12065 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12066 {
12067 	struct i40e_pf *pf = vsi->back;
12068 	int err, v_idx, num_q_vectors;
12069 
12070 	/* if not MSIX, give the one vector only to the LAN VSI */
12071 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12072 		num_q_vectors = vsi->num_q_vectors;
12073 	else if (vsi->type == I40E_VSI_MAIN)
12074 		num_q_vectors = 1;
12075 	else
12076 		return -EINVAL;
12077 
12078 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12079 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12080 		if (err)
12081 			goto err_out;
12082 	}
12083 
12084 	return 0;
12085 
12086 err_out:
12087 	while (v_idx--)
12088 		i40e_free_q_vector(vsi, v_idx);
12089 
12090 	return err;
12091 }
12092 
12093 /**
12094  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12095  * @pf: board private structure to initialize
12096  **/
12097 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12098 {
12099 	int vectors = 0;
12100 	ssize_t size;
12101 
12102 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12103 		vectors = i40e_init_msix(pf);
12104 		if (vectors < 0) {
12105 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12106 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12107 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12108 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12109 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12110 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12111 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12112 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12113 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12114 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12115 
12116 			/* rework the queue expectations without MSIX */
12117 			i40e_determine_queue_usage(pf);
12118 		}
12119 	}
12120 
12121 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12122 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12123 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12124 		vectors = pci_enable_msi(pf->pdev);
12125 		if (vectors < 0) {
12126 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12127 				 vectors);
12128 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12129 		}
12130 		vectors = 1;  /* one MSI or Legacy vector */
12131 	}
12132 
12133 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12134 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12135 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12136 
12137 	/* set up vector assignment tracking */
12138 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12139 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12140 	if (!pf->irq_pile)
12141 		return -ENOMEM;
12142 
12143 	pf->irq_pile->num_entries = vectors;
12144 
12145 	/* track first vector for misc interrupts, ignore return */
12146 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12147 
12148 	return 0;
12149 }
12150 
12151 /**
12152  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12153  * @pf: private board data structure
12154  *
12155  * Restore the interrupt scheme that was cleared when we suspended the
12156  * device. This should be called during resume to re-allocate the q_vectors
12157  * and reacquire IRQs.
12158  */
12159 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12160 {
12161 	struct i40e_vsi *vsi;
12162 	int err, i;
12163 
12164 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12165 	 * scheme. We need to re-enabled them here in order to attempt to
12166 	 * re-acquire the MSI or MSI-X vectors
12167 	 */
12168 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12169 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12170 
12171 	err = i40e_init_interrupt_scheme(pf);
12172 	if (err)
12173 		return err;
12174 
12175 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12176 	 * rings together again.
12177 	 */
12178 	i40e_pf_for_each_vsi(pf, i, vsi) {
12179 		err = i40e_vsi_alloc_q_vectors(vsi);
12180 		if (err)
12181 			goto err_unwind;
12182 
12183 		i40e_vsi_map_rings_to_vectors(vsi);
12184 	}
12185 
12186 	err = i40e_setup_misc_vector(pf);
12187 	if (err)
12188 		goto err_unwind;
12189 
12190 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12191 		i40e_client_update_msix_info(pf);
12192 
12193 	return 0;
12194 
12195 err_unwind:
12196 	while (i--) {
12197 		if (pf->vsi[i])
12198 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12199 	}
12200 
12201 	return err;
12202 }
12203 
12204 /**
12205  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12206  * non queue events in recovery mode
12207  * @pf: board private structure
12208  *
12209  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12210  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12211  * This is handled differently than in recovery mode since no Tx/Rx resources
12212  * are being allocated.
12213  **/
12214 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12215 {
12216 	int err;
12217 
12218 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12219 		err = i40e_setup_misc_vector(pf);
12220 
12221 		if (err) {
12222 			dev_info(&pf->pdev->dev,
12223 				 "MSI-X misc vector request failed, error %d\n",
12224 				 err);
12225 			return err;
12226 		}
12227 	} else {
12228 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12229 
12230 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12231 				  pf->int_name, pf);
12232 
12233 		if (err) {
12234 			dev_info(&pf->pdev->dev,
12235 				 "MSI/legacy misc vector request failed, error %d\n",
12236 				 err);
12237 			return err;
12238 		}
12239 		i40e_enable_misc_int_causes(pf);
12240 		i40e_irq_dynamic_enable_icr0(pf);
12241 	}
12242 
12243 	return 0;
12244 }
12245 
12246 /**
12247  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12248  * @pf: board private structure
12249  *
12250  * This sets up the handler for MSIX 0, which is used to manage the
12251  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12252  * when in MSI or Legacy interrupt mode.
12253  **/
12254 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12255 {
12256 	struct i40e_hw *hw = &pf->hw;
12257 	int err = 0;
12258 
12259 	/* Only request the IRQ once, the first time through. */
12260 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12261 		err = request_irq(pf->msix_entries[0].vector,
12262 				  i40e_intr, 0, pf->int_name, pf);
12263 		if (err) {
12264 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12265 			dev_info(&pf->pdev->dev,
12266 				 "request_irq for %s failed: %d\n",
12267 				 pf->int_name, err);
12268 			return -EFAULT;
12269 		}
12270 	}
12271 
12272 	i40e_enable_misc_int_causes(pf);
12273 
12274 	/* associate no queues to the misc vector */
12275 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12276 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12277 
12278 	i40e_flush(hw);
12279 
12280 	i40e_irq_dynamic_enable_icr0(pf);
12281 
12282 	return err;
12283 }
12284 
12285 /**
12286  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12287  * @vsi: Pointer to vsi structure
12288  * @seed: Buffter to store the hash keys
12289  * @lut: Buffer to store the lookup table entries
12290  * @lut_size: Size of buffer to store the lookup table entries
12291  *
12292  * Return 0 on success, negative on failure
12293  */
12294 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12295 			   u8 *lut, u16 lut_size)
12296 {
12297 	struct i40e_pf *pf = vsi->back;
12298 	struct i40e_hw *hw = &pf->hw;
12299 	int ret = 0;
12300 
12301 	if (seed) {
12302 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12303 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12304 		if (ret) {
12305 			dev_info(&pf->pdev->dev,
12306 				 "Cannot get RSS key, err %pe aq_err %s\n",
12307 				 ERR_PTR(ret),
12308 				 i40e_aq_str(&pf->hw,
12309 					     pf->hw.aq.asq_last_status));
12310 			return ret;
12311 		}
12312 	}
12313 
12314 	if (lut) {
12315 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12316 
12317 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12318 		if (ret) {
12319 			dev_info(&pf->pdev->dev,
12320 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12321 				 ERR_PTR(ret),
12322 				 i40e_aq_str(&pf->hw,
12323 					     pf->hw.aq.asq_last_status));
12324 			return ret;
12325 		}
12326 	}
12327 
12328 	return ret;
12329 }
12330 
12331 /**
12332  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12333  * @vsi: Pointer to vsi structure
12334  * @seed: RSS hash seed
12335  * @lut: Lookup table
12336  * @lut_size: Lookup table size
12337  *
12338  * Returns 0 on success, negative on failure
12339  **/
12340 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12341 			       const u8 *lut, u16 lut_size)
12342 {
12343 	struct i40e_pf *pf = vsi->back;
12344 	struct i40e_hw *hw = &pf->hw;
12345 	u16 vf_id = vsi->vf_id;
12346 	u8 i;
12347 
12348 	/* Fill out hash function seed */
12349 	if (seed) {
12350 		u32 *seed_dw = (u32 *)seed;
12351 
12352 		if (vsi->type == I40E_VSI_MAIN) {
12353 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12354 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12355 		} else if (vsi->type == I40E_VSI_SRIOV) {
12356 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12357 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12358 		} else {
12359 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12360 		}
12361 	}
12362 
12363 	if (lut) {
12364 		u32 *lut_dw = (u32 *)lut;
12365 
12366 		if (vsi->type == I40E_VSI_MAIN) {
12367 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12368 				return -EINVAL;
12369 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12370 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12371 		} else if (vsi->type == I40E_VSI_SRIOV) {
12372 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12373 				return -EINVAL;
12374 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12375 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12376 		} else {
12377 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12378 		}
12379 	}
12380 	i40e_flush(hw);
12381 
12382 	return 0;
12383 }
12384 
12385 /**
12386  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12387  * @vsi: Pointer to VSI structure
12388  * @seed: Buffer to store the keys
12389  * @lut: Buffer to store the lookup table entries
12390  * @lut_size: Size of buffer to store the lookup table entries
12391  *
12392  * Returns 0 on success, negative on failure
12393  */
12394 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12395 			    u8 *lut, u16 lut_size)
12396 {
12397 	struct i40e_pf *pf = vsi->back;
12398 	struct i40e_hw *hw = &pf->hw;
12399 	u16 i;
12400 
12401 	if (seed) {
12402 		u32 *seed_dw = (u32 *)seed;
12403 
12404 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12405 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12406 	}
12407 	if (lut) {
12408 		u32 *lut_dw = (u32 *)lut;
12409 
12410 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12411 			return -EINVAL;
12412 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12413 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12414 	}
12415 
12416 	return 0;
12417 }
12418 
12419 /**
12420  * i40e_config_rss - Configure RSS keys and lut
12421  * @vsi: Pointer to VSI structure
12422  * @seed: RSS hash seed
12423  * @lut: Lookup table
12424  * @lut_size: Lookup table size
12425  *
12426  * Returns 0 on success, negative on failure
12427  */
12428 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12429 {
12430 	struct i40e_pf *pf = vsi->back;
12431 
12432 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12433 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12434 	else
12435 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12436 }
12437 
12438 /**
12439  * i40e_get_rss - Get RSS keys and lut
12440  * @vsi: Pointer to VSI structure
12441  * @seed: Buffer to store the keys
12442  * @lut: Buffer to store the lookup table entries
12443  * @lut_size: Size of buffer to store the lookup table entries
12444  *
12445  * Returns 0 on success, negative on failure
12446  */
12447 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12448 {
12449 	struct i40e_pf *pf = vsi->back;
12450 
12451 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12452 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12453 	else
12454 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12455 }
12456 
12457 /**
12458  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12459  * @pf: Pointer to board private structure
12460  * @lut: Lookup table
12461  * @rss_table_size: Lookup table size
12462  * @rss_size: Range of queue number for hashing
12463  */
12464 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12465 		       u16 rss_table_size, u16 rss_size)
12466 {
12467 	u16 i;
12468 
12469 	for (i = 0; i < rss_table_size; i++)
12470 		lut[i] = i % rss_size;
12471 }
12472 
12473 /**
12474  * i40e_pf_config_rss - Prepare for RSS if used
12475  * @pf: board private structure
12476  **/
12477 static int i40e_pf_config_rss(struct i40e_pf *pf)
12478 {
12479 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12480 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12481 	u8 *lut;
12482 	struct i40e_hw *hw = &pf->hw;
12483 	u32 reg_val;
12484 	u64 hena;
12485 	int ret;
12486 
12487 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12488 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12489 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12490 	hena |= i40e_pf_get_default_rss_hashcfg(pf);
12491 
12492 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12493 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12494 
12495 	/* Determine the RSS table size based on the hardware capabilities */
12496 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12497 	reg_val = (pf->rss_table_size == 512) ?
12498 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12499 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12500 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12501 
12502 	/* Determine the RSS size of the VSI */
12503 	if (!vsi->rss_size) {
12504 		u16 qcount;
12505 		/* If the firmware does something weird during VSI init, we
12506 		 * could end up with zero TCs. Check for that to avoid
12507 		 * divide-by-zero. It probably won't pass traffic, but it also
12508 		 * won't panic.
12509 		 */
12510 		qcount = vsi->num_queue_pairs /
12511 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12512 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12513 	}
12514 	if (!vsi->rss_size)
12515 		return -EINVAL;
12516 
12517 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12518 	if (!lut)
12519 		return -ENOMEM;
12520 
12521 	/* Use user configured lut if there is one, otherwise use default */
12522 	if (vsi->rss_lut_user)
12523 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12524 	else
12525 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12526 
12527 	/* Use user configured hash key if there is one, otherwise
12528 	 * use default.
12529 	 */
12530 	if (vsi->rss_hkey_user)
12531 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12532 	else
12533 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12534 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12535 	kfree(lut);
12536 
12537 	return ret;
12538 }
12539 
12540 /**
12541  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12542  * @pf: board private structure
12543  * @queue_count: the requested queue count for rss.
12544  *
12545  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12546  * count which may be different from the requested queue count.
12547  * Note: expects to be called while under rtnl_lock()
12548  **/
12549 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12550 {
12551 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12552 	int new_rss_size;
12553 
12554 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12555 		return 0;
12556 
12557 	queue_count = min_t(int, queue_count, num_online_cpus());
12558 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12559 
12560 	if (queue_count != vsi->num_queue_pairs) {
12561 		u16 qcount;
12562 
12563 		vsi->req_queue_pairs = queue_count;
12564 		i40e_prep_for_reset(pf);
12565 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12566 			return pf->alloc_rss_size;
12567 
12568 		pf->alloc_rss_size = new_rss_size;
12569 
12570 		i40e_reset_and_rebuild(pf, true, true);
12571 
12572 		/* Discard the user configured hash keys and lut, if less
12573 		 * queues are enabled.
12574 		 */
12575 		if (queue_count < vsi->rss_size) {
12576 			i40e_clear_rss_config_user(vsi);
12577 			dev_dbg(&pf->pdev->dev,
12578 				"discard user configured hash keys and lut\n");
12579 		}
12580 
12581 		/* Reset vsi->rss_size, as number of enabled queues changed */
12582 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12583 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12584 
12585 		i40e_pf_config_rss(pf);
12586 	}
12587 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12588 		 vsi->req_queue_pairs, pf->rss_size_max);
12589 	return pf->alloc_rss_size;
12590 }
12591 
12592 /**
12593  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12594  * @pf: board private structure
12595  **/
12596 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12597 {
12598 	bool min_valid, max_valid;
12599 	u32 max_bw, min_bw;
12600 	int status;
12601 
12602 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12603 					   &min_valid, &max_valid);
12604 
12605 	if (!status) {
12606 		if (min_valid)
12607 			pf->min_bw = min_bw;
12608 		if (max_valid)
12609 			pf->max_bw = max_bw;
12610 	}
12611 
12612 	return status;
12613 }
12614 
12615 /**
12616  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12617  * @pf: board private structure
12618  **/
12619 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12620 {
12621 	struct i40e_aqc_configure_partition_bw_data bw_data;
12622 	int status;
12623 
12624 	memset(&bw_data, 0, sizeof(bw_data));
12625 
12626 	/* Set the valid bit for this PF */
12627 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12628 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12629 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12630 
12631 	/* Set the new bandwidths */
12632 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12633 
12634 	return status;
12635 }
12636 
12637 /**
12638  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12639  * if total port shutdown feature is enabled for this PF
12640  * @pf: board private structure
12641  **/
12642 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12643 {
12644 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12645 #define I40E_FEATURES_ENABLE_PTR		0x2A
12646 #define I40E_CURRENT_SETTING_PTR		0x2B
12647 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12648 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12649 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12650 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12651 	u16 sr_emp_sr_settings_ptr = 0;
12652 	u16 features_enable = 0;
12653 	u16 link_behavior = 0;
12654 	int read_status = 0;
12655 	bool ret = false;
12656 
12657 	read_status = i40e_read_nvm_word(&pf->hw,
12658 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12659 					 &sr_emp_sr_settings_ptr);
12660 	if (read_status)
12661 		goto err_nvm;
12662 	read_status = i40e_read_nvm_word(&pf->hw,
12663 					 sr_emp_sr_settings_ptr +
12664 					 I40E_FEATURES_ENABLE_PTR,
12665 					 &features_enable);
12666 	if (read_status)
12667 		goto err_nvm;
12668 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12669 		read_status = i40e_read_nvm_module_data(&pf->hw,
12670 							I40E_SR_EMP_SR_SETTINGS_PTR,
12671 							I40E_CURRENT_SETTING_PTR,
12672 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12673 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12674 							&link_behavior);
12675 		if (read_status)
12676 			goto err_nvm;
12677 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12678 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12679 	}
12680 	return ret;
12681 
12682 err_nvm:
12683 	dev_warn(&pf->pdev->dev,
12684 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12685 		 ERR_PTR(read_status));
12686 	return ret;
12687 }
12688 
12689 /**
12690  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12691  * @pf: board private structure to initialize
12692  *
12693  * i40e_sw_init initializes the Adapter private data structure.
12694  * Fields are initialized based on PCI device information and
12695  * OS network device settings (MTU size).
12696  **/
12697 static int i40e_sw_init(struct i40e_pf *pf)
12698 {
12699 	int err = 0;
12700 	int size;
12701 	u16 pow;
12702 
12703 	/* Set default capability flags */
12704 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12705 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12706 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12707 
12708 	/* Set default ITR */
12709 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12710 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12711 
12712 	/* Depending on PF configurations, it is possible that the RSS
12713 	 * maximum might end up larger than the available queues
12714 	 */
12715 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12716 	pf->alloc_rss_size = 1;
12717 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12718 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12719 				 pf->hw.func_caps.num_tx_qp);
12720 
12721 	/* find the next higher power-of-2 of num cpus */
12722 	pow = roundup_pow_of_two(num_online_cpus());
12723 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12724 
12725 	if (pf->hw.func_caps.rss) {
12726 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12727 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12728 					   num_online_cpus());
12729 	}
12730 
12731 	/* MFP mode enabled */
12732 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12733 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12734 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12735 		if (i40e_get_partition_bw_setting(pf)) {
12736 			dev_warn(&pf->pdev->dev,
12737 				 "Could not get partition bw settings\n");
12738 		} else {
12739 			dev_info(&pf->pdev->dev,
12740 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12741 				 pf->min_bw, pf->max_bw);
12742 
12743 			/* nudge the Tx scheduler */
12744 			i40e_set_partition_bw_setting(pf);
12745 		}
12746 	}
12747 
12748 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12749 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12750 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12751 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12752 		    pf->hw.num_partitions > 1)
12753 			dev_info(&pf->pdev->dev,
12754 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12755 		else
12756 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12757 		pf->fdir_pf_filter_count =
12758 				 pf->hw.func_caps.fd_filters_guaranteed;
12759 		pf->hw.fdir_shared_filter_count =
12760 				 pf->hw.func_caps.fd_filters_best_effort;
12761 	}
12762 
12763 	/* Enable HW ATR eviction if possible */
12764 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12765 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12766 
12767 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12768 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12769 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12770 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12771 	}
12772 
12773 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12774 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12775 		/* IWARP needs one extra vector for CQP just like MISC.*/
12776 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12777 	}
12778 	/* Stopping FW LLDP engine is supported on XL710 and X722
12779 	 * starting from FW versions determined in i40e_init_adminq.
12780 	 * Stopping the FW LLDP engine is not supported on XL710
12781 	 * if NPAR is functioning so unset this hw flag in this case.
12782 	 */
12783 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12784 	    pf->hw.func_caps.npar_enable)
12785 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12786 
12787 #ifdef CONFIG_PCI_IOV
12788 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12789 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12790 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12791 		pf->num_req_vfs = min_t(int,
12792 					pf->hw.func_caps.num_vfs,
12793 					I40E_MAX_VF_COUNT);
12794 	}
12795 #endif /* CONFIG_PCI_IOV */
12796 	pf->lan_veb = I40E_NO_VEB;
12797 	pf->lan_vsi = I40E_NO_VSI;
12798 
12799 	/* By default FW has this off for performance reasons */
12800 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12801 
12802 	/* set up queue assignment tracking */
12803 	size = sizeof(struct i40e_lump_tracking)
12804 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12805 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12806 	if (!pf->qp_pile) {
12807 		err = -ENOMEM;
12808 		goto sw_init_done;
12809 	}
12810 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12811 
12812 	pf->tx_timeout_recovery_level = 1;
12813 
12814 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12815 	    i40e_is_total_port_shutdown_enabled(pf)) {
12816 		/* Link down on close must be on when total port shutdown
12817 		 * is enabled for a given port
12818 		 */
12819 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12820 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12821 		dev_info(&pf->pdev->dev,
12822 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12823 	}
12824 	mutex_init(&pf->switch_mutex);
12825 
12826 sw_init_done:
12827 	return err;
12828 }
12829 
12830 /**
12831  * i40e_set_ntuple - set the ntuple feature flag and take action
12832  * @pf: board private structure to initialize
12833  * @features: the feature set that the stack is suggesting
12834  *
12835  * returns a bool to indicate if reset needs to happen
12836  **/
12837 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12838 {
12839 	bool need_reset = false;
12840 
12841 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12842 	 * the state changed, we need to reset.
12843 	 */
12844 	if (features & NETIF_F_NTUPLE) {
12845 		/* Enable filters and mark for reset */
12846 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12847 			need_reset = true;
12848 		/* enable FD_SB only if there is MSI-X vector and no cloud
12849 		 * filters exist
12850 		 */
12851 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12852 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12853 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12854 		}
12855 	} else {
12856 		/* turn off filters, mark for reset and clear SW filter list */
12857 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12858 			need_reset = true;
12859 			i40e_fdir_filter_exit(pf);
12860 		}
12861 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12862 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12863 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12864 
12865 		/* reset fd counters */
12866 		pf->fd_add_err = 0;
12867 		pf->fd_atr_cnt = 0;
12868 		/* if ATR was auto disabled it can be re-enabled. */
12869 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12870 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12871 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12872 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12873 	}
12874 	return need_reset;
12875 }
12876 
12877 /**
12878  * i40e_clear_rss_lut - clear the rx hash lookup table
12879  * @vsi: the VSI being configured
12880  **/
12881 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12882 {
12883 	struct i40e_pf *pf = vsi->back;
12884 	struct i40e_hw *hw = &pf->hw;
12885 	u16 vf_id = vsi->vf_id;
12886 	u8 i;
12887 
12888 	if (vsi->type == I40E_VSI_MAIN) {
12889 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12890 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12891 	} else if (vsi->type == I40E_VSI_SRIOV) {
12892 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12893 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12894 	} else {
12895 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12896 	}
12897 }
12898 
12899 /**
12900  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12901  * @vsi: ptr to VSI
12902  * @ena: flag to indicate the on/off setting
12903  */
12904 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12905 {
12906 	bool if_running = netif_running(vsi->netdev) &&
12907 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12908 	int ret;
12909 
12910 	if (if_running)
12911 		i40e_down(vsi);
12912 
12913 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12914 	if (ret)
12915 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12916 	if (if_running)
12917 		i40e_up(vsi);
12918 
12919 	return ret;
12920 }
12921 
12922 /**
12923  * i40e_set_features - set the netdev feature flags
12924  * @netdev: ptr to the netdev being adjusted
12925  * @features: the feature set that the stack is suggesting
12926  * Note: expects to be called while under rtnl_lock()
12927  **/
12928 static int i40e_set_features(struct net_device *netdev,
12929 			     netdev_features_t features)
12930 {
12931 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12932 	struct i40e_vsi *vsi = np->vsi;
12933 	struct i40e_pf *pf = vsi->back;
12934 	bool need_reset;
12935 
12936 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12937 		i40e_pf_config_rss(pf);
12938 	else if (!(features & NETIF_F_RXHASH) &&
12939 		 netdev->features & NETIF_F_RXHASH)
12940 		i40e_clear_rss_lut(vsi);
12941 
12942 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12943 		i40e_vlan_stripping_enable(vsi);
12944 	else
12945 		i40e_vlan_stripping_disable(vsi);
12946 
12947 	if (!(features & NETIF_F_HW_TC) &&
12948 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12949 		dev_err(&pf->pdev->dev,
12950 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12951 		return -EINVAL;
12952 	}
12953 
12954 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12955 		i40e_del_all_macvlans(vsi);
12956 
12957 	need_reset = i40e_set_ntuple(pf, features);
12958 
12959 	if (need_reset)
12960 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12961 
12962 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12963 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12964 
12965 	return 0;
12966 }
12967 
12968 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12969 				    unsigned int table, unsigned int idx,
12970 				    struct udp_tunnel_info *ti)
12971 {
12972 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12973 	struct i40e_hw *hw = &np->vsi->back->hw;
12974 	u8 type, filter_index;
12975 	int ret;
12976 
12977 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12978 						   I40E_AQC_TUNNEL_TYPE_NGE;
12979 
12980 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12981 				     NULL);
12982 	if (ret) {
12983 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
12984 			    ERR_PTR(ret),
12985 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12986 		return -EIO;
12987 	}
12988 
12989 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12990 	return 0;
12991 }
12992 
12993 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12994 				      unsigned int table, unsigned int idx,
12995 				      struct udp_tunnel_info *ti)
12996 {
12997 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12998 	struct i40e_hw *hw = &np->vsi->back->hw;
12999 	int ret;
13000 
13001 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13002 	if (ret) {
13003 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13004 			    ERR_PTR(ret),
13005 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13006 		return -EIO;
13007 	}
13008 
13009 	return 0;
13010 }
13011 
13012 static int i40e_get_phys_port_id(struct net_device *netdev,
13013 				 struct netdev_phys_item_id *ppid)
13014 {
13015 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13016 	struct i40e_pf *pf = np->vsi->back;
13017 	struct i40e_hw *hw = &pf->hw;
13018 
13019 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13020 		return -EOPNOTSUPP;
13021 
13022 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13023 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13024 
13025 	return 0;
13026 }
13027 
13028 /**
13029  * i40e_ndo_fdb_add - add an entry to the hardware database
13030  * @ndm: the input from the stack
13031  * @tb: pointer to array of nladdr (unused)
13032  * @dev: the net device pointer
13033  * @addr: the MAC address entry being added
13034  * @vid: VLAN ID
13035  * @flags: instructions from stack about fdb operation
13036  * @notified: whether notification was emitted
13037  * @extack: netlink extended ack, unused currently
13038  */
13039 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13040 			    struct net_device *dev,
13041 			    const unsigned char *addr, u16 vid,
13042 			    u16 flags, bool *notified,
13043 			    struct netlink_ext_ack *extack)
13044 {
13045 	struct i40e_netdev_priv *np = netdev_priv(dev);
13046 	struct i40e_pf *pf = np->vsi->back;
13047 	int err = 0;
13048 
13049 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13050 		return -EOPNOTSUPP;
13051 
13052 	if (vid) {
13053 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13054 		return -EINVAL;
13055 	}
13056 
13057 	/* Hardware does not support aging addresses so if a
13058 	 * ndm_state is given only allow permanent addresses
13059 	 */
13060 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13061 		netdev_info(dev, "FDB only supports static addresses\n");
13062 		return -EINVAL;
13063 	}
13064 
13065 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13066 		err = dev_uc_add_excl(dev, addr);
13067 	else if (is_multicast_ether_addr(addr))
13068 		err = dev_mc_add_excl(dev, addr);
13069 	else
13070 		err = -EINVAL;
13071 
13072 	/* Only return duplicate errors if NLM_F_EXCL is set */
13073 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13074 		err = 0;
13075 
13076 	return err;
13077 }
13078 
13079 /**
13080  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13081  * @dev: the netdev being configured
13082  * @nlh: RTNL message
13083  * @flags: bridge flags
13084  * @extack: netlink extended ack
13085  *
13086  * Inserts a new hardware bridge if not already created and
13087  * enables the bridging mode requested (VEB or VEPA). If the
13088  * hardware bridge has already been inserted and the request
13089  * is to change the mode then that requires a PF reset to
13090  * allow rebuild of the components with required hardware
13091  * bridge mode enabled.
13092  *
13093  * Note: expects to be called while under rtnl_lock()
13094  **/
13095 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13096 				   struct nlmsghdr *nlh,
13097 				   u16 flags,
13098 				   struct netlink_ext_ack *extack)
13099 {
13100 	struct i40e_netdev_priv *np = netdev_priv(dev);
13101 	struct i40e_vsi *vsi = np->vsi;
13102 	struct i40e_pf *pf = vsi->back;
13103 	struct nlattr *attr, *br_spec;
13104 	struct i40e_veb *veb;
13105 	int rem;
13106 
13107 	/* Only for PF VSI for now */
13108 	if (vsi->type != I40E_VSI_MAIN)
13109 		return -EOPNOTSUPP;
13110 
13111 	/* Find the HW bridge for PF VSI */
13112 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13113 
13114 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13115 	if (!br_spec)
13116 		return -EINVAL;
13117 
13118 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
13119 		__u16 mode = nla_get_u16(attr);
13120 
13121 		if ((mode != BRIDGE_MODE_VEPA) &&
13122 		    (mode != BRIDGE_MODE_VEB))
13123 			return -EINVAL;
13124 
13125 		/* Insert a new HW bridge */
13126 		if (!veb) {
13127 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
13128 					     vsi->tc_config.enabled_tc);
13129 			if (veb) {
13130 				veb->bridge_mode = mode;
13131 				i40e_config_bridge_mode(veb);
13132 			} else {
13133 				/* No Bridge HW offload available */
13134 				return -ENOENT;
13135 			}
13136 			break;
13137 		} else if (mode != veb->bridge_mode) {
13138 			/* Existing HW bridge but different mode needs reset */
13139 			veb->bridge_mode = mode;
13140 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13141 			if (mode == BRIDGE_MODE_VEB)
13142 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13143 			else
13144 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13145 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13146 			break;
13147 		}
13148 	}
13149 
13150 	return 0;
13151 }
13152 
13153 /**
13154  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13155  * @skb: skb buff
13156  * @pid: process id
13157  * @seq: RTNL message seq #
13158  * @dev: the netdev being configured
13159  * @filter_mask: unused
13160  * @nlflags: netlink flags passed in
13161  *
13162  * Return the mode in which the hardware bridge is operating in
13163  * i.e VEB or VEPA.
13164  **/
13165 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13166 				   struct net_device *dev,
13167 				   u32 __always_unused filter_mask,
13168 				   int nlflags)
13169 {
13170 	struct i40e_netdev_priv *np = netdev_priv(dev);
13171 	struct i40e_vsi *vsi = np->vsi;
13172 	struct i40e_pf *pf = vsi->back;
13173 	struct i40e_veb *veb;
13174 
13175 	/* Only for PF VSI for now */
13176 	if (vsi->type != I40E_VSI_MAIN)
13177 		return -EOPNOTSUPP;
13178 
13179 	/* Find the HW bridge for the PF VSI */
13180 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13181 	if (!veb)
13182 		return 0;
13183 
13184 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13185 				       0, 0, nlflags, filter_mask, NULL);
13186 }
13187 
13188 /**
13189  * i40e_features_check - Validate encapsulated packet conforms to limits
13190  * @skb: skb buff
13191  * @dev: This physical port's netdev
13192  * @features: Offload features that the stack believes apply
13193  **/
13194 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13195 					     struct net_device *dev,
13196 					     netdev_features_t features)
13197 {
13198 	size_t len;
13199 
13200 	/* No point in doing any of this if neither checksum nor GSO are
13201 	 * being requested for this frame.  We can rule out both by just
13202 	 * checking for CHECKSUM_PARTIAL
13203 	 */
13204 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13205 		return features;
13206 
13207 	/* We cannot support GSO if the MSS is going to be less than
13208 	 * 64 bytes.  If it is then we need to drop support for GSO.
13209 	 */
13210 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13211 		features &= ~NETIF_F_GSO_MASK;
13212 
13213 	/* MACLEN can support at most 63 words */
13214 	len = skb_network_offset(skb);
13215 	if (len & ~(63 * 2))
13216 		goto out_err;
13217 
13218 	/* IPLEN and EIPLEN can support at most 127 dwords */
13219 	len = skb_network_header_len(skb);
13220 	if (len & ~(127 * 4))
13221 		goto out_err;
13222 
13223 	if (skb->encapsulation) {
13224 		/* L4TUNLEN can support 127 words */
13225 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13226 		if (len & ~(127 * 2))
13227 			goto out_err;
13228 
13229 		/* IPLEN can support at most 127 dwords */
13230 		len = skb_inner_transport_header(skb) -
13231 		      skb_inner_network_header(skb);
13232 		if (len & ~(127 * 4))
13233 			goto out_err;
13234 	}
13235 
13236 	/* No need to validate L4LEN as TCP is the only protocol with a
13237 	 * flexible value and we support all possible values supported
13238 	 * by TCP, which is at most 15 dwords
13239 	 */
13240 
13241 	return features;
13242 out_err:
13243 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13244 }
13245 
13246 /**
13247  * i40e_xdp_setup - add/remove an XDP program
13248  * @vsi: VSI to changed
13249  * @prog: XDP program
13250  * @extack: netlink extended ack
13251  **/
13252 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13253 			  struct netlink_ext_ack *extack)
13254 {
13255 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13256 	struct i40e_pf *pf = vsi->back;
13257 	struct bpf_prog *old_prog;
13258 	bool need_reset;
13259 	int i;
13260 
13261 	/* VSI shall be deleted in a moment, block loading new programs */
13262 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13263 		return -EINVAL;
13264 
13265 	/* Don't allow frames that span over multiple buffers */
13266 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13267 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13268 		return -EINVAL;
13269 	}
13270 
13271 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13272 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13273 	if (need_reset)
13274 		i40e_prep_for_reset(pf);
13275 
13276 	old_prog = xchg(&vsi->xdp_prog, prog);
13277 
13278 	if (need_reset) {
13279 		if (!prog) {
13280 			xdp_features_clear_redirect_target(vsi->netdev);
13281 			/* Wait until ndo_xsk_wakeup completes. */
13282 			synchronize_rcu();
13283 		}
13284 		i40e_reset_and_rebuild(pf, true, true);
13285 	}
13286 
13287 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13288 		if (i40e_realloc_rx_bi_zc(vsi, true))
13289 			return -ENOMEM;
13290 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13291 		if (i40e_realloc_rx_bi_zc(vsi, false))
13292 			return -ENOMEM;
13293 	}
13294 
13295 	for (i = 0; i < vsi->num_queue_pairs; i++)
13296 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13297 
13298 	if (old_prog)
13299 		bpf_prog_put(old_prog);
13300 
13301 	/* Kick start the NAPI context if there is an AF_XDP socket open
13302 	 * on that queue id. This so that receiving will start.
13303 	 */
13304 	if (need_reset && prog) {
13305 		for (i = 0; i < vsi->num_queue_pairs; i++)
13306 			if (vsi->xdp_rings[i]->xsk_pool)
13307 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13308 						      XDP_WAKEUP_RX);
13309 		xdp_features_set_redirect_target(vsi->netdev, true);
13310 	}
13311 
13312 	return 0;
13313 }
13314 
13315 /**
13316  * i40e_enter_busy_conf - Enters busy config state
13317  * @vsi: vsi
13318  *
13319  * Returns 0 on success, <0 for failure.
13320  **/
13321 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13322 {
13323 	struct i40e_pf *pf = vsi->back;
13324 	int timeout = 50;
13325 
13326 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13327 		timeout--;
13328 		if (!timeout)
13329 			return -EBUSY;
13330 		usleep_range(1000, 2000);
13331 	}
13332 
13333 	return 0;
13334 }
13335 
13336 /**
13337  * i40e_exit_busy_conf - Exits busy config state
13338  * @vsi: vsi
13339  **/
13340 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13341 {
13342 	struct i40e_pf *pf = vsi->back;
13343 
13344 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13345 }
13346 
13347 /**
13348  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13349  * @vsi: vsi
13350  * @queue_pair: queue pair
13351  **/
13352 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13353 {
13354 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13355 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13356 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13357 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13358 	if (i40e_enabled_xdp_vsi(vsi)) {
13359 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13360 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13361 	}
13362 }
13363 
13364 /**
13365  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13366  * @vsi: vsi
13367  * @queue_pair: queue pair
13368  **/
13369 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13370 {
13371 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13372 	if (i40e_enabled_xdp_vsi(vsi)) {
13373 		/* Make sure that in-progress ndo_xdp_xmit calls are
13374 		 * completed.
13375 		 */
13376 		synchronize_rcu();
13377 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13378 	}
13379 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13380 }
13381 
13382 /**
13383  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13384  * @vsi: vsi
13385  * @queue_pair: queue pair
13386  * @enable: true for enable, false for disable
13387  **/
13388 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13389 					bool enable)
13390 {
13391 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13392 	struct i40e_q_vector *q_vector = rxr->q_vector;
13393 
13394 	if (!vsi->netdev)
13395 		return;
13396 
13397 	/* All rings in a qp belong to the same qvector. */
13398 	if (q_vector->rx.ring || q_vector->tx.ring) {
13399 		if (enable)
13400 			napi_enable(&q_vector->napi);
13401 		else
13402 			napi_disable(&q_vector->napi);
13403 	}
13404 }
13405 
13406 /**
13407  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13408  * @vsi: vsi
13409  * @queue_pair: queue pair
13410  * @enable: true for enable, false for disable
13411  *
13412  * Returns 0 on success, <0 on failure.
13413  **/
13414 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13415 					bool enable)
13416 {
13417 	struct i40e_pf *pf = vsi->back;
13418 	int pf_q, ret = 0;
13419 
13420 	pf_q = vsi->base_queue + queue_pair;
13421 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13422 				     false /*is xdp*/, enable);
13423 	if (ret) {
13424 		dev_info(&pf->pdev->dev,
13425 			 "VSI seid %d Tx ring %d %sable timeout\n",
13426 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13427 		return ret;
13428 	}
13429 
13430 	i40e_control_rx_q(pf, pf_q, enable);
13431 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13432 	if (ret) {
13433 		dev_info(&pf->pdev->dev,
13434 			 "VSI seid %d Rx ring %d %sable timeout\n",
13435 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13436 		return ret;
13437 	}
13438 
13439 	/* Due to HW errata, on Rx disable only, the register can
13440 	 * indicate done before it really is. Needs 50ms to be sure
13441 	 */
13442 	if (!enable)
13443 		mdelay(50);
13444 
13445 	if (!i40e_enabled_xdp_vsi(vsi))
13446 		return ret;
13447 
13448 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13449 				     pf_q + vsi->alloc_queue_pairs,
13450 				     true /*is xdp*/, enable);
13451 	if (ret) {
13452 		dev_info(&pf->pdev->dev,
13453 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13454 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13455 	}
13456 
13457 	return ret;
13458 }
13459 
13460 /**
13461  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13462  * @vsi: vsi
13463  * @queue_pair: queue_pair
13464  **/
13465 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13466 {
13467 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13468 	struct i40e_pf *pf = vsi->back;
13469 	struct i40e_hw *hw = &pf->hw;
13470 
13471 	/* All rings in a qp belong to the same qvector. */
13472 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13473 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13474 	else
13475 		i40e_irq_dynamic_enable_icr0(pf);
13476 
13477 	i40e_flush(hw);
13478 }
13479 
13480 /**
13481  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13482  * @vsi: vsi
13483  * @queue_pair: queue_pair
13484  **/
13485 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13486 {
13487 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13488 	struct i40e_pf *pf = vsi->back;
13489 	struct i40e_hw *hw = &pf->hw;
13490 
13491 	/* For simplicity, instead of removing the qp interrupt causes
13492 	 * from the interrupt linked list, we simply disable the interrupt, and
13493 	 * leave the list intact.
13494 	 *
13495 	 * All rings in a qp belong to the same qvector.
13496 	 */
13497 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13498 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13499 
13500 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13501 		i40e_flush(hw);
13502 		synchronize_irq(pf->msix_entries[intpf].vector);
13503 	} else {
13504 		/* Legacy and MSI mode - this stops all interrupt handling */
13505 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13506 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13507 		i40e_flush(hw);
13508 		synchronize_irq(pf->pdev->irq);
13509 	}
13510 }
13511 
13512 /**
13513  * i40e_queue_pair_disable - Disables a queue pair
13514  * @vsi: vsi
13515  * @queue_pair: queue pair
13516  *
13517  * Returns 0 on success, <0 on failure.
13518  **/
13519 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13520 {
13521 	int err;
13522 
13523 	err = i40e_enter_busy_conf(vsi);
13524 	if (err)
13525 		return err;
13526 
13527 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13528 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13529 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13530 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13531 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13532 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13533 
13534 	return err;
13535 }
13536 
13537 /**
13538  * i40e_queue_pair_enable - Enables a queue pair
13539  * @vsi: vsi
13540  * @queue_pair: queue pair
13541  *
13542  * Returns 0 on success, <0 on failure.
13543  **/
13544 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13545 {
13546 	int err;
13547 
13548 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13549 	if (err)
13550 		return err;
13551 
13552 	if (i40e_enabled_xdp_vsi(vsi)) {
13553 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13554 		if (err)
13555 			return err;
13556 	}
13557 
13558 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13559 	if (err)
13560 		return err;
13561 
13562 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13563 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13564 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13565 
13566 	i40e_exit_busy_conf(vsi);
13567 
13568 	return err;
13569 }
13570 
13571 /**
13572  * i40e_xdp - implements ndo_bpf for i40e
13573  * @dev: netdevice
13574  * @xdp: XDP command
13575  **/
13576 static int i40e_xdp(struct net_device *dev,
13577 		    struct netdev_bpf *xdp)
13578 {
13579 	struct i40e_netdev_priv *np = netdev_priv(dev);
13580 	struct i40e_vsi *vsi = np->vsi;
13581 
13582 	if (vsi->type != I40E_VSI_MAIN)
13583 		return -EINVAL;
13584 
13585 	switch (xdp->command) {
13586 	case XDP_SETUP_PROG:
13587 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13588 	case XDP_SETUP_XSK_POOL:
13589 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13590 					   xdp->xsk.queue_id);
13591 	default:
13592 		return -EINVAL;
13593 	}
13594 }
13595 
13596 static const struct net_device_ops i40e_netdev_ops = {
13597 	.ndo_open		= i40e_open,
13598 	.ndo_stop		= i40e_close,
13599 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13600 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13601 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13602 	.ndo_validate_addr	= eth_validate_addr,
13603 	.ndo_set_mac_address	= i40e_set_mac,
13604 	.ndo_change_mtu		= i40e_change_mtu,
13605 	.ndo_tx_timeout		= i40e_tx_timeout,
13606 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13607 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13608 #ifdef CONFIG_NET_POLL_CONTROLLER
13609 	.ndo_poll_controller	= i40e_netpoll,
13610 #endif
13611 	.ndo_setup_tc		= __i40e_setup_tc,
13612 	.ndo_select_queue	= i40e_lan_select_queue,
13613 	.ndo_set_features	= i40e_set_features,
13614 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13615 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13616 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13617 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13618 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13619 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13620 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13621 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13622 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13623 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13624 	.ndo_features_check	= i40e_features_check,
13625 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13626 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13627 	.ndo_bpf		= i40e_xdp,
13628 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13629 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13630 	.ndo_dfwd_add_station	= i40e_fwd_add,
13631 	.ndo_dfwd_del_station	= i40e_fwd_del,
13632 	.ndo_hwtstamp_get	= i40e_ptp_hwtstamp_get,
13633 	.ndo_hwtstamp_set	= i40e_ptp_hwtstamp_set,
13634 };
13635 
13636 /**
13637  * i40e_config_netdev - Setup the netdev flags
13638  * @vsi: the VSI being configured
13639  *
13640  * Returns 0 on success, negative value on failure
13641  **/
13642 static int i40e_config_netdev(struct i40e_vsi *vsi)
13643 {
13644 	struct i40e_pf *pf = vsi->back;
13645 	struct i40e_hw *hw = &pf->hw;
13646 	struct i40e_netdev_priv *np;
13647 	struct net_device *netdev;
13648 	u8 broadcast[ETH_ALEN];
13649 	u8 mac_addr[ETH_ALEN];
13650 	int etherdev_size;
13651 	netdev_features_t hw_enc_features;
13652 	netdev_features_t hw_features;
13653 
13654 	etherdev_size = sizeof(struct i40e_netdev_priv);
13655 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13656 	if (!netdev)
13657 		return -ENOMEM;
13658 
13659 	vsi->netdev = netdev;
13660 	np = netdev_priv(netdev);
13661 	np->vsi = vsi;
13662 
13663 	hw_enc_features = NETIF_F_SG			|
13664 			  NETIF_F_HW_CSUM		|
13665 			  NETIF_F_HIGHDMA		|
13666 			  NETIF_F_SOFT_FEATURES		|
13667 			  NETIF_F_TSO			|
13668 			  NETIF_F_TSO_ECN		|
13669 			  NETIF_F_TSO6			|
13670 			  NETIF_F_GSO_GRE		|
13671 			  NETIF_F_GSO_GRE_CSUM		|
13672 			  NETIF_F_GSO_PARTIAL		|
13673 			  NETIF_F_GSO_IPXIP4		|
13674 			  NETIF_F_GSO_IPXIP6		|
13675 			  NETIF_F_GSO_UDP_TUNNEL	|
13676 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13677 			  NETIF_F_GSO_UDP_L4		|
13678 			  NETIF_F_SCTP_CRC		|
13679 			  NETIF_F_RXHASH		|
13680 			  NETIF_F_RXCSUM		|
13681 			  0;
13682 
13683 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13684 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13685 
13686 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13687 
13688 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13689 
13690 	netdev->hw_enc_features |= hw_enc_features;
13691 
13692 	/* record features VLANs can make use of */
13693 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13694 
13695 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13696 				   NETIF_F_GSO_GRE_CSUM |	\
13697 				   NETIF_F_GSO_IPXIP4 |		\
13698 				   NETIF_F_GSO_IPXIP6 |		\
13699 				   NETIF_F_GSO_UDP_TUNNEL |	\
13700 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13701 
13702 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13703 	netdev->features |= NETIF_F_GSO_PARTIAL |
13704 			    I40E_GSO_PARTIAL_FEATURES;
13705 
13706 	netdev->mpls_features |= NETIF_F_SG;
13707 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13708 	netdev->mpls_features |= NETIF_F_TSO;
13709 	netdev->mpls_features |= NETIF_F_TSO6;
13710 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13711 
13712 	/* enable macvlan offloads */
13713 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13714 
13715 	hw_features = hw_enc_features		|
13716 		      NETIF_F_HW_VLAN_CTAG_TX	|
13717 		      NETIF_F_HW_VLAN_CTAG_RX;
13718 
13719 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13720 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13721 
13722 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13723 
13724 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13725 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13726 
13727 	netdev->features &= ~NETIF_F_HW_TC;
13728 
13729 	if (vsi->type == I40E_VSI_MAIN) {
13730 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13731 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13732 		/* The following steps are necessary for two reasons. First,
13733 		 * some older NVM configurations load a default MAC-VLAN
13734 		 * filter that will accept any tagged packet, and we want to
13735 		 * replace this with a normal filter. Additionally, it is
13736 		 * possible our MAC address was provided by the platform using
13737 		 * Open Firmware or similar.
13738 		 *
13739 		 * Thus, we need to remove the default filter and install one
13740 		 * specific to the MAC address.
13741 		 */
13742 		i40e_rm_default_mac_filter(vsi, mac_addr);
13743 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13744 		i40e_add_mac_filter(vsi, mac_addr);
13745 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13746 
13747 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13748 				       NETDEV_XDP_ACT_REDIRECT |
13749 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13750 				       NETDEV_XDP_ACT_RX_SG;
13751 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13752 	} else {
13753 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13754 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13755 		 * the end, which is 4 bytes long, so force truncation of the
13756 		 * original name by IFNAMSIZ - 4
13757 		 */
13758 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
13759 
13760 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
13761 			 main_vsi->netdev->name);
13762 		eth_random_addr(mac_addr);
13763 
13764 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13765 		i40e_add_mac_filter(vsi, mac_addr);
13766 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13767 	}
13768 
13769 	/* Add the broadcast filter so that we initially will receive
13770 	 * broadcast packets. Note that when a new VLAN is first added the
13771 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13772 	 * specific filters as part of transitioning into "vlan" operation.
13773 	 * When more VLANs are added, the driver will copy each existing MAC
13774 	 * filter and add it for the new VLAN.
13775 	 *
13776 	 * Broadcast filters are handled specially by
13777 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13778 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13779 	 * filter. The subtask will update the correct broadcast promiscuous
13780 	 * bits as VLANs become active or inactive.
13781 	 */
13782 	eth_broadcast_addr(broadcast);
13783 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13784 	i40e_add_mac_filter(vsi, broadcast);
13785 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13786 
13787 	eth_hw_addr_set(netdev, mac_addr);
13788 	ether_addr_copy(netdev->perm_addr, mac_addr);
13789 
13790 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13791 	netdev->neigh_priv_len = sizeof(u32) * 4;
13792 
13793 	netdev->priv_flags |= IFF_UNICAST_FLT;
13794 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13795 	/* Setup netdev TC information */
13796 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13797 
13798 	netdev->netdev_ops = &i40e_netdev_ops;
13799 	netdev->watchdog_timeo = 5 * HZ;
13800 	i40e_set_ethtool_ops(netdev);
13801 
13802 	/* MTU range: 68 - 9706 */
13803 	netdev->min_mtu = ETH_MIN_MTU;
13804 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13805 
13806 	return 0;
13807 }
13808 
13809 /**
13810  * i40e_vsi_delete - Delete a VSI from the switch
13811  * @vsi: the VSI being removed
13812  *
13813  * Returns 0 on success, negative value on failure
13814  **/
13815 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13816 {
13817 	/* remove default VSI is not allowed */
13818 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13819 		return;
13820 
13821 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13822 }
13823 
13824 /**
13825  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13826  * @vsi: the VSI being queried
13827  *
13828  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13829  **/
13830 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13831 {
13832 	struct i40e_veb *veb;
13833 	struct i40e_pf *pf = vsi->back;
13834 
13835 	/* Uplink is not a bridge so default to VEB */
13836 	if (vsi->veb_idx >= I40E_MAX_VEB)
13837 		return 1;
13838 
13839 	veb = pf->veb[vsi->veb_idx];
13840 	if (!veb) {
13841 		dev_info(&pf->pdev->dev,
13842 			 "There is no veb associated with the bridge\n");
13843 		return -ENOENT;
13844 	}
13845 
13846 	/* Uplink is a bridge in VEPA mode */
13847 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13848 		return 0;
13849 	} else {
13850 		/* Uplink is a bridge in VEB mode */
13851 		return 1;
13852 	}
13853 
13854 	/* VEPA is now default bridge, so return 0 */
13855 	return 0;
13856 }
13857 
13858 /**
13859  * i40e_add_vsi - Add a VSI to the switch
13860  * @vsi: the VSI being configured
13861  *
13862  * This initializes a VSI context depending on the VSI type to be added and
13863  * passes it down to the add_vsi aq command.
13864  **/
13865 static int i40e_add_vsi(struct i40e_vsi *vsi)
13866 {
13867 	int ret = -ENODEV;
13868 	struct i40e_pf *pf = vsi->back;
13869 	struct i40e_hw *hw = &pf->hw;
13870 	struct i40e_vsi_context ctxt;
13871 	struct i40e_mac_filter *f;
13872 	struct hlist_node *h;
13873 	int bkt;
13874 
13875 	u8 enabled_tc = 0x1; /* TC0 enabled */
13876 	int f_count = 0;
13877 
13878 	memset(&ctxt, 0, sizeof(ctxt));
13879 	switch (vsi->type) {
13880 	case I40E_VSI_MAIN:
13881 		/* The PF's main VSI is already setup as part of the
13882 		 * device initialization, so we'll not bother with
13883 		 * the add_vsi call, but we will retrieve the current
13884 		 * VSI context.
13885 		 */
13886 		ctxt.seid = pf->main_vsi_seid;
13887 		ctxt.pf_num = pf->hw.pf_id;
13888 		ctxt.vf_num = 0;
13889 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13890 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13891 		if (ret) {
13892 			dev_info(&pf->pdev->dev,
13893 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13894 				 ERR_PTR(ret),
13895 				 i40e_aq_str(&pf->hw,
13896 					     pf->hw.aq.asq_last_status));
13897 			return -ENOENT;
13898 		}
13899 		vsi->info = ctxt.info;
13900 		vsi->info.valid_sections = 0;
13901 
13902 		vsi->seid = ctxt.seid;
13903 		vsi->id = ctxt.vsi_number;
13904 
13905 		enabled_tc = i40e_pf_get_tc_map(pf);
13906 
13907 		/* Source pruning is enabled by default, so the flag is
13908 		 * negative logic - if it's set, we need to fiddle with
13909 		 * the VSI to disable source pruning.
13910 		 */
13911 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13912 			memset(&ctxt, 0, sizeof(ctxt));
13913 			ctxt.seid = pf->main_vsi_seid;
13914 			ctxt.pf_num = pf->hw.pf_id;
13915 			ctxt.vf_num = 0;
13916 			ctxt.info.valid_sections |=
13917 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13918 			ctxt.info.switch_id =
13919 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13920 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13921 			if (ret) {
13922 				dev_info(&pf->pdev->dev,
13923 					 "update vsi failed, err %d aq_err %s\n",
13924 					 ret,
13925 					 i40e_aq_str(&pf->hw,
13926 						     pf->hw.aq.asq_last_status));
13927 				ret = -ENOENT;
13928 				goto err;
13929 			}
13930 		}
13931 
13932 		/* MFP mode setup queue map and update VSI */
13933 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13934 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13935 			memset(&ctxt, 0, sizeof(ctxt));
13936 			ctxt.seid = pf->main_vsi_seid;
13937 			ctxt.pf_num = pf->hw.pf_id;
13938 			ctxt.vf_num = 0;
13939 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13940 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13941 			if (ret) {
13942 				dev_info(&pf->pdev->dev,
13943 					 "update vsi failed, err %pe aq_err %s\n",
13944 					 ERR_PTR(ret),
13945 					 i40e_aq_str(&pf->hw,
13946 						    pf->hw.aq.asq_last_status));
13947 				ret = -ENOENT;
13948 				goto err;
13949 			}
13950 			/* update the local VSI info queue map */
13951 			i40e_vsi_update_queue_map(vsi, &ctxt);
13952 			vsi->info.valid_sections = 0;
13953 		} else {
13954 			/* Default/Main VSI is only enabled for TC0
13955 			 * reconfigure it to enable all TCs that are
13956 			 * available on the port in SFP mode.
13957 			 * For MFP case the iSCSI PF would use this
13958 			 * flow to enable LAN+iSCSI TC.
13959 			 */
13960 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13961 			if (ret) {
13962 				/* Single TC condition is not fatal,
13963 				 * message and continue
13964 				 */
13965 				dev_info(&pf->pdev->dev,
13966 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
13967 					 enabled_tc,
13968 					 ERR_PTR(ret),
13969 					 i40e_aq_str(&pf->hw,
13970 						    pf->hw.aq.asq_last_status));
13971 			}
13972 		}
13973 		break;
13974 
13975 	case I40E_VSI_FDIR:
13976 		ctxt.pf_num = hw->pf_id;
13977 		ctxt.vf_num = 0;
13978 		ctxt.uplink_seid = vsi->uplink_seid;
13979 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13980 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13981 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
13982 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13983 			ctxt.info.valid_sections |=
13984 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13985 			ctxt.info.switch_id =
13986 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13987 		}
13988 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13989 		break;
13990 
13991 	case I40E_VSI_VMDQ2:
13992 		ctxt.pf_num = hw->pf_id;
13993 		ctxt.vf_num = 0;
13994 		ctxt.uplink_seid = vsi->uplink_seid;
13995 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13996 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13997 
13998 		/* This VSI is connected to VEB so the switch_id
13999 		 * should be set to zero by default.
14000 		 */
14001 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14002 			ctxt.info.valid_sections |=
14003 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14004 			ctxt.info.switch_id =
14005 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14006 		}
14007 
14008 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14009 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14010 		break;
14011 
14012 	case I40E_VSI_SRIOV:
14013 		ctxt.pf_num = hw->pf_id;
14014 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14015 		ctxt.uplink_seid = vsi->uplink_seid;
14016 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14017 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14018 
14019 		/* This VSI is connected to VEB so the switch_id
14020 		 * should be set to zero by default.
14021 		 */
14022 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14023 			ctxt.info.valid_sections |=
14024 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14025 			ctxt.info.switch_id =
14026 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14027 		}
14028 
14029 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14030 			ctxt.info.valid_sections |=
14031 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14032 			ctxt.info.queueing_opt_flags |=
14033 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14034 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14035 		}
14036 
14037 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14038 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14039 		if (pf->vf[vsi->vf_id].spoofchk) {
14040 			ctxt.info.valid_sections |=
14041 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14042 			ctxt.info.sec_flags |=
14043 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14044 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14045 		}
14046 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14047 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14048 		break;
14049 
14050 	case I40E_VSI_IWARP:
14051 		/* send down message to iWARP */
14052 		break;
14053 
14054 	default:
14055 		return -ENODEV;
14056 	}
14057 
14058 	if (vsi->type != I40E_VSI_MAIN) {
14059 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14060 		if (ret) {
14061 			dev_info(&vsi->back->pdev->dev,
14062 				 "add vsi failed, err %pe aq_err %s\n",
14063 				 ERR_PTR(ret),
14064 				 i40e_aq_str(&pf->hw,
14065 					     pf->hw.aq.asq_last_status));
14066 			ret = -ENOENT;
14067 			goto err;
14068 		}
14069 		vsi->info = ctxt.info;
14070 		vsi->info.valid_sections = 0;
14071 		vsi->seid = ctxt.seid;
14072 		vsi->id = ctxt.vsi_number;
14073 	}
14074 
14075 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14076 	vsi->active_filters = 0;
14077 	/* If macvlan filters already exist, force them to get loaded */
14078 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14079 		f->state = I40E_FILTER_NEW;
14080 		f_count++;
14081 	}
14082 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14083 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14084 
14085 	if (f_count) {
14086 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14087 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14088 	}
14089 
14090 	/* Update VSI BW information */
14091 	ret = i40e_vsi_get_bw_info(vsi);
14092 	if (ret) {
14093 		dev_info(&pf->pdev->dev,
14094 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14095 			 ERR_PTR(ret),
14096 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14097 		/* VSI is already added so not tearing that up */
14098 		ret = 0;
14099 	}
14100 
14101 err:
14102 	return ret;
14103 }
14104 
14105 /**
14106  * i40e_vsi_release - Delete a VSI and free its resources
14107  * @vsi: the VSI being removed
14108  *
14109  * Returns 0 on success or < 0 on error
14110  **/
14111 int i40e_vsi_release(struct i40e_vsi *vsi)
14112 {
14113 	struct i40e_mac_filter *f;
14114 	struct hlist_node *h;
14115 	struct i40e_veb *veb;
14116 	struct i40e_pf *pf;
14117 	u16 uplink_seid;
14118 	int i, n, bkt;
14119 
14120 	pf = vsi->back;
14121 
14122 	/* release of a VEB-owner or last VSI is not allowed */
14123 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14124 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14125 			 vsi->seid, vsi->uplink_seid);
14126 		return -ENODEV;
14127 	}
14128 	if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
14129 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14130 		return -ENODEV;
14131 	}
14132 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14133 	uplink_seid = vsi->uplink_seid;
14134 
14135 	if (vsi->type != I40E_VSI_SRIOV) {
14136 		if (vsi->netdev_registered) {
14137 			vsi->netdev_registered = false;
14138 			if (vsi->netdev) {
14139 				/* results in a call to i40e_close() */
14140 				unregister_netdev(vsi->netdev);
14141 			}
14142 		} else {
14143 			i40e_vsi_close(vsi);
14144 		}
14145 		i40e_vsi_disable_irq(vsi);
14146 	}
14147 
14148 	if (vsi->type == I40E_VSI_MAIN)
14149 		i40e_devlink_destroy_port(pf);
14150 
14151 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14152 
14153 	/* clear the sync flag on all filters */
14154 	if (vsi->netdev) {
14155 		__dev_uc_unsync(vsi->netdev, NULL);
14156 		__dev_mc_unsync(vsi->netdev, NULL);
14157 	}
14158 
14159 	/* make sure any remaining filters are marked for deletion */
14160 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14161 		__i40e_del_filter(vsi, f);
14162 
14163 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14164 
14165 	i40e_sync_vsi_filters(vsi);
14166 
14167 	i40e_vsi_delete(vsi);
14168 	i40e_vsi_free_q_vectors(vsi);
14169 	if (vsi->netdev) {
14170 		free_netdev(vsi->netdev);
14171 		vsi->netdev = NULL;
14172 	}
14173 	i40e_vsi_clear_rings(vsi);
14174 	i40e_vsi_clear(vsi);
14175 
14176 	/* If this was the last thing on the VEB, except for the
14177 	 * controlling VSI, remove the VEB, which puts the controlling
14178 	 * VSI onto the uplink port.
14179 	 *
14180 	 * Well, okay, there's one more exception here: don't remove
14181 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14182 	 * from up the network stack.
14183 	 */
14184 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14185 	if (veb && veb->uplink_seid) {
14186 		n = 0;
14187 
14188 		/* Count non-controlling VSIs present on  the VEB */
14189 		i40e_pf_for_each_vsi(pf, i, vsi)
14190 			if (vsi->uplink_seid == uplink_seid &&
14191 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14192 				n++;
14193 
14194 		/* If there is no VSI except the control one then release
14195 		 * the VEB and put the control VSI onto VEB uplink.
14196 		 */
14197 		if (!n)
14198 			i40e_veb_release(veb);
14199 	}
14200 
14201 	return 0;
14202 }
14203 
14204 /**
14205  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14206  * @vsi: ptr to the VSI
14207  *
14208  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14209  * corresponding SW VSI structure and initializes num_queue_pairs for the
14210  * newly allocated VSI.
14211  *
14212  * Returns 0 on success or negative on failure
14213  **/
14214 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14215 {
14216 	int ret = -ENOENT;
14217 	struct i40e_pf *pf = vsi->back;
14218 
14219 	if (vsi->q_vectors[0]) {
14220 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14221 			 vsi->seid);
14222 		return -EEXIST;
14223 	}
14224 
14225 	if (vsi->base_vector) {
14226 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14227 			 vsi->seid, vsi->base_vector);
14228 		return -EEXIST;
14229 	}
14230 
14231 	ret = i40e_vsi_alloc_q_vectors(vsi);
14232 	if (ret) {
14233 		dev_info(&pf->pdev->dev,
14234 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14235 			 vsi->num_q_vectors, vsi->seid, ret);
14236 		vsi->num_q_vectors = 0;
14237 		goto vector_setup_out;
14238 	}
14239 
14240 	/* In Legacy mode, we do not have to get any other vector since we
14241 	 * piggyback on the misc/ICR0 for queue interrupts.
14242 	*/
14243 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14244 		return ret;
14245 	if (vsi->num_q_vectors)
14246 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14247 						 vsi->num_q_vectors, vsi->idx);
14248 	if (vsi->base_vector < 0) {
14249 		dev_info(&pf->pdev->dev,
14250 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14251 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14252 		i40e_vsi_free_q_vectors(vsi);
14253 		ret = -ENOENT;
14254 		goto vector_setup_out;
14255 	}
14256 
14257 vector_setup_out:
14258 	return ret;
14259 }
14260 
14261 /**
14262  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14263  * @vsi: pointer to the vsi.
14264  *
14265  * This re-allocates a vsi's queue resources.
14266  *
14267  * Returns pointer to the successfully allocated and configured VSI sw struct
14268  * on success, otherwise returns NULL on failure.
14269  **/
14270 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14271 {
14272 	struct i40e_vsi *main_vsi;
14273 	u16 alloc_queue_pairs;
14274 	struct i40e_pf *pf;
14275 	int ret;
14276 
14277 	if (!vsi)
14278 		return NULL;
14279 
14280 	pf = vsi->back;
14281 
14282 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14283 	i40e_vsi_clear_rings(vsi);
14284 
14285 	i40e_vsi_free_arrays(vsi, false);
14286 	i40e_set_num_rings_in_vsi(vsi);
14287 	ret = i40e_vsi_alloc_arrays(vsi, false);
14288 	if (ret)
14289 		goto err_vsi;
14290 
14291 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14292 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14293 
14294 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14295 	if (ret < 0) {
14296 		dev_info(&pf->pdev->dev,
14297 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14298 			 alloc_queue_pairs, vsi->seid, ret);
14299 		goto err_vsi;
14300 	}
14301 	vsi->base_queue = ret;
14302 
14303 	/* Update the FW view of the VSI. Force a reset of TC and queue
14304 	 * layout configurations.
14305 	 */
14306 	main_vsi = i40e_pf_get_main_vsi(pf);
14307 	main_vsi->seid = pf->main_vsi_seid;
14308 	i40e_vsi_reconfig_tc(main_vsi);
14309 
14310 	if (vsi->type == I40E_VSI_MAIN)
14311 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14312 
14313 	/* assign it some queues */
14314 	ret = i40e_alloc_rings(vsi);
14315 	if (ret)
14316 		goto err_rings;
14317 
14318 	/* map all of the rings to the q_vectors */
14319 	i40e_vsi_map_rings_to_vectors(vsi);
14320 	return vsi;
14321 
14322 err_rings:
14323 	i40e_vsi_free_q_vectors(vsi);
14324 	if (vsi->netdev_registered) {
14325 		vsi->netdev_registered = false;
14326 		unregister_netdev(vsi->netdev);
14327 		free_netdev(vsi->netdev);
14328 		vsi->netdev = NULL;
14329 	}
14330 	if (vsi->type == I40E_VSI_MAIN)
14331 		i40e_devlink_destroy_port(pf);
14332 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14333 err_vsi:
14334 	i40e_vsi_clear(vsi);
14335 	return NULL;
14336 }
14337 
14338 /**
14339  * i40e_vsi_setup - Set up a VSI by a given type
14340  * @pf: board private structure
14341  * @type: VSI type
14342  * @uplink_seid: the switch element to link to
14343  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14344  *
14345  * This allocates the sw VSI structure and its queue resources, then add a VSI
14346  * to the identified VEB.
14347  *
14348  * Returns pointer to the successfully allocated and configure VSI sw struct on
14349  * success, otherwise returns NULL on failure.
14350  **/
14351 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14352 				u16 uplink_seid, u32 param1)
14353 {
14354 	struct i40e_vsi *vsi = NULL;
14355 	struct i40e_veb *veb = NULL;
14356 	u16 alloc_queue_pairs;
14357 	int v_idx;
14358 	int ret;
14359 
14360 	/* The requested uplink_seid must be either
14361 	 *     - the PF's port seid
14362 	 *              no VEB is needed because this is the PF
14363 	 *              or this is a Flow Director special case VSI
14364 	 *     - seid of an existing VEB
14365 	 *     - seid of a VSI that owns an existing VEB
14366 	 *     - seid of a VSI that doesn't own a VEB
14367 	 *              a new VEB is created and the VSI becomes the owner
14368 	 *     - seid of the PF VSI, which is what creates the first VEB
14369 	 *              this is a special case of the previous
14370 	 *
14371 	 * Find which uplink_seid we were given and create a new VEB if needed
14372 	 */
14373 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14374 	if (!veb && uplink_seid != pf->mac_seid) {
14375 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14376 		if (!vsi) {
14377 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14378 				 uplink_seid);
14379 			return NULL;
14380 		}
14381 
14382 		if (vsi->uplink_seid == pf->mac_seid)
14383 			veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
14384 					     vsi->tc_config.enabled_tc);
14385 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14386 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
14387 					     vsi->tc_config.enabled_tc);
14388 		if (veb) {
14389 			if (vsi->type != I40E_VSI_MAIN) {
14390 				dev_info(&vsi->back->pdev->dev,
14391 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14392 				return NULL;
14393 			}
14394 			/* We come up by default in VEPA mode if SRIOV is not
14395 			 * already enabled, in which case we can't force VEPA
14396 			 * mode.
14397 			 */
14398 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14399 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14400 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14401 			}
14402 			i40e_config_bridge_mode(veb);
14403 		}
14404 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14405 		if (!veb) {
14406 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14407 			return NULL;
14408 		}
14409 
14410 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14411 		uplink_seid = veb->seid;
14412 	}
14413 
14414 	/* get vsi sw struct */
14415 	v_idx = i40e_vsi_mem_alloc(pf, type);
14416 	if (v_idx < 0)
14417 		goto err_alloc;
14418 	vsi = pf->vsi[v_idx];
14419 	if (!vsi)
14420 		goto err_alloc;
14421 	vsi->type = type;
14422 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14423 
14424 	if (type == I40E_VSI_MAIN)
14425 		pf->lan_vsi = v_idx;
14426 	else if (type == I40E_VSI_SRIOV)
14427 		vsi->vf_id = param1;
14428 	/* assign it some queues */
14429 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14430 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14431 
14432 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14433 	if (ret < 0) {
14434 		dev_info(&pf->pdev->dev,
14435 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14436 			 alloc_queue_pairs, vsi->seid, ret);
14437 		goto err_vsi;
14438 	}
14439 	vsi->base_queue = ret;
14440 
14441 	/* get a VSI from the hardware */
14442 	vsi->uplink_seid = uplink_seid;
14443 	ret = i40e_add_vsi(vsi);
14444 	if (ret)
14445 		goto err_vsi;
14446 
14447 	switch (vsi->type) {
14448 	/* setup the netdev if needed */
14449 	case I40E_VSI_MAIN:
14450 	case I40E_VSI_VMDQ2:
14451 		ret = i40e_config_netdev(vsi);
14452 		if (ret)
14453 			goto err_netdev;
14454 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14455 		if (ret)
14456 			goto err_netdev;
14457 		if (vsi->type == I40E_VSI_MAIN) {
14458 			ret = i40e_devlink_create_port(pf);
14459 			if (ret)
14460 				goto err_netdev;
14461 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14462 		}
14463 		ret = register_netdev(vsi->netdev);
14464 		if (ret)
14465 			goto err_dl_port;
14466 		vsi->netdev_registered = true;
14467 		netif_carrier_off(vsi->netdev);
14468 #ifdef CONFIG_I40E_DCB
14469 		/* Setup DCB netlink interface */
14470 		i40e_dcbnl_setup(vsi);
14471 #endif /* CONFIG_I40E_DCB */
14472 		fallthrough;
14473 	case I40E_VSI_FDIR:
14474 		/* set up vectors and rings if needed */
14475 		ret = i40e_vsi_setup_vectors(vsi);
14476 		if (ret)
14477 			goto err_msix;
14478 
14479 		ret = i40e_alloc_rings(vsi);
14480 		if (ret)
14481 			goto err_rings;
14482 
14483 		/* map all of the rings to the q_vectors */
14484 		i40e_vsi_map_rings_to_vectors(vsi);
14485 
14486 		i40e_vsi_reset_stats(vsi);
14487 		break;
14488 	default:
14489 		/* no netdev or rings for the other VSI types */
14490 		break;
14491 	}
14492 
14493 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14494 	    vsi->type == I40E_VSI_VMDQ2) {
14495 		ret = i40e_vsi_config_rss(vsi);
14496 		if (ret)
14497 			goto err_config;
14498 	}
14499 	return vsi;
14500 
14501 err_config:
14502 	i40e_vsi_clear_rings(vsi);
14503 err_rings:
14504 	i40e_vsi_free_q_vectors(vsi);
14505 err_msix:
14506 	if (vsi->netdev_registered) {
14507 		vsi->netdev_registered = false;
14508 		unregister_netdev(vsi->netdev);
14509 		free_netdev(vsi->netdev);
14510 		vsi->netdev = NULL;
14511 	}
14512 err_dl_port:
14513 	if (vsi->type == I40E_VSI_MAIN)
14514 		i40e_devlink_destroy_port(pf);
14515 err_netdev:
14516 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14517 err_vsi:
14518 	i40e_vsi_clear(vsi);
14519 err_alloc:
14520 	return NULL;
14521 }
14522 
14523 /**
14524  * i40e_veb_get_bw_info - Query VEB BW information
14525  * @veb: the veb to query
14526  *
14527  * Query the Tx scheduler BW configuration data for given VEB
14528  **/
14529 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14530 {
14531 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14532 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14533 	struct i40e_pf *pf = veb->pf;
14534 	struct i40e_hw *hw = &pf->hw;
14535 	u32 tc_bw_max;
14536 	int ret = 0;
14537 	int i;
14538 
14539 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14540 						  &bw_data, NULL);
14541 	if (ret) {
14542 		dev_info(&pf->pdev->dev,
14543 			 "query veb bw config failed, err %pe aq_err %s\n",
14544 			 ERR_PTR(ret),
14545 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14546 		goto out;
14547 	}
14548 
14549 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14550 						   &ets_data, NULL);
14551 	if (ret) {
14552 		dev_info(&pf->pdev->dev,
14553 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14554 			 ERR_PTR(ret),
14555 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14556 		goto out;
14557 	}
14558 
14559 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14560 	veb->bw_max_quanta = ets_data.tc_bw_max;
14561 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14562 	veb->enabled_tc = ets_data.tc_valid_bits;
14563 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14564 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14565 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14566 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14567 		veb->bw_tc_limit_credits[i] =
14568 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14569 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14570 	}
14571 
14572 out:
14573 	return ret;
14574 }
14575 
14576 /**
14577  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14578  * @pf: board private structure
14579  *
14580  * On error: returns error code (negative)
14581  * On success: returns vsi index in PF (positive)
14582  **/
14583 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14584 {
14585 	int ret = -ENOENT;
14586 	struct i40e_veb *veb;
14587 	int i;
14588 
14589 	/* Need to protect the allocation of switch elements at the PF level */
14590 	mutex_lock(&pf->switch_mutex);
14591 
14592 	/* VEB list may be fragmented if VEB creation/destruction has
14593 	 * been happening.  We can afford to do a quick scan to look
14594 	 * for any free slots in the list.
14595 	 *
14596 	 * find next empty veb slot, looping back around if necessary
14597 	 */
14598 	i = 0;
14599 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14600 		i++;
14601 	if (i >= I40E_MAX_VEB) {
14602 		ret = -ENOMEM;
14603 		goto err_alloc_veb;  /* out of VEB slots! */
14604 	}
14605 
14606 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14607 	if (!veb) {
14608 		ret = -ENOMEM;
14609 		goto err_alloc_veb;
14610 	}
14611 	veb->pf = pf;
14612 	veb->idx = i;
14613 	veb->enabled_tc = 1;
14614 
14615 	pf->veb[i] = veb;
14616 	ret = i;
14617 err_alloc_veb:
14618 	mutex_unlock(&pf->switch_mutex);
14619 	return ret;
14620 }
14621 
14622 /**
14623  * i40e_switch_branch_release - Delete a branch of the switch tree
14624  * @branch: where to start deleting
14625  *
14626  * This uses recursion to find the tips of the branch to be
14627  * removed, deleting until we get back to and can delete this VEB.
14628  **/
14629 static void i40e_switch_branch_release(struct i40e_veb *branch)
14630 {
14631 	struct i40e_pf *pf = branch->pf;
14632 	u16 branch_seid = branch->seid;
14633 	u16 veb_idx = branch->idx;
14634 	struct i40e_vsi *vsi;
14635 	struct i40e_veb *veb;
14636 	int i;
14637 
14638 	/* release any VEBs on this VEB - RECURSION */
14639 	i40e_pf_for_each_veb(pf, i, veb)
14640 		if (veb->uplink_seid == branch->seid)
14641 			i40e_switch_branch_release(veb);
14642 
14643 	/* Release the VSIs on this VEB, but not the owner VSI.
14644 	 *
14645 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14646 	 *       the VEB itself, so don't use (*branch) after this loop.
14647 	 */
14648 	i40e_pf_for_each_vsi(pf, i, vsi)
14649 		if (vsi->uplink_seid == branch_seid &&
14650 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14651 			i40e_vsi_release(vsi);
14652 
14653 	/* There's one corner case where the VEB might not have been
14654 	 * removed, so double check it here and remove it if needed.
14655 	 * This case happens if the veb was created from the debugfs
14656 	 * commands and no VSIs were added to it.
14657 	 */
14658 	if (pf->veb[veb_idx])
14659 		i40e_veb_release(pf->veb[veb_idx]);
14660 }
14661 
14662 /**
14663  * i40e_veb_clear - remove veb struct
14664  * @veb: the veb to remove
14665  **/
14666 static void i40e_veb_clear(struct i40e_veb *veb)
14667 {
14668 	if (!veb)
14669 		return;
14670 
14671 	if (veb->pf) {
14672 		struct i40e_pf *pf = veb->pf;
14673 
14674 		mutex_lock(&pf->switch_mutex);
14675 		if (pf->veb[veb->idx] == veb)
14676 			pf->veb[veb->idx] = NULL;
14677 		mutex_unlock(&pf->switch_mutex);
14678 	}
14679 
14680 	kfree(veb);
14681 }
14682 
14683 /**
14684  * i40e_veb_release - Delete a VEB and free its resources
14685  * @veb: the VEB being removed
14686  **/
14687 void i40e_veb_release(struct i40e_veb *veb)
14688 {
14689 	struct i40e_vsi *vsi, *vsi_it;
14690 	struct i40e_pf *pf;
14691 	int i, n = 0;
14692 
14693 	pf = veb->pf;
14694 
14695 	/* find the remaining VSI and check for extras */
14696 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14697 		if (vsi_it->uplink_seid == veb->seid) {
14698 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14699 				vsi = vsi_it;
14700 			n++;
14701 		}
14702 
14703 	/* Floating VEB has to be empty and regular one must have
14704 	 * single owner VSI.
14705 	 */
14706 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14707 		dev_info(&pf->pdev->dev,
14708 			 "can't remove VEB %d with %d VSIs left\n",
14709 			 veb->seid, n);
14710 		return;
14711 	}
14712 
14713 	/* For regular VEB move the owner VSI to uplink port */
14714 	if (veb->uplink_seid) {
14715 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14716 		vsi->uplink_seid = veb->uplink_seid;
14717 		vsi->veb_idx = I40E_NO_VEB;
14718 	}
14719 
14720 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14721 	i40e_veb_clear(veb);
14722 }
14723 
14724 /**
14725  * i40e_add_veb - create the VEB in the switch
14726  * @veb: the VEB to be instantiated
14727  * @vsi: the controlling VSI
14728  **/
14729 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14730 {
14731 	struct i40e_pf *pf = veb->pf;
14732 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14733 	int ret;
14734 
14735 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14736 			      veb->enabled_tc, vsi ? false : true,
14737 			      &veb->seid, enable_stats, NULL);
14738 
14739 	/* get a VEB from the hardware */
14740 	if (ret) {
14741 		dev_info(&pf->pdev->dev,
14742 			 "couldn't add VEB, err %pe aq_err %s\n",
14743 			 ERR_PTR(ret),
14744 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14745 		return -EPERM;
14746 	}
14747 
14748 	/* get statistics counter */
14749 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14750 					 &veb->stats_idx, NULL, NULL, NULL);
14751 	if (ret) {
14752 		dev_info(&pf->pdev->dev,
14753 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14754 			 ERR_PTR(ret),
14755 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14756 		return -EPERM;
14757 	}
14758 	ret = i40e_veb_get_bw_info(veb);
14759 	if (ret) {
14760 		dev_info(&pf->pdev->dev,
14761 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14762 			 ERR_PTR(ret),
14763 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14764 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14765 		return -ENOENT;
14766 	}
14767 
14768 	if (vsi) {
14769 		vsi->uplink_seid = veb->seid;
14770 		vsi->veb_idx = veb->idx;
14771 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14772 	}
14773 
14774 	return 0;
14775 }
14776 
14777 /**
14778  * i40e_veb_setup - Set up a VEB
14779  * @pf: board private structure
14780  * @uplink_seid: the switch element to link to
14781  * @vsi_seid: the initial VSI seid
14782  * @enabled_tc: Enabled TC bit-map
14783  *
14784  * This allocates the sw VEB structure and links it into the switch
14785  * It is possible and legal for this to be a duplicate of an already
14786  * existing VEB.  It is also possible for both uplink and vsi seids
14787  * to be zero, in order to create a floating VEB.
14788  *
14789  * Returns pointer to the successfully allocated VEB sw struct on
14790  * success, otherwise returns NULL on failure.
14791  **/
14792 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
14793 				u16 vsi_seid, u8 enabled_tc)
14794 {
14795 	struct i40e_vsi *vsi = NULL;
14796 	struct i40e_veb *veb;
14797 	int veb_idx;
14798 	int ret;
14799 
14800 	/* if one seid is 0, the other must be 0 to create a floating relay */
14801 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14802 	    (uplink_seid + vsi_seid != 0)) {
14803 		dev_info(&pf->pdev->dev,
14804 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14805 			 uplink_seid, vsi_seid);
14806 		return NULL;
14807 	}
14808 
14809 	/* make sure there is such a vsi and uplink */
14810 	if (vsi_seid) {
14811 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14812 		if (!vsi) {
14813 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14814 				vsi_seid);
14815 			return NULL;
14816 		}
14817 	}
14818 
14819 	/* get veb sw struct */
14820 	veb_idx = i40e_veb_mem_alloc(pf);
14821 	if (veb_idx < 0)
14822 		goto err_alloc;
14823 	veb = pf->veb[veb_idx];
14824 	veb->uplink_seid = uplink_seid;
14825 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14826 
14827 	/* create the VEB in the switch */
14828 	ret = i40e_add_veb(veb, vsi);
14829 	if (ret)
14830 		goto err_veb;
14831 
14832 	if (vsi && vsi->idx == pf->lan_vsi)
14833 		pf->lan_veb = veb->idx;
14834 
14835 	return veb;
14836 
14837 err_veb:
14838 	i40e_veb_clear(veb);
14839 err_alloc:
14840 	return NULL;
14841 }
14842 
14843 /**
14844  * i40e_setup_pf_switch_element - set PF vars based on switch type
14845  * @pf: board private structure
14846  * @ele: element we are building info from
14847  * @num_reported: total number of elements
14848  * @printconfig: should we print the contents
14849  *
14850  * helper function to assist in extracting a few useful SEID values.
14851  **/
14852 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14853 				struct i40e_aqc_switch_config_element_resp *ele,
14854 				u16 num_reported, bool printconfig)
14855 {
14856 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14857 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14858 	u8 element_type = ele->element_type;
14859 	u16 seid = le16_to_cpu(ele->seid);
14860 	struct i40e_veb *veb;
14861 
14862 	if (printconfig)
14863 		dev_info(&pf->pdev->dev,
14864 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14865 			 element_type, seid, uplink_seid, downlink_seid);
14866 
14867 	switch (element_type) {
14868 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14869 		pf->mac_seid = seid;
14870 		break;
14871 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14872 		/* Main VEB? */
14873 		if (uplink_seid != pf->mac_seid)
14874 			break;
14875 		veb = i40e_pf_get_main_veb(pf);
14876 		if (!veb) {
14877 			int v;
14878 
14879 			/* find existing or else empty VEB */
14880 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14881 			if (veb) {
14882 				pf->lan_veb = veb->idx;
14883 			} else {
14884 				v = i40e_veb_mem_alloc(pf);
14885 				if (v < 0)
14886 					break;
14887 				pf->lan_veb = v;
14888 			}
14889 		}
14890 
14891 		/* Try to get again main VEB as pf->lan_veb may have changed */
14892 		veb = i40e_pf_get_main_veb(pf);
14893 		if (!veb)
14894 			break;
14895 
14896 		veb->seid = seid;
14897 		veb->uplink_seid = pf->mac_seid;
14898 		veb->pf = pf;
14899 		break;
14900 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14901 		if (num_reported != 1)
14902 			break;
14903 		/* This is immediately after a reset so we can assume this is
14904 		 * the PF's VSI
14905 		 */
14906 		pf->mac_seid = uplink_seid;
14907 		pf->main_vsi_seid = seid;
14908 		if (printconfig)
14909 			dev_info(&pf->pdev->dev,
14910 				 "pf_seid=%d main_vsi_seid=%d\n",
14911 				 downlink_seid, pf->main_vsi_seid);
14912 		break;
14913 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14914 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14915 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14916 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14917 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14918 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14919 		/* ignore these for now */
14920 		break;
14921 	default:
14922 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14923 			 element_type, seid);
14924 		break;
14925 	}
14926 }
14927 
14928 /**
14929  * i40e_fetch_switch_configuration - Get switch config from firmware
14930  * @pf: board private structure
14931  * @printconfig: should we print the contents
14932  *
14933  * Get the current switch configuration from the device and
14934  * extract a few useful SEID values.
14935  **/
14936 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14937 {
14938 	struct i40e_aqc_get_switch_config_resp *sw_config;
14939 	u16 next_seid = 0;
14940 	int ret = 0;
14941 	u8 *aq_buf;
14942 	int i;
14943 
14944 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14945 	if (!aq_buf)
14946 		return -ENOMEM;
14947 
14948 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14949 	do {
14950 		u16 num_reported, num_total;
14951 
14952 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14953 						I40E_AQ_LARGE_BUF,
14954 						&next_seid, NULL);
14955 		if (ret) {
14956 			dev_info(&pf->pdev->dev,
14957 				 "get switch config failed err %d aq_err %s\n",
14958 				 ret,
14959 				 i40e_aq_str(&pf->hw,
14960 					     pf->hw.aq.asq_last_status));
14961 			kfree(aq_buf);
14962 			return -ENOENT;
14963 		}
14964 
14965 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14966 		num_total = le16_to_cpu(sw_config->header.num_total);
14967 
14968 		if (printconfig)
14969 			dev_info(&pf->pdev->dev,
14970 				 "header: %d reported %d total\n",
14971 				 num_reported, num_total);
14972 
14973 		for (i = 0; i < num_reported; i++) {
14974 			struct i40e_aqc_switch_config_element_resp *ele =
14975 				&sw_config->element[i];
14976 
14977 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14978 						     printconfig);
14979 		}
14980 	} while (next_seid != 0);
14981 
14982 	kfree(aq_buf);
14983 	return ret;
14984 }
14985 
14986 /**
14987  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14988  * @pf: board private structure
14989  * @reinit: if the Main VSI needs to re-initialized.
14990  * @lock_acquired: indicates whether or not the lock has been acquired
14991  *
14992  * Returns 0 on success, negative value on failure
14993  **/
14994 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14995 {
14996 	struct i40e_vsi *main_vsi;
14997 	u16 flags = 0;
14998 	int ret;
14999 
15000 	/* find out what's out there already */
15001 	ret = i40e_fetch_switch_configuration(pf, false);
15002 	if (ret) {
15003 		dev_info(&pf->pdev->dev,
15004 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15005 			 ERR_PTR(ret),
15006 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15007 		return ret;
15008 	}
15009 	i40e_pf_reset_stats(pf);
15010 
15011 	/* set the switch config bit for the whole device to
15012 	 * support limited promisc or true promisc
15013 	 * when user requests promisc. The default is limited
15014 	 * promisc.
15015 	*/
15016 
15017 	if ((pf->hw.pf_id == 0) &&
15018 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
15019 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15020 		pf->last_sw_conf_flags = flags;
15021 	}
15022 
15023 	if (pf->hw.pf_id == 0) {
15024 		u16 valid_flags;
15025 
15026 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15027 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15028 						NULL);
15029 		if (ret && pf->hw.aq.asq_last_status != LIBIE_AQ_RC_ESRCH) {
15030 			dev_info(&pf->pdev->dev,
15031 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15032 				 ERR_PTR(ret),
15033 				 i40e_aq_str(&pf->hw,
15034 					     pf->hw.aq.asq_last_status));
15035 			/* not a fatal problem, just keep going */
15036 		}
15037 		pf->last_sw_conf_valid_flags = valid_flags;
15038 	}
15039 
15040 	/* first time setup */
15041 	main_vsi = i40e_pf_get_main_vsi(pf);
15042 	if (!main_vsi || reinit) {
15043 		struct i40e_veb *veb;
15044 		u16 uplink_seid;
15045 
15046 		/* Set up the PF VSI associated with the PF's main VSI
15047 		 * that is already in the HW switch
15048 		 */
15049 		veb = i40e_pf_get_main_veb(pf);
15050 		if (veb)
15051 			uplink_seid = veb->seid;
15052 		else
15053 			uplink_seid = pf->mac_seid;
15054 		if (!main_vsi)
15055 			main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
15056 						  uplink_seid, 0);
15057 		else if (reinit)
15058 			main_vsi = i40e_vsi_reinit_setup(main_vsi);
15059 		if (!main_vsi) {
15060 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15061 			i40e_cloud_filter_exit(pf);
15062 			i40e_fdir_teardown(pf);
15063 			return -EAGAIN;
15064 		}
15065 	} else {
15066 		/* force a reset of TC and queue layout configurations */
15067 		main_vsi->seid = pf->main_vsi_seid;
15068 		i40e_vsi_reconfig_tc(main_vsi);
15069 	}
15070 	i40e_vlan_stripping_disable(main_vsi);
15071 
15072 	i40e_fdir_sb_setup(pf);
15073 
15074 	/* Setup static PF queue filter control settings */
15075 	ret = i40e_setup_pf_filter_control(pf);
15076 	if (ret) {
15077 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15078 			 ret);
15079 		/* Failure here should not stop continuing other steps */
15080 	}
15081 
15082 	/* enable RSS in the HW, even for only one queue, as the stack can use
15083 	 * the hash
15084 	 */
15085 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15086 		i40e_pf_config_rss(pf);
15087 
15088 	/* fill in link information and enable LSE reporting */
15089 	i40e_link_event(pf);
15090 
15091 	i40e_ptp_init(pf);
15092 
15093 	if (!lock_acquired)
15094 		rtnl_lock();
15095 
15096 	/* repopulate tunnel port filters */
15097 	udp_tunnel_nic_reset_ntf(main_vsi->netdev);
15098 
15099 	if (!lock_acquired)
15100 		rtnl_unlock();
15101 
15102 	return ret;
15103 }
15104 
15105 /**
15106  * i40e_determine_queue_usage - Work out queue distribution
15107  * @pf: board private structure
15108  **/
15109 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15110 {
15111 	int queues_left;
15112 	int q_max;
15113 
15114 	pf->num_lan_qps = 0;
15115 
15116 	/* Find the max queues to be put into basic use.  We'll always be
15117 	 * using TC0, whether or not DCB is running, and TC0 will get the
15118 	 * big RSS set.
15119 	 */
15120 	queues_left = pf->hw.func_caps.num_tx_qp;
15121 
15122 	if ((queues_left == 1) ||
15123 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15124 		/* one qp for PF, no queues for anything else */
15125 		queues_left = 0;
15126 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15127 
15128 		/* make sure all the fancies are disabled */
15129 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15130 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15131 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15132 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15133 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15134 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15135 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15136 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15137 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15138 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15139 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15140 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15141 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15142 		/* one qp for PF */
15143 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15144 		queues_left -= pf->num_lan_qps;
15145 
15146 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15147 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15148 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15149 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15150 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15151 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15152 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15153 	} else {
15154 		/* Not enough queues for all TCs */
15155 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15156 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15157 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15158 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15159 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15160 		}
15161 
15162 		/* limit lan qps to the smaller of qps, cpus or msix */
15163 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15164 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15165 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15166 		pf->num_lan_qps = q_max;
15167 
15168 		queues_left -= pf->num_lan_qps;
15169 	}
15170 
15171 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15172 		if (queues_left > 1) {
15173 			queues_left -= 1; /* save 1 queue for FD */
15174 		} else {
15175 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15176 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15177 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15178 		}
15179 	}
15180 
15181 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15182 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15183 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15184 					(queues_left / pf->num_vf_qps));
15185 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15186 	}
15187 
15188 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15189 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15190 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15191 					  (queues_left / pf->num_vmdq_qps));
15192 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15193 	}
15194 
15195 	pf->queues_left = queues_left;
15196 	dev_dbg(&pf->pdev->dev,
15197 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15198 		pf->hw.func_caps.num_tx_qp,
15199 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15200 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15201 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15202 		queues_left);
15203 }
15204 
15205 /**
15206  * i40e_setup_pf_filter_control - Setup PF static filter control
15207  * @pf: PF to be setup
15208  *
15209  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15210  * settings. If PE/FCoE are enabled then it will also set the per PF
15211  * based filter sizes required for them. It also enables Flow director,
15212  * ethertype and macvlan type filter settings for the pf.
15213  *
15214  * Returns 0 on success, negative on failure
15215  **/
15216 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15217 {
15218 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15219 
15220 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15221 
15222 	/* Flow Director is enabled */
15223 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15224 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15225 		settings->enable_fdir = true;
15226 
15227 	/* Ethtype and MACVLAN filters enabled for PF */
15228 	settings->enable_ethtype = true;
15229 	settings->enable_macvlan = true;
15230 
15231 	if (i40e_set_filter_control(&pf->hw, settings))
15232 		return -ENOENT;
15233 
15234 	return 0;
15235 }
15236 
15237 #define INFO_STRING_LEN 255
15238 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15239 static void i40e_print_features(struct i40e_pf *pf)
15240 {
15241 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
15242 	struct i40e_hw *hw = &pf->hw;
15243 	char *buf;
15244 	int i;
15245 
15246 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15247 	if (!buf)
15248 		return;
15249 
15250 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15251 #ifdef CONFIG_PCI_IOV
15252 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15253 #endif
15254 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15255 		       pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
15256 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15257 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15258 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15259 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15260 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15261 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15262 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15263 	}
15264 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15265 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15266 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15267 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15268 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15269 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15270 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15271 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15272 	else
15273 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15274 
15275 	dev_info(&pf->pdev->dev, "%s\n", buf);
15276 	kfree(buf);
15277 	WARN_ON(i > INFO_STRING_LEN);
15278 }
15279 
15280 /**
15281  * i40e_get_platform_mac_addr - get platform-specific MAC address
15282  * @pdev: PCI device information struct
15283  * @pf: board private structure
15284  *
15285  * Look up the MAC address for the device. First we'll try
15286  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15287  * specific fallback. Otherwise, we'll default to the stored value in
15288  * firmware.
15289  **/
15290 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15291 {
15292 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15293 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15294 }
15295 
15296 /**
15297  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15298  * @fec_cfg: FEC option to set in flags
15299  * @flags: ptr to flags in which we set FEC option
15300  **/
15301 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15302 {
15303 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15304 		set_bit(I40E_FLAG_RS_FEC, flags);
15305 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15306 	}
15307 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15308 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15309 		set_bit(I40E_FLAG_RS_FEC, flags);
15310 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15311 	}
15312 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15313 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15314 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15315 		clear_bit(I40E_FLAG_RS_FEC, flags);
15316 	}
15317 	if (fec_cfg == 0) {
15318 		clear_bit(I40E_FLAG_RS_FEC, flags);
15319 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15320 	}
15321 }
15322 
15323 /**
15324  * i40e_check_recovery_mode - check if we are running transition firmware
15325  * @pf: board private structure
15326  *
15327  * Check registers indicating the firmware runs in recovery mode. Sets the
15328  * appropriate driver state.
15329  *
15330  * Returns true if the recovery mode was detected, false otherwise
15331  **/
15332 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15333 {
15334 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15335 
15336 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15337 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15338 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15339 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15340 
15341 		return true;
15342 	}
15343 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15344 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15345 
15346 	return false;
15347 }
15348 
15349 /**
15350  * i40e_pf_loop_reset - perform reset in a loop.
15351  * @pf: board private structure
15352  *
15353  * This function is useful when a NIC is about to enter recovery mode.
15354  * When a NIC's internal data structures are corrupted the NIC's
15355  * firmware is going to enter recovery mode.
15356  * Right after a POR it takes about 7 minutes for firmware to enter
15357  * recovery mode. Until that time a NIC is in some kind of intermediate
15358  * state. After that time period the NIC almost surely enters
15359  * recovery mode. The only way for a driver to detect intermediate
15360  * state is to issue a series of pf-resets and check a return value.
15361  * If a PF reset returns success then the firmware could be in recovery
15362  * mode so the caller of this code needs to check for recovery mode
15363  * if this function returns success. There is a little chance that
15364  * firmware will hang in intermediate state forever.
15365  * Since waiting 7 minutes is quite a lot of time this function waits
15366  * 10 seconds and then gives up by returning an error.
15367  *
15368  * Return 0 on success, negative on failure.
15369  **/
15370 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15371 {
15372 	/* wait max 10 seconds for PF reset to succeed */
15373 	const unsigned long time_end = jiffies + 10 * HZ;
15374 	struct i40e_hw *hw = &pf->hw;
15375 	int ret;
15376 
15377 	ret = i40e_pf_reset(hw);
15378 	while (ret != 0 && time_before(jiffies, time_end)) {
15379 		usleep_range(10000, 20000);
15380 		ret = i40e_pf_reset(hw);
15381 	}
15382 
15383 	if (ret == 0)
15384 		pf->pfr_count++;
15385 	else
15386 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15387 
15388 	return ret;
15389 }
15390 
15391 /**
15392  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15393  * @pf: board private structure
15394  *
15395  * Check FW registers to determine if FW issued unexpected EMP Reset.
15396  * Every time when unexpected EMP Reset occurs the FW increments
15397  * a counter of unexpected EMP Resets. When the counter reaches 10
15398  * the FW should enter the Recovery mode
15399  *
15400  * Returns true if FW issued unexpected EMP Reset
15401  **/
15402 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15403 {
15404 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15405 			   I40E_GL_FWSTS_FWS1B_MASK;
15406 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15407 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15408 }
15409 
15410 /**
15411  * i40e_handle_resets - handle EMP resets and PF resets
15412  * @pf: board private structure
15413  *
15414  * Handle both EMP resets and PF resets and conclude whether there are
15415  * any issues regarding these resets. If there are any issues then
15416  * generate log entry.
15417  *
15418  * Return 0 if NIC is healthy or negative value when there are issues
15419  * with resets
15420  **/
15421 static int i40e_handle_resets(struct i40e_pf *pf)
15422 {
15423 	const int pfr = i40e_pf_loop_reset(pf);
15424 	const bool is_empr = i40e_check_fw_empr(pf);
15425 
15426 	if (is_empr || pfr != 0)
15427 		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");
15428 
15429 	return is_empr ? -EIO : pfr;
15430 }
15431 
15432 /**
15433  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15434  * @pf: board private structure
15435  * @hw: ptr to the hardware info
15436  *
15437  * This function does a minimal setup of all subsystems needed for running
15438  * recovery mode.
15439  *
15440  * Returns 0 on success, negative on failure
15441  **/
15442 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15443 {
15444 	struct i40e_vsi *vsi;
15445 	int err;
15446 	int v_idx;
15447 
15448 	pci_set_drvdata(pf->pdev, pf);
15449 	pci_save_state(pf->pdev);
15450 
15451 	/* set up periodic task facility */
15452 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15453 	pf->service_timer_period = HZ;
15454 
15455 	INIT_WORK(&pf->service_task, i40e_service_task);
15456 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15457 
15458 	err = i40e_init_interrupt_scheme(pf);
15459 	if (err)
15460 		goto err_switch_setup;
15461 
15462 	/* The number of VSIs reported by the FW is the minimum guaranteed
15463 	 * to us; HW supports far more and we share the remaining pool with
15464 	 * the other PFs. We allocate space for more than the guarantee with
15465 	 * the understanding that we might not get them all later.
15466 	 */
15467 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15468 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15469 	else
15470 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15471 
15472 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15473 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15474 			  GFP_KERNEL);
15475 	if (!pf->vsi) {
15476 		err = -ENOMEM;
15477 		goto err_switch_setup;
15478 	}
15479 
15480 	/* We allocate one VSI which is needed as absolute minimum
15481 	 * in order to register the netdev
15482 	 */
15483 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15484 	if (v_idx < 0) {
15485 		err = v_idx;
15486 		goto err_switch_setup;
15487 	}
15488 	pf->lan_vsi = v_idx;
15489 	vsi = pf->vsi[v_idx];
15490 	if (!vsi) {
15491 		err = -EFAULT;
15492 		goto err_switch_setup;
15493 	}
15494 	vsi->alloc_queue_pairs = 1;
15495 	err = i40e_config_netdev(vsi);
15496 	if (err)
15497 		goto err_switch_setup;
15498 	err = register_netdev(vsi->netdev);
15499 	if (err)
15500 		goto err_switch_setup;
15501 	vsi->netdev_registered = true;
15502 	i40e_dbg_pf_init(pf);
15503 
15504 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15505 	if (err)
15506 		goto err_switch_setup;
15507 
15508 	/* tell the firmware that we're starting */
15509 	i40e_send_version(pf);
15510 
15511 	/* since everything's happy, start the service_task timer */
15512 	mod_timer(&pf->service_timer,
15513 		  round_jiffies(jiffies + pf->service_timer_period));
15514 
15515 	return 0;
15516 
15517 err_switch_setup:
15518 	i40e_reset_interrupt_capability(pf);
15519 	timer_shutdown_sync(&pf->service_timer);
15520 	i40e_shutdown_adminq(hw);
15521 	iounmap(hw->hw_addr);
15522 	pci_release_mem_regions(pf->pdev);
15523 	pci_disable_device(pf->pdev);
15524 	i40e_free_pf(pf);
15525 
15526 	return err;
15527 }
15528 
15529 /**
15530  * i40e_set_subsystem_device_id - set subsystem device id
15531  * @hw: pointer to the hardware info
15532  *
15533  * Set PCI subsystem device id either from a pci_dev structure or
15534  * a specific FW register.
15535  **/
15536 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15537 {
15538 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15539 
15540 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15541 		pf->pdev->subsystem_device :
15542 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15543 }
15544 
15545 /**
15546  * i40e_probe - Device initialization routine
15547  * @pdev: PCI device information struct
15548  * @ent: entry in i40e_pci_tbl
15549  *
15550  * i40e_probe initializes a PF identified by a pci_dev structure.
15551  * The OS initialization, configuring of the PF private structure,
15552  * and a hardware reset occur.
15553  *
15554  * Returns 0 on success, negative on failure
15555  **/
15556 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15557 {
15558 	struct i40e_aq_get_phy_abilities_resp abilities;
15559 #ifdef CONFIG_I40E_DCB
15560 	enum i40e_get_fw_lldp_status_resp lldp_status;
15561 #endif /* CONFIG_I40E_DCB */
15562 	struct i40e_vsi *vsi;
15563 	struct i40e_pf *pf;
15564 	struct i40e_hw *hw;
15565 	u16 wol_nvm_bits;
15566 	char nvm_ver[32];
15567 	u16 link_status;
15568 #ifdef CONFIG_I40E_DCB
15569 	int status;
15570 #endif /* CONFIG_I40E_DCB */
15571 	int err;
15572 	u32 val;
15573 
15574 	err = pci_enable_device_mem(pdev);
15575 	if (err)
15576 		return err;
15577 
15578 	/* set up for high or low dma */
15579 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15580 	if (err) {
15581 		dev_err(&pdev->dev,
15582 			"DMA configuration failed: 0x%x\n", err);
15583 		goto err_dma;
15584 	}
15585 
15586 	/* set up pci connections */
15587 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15588 	if (err) {
15589 		dev_info(&pdev->dev,
15590 			 "pci_request_selected_regions failed %d\n", err);
15591 		goto err_pci_reg;
15592 	}
15593 
15594 	pci_set_master(pdev);
15595 
15596 	/* Now that we have a PCI connection, we need to do the
15597 	 * low level device setup.  This is primarily setting up
15598 	 * the Admin Queue structures and then querying for the
15599 	 * device's current profile information.
15600 	 */
15601 	pf = i40e_alloc_pf(&pdev->dev);
15602 	if (!pf) {
15603 		err = -ENOMEM;
15604 		goto err_pf_alloc;
15605 	}
15606 	pf->next_vsi = 0;
15607 	pf->pdev = pdev;
15608 	set_bit(__I40E_DOWN, pf->state);
15609 
15610 	hw = &pf->hw;
15611 
15612 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15613 				I40E_MAX_CSR_SPACE);
15614 	/* We believe that the highest register to read is
15615 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15616 	 * is not less than that before mapping to prevent a
15617 	 * kernel panic.
15618 	 */
15619 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15620 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15621 			pf->ioremap_len);
15622 		err = -ENOMEM;
15623 		goto err_ioremap;
15624 	}
15625 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15626 	if (!hw->hw_addr) {
15627 		err = -EIO;
15628 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15629 			 (unsigned int)pci_resource_start(pdev, 0),
15630 			 pf->ioremap_len, err);
15631 		goto err_ioremap;
15632 	}
15633 	hw->vendor_id = pdev->vendor;
15634 	hw->device_id = pdev->device;
15635 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15636 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15637 	i40e_set_subsystem_device_id(hw);
15638 	hw->bus.device = PCI_SLOT(pdev->devfn);
15639 	hw->bus.func = PCI_FUNC(pdev->devfn);
15640 	hw->bus.bus_id = pdev->bus->number;
15641 
15642 	/* Select something other than the 802.1ad ethertype for the
15643 	 * switch to use internally and drop on ingress.
15644 	 */
15645 	hw->switch_tag = 0xffff;
15646 	hw->first_tag = ETH_P_8021AD;
15647 	hw->second_tag = ETH_P_8021Q;
15648 
15649 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15650 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15651 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15652 
15653 	/* set up the locks for the AQ, do this only once in probe
15654 	 * and destroy them only once in remove
15655 	 */
15656 	mutex_init(&hw->aq.asq_mutex);
15657 	mutex_init(&hw->aq.arq_mutex);
15658 
15659 	pf->msg_enable = netif_msg_init(debug,
15660 					NETIF_MSG_DRV |
15661 					NETIF_MSG_PROBE |
15662 					NETIF_MSG_LINK);
15663 	if (debug < -1)
15664 		pf->hw.debug_mask = debug;
15665 
15666 	/* do a special CORER for clearing PXE mode once at init */
15667 	if (hw->revision_id == 0 &&
15668 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15669 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15670 		i40e_flush(hw);
15671 		msleep(200);
15672 		pf->corer_count++;
15673 
15674 		i40e_clear_pxe_mode(hw);
15675 	}
15676 
15677 	/* Reset here to make sure all is clean and to define PF 'n' */
15678 	i40e_clear_hw(hw);
15679 
15680 	err = i40e_set_mac_type(hw);
15681 	if (err) {
15682 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15683 			 err);
15684 		goto err_pf_reset;
15685 	}
15686 
15687 	err = i40e_handle_resets(pf);
15688 	if (err)
15689 		goto err_pf_reset;
15690 
15691 	i40e_check_recovery_mode(pf);
15692 
15693 	if (is_kdump_kernel()) {
15694 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15695 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15696 	} else {
15697 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15698 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15699 	}
15700 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15701 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15702 
15703 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15704 		 "%s-%s:misc",
15705 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15706 
15707 	err = i40e_init_shared_code(hw);
15708 	if (err) {
15709 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15710 			 err);
15711 		goto err_pf_reset;
15712 	}
15713 
15714 	/* set up a default setting for link flow control */
15715 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15716 
15717 	err = i40e_init_adminq(hw);
15718 	if (err) {
15719 		if (err == -EIO)
15720 			dev_info(&pdev->dev,
15721 				 "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",
15722 				 hw->aq.api_maj_ver,
15723 				 hw->aq.api_min_ver,
15724 				 I40E_FW_API_VERSION_MAJOR,
15725 				 I40E_FW_MINOR_VERSION(hw));
15726 		else
15727 			dev_info(&pdev->dev,
15728 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15729 
15730 		goto err_pf_reset;
15731 	}
15732 	i40e_get_oem_version(hw);
15733 	i40e_get_pba_string(hw);
15734 
15735 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15736 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15737 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15738 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15739 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15740 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15741 		 hw->subsystem_device_id);
15742 
15743 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15744 				  I40E_FW_MINOR_VERSION(hw) + 1))
15745 		dev_dbg(&pdev->dev,
15746 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15747 			 hw->aq.api_maj_ver,
15748 			 hw->aq.api_min_ver,
15749 			 I40E_FW_API_VERSION_MAJOR,
15750 			 I40E_FW_MINOR_VERSION(hw));
15751 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15752 		dev_info(&pdev->dev,
15753 			 "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",
15754 			 hw->aq.api_maj_ver,
15755 			 hw->aq.api_min_ver,
15756 			 I40E_FW_API_VERSION_MAJOR,
15757 			 I40E_FW_MINOR_VERSION(hw));
15758 
15759 	i40e_verify_eeprom(pf);
15760 
15761 	/* Rev 0 hardware was never productized */
15762 	if (hw->revision_id < 1)
15763 		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");
15764 
15765 	i40e_clear_pxe_mode(hw);
15766 
15767 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15768 	if (err)
15769 		goto err_adminq_setup;
15770 
15771 	err = i40e_sw_init(pf);
15772 	if (err) {
15773 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15774 		goto err_sw_init;
15775 	}
15776 
15777 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15778 		return i40e_init_recovery_mode(pf, hw);
15779 
15780 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15781 				hw->func_caps.num_rx_qp, 0, 0);
15782 	if (err) {
15783 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15784 		goto err_init_lan_hmc;
15785 	}
15786 
15787 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15788 	if (err) {
15789 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15790 		err = -ENOENT;
15791 		goto err_configure_lan_hmc;
15792 	}
15793 
15794 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15795 	 * Ignore error return codes because if it was already disabled via
15796 	 * hardware settings this will fail
15797 	 */
15798 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15799 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15800 		i40e_aq_stop_lldp(hw, true, false, NULL);
15801 	}
15802 
15803 	/* allow a platform config to override the HW addr */
15804 	i40e_get_platform_mac_addr(pdev, pf);
15805 
15806 	if (!is_valid_ether_addr(hw->mac.addr)) {
15807 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15808 		err = -EIO;
15809 		goto err_mac_addr;
15810 	}
15811 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15812 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15813 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15814 	if (is_valid_ether_addr(hw->mac.port_addr))
15815 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15816 
15817 	i40e_ptp_alloc_pins(pf);
15818 	pci_set_drvdata(pdev, pf);
15819 	pci_save_state(pdev);
15820 
15821 #ifdef CONFIG_I40E_DCB
15822 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15823 	(!status &&
15824 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15825 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15826 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15827 	dev_info(&pdev->dev,
15828 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15829 			"FW LLDP is disabled\n" :
15830 			"FW LLDP is enabled\n");
15831 
15832 	/* Enable FW to write default DCB config on link-up */
15833 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15834 
15835 	err = i40e_init_pf_dcb(pf);
15836 	if (err) {
15837 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15838 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15839 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15840 		/* Continue without DCB enabled */
15841 	}
15842 #endif /* CONFIG_I40E_DCB */
15843 
15844 	/* set up periodic task facility */
15845 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15846 	pf->service_timer_period = HZ;
15847 
15848 	INIT_WORK(&pf->service_task, i40e_service_task);
15849 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15850 
15851 	/* NVM bit on means WoL disabled for the port */
15852 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15853 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15854 		pf->wol_en = false;
15855 	else
15856 		pf->wol_en = true;
15857 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15858 
15859 	/* set up the main switch operations */
15860 	i40e_determine_queue_usage(pf);
15861 	err = i40e_init_interrupt_scheme(pf);
15862 	if (err)
15863 		goto err_switch_setup;
15864 
15865 	/* Reduce Tx and Rx pairs for kdump
15866 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15867 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15868 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15869 	 */
15870 	if (is_kdump_kernel())
15871 		pf->num_lan_msix = 1;
15872 
15873 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15874 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15875 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15876 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15877 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15878 						    UDP_TUNNEL_TYPE_GENEVE;
15879 
15880 	/* The number of VSIs reported by the FW is the minimum guaranteed
15881 	 * to us; HW supports far more and we share the remaining pool with
15882 	 * the other PFs. We allocate space for more than the guarantee with
15883 	 * the understanding that we might not get them all later.
15884 	 */
15885 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15886 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15887 	else
15888 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15889 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15890 		dev_warn(&pf->pdev->dev,
15891 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15892 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15893 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15894 	}
15895 
15896 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15897 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15898 			  GFP_KERNEL);
15899 	if (!pf->vsi) {
15900 		err = -ENOMEM;
15901 		goto err_switch_setup;
15902 	}
15903 
15904 #ifdef CONFIG_PCI_IOV
15905 	/* prep for VF support */
15906 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15907 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15908 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15909 		if (pci_num_vf(pdev))
15910 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15911 	}
15912 #endif
15913 	err = i40e_setup_pf_switch(pf, false, false);
15914 	if (err) {
15915 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15916 		goto err_vsis;
15917 	}
15918 
15919 	vsi = i40e_pf_get_main_vsi(pf);
15920 	INIT_LIST_HEAD(&vsi->ch_list);
15921 
15922 	/* if FDIR VSI was set up, start it now */
15923 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
15924 	if (vsi)
15925 		i40e_vsi_open(vsi);
15926 
15927 	/* The driver only wants link up/down and module qualification
15928 	 * reports from firmware.  Note the negative logic.
15929 	 */
15930 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15931 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15932 					 I40E_AQ_EVENT_MEDIA_NA |
15933 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15934 	if (err)
15935 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15936 			 ERR_PTR(err),
15937 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15938 
15939 	/* VF MDD event logs are rate limited to one second intervals */
15940 	ratelimit_state_init(&pf->mdd_message_rate_limit, 1 * HZ, 1);
15941 
15942 	/* Reconfigure hardware for allowing smaller MSS in the case
15943 	 * of TSO, so that we avoid the MDD being fired and causing
15944 	 * a reset in the case of small MSS+TSO.
15945 	 */
15946 	val = rd32(hw, I40E_REG_MSS);
15947 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15948 		val &= ~I40E_REG_MSS_MIN_MASK;
15949 		val |= I40E_64BYTE_MSS;
15950 		wr32(hw, I40E_REG_MSS, val);
15951 	}
15952 
15953 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
15954 		msleep(75);
15955 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15956 		if (err)
15957 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
15958 				 ERR_PTR(err),
15959 				 i40e_aq_str(&pf->hw,
15960 					     pf->hw.aq.asq_last_status));
15961 	}
15962 	/* The main driver is (mostly) up and happy. We need to set this state
15963 	 * before setting up the misc vector or we get a race and the vector
15964 	 * ends up disabled forever.
15965 	 */
15966 	clear_bit(__I40E_DOWN, pf->state);
15967 
15968 	/* In case of MSIX we are going to setup the misc vector right here
15969 	 * to handle admin queue events etc. In case of legacy and MSI
15970 	 * the misc functionality and queue processing is combined in
15971 	 * the same vector and that gets setup at open.
15972 	 */
15973 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15974 		err = i40e_setup_misc_vector(pf);
15975 		if (err) {
15976 			dev_info(&pdev->dev,
15977 				 "setup of misc vector failed: %d\n", err);
15978 			i40e_cloud_filter_exit(pf);
15979 			i40e_fdir_teardown(pf);
15980 			goto err_vsis;
15981 		}
15982 	}
15983 
15984 #ifdef CONFIG_PCI_IOV
15985 	/* prep for VF support */
15986 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15987 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15988 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15989 		/* disable link interrupts for VFs */
15990 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15991 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15992 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15993 		i40e_flush(hw);
15994 
15995 		if (pci_num_vf(pdev)) {
15996 			dev_info(&pdev->dev,
15997 				 "Active VFs found, allocating resources.\n");
15998 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15999 			if (err)
16000 				dev_info(&pdev->dev,
16001 					 "Error %d allocating resources for existing VFs\n",
16002 					 err);
16003 		}
16004 	}
16005 #endif /* CONFIG_PCI_IOV */
16006 
16007 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16008 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16009 						      pf->num_iwarp_msix,
16010 						      I40E_IWARP_IRQ_PILE_ID);
16011 		if (pf->iwarp_base_vector < 0) {
16012 			dev_info(&pdev->dev,
16013 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16014 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16015 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
16016 		}
16017 	}
16018 
16019 	i40e_dbg_pf_init(pf);
16020 
16021 	/* tell the firmware that we're starting */
16022 	i40e_send_version(pf);
16023 
16024 	/* since everything's happy, start the service_task timer */
16025 	mod_timer(&pf->service_timer,
16026 		  round_jiffies(jiffies + pf->service_timer_period));
16027 
16028 	/* add this PF to client device list and launch a client service task */
16029 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16030 		err = i40e_lan_add_device(pf);
16031 		if (err)
16032 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16033 				 err);
16034 	}
16035 
16036 #define PCI_SPEED_SIZE 8
16037 #define PCI_WIDTH_SIZE 8
16038 	/* Devices on the IOSF bus do not have this information
16039 	 * and will report PCI Gen 1 x 1 by default so don't bother
16040 	 * checking them.
16041 	 */
16042 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16043 		char speed[PCI_SPEED_SIZE] = "Unknown";
16044 		char width[PCI_WIDTH_SIZE] = "Unknown";
16045 
16046 		/* Get the negotiated link width and speed from PCI config
16047 		 * space
16048 		 */
16049 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16050 					  &link_status);
16051 
16052 		i40e_set_pci_config_data(hw, link_status);
16053 
16054 		switch (hw->bus.speed) {
16055 		case i40e_bus_speed_8000:
16056 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16057 		case i40e_bus_speed_5000:
16058 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16059 		case i40e_bus_speed_2500:
16060 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16061 		default:
16062 			break;
16063 		}
16064 		switch (hw->bus.width) {
16065 		case i40e_bus_width_pcie_x8:
16066 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16067 		case i40e_bus_width_pcie_x4:
16068 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16069 		case i40e_bus_width_pcie_x2:
16070 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16071 		case i40e_bus_width_pcie_x1:
16072 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16073 		default:
16074 			break;
16075 		}
16076 
16077 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16078 			 speed, width);
16079 
16080 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16081 		    hw->bus.speed < i40e_bus_speed_8000) {
16082 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16083 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16084 		}
16085 	}
16086 
16087 	/* get the requested speeds from the fw */
16088 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16089 	if (err)
16090 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16091 			ERR_PTR(err),
16092 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16093 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16094 
16095 	/* set the FEC config due to the board capabilities */
16096 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16097 
16098 	/* get the supported phy types from the fw */
16099 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16100 	if (err)
16101 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16102 			ERR_PTR(err),
16103 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16104 
16105 	/* make sure the MFS hasn't been set lower than the default */
16106 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16107 	val = FIELD_GET(I40E_PRTGL_SAH_MFS_MASK,
16108 			rd32(&pf->hw, I40E_PRTGL_SAH));
16109 	if (val < MAX_FRAME_SIZE_DEFAULT)
16110 		dev_warn(&pdev->dev, "MFS for port %x (%d) has been set below the default (%d)\n",
16111 			 pf->hw.port, val, MAX_FRAME_SIZE_DEFAULT);
16112 
16113 	/* Add a filter to drop all Flow control frames from any VSI from being
16114 	 * transmitted. By doing so we stop a malicious VF from sending out
16115 	 * PAUSE or PFC frames and potentially controlling traffic for other
16116 	 * PF/VF VSIs.
16117 	 * The FW can still send Flow control frames if enabled.
16118 	 */
16119 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16120 						       pf->main_vsi_seid);
16121 
16122 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16123 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16124 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16125 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16126 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16127 	/* print a string summarizing features */
16128 	i40e_print_features(pf);
16129 
16130 	i40e_devlink_register(pf);
16131 
16132 	return 0;
16133 
16134 	/* Unwind what we've done if something failed in the setup */
16135 err_vsis:
16136 	set_bit(__I40E_DOWN, pf->state);
16137 	i40e_clear_interrupt_scheme(pf);
16138 	kfree(pf->vsi);
16139 err_switch_setup:
16140 	i40e_reset_interrupt_capability(pf);
16141 	timer_shutdown_sync(&pf->service_timer);
16142 err_mac_addr:
16143 err_configure_lan_hmc:
16144 	(void)i40e_shutdown_lan_hmc(hw);
16145 err_init_lan_hmc:
16146 	kfree(pf->qp_pile);
16147 err_sw_init:
16148 err_adminq_setup:
16149 err_pf_reset:
16150 	iounmap(hw->hw_addr);
16151 err_ioremap:
16152 	i40e_free_pf(pf);
16153 err_pf_alloc:
16154 	pci_release_mem_regions(pdev);
16155 err_pci_reg:
16156 err_dma:
16157 	pci_disable_device(pdev);
16158 	return err;
16159 }
16160 
16161 /**
16162  * i40e_remove - Device removal routine
16163  * @pdev: PCI device information struct
16164  *
16165  * i40e_remove is called by the PCI subsystem to alert the driver
16166  * that is should release a PCI device.  This could be caused by a
16167  * Hot-Plug event, or because the driver is going to be removed from
16168  * memory.
16169  **/
16170 static void i40e_remove(struct pci_dev *pdev)
16171 {
16172 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16173 	struct i40e_hw *hw = &pf->hw;
16174 	struct i40e_vsi *vsi;
16175 	struct i40e_veb *veb;
16176 	int ret_code;
16177 	int i;
16178 
16179 	i40e_devlink_unregister(pf);
16180 
16181 	i40e_dbg_pf_exit(pf);
16182 
16183 	i40e_ptp_stop(pf);
16184 
16185 	/* Disable RSS in hw */
16186 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16187 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16188 
16189 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16190 	 * flags, once they are set, i40e_rebuild should not be called as
16191 	 * i40e_prep_for_reset always returns early.
16192 	 */
16193 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16194 		usleep_range(1000, 2000);
16195 	set_bit(__I40E_IN_REMOVE, pf->state);
16196 
16197 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16198 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16199 		i40e_free_vfs(pf);
16200 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16201 	}
16202 	/* no more scheduling of any task */
16203 	set_bit(__I40E_SUSPENDED, pf->state);
16204 	set_bit(__I40E_DOWN, pf->state);
16205 	if (pf->service_timer.function)
16206 		timer_shutdown_sync(&pf->service_timer);
16207 	if (pf->service_task.func)
16208 		cancel_work_sync(&pf->service_task);
16209 
16210 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16211 		struct i40e_vsi *vsi = pf->vsi[0];
16212 
16213 		/* We know that we have allocated only one vsi for this PF,
16214 		 * it was just for registering netdevice, so the interface
16215 		 * could be visible in the 'ifconfig' output
16216 		 */
16217 		unregister_netdev(vsi->netdev);
16218 		free_netdev(vsi->netdev);
16219 
16220 		goto unmap;
16221 	}
16222 
16223 	/* Client close must be called explicitly here because the timer
16224 	 * has been stopped.
16225 	 */
16226 	i40e_notify_client_of_netdev_close(pf, false);
16227 
16228 	i40e_fdir_teardown(pf);
16229 
16230 	/* If there is a switch structure or any orphans, remove them.
16231 	 * This will leave only the PF's VSI remaining.
16232 	 */
16233 	i40e_pf_for_each_veb(pf, i, veb)
16234 		if (veb->uplink_seid == pf->mac_seid ||
16235 		    veb->uplink_seid == 0)
16236 			i40e_switch_branch_release(veb);
16237 
16238 	/* Now we can shutdown the PF's VSIs, just before we kill
16239 	 * adminq and hmc.
16240 	 */
16241 	i40e_pf_for_each_vsi(pf, i, vsi) {
16242 		i40e_vsi_close(vsi);
16243 		i40e_vsi_release(vsi);
16244 		pf->vsi[i] = NULL;
16245 	}
16246 
16247 	i40e_cloud_filter_exit(pf);
16248 
16249 	/* remove attached clients */
16250 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16251 		ret_code = i40e_lan_del_device(pf);
16252 		if (ret_code)
16253 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16254 				 ret_code);
16255 	}
16256 
16257 	/* shutdown and destroy the HMC */
16258 	if (hw->hmc.hmc_obj) {
16259 		ret_code = i40e_shutdown_lan_hmc(hw);
16260 		if (ret_code)
16261 			dev_warn(&pdev->dev,
16262 				 "Failed to destroy the HMC resources: %d\n",
16263 				 ret_code);
16264 	}
16265 
16266 unmap:
16267 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16268 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16269 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16270 		free_irq(pf->pdev->irq, pf);
16271 
16272 	/* shutdown the adminq */
16273 	i40e_shutdown_adminq(hw);
16274 
16275 	/* destroy the locks only once, here */
16276 	mutex_destroy(&hw->aq.arq_mutex);
16277 	mutex_destroy(&hw->aq.asq_mutex);
16278 
16279 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16280 	rtnl_lock();
16281 	i40e_clear_interrupt_scheme(pf);
16282 	i40e_pf_for_each_vsi(pf, i, vsi) {
16283 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16284 			i40e_vsi_clear_rings(vsi);
16285 
16286 		i40e_vsi_clear(vsi);
16287 		pf->vsi[i] = NULL;
16288 	}
16289 	rtnl_unlock();
16290 
16291 	i40e_pf_for_each_veb(pf, i, veb) {
16292 		kfree(veb);
16293 		pf->veb[i] = NULL;
16294 	}
16295 
16296 	kfree(pf->qp_pile);
16297 	kfree(pf->vsi);
16298 
16299 	iounmap(hw->hw_addr);
16300 	i40e_free_pf(pf);
16301 	pci_release_mem_regions(pdev);
16302 
16303 	pci_disable_device(pdev);
16304 }
16305 
16306 /**
16307  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16308  * using the mac_address_write admin q function
16309  * @pf: pointer to i40e_pf struct
16310  **/
16311 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16312 {
16313 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
16314 	struct i40e_hw *hw = &pf->hw;
16315 	u8 mac_addr[6];
16316 	u16 flags = 0;
16317 	int ret;
16318 
16319 	/* Get current MAC address in case it's an LAA */
16320 	if (main_vsi && main_vsi->netdev) {
16321 		ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
16322 	} else {
16323 		dev_err(&pf->pdev->dev,
16324 			"Failed to retrieve MAC address; using default\n");
16325 		ether_addr_copy(mac_addr, hw->mac.addr);
16326 	}
16327 
16328 	/* The FW expects the mac address write cmd to first be called with
16329 	 * one of these flags before calling it again with the multicast
16330 	 * enable flags.
16331 	 */
16332 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16333 
16334 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16335 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16336 
16337 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16338 	if (ret) {
16339 		dev_err(&pf->pdev->dev,
16340 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16341 		return;
16342 	}
16343 
16344 	flags = I40E_AQC_MC_MAG_EN
16345 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16346 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16347 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16348 	if (ret)
16349 		dev_err(&pf->pdev->dev,
16350 			"Failed to enable Multicast Magic Packet wake up\n");
16351 }
16352 
16353 /**
16354  * i40e_io_suspend - suspend all IO operations
16355  * @pf: pointer to i40e_pf struct
16356  *
16357  **/
16358 static int i40e_io_suspend(struct i40e_pf *pf)
16359 {
16360 	struct i40e_hw *hw = &pf->hw;
16361 
16362 	set_bit(__I40E_DOWN, pf->state);
16363 
16364 	/* Ensure service task will not be running */
16365 	timer_delete_sync(&pf->service_timer);
16366 	cancel_work_sync(&pf->service_task);
16367 
16368 	/* Client close must be called explicitly here because the timer
16369 	 * has been stopped.
16370 	 */
16371 	i40e_notify_client_of_netdev_close(pf, false);
16372 
16373 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16374 	    pf->wol_en)
16375 		i40e_enable_mc_magic_wake(pf);
16376 
16377 	/* Since we're going to destroy queues during the
16378 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16379 	 * whole section
16380 	 */
16381 	rtnl_lock();
16382 
16383 	i40e_prep_for_reset(pf);
16384 
16385 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16386 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16387 
16388 	/* Clear the interrupt scheme and release our IRQs so that the system
16389 	 * can safely hibernate even when there are a large number of CPUs.
16390 	 * Otherwise hibernation might fail when mapping all the vectors back
16391 	 * to CPU0.
16392 	 */
16393 	i40e_clear_interrupt_scheme(pf);
16394 
16395 	rtnl_unlock();
16396 
16397 	return 0;
16398 }
16399 
16400 /**
16401  * i40e_io_resume - resume IO operations
16402  * @pf: pointer to i40e_pf struct
16403  *
16404  **/
16405 static int i40e_io_resume(struct i40e_pf *pf)
16406 {
16407 	struct device *dev = &pf->pdev->dev;
16408 	int err;
16409 
16410 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16411 	 * since we're going to be restoring queues
16412 	 */
16413 	rtnl_lock();
16414 
16415 	/* We cleared the interrupt scheme when we suspended, so we need to
16416 	 * restore it now to resume device functionality.
16417 	 */
16418 	err = i40e_restore_interrupt_scheme(pf);
16419 	if (err) {
16420 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16421 			err);
16422 	}
16423 
16424 	clear_bit(__I40E_DOWN, pf->state);
16425 	i40e_reset_and_rebuild(pf, false, true);
16426 
16427 	rtnl_unlock();
16428 
16429 	/* Clear suspended state last after everything is recovered */
16430 	clear_bit(__I40E_SUSPENDED, pf->state);
16431 
16432 	/* Restart the service task */
16433 	mod_timer(&pf->service_timer,
16434 		  round_jiffies(jiffies + pf->service_timer_period));
16435 
16436 	return 0;
16437 }
16438 
16439 /**
16440  * i40e_pci_error_detected - warning that something funky happened in PCI land
16441  * @pdev: PCI device information struct
16442  * @error: the type of PCI error
16443  *
16444  * Called to warn that something happened and the error handling steps
16445  * are in progress.  Allows the driver to quiesce things, be ready for
16446  * remediation.
16447  **/
16448 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16449 						pci_channel_state_t error)
16450 {
16451 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16452 
16453 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16454 
16455 	if (!pf) {
16456 		dev_info(&pdev->dev,
16457 			 "Cannot recover - error happened during device probe\n");
16458 		return PCI_ERS_RESULT_DISCONNECT;
16459 	}
16460 
16461 	/* shutdown all operations */
16462 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16463 		i40e_io_suspend(pf);
16464 
16465 	/* Request a slot reset */
16466 	return PCI_ERS_RESULT_NEED_RESET;
16467 }
16468 
16469 /**
16470  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16471  * @pdev: PCI device information struct
16472  *
16473  * Called to find if the driver can work with the device now that
16474  * the pci slot has been reset.  If a basic connection seems good
16475  * (registers are readable and have sane content) then return a
16476  * happy little PCI_ERS_RESULT_xxx.
16477  **/
16478 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16479 {
16480 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16481 	pci_ers_result_t result;
16482 	u32 reg;
16483 
16484 	dev_dbg(&pdev->dev, "%s\n", __func__);
16485 	/* enable I/O and memory of the device  */
16486 	if (pci_enable_device(pdev)) {
16487 		dev_info(&pdev->dev,
16488 			 "Cannot re-enable PCI device after reset.\n");
16489 		result = PCI_ERS_RESULT_DISCONNECT;
16490 	} else {
16491 		pci_set_master(pdev);
16492 		pci_restore_state(pdev);
16493 		pci_save_state(pdev);
16494 		pci_wake_from_d3(pdev, false);
16495 
16496 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16497 		if (reg == 0)
16498 			result = PCI_ERS_RESULT_RECOVERED;
16499 		else
16500 			result = PCI_ERS_RESULT_DISCONNECT;
16501 	}
16502 
16503 	return result;
16504 }
16505 
16506 /**
16507  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16508  * @pdev: PCI device information struct
16509  */
16510 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16511 {
16512 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16513 
16514 	i40e_prep_for_reset(pf);
16515 }
16516 
16517 /**
16518  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16519  * @pdev: PCI device information struct
16520  */
16521 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16522 {
16523 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16524 
16525 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16526 		return;
16527 
16528 	i40e_reset_and_rebuild(pf, false, false);
16529 #ifdef CONFIG_PCI_IOV
16530 	i40e_restore_all_vfs_msi_state(pdev);
16531 #endif /* CONFIG_PCI_IOV */
16532 }
16533 
16534 /**
16535  * i40e_pci_error_resume - restart operations after PCI error recovery
16536  * @pdev: PCI device information struct
16537  *
16538  * Called to allow the driver to bring things back up after PCI error
16539  * and/or reset recovery has finished.
16540  **/
16541 static void i40e_pci_error_resume(struct pci_dev *pdev)
16542 {
16543 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16544 
16545 	dev_dbg(&pdev->dev, "%s\n", __func__);
16546 	if (test_bit(__I40E_SUSPENDED, pf->state))
16547 		return;
16548 
16549 	i40e_io_resume(pf);
16550 }
16551 
16552 /**
16553  * i40e_shutdown - PCI callback for shutting down
16554  * @pdev: PCI device information struct
16555  **/
16556 static void i40e_shutdown(struct pci_dev *pdev)
16557 {
16558 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16559 	struct i40e_hw *hw = &pf->hw;
16560 
16561 	set_bit(__I40E_SUSPENDED, pf->state);
16562 	set_bit(__I40E_DOWN, pf->state);
16563 
16564 	timer_delete_sync(&pf->service_timer);
16565 	cancel_work_sync(&pf->service_task);
16566 	i40e_cloud_filter_exit(pf);
16567 	i40e_fdir_teardown(pf);
16568 
16569 	/* Client close must be called explicitly here because the timer
16570 	 * has been stopped.
16571 	 */
16572 	i40e_notify_client_of_netdev_close(pf, false);
16573 
16574 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16575 	    pf->wol_en)
16576 		i40e_enable_mc_magic_wake(pf);
16577 
16578 	i40e_prep_for_reset(pf);
16579 
16580 	wr32(hw, I40E_PFPM_APM,
16581 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16582 	wr32(hw, I40E_PFPM_WUFC,
16583 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16584 
16585 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16586 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16587 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16588 		free_irq(pf->pdev->irq, pf);
16589 
16590 	/* Since we're going to destroy queues during the
16591 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16592 	 * whole section
16593 	 */
16594 	rtnl_lock();
16595 	i40e_clear_interrupt_scheme(pf);
16596 	rtnl_unlock();
16597 
16598 	if (system_state == SYSTEM_POWER_OFF) {
16599 		pci_wake_from_d3(pdev, pf->wol_en);
16600 		pci_set_power_state(pdev, PCI_D3hot);
16601 	}
16602 }
16603 
16604 /**
16605  * i40e_suspend - PM callback for moving to D3
16606  * @dev: generic device information structure
16607  **/
16608 static int i40e_suspend(struct device *dev)
16609 {
16610 	struct i40e_pf *pf = dev_get_drvdata(dev);
16611 
16612 	/* If we're already suspended, then there is nothing to do */
16613 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16614 		return 0;
16615 	return i40e_io_suspend(pf);
16616 }
16617 
16618 /**
16619  * i40e_resume - PM callback for waking up from D3
16620  * @dev: generic device information structure
16621  **/
16622 static int i40e_resume(struct device *dev)
16623 {
16624 	struct i40e_pf *pf = dev_get_drvdata(dev);
16625 
16626 	/* If we're not suspended, then there is nothing to do */
16627 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16628 		return 0;
16629 	return i40e_io_resume(pf);
16630 }
16631 
16632 static const struct pci_error_handlers i40e_err_handler = {
16633 	.error_detected = i40e_pci_error_detected,
16634 	.slot_reset = i40e_pci_error_slot_reset,
16635 	.reset_prepare = i40e_pci_error_reset_prepare,
16636 	.reset_done = i40e_pci_error_reset_done,
16637 	.resume = i40e_pci_error_resume,
16638 };
16639 
16640 static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16641 
16642 static struct pci_driver i40e_driver = {
16643 	.name     = i40e_driver_name,
16644 	.id_table = i40e_pci_tbl,
16645 	.probe    = i40e_probe,
16646 	.remove   = i40e_remove,
16647 	.driver.pm = pm_sleep_ptr(&i40e_pm_ops),
16648 	.shutdown = i40e_shutdown,
16649 	.err_handler = &i40e_err_handler,
16650 	.sriov_configure = i40e_pci_sriov_configure,
16651 };
16652 
16653 /**
16654  * i40e_init_module - Driver registration routine
16655  *
16656  * i40e_init_module is the first routine called when the driver is
16657  * loaded. All it does is register with the PCI subsystem.
16658  **/
16659 static int __init i40e_init_module(void)
16660 {
16661 	int err;
16662 
16663 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16664 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16665 
16666 	/* There is no need to throttle the number of active tasks because
16667 	 * each device limits its own task using a state bit for scheduling
16668 	 * the service task, and the device tasks do not interfere with each
16669 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16670 	 * since we need to be able to guarantee forward progress even under
16671 	 * memory pressure.
16672 	 */
16673 	i40e_wq = alloc_workqueue("%s", 0, 0, i40e_driver_name);
16674 	if (!i40e_wq) {
16675 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16676 		return -ENOMEM;
16677 	}
16678 
16679 	i40e_dbg_init();
16680 	err = pci_register_driver(&i40e_driver);
16681 	if (err) {
16682 		destroy_workqueue(i40e_wq);
16683 		i40e_dbg_exit();
16684 		return err;
16685 	}
16686 
16687 	return 0;
16688 }
16689 module_init(i40e_init_module);
16690 
16691 /**
16692  * i40e_exit_module - Driver exit cleanup routine
16693  *
16694  * i40e_exit_module is called just before the driver is removed
16695  * from memory.
16696  **/
16697 static void __exit i40e_exit_module(void)
16698 {
16699 	pci_unregister_driver(&i40e_driver);
16700 	destroy_workqueue(i40e_wq);
16701 	ida_destroy(&i40e_client_ida);
16702 	i40e_dbg_exit();
16703 }
16704 module_exit(i40e_exit_module);
16705