xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 566ab427f827b0256d3e8ce0235d088e6a9c28bd)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <generated/utsrelease.h>
5 #include <linux/crash_dump.h>
6 #include <linux/if_bridge.h>
7 #include <linux/if_macvlan.h>
8 #include <linux/module.h>
9 #include <net/pkt_cls.h>
10 #include <net/xdp_sock_drv.h>
11 
12 /* Local includes */
13 #include "i40e.h"
14 #include "i40e_devids.h"
15 #include "i40e_diag.h"
16 #include "i40e_lan_hmc.h"
17 #include "i40e_virtchnl_pf.h"
18 #include "i40e_xsk.h"
19 
20 /* All i40e tracepoints are defined by the include below, which
21  * must be included exactly once across the whole kernel with
22  * CREATE_TRACE_POINTS defined
23  */
24 #define CREATE_TRACE_POINTS
25 #include "i40e_trace.h"
26 
27 const char i40e_driver_name[] = "i40e";
28 static const char i40e_driver_string[] =
29 			"Intel(R) Ethernet Connection XL710 Network Driver";
30 
31 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
32 
33 /* a bit of forward declarations */
34 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
35 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
36 static int i40e_add_vsi(struct i40e_vsi *vsi);
37 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
38 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
39 static int i40e_setup_misc_vector(struct i40e_pf *pf);
40 static void i40e_determine_queue_usage(struct i40e_pf *pf);
41 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
42 static void i40e_prep_for_reset(struct i40e_pf *pf);
43 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
44 				   bool lock_acquired);
45 static int i40e_reset(struct i40e_pf *pf);
46 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
47 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
48 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
49 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
50 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
51 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
52 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
53 static int i40e_get_capabilities(struct i40e_pf *pf,
54 				 enum i40e_admin_queue_opc list_type);
55 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
56 
57 /* i40e_pci_tbl - PCI Device ID Table
58  *
59  * Last entry must be all 0s
60  *
61  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
62  *   Class, Class Mask, private data (not used) }
63  */
64 static const struct pci_device_id i40e_pci_tbl[] = {
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
91 	/* required last entry */
92 	{0, }
93 };
94 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95 
96 #define I40E_MAX_VF_COUNT 128
97 static int debug = -1;
98 module_param(debug, uint, 0);
99 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
100 
101 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
102 MODULE_IMPORT_NS(LIBIE);
103 MODULE_LICENSE("GPL v2");
104 
105 static struct workqueue_struct *i40e_wq;
106 
107 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
108 				  struct net_device *netdev, int delta)
109 {
110 	struct netdev_hw_addr_list *ha_list;
111 	struct netdev_hw_addr *ha;
112 
113 	if (!f || !netdev)
114 		return;
115 
116 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
117 		ha_list = &netdev->uc;
118 	else
119 		ha_list = &netdev->mc;
120 
121 	netdev_hw_addr_list_for_each(ha, ha_list) {
122 		if (ether_addr_equal(ha->addr, f->macaddr)) {
123 			ha->refcount += delta;
124 			if (ha->refcount <= 0)
125 				ha->refcount = 1;
126 			break;
127 		}
128 	}
129 }
130 
131 /**
132  * i40e_hw_to_dev - get device pointer from the hardware structure
133  * @hw: pointer to the device HW structure
134  **/
135 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
136 {
137 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
138 
139 	return &pf->pdev->dev;
140 }
141 
142 /**
143  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to fill out
146  * @size: size of memory requested
147  * @alignment: what to align the allocation to
148  **/
149 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
150 			  u64 size, u32 alignment)
151 {
152 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
153 
154 	mem->size = ALIGN(size, alignment);
155 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
156 				     GFP_KERNEL);
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_dma_mem - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
168 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
169 {
170 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
171 
172 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
173 	mem->va = NULL;
174 	mem->pa = 0;
175 	mem->size = 0;
176 
177 	return 0;
178 }
179 
180 /**
181  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
182  * @hw:   pointer to the HW structure
183  * @mem:  ptr to mem struct to fill out
184  * @size: size of memory requested
185  **/
186 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
187 			   u32 size)
188 {
189 	mem->size = size;
190 	mem->va = kzalloc(size, GFP_KERNEL);
191 
192 	if (!mem->va)
193 		return -ENOMEM;
194 
195 	return 0;
196 }
197 
198 /**
199  * i40e_free_virt_mem - OS specific memory free for shared code
200  * @hw:   pointer to the HW structure
201  * @mem:  ptr to mem struct to free
202  **/
203 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
204 {
205 	/* it's ok to kfree a NULL pointer */
206 	kfree(mem->va);
207 	mem->va = NULL;
208 	mem->size = 0;
209 
210 	return 0;
211 }
212 
213 /**
214  * i40e_get_lump - find a lump of free generic resource
215  * @pf: board private structure
216  * @pile: the pile of resource to search
217  * @needed: the number of items needed
218  * @id: an owner id to stick on the items assigned
219  *
220  * Returns the base item index of the lump, or negative for error
221  **/
222 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
223 			 u16 needed, u16 id)
224 {
225 	int ret = -ENOMEM;
226 	int i, j;
227 
228 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
229 		dev_info(&pf->pdev->dev,
230 			 "param err: pile=%s needed=%d id=0x%04x\n",
231 			 pile ? "<valid>" : "<null>", needed, id);
232 		return -EINVAL;
233 	}
234 
235 	/* Allocate last queue in the pile for FDIR VSI queue
236 	 * so it doesn't fragment the qp_pile
237 	 */
238 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
239 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
240 			dev_err(&pf->pdev->dev,
241 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
242 				pile->num_entries - 1);
243 			return -ENOMEM;
244 		}
245 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
246 		return pile->num_entries - 1;
247 	}
248 
249 	i = 0;
250 	while (i < pile->num_entries) {
251 		/* skip already allocated entries */
252 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
253 			i++;
254 			continue;
255 		}
256 
257 		/* do we have enough in this lump? */
258 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
259 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
260 				break;
261 		}
262 
263 		if (j == needed) {
264 			/* there was enough, so assign it to the requestor */
265 			for (j = 0; j < needed; j++)
266 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
267 			ret = i;
268 			break;
269 		}
270 
271 		/* not enough, so skip over it and continue looking */
272 		i += j;
273 	}
274 
275 	return ret;
276 }
277 
278 /**
279  * i40e_put_lump - return a lump of generic resource
280  * @pile: the pile of resource to search
281  * @index: the base item index
282  * @id: the owner id of the items assigned
283  *
284  * Returns the count of items in the lump
285  **/
286 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
287 {
288 	int valid_id = (id | I40E_PILE_VALID_BIT);
289 	int count = 0;
290 	u16 i;
291 
292 	if (!pile || index >= pile->num_entries)
293 		return -EINVAL;
294 
295 	for (i = index;
296 	     i < pile->num_entries && pile->list[i] == valid_id;
297 	     i++) {
298 		pile->list[i] = 0;
299 		count++;
300 	}
301 
302 
303 	return count;
304 }
305 
306 /**
307  * i40e_find_vsi_from_id - searches for the vsi with the given id
308  * @pf: the pf structure to search for the vsi
309  * @id: id of the vsi it is searching for
310  **/
311 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
312 {
313 	struct i40e_vsi *vsi;
314 	int i;
315 
316 	i40e_pf_for_each_vsi(pf, i, vsi)
317 		if (vsi->id == id)
318 			return vsi;
319 
320 	return NULL;
321 }
322 
323 /**
324  * i40e_service_event_schedule - Schedule the service task to wake up
325  * @pf: board private structure
326  *
327  * If not already scheduled, this puts the task into the work queue
328  **/
329 void i40e_service_event_schedule(struct i40e_pf *pf)
330 {
331 	if ((!test_bit(__I40E_DOWN, pf->state) &&
332 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
333 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
334 		queue_work(i40e_wq, &pf->service_task);
335 }
336 
337 /**
338  * i40e_tx_timeout - Respond to a Tx Hang
339  * @netdev: network interface device structure
340  * @txqueue: queue number timing out
341  *
342  * If any port has noticed a Tx timeout, it is likely that the whole
343  * device is munged, not just the one netdev port, so go for the full
344  * reset.
345  **/
346 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
347 {
348 	struct i40e_netdev_priv *np = netdev_priv(netdev);
349 	struct i40e_vsi *vsi = np->vsi;
350 	struct i40e_pf *pf = vsi->back;
351 	struct i40e_ring *tx_ring = NULL;
352 	unsigned int i;
353 	u32 head, val;
354 
355 	pf->tx_timeout_count++;
356 
357 	/* with txqueue index, find the tx_ring struct */
358 	for (i = 0; i < vsi->num_queue_pairs; i++) {
359 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
360 			if (txqueue ==
361 			    vsi->tx_rings[i]->queue_index) {
362 				tx_ring = vsi->tx_rings[i];
363 				break;
364 			}
365 		}
366 	}
367 
368 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
369 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
370 	else if (time_before(jiffies,
371 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
372 		return;   /* don't do any new action before the next timeout */
373 
374 	/* don't kick off another recovery if one is already pending */
375 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
376 		return;
377 
378 	if (tx_ring) {
379 		head = i40e_get_head(tx_ring);
380 		/* Read interrupt register */
381 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
382 			val = rd32(&pf->hw,
383 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
384 						tx_ring->vsi->base_vector - 1));
385 		else
386 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
387 
388 		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",
389 			    vsi->seid, txqueue, tx_ring->next_to_clean,
390 			    head, tx_ring->next_to_use,
391 			    readl(tx_ring->tail), val);
392 	}
393 
394 	pf->tx_timeout_last_recovery = jiffies;
395 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
396 		    pf->tx_timeout_recovery_level, txqueue);
397 
398 	switch (pf->tx_timeout_recovery_level) {
399 	case 1:
400 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
401 		break;
402 	case 2:
403 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
404 		break;
405 	case 3:
406 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
407 		break;
408 	default:
409 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
410 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
411 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
412 		break;
413 	}
414 
415 	i40e_service_event_schedule(pf);
416 	pf->tx_timeout_recovery_level++;
417 }
418 
419 /**
420  * i40e_get_vsi_stats_struct - Get System Network Statistics
421  * @vsi: the VSI we care about
422  *
423  * Returns the address of the device statistics structure.
424  * The statistics are actually updated from the service task.
425  **/
426 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
427 {
428 	return &vsi->net_stats;
429 }
430 
431 /**
432  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
433  * @ring: Tx ring to get statistics from
434  * @stats: statistics entry to be updated
435  **/
436 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
437 					    struct rtnl_link_stats64 *stats)
438 {
439 	u64 bytes, packets;
440 	unsigned int start;
441 
442 	do {
443 		start = u64_stats_fetch_begin(&ring->syncp);
444 		packets = ring->stats.packets;
445 		bytes   = ring->stats.bytes;
446 	} while (u64_stats_fetch_retry(&ring->syncp, start));
447 
448 	stats->tx_packets += packets;
449 	stats->tx_bytes   += bytes;
450 }
451 
452 /**
453  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
454  * @netdev: network interface device structure
455  * @stats: data structure to store statistics
456  *
457  * Returns the address of the device statistics structure.
458  * The statistics are actually updated from the service task.
459  **/
460 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
461 				  struct rtnl_link_stats64 *stats)
462 {
463 	struct i40e_netdev_priv *np = netdev_priv(netdev);
464 	struct i40e_vsi *vsi = np->vsi;
465 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
466 	struct i40e_ring *ring;
467 	int i;
468 
469 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
470 		return;
471 
472 	if (!vsi->tx_rings)
473 		return;
474 
475 	rcu_read_lock();
476 	for (i = 0; i < vsi->num_queue_pairs; i++) {
477 		u64 bytes, packets;
478 		unsigned int start;
479 
480 		ring = READ_ONCE(vsi->tx_rings[i]);
481 		if (!ring)
482 			continue;
483 		i40e_get_netdev_stats_struct_tx(ring, stats);
484 
485 		if (i40e_enabled_xdp_vsi(vsi)) {
486 			ring = READ_ONCE(vsi->xdp_rings[i]);
487 			if (!ring)
488 				continue;
489 			i40e_get_netdev_stats_struct_tx(ring, stats);
490 		}
491 
492 		ring = READ_ONCE(vsi->rx_rings[i]);
493 		if (!ring)
494 			continue;
495 		do {
496 			start   = u64_stats_fetch_begin(&ring->syncp);
497 			packets = ring->stats.packets;
498 			bytes   = ring->stats.bytes;
499 		} while (u64_stats_fetch_retry(&ring->syncp, start));
500 
501 		stats->rx_packets += packets;
502 		stats->rx_bytes   += bytes;
503 
504 	}
505 	rcu_read_unlock();
506 
507 	/* following stats updated by i40e_watchdog_subtask() */
508 	stats->multicast	= vsi_stats->multicast;
509 	stats->tx_errors	= vsi_stats->tx_errors;
510 	stats->tx_dropped	= vsi_stats->tx_dropped;
511 	stats->rx_errors	= vsi_stats->rx_errors;
512 	stats->rx_dropped	= vsi_stats->rx_dropped;
513 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
514 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
515 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
516 }
517 
518 /**
519  * i40e_vsi_reset_stats - Resets all stats of the given vsi
520  * @vsi: the VSI to have its stats reset
521  **/
522 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
523 {
524 	struct rtnl_link_stats64 *ns;
525 	int i;
526 
527 	if (!vsi)
528 		return;
529 
530 	ns = i40e_get_vsi_stats_struct(vsi);
531 	memset(ns, 0, sizeof(*ns));
532 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
533 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
534 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
535 	if (vsi->rx_rings && vsi->rx_rings[0]) {
536 		for (i = 0; i < vsi->num_queue_pairs; i++) {
537 			memset(&vsi->rx_rings[i]->stats, 0,
538 			       sizeof(vsi->rx_rings[i]->stats));
539 			memset(&vsi->rx_rings[i]->rx_stats, 0,
540 			       sizeof(vsi->rx_rings[i]->rx_stats));
541 			memset(&vsi->tx_rings[i]->stats, 0,
542 			       sizeof(vsi->tx_rings[i]->stats));
543 			memset(&vsi->tx_rings[i]->tx_stats, 0,
544 			       sizeof(vsi->tx_rings[i]->tx_stats));
545 		}
546 	}
547 	vsi->stat_offsets_loaded = false;
548 }
549 
550 /**
551  * i40e_pf_reset_stats - Reset all of the stats for the given PF
552  * @pf: the PF to be reset
553  **/
554 void i40e_pf_reset_stats(struct i40e_pf *pf)
555 {
556 	struct i40e_veb *veb;
557 	int i;
558 
559 	memset(&pf->stats, 0, sizeof(pf->stats));
560 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
561 	pf->stat_offsets_loaded = false;
562 
563 	i40e_pf_for_each_veb(pf, i, veb) {
564 		memset(&veb->stats, 0, sizeof(veb->stats));
565 		memset(&veb->stats_offsets, 0, sizeof(veb->stats_offsets));
566 		memset(&veb->tc_stats, 0, sizeof(veb->tc_stats));
567 		memset(&veb->tc_stats_offsets, 0, sizeof(veb->tc_stats_offsets));
568 		veb->stat_offsets_loaded = false;
569 	}
570 	pf->hw_csum_rx_error = 0;
571 }
572 
573 /**
574  * i40e_compute_pci_to_hw_id - compute index form PCI function.
575  * @vsi: ptr to the VSI to read from.
576  * @hw: ptr to the hardware info.
577  **/
578 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
579 {
580 	int pf_count = i40e_get_pf_count(hw);
581 
582 	if (vsi->type == I40E_VSI_SRIOV)
583 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
584 
585 	return hw->port + BIT(7);
586 }
587 
588 /**
589  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
590  * @hw: ptr to the hardware info.
591  * @hireg: the high 32 bit reg to read.
592  * @loreg: the low 32 bit reg to read.
593  * @offset_loaded: has the initial offset been loaded yet.
594  * @offset: ptr to current offset value.
595  * @stat: ptr to the stat.
596  *
597  * Since the device stats are not reset at PFReset, they will not
598  * be zeroed when the driver starts.  We'll save the first values read
599  * and use them as offsets to be subtracted from the raw values in order
600  * to report stats that count from zero.
601  **/
602 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
603 			       bool offset_loaded, u64 *offset, u64 *stat)
604 {
605 	u64 new_data;
606 
607 	new_data = rd64(hw, loreg);
608 
609 	if (!offset_loaded || new_data < *offset)
610 		*offset = new_data;
611 	*stat = new_data - *offset;
612 }
613 
614 /**
615  * i40e_stat_update48 - read and update a 48 bit stat from the chip
616  * @hw: ptr to the hardware info
617  * @hireg: the high 32 bit reg to read
618  * @loreg: the low 32 bit reg to read
619  * @offset_loaded: has the initial offset been loaded yet
620  * @offset: ptr to current offset value
621  * @stat: ptr to the stat
622  *
623  * Since the device stats are not reset at PFReset, they likely will not
624  * be zeroed when the driver starts.  We'll save the first values read
625  * and use them as offsets to be subtracted from the raw values in order
626  * to report stats that count from zero.  In the process, we also manage
627  * the potential roll-over.
628  **/
629 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
630 			       bool offset_loaded, u64 *offset, u64 *stat)
631 {
632 	u64 new_data;
633 
634 	if (hw->device_id == I40E_DEV_ID_QEMU) {
635 		new_data = rd32(hw, loreg);
636 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
637 	} else {
638 		new_data = rd64(hw, loreg);
639 	}
640 	if (!offset_loaded)
641 		*offset = new_data;
642 	if (likely(new_data >= *offset))
643 		*stat = new_data - *offset;
644 	else
645 		*stat = (new_data + BIT_ULL(48)) - *offset;
646 	*stat &= 0xFFFFFFFFFFFFULL;
647 }
648 
649 /**
650  * i40e_stat_update32 - read and update a 32 bit stat from the chip
651  * @hw: ptr to the hardware info
652  * @reg: the hw reg to read
653  * @offset_loaded: has the initial offset been loaded yet
654  * @offset: ptr to current offset value
655  * @stat: ptr to the stat
656  **/
657 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
658 			       bool offset_loaded, u64 *offset, u64 *stat)
659 {
660 	u32 new_data;
661 
662 	new_data = rd32(hw, reg);
663 	if (!offset_loaded)
664 		*offset = new_data;
665 	if (likely(new_data >= *offset))
666 		*stat = (u32)(new_data - *offset);
667 	else
668 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
669 }
670 
671 /**
672  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
673  * @hw: ptr to the hardware info
674  * @reg: the hw reg to read and clear
675  * @stat: ptr to the stat
676  **/
677 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
678 {
679 	u32 new_data = rd32(hw, reg);
680 
681 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
682 	*stat += new_data;
683 }
684 
685 /**
686  * i40e_stats_update_rx_discards - update rx_discards.
687  * @vsi: ptr to the VSI to be updated.
688  * @hw: ptr to the hardware info.
689  * @stat_idx: VSI's stat_counter_idx.
690  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
691  * @stat_offset: ptr to stat_offset to store first read of specific register.
692  * @stat: ptr to VSI's stat to be updated.
693  **/
694 static void
695 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
696 			      int stat_idx, bool offset_loaded,
697 			      struct i40e_eth_stats *stat_offset,
698 			      struct i40e_eth_stats *stat)
699 {
700 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
701 			   &stat_offset->rx_discards, &stat->rx_discards);
702 	i40e_stat_update64(hw,
703 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
704 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
705 			   offset_loaded, &stat_offset->rx_discards_other,
706 			   &stat->rx_discards_other);
707 }
708 
709 /**
710  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
711  * @vsi: the VSI to be updated
712  **/
713 void i40e_update_eth_stats(struct i40e_vsi *vsi)
714 {
715 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
716 	struct i40e_pf *pf = vsi->back;
717 	struct i40e_hw *hw = &pf->hw;
718 	struct i40e_eth_stats *oes;
719 	struct i40e_eth_stats *es;     /* device's eth stats */
720 
721 	es = &vsi->eth_stats;
722 	oes = &vsi->eth_stats_offsets;
723 
724 	/* Gather up the stats that the hw collects */
725 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
726 			   vsi->stat_offsets_loaded,
727 			   &oes->tx_errors, &es->tx_errors);
728 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
729 			   vsi->stat_offsets_loaded,
730 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
731 
732 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
733 			   I40E_GLV_GORCL(stat_idx),
734 			   vsi->stat_offsets_loaded,
735 			   &oes->rx_bytes, &es->rx_bytes);
736 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
737 			   I40E_GLV_UPRCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->rx_unicast, &es->rx_unicast);
740 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
741 			   I40E_GLV_MPRCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->rx_multicast, &es->rx_multicast);
744 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
745 			   I40E_GLV_BPRCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->rx_broadcast, &es->rx_broadcast);
748 
749 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
750 			   I40E_GLV_GOTCL(stat_idx),
751 			   vsi->stat_offsets_loaded,
752 			   &oes->tx_bytes, &es->tx_bytes);
753 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
754 			   I40E_GLV_UPTCL(stat_idx),
755 			   vsi->stat_offsets_loaded,
756 			   &oes->tx_unicast, &es->tx_unicast);
757 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
758 			   I40E_GLV_MPTCL(stat_idx),
759 			   vsi->stat_offsets_loaded,
760 			   &oes->tx_multicast, &es->tx_multicast);
761 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
762 			   I40E_GLV_BPTCL(stat_idx),
763 			   vsi->stat_offsets_loaded,
764 			   &oes->tx_broadcast, &es->tx_broadcast);
765 
766 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
767 				      vsi->stat_offsets_loaded, oes, es);
768 
769 	vsi->stat_offsets_loaded = true;
770 }
771 
772 /**
773  * i40e_update_veb_stats - Update Switch component statistics
774  * @veb: the VEB being updated
775  **/
776 void i40e_update_veb_stats(struct i40e_veb *veb)
777 {
778 	struct i40e_pf *pf = veb->pf;
779 	struct i40e_hw *hw = &pf->hw;
780 	struct i40e_eth_stats *oes;
781 	struct i40e_eth_stats *es;     /* device's eth stats */
782 	struct i40e_veb_tc_stats *veb_oes;
783 	struct i40e_veb_tc_stats *veb_es;
784 	int i, idx = 0;
785 
786 	idx = veb->stats_idx;
787 	es = &veb->stats;
788 	oes = &veb->stats_offsets;
789 	veb_es = &veb->tc_stats;
790 	veb_oes = &veb->tc_stats_offsets;
791 
792 	/* Gather up the stats that the hw collects */
793 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
794 			   veb->stat_offsets_loaded,
795 			   &oes->tx_discards, &es->tx_discards);
796 	if (hw->revision_id > 0)
797 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
798 				   veb->stat_offsets_loaded,
799 				   &oes->rx_unknown_protocol,
800 				   &es->rx_unknown_protocol);
801 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
802 			   veb->stat_offsets_loaded,
803 			   &oes->rx_bytes, &es->rx_bytes);
804 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
805 			   veb->stat_offsets_loaded,
806 			   &oes->rx_unicast, &es->rx_unicast);
807 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
808 			   veb->stat_offsets_loaded,
809 			   &oes->rx_multicast, &es->rx_multicast);
810 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->rx_broadcast, &es->rx_broadcast);
813 
814 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
815 			   veb->stat_offsets_loaded,
816 			   &oes->tx_bytes, &es->tx_bytes);
817 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
818 			   veb->stat_offsets_loaded,
819 			   &oes->tx_unicast, &es->tx_unicast);
820 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
821 			   veb->stat_offsets_loaded,
822 			   &oes->tx_multicast, &es->tx_multicast);
823 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
824 			   veb->stat_offsets_loaded,
825 			   &oes->tx_broadcast, &es->tx_broadcast);
826 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
827 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
828 				   I40E_GLVEBTC_RPCL(i, idx),
829 				   veb->stat_offsets_loaded,
830 				   &veb_oes->tc_rx_packets[i],
831 				   &veb_es->tc_rx_packets[i]);
832 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
833 				   I40E_GLVEBTC_RBCL(i, idx),
834 				   veb->stat_offsets_loaded,
835 				   &veb_oes->tc_rx_bytes[i],
836 				   &veb_es->tc_rx_bytes[i]);
837 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
838 				   I40E_GLVEBTC_TPCL(i, idx),
839 				   veb->stat_offsets_loaded,
840 				   &veb_oes->tc_tx_packets[i],
841 				   &veb_es->tc_tx_packets[i]);
842 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
843 				   I40E_GLVEBTC_TBCL(i, idx),
844 				   veb->stat_offsets_loaded,
845 				   &veb_oes->tc_tx_bytes[i],
846 				   &veb_es->tc_tx_bytes[i]);
847 	}
848 	veb->stat_offsets_loaded = true;
849 }
850 
851 /**
852  * i40e_update_vsi_stats - Update the vsi statistics counters.
853  * @vsi: the VSI to be updated
854  *
855  * There are a few instances where we store the same stat in a
856  * couple of different structs.  This is partly because we have
857  * the netdev stats that need to be filled out, which is slightly
858  * different from the "eth_stats" defined by the chip and used in
859  * VF communications.  We sort it out here.
860  **/
861 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
862 {
863 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
864 	struct i40e_pf *pf = vsi->back;
865 	struct rtnl_link_stats64 *ons;
866 	struct rtnl_link_stats64 *ns;   /* netdev stats */
867 	struct i40e_eth_stats *oes;
868 	struct i40e_eth_stats *es;     /* device's eth stats */
869 	u64 tx_restart, tx_busy;
870 	struct i40e_ring *p;
871 	u64 bytes, packets;
872 	unsigned int start;
873 	u64 tx_linearize;
874 	u64 tx_force_wb;
875 	u64 tx_stopped;
876 	u64 rx_p, rx_b;
877 	u64 tx_p, tx_b;
878 	u16 q;
879 
880 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
881 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
882 		return;
883 
884 	ns = i40e_get_vsi_stats_struct(vsi);
885 	ons = &vsi->net_stats_offsets;
886 	es = &vsi->eth_stats;
887 	oes = &vsi->eth_stats_offsets;
888 
889 	/* Gather up the netdev and vsi stats that the driver collects
890 	 * on the fly during packet processing
891 	 */
892 	rx_b = rx_p = 0;
893 	tx_b = tx_p = 0;
894 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
895 	tx_stopped = 0;
896 	rx_page = 0;
897 	rx_buf = 0;
898 	rx_reuse = 0;
899 	rx_alloc = 0;
900 	rx_waive = 0;
901 	rx_busy = 0;
902 	rcu_read_lock();
903 	for (q = 0; q < vsi->num_queue_pairs; q++) {
904 		/* locate Tx ring */
905 		p = READ_ONCE(vsi->tx_rings[q]);
906 		if (!p)
907 			continue;
908 
909 		do {
910 			start = u64_stats_fetch_begin(&p->syncp);
911 			packets = p->stats.packets;
912 			bytes = p->stats.bytes;
913 		} while (u64_stats_fetch_retry(&p->syncp, start));
914 		tx_b += bytes;
915 		tx_p += packets;
916 		tx_restart += p->tx_stats.restart_queue;
917 		tx_busy += p->tx_stats.tx_busy;
918 		tx_linearize += p->tx_stats.tx_linearize;
919 		tx_force_wb += p->tx_stats.tx_force_wb;
920 		tx_stopped += p->tx_stats.tx_stopped;
921 
922 		/* locate Rx ring */
923 		p = READ_ONCE(vsi->rx_rings[q]);
924 		if (!p)
925 			continue;
926 
927 		do {
928 			start = u64_stats_fetch_begin(&p->syncp);
929 			packets = p->stats.packets;
930 			bytes = p->stats.bytes;
931 		} while (u64_stats_fetch_retry(&p->syncp, start));
932 		rx_b += bytes;
933 		rx_p += packets;
934 		rx_buf += p->rx_stats.alloc_buff_failed;
935 		rx_page += p->rx_stats.alloc_page_failed;
936 		rx_reuse += p->rx_stats.page_reuse_count;
937 		rx_alloc += p->rx_stats.page_alloc_count;
938 		rx_waive += p->rx_stats.page_waive_count;
939 		rx_busy += p->rx_stats.page_busy_count;
940 
941 		if (i40e_enabled_xdp_vsi(vsi)) {
942 			/* locate XDP ring */
943 			p = READ_ONCE(vsi->xdp_rings[q]);
944 			if (!p)
945 				continue;
946 
947 			do {
948 				start = u64_stats_fetch_begin(&p->syncp);
949 				packets = p->stats.packets;
950 				bytes = p->stats.bytes;
951 			} while (u64_stats_fetch_retry(&p->syncp, start));
952 			tx_b += bytes;
953 			tx_p += packets;
954 			tx_restart += p->tx_stats.restart_queue;
955 			tx_busy += p->tx_stats.tx_busy;
956 			tx_linearize += p->tx_stats.tx_linearize;
957 			tx_force_wb += p->tx_stats.tx_force_wb;
958 		}
959 	}
960 	rcu_read_unlock();
961 	vsi->tx_restart = tx_restart;
962 	vsi->tx_busy = tx_busy;
963 	vsi->tx_linearize = tx_linearize;
964 	vsi->tx_force_wb = tx_force_wb;
965 	vsi->tx_stopped = tx_stopped;
966 	vsi->rx_page_failed = rx_page;
967 	vsi->rx_buf_failed = rx_buf;
968 	vsi->rx_page_reuse = rx_reuse;
969 	vsi->rx_page_alloc = rx_alloc;
970 	vsi->rx_page_waive = rx_waive;
971 	vsi->rx_page_busy = rx_busy;
972 
973 	ns->rx_packets = rx_p;
974 	ns->rx_bytes = rx_b;
975 	ns->tx_packets = tx_p;
976 	ns->tx_bytes = tx_b;
977 
978 	/* update netdev stats from eth stats */
979 	i40e_update_eth_stats(vsi);
980 	ons->tx_errors = oes->tx_errors;
981 	ns->tx_errors = es->tx_errors;
982 	ons->multicast = oes->rx_multicast;
983 	ns->multicast = es->rx_multicast;
984 	ons->rx_dropped = oes->rx_discards_other;
985 	ns->rx_dropped = es->rx_discards_other;
986 	ons->rx_missed_errors = oes->rx_discards;
987 	ns->rx_missed_errors = es->rx_discards;
988 	ons->tx_dropped = oes->tx_discards;
989 	ns->tx_dropped = es->tx_discards;
990 
991 	/* pull in a couple PF stats if this is the main vsi */
992 	if (vsi->type == I40E_VSI_MAIN) {
993 		ns->rx_crc_errors = pf->stats.crc_errors;
994 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
995 		ns->rx_length_errors = pf->stats.rx_length_errors;
996 	}
997 }
998 
999 /**
1000  * i40e_update_pf_stats - Update the PF statistics counters.
1001  * @pf: the PF to be updated
1002  **/
1003 static void i40e_update_pf_stats(struct i40e_pf *pf)
1004 {
1005 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1006 	struct i40e_hw_port_stats *nsd = &pf->stats;
1007 	struct i40e_hw *hw = &pf->hw;
1008 	u32 val;
1009 	int i;
1010 
1011 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1012 			   I40E_GLPRT_GORCL(hw->port),
1013 			   pf->stat_offsets_loaded,
1014 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1015 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1016 			   I40E_GLPRT_GOTCL(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1019 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1020 			   pf->stat_offsets_loaded,
1021 			   &osd->eth.rx_discards,
1022 			   &nsd->eth.rx_discards);
1023 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1024 			   I40E_GLPRT_UPRCL(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->eth.rx_unicast,
1027 			   &nsd->eth.rx_unicast);
1028 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1029 			   I40E_GLPRT_MPRCL(hw->port),
1030 			   pf->stat_offsets_loaded,
1031 			   &osd->eth.rx_multicast,
1032 			   &nsd->eth.rx_multicast);
1033 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1034 			   I40E_GLPRT_BPRCL(hw->port),
1035 			   pf->stat_offsets_loaded,
1036 			   &osd->eth.rx_broadcast,
1037 			   &nsd->eth.rx_broadcast);
1038 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1039 			   I40E_GLPRT_UPTCL(hw->port),
1040 			   pf->stat_offsets_loaded,
1041 			   &osd->eth.tx_unicast,
1042 			   &nsd->eth.tx_unicast);
1043 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1044 			   I40E_GLPRT_MPTCL(hw->port),
1045 			   pf->stat_offsets_loaded,
1046 			   &osd->eth.tx_multicast,
1047 			   &nsd->eth.tx_multicast);
1048 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1049 			   I40E_GLPRT_BPTCL(hw->port),
1050 			   pf->stat_offsets_loaded,
1051 			   &osd->eth.tx_broadcast,
1052 			   &nsd->eth.tx_broadcast);
1053 
1054 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1055 			   pf->stat_offsets_loaded,
1056 			   &osd->tx_dropped_link_down,
1057 			   &nsd->tx_dropped_link_down);
1058 
1059 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->crc_errors, &nsd->crc_errors);
1062 
1063 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1066 
1067 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->mac_local_faults,
1070 			   &nsd->mac_local_faults);
1071 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->mac_remote_faults,
1074 			   &nsd->mac_remote_faults);
1075 
1076 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1077 			   pf->stat_offsets_loaded,
1078 			   &osd->rx_length_errors,
1079 			   &nsd->rx_length_errors);
1080 
1081 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1082 			   pf->stat_offsets_loaded,
1083 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1084 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1085 			   pf->stat_offsets_loaded,
1086 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1087 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1088 			   pf->stat_offsets_loaded,
1089 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1090 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1091 			   pf->stat_offsets_loaded,
1092 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1093 
1094 	for (i = 0; i < 8; i++) {
1095 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1096 				   pf->stat_offsets_loaded,
1097 				   &osd->priority_xoff_rx[i],
1098 				   &nsd->priority_xoff_rx[i]);
1099 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1100 				   pf->stat_offsets_loaded,
1101 				   &osd->priority_xon_rx[i],
1102 				   &nsd->priority_xon_rx[i]);
1103 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1104 				   pf->stat_offsets_loaded,
1105 				   &osd->priority_xon_tx[i],
1106 				   &nsd->priority_xon_tx[i]);
1107 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1108 				   pf->stat_offsets_loaded,
1109 				   &osd->priority_xoff_tx[i],
1110 				   &nsd->priority_xoff_tx[i]);
1111 		i40e_stat_update32(hw,
1112 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1113 				   pf->stat_offsets_loaded,
1114 				   &osd->priority_xon_2_xoff[i],
1115 				   &nsd->priority_xon_2_xoff[i]);
1116 	}
1117 
1118 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1119 			   I40E_GLPRT_PRC64L(hw->port),
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->rx_size_64, &nsd->rx_size_64);
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1123 			   I40E_GLPRT_PRC127L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_127, &nsd->rx_size_127);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1127 			   I40E_GLPRT_PRC255L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_255, &nsd->rx_size_255);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1131 			   I40E_GLPRT_PRC511L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_511, &nsd->rx_size_511);
1134 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1135 			   I40E_GLPRT_PRC1023L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1139 			   I40E_GLPRT_PRC1522L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1143 			   I40E_GLPRT_PRC9522L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->rx_size_big, &nsd->rx_size_big);
1146 
1147 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1148 			   I40E_GLPRT_PTC64L(hw->port),
1149 			   pf->stat_offsets_loaded,
1150 			   &osd->tx_size_64, &nsd->tx_size_64);
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1152 			   I40E_GLPRT_PTC127L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_127, &nsd->tx_size_127);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1156 			   I40E_GLPRT_PTC255L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_255, &nsd->tx_size_255);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1160 			   I40E_GLPRT_PTC511L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_511, &nsd->tx_size_511);
1163 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1164 			   I40E_GLPRT_PTC1023L(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1167 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1168 			   I40E_GLPRT_PTC1522L(hw->port),
1169 			   pf->stat_offsets_loaded,
1170 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1171 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1172 			   I40E_GLPRT_PTC9522L(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->tx_size_big, &nsd->tx_size_big);
1175 
1176 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->rx_undersize, &nsd->rx_undersize);
1179 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1180 			   pf->stat_offsets_loaded,
1181 			   &osd->rx_fragments, &nsd->rx_fragments);
1182 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1183 			   pf->stat_offsets_loaded,
1184 			   &osd->rx_oversize, &nsd->rx_oversize);
1185 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1186 			   pf->stat_offsets_loaded,
1187 			   &osd->rx_jabber, &nsd->rx_jabber);
1188 
1189 	/* FDIR stats */
1190 	i40e_stat_update_and_clear32(hw,
1191 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1192 			&nsd->fd_atr_match);
1193 	i40e_stat_update_and_clear32(hw,
1194 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1195 			&nsd->fd_sb_match);
1196 	i40e_stat_update_and_clear32(hw,
1197 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1198 			&nsd->fd_atr_tunnel_match);
1199 
1200 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1201 	nsd->tx_lpi_status =
1202 		       FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
1203 	nsd->rx_lpi_status =
1204 		       FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
1205 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1206 			   pf->stat_offsets_loaded,
1207 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1208 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1209 			   pf->stat_offsets_loaded,
1210 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1211 
1212 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1213 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1214 		nsd->fd_sb_status = true;
1215 	else
1216 		nsd->fd_sb_status = false;
1217 
1218 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1219 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1220 		nsd->fd_atr_status = true;
1221 	else
1222 		nsd->fd_atr_status = false;
1223 
1224 	pf->stat_offsets_loaded = true;
1225 }
1226 
1227 /**
1228  * i40e_update_stats - Update the various statistics counters.
1229  * @vsi: the VSI to be updated
1230  *
1231  * Update the various stats for this VSI and its related entities.
1232  **/
1233 void i40e_update_stats(struct i40e_vsi *vsi)
1234 {
1235 	struct i40e_pf *pf = vsi->back;
1236 
1237 	if (vsi->type == I40E_VSI_MAIN)
1238 		i40e_update_pf_stats(pf);
1239 
1240 	i40e_update_vsi_stats(vsi);
1241 }
1242 
1243 /**
1244  * i40e_count_filters - counts VSI mac filters
1245  * @vsi: the VSI to be searched
1246  *
1247  * Returns count of mac filters
1248  **/
1249 int i40e_count_filters(struct i40e_vsi *vsi)
1250 {
1251 	struct i40e_mac_filter *f;
1252 	struct hlist_node *h;
1253 	int bkt;
1254 	int cnt = 0;
1255 
1256 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1257 		if (f->state == I40E_FILTER_NEW ||
1258 		    f->state == I40E_FILTER_ACTIVE)
1259 			++cnt;
1260 	}
1261 
1262 	return cnt;
1263 }
1264 
1265 /**
1266  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1267  * @vsi: the VSI to be searched
1268  * @macaddr: the MAC address
1269  * @vlan: the vlan
1270  *
1271  * Returns ptr to the filter object or NULL
1272  **/
1273 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1274 						const u8 *macaddr, s16 vlan)
1275 {
1276 	struct i40e_mac_filter *f;
1277 	u64 key;
1278 
1279 	if (!vsi || !macaddr)
1280 		return NULL;
1281 
1282 	key = i40e_addr_to_hkey(macaddr);
1283 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1284 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1285 		    (vlan == f->vlan))
1286 			return f;
1287 	}
1288 	return NULL;
1289 }
1290 
1291 /**
1292  * i40e_find_mac - Find a mac addr in the macvlan filters list
1293  * @vsi: the VSI to be searched
1294  * @macaddr: the MAC address we are searching for
1295  *
1296  * Returns the first filter with the provided MAC address or NULL if
1297  * MAC address was not found
1298  **/
1299 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1300 {
1301 	struct i40e_mac_filter *f;
1302 	u64 key;
1303 
1304 	if (!vsi || !macaddr)
1305 		return NULL;
1306 
1307 	key = i40e_addr_to_hkey(macaddr);
1308 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1309 		if ((ether_addr_equal(macaddr, f->macaddr)))
1310 			return f;
1311 	}
1312 	return NULL;
1313 }
1314 
1315 /**
1316  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1317  * @vsi: the VSI to be searched
1318  *
1319  * Returns true if VSI is in vlan mode or false otherwise
1320  **/
1321 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1322 {
1323 	/* If we have a PVID, always operate in VLAN mode */
1324 	if (vsi->info.pvid)
1325 		return true;
1326 
1327 	/* We need to operate in VLAN mode whenever we have any filters with
1328 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1329 	 * time, incurring search cost repeatedly. However, we can notice two
1330 	 * things:
1331 	 *
1332 	 * 1) the only place where we can gain a VLAN filter is in
1333 	 *    i40e_add_filter.
1334 	 *
1335 	 * 2) the only place where filters are actually removed is in
1336 	 *    i40e_sync_filters_subtask.
1337 	 *
1338 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1339 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1340 	 * we have to perform the full search after deleting filters in
1341 	 * i40e_sync_filters_subtask, but we already have to search
1342 	 * filters here and can perform the check at the same time. This
1343 	 * results in avoiding embedding a loop for VLAN mode inside another
1344 	 * loop over all the filters, and should maintain correctness as noted
1345 	 * above.
1346 	 */
1347 	return vsi->has_vlan_filter;
1348 }
1349 
1350 /**
1351  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1352  * @vsi: the VSI to configure
1353  * @tmp_add_list: list of filters ready to be added
1354  * @tmp_del_list: list of filters ready to be deleted
1355  * @vlan_filters: the number of active VLAN filters
1356  *
1357  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1358  * behave as expected. If we have any active VLAN filters remaining or about
1359  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1360  * so that they only match against untagged traffic. If we no longer have any
1361  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1362  * so that they match against both tagged and untagged traffic. In this way,
1363  * we ensure that we correctly receive the desired traffic. This ensures that
1364  * when we have an active VLAN we will receive only untagged traffic and
1365  * traffic matching active VLANs. If we have no active VLANs then we will
1366  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1367  *
1368  * Finally, in a similar fashion, this function also corrects filters when
1369  * there is an active PVID assigned to this VSI.
1370  *
1371  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1372  *
1373  * This function is only expected to be called from within
1374  * i40e_sync_vsi_filters.
1375  *
1376  * NOTE: This function expects to be called while under the
1377  * mac_filter_hash_lock
1378  */
1379 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1380 					 struct hlist_head *tmp_add_list,
1381 					 struct hlist_head *tmp_del_list,
1382 					 int vlan_filters)
1383 {
1384 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1385 	struct i40e_mac_filter *f, *add_head;
1386 	struct i40e_new_mac_filter *new;
1387 	struct hlist_node *h;
1388 	int bkt, new_vlan;
1389 
1390 	/* To determine if a particular filter needs to be replaced we
1391 	 * have the three following conditions:
1392 	 *
1393 	 * a) if we have a PVID assigned, then all filters which are
1394 	 *    not marked as VLAN=PVID must be replaced with filters that
1395 	 *    are.
1396 	 * b) otherwise, if we have any active VLANS, all filters
1397 	 *    which are marked as VLAN=-1 must be replaced with
1398 	 *    filters marked as VLAN=0
1399 	 * c) finally, if we do not have any active VLANS, all filters
1400 	 *    which are marked as VLAN=0 must be replaced with filters
1401 	 *    marked as VLAN=-1
1402 	 */
1403 
1404 	/* Update the filters about to be added in place */
1405 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1406 		if (pvid && new->f->vlan != pvid)
1407 			new->f->vlan = pvid;
1408 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1409 			new->f->vlan = 0;
1410 		else if (!vlan_filters && new->f->vlan == 0)
1411 			new->f->vlan = I40E_VLAN_ANY;
1412 	}
1413 
1414 	/* Update the remaining active filters */
1415 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1416 		/* Combine the checks for whether a filter needs to be changed
1417 		 * and then determine the new VLAN inside the if block, in
1418 		 * order to avoid duplicating code for adding the new filter
1419 		 * then deleting the old filter.
1420 		 */
1421 		if ((pvid && f->vlan != pvid) ||
1422 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1423 		    (!vlan_filters && f->vlan == 0)) {
1424 			/* Determine the new vlan we will be adding */
1425 			if (pvid)
1426 				new_vlan = pvid;
1427 			else if (vlan_filters)
1428 				new_vlan = 0;
1429 			else
1430 				new_vlan = I40E_VLAN_ANY;
1431 
1432 			/* Create the new filter */
1433 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1434 			if (!add_head)
1435 				return -ENOMEM;
1436 
1437 			/* Create a temporary i40e_new_mac_filter */
1438 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1439 			if (!new)
1440 				return -ENOMEM;
1441 
1442 			new->f = add_head;
1443 			new->state = add_head->state;
1444 
1445 			/* Add the new filter to the tmp list */
1446 			hlist_add_head(&new->hlist, tmp_add_list);
1447 
1448 			/* Put the original filter into the delete list */
1449 			f->state = I40E_FILTER_REMOVE;
1450 			hash_del(&f->hlist);
1451 			hlist_add_head(&f->hlist, tmp_del_list);
1452 		}
1453 	}
1454 
1455 	vsi->has_vlan_filter = !!vlan_filters;
1456 
1457 	return 0;
1458 }
1459 
1460 /**
1461  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1462  * @vsi: the vsi to configure
1463  * @new_mac: new mac filter to be added
1464  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1465  * @vlan_filters: the number of active VLAN filters
1466  * @trusted: flag if the VF is trusted
1467  *
1468  * Get new VLAN id based on current VLAN filters, trust, PVID
1469  * and vf-vlan-prune-disable flag.
1470  *
1471  * Returns the value of the new vlan filter or
1472  * the old value if no new filter is needed.
1473  */
1474 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1475 				struct i40e_new_mac_filter *new_mac,
1476 				struct i40e_mac_filter *f,
1477 				int vlan_filters,
1478 				bool trusted)
1479 {
1480 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1481 	struct i40e_pf *pf = vsi->back;
1482 	bool is_any;
1483 
1484 	if (new_mac)
1485 		f = new_mac->f;
1486 
1487 	if (pvid && f->vlan != pvid)
1488 		return pvid;
1489 
1490 	is_any = (trusted ||
1491 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1492 
1493 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1494 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1495 	    (is_any && !vlan_filters && f->vlan == 0)) {
1496 		if (is_any)
1497 			return I40E_VLAN_ANY;
1498 		else
1499 			return 0;
1500 	}
1501 
1502 	return f->vlan;
1503 }
1504 
1505 /**
1506  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1507  * @vsi: the vsi to configure
1508  * @tmp_add_list: list of filters ready to be added
1509  * @tmp_del_list: list of filters ready to be deleted
1510  * @vlan_filters: the number of active VLAN filters
1511  * @trusted: flag if the VF is trusted
1512  *
1513  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1514  * and vf-vlan-prune-disable flag.
1515  *
1516  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1517  *
1518  * This function is only expected to be called from within
1519  * i40e_sync_vsi_filters.
1520  *
1521  * NOTE: This function expects to be called while under the
1522  * mac_filter_hash_lock
1523  */
1524 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1525 					    struct hlist_head *tmp_add_list,
1526 					    struct hlist_head *tmp_del_list,
1527 					    int vlan_filters,
1528 					    bool trusted)
1529 {
1530 	struct i40e_mac_filter *f, *add_head;
1531 	struct i40e_new_mac_filter *new_mac;
1532 	struct hlist_node *h;
1533 	int bkt, new_vlan;
1534 
1535 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1536 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1537 							vlan_filters, trusted);
1538 	}
1539 
1540 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1541 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1542 						trusted);
1543 		if (new_vlan != f->vlan) {
1544 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1545 			if (!add_head)
1546 				return -ENOMEM;
1547 			/* Create a temporary i40e_new_mac_filter */
1548 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1549 			if (!new_mac)
1550 				return -ENOMEM;
1551 			new_mac->f = add_head;
1552 			new_mac->state = add_head->state;
1553 
1554 			/* Add the new filter to the tmp list */
1555 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1556 
1557 			/* Put the original filter into the delete list */
1558 			f->state = I40E_FILTER_REMOVE;
1559 			hash_del(&f->hlist);
1560 			hlist_add_head(&f->hlist, tmp_del_list);
1561 		}
1562 	}
1563 
1564 	vsi->has_vlan_filter = !!vlan_filters;
1565 	return 0;
1566 }
1567 
1568 /**
1569  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1570  * @vsi: the PF Main VSI - inappropriate for any other VSI
1571  * @macaddr: the MAC address
1572  *
1573  * Remove whatever filter the firmware set up so the driver can manage
1574  * its own filtering intelligently.
1575  **/
1576 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1577 {
1578 	struct i40e_aqc_remove_macvlan_element_data element;
1579 	struct i40e_pf *pf = vsi->back;
1580 
1581 	/* Only appropriate for the PF main VSI */
1582 	if (vsi->type != I40E_VSI_MAIN)
1583 		return;
1584 
1585 	memset(&element, 0, sizeof(element));
1586 	ether_addr_copy(element.mac_addr, macaddr);
1587 	element.vlan_tag = 0;
1588 	/* Ignore error returns, some firmware does it this way... */
1589 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1590 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1591 
1592 	memset(&element, 0, sizeof(element));
1593 	ether_addr_copy(element.mac_addr, macaddr);
1594 	element.vlan_tag = 0;
1595 	/* ...and some firmware does it this way. */
1596 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1597 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1598 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1599 }
1600 
1601 /**
1602  * i40e_add_filter - Add a mac/vlan filter to the VSI
1603  * @vsi: the VSI to be searched
1604  * @macaddr: the MAC address
1605  * @vlan: the vlan
1606  *
1607  * Returns ptr to the filter object or NULL when no memory available.
1608  *
1609  * NOTE: This function is expected to be called with mac_filter_hash_lock
1610  * being held.
1611  **/
1612 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1613 					const u8 *macaddr, s16 vlan)
1614 {
1615 	struct i40e_mac_filter *f;
1616 	u64 key;
1617 
1618 	if (!vsi || !macaddr)
1619 		return NULL;
1620 
1621 	f = i40e_find_filter(vsi, macaddr, vlan);
1622 	if (!f) {
1623 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1624 		if (!f)
1625 			return NULL;
1626 
1627 		/* Update the boolean indicating if we need to function in
1628 		 * VLAN mode.
1629 		 */
1630 		if (vlan >= 0)
1631 			vsi->has_vlan_filter = true;
1632 
1633 		ether_addr_copy(f->macaddr, macaddr);
1634 		f->vlan = vlan;
1635 		f->state = I40E_FILTER_NEW;
1636 		INIT_HLIST_NODE(&f->hlist);
1637 
1638 		key = i40e_addr_to_hkey(macaddr);
1639 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1640 
1641 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1642 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1643 	}
1644 
1645 	/* If we're asked to add a filter that has been marked for removal, it
1646 	 * is safe to simply restore it to active state. __i40e_del_filter
1647 	 * will have simply deleted any filters which were previously marked
1648 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1649 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1650 	 * task, just restore this filter to the ACTIVE state so that the
1651 	 * sync task leaves it in place
1652 	 */
1653 	if (f->state == I40E_FILTER_REMOVE)
1654 		f->state = I40E_FILTER_ACTIVE;
1655 
1656 	return f;
1657 }
1658 
1659 /**
1660  * __i40e_del_filter - Remove a specific filter from the VSI
1661  * @vsi: VSI to remove from
1662  * @f: the filter to remove from the list
1663  *
1664  * This function should be called instead of i40e_del_filter only if you know
1665  * the exact filter you will remove already, such as via i40e_find_filter or
1666  * i40e_find_mac.
1667  *
1668  * NOTE: This function is expected to be called with mac_filter_hash_lock
1669  * being held.
1670  * ANOTHER NOTE: This function MUST be called from within the context of
1671  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1672  * instead of list_for_each_entry().
1673  **/
1674 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1675 {
1676 	if (!f)
1677 		return;
1678 
1679 	/* If the filter was never added to firmware then we can just delete it
1680 	 * directly and we don't want to set the status to remove or else an
1681 	 * admin queue command will unnecessarily fire.
1682 	 */
1683 	if ((f->state == I40E_FILTER_FAILED) ||
1684 	    (f->state == I40E_FILTER_NEW)) {
1685 		hash_del(&f->hlist);
1686 		kfree(f);
1687 	} else {
1688 		f->state = I40E_FILTER_REMOVE;
1689 	}
1690 
1691 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1692 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1693 }
1694 
1695 /**
1696  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1697  * @vsi: the VSI to be searched
1698  * @macaddr: the MAC address
1699  * @vlan: the VLAN
1700  *
1701  * NOTE: This function is expected to be called with mac_filter_hash_lock
1702  * being held.
1703  * ANOTHER NOTE: This function MUST be called from within the context of
1704  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1705  * instead of list_for_each_entry().
1706  **/
1707 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1708 {
1709 	struct i40e_mac_filter *f;
1710 
1711 	if (!vsi || !macaddr)
1712 		return;
1713 
1714 	f = i40e_find_filter(vsi, macaddr, vlan);
1715 	__i40e_del_filter(vsi, f);
1716 }
1717 
1718 /**
1719  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1720  * @vsi: the VSI to be searched
1721  * @macaddr: the mac address to be filtered
1722  *
1723  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1724  * go through all the macvlan filters and add a macvlan filter for each
1725  * unique vlan that already exists. If a PVID has been assigned, instead only
1726  * add the macaddr to that VLAN.
1727  *
1728  * Returns last filter added on success, else NULL
1729  **/
1730 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1731 					    const u8 *macaddr)
1732 {
1733 	struct i40e_mac_filter *f, *add = NULL;
1734 	struct hlist_node *h;
1735 	int bkt;
1736 
1737 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1738 	if (vsi->info.pvid)
1739 		return i40e_add_filter(vsi, macaddr,
1740 				       le16_to_cpu(vsi->info.pvid));
1741 
1742 	if (!i40e_is_vsi_in_vlan(vsi))
1743 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1744 
1745 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1746 		if (f->state == I40E_FILTER_REMOVE)
1747 			continue;
1748 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1749 		if (!add)
1750 			return NULL;
1751 	}
1752 
1753 	return add;
1754 }
1755 
1756 /**
1757  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1758  * @vsi: the VSI to be searched
1759  * @macaddr: the mac address to be removed
1760  *
1761  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1762  * associated with.
1763  *
1764  * Returns 0 for success, or error
1765  **/
1766 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1767 {
1768 	struct i40e_mac_filter *f;
1769 	struct hlist_node *h;
1770 	bool found = false;
1771 	int bkt;
1772 
1773 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1774 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1775 		if (ether_addr_equal(macaddr, f->macaddr)) {
1776 			__i40e_del_filter(vsi, f);
1777 			found = true;
1778 		}
1779 	}
1780 
1781 	if (found)
1782 		return 0;
1783 	else
1784 		return -ENOENT;
1785 }
1786 
1787 /**
1788  * i40e_set_mac - NDO callback to set mac address
1789  * @netdev: network interface device structure
1790  * @p: pointer to an address structure
1791  *
1792  * Returns 0 on success, negative on failure
1793  **/
1794 static int i40e_set_mac(struct net_device *netdev, void *p)
1795 {
1796 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1797 	struct i40e_vsi *vsi = np->vsi;
1798 	struct i40e_pf *pf = vsi->back;
1799 	struct i40e_hw *hw = &pf->hw;
1800 	struct sockaddr *addr = p;
1801 
1802 	if (!is_valid_ether_addr(addr->sa_data))
1803 		return -EADDRNOTAVAIL;
1804 
1805 	if (test_bit(__I40E_DOWN, pf->state) ||
1806 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1807 		return -EADDRNOTAVAIL;
1808 
1809 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1810 		netdev_info(netdev, "returning to hw mac address %pM\n",
1811 			    hw->mac.addr);
1812 	else
1813 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1814 
1815 	/* Copy the address first, so that we avoid a possible race with
1816 	 * .set_rx_mode().
1817 	 * - Remove old address from MAC filter
1818 	 * - Copy new address
1819 	 * - Add new address to MAC filter
1820 	 */
1821 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1822 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1823 	eth_hw_addr_set(netdev, addr->sa_data);
1824 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1825 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1826 
1827 	if (vsi->type == I40E_VSI_MAIN) {
1828 		int ret;
1829 
1830 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1831 						addr->sa_data, NULL);
1832 		if (ret)
1833 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1834 				    ERR_PTR(ret),
1835 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1836 	}
1837 
1838 	/* schedule our worker thread which will take care of
1839 	 * applying the new filter changes
1840 	 */
1841 	i40e_service_event_schedule(pf);
1842 	return 0;
1843 }
1844 
1845 /**
1846  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1847  * @vsi: vsi structure
1848  * @seed: RSS hash seed
1849  * @lut: pointer to lookup table of lut_size
1850  * @lut_size: size of the lookup table
1851  **/
1852 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1853 			      u8 *lut, u16 lut_size)
1854 {
1855 	struct i40e_pf *pf = vsi->back;
1856 	struct i40e_hw *hw = &pf->hw;
1857 	int ret = 0;
1858 
1859 	if (seed) {
1860 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1861 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1862 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1863 		if (ret) {
1864 			dev_info(&pf->pdev->dev,
1865 				 "Cannot set RSS key, err %pe aq_err %s\n",
1866 				 ERR_PTR(ret),
1867 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1868 			return ret;
1869 		}
1870 	}
1871 	if (lut) {
1872 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1873 
1874 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1875 		if (ret) {
1876 			dev_info(&pf->pdev->dev,
1877 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1878 				 ERR_PTR(ret),
1879 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1880 			return ret;
1881 		}
1882 	}
1883 	return ret;
1884 }
1885 
1886 /**
1887  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1888  * @vsi: VSI structure
1889  **/
1890 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1891 {
1892 	struct i40e_pf *pf = vsi->back;
1893 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1894 	u8 *lut;
1895 	int ret;
1896 
1897 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1898 		return 0;
1899 	if (!vsi->rss_size)
1900 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1901 				      vsi->num_queue_pairs);
1902 	if (!vsi->rss_size)
1903 		return -EINVAL;
1904 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1905 	if (!lut)
1906 		return -ENOMEM;
1907 
1908 	/* Use the user configured hash keys and lookup table if there is one,
1909 	 * otherwise use default
1910 	 */
1911 	if (vsi->rss_lut_user)
1912 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1913 	else
1914 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1915 	if (vsi->rss_hkey_user)
1916 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1917 	else
1918 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1919 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1920 	kfree(lut);
1921 	return ret;
1922 }
1923 
1924 /**
1925  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1926  * @vsi: the VSI being configured,
1927  * @ctxt: VSI context structure
1928  * @enabled_tc: number of traffic classes to enable
1929  *
1930  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1931  **/
1932 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1933 					   struct i40e_vsi_context *ctxt,
1934 					   u8 enabled_tc)
1935 {
1936 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1937 	int i, override_q, pow, num_qps, ret;
1938 	u8 netdev_tc = 0, offset = 0;
1939 
1940 	if (vsi->type != I40E_VSI_MAIN)
1941 		return -EINVAL;
1942 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1943 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1944 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1945 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1946 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1947 
1948 	/* find the next higher power-of-2 of num queue pairs */
1949 	pow = ilog2(num_qps);
1950 	if (!is_power_of_2(num_qps))
1951 		pow++;
1952 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1953 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1954 
1955 	/* Setup queue offset/count for all TCs for given VSI */
1956 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1957 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1958 		/* See if the given TC is enabled for the given VSI */
1959 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1960 			offset = vsi->mqprio_qopt.qopt.offset[i];
1961 			qcount = vsi->mqprio_qopt.qopt.count[i];
1962 			if (qcount > max_qcount)
1963 				max_qcount = qcount;
1964 			vsi->tc_config.tc_info[i].qoffset = offset;
1965 			vsi->tc_config.tc_info[i].qcount = qcount;
1966 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1967 		} else {
1968 			/* TC is not enabled so set the offset to
1969 			 * default queue and allocate one queue
1970 			 * for the given TC.
1971 			 */
1972 			vsi->tc_config.tc_info[i].qoffset = 0;
1973 			vsi->tc_config.tc_info[i].qcount = 1;
1974 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1975 		}
1976 	}
1977 
1978 	/* Set actual Tx/Rx queue pairs */
1979 	vsi->num_queue_pairs = offset + qcount;
1980 
1981 	/* Setup queue TC[0].qmap for given VSI context */
1982 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1983 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1984 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1985 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1986 
1987 	/* Reconfigure RSS for main VSI with max queue count */
1988 	vsi->rss_size = max_qcount;
1989 	ret = i40e_vsi_config_rss(vsi);
1990 	if (ret) {
1991 		dev_info(&vsi->back->pdev->dev,
1992 			 "Failed to reconfig rss for num_queues (%u)\n",
1993 			 max_qcount);
1994 		return ret;
1995 	}
1996 	vsi->reconfig_rss = true;
1997 	dev_dbg(&vsi->back->pdev->dev,
1998 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1999 
2000 	/* Find queue count available for channel VSIs and starting offset
2001 	 * for channel VSIs
2002 	 */
2003 	override_q = vsi->mqprio_qopt.qopt.count[0];
2004 	if (override_q && override_q < vsi->num_queue_pairs) {
2005 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2006 		vsi->next_base_queue = override_q;
2007 	}
2008 	return 0;
2009 }
2010 
2011 /**
2012  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2013  * @vsi: the VSI being setup
2014  * @ctxt: VSI context structure
2015  * @enabled_tc: Enabled TCs bitmap
2016  * @is_add: True if called before Add VSI
2017  *
2018  * Setup VSI queue mapping for enabled traffic classes.
2019  **/
2020 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2021 				     struct i40e_vsi_context *ctxt,
2022 				     u8 enabled_tc,
2023 				     bool is_add)
2024 {
2025 	struct i40e_pf *pf = vsi->back;
2026 	u16 num_tc_qps = 0;
2027 	u16 sections = 0;
2028 	u8 netdev_tc = 0;
2029 	u16 numtc = 1;
2030 	u16 qcount;
2031 	u8 offset;
2032 	u16 qmap;
2033 	int i;
2034 
2035 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2036 	offset = 0;
2037 	/* zero out queue mapping, it will get updated on the end of the function */
2038 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2039 
2040 	if (vsi->type == I40E_VSI_MAIN) {
2041 		/* This code helps add more queue to the VSI if we have
2042 		 * more cores than RSS can support, the higher cores will
2043 		 * be served by ATR or other filters. Furthermore, the
2044 		 * non-zero req_queue_pairs says that user requested a new
2045 		 * queue count via ethtool's set_channels, so use this
2046 		 * value for queues distribution across traffic classes
2047 		 * We need at least one queue pair for the interface
2048 		 * to be usable as we see in else statement.
2049 		 */
2050 		if (vsi->req_queue_pairs > 0)
2051 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2052 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2053 			vsi->num_queue_pairs = pf->num_lan_msix;
2054 		else
2055 			vsi->num_queue_pairs = 1;
2056 	}
2057 
2058 	/* Number of queues per enabled TC */
2059 	if (vsi->type == I40E_VSI_MAIN ||
2060 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2061 		num_tc_qps = vsi->num_queue_pairs;
2062 	else
2063 		num_tc_qps = vsi->alloc_queue_pairs;
2064 
2065 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2066 		/* Find numtc from enabled TC bitmap */
2067 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2068 			if (enabled_tc & BIT(i)) /* TC is enabled */
2069 				numtc++;
2070 		}
2071 		if (!numtc) {
2072 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2073 			numtc = 1;
2074 		}
2075 		num_tc_qps = num_tc_qps / numtc;
2076 		num_tc_qps = min_t(int, num_tc_qps,
2077 				   i40e_pf_get_max_q_per_tc(pf));
2078 	}
2079 
2080 	vsi->tc_config.numtc = numtc;
2081 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2082 
2083 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2084 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2085 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2086 
2087 	/* Setup queue offset/count for all TCs for given VSI */
2088 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2089 		/* See if the given TC is enabled for the given VSI */
2090 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2091 			/* TC is enabled */
2092 			int pow, num_qps;
2093 
2094 			switch (vsi->type) {
2095 			case I40E_VSI_MAIN:
2096 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2097 					       pf->flags) &&
2098 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2099 					       pf->flags)) ||
2100 				    vsi->tc_config.enabled_tc != 1) {
2101 					qcount = min_t(int, pf->alloc_rss_size,
2102 						       num_tc_qps);
2103 					break;
2104 				}
2105 				fallthrough;
2106 			case I40E_VSI_FDIR:
2107 			case I40E_VSI_SRIOV:
2108 			case I40E_VSI_VMDQ2:
2109 			default:
2110 				qcount = num_tc_qps;
2111 				WARN_ON(i != 0);
2112 				break;
2113 			}
2114 			vsi->tc_config.tc_info[i].qoffset = offset;
2115 			vsi->tc_config.tc_info[i].qcount = qcount;
2116 
2117 			/* find the next higher power-of-2 of num queue pairs */
2118 			num_qps = qcount;
2119 			pow = 0;
2120 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2121 				pow++;
2122 				num_qps >>= 1;
2123 			}
2124 
2125 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2126 			qmap =
2127 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2128 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2129 
2130 			offset += qcount;
2131 		} else {
2132 			/* TC is not enabled so set the offset to
2133 			 * default queue and allocate one queue
2134 			 * for the given TC.
2135 			 */
2136 			vsi->tc_config.tc_info[i].qoffset = 0;
2137 			vsi->tc_config.tc_info[i].qcount = 1;
2138 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2139 
2140 			qmap = 0;
2141 		}
2142 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2143 	}
2144 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2145 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2146 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2147 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2148 		vsi->num_queue_pairs = offset;
2149 
2150 	/* Scheduler section valid can only be set for ADD VSI */
2151 	if (is_add) {
2152 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2153 
2154 		ctxt->info.up_enable_bits = enabled_tc;
2155 	}
2156 	if (vsi->type == I40E_VSI_SRIOV) {
2157 		ctxt->info.mapping_flags |=
2158 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2159 		for (i = 0; i < vsi->num_queue_pairs; i++)
2160 			ctxt->info.queue_mapping[i] =
2161 					       cpu_to_le16(vsi->base_queue + i);
2162 	} else {
2163 		ctxt->info.mapping_flags |=
2164 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2165 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2166 	}
2167 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2168 }
2169 
2170 /**
2171  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2172  * @netdev: the netdevice
2173  * @addr: address to add
2174  *
2175  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2176  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2177  */
2178 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2179 {
2180 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2181 	struct i40e_vsi *vsi = np->vsi;
2182 
2183 	if (i40e_add_mac_filter(vsi, addr))
2184 		return 0;
2185 	else
2186 		return -ENOMEM;
2187 }
2188 
2189 /**
2190  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2191  * @netdev: the netdevice
2192  * @addr: address to add
2193  *
2194  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2195  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2196  */
2197 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2198 {
2199 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2200 	struct i40e_vsi *vsi = np->vsi;
2201 
2202 	/* Under some circumstances, we might receive a request to delete
2203 	 * our own device address from our uc list. Because we store the
2204 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2205 	 * such requests and not delete our device address from this list.
2206 	 */
2207 	if (ether_addr_equal(addr, netdev->dev_addr))
2208 		return 0;
2209 
2210 	i40e_del_mac_filter(vsi, addr);
2211 
2212 	return 0;
2213 }
2214 
2215 /**
2216  * i40e_set_rx_mode - NDO callback to set the netdev filters
2217  * @netdev: network interface device structure
2218  **/
2219 static void i40e_set_rx_mode(struct net_device *netdev)
2220 {
2221 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2222 	struct i40e_vsi *vsi = np->vsi;
2223 
2224 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2225 
2226 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2227 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2228 
2229 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2230 
2231 	/* check for other flag changes */
2232 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2233 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2234 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2235 	}
2236 }
2237 
2238 /**
2239  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2240  * @vsi: Pointer to VSI struct
2241  * @from: Pointer to list which contains MAC filter entries - changes to
2242  *        those entries needs to be undone.
2243  *
2244  * MAC filter entries from this list were slated for deletion.
2245  **/
2246 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2247 					 struct hlist_head *from)
2248 {
2249 	struct i40e_mac_filter *f;
2250 	struct hlist_node *h;
2251 
2252 	hlist_for_each_entry_safe(f, h, from, hlist) {
2253 		u64 key = i40e_addr_to_hkey(f->macaddr);
2254 
2255 		/* Move the element back into MAC filter list*/
2256 		hlist_del(&f->hlist);
2257 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2258 	}
2259 }
2260 
2261 /**
2262  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2263  * @vsi: Pointer to vsi struct
2264  * @from: Pointer to list which contains MAC filter entries - changes to
2265  *        those entries needs to be undone.
2266  *
2267  * MAC filter entries from this list were slated for addition.
2268  **/
2269 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2270 					 struct hlist_head *from)
2271 {
2272 	struct i40e_new_mac_filter *new;
2273 	struct hlist_node *h;
2274 
2275 	hlist_for_each_entry_safe(new, h, from, hlist) {
2276 		/* We can simply free the wrapper structure */
2277 		hlist_del(&new->hlist);
2278 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2279 		kfree(new);
2280 	}
2281 }
2282 
2283 /**
2284  * i40e_next_filter - Get the next non-broadcast filter from a list
2285  * @next: pointer to filter in list
2286  *
2287  * Returns the next non-broadcast filter in the list. Required so that we
2288  * ignore broadcast filters within the list, since these are not handled via
2289  * the normal firmware update path.
2290  */
2291 static
2292 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2293 {
2294 	hlist_for_each_entry_continue(next, hlist) {
2295 		if (!is_broadcast_ether_addr(next->f->macaddr))
2296 			return next;
2297 	}
2298 
2299 	return NULL;
2300 }
2301 
2302 /**
2303  * i40e_update_filter_state - Update filter state based on return data
2304  * from firmware
2305  * @count: Number of filters added
2306  * @add_list: return data from fw
2307  * @add_head: pointer to first filter in current batch
2308  *
2309  * MAC filter entries from list were slated to be added to device. Returns
2310  * number of successful filters. Note that 0 does NOT mean success!
2311  **/
2312 static int
2313 i40e_update_filter_state(int count,
2314 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2315 			 struct i40e_new_mac_filter *add_head)
2316 {
2317 	int retval = 0;
2318 	int i;
2319 
2320 	for (i = 0; i < count; i++) {
2321 		/* Always check status of each filter. We don't need to check
2322 		 * the firmware return status because we pre-set the filter
2323 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2324 		 * request to the adminq. Thus, if it no longer matches then
2325 		 * we know the filter is active.
2326 		 */
2327 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2328 			add_head->state = I40E_FILTER_FAILED;
2329 		} else {
2330 			add_head->state = I40E_FILTER_ACTIVE;
2331 			retval++;
2332 		}
2333 
2334 		add_head = i40e_next_filter(add_head);
2335 		if (!add_head)
2336 			break;
2337 	}
2338 
2339 	return retval;
2340 }
2341 
2342 /**
2343  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2344  * @vsi: ptr to the VSI
2345  * @vsi_name: name to display in messages
2346  * @list: the list of filters to send to firmware
2347  * @num_del: the number of filters to delete
2348  * @retval: Set to -EIO on failure to delete
2349  *
2350  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2351  * *retval instead of a return value so that success does not force ret_val to
2352  * be set to 0. This ensures that a sequence of calls to this function
2353  * preserve the previous value of *retval on successful delete.
2354  */
2355 static
2356 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2357 			  struct i40e_aqc_remove_macvlan_element_data *list,
2358 			  int num_del, int *retval)
2359 {
2360 	struct i40e_hw *hw = &vsi->back->hw;
2361 	enum i40e_admin_queue_err aq_status;
2362 	int aq_ret;
2363 
2364 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2365 					   &aq_status);
2366 
2367 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2368 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2369 		*retval = -EIO;
2370 		dev_info(&vsi->back->pdev->dev,
2371 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2372 			 vsi_name, ERR_PTR(aq_ret),
2373 			 i40e_aq_str(hw, aq_status));
2374 	}
2375 }
2376 
2377 /**
2378  * i40e_aqc_add_filters - Request firmware to add a set of filters
2379  * @vsi: ptr to the VSI
2380  * @vsi_name: name to display in messages
2381  * @list: the list of filters to send to firmware
2382  * @add_head: Position in the add hlist
2383  * @num_add: the number of filters to add
2384  *
2385  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2386  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2387  * space for more filters.
2388  */
2389 static
2390 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2391 			  struct i40e_aqc_add_macvlan_element_data *list,
2392 			  struct i40e_new_mac_filter *add_head,
2393 			  int num_add)
2394 {
2395 	struct i40e_hw *hw = &vsi->back->hw;
2396 	enum i40e_admin_queue_err aq_status;
2397 	int fcnt;
2398 
2399 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2400 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2401 
2402 	if (fcnt != num_add) {
2403 		if (vsi->type == I40E_VSI_MAIN) {
2404 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2405 			dev_warn(&vsi->back->pdev->dev,
2406 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2407 				 i40e_aq_str(hw, aq_status), vsi_name);
2408 		} else if (vsi->type == I40E_VSI_SRIOV ||
2409 			   vsi->type == I40E_VSI_VMDQ1 ||
2410 			   vsi->type == I40E_VSI_VMDQ2) {
2411 			dev_warn(&vsi->back->pdev->dev,
2412 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2413 				 i40e_aq_str(hw, aq_status), vsi_name,
2414 					     vsi_name);
2415 		} else {
2416 			dev_warn(&vsi->back->pdev->dev,
2417 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2418 				 i40e_aq_str(hw, aq_status), vsi_name,
2419 					     vsi->type);
2420 		}
2421 	}
2422 }
2423 
2424 /**
2425  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2426  * @vsi: pointer to the VSI
2427  * @vsi_name: the VSI name
2428  * @f: filter data
2429  *
2430  * This function sets or clears the promiscuous broadcast flags for VLAN
2431  * filters in order to properly receive broadcast frames. Assumes that only
2432  * broadcast filters are passed.
2433  *
2434  * Returns status indicating success or failure;
2435  **/
2436 static int
2437 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2438 			  struct i40e_mac_filter *f)
2439 {
2440 	bool enable = f->state == I40E_FILTER_NEW;
2441 	struct i40e_hw *hw = &vsi->back->hw;
2442 	int aq_ret;
2443 
2444 	if (f->vlan == I40E_VLAN_ANY) {
2445 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2446 						   vsi->seid,
2447 						   enable,
2448 						   NULL);
2449 	} else {
2450 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2451 							    vsi->seid,
2452 							    enable,
2453 							    f->vlan,
2454 							    NULL);
2455 	}
2456 
2457 	if (aq_ret) {
2458 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2459 		dev_warn(&vsi->back->pdev->dev,
2460 			 "Error %s, forcing overflow promiscuous on %s\n",
2461 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2462 			 vsi_name);
2463 	}
2464 
2465 	return aq_ret;
2466 }
2467 
2468 /**
2469  * i40e_set_promiscuous - set promiscuous mode
2470  * @pf: board private structure
2471  * @promisc: promisc on or off
2472  *
2473  * There are different ways of setting promiscuous mode on a PF depending on
2474  * what state/environment we're in.  This identifies and sets it appropriately.
2475  * Returns 0 on success.
2476  **/
2477 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2478 {
2479 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
2480 	struct i40e_hw *hw = &pf->hw;
2481 	int aq_ret;
2482 
2483 	if (vsi->type == I40E_VSI_MAIN &&
2484 	    i40e_pf_get_main_veb(pf) &&
2485 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2486 		/* set defport ON for Main VSI instead of true promisc
2487 		 * this way we will get all unicast/multicast and VLAN
2488 		 * promisc behavior but will not get VF or VMDq traffic
2489 		 * replicated on the Main VSI.
2490 		 */
2491 		if (promisc)
2492 			aq_ret = i40e_aq_set_default_vsi(hw,
2493 							 vsi->seid,
2494 							 NULL);
2495 		else
2496 			aq_ret = i40e_aq_clear_default_vsi(hw,
2497 							   vsi->seid,
2498 							   NULL);
2499 		if (aq_ret) {
2500 			dev_info(&pf->pdev->dev,
2501 				 "Set default VSI failed, err %pe, aq_err %s\n",
2502 				 ERR_PTR(aq_ret),
2503 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2504 		}
2505 	} else {
2506 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2507 						  hw,
2508 						  vsi->seid,
2509 						  promisc, NULL,
2510 						  true);
2511 		if (aq_ret) {
2512 			dev_info(&pf->pdev->dev,
2513 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2514 				 ERR_PTR(aq_ret),
2515 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2516 		}
2517 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2518 						  hw,
2519 						  vsi->seid,
2520 						  promisc, NULL);
2521 		if (aq_ret) {
2522 			dev_info(&pf->pdev->dev,
2523 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2524 				 ERR_PTR(aq_ret),
2525 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2526 		}
2527 	}
2528 
2529 	if (!aq_ret)
2530 		pf->cur_promisc = promisc;
2531 
2532 	return aq_ret;
2533 }
2534 
2535 /**
2536  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2537  * @vsi: ptr to the VSI
2538  *
2539  * Push any outstanding VSI filter changes through the AdminQ.
2540  *
2541  * Returns 0 or error value
2542  **/
2543 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2544 {
2545 	struct hlist_head tmp_add_list, tmp_del_list;
2546 	struct i40e_mac_filter *f;
2547 	struct i40e_new_mac_filter *new, *add_head = NULL;
2548 	struct i40e_hw *hw = &vsi->back->hw;
2549 	bool old_overflow, new_overflow;
2550 	unsigned int failed_filters = 0;
2551 	unsigned int vlan_filters = 0;
2552 	char vsi_name[16] = "PF";
2553 	int filter_list_len = 0;
2554 	u32 changed_flags = 0;
2555 	struct hlist_node *h;
2556 	struct i40e_pf *pf;
2557 	int num_add = 0;
2558 	int num_del = 0;
2559 	int aq_ret = 0;
2560 	int retval = 0;
2561 	u16 cmd_flags;
2562 	int list_size;
2563 	int bkt;
2564 
2565 	/* empty array typed pointers, kcalloc later */
2566 	struct i40e_aqc_add_macvlan_element_data *add_list;
2567 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2568 
2569 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2570 		usleep_range(1000, 2000);
2571 	pf = vsi->back;
2572 
2573 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2574 
2575 	if (vsi->netdev) {
2576 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2577 		vsi->current_netdev_flags = vsi->netdev->flags;
2578 	}
2579 
2580 	INIT_HLIST_HEAD(&tmp_add_list);
2581 	INIT_HLIST_HEAD(&tmp_del_list);
2582 
2583 	if (vsi->type == I40E_VSI_SRIOV)
2584 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2585 	else if (vsi->type != I40E_VSI_MAIN)
2586 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2587 
2588 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2589 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2590 
2591 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2592 		/* Create a list of filters to delete. */
2593 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2594 			if (f->state == I40E_FILTER_REMOVE) {
2595 				/* Move the element into temporary del_list */
2596 				hash_del(&f->hlist);
2597 				hlist_add_head(&f->hlist, &tmp_del_list);
2598 
2599 				/* Avoid counting removed filters */
2600 				continue;
2601 			}
2602 			if (f->state == I40E_FILTER_NEW) {
2603 				/* Create a temporary i40e_new_mac_filter */
2604 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2605 				if (!new)
2606 					goto err_no_memory_locked;
2607 
2608 				/* Store pointer to the real filter */
2609 				new->f = f;
2610 				new->state = f->state;
2611 
2612 				/* Add it to the hash list */
2613 				hlist_add_head(&new->hlist, &tmp_add_list);
2614 			}
2615 
2616 			/* Count the number of active (current and new) VLAN
2617 			 * filters we have now. Does not count filters which
2618 			 * are marked for deletion.
2619 			 */
2620 			if (f->vlan > 0)
2621 				vlan_filters++;
2622 		}
2623 
2624 		if (vsi->type != I40E_VSI_SRIOV)
2625 			retval = i40e_correct_mac_vlan_filters
2626 				(vsi, &tmp_add_list, &tmp_del_list,
2627 				 vlan_filters);
2628 		else if (pf->vf)
2629 			retval = i40e_correct_vf_mac_vlan_filters
2630 				(vsi, &tmp_add_list, &tmp_del_list,
2631 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2632 
2633 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2634 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2635 
2636 		if (retval)
2637 			goto err_no_memory_locked;
2638 
2639 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2640 	}
2641 
2642 	/* Now process 'del_list' outside the lock */
2643 	if (!hlist_empty(&tmp_del_list)) {
2644 		filter_list_len = hw->aq.asq_buf_size /
2645 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2646 		list_size = filter_list_len *
2647 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2648 		del_list = kzalloc(list_size, GFP_ATOMIC);
2649 		if (!del_list)
2650 			goto err_no_memory;
2651 
2652 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2653 			cmd_flags = 0;
2654 
2655 			/* handle broadcast filters by updating the broadcast
2656 			 * promiscuous flag and release filter list.
2657 			 */
2658 			if (is_broadcast_ether_addr(f->macaddr)) {
2659 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2660 
2661 				hlist_del(&f->hlist);
2662 				kfree(f);
2663 				continue;
2664 			}
2665 
2666 			/* add to delete list */
2667 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2668 			if (f->vlan == I40E_VLAN_ANY) {
2669 				del_list[num_del].vlan_tag = 0;
2670 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2671 			} else {
2672 				del_list[num_del].vlan_tag =
2673 					cpu_to_le16((u16)(f->vlan));
2674 			}
2675 
2676 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2677 			del_list[num_del].flags = cmd_flags;
2678 			num_del++;
2679 
2680 			/* flush a full buffer */
2681 			if (num_del == filter_list_len) {
2682 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2683 						     num_del, &retval);
2684 				memset(del_list, 0, list_size);
2685 				num_del = 0;
2686 			}
2687 			/* Release memory for MAC filter entries which were
2688 			 * synced up with HW.
2689 			 */
2690 			hlist_del(&f->hlist);
2691 			kfree(f);
2692 		}
2693 
2694 		if (num_del) {
2695 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2696 					     num_del, &retval);
2697 		}
2698 
2699 		kfree(del_list);
2700 		del_list = NULL;
2701 	}
2702 
2703 	if (!hlist_empty(&tmp_add_list)) {
2704 		/* Do all the adds now. */
2705 		filter_list_len = hw->aq.asq_buf_size /
2706 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2707 		list_size = filter_list_len *
2708 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2709 		add_list = kzalloc(list_size, GFP_ATOMIC);
2710 		if (!add_list)
2711 			goto err_no_memory;
2712 
2713 		num_add = 0;
2714 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2715 			/* handle broadcast filters by updating the broadcast
2716 			 * promiscuous flag instead of adding a MAC filter.
2717 			 */
2718 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2719 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2720 							      new->f))
2721 					new->state = I40E_FILTER_FAILED;
2722 				else
2723 					new->state = I40E_FILTER_ACTIVE;
2724 				continue;
2725 			}
2726 
2727 			/* add to add array */
2728 			if (num_add == 0)
2729 				add_head = new;
2730 			cmd_flags = 0;
2731 			ether_addr_copy(add_list[num_add].mac_addr,
2732 					new->f->macaddr);
2733 			if (new->f->vlan == I40E_VLAN_ANY) {
2734 				add_list[num_add].vlan_tag = 0;
2735 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2736 			} else {
2737 				add_list[num_add].vlan_tag =
2738 					cpu_to_le16((u16)(new->f->vlan));
2739 			}
2740 			add_list[num_add].queue_number = 0;
2741 			/* set invalid match method for later detection */
2742 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2743 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2744 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2745 			num_add++;
2746 
2747 			/* flush a full buffer */
2748 			if (num_add == filter_list_len) {
2749 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2750 						     add_head, num_add);
2751 				memset(add_list, 0, list_size);
2752 				num_add = 0;
2753 			}
2754 		}
2755 		if (num_add) {
2756 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2757 					     num_add);
2758 		}
2759 		/* Now move all of the filters from the temp add list back to
2760 		 * the VSI's list.
2761 		 */
2762 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2763 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2764 			/* Only update the state if we're still NEW */
2765 			if (new->f->state == I40E_FILTER_NEW)
2766 				new->f->state = new->state;
2767 			hlist_del(&new->hlist);
2768 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2769 			kfree(new);
2770 		}
2771 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2772 		kfree(add_list);
2773 		add_list = NULL;
2774 	}
2775 
2776 	/* Determine the number of active and failed filters. */
2777 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2778 	vsi->active_filters = 0;
2779 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2780 		if (f->state == I40E_FILTER_ACTIVE)
2781 			vsi->active_filters++;
2782 		else if (f->state == I40E_FILTER_FAILED)
2783 			failed_filters++;
2784 	}
2785 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2786 
2787 	/* Check if we are able to exit overflow promiscuous mode. We can
2788 	 * safely exit if we didn't just enter, we no longer have any failed
2789 	 * filters, and we have reduced filters below the threshold value.
2790 	 */
2791 	if (old_overflow && !failed_filters &&
2792 	    vsi->active_filters < vsi->promisc_threshold) {
2793 		dev_info(&pf->pdev->dev,
2794 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2795 			 vsi_name);
2796 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2797 		vsi->promisc_threshold = 0;
2798 	}
2799 
2800 	/* if the VF is not trusted do not do promisc */
2801 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2802 	    !pf->vf[vsi->vf_id].trusted) {
2803 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2804 		goto out;
2805 	}
2806 
2807 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2808 
2809 	/* If we are entering overflow promiscuous, we need to calculate a new
2810 	 * threshold for when we are safe to exit
2811 	 */
2812 	if (!old_overflow && new_overflow)
2813 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2814 
2815 	/* check for changes in promiscuous modes */
2816 	if (changed_flags & IFF_ALLMULTI) {
2817 		bool cur_multipromisc;
2818 
2819 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2820 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2821 							       vsi->seid,
2822 							       cur_multipromisc,
2823 							       NULL);
2824 		if (aq_ret) {
2825 			retval = i40e_aq_rc_to_posix(aq_ret,
2826 						     hw->aq.asq_last_status);
2827 			dev_info(&pf->pdev->dev,
2828 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2829 				 vsi_name,
2830 				 ERR_PTR(aq_ret),
2831 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2832 		} else {
2833 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2834 				 cur_multipromisc ? "entering" : "leaving");
2835 		}
2836 	}
2837 
2838 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2839 		bool cur_promisc;
2840 
2841 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2842 			       new_overflow);
2843 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2844 		if (aq_ret) {
2845 			retval = i40e_aq_rc_to_posix(aq_ret,
2846 						     hw->aq.asq_last_status);
2847 			dev_info(&pf->pdev->dev,
2848 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2849 				 cur_promisc ? "on" : "off",
2850 				 vsi_name,
2851 				 ERR_PTR(aq_ret),
2852 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2853 		}
2854 	}
2855 out:
2856 	/* if something went wrong then set the changed flag so we try again */
2857 	if (retval)
2858 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2859 
2860 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2861 	return retval;
2862 
2863 err_no_memory:
2864 	/* Restore elements on the temporary add and delete lists */
2865 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2866 err_no_memory_locked:
2867 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2868 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2869 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2870 
2871 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2872 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2873 	return -ENOMEM;
2874 }
2875 
2876 /**
2877  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2878  * @pf: board private structure
2879  **/
2880 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2881 {
2882 	struct i40e_vsi *vsi;
2883 	int v;
2884 
2885 	if (!pf)
2886 		return;
2887 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2888 		return;
2889 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2890 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2891 		return;
2892 	}
2893 
2894 	i40e_pf_for_each_vsi(pf, v, vsi) {
2895 		if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2896 		    !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
2897 			int ret = i40e_sync_vsi_filters(vsi);
2898 
2899 			if (ret) {
2900 				/* come back and try again later */
2901 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2902 					pf->state);
2903 				break;
2904 			}
2905 		}
2906 	}
2907 }
2908 
2909 /**
2910  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2911  *
2912  * @vsi: VSI to calculate rx_buf_len from
2913  */
2914 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2915 {
2916 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2917 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2918 
2919 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2920 }
2921 
2922 /**
2923  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2924  * @vsi: the vsi
2925  * @xdp_prog: XDP program
2926  **/
2927 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2928 				   struct bpf_prog *xdp_prog)
2929 {
2930 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2931 	u16 chain_len;
2932 
2933 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2934 		chain_len = 1;
2935 	else
2936 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2937 
2938 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2939 }
2940 
2941 /**
2942  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2943  * @netdev: network interface device structure
2944  * @new_mtu: new value for maximum frame size
2945  *
2946  * Returns 0 on success, negative on failure
2947  **/
2948 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2949 {
2950 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2951 	struct i40e_vsi *vsi = np->vsi;
2952 	struct i40e_pf *pf = vsi->back;
2953 	int frame_size;
2954 
2955 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2956 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2957 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2958 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2959 		return -EINVAL;
2960 	}
2961 
2962 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2963 		   netdev->mtu, new_mtu);
2964 	WRITE_ONCE(netdev->mtu, new_mtu);
2965 	if (netif_running(netdev))
2966 		i40e_vsi_reinit_locked(vsi);
2967 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2968 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2969 	return 0;
2970 }
2971 
2972 /**
2973  * i40e_ioctl - Access the hwtstamp interface
2974  * @netdev: network interface device structure
2975  * @ifr: interface request data
2976  * @cmd: ioctl command
2977  **/
2978 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2979 {
2980 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2981 	struct i40e_pf *pf = np->vsi->back;
2982 
2983 	switch (cmd) {
2984 	case SIOCGHWTSTAMP:
2985 		return i40e_ptp_get_ts_config(pf, ifr);
2986 	case SIOCSHWTSTAMP:
2987 		return i40e_ptp_set_ts_config(pf, ifr);
2988 	default:
2989 		return -EOPNOTSUPP;
2990 	}
2991 }
2992 
2993 /**
2994  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2995  * @vsi: the vsi being adjusted
2996  **/
2997 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2998 {
2999 	struct i40e_vsi_context ctxt;
3000 	int ret;
3001 
3002 	/* Don't modify stripping options if a port VLAN is active */
3003 	if (vsi->info.pvid)
3004 		return;
3005 
3006 	if ((vsi->info.valid_sections &
3007 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3008 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3009 		return;  /* already enabled */
3010 
3011 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3012 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3013 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3014 
3015 	ctxt.seid = vsi->seid;
3016 	ctxt.info = vsi->info;
3017 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3018 	if (ret) {
3019 		dev_info(&vsi->back->pdev->dev,
3020 			 "update vlan stripping failed, err %pe aq_err %s\n",
3021 			 ERR_PTR(ret),
3022 			 i40e_aq_str(&vsi->back->hw,
3023 				     vsi->back->hw.aq.asq_last_status));
3024 	}
3025 }
3026 
3027 /**
3028  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3029  * @vsi: the vsi being adjusted
3030  **/
3031 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3032 {
3033 	struct i40e_vsi_context ctxt;
3034 	int ret;
3035 
3036 	/* Don't modify stripping options if a port VLAN is active */
3037 	if (vsi->info.pvid)
3038 		return;
3039 
3040 	if ((vsi->info.valid_sections &
3041 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3042 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3043 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3044 		return;  /* already disabled */
3045 
3046 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3047 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3048 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3049 
3050 	ctxt.seid = vsi->seid;
3051 	ctxt.info = vsi->info;
3052 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3053 	if (ret) {
3054 		dev_info(&vsi->back->pdev->dev,
3055 			 "update vlan stripping failed, err %pe aq_err %s\n",
3056 			 ERR_PTR(ret),
3057 			 i40e_aq_str(&vsi->back->hw,
3058 				     vsi->back->hw.aq.asq_last_status));
3059 	}
3060 }
3061 
3062 /**
3063  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3064  * @vsi: the vsi being configured
3065  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3066  *
3067  * This is a helper function for adding a new MAC/VLAN filter with the
3068  * specified VLAN for each existing MAC address already in the hash table.
3069  * This function does *not* perform any accounting to update filters based on
3070  * VLAN mode.
3071  *
3072  * NOTE: this function expects to be called while under the
3073  * mac_filter_hash_lock
3074  **/
3075 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3076 {
3077 	struct i40e_mac_filter *f, *add_f;
3078 	struct hlist_node *h;
3079 	int bkt;
3080 
3081 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3082 		/* If we're asked to add a filter that has been marked for
3083 		 * removal, it is safe to simply restore it to active state.
3084 		 * __i40e_del_filter will have simply deleted any filters which
3085 		 * were previously marked NEW or FAILED, so if it is currently
3086 		 * marked REMOVE it must have previously been ACTIVE. Since we
3087 		 * haven't yet run the sync filters task, just restore this
3088 		 * filter to the ACTIVE state so that the sync task leaves it
3089 		 * in place.
3090 		 */
3091 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3092 			f->state = I40E_FILTER_ACTIVE;
3093 			continue;
3094 		} else if (f->state == I40E_FILTER_REMOVE) {
3095 			continue;
3096 		}
3097 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3098 		if (!add_f) {
3099 			dev_info(&vsi->back->pdev->dev,
3100 				 "Could not add vlan filter %d for %pM\n",
3101 				 vid, f->macaddr);
3102 			return -ENOMEM;
3103 		}
3104 	}
3105 
3106 	return 0;
3107 }
3108 
3109 /**
3110  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3111  * @vsi: the VSI being configured
3112  * @vid: VLAN id to be added
3113  **/
3114 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3115 {
3116 	int err;
3117 
3118 	if (vsi->info.pvid)
3119 		return -EINVAL;
3120 
3121 	/* The network stack will attempt to add VID=0, with the intention to
3122 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3123 	 * these packets by default when configured to receive untagged
3124 	 * packets, so we don't need to add a filter for this case.
3125 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3126 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3127 	 * Thus, we do not want to actually add a filter for VID=0
3128 	 */
3129 	if (!vid)
3130 		return 0;
3131 
3132 	/* Locked once because all functions invoked below iterates list*/
3133 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3134 	err = i40e_add_vlan_all_mac(vsi, vid);
3135 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3136 	if (err)
3137 		return err;
3138 
3139 	/* schedule our worker thread which will take care of
3140 	 * applying the new filter changes
3141 	 */
3142 	i40e_service_event_schedule(vsi->back);
3143 	return 0;
3144 }
3145 
3146 /**
3147  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3148  * @vsi: the vsi being configured
3149  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3150  *
3151  * This function should be used to remove all VLAN filters which match the
3152  * given VID. It does not schedule the service event and does not take the
3153  * mac_filter_hash_lock so it may be combined with other operations under
3154  * a single invocation of the mac_filter_hash_lock.
3155  *
3156  * NOTE: this function expects to be called while under the
3157  * mac_filter_hash_lock
3158  */
3159 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3160 {
3161 	struct i40e_mac_filter *f;
3162 	struct hlist_node *h;
3163 	int bkt;
3164 
3165 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3166 		if (f->vlan == vid)
3167 			__i40e_del_filter(vsi, f);
3168 	}
3169 }
3170 
3171 /**
3172  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3173  * @vsi: the VSI being configured
3174  * @vid: VLAN id to be removed
3175  **/
3176 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3177 {
3178 	if (!vid || vsi->info.pvid)
3179 		return;
3180 
3181 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3182 	i40e_rm_vlan_all_mac(vsi, vid);
3183 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3184 
3185 	/* schedule our worker thread which will take care of
3186 	 * applying the new filter changes
3187 	 */
3188 	i40e_service_event_schedule(vsi->back);
3189 }
3190 
3191 /**
3192  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3193  * @netdev: network interface to be adjusted
3194  * @proto: unused protocol value
3195  * @vid: vlan id to be added
3196  *
3197  * net_device_ops implementation for adding vlan ids
3198  **/
3199 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3200 				__always_unused __be16 proto, u16 vid)
3201 {
3202 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3203 	struct i40e_vsi *vsi = np->vsi;
3204 	int ret = 0;
3205 
3206 	if (vid >= VLAN_N_VID)
3207 		return -EINVAL;
3208 
3209 	ret = i40e_vsi_add_vlan(vsi, vid);
3210 	if (!ret)
3211 		set_bit(vid, vsi->active_vlans);
3212 
3213 	return ret;
3214 }
3215 
3216 /**
3217  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3218  * @netdev: network interface to be adjusted
3219  * @proto: unused protocol value
3220  * @vid: vlan id to be added
3221  **/
3222 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3223 				    __always_unused __be16 proto, u16 vid)
3224 {
3225 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3226 	struct i40e_vsi *vsi = np->vsi;
3227 
3228 	if (vid >= VLAN_N_VID)
3229 		return;
3230 	set_bit(vid, vsi->active_vlans);
3231 }
3232 
3233 /**
3234  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3235  * @netdev: network interface to be adjusted
3236  * @proto: unused protocol value
3237  * @vid: vlan id to be removed
3238  *
3239  * net_device_ops implementation for removing vlan ids
3240  **/
3241 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3242 				 __always_unused __be16 proto, u16 vid)
3243 {
3244 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3245 	struct i40e_vsi *vsi = np->vsi;
3246 
3247 	/* return code is ignored as there is nothing a user
3248 	 * can do about failure to remove and a log message was
3249 	 * already printed from the other function
3250 	 */
3251 	i40e_vsi_kill_vlan(vsi, vid);
3252 
3253 	clear_bit(vid, vsi->active_vlans);
3254 
3255 	return 0;
3256 }
3257 
3258 /**
3259  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3260  * @vsi: the vsi being brought back up
3261  **/
3262 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3263 {
3264 	u16 vid;
3265 
3266 	if (!vsi->netdev)
3267 		return;
3268 
3269 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3270 		i40e_vlan_stripping_enable(vsi);
3271 	else
3272 		i40e_vlan_stripping_disable(vsi);
3273 
3274 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3275 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3276 					vid);
3277 }
3278 
3279 /**
3280  * i40e_vsi_add_pvid - Add pvid for the VSI
3281  * @vsi: the vsi being adjusted
3282  * @vid: the vlan id to set as a PVID
3283  **/
3284 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3285 {
3286 	struct i40e_vsi_context ctxt;
3287 	int ret;
3288 
3289 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3290 	vsi->info.pvid = cpu_to_le16(vid);
3291 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3292 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3293 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3294 
3295 	ctxt.seid = vsi->seid;
3296 	ctxt.info = vsi->info;
3297 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3298 	if (ret) {
3299 		dev_info(&vsi->back->pdev->dev,
3300 			 "add pvid failed, err %pe aq_err %s\n",
3301 			 ERR_PTR(ret),
3302 			 i40e_aq_str(&vsi->back->hw,
3303 				     vsi->back->hw.aq.asq_last_status));
3304 		return -ENOENT;
3305 	}
3306 
3307 	return 0;
3308 }
3309 
3310 /**
3311  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3312  * @vsi: the vsi being adjusted
3313  *
3314  * Just use the vlan_rx_register() service to put it back to normal
3315  **/
3316 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3317 {
3318 	vsi->info.pvid = 0;
3319 
3320 	i40e_vlan_stripping_disable(vsi);
3321 }
3322 
3323 /**
3324  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3325  * @vsi: ptr to the VSI
3326  *
3327  * If this function returns with an error, then it's possible one or
3328  * more of the rings is populated (while the rest are not).  It is the
3329  * callers duty to clean those orphaned rings.
3330  *
3331  * Return 0 on success, negative on failure
3332  **/
3333 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3334 {
3335 	int i, err = 0;
3336 
3337 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3338 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3339 
3340 	if (!i40e_enabled_xdp_vsi(vsi))
3341 		return err;
3342 
3343 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3344 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3345 
3346 	return err;
3347 }
3348 
3349 /**
3350  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3351  * @vsi: ptr to the VSI
3352  *
3353  * Free VSI's transmit software resources
3354  **/
3355 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3356 {
3357 	int i;
3358 
3359 	if (vsi->tx_rings) {
3360 		for (i = 0; i < vsi->num_queue_pairs; i++)
3361 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3362 				i40e_free_tx_resources(vsi->tx_rings[i]);
3363 	}
3364 
3365 	if (vsi->xdp_rings) {
3366 		for (i = 0; i < vsi->num_queue_pairs; i++)
3367 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3368 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3369 	}
3370 }
3371 
3372 /**
3373  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3374  * @vsi: ptr to the VSI
3375  *
3376  * If this function returns with an error, then it's possible one or
3377  * more of the rings is populated (while the rest are not).  It is the
3378  * callers duty to clean those orphaned rings.
3379  *
3380  * Return 0 on success, negative on failure
3381  **/
3382 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3383 {
3384 	int i, err = 0;
3385 
3386 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3387 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3388 	return err;
3389 }
3390 
3391 /**
3392  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3393  * @vsi: ptr to the VSI
3394  *
3395  * Free all receive software resources
3396  **/
3397 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3398 {
3399 	int i;
3400 
3401 	if (!vsi->rx_rings)
3402 		return;
3403 
3404 	for (i = 0; i < vsi->num_queue_pairs; i++)
3405 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3406 			i40e_free_rx_resources(vsi->rx_rings[i]);
3407 }
3408 
3409 /**
3410  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3411  * @ring: The Tx ring to configure
3412  *
3413  * This enables/disables XPS for a given Tx descriptor ring
3414  * based on the TCs enabled for the VSI that ring belongs to.
3415  **/
3416 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3417 {
3418 	int cpu;
3419 
3420 	if (!ring->q_vector || !ring->netdev || ring->ch)
3421 		return;
3422 
3423 	/* We only initialize XPS once, so as not to overwrite user settings */
3424 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3425 		return;
3426 
3427 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3428 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3429 			    ring->queue_index);
3430 }
3431 
3432 /**
3433  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3434  * @ring: The Tx or Rx ring
3435  *
3436  * Returns the AF_XDP buffer pool or NULL.
3437  **/
3438 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3439 {
3440 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3441 	int qid = ring->queue_index;
3442 
3443 	if (ring_is_xdp(ring))
3444 		qid -= ring->vsi->alloc_queue_pairs;
3445 
3446 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3447 		return NULL;
3448 
3449 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3450 }
3451 
3452 /**
3453  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3454  * @ring: The Tx ring to configure
3455  *
3456  * Configure the Tx descriptor ring in the HMC context.
3457  **/
3458 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3459 {
3460 	struct i40e_vsi *vsi = ring->vsi;
3461 	u16 pf_q = vsi->base_queue + ring->queue_index;
3462 	struct i40e_hw *hw = &vsi->back->hw;
3463 	struct i40e_hmc_obj_txq tx_ctx;
3464 	u32 qtx_ctl = 0;
3465 	int err = 0;
3466 
3467 	if (ring_is_xdp(ring))
3468 		ring->xsk_pool = i40e_xsk_pool(ring);
3469 
3470 	/* some ATR related tx ring init */
3471 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3472 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3473 		ring->atr_count = 0;
3474 	} else {
3475 		ring->atr_sample_rate = 0;
3476 	}
3477 
3478 	/* configure XPS */
3479 	i40e_config_xps_tx_ring(ring);
3480 
3481 	/* clear the context structure first */
3482 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3483 
3484 	tx_ctx.new_context = 1;
3485 	tx_ctx.base = (ring->dma / 128);
3486 	tx_ctx.qlen = ring->count;
3487 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3488 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3489 		tx_ctx.fd_ena = 1;
3490 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3491 		tx_ctx.timesync_ena = 1;
3492 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3493 	if (vsi->type != I40E_VSI_FDIR)
3494 		tx_ctx.head_wb_ena = 1;
3495 	tx_ctx.head_wb_addr = ring->dma +
3496 			      (ring->count * sizeof(struct i40e_tx_desc));
3497 
3498 	/* As part of VSI creation/update, FW allocates certain
3499 	 * Tx arbitration queue sets for each TC enabled for
3500 	 * the VSI. The FW returns the handles to these queue
3501 	 * sets as part of the response buffer to Add VSI,
3502 	 * Update VSI, etc. AQ commands. It is expected that
3503 	 * these queue set handles be associated with the Tx
3504 	 * queues by the driver as part of the TX queue context
3505 	 * initialization. This has to be done regardless of
3506 	 * DCB as by default everything is mapped to TC0.
3507 	 */
3508 
3509 	if (ring->ch)
3510 		tx_ctx.rdylist =
3511 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3512 
3513 	else
3514 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3515 
3516 	tx_ctx.rdylist_act = 0;
3517 
3518 	/* clear the context in the HMC */
3519 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3520 	if (err) {
3521 		dev_info(&vsi->back->pdev->dev,
3522 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3523 			 ring->queue_index, pf_q, err);
3524 		return -ENOMEM;
3525 	}
3526 
3527 	/* set the context in the HMC */
3528 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3529 	if (err) {
3530 		dev_info(&vsi->back->pdev->dev,
3531 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3532 			 ring->queue_index, pf_q, err);
3533 		return -ENOMEM;
3534 	}
3535 
3536 	/* Now associate this queue with this PCI function */
3537 	if (ring->ch) {
3538 		if (ring->ch->type == I40E_VSI_VMDQ2)
3539 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3540 		else
3541 			return -EINVAL;
3542 
3543 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3544 				      ring->ch->vsi_number);
3545 	} else {
3546 		if (vsi->type == I40E_VSI_VMDQ2) {
3547 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3548 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3549 					      vsi->id);
3550 		} else {
3551 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3552 		}
3553 	}
3554 
3555 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3556 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3557 	i40e_flush(hw);
3558 
3559 	/* cache tail off for easier writes later */
3560 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3561 
3562 	return 0;
3563 }
3564 
3565 /**
3566  * i40e_rx_offset - Return expected offset into page to access data
3567  * @rx_ring: Ring we are requesting offset of
3568  *
3569  * Returns the offset value for ring into the data buffer.
3570  */
3571 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3572 {
3573 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3574 }
3575 
3576 /**
3577  * i40e_configure_rx_ring - Configure a receive ring context
3578  * @ring: The Rx ring to configure
3579  *
3580  * Configure the Rx descriptor ring in the HMC context.
3581  **/
3582 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3583 {
3584 	struct i40e_vsi *vsi = ring->vsi;
3585 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3586 	u16 pf_q = vsi->base_queue + ring->queue_index;
3587 	struct i40e_hw *hw = &vsi->back->hw;
3588 	struct i40e_hmc_obj_rxq rx_ctx;
3589 	int err = 0;
3590 	bool ok;
3591 
3592 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3593 
3594 	/* clear the context structure first */
3595 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3596 
3597 	ring->rx_buf_len = vsi->rx_buf_len;
3598 
3599 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3600 	if (ring->vsi->type != I40E_VSI_MAIN)
3601 		goto skip;
3602 
3603 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3604 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3605 					 ring->queue_index,
3606 					 ring->q_vector->napi.napi_id,
3607 					 ring->rx_buf_len);
3608 		if (err)
3609 			return err;
3610 	}
3611 
3612 	ring->xsk_pool = i40e_xsk_pool(ring);
3613 	if (ring->xsk_pool) {
3614 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3615 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3616 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3617 					 ring->queue_index,
3618 					 ring->q_vector->napi.napi_id,
3619 					 ring->rx_buf_len);
3620 		if (err)
3621 			return err;
3622 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3623 						 MEM_TYPE_XSK_BUFF_POOL,
3624 						 NULL);
3625 		if (err)
3626 			return err;
3627 		dev_info(&vsi->back->pdev->dev,
3628 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3629 			 ring->queue_index);
3630 
3631 	} else {
3632 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3633 						 MEM_TYPE_PAGE_SHARED,
3634 						 NULL);
3635 		if (err)
3636 			return err;
3637 	}
3638 
3639 skip:
3640 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3641 
3642 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3643 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3644 
3645 	rx_ctx.base = (ring->dma / 128);
3646 	rx_ctx.qlen = ring->count;
3647 
3648 	/* use 16 byte descriptors */
3649 	rx_ctx.dsize = 0;
3650 
3651 	/* descriptor type is always zero
3652 	 * rx_ctx.dtype = 0;
3653 	 */
3654 	rx_ctx.hsplit_0 = 0;
3655 
3656 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3657 	if (hw->revision_id == 0)
3658 		rx_ctx.lrxqthresh = 0;
3659 	else
3660 		rx_ctx.lrxqthresh = 1;
3661 	rx_ctx.crcstrip = 1;
3662 	rx_ctx.l2tsel = 1;
3663 	/* this controls whether VLAN is stripped from inner headers */
3664 	rx_ctx.showiv = 0;
3665 	/* set the prefena field to 1 because the manual says to */
3666 	rx_ctx.prefena = 1;
3667 
3668 	/* clear the context in the HMC */
3669 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3670 	if (err) {
3671 		dev_info(&vsi->back->pdev->dev,
3672 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3673 			 ring->queue_index, pf_q, err);
3674 		return -ENOMEM;
3675 	}
3676 
3677 	/* set the context in the HMC */
3678 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3679 	if (err) {
3680 		dev_info(&vsi->back->pdev->dev,
3681 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3682 			 ring->queue_index, pf_q, err);
3683 		return -ENOMEM;
3684 	}
3685 
3686 	/* configure Rx buffer alignment */
3687 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3688 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3689 			dev_info(&vsi->back->pdev->dev,
3690 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3691 			return -EOPNOTSUPP;
3692 		}
3693 		clear_ring_build_skb_enabled(ring);
3694 	} else {
3695 		set_ring_build_skb_enabled(ring);
3696 	}
3697 
3698 	ring->rx_offset = i40e_rx_offset(ring);
3699 
3700 	/* cache tail for quicker writes, and clear the reg before use */
3701 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3702 	writel(0, ring->tail);
3703 
3704 	if (ring->xsk_pool) {
3705 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3706 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3707 	} else {
3708 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3709 	}
3710 	if (!ok) {
3711 		/* Log this in case the user has forgotten to give the kernel
3712 		 * any buffers, even later in the application.
3713 		 */
3714 		dev_info(&vsi->back->pdev->dev,
3715 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3716 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3717 			 ring->queue_index, pf_q);
3718 	}
3719 
3720 	return 0;
3721 }
3722 
3723 /**
3724  * i40e_vsi_configure_tx - Configure the VSI for Tx
3725  * @vsi: VSI structure describing this set of rings and resources
3726  *
3727  * Configure the Tx VSI for operation.
3728  **/
3729 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3730 {
3731 	int err = 0;
3732 	u16 i;
3733 
3734 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3735 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3736 
3737 	if (err || !i40e_enabled_xdp_vsi(vsi))
3738 		return err;
3739 
3740 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3741 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3742 
3743 	return err;
3744 }
3745 
3746 /**
3747  * i40e_vsi_configure_rx - Configure the VSI for Rx
3748  * @vsi: the VSI being configured
3749  *
3750  * Configure the Rx VSI for operation.
3751  **/
3752 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3753 {
3754 	int err = 0;
3755 	u16 i;
3756 
3757 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3758 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3759 
3760 #if (PAGE_SIZE < 8192)
3761 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3762 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3763 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3764 		vsi->max_frame = vsi->rx_buf_len;
3765 	}
3766 #endif
3767 
3768 	/* set up individual rings */
3769 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3770 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3771 
3772 	return err;
3773 }
3774 
3775 /**
3776  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3777  * @vsi: ptr to the VSI
3778  **/
3779 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3780 {
3781 	struct i40e_ring *tx_ring, *rx_ring;
3782 	u16 qoffset, qcount;
3783 	int i, n;
3784 
3785 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3786 		/* Reset the TC information */
3787 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3788 			rx_ring = vsi->rx_rings[i];
3789 			tx_ring = vsi->tx_rings[i];
3790 			rx_ring->dcb_tc = 0;
3791 			tx_ring->dcb_tc = 0;
3792 		}
3793 		return;
3794 	}
3795 
3796 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3797 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3798 			continue;
3799 
3800 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3801 		qcount = vsi->tc_config.tc_info[n].qcount;
3802 		for (i = qoffset; i < (qoffset + qcount); i++) {
3803 			rx_ring = vsi->rx_rings[i];
3804 			tx_ring = vsi->tx_rings[i];
3805 			rx_ring->dcb_tc = n;
3806 			tx_ring->dcb_tc = n;
3807 		}
3808 	}
3809 }
3810 
3811 /**
3812  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3813  * @vsi: ptr to the VSI
3814  **/
3815 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3816 {
3817 	if (vsi->netdev)
3818 		i40e_set_rx_mode(vsi->netdev);
3819 }
3820 
3821 /**
3822  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3823  * @pf: Pointer to the targeted PF
3824  *
3825  * Set all flow director counters to 0.
3826  */
3827 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3828 {
3829 	pf->fd_tcp4_filter_cnt = 0;
3830 	pf->fd_udp4_filter_cnt = 0;
3831 	pf->fd_sctp4_filter_cnt = 0;
3832 	pf->fd_ip4_filter_cnt = 0;
3833 	pf->fd_tcp6_filter_cnt = 0;
3834 	pf->fd_udp6_filter_cnt = 0;
3835 	pf->fd_sctp6_filter_cnt = 0;
3836 	pf->fd_ip6_filter_cnt = 0;
3837 }
3838 
3839 /**
3840  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3841  * @vsi: Pointer to the targeted VSI
3842  *
3843  * This function replays the hlist on the hw where all the SB Flow Director
3844  * filters were saved.
3845  **/
3846 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3847 {
3848 	struct i40e_fdir_filter *filter;
3849 	struct i40e_pf *pf = vsi->back;
3850 	struct hlist_node *node;
3851 
3852 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3853 		return;
3854 
3855 	/* Reset FDir counters as we're replaying all existing filters */
3856 	i40e_reset_fdir_filter_cnt(pf);
3857 
3858 	hlist_for_each_entry_safe(filter, node,
3859 				  &pf->fdir_filter_list, fdir_node) {
3860 		i40e_add_del_fdir(vsi, filter, true);
3861 	}
3862 }
3863 
3864 /**
3865  * i40e_vsi_configure - Set up the VSI for action
3866  * @vsi: the VSI being configured
3867  **/
3868 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3869 {
3870 	int err;
3871 
3872 	i40e_set_vsi_rx_mode(vsi);
3873 	i40e_restore_vlan(vsi);
3874 	i40e_vsi_config_dcb_rings(vsi);
3875 	err = i40e_vsi_configure_tx(vsi);
3876 	if (!err)
3877 		err = i40e_vsi_configure_rx(vsi);
3878 
3879 	return err;
3880 }
3881 
3882 /**
3883  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3884  * @vsi: the VSI being configured
3885  **/
3886 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3887 {
3888 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3889 	struct i40e_pf *pf = vsi->back;
3890 	struct i40e_hw *hw = &pf->hw;
3891 	u16 vector;
3892 	int i, q;
3893 	u32 qp;
3894 
3895 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3896 	 * and PFINT_LNKLSTn registers, e.g.:
3897 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3898 	 */
3899 	qp = vsi->base_queue;
3900 	vector = vsi->base_vector;
3901 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3902 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3903 
3904 		q_vector->rx.next_update = jiffies + 1;
3905 		q_vector->rx.target_itr =
3906 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3907 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3908 		     q_vector->rx.target_itr >> 1);
3909 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3910 
3911 		q_vector->tx.next_update = jiffies + 1;
3912 		q_vector->tx.target_itr =
3913 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3914 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3915 		     q_vector->tx.target_itr >> 1);
3916 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3917 
3918 		/* Set ITR for software interrupts triggered after exiting
3919 		 * busy-loop polling.
3920 		 */
3921 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3922 		     I40E_ITR_20K);
3923 
3924 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3925 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3926 
3927 		/* begin of linked list for RX queue assigned to this vector */
3928 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3929 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3930 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3931 			u32 val;
3932 
3933 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3934 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3935 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3936 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3937 			      (I40E_QUEUE_TYPE_TX <<
3938 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3939 
3940 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3941 
3942 			if (has_xdp) {
3943 				/* TX queue with next queue set to TX */
3944 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3945 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3946 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3947 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3948 				      (I40E_QUEUE_TYPE_TX <<
3949 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3950 
3951 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3952 			}
3953 			/* TX queue with next RX or end of linked list */
3954 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3955 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3956 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3957 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3958 			      (I40E_QUEUE_TYPE_RX <<
3959 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3960 
3961 			/* Terminate the linked list */
3962 			if (q == (q_vector->num_ringpairs - 1))
3963 				val |= (I40E_QUEUE_END_OF_LIST <<
3964 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3965 
3966 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3967 			qp++;
3968 		}
3969 	}
3970 
3971 	i40e_flush(hw);
3972 }
3973 
3974 /**
3975  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3976  * @pf: pointer to private device data structure
3977  **/
3978 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3979 {
3980 	struct i40e_hw *hw = &pf->hw;
3981 	u32 val;
3982 
3983 	/* clear things first */
3984 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3985 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3986 
3987 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3988 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3989 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3990 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3991 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3992 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3993 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3994 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3995 
3996 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3997 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3998 
3999 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
4000 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4001 
4002 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
4003 
4004 	/* SW_ITR_IDX = 0, but don't change INTENA */
4005 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
4006 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
4007 
4008 	/* OTHER_ITR_IDX = 0 */
4009 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
4010 }
4011 
4012 /**
4013  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
4014  * @vsi: the VSI being configured
4015  **/
4016 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
4017 {
4018 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
4019 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4020 	struct i40e_pf *pf = vsi->back;
4021 	struct i40e_hw *hw = &pf->hw;
4022 
4023 	/* set the ITR configuration */
4024 	q_vector->rx.next_update = jiffies + 1;
4025 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4026 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4027 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4028 	q_vector->tx.next_update = jiffies + 1;
4029 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4030 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4031 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4032 
4033 	i40e_enable_misc_int_causes(pf);
4034 
4035 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4036 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4037 
4038 	/* Associate the queue pair to the vector and enable the queue
4039 	 * interrupt RX queue in linked list with next queue set to TX
4040 	 */
4041 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4042 
4043 	if (i40e_enabled_xdp_vsi(vsi)) {
4044 		/* TX queue in linked list with next queue set to TX */
4045 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4046 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4047 	}
4048 
4049 	/* last TX queue so the next RX queue doesn't matter */
4050 	wr32(hw, I40E_QINT_TQCTL(0),
4051 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4052 	i40e_flush(hw);
4053 }
4054 
4055 /**
4056  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4057  * @pf: board private structure
4058  **/
4059 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4060 {
4061 	struct i40e_hw *hw = &pf->hw;
4062 
4063 	wr32(hw, I40E_PFINT_DYN_CTL0,
4064 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4065 	i40e_flush(hw);
4066 }
4067 
4068 /**
4069  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4070  * @pf: board private structure
4071  **/
4072 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4073 {
4074 	struct i40e_hw *hw = &pf->hw;
4075 	u32 val;
4076 
4077 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4078 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4079 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4080 
4081 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4082 	i40e_flush(hw);
4083 }
4084 
4085 /**
4086  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4087  * @irq: interrupt number
4088  * @data: pointer to a q_vector
4089  **/
4090 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4091 {
4092 	struct i40e_q_vector *q_vector = data;
4093 
4094 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4095 		return IRQ_HANDLED;
4096 
4097 	napi_schedule_irqoff(&q_vector->napi);
4098 
4099 	return IRQ_HANDLED;
4100 }
4101 
4102 /**
4103  * i40e_irq_affinity_notify - Callback for affinity changes
4104  * @notify: context as to what irq was changed
4105  * @mask: the new affinity mask
4106  *
4107  * This is a callback function used by the irq_set_affinity_notifier function
4108  * so that we may register to receive changes to the irq affinity masks.
4109  **/
4110 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4111 				     const cpumask_t *mask)
4112 {
4113 	struct i40e_q_vector *q_vector =
4114 		container_of(notify, struct i40e_q_vector, affinity_notify);
4115 
4116 	cpumask_copy(&q_vector->affinity_mask, mask);
4117 }
4118 
4119 /**
4120  * i40e_irq_affinity_release - Callback for affinity notifier release
4121  * @ref: internal core kernel usage
4122  *
4123  * This is a callback function used by the irq_set_affinity_notifier function
4124  * to inform the current notification subscriber that they will no longer
4125  * receive notifications.
4126  **/
4127 static void i40e_irq_affinity_release(struct kref *ref) {}
4128 
4129 /**
4130  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4131  * @vsi: the VSI being configured
4132  * @basename: name for the vector
4133  *
4134  * Allocates MSI-X vectors and requests interrupts from the kernel.
4135  **/
4136 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4137 {
4138 	int q_vectors = vsi->num_q_vectors;
4139 	struct i40e_pf *pf = vsi->back;
4140 	int base = vsi->base_vector;
4141 	int rx_int_idx = 0;
4142 	int tx_int_idx = 0;
4143 	int vector, err;
4144 	int irq_num;
4145 	int cpu;
4146 
4147 	for (vector = 0; vector < q_vectors; vector++) {
4148 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4149 
4150 		irq_num = pf->msix_entries[base + vector].vector;
4151 
4152 		if (q_vector->tx.ring && q_vector->rx.ring) {
4153 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4154 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4155 			tx_int_idx++;
4156 		} else if (q_vector->rx.ring) {
4157 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4158 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4159 		} else if (q_vector->tx.ring) {
4160 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4161 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4162 		} else {
4163 			/* skip this unused q_vector */
4164 			continue;
4165 		}
4166 		err = request_irq(irq_num,
4167 				  vsi->irq_handler,
4168 				  0,
4169 				  q_vector->name,
4170 				  q_vector);
4171 		if (err) {
4172 			dev_info(&pf->pdev->dev,
4173 				 "MSIX request_irq failed, error: %d\n", err);
4174 			goto free_queue_irqs;
4175 		}
4176 
4177 		/* register for affinity change notifications */
4178 		q_vector->irq_num = irq_num;
4179 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4180 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4181 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4182 		/* Spread affinity hints out across online CPUs.
4183 		 *
4184 		 * get_cpu_mask returns a static constant mask with
4185 		 * a permanent lifetime so it's ok to pass to
4186 		 * irq_update_affinity_hint without making a copy.
4187 		 */
4188 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4189 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4190 	}
4191 
4192 	vsi->irqs_ready = true;
4193 	return 0;
4194 
4195 free_queue_irqs:
4196 	while (vector) {
4197 		vector--;
4198 		irq_num = pf->msix_entries[base + vector].vector;
4199 		irq_set_affinity_notifier(irq_num, NULL);
4200 		irq_update_affinity_hint(irq_num, NULL);
4201 		free_irq(irq_num, &vsi->q_vectors[vector]);
4202 	}
4203 	return err;
4204 }
4205 
4206 /**
4207  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4208  * @vsi: the VSI being un-configured
4209  **/
4210 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4211 {
4212 	struct i40e_pf *pf = vsi->back;
4213 	struct i40e_hw *hw = &pf->hw;
4214 	int base = vsi->base_vector;
4215 	int i;
4216 
4217 	/* disable interrupt causation from each queue */
4218 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4219 		u32 val;
4220 
4221 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4222 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4223 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4224 
4225 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4226 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4227 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4228 
4229 		if (!i40e_enabled_xdp_vsi(vsi))
4230 			continue;
4231 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4232 	}
4233 
4234 	/* disable each interrupt */
4235 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4236 		for (i = vsi->base_vector;
4237 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4238 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4239 
4240 		i40e_flush(hw);
4241 		for (i = 0; i < vsi->num_q_vectors; i++)
4242 			synchronize_irq(pf->msix_entries[i + base].vector);
4243 	} else {
4244 		/* Legacy and MSI mode - this stops all interrupt handling */
4245 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4246 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4247 		i40e_flush(hw);
4248 		synchronize_irq(pf->pdev->irq);
4249 	}
4250 }
4251 
4252 /**
4253  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4254  * @vsi: the VSI being configured
4255  **/
4256 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4257 {
4258 	struct i40e_pf *pf = vsi->back;
4259 	int i;
4260 
4261 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4262 		for (i = 0; i < vsi->num_q_vectors; i++)
4263 			i40e_irq_dynamic_enable(vsi, i);
4264 	} else {
4265 		i40e_irq_dynamic_enable_icr0(pf);
4266 	}
4267 
4268 	i40e_flush(&pf->hw);
4269 	return 0;
4270 }
4271 
4272 /**
4273  * i40e_free_misc_vector - Free the vector that handles non-queue events
4274  * @pf: board private structure
4275  **/
4276 static void i40e_free_misc_vector(struct i40e_pf *pf)
4277 {
4278 	/* Disable ICR 0 */
4279 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4280 	i40e_flush(&pf->hw);
4281 
4282 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4283 		free_irq(pf->msix_entries[0].vector, pf);
4284 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4285 	}
4286 }
4287 
4288 /**
4289  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4290  * @irq: interrupt number
4291  * @data: pointer to a q_vector
4292  *
4293  * This is the handler used for all MSI/Legacy interrupts, and deals
4294  * with both queue and non-queue interrupts.  This is also used in
4295  * MSIX mode to handle the non-queue interrupts.
4296  **/
4297 static irqreturn_t i40e_intr(int irq, void *data)
4298 {
4299 	struct i40e_pf *pf = (struct i40e_pf *)data;
4300 	struct i40e_hw *hw = &pf->hw;
4301 	irqreturn_t ret = IRQ_NONE;
4302 	u32 icr0, icr0_remaining;
4303 	u32 val, ena_mask;
4304 
4305 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4306 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4307 
4308 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4309 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4310 		goto enable_intr;
4311 
4312 	/* if interrupt but no bits showing, must be SWINT */
4313 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4314 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4315 		pf->sw_int_count++;
4316 
4317 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4318 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4319 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4320 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4321 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4322 	}
4323 
4324 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4325 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4326 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
4327 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4328 
4329 		/* We do not have a way to disarm Queue causes while leaving
4330 		 * interrupt enabled for all other causes, ideally
4331 		 * interrupt should be disabled while we are in NAPI but
4332 		 * this is not a performance path and napi_schedule()
4333 		 * can deal with rescheduling.
4334 		 */
4335 		if (!test_bit(__I40E_DOWN, pf->state))
4336 			napi_schedule_irqoff(&q_vector->napi);
4337 	}
4338 
4339 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4340 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4341 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4342 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4343 	}
4344 
4345 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4346 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4347 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4348 	}
4349 
4350 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4351 		/* disable any further VFLR event notifications */
4352 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4353 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4354 
4355 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4356 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4357 		} else {
4358 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4359 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4360 		}
4361 	}
4362 
4363 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4364 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4365 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4366 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4367 		val = rd32(hw, I40E_GLGEN_RSTAT);
4368 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4369 		if (val == I40E_RESET_CORER) {
4370 			pf->corer_count++;
4371 		} else if (val == I40E_RESET_GLOBR) {
4372 			pf->globr_count++;
4373 		} else if (val == I40E_RESET_EMPR) {
4374 			pf->empr_count++;
4375 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4376 		}
4377 	}
4378 
4379 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4380 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4381 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4382 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4383 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4384 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4385 	}
4386 
4387 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4388 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4389 
4390 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4391 			schedule_work(&pf->ptp_extts0_work);
4392 
4393 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4394 			i40e_ptp_tx_hwtstamp(pf);
4395 
4396 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4397 	}
4398 
4399 	/* If a critical error is pending we have no choice but to reset the
4400 	 * device.
4401 	 * Report and mask out any remaining unexpected interrupts.
4402 	 */
4403 	icr0_remaining = icr0 & ena_mask;
4404 	if (icr0_remaining) {
4405 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4406 			 icr0_remaining);
4407 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4408 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4409 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4410 			dev_info(&pf->pdev->dev, "device will be reset\n");
4411 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4412 			i40e_service_event_schedule(pf);
4413 		}
4414 		ena_mask &= ~icr0_remaining;
4415 	}
4416 	ret = IRQ_HANDLED;
4417 
4418 enable_intr:
4419 	/* re-enable interrupt causes */
4420 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4421 	if (!test_bit(__I40E_DOWN, pf->state) ||
4422 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4423 		i40e_service_event_schedule(pf);
4424 		i40e_irq_dynamic_enable_icr0(pf);
4425 	}
4426 
4427 	return ret;
4428 }
4429 
4430 /**
4431  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4432  * @tx_ring:  tx ring to clean
4433  * @budget:   how many cleans we're allowed
4434  *
4435  * Returns true if there's any budget left (e.g. the clean is finished)
4436  **/
4437 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4438 {
4439 	struct i40e_vsi *vsi = tx_ring->vsi;
4440 	u16 i = tx_ring->next_to_clean;
4441 	struct i40e_tx_buffer *tx_buf;
4442 	struct i40e_tx_desc *tx_desc;
4443 
4444 	tx_buf = &tx_ring->tx_bi[i];
4445 	tx_desc = I40E_TX_DESC(tx_ring, i);
4446 	i -= tx_ring->count;
4447 
4448 	do {
4449 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4450 
4451 		/* if next_to_watch is not set then there is no work pending */
4452 		if (!eop_desc)
4453 			break;
4454 
4455 		/* prevent any other reads prior to eop_desc */
4456 		smp_rmb();
4457 
4458 		/* if the descriptor isn't done, no work yet to do */
4459 		if (!(eop_desc->cmd_type_offset_bsz &
4460 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4461 			break;
4462 
4463 		/* clear next_to_watch to prevent false hangs */
4464 		tx_buf->next_to_watch = NULL;
4465 
4466 		tx_desc->buffer_addr = 0;
4467 		tx_desc->cmd_type_offset_bsz = 0;
4468 		/* move past filter desc */
4469 		tx_buf++;
4470 		tx_desc++;
4471 		i++;
4472 		if (unlikely(!i)) {
4473 			i -= tx_ring->count;
4474 			tx_buf = tx_ring->tx_bi;
4475 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4476 		}
4477 		/* unmap skb header data */
4478 		dma_unmap_single(tx_ring->dev,
4479 				 dma_unmap_addr(tx_buf, dma),
4480 				 dma_unmap_len(tx_buf, len),
4481 				 DMA_TO_DEVICE);
4482 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4483 			kfree(tx_buf->raw_buf);
4484 
4485 		tx_buf->raw_buf = NULL;
4486 		tx_buf->tx_flags = 0;
4487 		tx_buf->next_to_watch = NULL;
4488 		dma_unmap_len_set(tx_buf, len, 0);
4489 		tx_desc->buffer_addr = 0;
4490 		tx_desc->cmd_type_offset_bsz = 0;
4491 
4492 		/* move us past the eop_desc for start of next FD desc */
4493 		tx_buf++;
4494 		tx_desc++;
4495 		i++;
4496 		if (unlikely(!i)) {
4497 			i -= tx_ring->count;
4498 			tx_buf = tx_ring->tx_bi;
4499 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4500 		}
4501 
4502 		/* update budget accounting */
4503 		budget--;
4504 	} while (likely(budget));
4505 
4506 	i += tx_ring->count;
4507 	tx_ring->next_to_clean = i;
4508 
4509 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4510 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4511 
4512 	return budget > 0;
4513 }
4514 
4515 /**
4516  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4517  * @irq: interrupt number
4518  * @data: pointer to a q_vector
4519  **/
4520 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4521 {
4522 	struct i40e_q_vector *q_vector = data;
4523 	struct i40e_vsi *vsi;
4524 
4525 	if (!q_vector->tx.ring)
4526 		return IRQ_HANDLED;
4527 
4528 	vsi = q_vector->tx.ring->vsi;
4529 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4530 
4531 	return IRQ_HANDLED;
4532 }
4533 
4534 /**
4535  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4536  * @vsi: the VSI being configured
4537  * @v_idx: vector index
4538  * @qp_idx: queue pair index
4539  **/
4540 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4541 {
4542 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4543 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4544 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4545 
4546 	tx_ring->q_vector = q_vector;
4547 	tx_ring->next = q_vector->tx.ring;
4548 	q_vector->tx.ring = tx_ring;
4549 	q_vector->tx.count++;
4550 
4551 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4552 	if (i40e_enabled_xdp_vsi(vsi)) {
4553 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4554 
4555 		xdp_ring->q_vector = q_vector;
4556 		xdp_ring->next = q_vector->tx.ring;
4557 		q_vector->tx.ring = xdp_ring;
4558 		q_vector->tx.count++;
4559 	}
4560 
4561 	rx_ring->q_vector = q_vector;
4562 	rx_ring->next = q_vector->rx.ring;
4563 	q_vector->rx.ring = rx_ring;
4564 	q_vector->rx.count++;
4565 }
4566 
4567 /**
4568  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4569  * @vsi: the VSI being configured
4570  *
4571  * This function maps descriptor rings to the queue-specific vectors
4572  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4573  * one vector per queue pair, but on a constrained vector budget, we
4574  * group the queue pairs as "efficiently" as possible.
4575  **/
4576 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4577 {
4578 	int qp_remaining = vsi->num_queue_pairs;
4579 	int q_vectors = vsi->num_q_vectors;
4580 	int num_ringpairs;
4581 	int v_start = 0;
4582 	int qp_idx = 0;
4583 
4584 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4585 	 * group them so there are multiple queues per vector.
4586 	 * It is also important to go through all the vectors available to be
4587 	 * sure that if we don't use all the vectors, that the remaining vectors
4588 	 * are cleared. This is especially important when decreasing the
4589 	 * number of queues in use.
4590 	 */
4591 	for (; v_start < q_vectors; v_start++) {
4592 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4593 
4594 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4595 
4596 		q_vector->num_ringpairs = num_ringpairs;
4597 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4598 
4599 		q_vector->rx.count = 0;
4600 		q_vector->tx.count = 0;
4601 		q_vector->rx.ring = NULL;
4602 		q_vector->tx.ring = NULL;
4603 
4604 		while (num_ringpairs--) {
4605 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4606 			qp_idx++;
4607 			qp_remaining--;
4608 		}
4609 	}
4610 }
4611 
4612 /**
4613  * i40e_vsi_request_irq - Request IRQ from the OS
4614  * @vsi: the VSI being configured
4615  * @basename: name for the vector
4616  **/
4617 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4618 {
4619 	struct i40e_pf *pf = vsi->back;
4620 	int err;
4621 
4622 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4623 		err = i40e_vsi_request_irq_msix(vsi, basename);
4624 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4625 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4626 				  pf->int_name, pf);
4627 	else
4628 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4629 				  pf->int_name, pf);
4630 
4631 	if (err)
4632 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4633 
4634 	return err;
4635 }
4636 
4637 #ifdef CONFIG_NET_POLL_CONTROLLER
4638 /**
4639  * i40e_netpoll - A Polling 'interrupt' handler
4640  * @netdev: network interface device structure
4641  *
4642  * This is used by netconsole to send skbs without having to re-enable
4643  * interrupts.  It's not called while the normal interrupt routine is executing.
4644  **/
4645 static void i40e_netpoll(struct net_device *netdev)
4646 {
4647 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4648 	struct i40e_vsi *vsi = np->vsi;
4649 	struct i40e_pf *pf = vsi->back;
4650 	int i;
4651 
4652 	/* if interface is down do nothing */
4653 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4654 		return;
4655 
4656 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4657 		for (i = 0; i < vsi->num_q_vectors; i++)
4658 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4659 	} else {
4660 		i40e_intr(pf->pdev->irq, netdev);
4661 	}
4662 }
4663 #endif
4664 
4665 #define I40E_QTX_ENA_WAIT_COUNT 50
4666 
4667 /**
4668  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4669  * @pf: the PF being configured
4670  * @pf_q: the PF queue
4671  * @enable: enable or disable state of the queue
4672  *
4673  * This routine will wait for the given Tx queue of the PF to reach the
4674  * enabled or disabled state.
4675  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4676  * multiple retries; else will return 0 in case of success.
4677  **/
4678 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4679 {
4680 	int i;
4681 	u32 tx_reg;
4682 
4683 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4684 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4685 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4686 			break;
4687 
4688 		usleep_range(10, 20);
4689 	}
4690 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4691 		return -ETIMEDOUT;
4692 
4693 	return 0;
4694 }
4695 
4696 /**
4697  * i40e_control_tx_q - Start or stop a particular Tx queue
4698  * @pf: the PF structure
4699  * @pf_q: the PF queue to configure
4700  * @enable: start or stop the queue
4701  *
4702  * This function enables or disables a single queue. Note that any delay
4703  * required after the operation is expected to be handled by the caller of
4704  * this function.
4705  **/
4706 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4707 {
4708 	struct i40e_hw *hw = &pf->hw;
4709 	u32 tx_reg;
4710 	int i;
4711 
4712 	/* warn the TX unit of coming changes */
4713 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4714 	if (!enable)
4715 		usleep_range(10, 20);
4716 
4717 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4718 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4719 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4720 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4721 			break;
4722 		usleep_range(1000, 2000);
4723 	}
4724 
4725 	/* Skip if the queue is already in the requested state */
4726 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4727 		return;
4728 
4729 	/* turn on/off the queue */
4730 	if (enable) {
4731 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4732 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4733 	} else {
4734 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4735 	}
4736 
4737 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4738 }
4739 
4740 /**
4741  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4742  * @seid: VSI SEID
4743  * @pf: the PF structure
4744  * @pf_q: the PF queue to configure
4745  * @is_xdp: true if the queue is used for XDP
4746  * @enable: start or stop the queue
4747  **/
4748 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4749 			   bool is_xdp, bool enable)
4750 {
4751 	int ret;
4752 
4753 	i40e_control_tx_q(pf, pf_q, enable);
4754 
4755 	/* wait for the change to finish */
4756 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4757 	if (ret) {
4758 		dev_info(&pf->pdev->dev,
4759 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4760 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4761 			 (enable ? "en" : "dis"));
4762 	}
4763 
4764 	return ret;
4765 }
4766 
4767 /**
4768  * i40e_vsi_enable_tx - Start a VSI's rings
4769  * @vsi: the VSI being configured
4770  **/
4771 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4772 {
4773 	struct i40e_pf *pf = vsi->back;
4774 	int i, pf_q, ret = 0;
4775 
4776 	pf_q = vsi->base_queue;
4777 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4778 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4779 					     pf_q,
4780 					     false /*is xdp*/, true);
4781 		if (ret)
4782 			break;
4783 
4784 		if (!i40e_enabled_xdp_vsi(vsi))
4785 			continue;
4786 
4787 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4788 					     pf_q + vsi->alloc_queue_pairs,
4789 					     true /*is xdp*/, true);
4790 		if (ret)
4791 			break;
4792 	}
4793 	return ret;
4794 }
4795 
4796 /**
4797  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4798  * @pf: the PF being configured
4799  * @pf_q: the PF queue
4800  * @enable: enable or disable state of the queue
4801  *
4802  * This routine will wait for the given Rx queue of the PF to reach the
4803  * enabled or disabled state.
4804  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4805  * multiple retries; else will return 0 in case of success.
4806  **/
4807 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4808 {
4809 	int i;
4810 	u32 rx_reg;
4811 
4812 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4813 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4814 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4815 			break;
4816 
4817 		usleep_range(10, 20);
4818 	}
4819 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4820 		return -ETIMEDOUT;
4821 
4822 	return 0;
4823 }
4824 
4825 /**
4826  * i40e_control_rx_q - Start or stop a particular Rx queue
4827  * @pf: the PF structure
4828  * @pf_q: the PF queue to configure
4829  * @enable: start or stop the queue
4830  *
4831  * This function enables or disables a single queue. Note that
4832  * any delay required after the operation is expected to be
4833  * handled by the caller of this function.
4834  **/
4835 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4836 {
4837 	struct i40e_hw *hw = &pf->hw;
4838 	u32 rx_reg;
4839 	int i;
4840 
4841 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4842 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4843 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4844 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4845 			break;
4846 		usleep_range(1000, 2000);
4847 	}
4848 
4849 	/* Skip if the queue is already in the requested state */
4850 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4851 		return;
4852 
4853 	/* turn on/off the queue */
4854 	if (enable)
4855 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4856 	else
4857 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4858 
4859 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4860 }
4861 
4862 /**
4863  * i40e_control_wait_rx_q
4864  * @pf: the PF structure
4865  * @pf_q: queue being configured
4866  * @enable: start or stop the rings
4867  *
4868  * This function enables or disables a single queue along with waiting
4869  * for the change to finish. The caller of this function should handle
4870  * the delays needed in the case of disabling queues.
4871  **/
4872 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4873 {
4874 	int ret = 0;
4875 
4876 	i40e_control_rx_q(pf, pf_q, enable);
4877 
4878 	/* wait for the change to finish */
4879 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4880 	if (ret)
4881 		return ret;
4882 
4883 	return ret;
4884 }
4885 
4886 /**
4887  * i40e_vsi_enable_rx - Start a VSI's rings
4888  * @vsi: the VSI being configured
4889  **/
4890 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4891 {
4892 	struct i40e_pf *pf = vsi->back;
4893 	int i, pf_q, ret = 0;
4894 
4895 	pf_q = vsi->base_queue;
4896 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4897 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4898 		if (ret) {
4899 			dev_info(&pf->pdev->dev,
4900 				 "VSI seid %d Rx ring %d enable timeout\n",
4901 				 vsi->seid, pf_q);
4902 			break;
4903 		}
4904 	}
4905 
4906 	return ret;
4907 }
4908 
4909 /**
4910  * i40e_vsi_start_rings - Start a VSI's rings
4911  * @vsi: the VSI being configured
4912  **/
4913 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4914 {
4915 	int ret = 0;
4916 
4917 	/* do rx first for enable and last for disable */
4918 	ret = i40e_vsi_enable_rx(vsi);
4919 	if (ret)
4920 		return ret;
4921 	ret = i40e_vsi_enable_tx(vsi);
4922 
4923 	return ret;
4924 }
4925 
4926 #define I40E_DISABLE_TX_GAP_MSEC	50
4927 
4928 /**
4929  * i40e_vsi_stop_rings - Stop a VSI's rings
4930  * @vsi: the VSI being configured
4931  **/
4932 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4933 {
4934 	struct i40e_pf *pf = vsi->back;
4935 	u32 pf_q, tx_q_end, rx_q_end;
4936 
4937 	/* When port TX is suspended, don't wait */
4938 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4939 		return i40e_vsi_stop_rings_no_wait(vsi);
4940 
4941 	tx_q_end = vsi->base_queue +
4942 		vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
4943 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4944 		i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
4945 
4946 	rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
4947 	for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
4948 		i40e_control_rx_q(pf, pf_q, false);
4949 
4950 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4951 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4952 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4953 
4954 	i40e_vsi_wait_queues_disabled(vsi);
4955 }
4956 
4957 /**
4958  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4959  * @vsi: the VSI being shutdown
4960  *
4961  * This function stops all the rings for a VSI but does not delay to verify
4962  * that rings have been disabled. It is expected that the caller is shutting
4963  * down multiple VSIs at once and will delay together for all the VSIs after
4964  * initiating the shutdown. This is particularly useful for shutting down lots
4965  * of VFs together. Otherwise, a large delay can be incurred while configuring
4966  * each VSI in serial.
4967  **/
4968 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4969 {
4970 	struct i40e_pf *pf = vsi->back;
4971 	int i, pf_q;
4972 
4973 	pf_q = vsi->base_queue;
4974 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4975 		i40e_control_tx_q(pf, pf_q, false);
4976 		i40e_control_rx_q(pf, pf_q, false);
4977 	}
4978 }
4979 
4980 /**
4981  * i40e_vsi_free_irq - Free the irq association with the OS
4982  * @vsi: the VSI being configured
4983  **/
4984 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4985 {
4986 	struct i40e_pf *pf = vsi->back;
4987 	struct i40e_hw *hw = &pf->hw;
4988 	int base = vsi->base_vector;
4989 	u32 val, qp;
4990 	int i;
4991 
4992 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4993 		if (!vsi->q_vectors)
4994 			return;
4995 
4996 		if (!vsi->irqs_ready)
4997 			return;
4998 
4999 		vsi->irqs_ready = false;
5000 		for (i = 0; i < vsi->num_q_vectors; i++) {
5001 			int irq_num;
5002 			u16 vector;
5003 
5004 			vector = i + base;
5005 			irq_num = pf->msix_entries[vector].vector;
5006 
5007 			/* free only the irqs that were actually requested */
5008 			if (!vsi->q_vectors[i] ||
5009 			    !vsi->q_vectors[i]->num_ringpairs)
5010 				continue;
5011 
5012 			/* clear the affinity notifier in the IRQ descriptor */
5013 			irq_set_affinity_notifier(irq_num, NULL);
5014 			/* remove our suggested affinity mask for this IRQ */
5015 			irq_update_affinity_hint(irq_num, NULL);
5016 			free_irq(irq_num, vsi->q_vectors[i]);
5017 
5018 			/* Tear down the interrupt queue link list
5019 			 *
5020 			 * We know that they come in pairs and always
5021 			 * the Rx first, then the Tx.  To clear the
5022 			 * link list, stick the EOL value into the
5023 			 * next_q field of the registers.
5024 			 */
5025 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5026 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
5027 				       val);
5028 			val |= I40E_QUEUE_END_OF_LIST
5029 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5030 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5031 
5032 			while (qp != I40E_QUEUE_END_OF_LIST) {
5033 				u32 next;
5034 
5035 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5036 
5037 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5038 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5039 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5040 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5041 
5042 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5043 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5044 
5045 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5046 
5047 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5048 
5049 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5050 						 val);
5051 
5052 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5053 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5054 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5055 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5056 
5057 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5058 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5059 
5060 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5061 				qp = next;
5062 			}
5063 		}
5064 	} else {
5065 		free_irq(pf->pdev->irq, pf);
5066 
5067 		val = rd32(hw, I40E_PFINT_LNKLST0);
5068 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5069 		val |= I40E_QUEUE_END_OF_LIST
5070 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5071 		wr32(hw, I40E_PFINT_LNKLST0, val);
5072 
5073 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5074 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5075 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5076 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5077 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5078 
5079 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5080 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5081 
5082 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5083 
5084 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5085 
5086 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5087 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5088 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5089 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5090 
5091 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5092 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5093 
5094 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5095 	}
5096 }
5097 
5098 /**
5099  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5100  * @vsi: the VSI being configured
5101  * @v_idx: Index of vector to be freed
5102  *
5103  * This function frees the memory allocated to the q_vector.  In addition if
5104  * NAPI is enabled it will delete any references to the NAPI struct prior
5105  * to freeing the q_vector.
5106  **/
5107 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5108 {
5109 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5110 	struct i40e_ring *ring;
5111 
5112 	if (!q_vector)
5113 		return;
5114 
5115 	/* disassociate q_vector from rings */
5116 	i40e_for_each_ring(ring, q_vector->tx)
5117 		ring->q_vector = NULL;
5118 
5119 	i40e_for_each_ring(ring, q_vector->rx)
5120 		ring->q_vector = NULL;
5121 
5122 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5123 	if (vsi->netdev)
5124 		netif_napi_del(&q_vector->napi);
5125 
5126 	vsi->q_vectors[v_idx] = NULL;
5127 
5128 	kfree_rcu(q_vector, rcu);
5129 }
5130 
5131 /**
5132  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5133  * @vsi: the VSI being un-configured
5134  *
5135  * This frees the memory allocated to the q_vectors and
5136  * deletes references to the NAPI struct.
5137  **/
5138 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5139 {
5140 	int v_idx;
5141 
5142 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5143 		i40e_free_q_vector(vsi, v_idx);
5144 }
5145 
5146 /**
5147  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5148  * @pf: board private structure
5149  **/
5150 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5151 {
5152 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5153 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5154 		pci_disable_msix(pf->pdev);
5155 		kfree(pf->msix_entries);
5156 		pf->msix_entries = NULL;
5157 		kfree(pf->irq_pile);
5158 		pf->irq_pile = NULL;
5159 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5160 		pci_disable_msi(pf->pdev);
5161 	}
5162 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5163 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5164 }
5165 
5166 /**
5167  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5168  * @pf: board private structure
5169  *
5170  * We go through and clear interrupt specific resources and reset the structure
5171  * to pre-load conditions
5172  **/
5173 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5174 {
5175 	struct i40e_vsi *vsi;
5176 	int i;
5177 
5178 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5179 		i40e_free_misc_vector(pf);
5180 
5181 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5182 		      I40E_IWARP_IRQ_PILE_ID);
5183 
5184 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5185 
5186 	i40e_pf_for_each_vsi(pf, i, vsi)
5187 		i40e_vsi_free_q_vectors(vsi);
5188 
5189 	i40e_reset_interrupt_capability(pf);
5190 }
5191 
5192 /**
5193  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5194  * @vsi: the VSI being configured
5195  **/
5196 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5197 {
5198 	int q_idx;
5199 
5200 	if (!vsi->netdev)
5201 		return;
5202 
5203 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5204 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5205 
5206 		if (q_vector->rx.ring || q_vector->tx.ring)
5207 			napi_enable(&q_vector->napi);
5208 	}
5209 }
5210 
5211 /**
5212  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5213  * @vsi: the VSI being configured
5214  **/
5215 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5216 {
5217 	int q_idx;
5218 
5219 	if (!vsi->netdev)
5220 		return;
5221 
5222 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5223 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5224 
5225 		if (q_vector->rx.ring || q_vector->tx.ring)
5226 			napi_disable(&q_vector->napi);
5227 	}
5228 }
5229 
5230 /**
5231  * i40e_vsi_close - Shut down a VSI
5232  * @vsi: the vsi to be quelled
5233  **/
5234 static void i40e_vsi_close(struct i40e_vsi *vsi)
5235 {
5236 	struct i40e_pf *pf = vsi->back;
5237 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5238 		i40e_down(vsi);
5239 	i40e_vsi_free_irq(vsi);
5240 	i40e_vsi_free_tx_resources(vsi);
5241 	i40e_vsi_free_rx_resources(vsi);
5242 	vsi->current_netdev_flags = 0;
5243 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5244 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5245 		set_bit(__I40E_CLIENT_RESET, pf->state);
5246 }
5247 
5248 /**
5249  * i40e_quiesce_vsi - Pause a given VSI
5250  * @vsi: the VSI being paused
5251  **/
5252 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5253 {
5254 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5255 		return;
5256 
5257 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5258 	if (vsi->netdev && netif_running(vsi->netdev))
5259 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5260 	else
5261 		i40e_vsi_close(vsi);
5262 }
5263 
5264 /**
5265  * i40e_unquiesce_vsi - Resume a given VSI
5266  * @vsi: the VSI being resumed
5267  **/
5268 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5269 {
5270 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5271 		return;
5272 
5273 	if (vsi->netdev && netif_running(vsi->netdev))
5274 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5275 	else
5276 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5277 }
5278 
5279 /**
5280  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5281  * @pf: the PF
5282  **/
5283 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5284 {
5285 	struct i40e_vsi *vsi;
5286 	int v;
5287 
5288 	i40e_pf_for_each_vsi(pf, v, vsi)
5289 		i40e_quiesce_vsi(vsi);
5290 }
5291 
5292 /**
5293  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5294  * @pf: the PF
5295  **/
5296 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5297 {
5298 	struct i40e_vsi *vsi;
5299 	int v;
5300 
5301 	i40e_pf_for_each_vsi(pf, v, vsi)
5302 		i40e_unquiesce_vsi(vsi);
5303 }
5304 
5305 /**
5306  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5307  * @vsi: the VSI being configured
5308  *
5309  * Wait until all queues on a given VSI have been disabled.
5310  **/
5311 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5312 {
5313 	struct i40e_pf *pf = vsi->back;
5314 	int i, pf_q, ret;
5315 
5316 	pf_q = vsi->base_queue;
5317 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5318 		/* Check and wait for the Tx queue */
5319 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5320 		if (ret) {
5321 			dev_info(&pf->pdev->dev,
5322 				 "VSI seid %d Tx ring %d disable timeout\n",
5323 				 vsi->seid, pf_q);
5324 			return ret;
5325 		}
5326 
5327 		if (!i40e_enabled_xdp_vsi(vsi))
5328 			goto wait_rx;
5329 
5330 		/* Check and wait for the XDP Tx queue */
5331 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5332 				       false);
5333 		if (ret) {
5334 			dev_info(&pf->pdev->dev,
5335 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5336 				 vsi->seid, pf_q);
5337 			return ret;
5338 		}
5339 wait_rx:
5340 		/* Check and wait for the Rx queue */
5341 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5342 		if (ret) {
5343 			dev_info(&pf->pdev->dev,
5344 				 "VSI seid %d Rx ring %d disable timeout\n",
5345 				 vsi->seid, pf_q);
5346 			return ret;
5347 		}
5348 	}
5349 
5350 	return 0;
5351 }
5352 
5353 #ifdef CONFIG_I40E_DCB
5354 /**
5355  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5356  * @pf: the PF
5357  *
5358  * This function waits for the queues to be in disabled state for all the
5359  * VSIs that are managed by this PF.
5360  **/
5361 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5362 {
5363 	struct i40e_vsi *vsi;
5364 	int v, ret = 0;
5365 
5366 	i40e_pf_for_each_vsi(pf, v, vsi) {
5367 		ret = i40e_vsi_wait_queues_disabled(vsi);
5368 		if (ret)
5369 			break;
5370 	}
5371 
5372 	return ret;
5373 }
5374 
5375 #endif
5376 
5377 /**
5378  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5379  * @pf: pointer to PF
5380  *
5381  * Get TC map for ISCSI PF type that will include iSCSI TC
5382  * and LAN TC.
5383  **/
5384 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5385 {
5386 	struct i40e_dcb_app_priority_table app;
5387 	struct i40e_hw *hw = &pf->hw;
5388 	u8 enabled_tc = 1; /* TC0 is always enabled */
5389 	u8 tc, i;
5390 	/* Get the iSCSI APP TLV */
5391 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5392 
5393 	for (i = 0; i < dcbcfg->numapps; i++) {
5394 		app = dcbcfg->app[i];
5395 		if (app.selector == I40E_APP_SEL_TCPIP &&
5396 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5397 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5398 			enabled_tc |= BIT(tc);
5399 			break;
5400 		}
5401 	}
5402 
5403 	return enabled_tc;
5404 }
5405 
5406 /**
5407  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5408  * @dcbcfg: the corresponding DCBx configuration structure
5409  *
5410  * Return the number of TCs from given DCBx configuration
5411  **/
5412 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5413 {
5414 	int i, tc_unused = 0;
5415 	u8 num_tc = 0;
5416 	u8 ret = 0;
5417 
5418 	/* Scan the ETS Config Priority Table to find
5419 	 * traffic class enabled for a given priority
5420 	 * and create a bitmask of enabled TCs
5421 	 */
5422 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5423 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5424 
5425 	/* Now scan the bitmask to check for
5426 	 * contiguous TCs starting with TC0
5427 	 */
5428 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5429 		if (num_tc & BIT(i)) {
5430 			if (!tc_unused) {
5431 				ret++;
5432 			} else {
5433 				pr_err("Non-contiguous TC - Disabling DCB\n");
5434 				return 1;
5435 			}
5436 		} else {
5437 			tc_unused = 1;
5438 		}
5439 	}
5440 
5441 	/* There is always at least TC0 */
5442 	if (!ret)
5443 		ret = 1;
5444 
5445 	return ret;
5446 }
5447 
5448 /**
5449  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5450  * @dcbcfg: the corresponding DCBx configuration structure
5451  *
5452  * Query the current DCB configuration and return the number of
5453  * traffic classes enabled from the given DCBX config
5454  **/
5455 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5456 {
5457 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5458 	u8 enabled_tc = 1;
5459 	u8 i;
5460 
5461 	for (i = 0; i < num_tc; i++)
5462 		enabled_tc |= BIT(i);
5463 
5464 	return enabled_tc;
5465 }
5466 
5467 /**
5468  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5469  * @pf: PF being queried
5470  *
5471  * Query the current MQPRIO configuration and return the number of
5472  * traffic classes enabled.
5473  **/
5474 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5475 {
5476 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5477 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5478 	u8 enabled_tc = 1, i;
5479 
5480 	for (i = 1; i < num_tc; i++)
5481 		enabled_tc |= BIT(i);
5482 	return enabled_tc;
5483 }
5484 
5485 /**
5486  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5487  * @pf: PF being queried
5488  *
5489  * Return number of traffic classes enabled for the given PF
5490  **/
5491 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5492 {
5493 	u8 i, enabled_tc = 1;
5494 	u8 num_tc = 0;
5495 
5496 	if (i40e_is_tc_mqprio_enabled(pf)) {
5497 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5498 
5499 		return vsi->mqprio_qopt.qopt.num_tc;
5500 	}
5501 
5502 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5503 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5504 		return 1;
5505 
5506 	/* SFP mode will be enabled for all TCs on port */
5507 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5508 		return i40e_dcb_get_num_tc(&pf->hw.local_dcbx_config);
5509 
5510 	/* MFP mode return count of enabled TCs for this PF */
5511 	if (pf->hw.func_caps.iscsi)
5512 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5513 	else
5514 		return 1; /* Only TC0 */
5515 
5516 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5517 		if (enabled_tc & BIT(i))
5518 			num_tc++;
5519 	}
5520 	return num_tc;
5521 }
5522 
5523 /**
5524  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5525  * @pf: PF being queried
5526  *
5527  * Return a bitmap for enabled traffic classes for this PF.
5528  **/
5529 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5530 {
5531 	if (i40e_is_tc_mqprio_enabled(pf))
5532 		return i40e_mqprio_get_enabled_tc(pf);
5533 
5534 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5535 	 * default TC
5536 	 */
5537 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5538 		return I40E_DEFAULT_TRAFFIC_CLASS;
5539 
5540 	/* SFP mode we want PF to be enabled for all TCs */
5541 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5542 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5543 
5544 	/* MFP enabled and iSCSI PF type */
5545 	if (pf->hw.func_caps.iscsi)
5546 		return i40e_get_iscsi_tc_map(pf);
5547 	else
5548 		return I40E_DEFAULT_TRAFFIC_CLASS;
5549 }
5550 
5551 /**
5552  * i40e_vsi_get_bw_info - Query VSI BW Information
5553  * @vsi: the VSI being queried
5554  *
5555  * Returns 0 on success, negative value on failure
5556  **/
5557 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5558 {
5559 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5560 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5561 	struct i40e_pf *pf = vsi->back;
5562 	struct i40e_hw *hw = &pf->hw;
5563 	u32 tc_bw_max;
5564 	int ret;
5565 	int i;
5566 
5567 	/* Get the VSI level BW configuration */
5568 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5569 	if (ret) {
5570 		dev_info(&pf->pdev->dev,
5571 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5572 			 ERR_PTR(ret),
5573 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5574 		return -EINVAL;
5575 	}
5576 
5577 	/* Get the VSI level BW configuration per TC */
5578 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5579 					       NULL);
5580 	if (ret) {
5581 		dev_info(&pf->pdev->dev,
5582 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5583 			 ERR_PTR(ret),
5584 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5585 		return -EINVAL;
5586 	}
5587 
5588 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5589 		dev_info(&pf->pdev->dev,
5590 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5591 			 bw_config.tc_valid_bits,
5592 			 bw_ets_config.tc_valid_bits);
5593 		/* Still continuing */
5594 	}
5595 
5596 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5597 	vsi->bw_max_quanta = bw_config.max_bw;
5598 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5599 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5600 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5601 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5602 		vsi->bw_ets_limit_credits[i] =
5603 					le16_to_cpu(bw_ets_config.credits[i]);
5604 		/* 3 bits out of 4 for each TC */
5605 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5606 	}
5607 
5608 	return 0;
5609 }
5610 
5611 /**
5612  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5613  * @vsi: the VSI being configured
5614  * @enabled_tc: TC bitmap
5615  * @bw_share: BW shared credits per TC
5616  *
5617  * Returns 0 on success, negative value on failure
5618  **/
5619 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5620 				       u8 *bw_share)
5621 {
5622 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5623 	struct i40e_pf *pf = vsi->back;
5624 	int ret;
5625 	int i;
5626 
5627 	/* There is no need to reset BW when mqprio mode is on.  */
5628 	if (i40e_is_tc_mqprio_enabled(pf))
5629 		return 0;
5630 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5631 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5632 		if (ret)
5633 			dev_info(&pf->pdev->dev,
5634 				 "Failed to reset tx rate for vsi->seid %u\n",
5635 				 vsi->seid);
5636 		return ret;
5637 	}
5638 	memset(&bw_data, 0, sizeof(bw_data));
5639 	bw_data.tc_valid_bits = enabled_tc;
5640 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5641 		bw_data.tc_bw_credits[i] = bw_share[i];
5642 
5643 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5644 	if (ret) {
5645 		dev_info(&pf->pdev->dev,
5646 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5647 			 pf->hw.aq.asq_last_status);
5648 		return -EINVAL;
5649 	}
5650 
5651 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5652 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5653 
5654 	return 0;
5655 }
5656 
5657 /**
5658  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5659  * @vsi: the VSI being configured
5660  * @enabled_tc: TC map to be enabled
5661  *
5662  **/
5663 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5664 {
5665 	struct net_device *netdev = vsi->netdev;
5666 	struct i40e_pf *pf = vsi->back;
5667 	struct i40e_hw *hw = &pf->hw;
5668 	u8 netdev_tc = 0;
5669 	int i;
5670 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5671 
5672 	if (!netdev)
5673 		return;
5674 
5675 	if (!enabled_tc) {
5676 		netdev_reset_tc(netdev);
5677 		return;
5678 	}
5679 
5680 	/* Set up actual enabled TCs on the VSI */
5681 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5682 		return;
5683 
5684 	/* set per TC queues for the VSI */
5685 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5686 		/* Only set TC queues for enabled tcs
5687 		 *
5688 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5689 		 * enabled_tc bitmap would be 0x00001001; the driver
5690 		 * will set the numtc for netdev as 2 that will be
5691 		 * referenced by the netdev layer as TC 0 and 1.
5692 		 */
5693 		if (vsi->tc_config.enabled_tc & BIT(i))
5694 			netdev_set_tc_queue(netdev,
5695 					vsi->tc_config.tc_info[i].netdev_tc,
5696 					vsi->tc_config.tc_info[i].qcount,
5697 					vsi->tc_config.tc_info[i].qoffset);
5698 	}
5699 
5700 	if (i40e_is_tc_mqprio_enabled(pf))
5701 		return;
5702 
5703 	/* Assign UP2TC map for the VSI */
5704 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5705 		/* Get the actual TC# for the UP */
5706 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5707 		/* Get the mapped netdev TC# for the UP */
5708 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5709 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5710 	}
5711 }
5712 
5713 /**
5714  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5715  * @vsi: the VSI being configured
5716  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5717  **/
5718 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5719 				      struct i40e_vsi_context *ctxt)
5720 {
5721 	/* copy just the sections touched not the entire info
5722 	 * since not all sections are valid as returned by
5723 	 * update vsi params
5724 	 */
5725 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5726 	memcpy(&vsi->info.queue_mapping,
5727 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5728 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5729 	       sizeof(vsi->info.tc_mapping));
5730 }
5731 
5732 /**
5733  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5734  * @vsi: the VSI being reconfigured
5735  * @vsi_offset: offset from main VF VSI
5736  */
5737 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5738 {
5739 	struct i40e_vsi_context ctxt = {};
5740 	struct i40e_pf *pf;
5741 	struct i40e_hw *hw;
5742 	int ret;
5743 
5744 	if (!vsi)
5745 		return -EINVAL;
5746 	pf = vsi->back;
5747 	hw = &pf->hw;
5748 
5749 	ctxt.seid = vsi->seid;
5750 	ctxt.pf_num = hw->pf_id;
5751 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5752 	ctxt.uplink_seid = vsi->uplink_seid;
5753 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5754 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5755 	ctxt.info = vsi->info;
5756 
5757 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5758 				 false);
5759 	if (vsi->reconfig_rss) {
5760 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5761 				      vsi->num_queue_pairs);
5762 		ret = i40e_vsi_config_rss(vsi);
5763 		if (ret) {
5764 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5765 			return ret;
5766 		}
5767 		vsi->reconfig_rss = false;
5768 	}
5769 
5770 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5771 	if (ret) {
5772 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5773 			 ERR_PTR(ret),
5774 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5775 		return ret;
5776 	}
5777 	/* update the local VSI info with updated queue map */
5778 	i40e_vsi_update_queue_map(vsi, &ctxt);
5779 	vsi->info.valid_sections = 0;
5780 
5781 	return ret;
5782 }
5783 
5784 /**
5785  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5786  * @vsi: VSI to be configured
5787  * @enabled_tc: TC bitmap
5788  *
5789  * This configures a particular VSI for TCs that are mapped to the
5790  * given TC bitmap. It uses default bandwidth share for TCs across
5791  * VSIs to configure TC for a particular VSI.
5792  *
5793  * NOTE:
5794  * It is expected that the VSI queues have been quisced before calling
5795  * this function.
5796  **/
5797 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5798 {
5799 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5800 	struct i40e_pf *pf = vsi->back;
5801 	struct i40e_hw *hw = &pf->hw;
5802 	struct i40e_vsi_context ctxt;
5803 	int ret = 0;
5804 	int i;
5805 
5806 	/* Check if enabled_tc is same as existing or new TCs */
5807 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5808 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5809 		return ret;
5810 
5811 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5812 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5813 		if (enabled_tc & BIT(i))
5814 			bw_share[i] = 1;
5815 	}
5816 
5817 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5818 	if (ret) {
5819 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5820 
5821 		dev_info(&pf->pdev->dev,
5822 			 "Failed configuring TC map %d for VSI %d\n",
5823 			 enabled_tc, vsi->seid);
5824 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5825 						  &bw_config, NULL);
5826 		if (ret) {
5827 			dev_info(&pf->pdev->dev,
5828 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5829 				 ERR_PTR(ret),
5830 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5831 			goto out;
5832 		}
5833 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5834 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5835 
5836 			if (!valid_tc)
5837 				valid_tc = bw_config.tc_valid_bits;
5838 			/* Always enable TC0, no matter what */
5839 			valid_tc |= 1;
5840 			dev_info(&pf->pdev->dev,
5841 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5842 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5843 			enabled_tc = valid_tc;
5844 		}
5845 
5846 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5847 		if (ret) {
5848 			dev_err(&pf->pdev->dev,
5849 				"Unable to  configure TC map %d for VSI %d\n",
5850 				enabled_tc, vsi->seid);
5851 			goto out;
5852 		}
5853 	}
5854 
5855 	/* Update Queue Pairs Mapping for currently enabled UPs */
5856 	ctxt.seid = vsi->seid;
5857 	ctxt.pf_num = vsi->back->hw.pf_id;
5858 	ctxt.vf_num = 0;
5859 	ctxt.uplink_seid = vsi->uplink_seid;
5860 	ctxt.info = vsi->info;
5861 	if (i40e_is_tc_mqprio_enabled(pf)) {
5862 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5863 		if (ret)
5864 			goto out;
5865 	} else {
5866 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5867 	}
5868 
5869 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5870 	 * queues changed.
5871 	 */
5872 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5873 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5874 				      vsi->num_queue_pairs);
5875 		ret = i40e_vsi_config_rss(vsi);
5876 		if (ret) {
5877 			dev_info(&vsi->back->pdev->dev,
5878 				 "Failed to reconfig rss for num_queues\n");
5879 			return ret;
5880 		}
5881 		vsi->reconfig_rss = false;
5882 	}
5883 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5884 		ctxt.info.valid_sections |=
5885 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5886 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5887 	}
5888 
5889 	/* Update the VSI after updating the VSI queue-mapping
5890 	 * information
5891 	 */
5892 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5893 	if (ret) {
5894 		dev_info(&pf->pdev->dev,
5895 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5896 			 ERR_PTR(ret),
5897 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5898 		goto out;
5899 	}
5900 	/* update the local VSI info with updated queue map */
5901 	i40e_vsi_update_queue_map(vsi, &ctxt);
5902 	vsi->info.valid_sections = 0;
5903 
5904 	/* Update current VSI BW information */
5905 	ret = i40e_vsi_get_bw_info(vsi);
5906 	if (ret) {
5907 		dev_info(&pf->pdev->dev,
5908 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5909 			 ERR_PTR(ret),
5910 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5911 		goto out;
5912 	}
5913 
5914 	/* Update the netdev TC setup */
5915 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5916 out:
5917 	return ret;
5918 }
5919 
5920 /**
5921  * i40e_vsi_reconfig_tc - Reconfigure VSI Tx Scheduler for stored TC map
5922  * @vsi: VSI to be reconfigured
5923  *
5924  * This reconfigures a particular VSI for TCs that are mapped to the
5925  * TC bitmap stored previously for the VSI.
5926  *
5927  * Context: It is expected that the VSI queues have been quisced before
5928  *          calling this function.
5929  *
5930  * Return: 0 on success, negative value on failure
5931  **/
5932 static int i40e_vsi_reconfig_tc(struct i40e_vsi *vsi)
5933 {
5934 	u8 enabled_tc;
5935 
5936 	enabled_tc = vsi->tc_config.enabled_tc;
5937 	vsi->tc_config.enabled_tc = 0;
5938 
5939 	return i40e_vsi_config_tc(vsi, enabled_tc);
5940 }
5941 
5942 /**
5943  * i40e_get_link_speed - Returns link speed for the interface
5944  * @vsi: VSI to be configured
5945  *
5946  **/
5947 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5948 {
5949 	struct i40e_pf *pf = vsi->back;
5950 
5951 	switch (pf->hw.phy.link_info.link_speed) {
5952 	case I40E_LINK_SPEED_40GB:
5953 		return 40000;
5954 	case I40E_LINK_SPEED_25GB:
5955 		return 25000;
5956 	case I40E_LINK_SPEED_20GB:
5957 		return 20000;
5958 	case I40E_LINK_SPEED_10GB:
5959 		return 10000;
5960 	case I40E_LINK_SPEED_1GB:
5961 		return 1000;
5962 	default:
5963 		return -EINVAL;
5964 	}
5965 }
5966 
5967 /**
5968  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5969  * @vsi: Pointer to vsi structure
5970  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5971  *
5972  * Helper function to convert units before send to set BW limit
5973  **/
5974 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5975 {
5976 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5977 		dev_warn(&vsi->back->pdev->dev,
5978 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5979 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5980 	} else {
5981 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5982 	}
5983 
5984 	return max_tx_rate;
5985 }
5986 
5987 /**
5988  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5989  * @vsi: VSI to be configured
5990  * @seid: seid of the channel/VSI
5991  * @max_tx_rate: max TX rate to be configured as BW limit
5992  *
5993  * Helper function to set BW limit for a given VSI
5994  **/
5995 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5996 {
5997 	struct i40e_pf *pf = vsi->back;
5998 	u64 credits = 0;
5999 	int speed = 0;
6000 	int ret = 0;
6001 
6002 	speed = i40e_get_link_speed(vsi);
6003 	if (max_tx_rate > speed) {
6004 		dev_err(&pf->pdev->dev,
6005 			"Invalid max tx rate %llu specified for VSI seid %d.",
6006 			max_tx_rate, seid);
6007 		return -EINVAL;
6008 	}
6009 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
6010 		dev_warn(&pf->pdev->dev,
6011 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
6012 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
6013 	}
6014 
6015 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
6016 	credits = max_tx_rate;
6017 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
6018 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
6019 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
6020 	if (ret)
6021 		dev_err(&pf->pdev->dev,
6022 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
6023 			max_tx_rate, seid, ERR_PTR(ret),
6024 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6025 	return ret;
6026 }
6027 
6028 /**
6029  * i40e_remove_queue_channels - Remove queue channels for the TCs
6030  * @vsi: VSI to be configured
6031  *
6032  * Remove queue channels for the TCs
6033  **/
6034 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6035 {
6036 	enum i40e_admin_queue_err last_aq_status;
6037 	struct i40e_cloud_filter *cfilter;
6038 	struct i40e_channel *ch, *ch_tmp;
6039 	struct i40e_pf *pf = vsi->back;
6040 	struct hlist_node *node;
6041 	int ret, i;
6042 
6043 	/* Reset rss size that was stored when reconfiguring rss for
6044 	 * channel VSIs with non-power-of-2 queue count.
6045 	 */
6046 	vsi->current_rss_size = 0;
6047 
6048 	/* perform cleanup for channels if they exist */
6049 	if (list_empty(&vsi->ch_list))
6050 		return;
6051 
6052 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6053 		struct i40e_vsi *p_vsi;
6054 
6055 		list_del(&ch->list);
6056 		p_vsi = ch->parent_vsi;
6057 		if (!p_vsi || !ch->initialized) {
6058 			kfree(ch);
6059 			continue;
6060 		}
6061 		/* Reset queue contexts */
6062 		for (i = 0; i < ch->num_queue_pairs; i++) {
6063 			struct i40e_ring *tx_ring, *rx_ring;
6064 			u16 pf_q;
6065 
6066 			pf_q = ch->base_queue + i;
6067 			tx_ring = vsi->tx_rings[pf_q];
6068 			tx_ring->ch = NULL;
6069 
6070 			rx_ring = vsi->rx_rings[pf_q];
6071 			rx_ring->ch = NULL;
6072 		}
6073 
6074 		/* Reset BW configured for this VSI via mqprio */
6075 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6076 		if (ret)
6077 			dev_info(&vsi->back->pdev->dev,
6078 				 "Failed to reset tx rate for ch->seid %u\n",
6079 				 ch->seid);
6080 
6081 		/* delete cloud filters associated with this channel */
6082 		hlist_for_each_entry_safe(cfilter, node,
6083 					  &pf->cloud_filter_list, cloud_node) {
6084 			if (cfilter->seid != ch->seid)
6085 				continue;
6086 
6087 			hash_del(&cfilter->cloud_node);
6088 			if (cfilter->dst_port)
6089 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6090 									cfilter,
6091 									false);
6092 			else
6093 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6094 								false);
6095 			last_aq_status = pf->hw.aq.asq_last_status;
6096 			if (ret)
6097 				dev_info(&pf->pdev->dev,
6098 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6099 					 ERR_PTR(ret),
6100 					 i40e_aq_str(&pf->hw, last_aq_status));
6101 			kfree(cfilter);
6102 		}
6103 
6104 		/* delete VSI from FW */
6105 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6106 					     NULL);
6107 		if (ret)
6108 			dev_err(&vsi->back->pdev->dev,
6109 				"unable to remove channel (%d) for parent VSI(%d)\n",
6110 				ch->seid, p_vsi->seid);
6111 		kfree(ch);
6112 	}
6113 	INIT_LIST_HEAD(&vsi->ch_list);
6114 }
6115 
6116 /**
6117  * i40e_get_max_queues_for_channel
6118  * @vsi: ptr to VSI to which channels are associated with
6119  *
6120  * Helper function which returns max value among the queue counts set on the
6121  * channels/TCs created.
6122  **/
6123 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6124 {
6125 	struct i40e_channel *ch, *ch_tmp;
6126 	int max = 0;
6127 
6128 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6129 		if (!ch->initialized)
6130 			continue;
6131 		if (ch->num_queue_pairs > max)
6132 			max = ch->num_queue_pairs;
6133 	}
6134 
6135 	return max;
6136 }
6137 
6138 /**
6139  * i40e_validate_num_queues - validate num_queues w.r.t channel
6140  * @pf: ptr to PF device
6141  * @num_queues: number of queues
6142  * @vsi: the parent VSI
6143  * @reconfig_rss: indicates should the RSS be reconfigured or not
6144  *
6145  * This function validates number of queues in the context of new channel
6146  * which is being established and determines if RSS should be reconfigured
6147  * or not for parent VSI.
6148  **/
6149 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6150 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6151 {
6152 	int max_ch_queues;
6153 
6154 	if (!reconfig_rss)
6155 		return -EINVAL;
6156 
6157 	*reconfig_rss = false;
6158 	if (vsi->current_rss_size) {
6159 		if (num_queues > vsi->current_rss_size) {
6160 			dev_dbg(&pf->pdev->dev,
6161 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6162 				num_queues, vsi->current_rss_size);
6163 			return -EINVAL;
6164 		} else if ((num_queues < vsi->current_rss_size) &&
6165 			   (!is_power_of_2(num_queues))) {
6166 			dev_dbg(&pf->pdev->dev,
6167 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6168 				num_queues, vsi->current_rss_size);
6169 			return -EINVAL;
6170 		}
6171 	}
6172 
6173 	if (!is_power_of_2(num_queues)) {
6174 		/* Find the max num_queues configured for channel if channel
6175 		 * exist.
6176 		 * if channel exist, then enforce 'num_queues' to be more than
6177 		 * max ever queues configured for channel.
6178 		 */
6179 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6180 		if (num_queues < max_ch_queues) {
6181 			dev_dbg(&pf->pdev->dev,
6182 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6183 				num_queues, max_ch_queues);
6184 			return -EINVAL;
6185 		}
6186 		*reconfig_rss = true;
6187 	}
6188 
6189 	return 0;
6190 }
6191 
6192 /**
6193  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6194  * @vsi: the VSI being setup
6195  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6196  *
6197  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6198  **/
6199 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6200 {
6201 	struct i40e_pf *pf = vsi->back;
6202 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6203 	struct i40e_hw *hw = &pf->hw;
6204 	int local_rss_size;
6205 	u8 *lut;
6206 	int ret;
6207 
6208 	if (!vsi->rss_size)
6209 		return -EINVAL;
6210 
6211 	if (rss_size > vsi->rss_size)
6212 		return -EINVAL;
6213 
6214 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6215 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6216 	if (!lut)
6217 		return -ENOMEM;
6218 
6219 	/* Ignoring user configured lut if there is one */
6220 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6221 
6222 	/* Use user configured hash key if there is one, otherwise
6223 	 * use default.
6224 	 */
6225 	if (vsi->rss_hkey_user)
6226 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6227 	else
6228 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6229 
6230 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6231 	if (ret) {
6232 		dev_info(&pf->pdev->dev,
6233 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6234 			 ERR_PTR(ret),
6235 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6236 		kfree(lut);
6237 		return ret;
6238 	}
6239 	kfree(lut);
6240 
6241 	/* Do the update w.r.t. storing rss_size */
6242 	if (!vsi->orig_rss_size)
6243 		vsi->orig_rss_size = vsi->rss_size;
6244 	vsi->current_rss_size = local_rss_size;
6245 
6246 	return ret;
6247 }
6248 
6249 /**
6250  * i40e_channel_setup_queue_map - Setup a channel queue map
6251  * @pf: ptr to PF device
6252  * @ctxt: VSI context structure
6253  * @ch: ptr to channel structure
6254  *
6255  * Setup queue map for a specific channel
6256  **/
6257 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6258 					 struct i40e_vsi_context *ctxt,
6259 					 struct i40e_channel *ch)
6260 {
6261 	u16 qcount, qmap, sections = 0;
6262 	u8 offset = 0;
6263 	int pow;
6264 
6265 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6266 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6267 
6268 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6269 	ch->num_queue_pairs = qcount;
6270 
6271 	/* find the next higher power-of-2 of num queue pairs */
6272 	pow = ilog2(qcount);
6273 	if (!is_power_of_2(qcount))
6274 		pow++;
6275 
6276 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6277 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6278 
6279 	/* Setup queue TC[0].qmap for given VSI context */
6280 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6281 
6282 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6283 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6284 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6285 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6286 }
6287 
6288 /**
6289  * i40e_add_channel - add a channel by adding VSI
6290  * @pf: ptr to PF device
6291  * @uplink_seid: underlying HW switching element (VEB) ID
6292  * @ch: ptr to channel structure
6293  *
6294  * Add a channel (VSI) using add_vsi and queue_map
6295  **/
6296 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6297 			    struct i40e_channel *ch)
6298 {
6299 	struct i40e_hw *hw = &pf->hw;
6300 	struct i40e_vsi_context ctxt;
6301 	u8 enabled_tc = 0x1; /* TC0 enabled */
6302 	int ret;
6303 
6304 	if (ch->type != I40E_VSI_VMDQ2) {
6305 		dev_info(&pf->pdev->dev,
6306 			 "add new vsi failed, ch->type %d\n", ch->type);
6307 		return -EINVAL;
6308 	}
6309 
6310 	memset(&ctxt, 0, sizeof(ctxt));
6311 	ctxt.pf_num = hw->pf_id;
6312 	ctxt.vf_num = 0;
6313 	ctxt.uplink_seid = uplink_seid;
6314 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6315 	if (ch->type == I40E_VSI_VMDQ2)
6316 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6317 
6318 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6319 		ctxt.info.valid_sections |=
6320 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6321 		ctxt.info.switch_id =
6322 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6323 	}
6324 
6325 	/* Set queue map for a given VSI context */
6326 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6327 
6328 	/* Now time to create VSI */
6329 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6330 	if (ret) {
6331 		dev_info(&pf->pdev->dev,
6332 			 "add new vsi failed, err %pe aq_err %s\n",
6333 			 ERR_PTR(ret),
6334 			 i40e_aq_str(&pf->hw,
6335 				     pf->hw.aq.asq_last_status));
6336 		return -ENOENT;
6337 	}
6338 
6339 	/* Success, update channel, set enabled_tc only if the channel
6340 	 * is not a macvlan
6341 	 */
6342 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6343 	ch->seid = ctxt.seid;
6344 	ch->vsi_number = ctxt.vsi_number;
6345 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6346 
6347 	/* copy just the sections touched not the entire info
6348 	 * since not all sections are valid as returned by
6349 	 * update vsi params
6350 	 */
6351 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6352 	memcpy(&ch->info.queue_mapping,
6353 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6354 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6355 	       sizeof(ctxt.info.tc_mapping));
6356 
6357 	return 0;
6358 }
6359 
6360 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6361 				  u8 *bw_share)
6362 {
6363 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6364 	int ret;
6365 	int i;
6366 
6367 	memset(&bw_data, 0, sizeof(bw_data));
6368 	bw_data.tc_valid_bits = ch->enabled_tc;
6369 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6370 		bw_data.tc_bw_credits[i] = bw_share[i];
6371 
6372 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6373 				       &bw_data, NULL);
6374 	if (ret) {
6375 		dev_info(&vsi->back->pdev->dev,
6376 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6377 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6378 		return -EINVAL;
6379 	}
6380 
6381 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6382 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6383 
6384 	return 0;
6385 }
6386 
6387 /**
6388  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6389  * @pf: ptr to PF device
6390  * @vsi: the VSI being setup
6391  * @ch: ptr to channel structure
6392  *
6393  * Configure TX rings associated with channel (VSI) since queues are being
6394  * from parent VSI.
6395  **/
6396 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6397 				       struct i40e_vsi *vsi,
6398 				       struct i40e_channel *ch)
6399 {
6400 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6401 	int ret;
6402 	int i;
6403 
6404 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6405 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6406 		if (ch->enabled_tc & BIT(i))
6407 			bw_share[i] = 1;
6408 	}
6409 
6410 	/* configure BW for new VSI */
6411 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6412 	if (ret) {
6413 		dev_info(&vsi->back->pdev->dev,
6414 			 "Failed configuring TC map %d for channel (seid %u)\n",
6415 			 ch->enabled_tc, ch->seid);
6416 		return ret;
6417 	}
6418 
6419 	for (i = 0; i < ch->num_queue_pairs; i++) {
6420 		struct i40e_ring *tx_ring, *rx_ring;
6421 		u16 pf_q;
6422 
6423 		pf_q = ch->base_queue + i;
6424 
6425 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6426 		 * context
6427 		 */
6428 		tx_ring = vsi->tx_rings[pf_q];
6429 		tx_ring->ch = ch;
6430 
6431 		/* Get the RX ring ptr */
6432 		rx_ring = vsi->rx_rings[pf_q];
6433 		rx_ring->ch = ch;
6434 	}
6435 
6436 	return 0;
6437 }
6438 
6439 /**
6440  * i40e_setup_hw_channel - setup new channel
6441  * @pf: ptr to PF device
6442  * @vsi: the VSI being setup
6443  * @ch: ptr to channel structure
6444  * @uplink_seid: underlying HW switching element (VEB) ID
6445  * @type: type of channel to be created (VMDq2/VF)
6446  *
6447  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6448  * and configures TX rings accordingly
6449  **/
6450 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6451 					struct i40e_vsi *vsi,
6452 					struct i40e_channel *ch,
6453 					u16 uplink_seid, u8 type)
6454 {
6455 	int ret;
6456 
6457 	ch->initialized = false;
6458 	ch->base_queue = vsi->next_base_queue;
6459 	ch->type = type;
6460 
6461 	/* Proceed with creation of channel (VMDq2) VSI */
6462 	ret = i40e_add_channel(pf, uplink_seid, ch);
6463 	if (ret) {
6464 		dev_info(&pf->pdev->dev,
6465 			 "failed to add_channel using uplink_seid %u\n",
6466 			 uplink_seid);
6467 		return ret;
6468 	}
6469 
6470 	/* Mark the successful creation of channel */
6471 	ch->initialized = true;
6472 
6473 	/* Reconfigure TX queues using QTX_CTL register */
6474 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6475 	if (ret) {
6476 		dev_info(&pf->pdev->dev,
6477 			 "failed to configure TX rings for channel %u\n",
6478 			 ch->seid);
6479 		return ret;
6480 	}
6481 
6482 	/* update 'next_base_queue' */
6483 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6484 	dev_dbg(&pf->pdev->dev,
6485 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6486 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6487 		ch->num_queue_pairs,
6488 		vsi->next_base_queue);
6489 	return ret;
6490 }
6491 
6492 /**
6493  * i40e_setup_channel - setup new channel using uplink element
6494  * @pf: ptr to PF device
6495  * @vsi: pointer to the VSI to set up the channel within
6496  * @ch: ptr to channel structure
6497  *
6498  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6499  * and uplink switching element (uplink_seid)
6500  **/
6501 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6502 			       struct i40e_channel *ch)
6503 {
6504 	struct i40e_vsi *main_vsi;
6505 	u8 vsi_type;
6506 	u16 seid;
6507 	int ret;
6508 
6509 	if (vsi->type == I40E_VSI_MAIN) {
6510 		vsi_type = I40E_VSI_VMDQ2;
6511 	} else {
6512 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6513 			vsi->type);
6514 		return false;
6515 	}
6516 
6517 	/* underlying switching element */
6518 	main_vsi = i40e_pf_get_main_vsi(pf);
6519 	seid = main_vsi->uplink_seid;
6520 
6521 	/* create channel (VSI), configure TX rings */
6522 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6523 	if (ret) {
6524 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6525 		return false;
6526 	}
6527 
6528 	return ch->initialized ? true : false;
6529 }
6530 
6531 /**
6532  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6533  * @vsi: ptr to VSI which has PF backing
6534  *
6535  * Sets up switch mode correctly if it needs to be changed and perform
6536  * what are allowed modes.
6537  **/
6538 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6539 {
6540 	u8 mode;
6541 	struct i40e_pf *pf = vsi->back;
6542 	struct i40e_hw *hw = &pf->hw;
6543 	int ret;
6544 
6545 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6546 	if (ret)
6547 		return -EINVAL;
6548 
6549 	if (hw->dev_caps.switch_mode) {
6550 		/* if switch mode is set, support mode2 (non-tunneled for
6551 		 * cloud filter) for now
6552 		 */
6553 		u32 switch_mode = hw->dev_caps.switch_mode &
6554 				  I40E_SWITCH_MODE_MASK;
6555 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6556 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6557 				return 0;
6558 			dev_err(&pf->pdev->dev,
6559 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6560 				hw->dev_caps.switch_mode);
6561 			return -EINVAL;
6562 		}
6563 	}
6564 
6565 	/* Set Bit 7 to be valid */
6566 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6567 
6568 	/* Set L4type for TCP support */
6569 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6570 
6571 	/* Set cloud filter mode */
6572 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6573 
6574 	/* Prep mode field for set_switch_config */
6575 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6576 					pf->last_sw_conf_valid_flags,
6577 					mode, NULL);
6578 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6579 		dev_err(&pf->pdev->dev,
6580 			"couldn't set switch config bits, err %pe aq_err %s\n",
6581 			ERR_PTR(ret),
6582 			i40e_aq_str(hw,
6583 				    hw->aq.asq_last_status));
6584 
6585 	return ret;
6586 }
6587 
6588 /**
6589  * i40e_create_queue_channel - function to create channel
6590  * @vsi: VSI to be configured
6591  * @ch: ptr to channel (it contains channel specific params)
6592  *
6593  * This function creates channel (VSI) using num_queues specified by user,
6594  * reconfigs RSS if needed.
6595  **/
6596 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6597 			      struct i40e_channel *ch)
6598 {
6599 	struct i40e_pf *pf = vsi->back;
6600 	bool reconfig_rss;
6601 	int err;
6602 
6603 	if (!ch)
6604 		return -EINVAL;
6605 
6606 	if (!ch->num_queue_pairs) {
6607 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6608 			ch->num_queue_pairs);
6609 		return -EINVAL;
6610 	}
6611 
6612 	/* validate user requested num_queues for channel */
6613 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6614 				       &reconfig_rss);
6615 	if (err) {
6616 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6617 			 ch->num_queue_pairs);
6618 		return -EINVAL;
6619 	}
6620 
6621 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6622 	 * VSI to be added switch to VEB mode.
6623 	 */
6624 
6625 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6626 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6627 
6628 		if (vsi->type == I40E_VSI_MAIN) {
6629 			if (i40e_is_tc_mqprio_enabled(pf))
6630 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6631 			else
6632 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6633 		}
6634 		/* now onwards for main VSI, number of queues will be value
6635 		 * of TC0's queue count
6636 		 */
6637 	}
6638 
6639 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6640 	 * it should be more than num_queues
6641 	 */
6642 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6643 		dev_dbg(&pf->pdev->dev,
6644 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6645 			vsi->cnt_q_avail, ch->num_queue_pairs);
6646 		return -EINVAL;
6647 	}
6648 
6649 	/* reconfig_rss only if vsi type is MAIN_VSI */
6650 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6651 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6652 		if (err) {
6653 			dev_info(&pf->pdev->dev,
6654 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6655 				 ch->num_queue_pairs);
6656 			return -EINVAL;
6657 		}
6658 	}
6659 
6660 	if (!i40e_setup_channel(pf, vsi, ch)) {
6661 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6662 		return -EINVAL;
6663 	}
6664 
6665 	dev_info(&pf->pdev->dev,
6666 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6667 		 ch->seid, ch->num_queue_pairs);
6668 
6669 	/* configure VSI for BW limit */
6670 	if (ch->max_tx_rate) {
6671 		u64 credits = ch->max_tx_rate;
6672 
6673 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6674 			return -EINVAL;
6675 
6676 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6677 		dev_dbg(&pf->pdev->dev,
6678 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6679 			ch->max_tx_rate,
6680 			credits,
6681 			ch->seid);
6682 	}
6683 
6684 	/* in case of VF, this will be main SRIOV VSI */
6685 	ch->parent_vsi = vsi;
6686 
6687 	/* and update main_vsi's count for queue_available to use */
6688 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6689 
6690 	return 0;
6691 }
6692 
6693 /**
6694  * i40e_configure_queue_channels - Add queue channel for the given TCs
6695  * @vsi: VSI to be configured
6696  *
6697  * Configures queue channel mapping to the given TCs
6698  **/
6699 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6700 {
6701 	struct i40e_channel *ch;
6702 	u64 max_rate = 0;
6703 	int ret = 0, i;
6704 
6705 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6706 	vsi->tc_seid_map[0] = vsi->seid;
6707 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6708 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6709 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6710 			if (!ch) {
6711 				ret = -ENOMEM;
6712 				goto err_free;
6713 			}
6714 
6715 			INIT_LIST_HEAD(&ch->list);
6716 			ch->num_queue_pairs =
6717 				vsi->tc_config.tc_info[i].qcount;
6718 			ch->base_queue =
6719 				vsi->tc_config.tc_info[i].qoffset;
6720 
6721 			/* Bandwidth limit through tc interface is in bytes/s,
6722 			 * change to Mbit/s
6723 			 */
6724 			max_rate = vsi->mqprio_qopt.max_rate[i];
6725 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6726 			ch->max_tx_rate = max_rate;
6727 
6728 			list_add_tail(&ch->list, &vsi->ch_list);
6729 
6730 			ret = i40e_create_queue_channel(vsi, ch);
6731 			if (ret) {
6732 				dev_err(&vsi->back->pdev->dev,
6733 					"Failed creating queue channel with TC%d: queues %d\n",
6734 					i, ch->num_queue_pairs);
6735 				goto err_free;
6736 			}
6737 			vsi->tc_seid_map[i] = ch->seid;
6738 		}
6739 	}
6740 
6741 	/* reset to reconfigure TX queue contexts */
6742 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6743 	return ret;
6744 
6745 err_free:
6746 	i40e_remove_queue_channels(vsi);
6747 	return ret;
6748 }
6749 
6750 /**
6751  * i40e_veb_config_tc - Configure TCs for given VEB
6752  * @veb: given VEB
6753  * @enabled_tc: TC bitmap
6754  *
6755  * Configures given TC bitmap for VEB (switching) element
6756  **/
6757 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6758 {
6759 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6760 	struct i40e_pf *pf = veb->pf;
6761 	int ret = 0;
6762 	int i;
6763 
6764 	/* No TCs or already enabled TCs just return */
6765 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6766 		return ret;
6767 
6768 	bw_data.tc_valid_bits = enabled_tc;
6769 	/* bw_data.absolute_credits is not set (relative) */
6770 
6771 	/* Enable ETS TCs with equal BW Share for now */
6772 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6773 		if (enabled_tc & BIT(i))
6774 			bw_data.tc_bw_share_credits[i] = 1;
6775 	}
6776 
6777 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6778 						   &bw_data, NULL);
6779 	if (ret) {
6780 		dev_info(&pf->pdev->dev,
6781 			 "VEB bw config failed, err %pe aq_err %s\n",
6782 			 ERR_PTR(ret),
6783 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6784 		goto out;
6785 	}
6786 
6787 	/* Update the BW information */
6788 	ret = i40e_veb_get_bw_info(veb);
6789 	if (ret) {
6790 		dev_info(&pf->pdev->dev,
6791 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6792 			 ERR_PTR(ret),
6793 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6794 	}
6795 
6796 out:
6797 	return ret;
6798 }
6799 
6800 #ifdef CONFIG_I40E_DCB
6801 /**
6802  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6803  * @pf: PF struct
6804  *
6805  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6806  * the caller would've quiesce all the VSIs before calling
6807  * this function
6808  **/
6809 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6810 {
6811 	struct i40e_vsi *vsi;
6812 	struct i40e_veb *veb;
6813 	u8 tc_map = 0;
6814 	int ret;
6815 	int v;
6816 
6817 	/* Enable the TCs available on PF to all VEBs */
6818 	tc_map = i40e_pf_get_tc_map(pf);
6819 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6820 		return;
6821 
6822 	i40e_pf_for_each_veb(pf, v, veb) {
6823 		ret = i40e_veb_config_tc(veb, tc_map);
6824 		if (ret) {
6825 			dev_info(&pf->pdev->dev,
6826 				 "Failed configuring TC for VEB seid=%d\n",
6827 				 veb->seid);
6828 			/* Will try to configure as many components */
6829 		}
6830 	}
6831 
6832 	/* Update each VSI */
6833 	i40e_pf_for_each_vsi(pf, v, vsi) {
6834 		/* - Enable all TCs for the LAN VSI
6835 		 * - For all others keep them at TC0 for now
6836 		 */
6837 		if (vsi->type == I40E_VSI_MAIN)
6838 			tc_map = i40e_pf_get_tc_map(pf);
6839 		else
6840 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6841 
6842 		ret = i40e_vsi_config_tc(vsi, tc_map);
6843 		if (ret) {
6844 			dev_info(&pf->pdev->dev,
6845 				 "Failed configuring TC for VSI seid=%d\n",
6846 				 vsi->seid);
6847 			/* Will try to configure as many components */
6848 		} else {
6849 			/* Re-configure VSI vectors based on updated TC map */
6850 			i40e_vsi_map_rings_to_vectors(vsi);
6851 			if (vsi->netdev)
6852 				i40e_dcbnl_set_all(vsi);
6853 		}
6854 	}
6855 }
6856 
6857 /**
6858  * i40e_resume_port_tx - Resume port Tx
6859  * @pf: PF struct
6860  *
6861  * Resume a port's Tx and issue a PF reset in case of failure to
6862  * resume.
6863  **/
6864 static int i40e_resume_port_tx(struct i40e_pf *pf)
6865 {
6866 	struct i40e_hw *hw = &pf->hw;
6867 	int ret;
6868 
6869 	ret = i40e_aq_resume_port_tx(hw, NULL);
6870 	if (ret) {
6871 		dev_info(&pf->pdev->dev,
6872 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6873 			  ERR_PTR(ret),
6874 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6875 		/* Schedule PF reset to recover */
6876 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6877 		i40e_service_event_schedule(pf);
6878 	}
6879 
6880 	return ret;
6881 }
6882 
6883 /**
6884  * i40e_suspend_port_tx - Suspend port Tx
6885  * @pf: PF struct
6886  *
6887  * Suspend a port's Tx and issue a PF reset in case of failure.
6888  **/
6889 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6890 {
6891 	struct i40e_hw *hw = &pf->hw;
6892 	int ret;
6893 
6894 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6895 	if (ret) {
6896 		dev_info(&pf->pdev->dev,
6897 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6898 			 ERR_PTR(ret),
6899 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6900 		/* Schedule PF reset to recover */
6901 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6902 		i40e_service_event_schedule(pf);
6903 	}
6904 
6905 	return ret;
6906 }
6907 
6908 /**
6909  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6910  * @pf: PF being configured
6911  * @new_cfg: New DCBX configuration
6912  *
6913  * Program DCB settings into HW and reconfigure VEB/VSIs on
6914  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6915  **/
6916 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6917 				  struct i40e_dcbx_config *new_cfg)
6918 {
6919 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6920 	int ret;
6921 
6922 	/* Check if need reconfiguration */
6923 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6924 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6925 		return 0;
6926 	}
6927 
6928 	/* Config change disable all VSIs */
6929 	i40e_pf_quiesce_all_vsi(pf);
6930 
6931 	/* Copy the new config to the current config */
6932 	*old_cfg = *new_cfg;
6933 	old_cfg->etsrec = old_cfg->etscfg;
6934 	ret = i40e_set_dcb_config(&pf->hw);
6935 	if (ret) {
6936 		dev_info(&pf->pdev->dev,
6937 			 "Set DCB Config failed, err %pe aq_err %s\n",
6938 			 ERR_PTR(ret),
6939 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6940 		goto out;
6941 	}
6942 
6943 	/* Changes in configuration update VEB/VSI */
6944 	i40e_dcb_reconfigure(pf);
6945 out:
6946 	/* In case of reset do not try to resume anything */
6947 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6948 		/* Re-start the VSIs if disabled */
6949 		ret = i40e_resume_port_tx(pf);
6950 		/* In case of error no point in resuming VSIs */
6951 		if (ret)
6952 			goto err;
6953 		i40e_pf_unquiesce_all_vsi(pf);
6954 	}
6955 err:
6956 	return ret;
6957 }
6958 
6959 /**
6960  * i40e_hw_dcb_config - Program new DCBX settings into HW
6961  * @pf: PF being configured
6962  * @new_cfg: New DCBX configuration
6963  *
6964  * Program DCB settings into HW and reconfigure VEB/VSIs on
6965  * given PF
6966  **/
6967 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6968 {
6969 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6970 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6971 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6972 	struct i40e_dcbx_config *old_cfg;
6973 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6974 	struct i40e_rx_pb_config pb_cfg;
6975 	struct i40e_hw *hw = &pf->hw;
6976 	u8 num_ports = hw->num_ports;
6977 	bool need_reconfig;
6978 	int ret = -EINVAL;
6979 	u8 lltc_map = 0;
6980 	u8 tc_map = 0;
6981 	u8 new_numtc;
6982 	u8 i;
6983 
6984 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6985 	/* Un-pack information to Program ETS HW via shared API
6986 	 * numtc, tcmap
6987 	 * LLTC map
6988 	 * ETS/NON-ETS arbiter mode
6989 	 * max exponent (credit refills)
6990 	 * Total number of ports
6991 	 * PFC priority bit-map
6992 	 * Priority Table
6993 	 * BW % per TC
6994 	 * Arbiter mode between UPs sharing same TC
6995 	 * TSA table (ETS or non-ETS)
6996 	 * EEE enabled or not
6997 	 * MFS TC table
6998 	 */
6999 
7000 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
7001 
7002 	memset(&ets_data, 0, sizeof(ets_data));
7003 	for (i = 0; i < new_numtc; i++) {
7004 		tc_map |= BIT(i);
7005 		switch (new_cfg->etscfg.tsatable[i]) {
7006 		case I40E_IEEE_TSA_ETS:
7007 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
7008 			ets_data.tc_bw_share_credits[i] =
7009 					new_cfg->etscfg.tcbwtable[i];
7010 			break;
7011 		case I40E_IEEE_TSA_STRICT:
7012 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
7013 			lltc_map |= BIT(i);
7014 			ets_data.tc_bw_share_credits[i] =
7015 					I40E_DCB_STRICT_PRIO_CREDITS;
7016 			break;
7017 		default:
7018 			/* Invalid TSA type */
7019 			need_reconfig = false;
7020 			goto out;
7021 		}
7022 	}
7023 
7024 	old_cfg = &hw->local_dcbx_config;
7025 	/* Check if need reconfiguration */
7026 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
7027 
7028 	/* If needed, enable/disable frame tagging, disable all VSIs
7029 	 * and suspend port tx
7030 	 */
7031 	if (need_reconfig) {
7032 		/* Enable DCB tagging only when more than one TC */
7033 		if (new_numtc > 1)
7034 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7035 		else
7036 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7037 
7038 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7039 		/* Reconfiguration needed quiesce all VSIs */
7040 		i40e_pf_quiesce_all_vsi(pf);
7041 		ret = i40e_suspend_port_tx(pf);
7042 		if (ret)
7043 			goto err;
7044 	}
7045 
7046 	/* Configure Port ETS Tx Scheduler */
7047 	ets_data.tc_valid_bits = tc_map;
7048 	ets_data.tc_strict_priority_flags = lltc_map;
7049 	ret = i40e_aq_config_switch_comp_ets
7050 		(hw, pf->mac_seid, &ets_data,
7051 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7052 	if (ret) {
7053 		dev_info(&pf->pdev->dev,
7054 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7055 			 ERR_PTR(ret),
7056 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7057 		goto out;
7058 	}
7059 
7060 	/* Configure Rx ETS HW */
7061 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7062 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7063 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7064 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7065 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7066 				   lltc_map);
7067 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7068 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7069 				     prio_type);
7070 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7071 			       new_cfg->etscfg.prioritytable);
7072 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7073 
7074 	/* Configure Rx Packet Buffers in HW */
7075 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7076 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
7077 
7078 		mfs_tc[i] = main_vsi->netdev->mtu;
7079 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7080 	}
7081 
7082 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7083 					 false, new_cfg->pfc.pfcenable,
7084 					 mfs_tc, &pb_cfg);
7085 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7086 
7087 	/* Update the local Rx Packet buffer config */
7088 	pf->pb_cfg = pb_cfg;
7089 
7090 	/* Inform the FW about changes to DCB configuration */
7091 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7092 	if (ret) {
7093 		dev_info(&pf->pdev->dev,
7094 			 "DCB Updated failed, err %pe aq_err %s\n",
7095 			 ERR_PTR(ret),
7096 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7097 		goto out;
7098 	}
7099 
7100 	/* Update the port DCBx configuration */
7101 	*old_cfg = *new_cfg;
7102 
7103 	/* Changes in configuration update VEB/VSI */
7104 	i40e_dcb_reconfigure(pf);
7105 out:
7106 	/* Re-start the VSIs if disabled */
7107 	if (need_reconfig) {
7108 		ret = i40e_resume_port_tx(pf);
7109 
7110 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7111 		/* In case of error no point in resuming VSIs */
7112 		if (ret)
7113 			goto err;
7114 
7115 		/* Wait for the PF's queues to be disabled */
7116 		ret = i40e_pf_wait_queues_disabled(pf);
7117 		if (ret) {
7118 			/* Schedule PF reset to recover */
7119 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7120 			i40e_service_event_schedule(pf);
7121 			goto err;
7122 		} else {
7123 			i40e_pf_unquiesce_all_vsi(pf);
7124 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7125 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7126 		}
7127 		/* registers are set, lets apply */
7128 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7129 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7130 	}
7131 
7132 err:
7133 	return ret;
7134 }
7135 
7136 /**
7137  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7138  * @pf: PF being queried
7139  *
7140  * Set default DCB configuration in case DCB is to be done in SW.
7141  **/
7142 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7143 {
7144 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7145 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7146 	struct i40e_hw *hw = &pf->hw;
7147 	int err;
7148 
7149 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7150 		/* Update the local cached instance with TC0 ETS */
7151 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7152 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7153 		pf->tmp_cfg.etscfg.maxtcs = 0;
7154 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7155 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7156 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7157 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7158 		/* FW needs one App to configure HW */
7159 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7160 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7161 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7162 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7163 
7164 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7165 	}
7166 
7167 	memset(&ets_data, 0, sizeof(ets_data));
7168 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7169 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7170 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7171 
7172 	/* Enable ETS on the Physical port */
7173 	err = i40e_aq_config_switch_comp_ets
7174 		(hw, pf->mac_seid, &ets_data,
7175 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7176 	if (err) {
7177 		dev_info(&pf->pdev->dev,
7178 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7179 			 ERR_PTR(err),
7180 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7181 		err = -ENOENT;
7182 		goto out;
7183 	}
7184 
7185 	/* Update the local cached instance with TC0 ETS */
7186 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7187 	dcb_cfg->etscfg.cbs = 0;
7188 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7189 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7190 
7191 out:
7192 	return err;
7193 }
7194 
7195 /**
7196  * i40e_init_pf_dcb - Initialize DCB configuration
7197  * @pf: PF being configured
7198  *
7199  * Query the current DCB configuration and cache it
7200  * in the hardware structure
7201  **/
7202 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7203 {
7204 	struct i40e_hw *hw = &pf->hw;
7205 	int err;
7206 
7207 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7208 	 * Also do not enable DCBx if FW LLDP agent is disabled
7209 	 */
7210 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7211 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7212 		err = -EOPNOTSUPP;
7213 		goto out;
7214 	}
7215 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7216 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7217 		err = i40e_dcb_sw_default_config(pf);
7218 		if (err) {
7219 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7220 			goto out;
7221 		}
7222 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7223 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7224 			       DCB_CAP_DCBX_VER_IEEE;
7225 		/* at init capable but disabled */
7226 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7227 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7228 		goto out;
7229 	}
7230 	err = i40e_init_dcb(hw, true);
7231 	if (!err) {
7232 		/* Device/Function is not DCBX capable */
7233 		if ((!hw->func_caps.dcb) ||
7234 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7235 			dev_info(&pf->pdev->dev,
7236 				 "DCBX offload is not supported or is disabled for this PF.\n");
7237 		} else {
7238 			/* When status is not DISABLED then DCBX in FW */
7239 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7240 				       DCB_CAP_DCBX_VER_IEEE;
7241 
7242 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7243 			/* Enable DCB tagging only when more than one TC
7244 			 * or explicitly disable if only one TC
7245 			 */
7246 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7247 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7248 			else
7249 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7250 			dev_dbg(&pf->pdev->dev,
7251 				"DCBX offload is supported for this PF.\n");
7252 		}
7253 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7254 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7255 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7256 	} else {
7257 		dev_info(&pf->pdev->dev,
7258 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7259 			 ERR_PTR(err),
7260 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7261 	}
7262 
7263 out:
7264 	return err;
7265 }
7266 #endif /* CONFIG_I40E_DCB */
7267 
7268 static void i40e_print_link_message_eee(struct i40e_vsi *vsi,
7269 					const char *speed, const char *fc)
7270 {
7271 	struct ethtool_keee kedata;
7272 
7273 	memzero_explicit(&kedata, sizeof(kedata));
7274 	if (vsi->netdev->ethtool_ops->get_eee)
7275 		vsi->netdev->ethtool_ops->get_eee(vsi->netdev, &kedata);
7276 
7277 	if (!linkmode_empty(kedata.supported))
7278 		netdev_info(vsi->netdev,
7279 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s, EEE: %s\n",
7280 			    speed, fc,
7281 			    kedata.eee_enabled ? "Enabled" : "Disabled");
7282 	else
7283 		netdev_info(vsi->netdev,
7284 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7285 			    speed, fc);
7286 }
7287 
7288 /**
7289  * i40e_print_link_message - print link up or down
7290  * @vsi: the VSI for which link needs a message
7291  * @isup: true of link is up, false otherwise
7292  */
7293 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7294 {
7295 	enum i40e_aq_link_speed new_speed;
7296 	struct i40e_pf *pf = vsi->back;
7297 	char *speed = "Unknown";
7298 	char *fc = "Unknown";
7299 	char *fec = "";
7300 	char *req_fec = "";
7301 	char *an = "";
7302 
7303 	if (isup)
7304 		new_speed = pf->hw.phy.link_info.link_speed;
7305 	else
7306 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7307 
7308 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7309 		return;
7310 	vsi->current_isup = isup;
7311 	vsi->current_speed = new_speed;
7312 	if (!isup) {
7313 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7314 		return;
7315 	}
7316 
7317 	/* Warn user if link speed on NPAR enabled partition is not at
7318 	 * least 10GB
7319 	 */
7320 	if (pf->hw.func_caps.npar_enable &&
7321 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7322 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7323 		netdev_warn(vsi->netdev,
7324 			    "The partition detected link speed that is less than 10Gbps\n");
7325 
7326 	switch (pf->hw.phy.link_info.link_speed) {
7327 	case I40E_LINK_SPEED_40GB:
7328 		speed = "40 G";
7329 		break;
7330 	case I40E_LINK_SPEED_20GB:
7331 		speed = "20 G";
7332 		break;
7333 	case I40E_LINK_SPEED_25GB:
7334 		speed = "25 G";
7335 		break;
7336 	case I40E_LINK_SPEED_10GB:
7337 		speed = "10 G";
7338 		break;
7339 	case I40E_LINK_SPEED_5GB:
7340 		speed = "5 G";
7341 		break;
7342 	case I40E_LINK_SPEED_2_5GB:
7343 		speed = "2.5 G";
7344 		break;
7345 	case I40E_LINK_SPEED_1GB:
7346 		speed = "1000 M";
7347 		break;
7348 	case I40E_LINK_SPEED_100MB:
7349 		speed = "100 M";
7350 		break;
7351 	default:
7352 		break;
7353 	}
7354 
7355 	switch (pf->hw.fc.current_mode) {
7356 	case I40E_FC_FULL:
7357 		fc = "RX/TX";
7358 		break;
7359 	case I40E_FC_TX_PAUSE:
7360 		fc = "TX";
7361 		break;
7362 	case I40E_FC_RX_PAUSE:
7363 		fc = "RX";
7364 		break;
7365 	default:
7366 		fc = "None";
7367 		break;
7368 	}
7369 
7370 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7371 		req_fec = "None";
7372 		fec = "None";
7373 		an = "False";
7374 
7375 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7376 			an = "True";
7377 
7378 		if (pf->hw.phy.link_info.fec_info &
7379 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7380 			fec = "CL74 FC-FEC/BASE-R";
7381 		else if (pf->hw.phy.link_info.fec_info &
7382 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7383 			fec = "CL108 RS-FEC";
7384 
7385 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7386 		 * both RS and FC are requested
7387 		 */
7388 		if (vsi->back->hw.phy.link_info.req_fec_info &
7389 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7390 			if (vsi->back->hw.phy.link_info.req_fec_info &
7391 			    I40E_AQ_REQUEST_FEC_RS)
7392 				req_fec = "CL108 RS-FEC";
7393 			else
7394 				req_fec = "CL74 FC-FEC/BASE-R";
7395 		}
7396 		netdev_info(vsi->netdev,
7397 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7398 			    speed, req_fec, fec, an, fc);
7399 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7400 		req_fec = "None";
7401 		fec = "None";
7402 		an = "False";
7403 
7404 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7405 			an = "True";
7406 
7407 		if (pf->hw.phy.link_info.fec_info &
7408 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7409 			fec = "CL74 FC-FEC/BASE-R";
7410 
7411 		if (pf->hw.phy.link_info.req_fec_info &
7412 		    I40E_AQ_REQUEST_FEC_KR)
7413 			req_fec = "CL74 FC-FEC/BASE-R";
7414 
7415 		netdev_info(vsi->netdev,
7416 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7417 			    speed, req_fec, fec, an, fc);
7418 	} else {
7419 		i40e_print_link_message_eee(vsi, speed, fc);
7420 	}
7421 
7422 }
7423 
7424 /**
7425  * i40e_up_complete - Finish the last steps of bringing up a connection
7426  * @vsi: the VSI being configured
7427  **/
7428 static int i40e_up_complete(struct i40e_vsi *vsi)
7429 {
7430 	struct i40e_pf *pf = vsi->back;
7431 	int err;
7432 
7433 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7434 		i40e_vsi_configure_msix(vsi);
7435 	else
7436 		i40e_configure_msi_and_legacy(vsi);
7437 
7438 	/* start rings */
7439 	err = i40e_vsi_start_rings(vsi);
7440 	if (err)
7441 		return err;
7442 
7443 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7444 	i40e_napi_enable_all(vsi);
7445 	i40e_vsi_enable_irq(vsi);
7446 
7447 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7448 	    (vsi->netdev)) {
7449 		i40e_print_link_message(vsi, true);
7450 		netif_tx_start_all_queues(vsi->netdev);
7451 		netif_carrier_on(vsi->netdev);
7452 	}
7453 
7454 	/* replay FDIR SB filters */
7455 	if (vsi->type == I40E_VSI_FDIR) {
7456 		/* reset fd counters */
7457 		pf->fd_add_err = 0;
7458 		pf->fd_atr_cnt = 0;
7459 		i40e_fdir_filter_restore(vsi);
7460 	}
7461 
7462 	/* On the next run of the service_task, notify any clients of the new
7463 	 * opened netdev
7464 	 */
7465 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7466 	i40e_service_event_schedule(pf);
7467 
7468 	return 0;
7469 }
7470 
7471 /**
7472  * i40e_vsi_reinit_locked - Reset the VSI
7473  * @vsi: the VSI being configured
7474  *
7475  * Rebuild the ring structs after some configuration
7476  * has changed, e.g. MTU size.
7477  **/
7478 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7479 {
7480 	struct i40e_pf *pf = vsi->back;
7481 
7482 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7483 		usleep_range(1000, 2000);
7484 	i40e_down(vsi);
7485 
7486 	i40e_up(vsi);
7487 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7488 }
7489 
7490 /**
7491  * i40e_force_link_state - Force the link status
7492  * @pf: board private structure
7493  * @is_up: whether the link state should be forced up or down
7494  **/
7495 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7496 {
7497 	struct i40e_aq_get_phy_abilities_resp abilities;
7498 	struct i40e_aq_set_phy_config config = {0};
7499 	bool non_zero_phy_type = is_up;
7500 	struct i40e_hw *hw = &pf->hw;
7501 	u64 mask;
7502 	u8 speed;
7503 	int err;
7504 
7505 	/* Card might've been put in an unstable state by other drivers
7506 	 * and applications, which causes incorrect speed values being
7507 	 * set on startup. In order to clear speed registers, we call
7508 	 * get_phy_capabilities twice, once to get initial state of
7509 	 * available speeds, and once to get current PHY config.
7510 	 */
7511 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7512 					   NULL);
7513 	if (err) {
7514 		dev_err(&pf->pdev->dev,
7515 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7516 			ERR_PTR(err),
7517 			i40e_aq_str(hw, hw->aq.asq_last_status));
7518 		return err;
7519 	}
7520 	speed = abilities.link_speed;
7521 
7522 	/* Get the current phy config */
7523 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7524 					   NULL);
7525 	if (err) {
7526 		dev_err(&pf->pdev->dev,
7527 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7528 			ERR_PTR(err),
7529 			i40e_aq_str(hw, hw->aq.asq_last_status));
7530 		return err;
7531 	}
7532 
7533 	/* If link needs to go up, but was not forced to go down,
7534 	 * and its speed values are OK, no need for a flap
7535 	 * if non_zero_phy_type was set, still need to force up
7536 	 */
7537 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7538 		non_zero_phy_type = true;
7539 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7540 		return 0;
7541 
7542 	/* To force link we need to set bits for all supported PHY types,
7543 	 * but there are now more than 32, so we need to split the bitmap
7544 	 * across two fields.
7545 	 */
7546 	mask = I40E_PHY_TYPES_BITMASK;
7547 	config.phy_type =
7548 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7549 	config.phy_type_ext =
7550 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7551 	/* Copy the old settings, except of phy_type */
7552 	config.abilities = abilities.abilities;
7553 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7554 		if (is_up)
7555 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7556 		else
7557 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7558 	}
7559 	if (abilities.link_speed != 0)
7560 		config.link_speed = abilities.link_speed;
7561 	else
7562 		config.link_speed = speed;
7563 	config.eee_capability = abilities.eee_capability;
7564 	config.eeer = abilities.eeer_val;
7565 	config.low_power_ctrl = abilities.d3_lpan;
7566 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7567 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7568 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7569 
7570 	if (err) {
7571 		dev_err(&pf->pdev->dev,
7572 			"set phy config ret =  %pe last_status =  %s\n",
7573 			ERR_PTR(err),
7574 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7575 		return err;
7576 	}
7577 
7578 	/* Update the link info */
7579 	err = i40e_update_link_info(hw);
7580 	if (err) {
7581 		/* Wait a little bit (on 40G cards it sometimes takes a really
7582 		 * long time for link to come back from the atomic reset)
7583 		 * and try once more
7584 		 */
7585 		msleep(1000);
7586 		i40e_update_link_info(hw);
7587 	}
7588 
7589 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7590 
7591 	return 0;
7592 }
7593 
7594 /**
7595  * i40e_up - Bring the connection back up after being down
7596  * @vsi: the VSI being configured
7597  **/
7598 int i40e_up(struct i40e_vsi *vsi)
7599 {
7600 	int err;
7601 
7602 	if (vsi->type == I40E_VSI_MAIN &&
7603 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7604 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7605 		i40e_force_link_state(vsi->back, true);
7606 
7607 	err = i40e_vsi_configure(vsi);
7608 	if (!err)
7609 		err = i40e_up_complete(vsi);
7610 
7611 	return err;
7612 }
7613 
7614 /**
7615  * i40e_down - Shutdown the connection processing
7616  * @vsi: the VSI being stopped
7617  **/
7618 void i40e_down(struct i40e_vsi *vsi)
7619 {
7620 	int i;
7621 
7622 	/* It is assumed that the caller of this function
7623 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7624 	 */
7625 	if (vsi->netdev) {
7626 		netif_carrier_off(vsi->netdev);
7627 		netif_tx_disable(vsi->netdev);
7628 	}
7629 	i40e_vsi_disable_irq(vsi);
7630 	i40e_vsi_stop_rings(vsi);
7631 	if (vsi->type == I40E_VSI_MAIN &&
7632 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7633 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7634 		i40e_force_link_state(vsi->back, false);
7635 	i40e_napi_disable_all(vsi);
7636 
7637 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7638 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7639 		if (i40e_enabled_xdp_vsi(vsi)) {
7640 			/* Make sure that in-progress ndo_xdp_xmit and
7641 			 * ndo_xsk_wakeup calls are completed.
7642 			 */
7643 			synchronize_rcu();
7644 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7645 		}
7646 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7647 	}
7648 
7649 }
7650 
7651 /**
7652  * i40e_validate_mqprio_qopt- validate queue mapping info
7653  * @vsi: the VSI being configured
7654  * @mqprio_qopt: queue parametrs
7655  **/
7656 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7657 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7658 {
7659 	u64 sum_max_rate = 0;
7660 	u64 max_rate = 0;
7661 	int i;
7662 
7663 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7664 	    mqprio_qopt->qopt.num_tc < 1 ||
7665 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7666 		return -EINVAL;
7667 	for (i = 0; ; i++) {
7668 		if (!mqprio_qopt->qopt.count[i])
7669 			return -EINVAL;
7670 		if (mqprio_qopt->min_rate[i]) {
7671 			dev_err(&vsi->back->pdev->dev,
7672 				"Invalid min tx rate (greater than 0) specified\n");
7673 			return -EINVAL;
7674 		}
7675 		max_rate = mqprio_qopt->max_rate[i];
7676 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7677 		sum_max_rate += max_rate;
7678 
7679 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7680 			break;
7681 		if (mqprio_qopt->qopt.offset[i + 1] !=
7682 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7683 			return -EINVAL;
7684 	}
7685 	if (vsi->num_queue_pairs <
7686 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7687 		dev_err(&vsi->back->pdev->dev,
7688 			"Failed to create traffic channel, insufficient number of queues.\n");
7689 		return -EINVAL;
7690 	}
7691 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7692 		dev_err(&vsi->back->pdev->dev,
7693 			"Invalid max tx rate specified\n");
7694 		return -EINVAL;
7695 	}
7696 	return 0;
7697 }
7698 
7699 /**
7700  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7701  * @vsi: the VSI being configured
7702  **/
7703 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7704 {
7705 	u16 qcount;
7706 	int i;
7707 
7708 	/* Only TC0 is enabled */
7709 	vsi->tc_config.numtc = 1;
7710 	vsi->tc_config.enabled_tc = 1;
7711 	qcount = min_t(int, vsi->alloc_queue_pairs,
7712 		       i40e_pf_get_max_q_per_tc(vsi->back));
7713 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7714 		/* For the TC that is not enabled set the offset to default
7715 		 * queue and allocate one queue for the given TC.
7716 		 */
7717 		vsi->tc_config.tc_info[i].qoffset = 0;
7718 		if (i == 0)
7719 			vsi->tc_config.tc_info[i].qcount = qcount;
7720 		else
7721 			vsi->tc_config.tc_info[i].qcount = 1;
7722 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7723 	}
7724 }
7725 
7726 /**
7727  * i40e_del_macvlan_filter
7728  * @hw: pointer to the HW structure
7729  * @seid: seid of the channel VSI
7730  * @macaddr: the mac address to apply as a filter
7731  * @aq_err: store the admin Q error
7732  *
7733  * This function deletes a mac filter on the channel VSI which serves as the
7734  * macvlan. Returns 0 on success.
7735  **/
7736 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7737 				   const u8 *macaddr, int *aq_err)
7738 {
7739 	struct i40e_aqc_remove_macvlan_element_data element;
7740 	int status;
7741 
7742 	memset(&element, 0, sizeof(element));
7743 	ether_addr_copy(element.mac_addr, macaddr);
7744 	element.vlan_tag = 0;
7745 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7746 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7747 	*aq_err = hw->aq.asq_last_status;
7748 
7749 	return status;
7750 }
7751 
7752 /**
7753  * i40e_add_macvlan_filter
7754  * @hw: pointer to the HW structure
7755  * @seid: seid of the channel VSI
7756  * @macaddr: the mac address to apply as a filter
7757  * @aq_err: store the admin Q error
7758  *
7759  * This function adds a mac filter on the channel VSI which serves as the
7760  * macvlan. Returns 0 on success.
7761  **/
7762 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7763 				   const u8 *macaddr, int *aq_err)
7764 {
7765 	struct i40e_aqc_add_macvlan_element_data element;
7766 	u16 cmd_flags = 0;
7767 	int status;
7768 
7769 	ether_addr_copy(element.mac_addr, macaddr);
7770 	element.vlan_tag = 0;
7771 	element.queue_number = 0;
7772 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7773 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7774 	element.flags = cpu_to_le16(cmd_flags);
7775 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7776 	*aq_err = hw->aq.asq_last_status;
7777 
7778 	return status;
7779 }
7780 
7781 /**
7782  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7783  * @vsi: the VSI we want to access
7784  * @ch: the channel we want to access
7785  */
7786 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7787 {
7788 	struct i40e_ring *tx_ring, *rx_ring;
7789 	u16 pf_q;
7790 	int i;
7791 
7792 	for (i = 0; i < ch->num_queue_pairs; i++) {
7793 		pf_q = ch->base_queue + i;
7794 		tx_ring = vsi->tx_rings[pf_q];
7795 		tx_ring->ch = NULL;
7796 		rx_ring = vsi->rx_rings[pf_q];
7797 		rx_ring->ch = NULL;
7798 	}
7799 }
7800 
7801 /**
7802  * i40e_free_macvlan_channels
7803  * @vsi: the VSI we want to access
7804  *
7805  * This function frees the Qs of the channel VSI from
7806  * the stack and also deletes the channel VSIs which
7807  * serve as macvlans.
7808  */
7809 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7810 {
7811 	struct i40e_channel *ch, *ch_tmp;
7812 	int ret;
7813 
7814 	if (list_empty(&vsi->macvlan_list))
7815 		return;
7816 
7817 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7818 		struct i40e_vsi *parent_vsi;
7819 
7820 		if (i40e_is_channel_macvlan(ch)) {
7821 			i40e_reset_ch_rings(vsi, ch);
7822 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7823 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7824 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7825 			kfree(ch->fwd);
7826 			ch->fwd = NULL;
7827 		}
7828 
7829 		list_del(&ch->list);
7830 		parent_vsi = ch->parent_vsi;
7831 		if (!parent_vsi || !ch->initialized) {
7832 			kfree(ch);
7833 			continue;
7834 		}
7835 
7836 		/* remove the VSI */
7837 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7838 					     NULL);
7839 		if (ret)
7840 			dev_err(&vsi->back->pdev->dev,
7841 				"unable to remove channel (%d) for parent VSI(%d)\n",
7842 				ch->seid, parent_vsi->seid);
7843 		kfree(ch);
7844 	}
7845 	vsi->macvlan_cnt = 0;
7846 }
7847 
7848 /**
7849  * i40e_fwd_ring_up - bring the macvlan device up
7850  * @vsi: the VSI we want to access
7851  * @vdev: macvlan netdevice
7852  * @fwd: the private fwd structure
7853  */
7854 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7855 			    struct i40e_fwd_adapter *fwd)
7856 {
7857 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7858 	int ret = 0, num_tc = 1,  i, aq_err;
7859 	struct i40e_pf *pf = vsi->back;
7860 	struct i40e_hw *hw = &pf->hw;
7861 
7862 	/* Go through the list and find an available channel */
7863 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7864 		if (!i40e_is_channel_macvlan(iter)) {
7865 			iter->fwd = fwd;
7866 			/* record configuration for macvlan interface in vdev */
7867 			for (i = 0; i < num_tc; i++)
7868 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7869 							     i,
7870 							     iter->num_queue_pairs,
7871 							     iter->base_queue);
7872 			for (i = 0; i < iter->num_queue_pairs; i++) {
7873 				struct i40e_ring *tx_ring, *rx_ring;
7874 				u16 pf_q;
7875 
7876 				pf_q = iter->base_queue + i;
7877 
7878 				/* Get to TX ring ptr */
7879 				tx_ring = vsi->tx_rings[pf_q];
7880 				tx_ring->ch = iter;
7881 
7882 				/* Get the RX ring ptr */
7883 				rx_ring = vsi->rx_rings[pf_q];
7884 				rx_ring->ch = iter;
7885 			}
7886 			ch = iter;
7887 			break;
7888 		}
7889 	}
7890 
7891 	if (!ch)
7892 		return -EINVAL;
7893 
7894 	/* Guarantee all rings are updated before we update the
7895 	 * MAC address filter.
7896 	 */
7897 	wmb();
7898 
7899 	/* Add a mac filter */
7900 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7901 	if (ret) {
7902 		/* if we cannot add the MAC rule then disable the offload */
7903 		macvlan_release_l2fw_offload(vdev);
7904 		for (i = 0; i < ch->num_queue_pairs; i++) {
7905 			struct i40e_ring *rx_ring;
7906 			u16 pf_q;
7907 
7908 			pf_q = ch->base_queue + i;
7909 			rx_ring = vsi->rx_rings[pf_q];
7910 			rx_ring->netdev = NULL;
7911 		}
7912 		dev_info(&pf->pdev->dev,
7913 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7914 			  ERR_PTR(ret),
7915 			  i40e_aq_str(hw, aq_err));
7916 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7917 	}
7918 
7919 	return ret;
7920 }
7921 
7922 /**
7923  * i40e_setup_macvlans - create the channels which will be macvlans
7924  * @vsi: the VSI we want to access
7925  * @macvlan_cnt: no. of macvlans to be setup
7926  * @qcnt: no. of Qs per macvlan
7927  * @vdev: macvlan netdevice
7928  */
7929 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7930 			       struct net_device *vdev)
7931 {
7932 	struct i40e_pf *pf = vsi->back;
7933 	struct i40e_hw *hw = &pf->hw;
7934 	struct i40e_vsi_context ctxt;
7935 	u16 sections, qmap, num_qps;
7936 	struct i40e_channel *ch;
7937 	int i, pow, ret = 0;
7938 	u8 offset = 0;
7939 
7940 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7941 		return -EINVAL;
7942 
7943 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7944 
7945 	/* find the next higher power-of-2 of num queue pairs */
7946 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7947 
7948 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7949 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7950 
7951 	/* Setup context bits for the main VSI */
7952 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7953 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7954 	memset(&ctxt, 0, sizeof(ctxt));
7955 	ctxt.seid = vsi->seid;
7956 	ctxt.pf_num = vsi->back->hw.pf_id;
7957 	ctxt.vf_num = 0;
7958 	ctxt.uplink_seid = vsi->uplink_seid;
7959 	ctxt.info = vsi->info;
7960 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7961 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7962 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7963 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7964 
7965 	/* Reconfigure RSS for main VSI with new max queue count */
7966 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7967 	ret = i40e_vsi_config_rss(vsi);
7968 	if (ret) {
7969 		dev_info(&pf->pdev->dev,
7970 			 "Failed to reconfig RSS for num_queues (%u)\n",
7971 			 vsi->rss_size);
7972 		return ret;
7973 	}
7974 	vsi->reconfig_rss = true;
7975 	dev_dbg(&vsi->back->pdev->dev,
7976 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7977 	vsi->next_base_queue = num_qps;
7978 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7979 
7980 	/* Update the VSI after updating the VSI queue-mapping
7981 	 * information
7982 	 */
7983 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7984 	if (ret) {
7985 		dev_info(&pf->pdev->dev,
7986 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7987 			 ERR_PTR(ret),
7988 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7989 		return ret;
7990 	}
7991 	/* update the local VSI info with updated queue map */
7992 	i40e_vsi_update_queue_map(vsi, &ctxt);
7993 	vsi->info.valid_sections = 0;
7994 
7995 	/* Create channels for macvlans */
7996 	INIT_LIST_HEAD(&vsi->macvlan_list);
7997 	for (i = 0; i < macvlan_cnt; i++) {
7998 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7999 		if (!ch) {
8000 			ret = -ENOMEM;
8001 			goto err_free;
8002 		}
8003 		INIT_LIST_HEAD(&ch->list);
8004 		ch->num_queue_pairs = qcnt;
8005 		if (!i40e_setup_channel(pf, vsi, ch)) {
8006 			ret = -EINVAL;
8007 			kfree(ch);
8008 			goto err_free;
8009 		}
8010 		ch->parent_vsi = vsi;
8011 		vsi->cnt_q_avail -= ch->num_queue_pairs;
8012 		vsi->macvlan_cnt++;
8013 		list_add_tail(&ch->list, &vsi->macvlan_list);
8014 	}
8015 
8016 	return ret;
8017 
8018 err_free:
8019 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
8020 	i40e_free_macvlan_channels(vsi);
8021 
8022 	return ret;
8023 }
8024 
8025 /**
8026  * i40e_fwd_add - configure macvlans
8027  * @netdev: net device to configure
8028  * @vdev: macvlan netdevice
8029  **/
8030 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
8031 {
8032 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8033 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
8034 	struct i40e_vsi *vsi = np->vsi;
8035 	struct i40e_pf *pf = vsi->back;
8036 	struct i40e_fwd_adapter *fwd;
8037 	int avail_macvlan, ret;
8038 
8039 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8040 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
8041 		return ERR_PTR(-EINVAL);
8042 	}
8043 	if (i40e_is_tc_mqprio_enabled(pf)) {
8044 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8045 		return ERR_PTR(-EINVAL);
8046 	}
8047 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8048 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8049 		return ERR_PTR(-EINVAL);
8050 	}
8051 
8052 	/* The macvlan device has to be a single Q device so that the
8053 	 * tc_to_txq field can be reused to pick the tx queue.
8054 	 */
8055 	if (netif_is_multiqueue(vdev))
8056 		return ERR_PTR(-ERANGE);
8057 
8058 	if (!vsi->macvlan_cnt) {
8059 		/* reserve bit 0 for the pf device */
8060 		set_bit(0, vsi->fwd_bitmask);
8061 
8062 		/* Try to reserve as many queues as possible for macvlans. First
8063 		 * reserve 3/4th of max vectors, then half, then quarter and
8064 		 * calculate Qs per macvlan as you go
8065 		 */
8066 		vectors = pf->num_lan_msix;
8067 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8068 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8069 			q_per_macvlan = 4;
8070 			macvlan_cnt = (vectors - 32) / 4;
8071 		} else if (vectors <= 64 && vectors > 32) {
8072 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8073 			q_per_macvlan = 2;
8074 			macvlan_cnt = (vectors - 16) / 2;
8075 		} else if (vectors <= 32 && vectors > 16) {
8076 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8077 			q_per_macvlan = 1;
8078 			macvlan_cnt = vectors - 16;
8079 		} else if (vectors <= 16 && vectors > 8) {
8080 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8081 			q_per_macvlan = 1;
8082 			macvlan_cnt = vectors - 8;
8083 		} else {
8084 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8085 			q_per_macvlan = 1;
8086 			macvlan_cnt = vectors - 1;
8087 		}
8088 
8089 		if (macvlan_cnt == 0)
8090 			return ERR_PTR(-EBUSY);
8091 
8092 		/* Quiesce VSI queues */
8093 		i40e_quiesce_vsi(vsi);
8094 
8095 		/* sets up the macvlans but does not "enable" them */
8096 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8097 					  vdev);
8098 		if (ret)
8099 			return ERR_PTR(ret);
8100 
8101 		/* Unquiesce VSI */
8102 		i40e_unquiesce_vsi(vsi);
8103 	}
8104 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8105 					    vsi->macvlan_cnt);
8106 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8107 		return ERR_PTR(-EBUSY);
8108 
8109 	/* create the fwd struct */
8110 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8111 	if (!fwd)
8112 		return ERR_PTR(-ENOMEM);
8113 
8114 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8115 	fwd->bit_no = avail_macvlan;
8116 	netdev_set_sb_channel(vdev, avail_macvlan);
8117 	fwd->netdev = vdev;
8118 
8119 	if (!netif_running(netdev))
8120 		return fwd;
8121 
8122 	/* Set fwd ring up */
8123 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8124 	if (ret) {
8125 		/* unbind the queues and drop the subordinate channel config */
8126 		netdev_unbind_sb_channel(netdev, vdev);
8127 		netdev_set_sb_channel(vdev, 0);
8128 
8129 		kfree(fwd);
8130 		return ERR_PTR(-EINVAL);
8131 	}
8132 
8133 	return fwd;
8134 }
8135 
8136 /**
8137  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8138  * @vsi: the VSI we want to access
8139  */
8140 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8141 {
8142 	struct i40e_channel *ch, *ch_tmp;
8143 	struct i40e_pf *pf = vsi->back;
8144 	struct i40e_hw *hw = &pf->hw;
8145 	int aq_err, ret = 0;
8146 
8147 	if (list_empty(&vsi->macvlan_list))
8148 		return;
8149 
8150 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8151 		if (i40e_is_channel_macvlan(ch)) {
8152 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8153 						      i40e_channel_mac(ch),
8154 						      &aq_err);
8155 			if (!ret) {
8156 				/* Reset queue contexts */
8157 				i40e_reset_ch_rings(vsi, ch);
8158 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8159 				netdev_unbind_sb_channel(vsi->netdev,
8160 							 ch->fwd->netdev);
8161 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8162 				kfree(ch->fwd);
8163 				ch->fwd = NULL;
8164 			}
8165 		}
8166 	}
8167 }
8168 
8169 /**
8170  * i40e_fwd_del - delete macvlan interfaces
8171  * @netdev: net device to configure
8172  * @vdev: macvlan netdevice
8173  */
8174 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8175 {
8176 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8177 	struct i40e_fwd_adapter *fwd = vdev;
8178 	struct i40e_channel *ch, *ch_tmp;
8179 	struct i40e_vsi *vsi = np->vsi;
8180 	struct i40e_pf *pf = vsi->back;
8181 	struct i40e_hw *hw = &pf->hw;
8182 	int aq_err, ret = 0;
8183 
8184 	/* Find the channel associated with the macvlan and del mac filter */
8185 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8186 		if (i40e_is_channel_macvlan(ch) &&
8187 		    ether_addr_equal(i40e_channel_mac(ch),
8188 				     fwd->netdev->dev_addr)) {
8189 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8190 						      i40e_channel_mac(ch),
8191 						      &aq_err);
8192 			if (!ret) {
8193 				/* Reset queue contexts */
8194 				i40e_reset_ch_rings(vsi, ch);
8195 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8196 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8197 				netdev_set_sb_channel(fwd->netdev, 0);
8198 				kfree(ch->fwd);
8199 				ch->fwd = NULL;
8200 			} else {
8201 				dev_info(&pf->pdev->dev,
8202 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8203 					  ERR_PTR(ret),
8204 					  i40e_aq_str(hw, aq_err));
8205 			}
8206 			break;
8207 		}
8208 	}
8209 }
8210 
8211 /**
8212  * i40e_setup_tc - configure multiple traffic classes
8213  * @netdev: net device to configure
8214  * @type_data: tc offload data
8215  **/
8216 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8217 {
8218 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8219 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8220 	struct i40e_vsi *vsi = np->vsi;
8221 	struct i40e_pf *pf = vsi->back;
8222 	u8 enabled_tc = 0, num_tc, hw;
8223 	bool need_reset = false;
8224 	int old_queue_pairs;
8225 	int ret = -EINVAL;
8226 	u16 mode;
8227 	int i;
8228 
8229 	old_queue_pairs = vsi->num_queue_pairs;
8230 	num_tc = mqprio_qopt->qopt.num_tc;
8231 	hw = mqprio_qopt->qopt.hw;
8232 	mode = mqprio_qopt->mode;
8233 	if (!hw) {
8234 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8235 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8236 		goto config_tc;
8237 	}
8238 
8239 	/* Check if MFP enabled */
8240 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8241 		netdev_info(netdev,
8242 			    "Configuring TC not supported in MFP mode\n");
8243 		return ret;
8244 	}
8245 	switch (mode) {
8246 	case TC_MQPRIO_MODE_DCB:
8247 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8248 
8249 		/* Check if DCB enabled to continue */
8250 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8251 			netdev_info(netdev,
8252 				    "DCB is not enabled for adapter\n");
8253 			return ret;
8254 		}
8255 
8256 		/* Check whether tc count is within enabled limit */
8257 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8258 			netdev_info(netdev,
8259 				    "TC count greater than enabled on link for adapter\n");
8260 			return ret;
8261 		}
8262 		break;
8263 	case TC_MQPRIO_MODE_CHANNEL:
8264 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8265 			netdev_info(netdev,
8266 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8267 			return ret;
8268 		}
8269 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8270 			return ret;
8271 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8272 		if (ret)
8273 			return ret;
8274 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8275 		       sizeof(*mqprio_qopt));
8276 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8277 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8278 		break;
8279 	default:
8280 		return -EINVAL;
8281 	}
8282 
8283 config_tc:
8284 	/* Generate TC map for number of tc requested */
8285 	for (i = 0; i < num_tc; i++)
8286 		enabled_tc |= BIT(i);
8287 
8288 	/* Requesting same TC configuration as already enabled */
8289 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8290 	    mode != TC_MQPRIO_MODE_CHANNEL)
8291 		return 0;
8292 
8293 	/* Quiesce VSI queues */
8294 	i40e_quiesce_vsi(vsi);
8295 
8296 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8297 		i40e_remove_queue_channels(vsi);
8298 
8299 	/* Configure VSI for enabled TCs */
8300 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8301 	if (ret) {
8302 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8303 			    vsi->seid);
8304 		need_reset = true;
8305 		goto exit;
8306 	} else if (enabled_tc &&
8307 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8308 		netdev_info(netdev,
8309 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8310 			    vsi->tc_config.tc_info[0].qcount);
8311 		ret = -EINVAL;
8312 		need_reset = true;
8313 		goto exit;
8314 	}
8315 
8316 	dev_info(&vsi->back->pdev->dev,
8317 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8318 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8319 
8320 	if (i40e_is_tc_mqprio_enabled(pf)) {
8321 		if (vsi->mqprio_qopt.max_rate[0]) {
8322 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8323 						  vsi->mqprio_qopt.max_rate[0]);
8324 
8325 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8326 			if (!ret) {
8327 				u64 credits = max_tx_rate;
8328 
8329 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8330 				dev_dbg(&vsi->back->pdev->dev,
8331 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8332 					max_tx_rate,
8333 					credits,
8334 					vsi->seid);
8335 			} else {
8336 				need_reset = true;
8337 				goto exit;
8338 			}
8339 		}
8340 		ret = i40e_configure_queue_channels(vsi);
8341 		if (ret) {
8342 			vsi->num_queue_pairs = old_queue_pairs;
8343 			netdev_info(netdev,
8344 				    "Failed configuring queue channels\n");
8345 			need_reset = true;
8346 			goto exit;
8347 		}
8348 	}
8349 
8350 exit:
8351 	/* Reset the configuration data to defaults, only TC0 is enabled */
8352 	if (need_reset) {
8353 		i40e_vsi_set_default_tc_config(vsi);
8354 		need_reset = false;
8355 	}
8356 
8357 	/* Unquiesce VSI */
8358 	i40e_unquiesce_vsi(vsi);
8359 	return ret;
8360 }
8361 
8362 /**
8363  * i40e_set_cld_element - sets cloud filter element data
8364  * @filter: cloud filter rule
8365  * @cld: ptr to cloud filter element data
8366  *
8367  * This is helper function to copy data into cloud filter element
8368  **/
8369 static inline void
8370 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8371 		     struct i40e_aqc_cloud_filters_element_data *cld)
8372 {
8373 	u32 ipa;
8374 	int i;
8375 
8376 	memset(cld, 0, sizeof(*cld));
8377 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8378 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8379 
8380 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8381 		return;
8382 
8383 	if (filter->n_proto == ETH_P_IPV6) {
8384 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8385 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8386 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8387 
8388 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8389 		}
8390 	} else {
8391 		ipa = be32_to_cpu(filter->dst_ipv4);
8392 
8393 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8394 	}
8395 
8396 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8397 
8398 	/* tenant_id is not supported by FW now, once the support is enabled
8399 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8400 	 */
8401 	if (filter->tenant_id)
8402 		return;
8403 }
8404 
8405 /**
8406  * i40e_add_del_cloud_filter - Add/del cloud filter
8407  * @vsi: pointer to VSI
8408  * @filter: cloud filter rule
8409  * @add: if true, add, if false, delete
8410  *
8411  * Add or delete a cloud filter for a specific flow spec.
8412  * Returns 0 if the filter were successfully added.
8413  **/
8414 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8415 			      struct i40e_cloud_filter *filter, bool add)
8416 {
8417 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8418 	struct i40e_pf *pf = vsi->back;
8419 	int ret;
8420 	static const u16 flag_table[128] = {
8421 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8422 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8423 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8424 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8425 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8426 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8427 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8428 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8429 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8430 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8431 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8432 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8433 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8434 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8435 	};
8436 
8437 	if (filter->flags >= ARRAY_SIZE(flag_table))
8438 		return -EIO;
8439 
8440 	memset(&cld_filter, 0, sizeof(cld_filter));
8441 
8442 	/* copy element needed to add cloud filter from filter */
8443 	i40e_set_cld_element(filter, &cld_filter);
8444 
8445 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8446 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8447 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8448 
8449 	if (filter->n_proto == ETH_P_IPV6)
8450 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8451 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8452 	else
8453 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8454 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8455 
8456 	if (add)
8457 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8458 						&cld_filter, 1);
8459 	else
8460 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8461 						&cld_filter, 1);
8462 	if (ret)
8463 		dev_dbg(&pf->pdev->dev,
8464 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8465 			add ? "add" : "delete", filter->dst_port, ret,
8466 			pf->hw.aq.asq_last_status);
8467 	else
8468 		dev_info(&pf->pdev->dev,
8469 			 "%s cloud filter for VSI: %d\n",
8470 			 add ? "Added" : "Deleted", filter->seid);
8471 	return ret;
8472 }
8473 
8474 /**
8475  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8476  * @vsi: pointer to VSI
8477  * @filter: cloud filter rule
8478  * @add: if true, add, if false, delete
8479  *
8480  * Add or delete a cloud filter for a specific flow spec using big buffer.
8481  * Returns 0 if the filter were successfully added.
8482  **/
8483 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8484 				      struct i40e_cloud_filter *filter,
8485 				      bool add)
8486 {
8487 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8488 	struct i40e_pf *pf = vsi->back;
8489 	int ret;
8490 
8491 	/* Both (src/dst) valid mac_addr are not supported */
8492 	if ((is_valid_ether_addr(filter->dst_mac) &&
8493 	     is_valid_ether_addr(filter->src_mac)) ||
8494 	    (is_multicast_ether_addr(filter->dst_mac) &&
8495 	     is_multicast_ether_addr(filter->src_mac)))
8496 		return -EOPNOTSUPP;
8497 
8498 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8499 	 * ports are not supported via big buffer now.
8500 	 */
8501 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8502 		return -EOPNOTSUPP;
8503 
8504 	/* adding filter using src_port/src_ip is not supported at this stage */
8505 	if (filter->src_port ||
8506 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8507 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8508 		return -EOPNOTSUPP;
8509 
8510 	memset(&cld_filter, 0, sizeof(cld_filter));
8511 
8512 	/* copy element needed to add cloud filter from filter */
8513 	i40e_set_cld_element(filter, &cld_filter.element);
8514 
8515 	if (is_valid_ether_addr(filter->dst_mac) ||
8516 	    is_valid_ether_addr(filter->src_mac) ||
8517 	    is_multicast_ether_addr(filter->dst_mac) ||
8518 	    is_multicast_ether_addr(filter->src_mac)) {
8519 		/* MAC + IP : unsupported mode */
8520 		if (filter->dst_ipv4)
8521 			return -EOPNOTSUPP;
8522 
8523 		/* since we validated that L4 port must be valid before
8524 		 * we get here, start with respective "flags" value
8525 		 * and update if vlan is present or not
8526 		 */
8527 		cld_filter.element.flags =
8528 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8529 
8530 		if (filter->vlan_id) {
8531 			cld_filter.element.flags =
8532 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8533 		}
8534 
8535 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8536 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8537 		cld_filter.element.flags =
8538 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8539 		if (filter->n_proto == ETH_P_IPV6)
8540 			cld_filter.element.flags |=
8541 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8542 		else
8543 			cld_filter.element.flags |=
8544 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8545 	} else {
8546 		dev_err(&pf->pdev->dev,
8547 			"either mac or ip has to be valid for cloud filter\n");
8548 		return -EINVAL;
8549 	}
8550 
8551 	/* Now copy L4 port in Byte 6..7 in general fields */
8552 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8553 						be16_to_cpu(filter->dst_port);
8554 
8555 	if (add) {
8556 		/* Validate current device switch mode, change if necessary */
8557 		ret = i40e_validate_and_set_switch_mode(vsi);
8558 		if (ret) {
8559 			dev_err(&pf->pdev->dev,
8560 				"failed to set switch mode, ret %d\n",
8561 				ret);
8562 			return ret;
8563 		}
8564 
8565 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8566 						   &cld_filter, 1);
8567 	} else {
8568 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8569 						   &cld_filter, 1);
8570 	}
8571 
8572 	if (ret)
8573 		dev_dbg(&pf->pdev->dev,
8574 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8575 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8576 	else
8577 		dev_info(&pf->pdev->dev,
8578 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8579 			 add ? "add" : "delete", filter->seid,
8580 			 ntohs(filter->dst_port));
8581 	return ret;
8582 }
8583 
8584 /**
8585  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8586  * @vsi: Pointer to VSI
8587  * @f: Pointer to struct flow_cls_offload
8588  * @filter: Pointer to cloud filter structure
8589  *
8590  **/
8591 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8592 				 struct flow_cls_offload *f,
8593 				 struct i40e_cloud_filter *filter)
8594 {
8595 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8596 	struct flow_dissector *dissector = rule->match.dissector;
8597 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8598 	struct i40e_pf *pf = vsi->back;
8599 	u8 field_flags = 0;
8600 
8601 	if (dissector->used_keys &
8602 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8603 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8604 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8605 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8606 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8607 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8608 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8609 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8610 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8611 			dissector->used_keys);
8612 		return -EOPNOTSUPP;
8613 	}
8614 
8615 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8616 		struct flow_match_enc_keyid match;
8617 
8618 		flow_rule_match_enc_keyid(rule, &match);
8619 		if (match.mask->keyid != 0)
8620 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8621 
8622 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8623 	}
8624 
8625 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8626 		struct flow_match_basic match;
8627 
8628 		flow_rule_match_basic(rule, &match);
8629 		n_proto_key = ntohs(match.key->n_proto);
8630 		n_proto_mask = ntohs(match.mask->n_proto);
8631 
8632 		if (n_proto_key == ETH_P_ALL) {
8633 			n_proto_key = 0;
8634 			n_proto_mask = 0;
8635 		}
8636 		filter->n_proto = n_proto_key & n_proto_mask;
8637 		filter->ip_proto = match.key->ip_proto;
8638 	}
8639 
8640 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8641 		struct flow_match_eth_addrs match;
8642 
8643 		flow_rule_match_eth_addrs(rule, &match);
8644 
8645 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8646 		if (!is_zero_ether_addr(match.mask->dst)) {
8647 			if (is_broadcast_ether_addr(match.mask->dst)) {
8648 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8649 			} else {
8650 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8651 					match.mask->dst);
8652 				return -EIO;
8653 			}
8654 		}
8655 
8656 		if (!is_zero_ether_addr(match.mask->src)) {
8657 			if (is_broadcast_ether_addr(match.mask->src)) {
8658 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8659 			} else {
8660 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8661 					match.mask->src);
8662 				return -EIO;
8663 			}
8664 		}
8665 		ether_addr_copy(filter->dst_mac, match.key->dst);
8666 		ether_addr_copy(filter->src_mac, match.key->src);
8667 	}
8668 
8669 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8670 		struct flow_match_vlan match;
8671 
8672 		flow_rule_match_vlan(rule, &match);
8673 		if (match.mask->vlan_id) {
8674 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8675 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8676 
8677 			} else {
8678 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8679 					match.mask->vlan_id);
8680 				return -EIO;
8681 			}
8682 		}
8683 
8684 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8685 	}
8686 
8687 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8688 		struct flow_match_control match;
8689 
8690 		flow_rule_match_control(rule, &match);
8691 		addr_type = match.key->addr_type;
8692 
8693 		if (flow_rule_has_control_flags(match.mask->flags,
8694 						f->common.extack))
8695 			return -EOPNOTSUPP;
8696 	}
8697 
8698 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8699 		struct flow_match_ipv4_addrs match;
8700 
8701 		flow_rule_match_ipv4_addrs(rule, &match);
8702 		if (match.mask->dst) {
8703 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8704 				field_flags |= I40E_CLOUD_FIELD_IIP;
8705 			} else {
8706 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8707 					&match.mask->dst);
8708 				return -EIO;
8709 			}
8710 		}
8711 
8712 		if (match.mask->src) {
8713 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8714 				field_flags |= I40E_CLOUD_FIELD_IIP;
8715 			} else {
8716 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8717 					&match.mask->src);
8718 				return -EIO;
8719 			}
8720 		}
8721 
8722 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8723 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8724 			return -EIO;
8725 		}
8726 		filter->dst_ipv4 = match.key->dst;
8727 		filter->src_ipv4 = match.key->src;
8728 	}
8729 
8730 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8731 		struct flow_match_ipv6_addrs match;
8732 
8733 		flow_rule_match_ipv6_addrs(rule, &match);
8734 
8735 		/* src and dest IPV6 address should not be LOOPBACK
8736 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8737 		 */
8738 		if (ipv6_addr_loopback(&match.key->dst) ||
8739 		    ipv6_addr_loopback(&match.key->src)) {
8740 			dev_err(&pf->pdev->dev,
8741 				"Bad ipv6, addr is LOOPBACK\n");
8742 			return -EIO;
8743 		}
8744 		if (!ipv6_addr_any(&match.mask->dst) ||
8745 		    !ipv6_addr_any(&match.mask->src))
8746 			field_flags |= I40E_CLOUD_FIELD_IIP;
8747 
8748 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8749 		       sizeof(filter->src_ipv6));
8750 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8751 		       sizeof(filter->dst_ipv6));
8752 	}
8753 
8754 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8755 		struct flow_match_ports match;
8756 
8757 		flow_rule_match_ports(rule, &match);
8758 		if (match.mask->src) {
8759 			if (match.mask->src == cpu_to_be16(0xffff)) {
8760 				field_flags |= I40E_CLOUD_FIELD_IIP;
8761 			} else {
8762 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8763 					be16_to_cpu(match.mask->src));
8764 				return -EIO;
8765 			}
8766 		}
8767 
8768 		if (match.mask->dst) {
8769 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8770 				field_flags |= I40E_CLOUD_FIELD_IIP;
8771 			} else {
8772 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8773 					be16_to_cpu(match.mask->dst));
8774 				return -EIO;
8775 			}
8776 		}
8777 
8778 		filter->dst_port = match.key->dst;
8779 		filter->src_port = match.key->src;
8780 
8781 		switch (filter->ip_proto) {
8782 		case IPPROTO_TCP:
8783 		case IPPROTO_UDP:
8784 			break;
8785 		default:
8786 			dev_err(&pf->pdev->dev,
8787 				"Only UDP and TCP transport are supported\n");
8788 			return -EINVAL;
8789 		}
8790 	}
8791 	filter->flags = field_flags;
8792 	return 0;
8793 }
8794 
8795 /**
8796  * i40e_handle_tclass: Forward to a traffic class on the device
8797  * @vsi: Pointer to VSI
8798  * @tc: traffic class index on the device
8799  * @filter: Pointer to cloud filter structure
8800  *
8801  **/
8802 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8803 			      struct i40e_cloud_filter *filter)
8804 {
8805 	struct i40e_channel *ch, *ch_tmp;
8806 
8807 	/* direct to a traffic class on the same device */
8808 	if (tc == 0) {
8809 		filter->seid = vsi->seid;
8810 		return 0;
8811 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8812 		if (!filter->dst_port) {
8813 			dev_err(&vsi->back->pdev->dev,
8814 				"Specify destination port to direct to traffic class that is not default\n");
8815 			return -EINVAL;
8816 		}
8817 		if (list_empty(&vsi->ch_list))
8818 			return -EINVAL;
8819 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8820 					 list) {
8821 			if (ch->seid == vsi->tc_seid_map[tc])
8822 				filter->seid = ch->seid;
8823 		}
8824 		return 0;
8825 	}
8826 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8827 	return -EINVAL;
8828 }
8829 
8830 /**
8831  * i40e_configure_clsflower - Configure tc flower filters
8832  * @vsi: Pointer to VSI
8833  * @cls_flower: Pointer to struct flow_cls_offload
8834  *
8835  **/
8836 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8837 				    struct flow_cls_offload *cls_flower)
8838 {
8839 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8840 	struct i40e_cloud_filter *filter = NULL;
8841 	struct i40e_pf *pf = vsi->back;
8842 	int err = 0;
8843 
8844 	if (tc < 0) {
8845 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8846 		return -EOPNOTSUPP;
8847 	}
8848 
8849 	if (!tc) {
8850 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8851 		return -EINVAL;
8852 	}
8853 
8854 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8855 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8856 		return -EBUSY;
8857 
8858 	if (pf->fdir_pf_active_filters ||
8859 	    (!hlist_empty(&pf->fdir_filter_list))) {
8860 		dev_err(&vsi->back->pdev->dev,
8861 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8862 		return -EINVAL;
8863 	}
8864 
8865 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8866 		dev_err(&vsi->back->pdev->dev,
8867 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8868 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8869 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8870 	}
8871 
8872 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8873 	if (!filter)
8874 		return -ENOMEM;
8875 
8876 	filter->cookie = cls_flower->cookie;
8877 
8878 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8879 	if (err < 0)
8880 		goto err;
8881 
8882 	err = i40e_handle_tclass(vsi, tc, filter);
8883 	if (err < 0)
8884 		goto err;
8885 
8886 	/* Add cloud filter */
8887 	if (filter->dst_port)
8888 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8889 	else
8890 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8891 
8892 	if (err) {
8893 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8894 			err);
8895 		goto err;
8896 	}
8897 
8898 	/* add filter to the ordered list */
8899 	INIT_HLIST_NODE(&filter->cloud_node);
8900 
8901 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8902 
8903 	pf->num_cloud_filters++;
8904 
8905 	return err;
8906 err:
8907 	kfree(filter);
8908 	return err;
8909 }
8910 
8911 /**
8912  * i40e_find_cloud_filter - Find the could filter in the list
8913  * @vsi: Pointer to VSI
8914  * @cookie: filter specific cookie
8915  *
8916  **/
8917 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8918 							unsigned long *cookie)
8919 {
8920 	struct i40e_cloud_filter *filter = NULL;
8921 	struct hlist_node *node2;
8922 
8923 	hlist_for_each_entry_safe(filter, node2,
8924 				  &vsi->back->cloud_filter_list, cloud_node)
8925 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8926 			return filter;
8927 	return NULL;
8928 }
8929 
8930 /**
8931  * i40e_delete_clsflower - Remove tc flower filters
8932  * @vsi: Pointer to VSI
8933  * @cls_flower: Pointer to struct flow_cls_offload
8934  *
8935  **/
8936 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8937 				 struct flow_cls_offload *cls_flower)
8938 {
8939 	struct i40e_cloud_filter *filter = NULL;
8940 	struct i40e_pf *pf = vsi->back;
8941 	int err = 0;
8942 
8943 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8944 
8945 	if (!filter)
8946 		return -EINVAL;
8947 
8948 	hash_del(&filter->cloud_node);
8949 
8950 	if (filter->dst_port)
8951 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8952 	else
8953 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8954 
8955 	kfree(filter);
8956 	if (err) {
8957 		dev_err(&pf->pdev->dev,
8958 			"Failed to delete cloud filter, err %pe\n",
8959 			ERR_PTR(err));
8960 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8961 	}
8962 
8963 	pf->num_cloud_filters--;
8964 	if (!pf->num_cloud_filters)
8965 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8966 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8967 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8968 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8969 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8970 		}
8971 	return 0;
8972 }
8973 
8974 /**
8975  * i40e_setup_tc_cls_flower - flower classifier offloads
8976  * @np: net device to configure
8977  * @cls_flower: offload data
8978  **/
8979 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8980 				    struct flow_cls_offload *cls_flower)
8981 {
8982 	struct i40e_vsi *vsi = np->vsi;
8983 
8984 	switch (cls_flower->command) {
8985 	case FLOW_CLS_REPLACE:
8986 		return i40e_configure_clsflower(vsi, cls_flower);
8987 	case FLOW_CLS_DESTROY:
8988 		return i40e_delete_clsflower(vsi, cls_flower);
8989 	case FLOW_CLS_STATS:
8990 		return -EOPNOTSUPP;
8991 	default:
8992 		return -EOPNOTSUPP;
8993 	}
8994 }
8995 
8996 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8997 				  void *cb_priv)
8998 {
8999 	struct i40e_netdev_priv *np = cb_priv;
9000 
9001 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
9002 		return -EOPNOTSUPP;
9003 
9004 	switch (type) {
9005 	case TC_SETUP_CLSFLOWER:
9006 		return i40e_setup_tc_cls_flower(np, type_data);
9007 
9008 	default:
9009 		return -EOPNOTSUPP;
9010 	}
9011 }
9012 
9013 static LIST_HEAD(i40e_block_cb_list);
9014 
9015 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
9016 			   void *type_data)
9017 {
9018 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9019 
9020 	switch (type) {
9021 	case TC_SETUP_QDISC_MQPRIO:
9022 		return i40e_setup_tc(netdev, type_data);
9023 	case TC_SETUP_BLOCK:
9024 		return flow_block_cb_setup_simple(type_data,
9025 						  &i40e_block_cb_list,
9026 						  i40e_setup_tc_block_cb,
9027 						  np, np, true);
9028 	default:
9029 		return -EOPNOTSUPP;
9030 	}
9031 }
9032 
9033 /**
9034  * i40e_open - Called when a network interface is made active
9035  * @netdev: network interface device structure
9036  *
9037  * The open entry point is called when a network interface is made
9038  * active by the system (IFF_UP).  At this point all resources needed
9039  * for transmit and receive operations are allocated, the interrupt
9040  * handler is registered with the OS, the netdev watchdog subtask is
9041  * enabled, and the stack is notified that the interface is ready.
9042  *
9043  * Returns 0 on success, negative value on failure
9044  **/
9045 int i40e_open(struct net_device *netdev)
9046 {
9047 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9048 	struct i40e_vsi *vsi = np->vsi;
9049 	struct i40e_pf *pf = vsi->back;
9050 	int err;
9051 
9052 	/* disallow open during test or if eeprom is broken */
9053 	if (test_bit(__I40E_TESTING, pf->state) ||
9054 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9055 		return -EBUSY;
9056 
9057 	netif_carrier_off(netdev);
9058 
9059 	if (i40e_force_link_state(pf, true))
9060 		return -EAGAIN;
9061 
9062 	err = i40e_vsi_open(vsi);
9063 	if (err)
9064 		return err;
9065 
9066 	/* configure global TSO hardware offload settings */
9067 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9068 						       TCP_FLAG_FIN) >> 16);
9069 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9070 						       TCP_FLAG_FIN |
9071 						       TCP_FLAG_CWR) >> 16);
9072 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9073 	udp_tunnel_get_rx_info(netdev);
9074 
9075 	return 0;
9076 }
9077 
9078 /**
9079  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9080  * @vsi: vsi structure
9081  *
9082  * This updates netdev's number of tx/rx queues
9083  *
9084  * Returns status of setting tx/rx queues
9085  **/
9086 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9087 {
9088 	int ret;
9089 
9090 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9091 					   vsi->num_queue_pairs);
9092 	if (ret)
9093 		return ret;
9094 
9095 	return netif_set_real_num_tx_queues(vsi->netdev,
9096 					    vsi->num_queue_pairs);
9097 }
9098 
9099 /**
9100  * i40e_vsi_open -
9101  * @vsi: the VSI to open
9102  *
9103  * Finish initialization of the VSI.
9104  *
9105  * Returns 0 on success, negative value on failure
9106  *
9107  * Note: expects to be called while under rtnl_lock()
9108  **/
9109 int i40e_vsi_open(struct i40e_vsi *vsi)
9110 {
9111 	struct i40e_pf *pf = vsi->back;
9112 	char int_name[I40E_INT_NAME_STR_LEN];
9113 	int err;
9114 
9115 	/* allocate descriptors */
9116 	err = i40e_vsi_setup_tx_resources(vsi);
9117 	if (err)
9118 		goto err_setup_tx;
9119 	err = i40e_vsi_setup_rx_resources(vsi);
9120 	if (err)
9121 		goto err_setup_rx;
9122 
9123 	err = i40e_vsi_configure(vsi);
9124 	if (err)
9125 		goto err_setup_rx;
9126 
9127 	if (vsi->netdev) {
9128 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9129 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9130 		err = i40e_vsi_request_irq(vsi, int_name);
9131 		if (err)
9132 			goto err_setup_rx;
9133 
9134 		/* Notify the stack of the actual queue counts. */
9135 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9136 		if (err)
9137 			goto err_set_queues;
9138 
9139 	} else if (vsi->type == I40E_VSI_FDIR) {
9140 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9141 			 dev_driver_string(&pf->pdev->dev),
9142 			 dev_name(&pf->pdev->dev));
9143 		err = i40e_vsi_request_irq(vsi, int_name);
9144 		if (err)
9145 			goto err_setup_rx;
9146 
9147 	} else {
9148 		err = -EINVAL;
9149 		goto err_setup_rx;
9150 	}
9151 
9152 	err = i40e_up_complete(vsi);
9153 	if (err)
9154 		goto err_up_complete;
9155 
9156 	return 0;
9157 
9158 err_up_complete:
9159 	i40e_down(vsi);
9160 err_set_queues:
9161 	i40e_vsi_free_irq(vsi);
9162 err_setup_rx:
9163 	i40e_vsi_free_rx_resources(vsi);
9164 err_setup_tx:
9165 	i40e_vsi_free_tx_resources(vsi);
9166 	if (vsi->type == I40E_VSI_MAIN)
9167 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9168 
9169 	return err;
9170 }
9171 
9172 /**
9173  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9174  * @pf: Pointer to PF
9175  *
9176  * This function destroys the hlist where all the Flow Director
9177  * filters were saved.
9178  **/
9179 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9180 {
9181 	struct i40e_fdir_filter *filter;
9182 	struct i40e_flex_pit *pit_entry, *tmp;
9183 	struct hlist_node *node2;
9184 
9185 	hlist_for_each_entry_safe(filter, node2,
9186 				  &pf->fdir_filter_list, fdir_node) {
9187 		hlist_del(&filter->fdir_node);
9188 		kfree(filter);
9189 	}
9190 
9191 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9192 		list_del(&pit_entry->list);
9193 		kfree(pit_entry);
9194 	}
9195 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9196 
9197 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9198 		list_del(&pit_entry->list);
9199 		kfree(pit_entry);
9200 	}
9201 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9202 
9203 	pf->fdir_pf_active_filters = 0;
9204 	i40e_reset_fdir_filter_cnt(pf);
9205 
9206 	/* Reprogram the default input set for TCP/IPv4 */
9207 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9208 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9209 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9210 
9211 	/* Reprogram the default input set for TCP/IPv6 */
9212 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9213 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9214 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9215 
9216 	/* Reprogram the default input set for UDP/IPv4 */
9217 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9218 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9219 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9220 
9221 	/* Reprogram the default input set for UDP/IPv6 */
9222 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9223 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9224 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9225 
9226 	/* Reprogram the default input set for SCTP/IPv4 */
9227 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9228 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9229 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9230 
9231 	/* Reprogram the default input set for SCTP/IPv6 */
9232 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9233 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9234 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9235 
9236 	/* Reprogram the default input set for Other/IPv4 */
9237 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9238 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9239 
9240 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9241 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9242 
9243 	/* Reprogram the default input set for Other/IPv6 */
9244 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9245 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9246 
9247 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9248 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9249 }
9250 
9251 /**
9252  * i40e_cloud_filter_exit - Cleans up the cloud filters
9253  * @pf: Pointer to PF
9254  *
9255  * This function destroys the hlist where all the cloud filters
9256  * were saved.
9257  **/
9258 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9259 {
9260 	struct i40e_cloud_filter *cfilter;
9261 	struct hlist_node *node;
9262 
9263 	hlist_for_each_entry_safe(cfilter, node,
9264 				  &pf->cloud_filter_list, cloud_node) {
9265 		hlist_del(&cfilter->cloud_node);
9266 		kfree(cfilter);
9267 	}
9268 	pf->num_cloud_filters = 0;
9269 
9270 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9271 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9272 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9273 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9274 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9275 	}
9276 }
9277 
9278 /**
9279  * i40e_close - Disables a network interface
9280  * @netdev: network interface device structure
9281  *
9282  * The close entry point is called when an interface is de-activated
9283  * by the OS.  The hardware is still under the driver's control, but
9284  * this netdev interface is disabled.
9285  *
9286  * Returns 0, this is not allowed to fail
9287  **/
9288 int i40e_close(struct net_device *netdev)
9289 {
9290 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9291 	struct i40e_vsi *vsi = np->vsi;
9292 
9293 	i40e_vsi_close(vsi);
9294 
9295 	return 0;
9296 }
9297 
9298 /**
9299  * i40e_do_reset - Start a PF or Core Reset sequence
9300  * @pf: board private structure
9301  * @reset_flags: which reset is requested
9302  * @lock_acquired: indicates whether or not the lock has been acquired
9303  * before this function was called.
9304  *
9305  * The essential difference in resets is that the PF Reset
9306  * doesn't clear the packet buffers, doesn't reset the PE
9307  * firmware, and doesn't bother the other PFs on the chip.
9308  **/
9309 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9310 {
9311 	struct i40e_vsi *vsi;
9312 	u32 val;
9313 	int i;
9314 
9315 	/* do the biggest reset indicated */
9316 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9317 
9318 		/* Request a Global Reset
9319 		 *
9320 		 * This will start the chip's countdown to the actual full
9321 		 * chip reset event, and a warning interrupt to be sent
9322 		 * to all PFs, including the requestor.  Our handler
9323 		 * for the warning interrupt will deal with the shutdown
9324 		 * and recovery of the switch setup.
9325 		 */
9326 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9327 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9328 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9329 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9330 
9331 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9332 
9333 		/* Request a Core Reset
9334 		 *
9335 		 * Same as Global Reset, except does *not* include the MAC/PHY
9336 		 */
9337 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9338 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9339 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9340 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9341 		i40e_flush(&pf->hw);
9342 
9343 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9344 
9345 		/* Request a PF Reset
9346 		 *
9347 		 * Resets only the PF-specific registers
9348 		 *
9349 		 * This goes directly to the tear-down and rebuild of
9350 		 * the switch, since we need to do all the recovery as
9351 		 * for the Core Reset.
9352 		 */
9353 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9354 		i40e_handle_reset_warning(pf, lock_acquired);
9355 
9356 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9357 		/* Request a PF Reset
9358 		 *
9359 		 * Resets PF and reinitializes PFs VSI.
9360 		 */
9361 		i40e_prep_for_reset(pf);
9362 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9363 		dev_info(&pf->pdev->dev,
9364 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9365 			 "FW LLDP is disabled\n" :
9366 			 "FW LLDP is enabled\n");
9367 
9368 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9369 		/* Find the VSI(s) that requested a re-init */
9370 		dev_info(&pf->pdev->dev, "VSI reinit requested\n");
9371 
9372 		i40e_pf_for_each_vsi(pf, i, vsi) {
9373 			if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9374 					       vsi->state))
9375 				i40e_vsi_reinit_locked(vsi);
9376 		}
9377 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9378 		/* Find the VSI(s) that needs to be brought down */
9379 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9380 
9381 		i40e_pf_for_each_vsi(pf, i, vsi) {
9382 			if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9383 					       vsi->state)) {
9384 				set_bit(__I40E_VSI_DOWN, vsi->state);
9385 				i40e_down(vsi);
9386 			}
9387 		}
9388 	} else {
9389 		dev_info(&pf->pdev->dev,
9390 			 "bad reset request 0x%08x\n", reset_flags);
9391 	}
9392 }
9393 
9394 #ifdef CONFIG_I40E_DCB
9395 /**
9396  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9397  * @pf: board private structure
9398  * @old_cfg: current DCB config
9399  * @new_cfg: new DCB config
9400  **/
9401 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9402 			    struct i40e_dcbx_config *old_cfg,
9403 			    struct i40e_dcbx_config *new_cfg)
9404 {
9405 	bool need_reconfig = false;
9406 
9407 	/* Check if ETS configuration has changed */
9408 	if (memcmp(&new_cfg->etscfg,
9409 		   &old_cfg->etscfg,
9410 		   sizeof(new_cfg->etscfg))) {
9411 		/* If Priority Table has changed reconfig is needed */
9412 		if (memcmp(&new_cfg->etscfg.prioritytable,
9413 			   &old_cfg->etscfg.prioritytable,
9414 			   sizeof(new_cfg->etscfg.prioritytable))) {
9415 			need_reconfig = true;
9416 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9417 		}
9418 
9419 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9420 			   &old_cfg->etscfg.tcbwtable,
9421 			   sizeof(new_cfg->etscfg.tcbwtable)))
9422 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9423 
9424 		if (memcmp(&new_cfg->etscfg.tsatable,
9425 			   &old_cfg->etscfg.tsatable,
9426 			   sizeof(new_cfg->etscfg.tsatable)))
9427 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9428 	}
9429 
9430 	/* Check if PFC configuration has changed */
9431 	if (memcmp(&new_cfg->pfc,
9432 		   &old_cfg->pfc,
9433 		   sizeof(new_cfg->pfc))) {
9434 		need_reconfig = true;
9435 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9436 	}
9437 
9438 	/* Check if APP Table has changed */
9439 	if (memcmp(&new_cfg->app,
9440 		   &old_cfg->app,
9441 		   sizeof(new_cfg->app))) {
9442 		need_reconfig = true;
9443 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9444 	}
9445 
9446 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9447 	return need_reconfig;
9448 }
9449 
9450 /**
9451  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9452  * @pf: board private structure
9453  * @e: event info posted on ARQ
9454  **/
9455 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9456 				  struct i40e_arq_event_info *e)
9457 {
9458 	struct i40e_aqc_lldp_get_mib *mib =
9459 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9460 	struct i40e_hw *hw = &pf->hw;
9461 	struct i40e_dcbx_config tmp_dcbx_cfg;
9462 	bool need_reconfig = false;
9463 	int ret = 0;
9464 	u8 type;
9465 
9466 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9467 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9468 	    (hw->phy.link_info.link_speed &
9469 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9470 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9471 		/* let firmware decide if the DCB should be disabled */
9472 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9473 
9474 	/* Not DCB capable or capability disabled */
9475 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9476 		return ret;
9477 
9478 	/* Ignore if event is not for Nearest Bridge */
9479 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9480 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9481 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9482 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9483 		return ret;
9484 
9485 	/* Check MIB Type and return if event for Remote MIB update */
9486 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9487 	dev_dbg(&pf->pdev->dev,
9488 		"LLDP event mib type %s\n", type ? "remote" : "local");
9489 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9490 		/* Update the remote cached instance and return */
9491 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9492 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9493 				&hw->remote_dcbx_config);
9494 		goto exit;
9495 	}
9496 
9497 	/* Store the old configuration */
9498 	tmp_dcbx_cfg = hw->local_dcbx_config;
9499 
9500 	/* Reset the old DCBx configuration data */
9501 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9502 	/* Get updated DCBX data from firmware */
9503 	ret = i40e_get_dcb_config(&pf->hw);
9504 	if (ret) {
9505 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9506 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9507 		    (hw->phy.link_info.link_speed &
9508 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9509 			dev_warn(&pf->pdev->dev,
9510 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9511 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9512 		} else {
9513 			dev_info(&pf->pdev->dev,
9514 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9515 				 ERR_PTR(ret),
9516 				 i40e_aq_str(&pf->hw,
9517 					     pf->hw.aq.asq_last_status));
9518 		}
9519 		goto exit;
9520 	}
9521 
9522 	/* No change detected in DCBX configs */
9523 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9524 		    sizeof(tmp_dcbx_cfg))) {
9525 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9526 		goto exit;
9527 	}
9528 
9529 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9530 					       &hw->local_dcbx_config);
9531 
9532 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9533 
9534 	if (!need_reconfig)
9535 		goto exit;
9536 
9537 	/* Enable DCB tagging only when more than one TC */
9538 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9539 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9540 	else
9541 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9542 
9543 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9544 	/* Reconfiguration needed quiesce all VSIs */
9545 	i40e_pf_quiesce_all_vsi(pf);
9546 
9547 	/* Changes in configuration update VEB/VSI */
9548 	i40e_dcb_reconfigure(pf);
9549 
9550 	ret = i40e_resume_port_tx(pf);
9551 
9552 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9553 	/* In case of error no point in resuming VSIs */
9554 	if (ret)
9555 		goto exit;
9556 
9557 	/* Wait for the PF's queues to be disabled */
9558 	ret = i40e_pf_wait_queues_disabled(pf);
9559 	if (ret) {
9560 		/* Schedule PF reset to recover */
9561 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9562 		i40e_service_event_schedule(pf);
9563 	} else {
9564 		i40e_pf_unquiesce_all_vsi(pf);
9565 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9566 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9567 	}
9568 
9569 exit:
9570 	return ret;
9571 }
9572 #endif /* CONFIG_I40E_DCB */
9573 
9574 /**
9575  * i40e_do_reset_safe - Protected reset path for userland calls.
9576  * @pf: board private structure
9577  * @reset_flags: which reset is requested
9578  *
9579  **/
9580 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9581 {
9582 	rtnl_lock();
9583 	i40e_do_reset(pf, reset_flags, true);
9584 	rtnl_unlock();
9585 }
9586 
9587 /**
9588  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9589  * @pf: board private structure
9590  * @e: event info posted on ARQ
9591  *
9592  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9593  * and VF queues
9594  **/
9595 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9596 					   struct i40e_arq_event_info *e)
9597 {
9598 	struct i40e_aqc_lan_overflow *data =
9599 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9600 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9601 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9602 	struct i40e_hw *hw = &pf->hw;
9603 	struct i40e_vf *vf;
9604 	u16 vf_id;
9605 
9606 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9607 		queue, qtx_ctl);
9608 
9609 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9610 	    I40E_QTX_CTL_VF_QUEUE)
9611 		return;
9612 
9613 	/* Queue belongs to VF, find the VF and issue VF reset */
9614 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9615 	vf_id -= hw->func_caps.vf_base_id;
9616 	vf = &pf->vf[vf_id];
9617 	i40e_vc_notify_vf_reset(vf);
9618 	/* Allow VF to process pending reset notification */
9619 	msleep(20);
9620 	i40e_reset_vf(vf, false);
9621 }
9622 
9623 /**
9624  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9625  * @pf: board private structure
9626  **/
9627 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9628 {
9629 	u32 val, fcnt_prog;
9630 
9631 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9632 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9633 	return fcnt_prog;
9634 }
9635 
9636 /**
9637  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9638  * @pf: board private structure
9639  **/
9640 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9641 {
9642 	u32 val, fcnt_prog;
9643 
9644 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9645 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9646 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9647 	return fcnt_prog;
9648 }
9649 
9650 /**
9651  * i40e_get_global_fd_count - Get total FD filters programmed on device
9652  * @pf: board private structure
9653  **/
9654 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9655 {
9656 	u32 val, fcnt_prog;
9657 
9658 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9659 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9660 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9661 	return fcnt_prog;
9662 }
9663 
9664 /**
9665  * i40e_reenable_fdir_sb - Restore FDir SB capability
9666  * @pf: board private structure
9667  **/
9668 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9669 {
9670 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9671 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9672 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9673 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9674 }
9675 
9676 /**
9677  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9678  * @pf: board private structure
9679  **/
9680 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9681 {
9682 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9683 		/* ATR uses the same filtering logic as SB rules. It only
9684 		 * functions properly if the input set mask is at the default
9685 		 * settings. It is safe to restore the default input set
9686 		 * because there are no active TCPv4 filter rules.
9687 		 */
9688 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9689 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9690 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9691 
9692 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9693 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9694 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9695 	}
9696 }
9697 
9698 /**
9699  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9700  * @pf: board private structure
9701  * @filter: FDir filter to remove
9702  */
9703 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9704 				       struct i40e_fdir_filter *filter)
9705 {
9706 	/* Update counters */
9707 	pf->fdir_pf_active_filters--;
9708 	pf->fd_inv = 0;
9709 
9710 	switch (filter->flow_type) {
9711 	case TCP_V4_FLOW:
9712 		pf->fd_tcp4_filter_cnt--;
9713 		break;
9714 	case UDP_V4_FLOW:
9715 		pf->fd_udp4_filter_cnt--;
9716 		break;
9717 	case SCTP_V4_FLOW:
9718 		pf->fd_sctp4_filter_cnt--;
9719 		break;
9720 	case TCP_V6_FLOW:
9721 		pf->fd_tcp6_filter_cnt--;
9722 		break;
9723 	case UDP_V6_FLOW:
9724 		pf->fd_udp6_filter_cnt--;
9725 		break;
9726 	case SCTP_V6_FLOW:
9727 		pf->fd_udp6_filter_cnt--;
9728 		break;
9729 	case IP_USER_FLOW:
9730 		switch (filter->ipl4_proto) {
9731 		case IPPROTO_TCP:
9732 			pf->fd_tcp4_filter_cnt--;
9733 			break;
9734 		case IPPROTO_UDP:
9735 			pf->fd_udp4_filter_cnt--;
9736 			break;
9737 		case IPPROTO_SCTP:
9738 			pf->fd_sctp4_filter_cnt--;
9739 			break;
9740 		case IPPROTO_IP:
9741 			pf->fd_ip4_filter_cnt--;
9742 			break;
9743 		}
9744 		break;
9745 	case IPV6_USER_FLOW:
9746 		switch (filter->ipl4_proto) {
9747 		case IPPROTO_TCP:
9748 			pf->fd_tcp6_filter_cnt--;
9749 			break;
9750 		case IPPROTO_UDP:
9751 			pf->fd_udp6_filter_cnt--;
9752 			break;
9753 		case IPPROTO_SCTP:
9754 			pf->fd_sctp6_filter_cnt--;
9755 			break;
9756 		case IPPROTO_IP:
9757 			pf->fd_ip6_filter_cnt--;
9758 			break;
9759 		}
9760 		break;
9761 	}
9762 
9763 	/* Remove the filter from the list and free memory */
9764 	hlist_del(&filter->fdir_node);
9765 	kfree(filter);
9766 }
9767 
9768 /**
9769  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9770  * @pf: board private structure
9771  **/
9772 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9773 {
9774 	struct i40e_fdir_filter *filter;
9775 	u32 fcnt_prog, fcnt_avail;
9776 	struct hlist_node *node;
9777 
9778 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9779 		return;
9780 
9781 	/* Check if we have enough room to re-enable FDir SB capability. */
9782 	fcnt_prog = i40e_get_global_fd_count(pf);
9783 	fcnt_avail = pf->fdir_pf_filter_count;
9784 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9785 	    (pf->fd_add_err == 0) ||
9786 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9787 		i40e_reenable_fdir_sb(pf);
9788 
9789 	/* We should wait for even more space before re-enabling ATR.
9790 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9791 	 * rules active.
9792 	 */
9793 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9794 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9795 		i40e_reenable_fdir_atr(pf);
9796 
9797 	/* if hw had a problem adding a filter, delete it */
9798 	if (pf->fd_inv > 0) {
9799 		hlist_for_each_entry_safe(filter, node,
9800 					  &pf->fdir_filter_list, fdir_node)
9801 			if (filter->fd_id == pf->fd_inv)
9802 				i40e_delete_invalid_filter(pf, filter);
9803 	}
9804 }
9805 
9806 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9807 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9808 /**
9809  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9810  * @pf: board private structure
9811  **/
9812 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9813 {
9814 	unsigned long min_flush_time;
9815 	int flush_wait_retry = 50;
9816 	bool disable_atr = false;
9817 	int fd_room;
9818 	int reg;
9819 
9820 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9821 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9822 		return;
9823 
9824 	/* If the flush is happening too quick and we have mostly SB rules we
9825 	 * should not re-enable ATR for some time.
9826 	 */
9827 	min_flush_time = pf->fd_flush_timestamp +
9828 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9829 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9830 
9831 	if (!(time_after(jiffies, min_flush_time)) &&
9832 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9833 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9834 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9835 		disable_atr = true;
9836 	}
9837 
9838 	pf->fd_flush_timestamp = jiffies;
9839 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9840 	/* flush all filters */
9841 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9842 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9843 	i40e_flush(&pf->hw);
9844 	pf->fd_flush_cnt++;
9845 	pf->fd_add_err = 0;
9846 	do {
9847 		/* Check FD flush status every 5-6msec */
9848 		usleep_range(5000, 6000);
9849 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9850 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9851 			break;
9852 	} while (flush_wait_retry--);
9853 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9854 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9855 	} else {
9856 		/* replay sideband filters */
9857 		i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
9858 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9859 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9860 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9861 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9862 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9863 	}
9864 }
9865 
9866 /**
9867  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9868  * @pf: board private structure
9869  **/
9870 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9871 {
9872 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9873 }
9874 
9875 /**
9876  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9877  * @pf: board private structure
9878  **/
9879 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9880 {
9881 
9882 	/* if interface is down do nothing */
9883 	if (test_bit(__I40E_DOWN, pf->state))
9884 		return;
9885 
9886 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9887 		i40e_fdir_flush_and_replay(pf);
9888 
9889 	i40e_fdir_check_and_reenable(pf);
9890 
9891 }
9892 
9893 /**
9894  * i40e_vsi_link_event - notify VSI of a link event
9895  * @vsi: vsi to be notified
9896  * @link_up: link up or down
9897  **/
9898 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9899 {
9900 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9901 		return;
9902 
9903 	switch (vsi->type) {
9904 	case I40E_VSI_MAIN:
9905 		if (!vsi->netdev || !vsi->netdev_registered)
9906 			break;
9907 
9908 		if (link_up) {
9909 			netif_carrier_on(vsi->netdev);
9910 			netif_tx_wake_all_queues(vsi->netdev);
9911 		} else {
9912 			netif_carrier_off(vsi->netdev);
9913 			netif_tx_stop_all_queues(vsi->netdev);
9914 		}
9915 		break;
9916 
9917 	case I40E_VSI_SRIOV:
9918 	case I40E_VSI_VMDQ2:
9919 	case I40E_VSI_CTRL:
9920 	case I40E_VSI_IWARP:
9921 	case I40E_VSI_MIRROR:
9922 	default:
9923 		/* there is no notification for other VSIs */
9924 		break;
9925 	}
9926 }
9927 
9928 /**
9929  * i40e_veb_link_event - notify elements on the veb of a link event
9930  * @veb: veb to be notified
9931  * @link_up: link up or down
9932  **/
9933 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9934 {
9935 	struct i40e_vsi *vsi;
9936 	struct i40e_pf *pf;
9937 	int i;
9938 
9939 	if (!veb || !veb->pf)
9940 		return;
9941 	pf = veb->pf;
9942 
9943 	/* Send link event to contained VSIs */
9944 	i40e_pf_for_each_vsi(pf, i, vsi)
9945 		if (vsi->uplink_seid == veb->seid)
9946 			i40e_vsi_link_event(vsi, link_up);
9947 }
9948 
9949 /**
9950  * i40e_link_event - Update netif_carrier status
9951  * @pf: board private structure
9952  **/
9953 static void i40e_link_event(struct i40e_pf *pf)
9954 {
9955 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
9956 	struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
9957 	u8 new_link_speed, old_link_speed;
9958 	bool new_link, old_link;
9959 	int status;
9960 #ifdef CONFIG_I40E_DCB
9961 	int err;
9962 #endif /* CONFIG_I40E_DCB */
9963 
9964 	/* set this to force the get_link_status call to refresh state */
9965 	pf->hw.phy.get_link_info = true;
9966 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9967 	status = i40e_get_link_status(&pf->hw, &new_link);
9968 
9969 	/* On success, disable temp link polling */
9970 	if (status == 0) {
9971 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9972 	} else {
9973 		/* Enable link polling temporarily until i40e_get_link_status
9974 		 * returns 0
9975 		 */
9976 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9977 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9978 			status);
9979 		return;
9980 	}
9981 
9982 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9983 	new_link_speed = pf->hw.phy.link_info.link_speed;
9984 
9985 	if (new_link == old_link &&
9986 	    new_link_speed == old_link_speed &&
9987 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9988 	     new_link == netif_carrier_ok(vsi->netdev)))
9989 		return;
9990 
9991 	i40e_print_link_message(vsi, new_link);
9992 
9993 	/* Notify the base of the switch tree connected to
9994 	 * the link.  Floating VEBs are not notified.
9995 	 */
9996 	if (veb)
9997 		i40e_veb_link_event(veb, new_link);
9998 	else
9999 		i40e_vsi_link_event(vsi, new_link);
10000 
10001 	if (pf->vf)
10002 		i40e_vc_notify_link_state(pf);
10003 
10004 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
10005 		i40e_ptp_set_increment(pf);
10006 #ifdef CONFIG_I40E_DCB
10007 	if (new_link == old_link)
10008 		return;
10009 	/* Not SW DCB so firmware will take care of default settings */
10010 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
10011 		return;
10012 
10013 	/* We cover here only link down, as after link up in case of SW DCB
10014 	 * SW LLDP agent will take care of setting it up
10015 	 */
10016 	if (!new_link) {
10017 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
10018 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
10019 		err = i40e_dcb_sw_default_config(pf);
10020 		if (err) {
10021 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10022 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
10023 		} else {
10024 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
10025 				       DCB_CAP_DCBX_VER_IEEE;
10026 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10027 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
10028 		}
10029 	}
10030 #endif /* CONFIG_I40E_DCB */
10031 }
10032 
10033 /**
10034  * i40e_watchdog_subtask - periodic checks not using event driven response
10035  * @pf: board private structure
10036  **/
10037 static void i40e_watchdog_subtask(struct i40e_pf *pf)
10038 {
10039 	struct i40e_vsi *vsi;
10040 	struct i40e_veb *veb;
10041 	int i;
10042 
10043 	/* if interface is down do nothing */
10044 	if (test_bit(__I40E_DOWN, pf->state) ||
10045 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
10046 		return;
10047 
10048 	/* make sure we don't do these things too often */
10049 	if (time_before(jiffies, (pf->service_timer_previous +
10050 				  pf->service_timer_period)))
10051 		return;
10052 	pf->service_timer_previous = jiffies;
10053 
10054 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10055 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10056 		i40e_link_event(pf);
10057 
10058 	/* Update the stats for active netdevs so the network stack
10059 	 * can look at updated numbers whenever it cares to
10060 	 */
10061 	i40e_pf_for_each_vsi(pf, i, vsi)
10062 		if (vsi->netdev)
10063 			i40e_update_stats(vsi);
10064 
10065 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10066 		/* Update the stats for the active switching components */
10067 		i40e_pf_for_each_veb(pf, i, veb)
10068 			i40e_update_veb_stats(veb);
10069 	}
10070 
10071 	i40e_ptp_rx_hang(pf);
10072 	i40e_ptp_tx_hang(pf);
10073 }
10074 
10075 /**
10076  * i40e_reset_subtask - Set up for resetting the device and driver
10077  * @pf: board private structure
10078  **/
10079 static void i40e_reset_subtask(struct i40e_pf *pf)
10080 {
10081 	u32 reset_flags = 0;
10082 
10083 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10084 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10085 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10086 	}
10087 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10088 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10089 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10090 	}
10091 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10092 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10093 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10094 	}
10095 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10096 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10097 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10098 	}
10099 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10100 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10101 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10102 	}
10103 
10104 	/* If there's a recovery already waiting, it takes
10105 	 * precedence before starting a new reset sequence.
10106 	 */
10107 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10108 		i40e_prep_for_reset(pf);
10109 		i40e_reset(pf);
10110 		i40e_rebuild(pf, false, false);
10111 	}
10112 
10113 	/* If we're already down or resetting, just bail */
10114 	if (reset_flags &&
10115 	    !test_bit(__I40E_DOWN, pf->state) &&
10116 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10117 		i40e_do_reset(pf, reset_flags, false);
10118 	}
10119 }
10120 
10121 /**
10122  * i40e_handle_link_event - Handle link event
10123  * @pf: board private structure
10124  * @e: event info posted on ARQ
10125  **/
10126 static void i40e_handle_link_event(struct i40e_pf *pf,
10127 				   struct i40e_arq_event_info *e)
10128 {
10129 	struct i40e_aqc_get_link_status *status =
10130 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10131 
10132 	/* Do a new status request to re-enable LSE reporting
10133 	 * and load new status information into the hw struct
10134 	 * This completely ignores any state information
10135 	 * in the ARQ event info, instead choosing to always
10136 	 * issue the AQ update link status command.
10137 	 */
10138 	i40e_link_event(pf);
10139 
10140 	/* Check if module meets thermal requirements */
10141 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10142 		dev_err(&pf->pdev->dev,
10143 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10144 		dev_err(&pf->pdev->dev,
10145 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10146 	} else {
10147 		/* check for unqualified module, if link is down, suppress
10148 		 * the message if link was forced to be down.
10149 		 */
10150 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10151 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10152 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10153 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10154 			dev_err(&pf->pdev->dev,
10155 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10156 			dev_err(&pf->pdev->dev,
10157 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10158 		}
10159 	}
10160 }
10161 
10162 /**
10163  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10164  * @pf: board private structure
10165  **/
10166 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10167 {
10168 	struct i40e_arq_event_info event;
10169 	struct i40e_hw *hw = &pf->hw;
10170 	u16 pending, i = 0;
10171 	u16 opcode;
10172 	u32 oldval;
10173 	int ret;
10174 	u32 val;
10175 
10176 	/* Do not run clean AQ when PF reset fails */
10177 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10178 		return;
10179 
10180 	/* check for error indications */
10181 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10182 	oldval = val;
10183 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10184 		if (hw->debug_mask & I40E_DEBUG_AQ)
10185 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10186 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10187 	}
10188 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10189 		if (hw->debug_mask & I40E_DEBUG_AQ)
10190 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10191 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10192 		pf->arq_overflows++;
10193 	}
10194 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10195 		if (hw->debug_mask & I40E_DEBUG_AQ)
10196 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10197 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10198 	}
10199 	if (oldval != val)
10200 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10201 
10202 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10203 	oldval = val;
10204 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10205 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10206 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10207 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10208 	}
10209 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10210 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10211 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10212 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10213 	}
10214 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10215 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10216 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10217 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10218 	}
10219 	if (oldval != val)
10220 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10221 
10222 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10223 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10224 	if (!event.msg_buf)
10225 		return;
10226 
10227 	do {
10228 		ret = i40e_clean_arq_element(hw, &event, &pending);
10229 		if (ret == -EALREADY)
10230 			break;
10231 		else if (ret) {
10232 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10233 			break;
10234 		}
10235 
10236 		opcode = le16_to_cpu(event.desc.opcode);
10237 		switch (opcode) {
10238 
10239 		case i40e_aqc_opc_get_link_status:
10240 			rtnl_lock();
10241 			i40e_handle_link_event(pf, &event);
10242 			rtnl_unlock();
10243 			break;
10244 		case i40e_aqc_opc_send_msg_to_pf:
10245 			ret = i40e_vc_process_vf_msg(pf,
10246 					le16_to_cpu(event.desc.retval),
10247 					le32_to_cpu(event.desc.cookie_high),
10248 					le32_to_cpu(event.desc.cookie_low),
10249 					event.msg_buf,
10250 					event.msg_len);
10251 			break;
10252 		case i40e_aqc_opc_lldp_update_mib:
10253 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10254 #ifdef CONFIG_I40E_DCB
10255 			rtnl_lock();
10256 			i40e_handle_lldp_event(pf, &event);
10257 			rtnl_unlock();
10258 #endif /* CONFIG_I40E_DCB */
10259 			break;
10260 		case i40e_aqc_opc_event_lan_overflow:
10261 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10262 			i40e_handle_lan_overflow_event(pf, &event);
10263 			break;
10264 		case i40e_aqc_opc_send_msg_to_peer:
10265 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10266 			break;
10267 		case i40e_aqc_opc_nvm_erase:
10268 		case i40e_aqc_opc_nvm_update:
10269 		case i40e_aqc_opc_oem_post_update:
10270 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10271 				   "ARQ NVM operation 0x%04x completed\n",
10272 				   opcode);
10273 			break;
10274 		default:
10275 			dev_info(&pf->pdev->dev,
10276 				 "ARQ: Unknown event 0x%04x ignored\n",
10277 				 opcode);
10278 			break;
10279 		}
10280 	} while (i++ < I40E_AQ_WORK_LIMIT);
10281 
10282 	if (i < I40E_AQ_WORK_LIMIT)
10283 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10284 
10285 	/* re-enable Admin queue interrupt cause */
10286 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10287 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10288 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10289 	i40e_flush(hw);
10290 
10291 	kfree(event.msg_buf);
10292 }
10293 
10294 /**
10295  * i40e_verify_eeprom - make sure eeprom is good to use
10296  * @pf: board private structure
10297  **/
10298 static void i40e_verify_eeprom(struct i40e_pf *pf)
10299 {
10300 	int err;
10301 
10302 	err = i40e_diag_eeprom_test(&pf->hw);
10303 	if (err) {
10304 		/* retry in case of garbage read */
10305 		err = i40e_diag_eeprom_test(&pf->hw);
10306 		if (err) {
10307 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10308 				 err);
10309 			set_bit(__I40E_BAD_EEPROM, pf->state);
10310 		}
10311 	}
10312 
10313 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10314 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10315 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10316 	}
10317 }
10318 
10319 /**
10320  * i40e_enable_pf_switch_lb
10321  * @pf: pointer to the PF structure
10322  *
10323  * enable switch loop back or die - no point in a return value
10324  **/
10325 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10326 {
10327 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10328 	struct i40e_vsi_context ctxt;
10329 	int ret;
10330 
10331 	ctxt.seid = pf->main_vsi_seid;
10332 	ctxt.pf_num = pf->hw.pf_id;
10333 	ctxt.vf_num = 0;
10334 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10335 	if (ret) {
10336 		dev_info(&pf->pdev->dev,
10337 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10338 			 ERR_PTR(ret),
10339 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10340 		return;
10341 	}
10342 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10343 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10344 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10345 
10346 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10347 	if (ret) {
10348 		dev_info(&pf->pdev->dev,
10349 			 "update vsi switch failed, err %pe aq_err %s\n",
10350 			 ERR_PTR(ret),
10351 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10352 	}
10353 }
10354 
10355 /**
10356  * i40e_disable_pf_switch_lb
10357  * @pf: pointer to the PF structure
10358  *
10359  * disable switch loop back or die - no point in a return value
10360  **/
10361 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10362 {
10363 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10364 	struct i40e_vsi_context ctxt;
10365 	int ret;
10366 
10367 	ctxt.seid = pf->main_vsi_seid;
10368 	ctxt.pf_num = pf->hw.pf_id;
10369 	ctxt.vf_num = 0;
10370 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10371 	if (ret) {
10372 		dev_info(&pf->pdev->dev,
10373 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10374 			 ERR_PTR(ret),
10375 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10376 		return;
10377 	}
10378 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10379 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10380 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10381 
10382 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10383 	if (ret) {
10384 		dev_info(&pf->pdev->dev,
10385 			 "update vsi switch failed, err %pe aq_err %s\n",
10386 			 ERR_PTR(ret),
10387 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10388 	}
10389 }
10390 
10391 /**
10392  * i40e_config_bridge_mode - Configure the HW bridge mode
10393  * @veb: pointer to the bridge instance
10394  *
10395  * Configure the loop back mode for the LAN VSI that is downlink to the
10396  * specified HW bridge instance. It is expected this function is called
10397  * when a new HW bridge is instantiated.
10398  **/
10399 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10400 {
10401 	struct i40e_pf *pf = veb->pf;
10402 
10403 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10404 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10405 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10406 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10407 		i40e_disable_pf_switch_lb(pf);
10408 	else
10409 		i40e_enable_pf_switch_lb(pf);
10410 }
10411 
10412 /**
10413  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10414  * @veb: pointer to the VEB instance
10415  *
10416  * This is a function that builds the attached VSIs. We track the connections
10417  * through our own index numbers because the seid's from the HW could change
10418  * across the reset.
10419  **/
10420 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10421 {
10422 	struct i40e_vsi *ctl_vsi = NULL;
10423 	struct i40e_pf *pf = veb->pf;
10424 	struct i40e_vsi *vsi;
10425 	int v, ret;
10426 
10427 	/* As we do not maintain PV (port virtualizer) switch element then
10428 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10429 	 * and its control VSI is the main one.
10430 	 */
10431 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10432 		dev_err(&pf->pdev->dev,
10433 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10434 		return -ENOENT;
10435 	}
10436 
10437 	if (veb->uplink_seid == pf->mac_seid) {
10438 		/* Check that the LAN VSI has VEB owning flag set */
10439 		ctl_vsi = i40e_pf_get_main_vsi(pf);
10440 
10441 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10442 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10443 			dev_err(&pf->pdev->dev,
10444 				"Invalid control VSI for VEB %d\n", veb->idx);
10445 			return -ENOENT;
10446 		}
10447 
10448 		/* Add the control VSI to switch */
10449 		ret = i40e_add_vsi(ctl_vsi);
10450 		if (ret) {
10451 			dev_err(&pf->pdev->dev,
10452 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10453 				veb->idx, ret);
10454 			return ret;
10455 		}
10456 
10457 		i40e_vsi_reset_stats(ctl_vsi);
10458 	}
10459 
10460 	/* create the VEB in the switch and move the VSI onto the VEB */
10461 	ret = i40e_add_veb(veb, ctl_vsi);
10462 	if (ret)
10463 		return ret;
10464 
10465 	if (veb->uplink_seid) {
10466 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10467 			veb->bridge_mode = BRIDGE_MODE_VEB;
10468 		else
10469 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10470 		i40e_config_bridge_mode(veb);
10471 	}
10472 
10473 	/* create the remaining VSIs attached to this VEB */
10474 	i40e_pf_for_each_vsi(pf, v, vsi) {
10475 		if (vsi == ctl_vsi)
10476 			continue;
10477 
10478 		if (vsi->veb_idx == veb->idx) {
10479 			vsi->uplink_seid = veb->seid;
10480 			ret = i40e_add_vsi(vsi);
10481 			if (ret) {
10482 				dev_info(&pf->pdev->dev,
10483 					 "rebuild of vsi_idx %d failed: %d\n",
10484 					 v, ret);
10485 				return ret;
10486 			}
10487 			i40e_vsi_reset_stats(vsi);
10488 		}
10489 	}
10490 
10491 	return ret;
10492 }
10493 
10494 /**
10495  * i40e_get_capabilities - get info about the HW
10496  * @pf: the PF struct
10497  * @list_type: AQ capability to be queried
10498  **/
10499 static int i40e_get_capabilities(struct i40e_pf *pf,
10500 				 enum i40e_admin_queue_opc list_type)
10501 {
10502 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10503 	u16 data_size;
10504 	int buf_len;
10505 	int err;
10506 
10507 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10508 	do {
10509 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10510 		if (!cap_buf)
10511 			return -ENOMEM;
10512 
10513 		/* this loads the data into the hw struct for us */
10514 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10515 						    &data_size, list_type,
10516 						    NULL);
10517 		/* data loaded, buffer no longer needed */
10518 		kfree(cap_buf);
10519 
10520 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10521 			/* retry with a larger buffer */
10522 			buf_len = data_size;
10523 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10524 			dev_info(&pf->pdev->dev,
10525 				 "capability discovery failed, err %pe aq_err %s\n",
10526 				 ERR_PTR(err),
10527 				 i40e_aq_str(&pf->hw,
10528 					     pf->hw.aq.asq_last_status));
10529 			return -ENODEV;
10530 		}
10531 	} while (err);
10532 
10533 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10534 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10535 			dev_info(&pf->pdev->dev,
10536 				 "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",
10537 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10538 				 pf->hw.func_caps.num_msix_vectors,
10539 				 pf->hw.func_caps.num_msix_vectors_vf,
10540 				 pf->hw.func_caps.fd_filters_guaranteed,
10541 				 pf->hw.func_caps.fd_filters_best_effort,
10542 				 pf->hw.func_caps.num_tx_qp,
10543 				 pf->hw.func_caps.num_vsis);
10544 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10545 			dev_info(&pf->pdev->dev,
10546 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10547 				 pf->hw.dev_caps.switch_mode,
10548 				 pf->hw.dev_caps.valid_functions);
10549 			dev_info(&pf->pdev->dev,
10550 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10551 				 pf->hw.dev_caps.sr_iov_1_1,
10552 				 pf->hw.dev_caps.num_vfs);
10553 			dev_info(&pf->pdev->dev,
10554 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10555 				 pf->hw.dev_caps.num_vsis,
10556 				 pf->hw.dev_caps.num_rx_qp,
10557 				 pf->hw.dev_caps.num_tx_qp);
10558 		}
10559 	}
10560 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10561 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10562 		       + pf->hw.func_caps.num_vfs)
10563 		if (pf->hw.revision_id == 0 &&
10564 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10565 			dev_info(&pf->pdev->dev,
10566 				 "got num_vsis %d, setting num_vsis to %d\n",
10567 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10568 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10569 		}
10570 	}
10571 	return 0;
10572 }
10573 
10574 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10575 
10576 /**
10577  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10578  * @pf: board private structure
10579  **/
10580 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10581 {
10582 	struct i40e_vsi *main_vsi, *vsi;
10583 
10584 	/* quick workaround for an NVM issue that leaves a critical register
10585 	 * uninitialized
10586 	 */
10587 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10588 		static const u32 hkey[] = {
10589 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10590 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10591 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10592 			0x95b3a76d};
10593 		int i;
10594 
10595 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10596 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10597 	}
10598 
10599 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10600 		return;
10601 
10602 	/* find existing VSI and see if it needs configuring */
10603 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10604 
10605 	/* create a new VSI if none exists */
10606 	if (!vsi) {
10607 		main_vsi = i40e_pf_get_main_vsi(pf);
10608 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
10609 		if (!vsi) {
10610 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10611 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10612 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10613 			return;
10614 		}
10615 	}
10616 
10617 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10618 }
10619 
10620 /**
10621  * i40e_fdir_teardown - release the Flow Director resources
10622  * @pf: board private structure
10623  **/
10624 static void i40e_fdir_teardown(struct i40e_pf *pf)
10625 {
10626 	struct i40e_vsi *vsi;
10627 
10628 	i40e_fdir_filter_exit(pf);
10629 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10630 	if (vsi)
10631 		i40e_vsi_release(vsi);
10632 }
10633 
10634 /**
10635  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10636  * @vsi: PF main vsi
10637  * @seid: seid of main or channel VSIs
10638  *
10639  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10640  * existed before reset
10641  **/
10642 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10643 {
10644 	struct i40e_cloud_filter *cfilter;
10645 	struct i40e_pf *pf = vsi->back;
10646 	struct hlist_node *node;
10647 	int ret;
10648 
10649 	/* Add cloud filters back if they exist */
10650 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10651 				  cloud_node) {
10652 		if (cfilter->seid != seid)
10653 			continue;
10654 
10655 		if (cfilter->dst_port)
10656 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10657 								true);
10658 		else
10659 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10660 
10661 		if (ret) {
10662 			dev_dbg(&pf->pdev->dev,
10663 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10664 				ERR_PTR(ret),
10665 				i40e_aq_str(&pf->hw,
10666 					    pf->hw.aq.asq_last_status));
10667 			return ret;
10668 		}
10669 	}
10670 	return 0;
10671 }
10672 
10673 /**
10674  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10675  * @vsi: PF main vsi
10676  *
10677  * Rebuilds channel VSIs if they existed before reset
10678  **/
10679 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10680 {
10681 	struct i40e_channel *ch, *ch_tmp;
10682 	int ret;
10683 
10684 	if (list_empty(&vsi->ch_list))
10685 		return 0;
10686 
10687 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10688 		if (!ch->initialized)
10689 			break;
10690 		/* Proceed with creation of channel (VMDq2) VSI */
10691 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10692 		if (ret) {
10693 			dev_info(&vsi->back->pdev->dev,
10694 				 "failed to rebuild channels using uplink_seid %u\n",
10695 				 vsi->uplink_seid);
10696 			return ret;
10697 		}
10698 		/* Reconfigure TX queues using QTX_CTL register */
10699 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10700 		if (ret) {
10701 			dev_info(&vsi->back->pdev->dev,
10702 				 "failed to configure TX rings for channel %u\n",
10703 				 ch->seid);
10704 			return ret;
10705 		}
10706 		/* update 'next_base_queue' */
10707 		vsi->next_base_queue = vsi->next_base_queue +
10708 							ch->num_queue_pairs;
10709 		if (ch->max_tx_rate) {
10710 			u64 credits = ch->max_tx_rate;
10711 
10712 			if (i40e_set_bw_limit(vsi, ch->seid,
10713 					      ch->max_tx_rate))
10714 				return -EINVAL;
10715 
10716 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10717 			dev_dbg(&vsi->back->pdev->dev,
10718 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10719 				ch->max_tx_rate,
10720 				credits,
10721 				ch->seid);
10722 		}
10723 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10724 		if (ret) {
10725 			dev_dbg(&vsi->back->pdev->dev,
10726 				"Failed to rebuild cloud filters for channel VSI %u\n",
10727 				ch->seid);
10728 			return ret;
10729 		}
10730 	}
10731 	return 0;
10732 }
10733 
10734 /**
10735  * i40e_clean_xps_state - clean xps state for every tx_ring
10736  * @vsi: ptr to the VSI
10737  **/
10738 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10739 {
10740 	int i;
10741 
10742 	if (vsi->tx_rings)
10743 		for (i = 0; i < vsi->num_queue_pairs; i++)
10744 			if (vsi->tx_rings[i])
10745 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10746 					  vsi->tx_rings[i]->state);
10747 }
10748 
10749 /**
10750  * i40e_prep_for_reset - prep for the core to reset
10751  * @pf: board private structure
10752  *
10753  * Close up the VFs and other things in prep for PF Reset.
10754   **/
10755 static void i40e_prep_for_reset(struct i40e_pf *pf)
10756 {
10757 	struct i40e_hw *hw = &pf->hw;
10758 	struct i40e_vsi *vsi;
10759 	int ret = 0;
10760 	u32 v;
10761 
10762 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10763 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10764 		return;
10765 	if (i40e_check_asq_alive(&pf->hw))
10766 		i40e_vc_notify_reset(pf);
10767 
10768 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10769 
10770 	/* quiesce the VSIs and their queues that are not already DOWN */
10771 	i40e_pf_quiesce_all_vsi(pf);
10772 
10773 	i40e_pf_for_each_vsi(pf, v, vsi) {
10774 		i40e_clean_xps_state(vsi);
10775 		vsi->seid = 0;
10776 	}
10777 
10778 	i40e_shutdown_adminq(&pf->hw);
10779 
10780 	/* call shutdown HMC */
10781 	if (hw->hmc.hmc_obj) {
10782 		ret = i40e_shutdown_lan_hmc(hw);
10783 		if (ret)
10784 			dev_warn(&pf->pdev->dev,
10785 				 "shutdown_lan_hmc failed: %d\n", ret);
10786 	}
10787 
10788 	/* Save the current PTP time so that we can restore the time after the
10789 	 * reset completes.
10790 	 */
10791 	i40e_ptp_save_hw_time(pf);
10792 }
10793 
10794 /**
10795  * i40e_send_version - update firmware with driver version
10796  * @pf: PF struct
10797  */
10798 static void i40e_send_version(struct i40e_pf *pf)
10799 {
10800 	struct i40e_driver_version dv;
10801 
10802 	dv.major_version = 0xff;
10803 	dv.minor_version = 0xff;
10804 	dv.build_version = 0xff;
10805 	dv.subbuild_version = 0;
10806 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10807 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10808 }
10809 
10810 /**
10811  * i40e_get_oem_version - get OEM specific version information
10812  * @hw: pointer to the hardware structure
10813  **/
10814 static void i40e_get_oem_version(struct i40e_hw *hw)
10815 {
10816 	u16 block_offset = 0xffff;
10817 	u16 block_length = 0;
10818 	u16 capabilities = 0;
10819 	u16 gen_snap = 0;
10820 	u16 release = 0;
10821 
10822 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10823 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10824 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10825 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10826 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10827 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10828 #define I40E_NVM_OEM_LENGTH			3
10829 
10830 	/* Check if pointer to OEM version block is valid. */
10831 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10832 	if (block_offset == 0xffff)
10833 		return;
10834 
10835 	/* Check if OEM version block has correct length. */
10836 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10837 			   &block_length);
10838 	if (block_length < I40E_NVM_OEM_LENGTH)
10839 		return;
10840 
10841 	/* Check if OEM version format is as expected. */
10842 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10843 			   &capabilities);
10844 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10845 		return;
10846 
10847 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10848 			   &gen_snap);
10849 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10850 			   &release);
10851 	hw->nvm.oem_ver =
10852 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10853 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10854 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10855 }
10856 
10857 /**
10858  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10859  * @pf: board private structure
10860  **/
10861 static int i40e_reset(struct i40e_pf *pf)
10862 {
10863 	struct i40e_hw *hw = &pf->hw;
10864 	int ret;
10865 
10866 	ret = i40e_pf_reset(hw);
10867 	if (ret) {
10868 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10869 		set_bit(__I40E_RESET_FAILED, pf->state);
10870 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10871 	} else {
10872 		pf->pfr_count++;
10873 	}
10874 	return ret;
10875 }
10876 
10877 /**
10878  * i40e_rebuild - rebuild using a saved config
10879  * @pf: board private structure
10880  * @reinit: if the Main VSI needs to re-initialized.
10881  * @lock_acquired: indicates whether or not the lock has been acquired
10882  * before this function was called.
10883  **/
10884 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10885 {
10886 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10887 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10888 	struct i40e_hw *hw = &pf->hw;
10889 	struct i40e_veb *veb;
10890 	int ret;
10891 	u32 val;
10892 	int v;
10893 
10894 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10895 	    is_recovery_mode_reported)
10896 		i40e_set_ethtool_ops(vsi->netdev);
10897 
10898 	if (test_bit(__I40E_DOWN, pf->state) &&
10899 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10900 		goto clear_recovery;
10901 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10902 
10903 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10904 	ret = i40e_init_adminq(&pf->hw);
10905 	if (ret) {
10906 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10907 			 ERR_PTR(ret),
10908 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10909 		goto clear_recovery;
10910 	}
10911 	i40e_get_oem_version(&pf->hw);
10912 
10913 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10914 		/* The following delay is necessary for firmware update. */
10915 		mdelay(1000);
10916 	}
10917 
10918 	/* re-verify the eeprom if we just had an EMP reset */
10919 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10920 		i40e_verify_eeprom(pf);
10921 
10922 	/* if we are going out of or into recovery mode we have to act
10923 	 * accordingly with regard to resources initialization
10924 	 * and deinitialization
10925 	 */
10926 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10927 		if (i40e_get_capabilities(pf,
10928 					  i40e_aqc_opc_list_func_capabilities))
10929 			goto end_unlock;
10930 
10931 		if (is_recovery_mode_reported) {
10932 			/* we're staying in recovery mode so we'll reinitialize
10933 			 * misc vector here
10934 			 */
10935 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10936 				goto end_unlock;
10937 		} else {
10938 			if (!lock_acquired)
10939 				rtnl_lock();
10940 			/* we're going out of recovery mode so we'll free
10941 			 * the IRQ allocated specifically for recovery mode
10942 			 * and restore the interrupt scheme
10943 			 */
10944 			free_irq(pf->pdev->irq, pf);
10945 			i40e_clear_interrupt_scheme(pf);
10946 			if (i40e_restore_interrupt_scheme(pf))
10947 				goto end_unlock;
10948 		}
10949 
10950 		/* tell the firmware that we're starting */
10951 		i40e_send_version(pf);
10952 
10953 		/* bail out in case recovery mode was detected, as there is
10954 		 * no need for further configuration.
10955 		 */
10956 		goto end_unlock;
10957 	}
10958 
10959 	i40e_clear_pxe_mode(hw);
10960 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10961 	if (ret)
10962 		goto end_core_reset;
10963 
10964 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10965 				hw->func_caps.num_rx_qp, 0, 0);
10966 	if (ret) {
10967 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10968 		goto end_core_reset;
10969 	}
10970 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10971 	if (ret) {
10972 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10973 		goto end_core_reset;
10974 	}
10975 
10976 #ifdef CONFIG_I40E_DCB
10977 	/* Enable FW to write a default DCB config on link-up
10978 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10979 	 * is not supported with new link speed
10980 	 */
10981 	if (i40e_is_tc_mqprio_enabled(pf)) {
10982 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10983 	} else {
10984 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10985 		    (hw->phy.link_info.link_speed &
10986 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10987 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10988 			dev_warn(&pf->pdev->dev,
10989 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10990 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10991 		} else {
10992 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10993 			ret = i40e_init_pf_dcb(pf);
10994 			if (ret) {
10995 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10996 					 ret);
10997 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10998 				/* Continue without DCB enabled */
10999 			}
11000 		}
11001 	}
11002 
11003 #endif /* CONFIG_I40E_DCB */
11004 	if (!lock_acquired)
11005 		rtnl_lock();
11006 	ret = i40e_setup_pf_switch(pf, reinit, true);
11007 	if (ret)
11008 		goto end_unlock;
11009 
11010 	/* The driver only wants link up/down and module qualification
11011 	 * reports from firmware.  Note the negative logic.
11012 	 */
11013 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
11014 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
11015 					 I40E_AQ_EVENT_MEDIA_NA |
11016 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
11017 	if (ret)
11018 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
11019 			 ERR_PTR(ret),
11020 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11021 
11022 	/* Rebuild the VSIs and VEBs that existed before reset.
11023 	 * They are still in our local switch element arrays, so only
11024 	 * need to rebuild the switch model in the HW.
11025 	 *
11026 	 * If there were VEBs but the reconstitution failed, we'll try
11027 	 * to recover minimal use by getting the basic PF VSI working.
11028 	 */
11029 	if (vsi->uplink_seid != pf->mac_seid) {
11030 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
11031 
11032 		/* Rebuild VEBs */
11033 		i40e_pf_for_each_veb(pf, v, veb) {
11034 			ret = i40e_reconstitute_veb(veb);
11035 			if (!ret)
11036 				continue;
11037 
11038 			/* If Main VEB failed, we're in deep doodoo,
11039 			 * so give up rebuilding the switch and set up
11040 			 * for minimal rebuild of PF VSI.
11041 			 * If orphan failed, we'll report the error
11042 			 * but try to keep going.
11043 			 */
11044 			if (veb->uplink_seid == pf->mac_seid) {
11045 				dev_info(&pf->pdev->dev,
11046 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
11047 					 ret);
11048 				vsi->uplink_seid = pf->mac_seid;
11049 				break;
11050 			} else if (veb->uplink_seid == 0) {
11051 				dev_info(&pf->pdev->dev,
11052 					 "rebuild of orphan VEB failed: %d\n",
11053 					 ret);
11054 			}
11055 		}
11056 	}
11057 
11058 	if (vsi->uplink_seid == pf->mac_seid) {
11059 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11060 		/* no VEB, so rebuild only the Main VSI */
11061 		ret = i40e_add_vsi(vsi);
11062 		if (ret) {
11063 			dev_info(&pf->pdev->dev,
11064 				 "rebuild of Main VSI failed: %d\n", ret);
11065 			goto end_unlock;
11066 		}
11067 	}
11068 
11069 	if (vsi->mqprio_qopt.max_rate[0]) {
11070 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11071 						  vsi->mqprio_qopt.max_rate[0]);
11072 		u64 credits = 0;
11073 
11074 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11075 		if (ret)
11076 			goto end_unlock;
11077 
11078 		credits = max_tx_rate;
11079 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11080 		dev_dbg(&vsi->back->pdev->dev,
11081 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11082 			max_tx_rate,
11083 			credits,
11084 			vsi->seid);
11085 	}
11086 
11087 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11088 	if (ret)
11089 		goto end_unlock;
11090 
11091 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11092 	 * for this main VSI if they exist
11093 	 */
11094 	ret = i40e_rebuild_channels(vsi);
11095 	if (ret)
11096 		goto end_unlock;
11097 
11098 	/* Reconfigure hardware for allowing smaller MSS in the case
11099 	 * of TSO, so that we avoid the MDD being fired and causing
11100 	 * a reset in the case of small MSS+TSO.
11101 	 */
11102 #define I40E_REG_MSS          0x000E64DC
11103 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11104 #define I40E_64BYTE_MSS       0x400000
11105 	val = rd32(hw, I40E_REG_MSS);
11106 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11107 		val &= ~I40E_REG_MSS_MIN_MASK;
11108 		val |= I40E_64BYTE_MSS;
11109 		wr32(hw, I40E_REG_MSS, val);
11110 	}
11111 
11112 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11113 		msleep(75);
11114 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11115 		if (ret)
11116 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11117 				 ERR_PTR(ret),
11118 				 i40e_aq_str(&pf->hw,
11119 					     pf->hw.aq.asq_last_status));
11120 	}
11121 	/* reinit the misc interrupt */
11122 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11123 		ret = i40e_setup_misc_vector(pf);
11124 		if (ret)
11125 			goto end_unlock;
11126 	}
11127 
11128 	/* Add a filter to drop all Flow control frames from any VSI from being
11129 	 * transmitted. By doing so we stop a malicious VF from sending out
11130 	 * PAUSE or PFC frames and potentially controlling traffic for other
11131 	 * PF/VF VSIs.
11132 	 * The FW can still send Flow control frames if enabled.
11133 	 */
11134 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11135 						       pf->main_vsi_seid);
11136 
11137 	/* restart the VSIs that were rebuilt and running before the reset */
11138 	i40e_pf_unquiesce_all_vsi(pf);
11139 
11140 	/* Release the RTNL lock before we start resetting VFs */
11141 	if (!lock_acquired)
11142 		rtnl_unlock();
11143 
11144 	/* Restore promiscuous settings */
11145 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11146 	if (ret)
11147 		dev_warn(&pf->pdev->dev,
11148 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11149 			 pf->cur_promisc ? "on" : "off",
11150 			 ERR_PTR(ret),
11151 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11152 
11153 	i40e_reset_all_vfs(pf, true);
11154 
11155 	/* tell the firmware that we're starting */
11156 	i40e_send_version(pf);
11157 
11158 	/* We've already released the lock, so don't do it again */
11159 	goto end_core_reset;
11160 
11161 end_unlock:
11162 	if (!lock_acquired)
11163 		rtnl_unlock();
11164 end_core_reset:
11165 	clear_bit(__I40E_RESET_FAILED, pf->state);
11166 clear_recovery:
11167 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11168 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11169 }
11170 
11171 /**
11172  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11173  * @pf: board private structure
11174  * @reinit: if the Main VSI needs to re-initialized.
11175  * @lock_acquired: indicates whether or not the lock has been acquired
11176  * before this function was called.
11177  **/
11178 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11179 				   bool lock_acquired)
11180 {
11181 	int ret;
11182 
11183 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11184 		return;
11185 	/* Now we wait for GRST to settle out.
11186 	 * We don't have to delete the VEBs or VSIs from the hw switch
11187 	 * because the reset will make them disappear.
11188 	 */
11189 	ret = i40e_reset(pf);
11190 	if (!ret)
11191 		i40e_rebuild(pf, reinit, lock_acquired);
11192 	else
11193 		dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
11194 }
11195 
11196 /**
11197  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11198  * @pf: board private structure
11199  *
11200  * Close up the VFs and other things in prep for a Core Reset,
11201  * then get ready to rebuild the world.
11202  * @lock_acquired: indicates whether or not the lock has been acquired
11203  * before this function was called.
11204  **/
11205 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11206 {
11207 	i40e_prep_for_reset(pf);
11208 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11209 }
11210 
11211 /**
11212  * i40e_handle_mdd_event
11213  * @pf: pointer to the PF structure
11214  *
11215  * Called from the MDD irq handler to identify possibly malicious vfs
11216  **/
11217 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11218 {
11219 	struct i40e_hw *hw = &pf->hw;
11220 	bool mdd_detected = false;
11221 	struct i40e_vf *vf;
11222 	u32 reg;
11223 	int i;
11224 
11225 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11226 		return;
11227 
11228 	/* find what triggered the MDD event */
11229 	reg = rd32(hw, I40E_GL_MDET_TX);
11230 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11231 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11232 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11233 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11234 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11235 				pf->hw.func_caps.base_queue;
11236 		if (netif_msg_tx_err(pf))
11237 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11238 				 event, queue, pf_num, vf_num);
11239 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11240 		mdd_detected = true;
11241 	}
11242 	reg = rd32(hw, I40E_GL_MDET_RX);
11243 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11244 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11245 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11246 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11247 				pf->hw.func_caps.base_queue;
11248 		if (netif_msg_rx_err(pf))
11249 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11250 				 event, queue, func);
11251 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11252 		mdd_detected = true;
11253 	}
11254 
11255 	if (mdd_detected) {
11256 		reg = rd32(hw, I40E_PF_MDET_TX);
11257 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11258 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11259 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11260 		}
11261 		reg = rd32(hw, I40E_PF_MDET_RX);
11262 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11263 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11264 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11265 		}
11266 	}
11267 
11268 	/* see if one of the VFs needs its hand slapped */
11269 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11270 		vf = &(pf->vf[i]);
11271 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11272 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11273 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11274 			vf->num_mdd_events++;
11275 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11276 				 i);
11277 			dev_info(&pf->pdev->dev,
11278 				 "Use PF Control I/F to re-enable the VF\n");
11279 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11280 		}
11281 
11282 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11283 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11284 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11285 			vf->num_mdd_events++;
11286 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11287 				 i);
11288 			dev_info(&pf->pdev->dev,
11289 				 "Use PF Control I/F to re-enable the VF\n");
11290 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11291 		}
11292 	}
11293 
11294 	/* re-enable mdd interrupt cause */
11295 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11296 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11297 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11298 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11299 	i40e_flush(hw);
11300 }
11301 
11302 /**
11303  * i40e_service_task - Run the driver's async subtasks
11304  * @work: pointer to work_struct containing our data
11305  **/
11306 static void i40e_service_task(struct work_struct *work)
11307 {
11308 	struct i40e_pf *pf = container_of(work,
11309 					  struct i40e_pf,
11310 					  service_task);
11311 	unsigned long start_time = jiffies;
11312 
11313 	/* don't bother with service tasks if a reset is in progress */
11314 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11315 	    test_bit(__I40E_SUSPENDED, pf->state))
11316 		return;
11317 
11318 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11319 		return;
11320 
11321 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11322 		i40e_detect_recover_hung(pf);
11323 		i40e_sync_filters_subtask(pf);
11324 		i40e_reset_subtask(pf);
11325 		i40e_handle_mdd_event(pf);
11326 		i40e_vc_process_vflr_event(pf);
11327 		i40e_watchdog_subtask(pf);
11328 		i40e_fdir_reinit_subtask(pf);
11329 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11330 			/* Client subtask will reopen next time through. */
11331 			i40e_notify_client_of_netdev_close(pf, true);
11332 		} else {
11333 			i40e_client_subtask(pf);
11334 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11335 					       pf->state))
11336 				i40e_notify_client_of_l2_param_changes(pf);
11337 		}
11338 		i40e_sync_filters_subtask(pf);
11339 	} else {
11340 		i40e_reset_subtask(pf);
11341 	}
11342 
11343 	i40e_clean_adminq_subtask(pf);
11344 
11345 	/* flush memory to make sure state is correct before next watchdog */
11346 	smp_mb__before_atomic();
11347 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11348 
11349 	/* If the tasks have taken longer than one timer cycle or there
11350 	 * is more work to be done, reschedule the service task now
11351 	 * rather than wait for the timer to tick again.
11352 	 */
11353 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11354 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11355 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11356 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11357 		i40e_service_event_schedule(pf);
11358 }
11359 
11360 /**
11361  * i40e_service_timer - timer callback
11362  * @t: timer list pointer
11363  **/
11364 static void i40e_service_timer(struct timer_list *t)
11365 {
11366 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11367 
11368 	mod_timer(&pf->service_timer,
11369 		  round_jiffies(jiffies + pf->service_timer_period));
11370 	i40e_service_event_schedule(pf);
11371 }
11372 
11373 /**
11374  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11375  * @vsi: the VSI being configured
11376  **/
11377 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11378 {
11379 	struct i40e_pf *pf = vsi->back;
11380 
11381 	switch (vsi->type) {
11382 	case I40E_VSI_MAIN:
11383 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11384 		if (!vsi->num_tx_desc)
11385 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11386 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11387 		if (!vsi->num_rx_desc)
11388 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11389 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11390 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11391 			vsi->num_q_vectors = pf->num_lan_msix;
11392 		else
11393 			vsi->num_q_vectors = 1;
11394 
11395 		break;
11396 
11397 	case I40E_VSI_FDIR:
11398 		vsi->alloc_queue_pairs = 1;
11399 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11400 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11401 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11402 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11403 		vsi->num_q_vectors = pf->num_fdsb_msix;
11404 		break;
11405 
11406 	case I40E_VSI_VMDQ2:
11407 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11408 		if (!vsi->num_tx_desc)
11409 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11410 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11411 		if (!vsi->num_rx_desc)
11412 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11413 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11414 		vsi->num_q_vectors = pf->num_vmdq_msix;
11415 		break;
11416 
11417 	case I40E_VSI_SRIOV:
11418 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11419 		if (!vsi->num_tx_desc)
11420 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11421 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11422 		if (!vsi->num_rx_desc)
11423 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11424 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11425 		break;
11426 
11427 	default:
11428 		WARN_ON(1);
11429 		return -ENODATA;
11430 	}
11431 
11432 	if (is_kdump_kernel()) {
11433 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11434 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11435 	}
11436 
11437 	return 0;
11438 }
11439 
11440 /**
11441  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11442  * @vsi: VSI pointer
11443  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11444  *
11445  * On error: returns error code (negative)
11446  * On success: returns 0
11447  **/
11448 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11449 {
11450 	struct i40e_ring **next_rings;
11451 	int size;
11452 	int ret = 0;
11453 
11454 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11455 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11456 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11457 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11458 	if (!vsi->tx_rings)
11459 		return -ENOMEM;
11460 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11461 	if (i40e_enabled_xdp_vsi(vsi)) {
11462 		vsi->xdp_rings = next_rings;
11463 		next_rings += vsi->alloc_queue_pairs;
11464 	}
11465 	vsi->rx_rings = next_rings;
11466 
11467 	if (alloc_qvectors) {
11468 		/* allocate memory for q_vector pointers */
11469 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11470 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11471 		if (!vsi->q_vectors) {
11472 			ret = -ENOMEM;
11473 			goto err_vectors;
11474 		}
11475 	}
11476 	return ret;
11477 
11478 err_vectors:
11479 	kfree(vsi->tx_rings);
11480 	return ret;
11481 }
11482 
11483 /**
11484  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11485  * @pf: board private structure
11486  * @type: type of VSI
11487  *
11488  * On error: returns error code (negative)
11489  * On success: returns vsi index in PF (positive)
11490  **/
11491 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11492 {
11493 	int ret = -ENODEV;
11494 	struct i40e_vsi *vsi;
11495 	int vsi_idx;
11496 	int i;
11497 
11498 	/* Need to protect the allocation of the VSIs at the PF level */
11499 	mutex_lock(&pf->switch_mutex);
11500 
11501 	/* VSI list may be fragmented if VSI creation/destruction has
11502 	 * been happening.  We can afford to do a quick scan to look
11503 	 * for any free VSIs in the list.
11504 	 *
11505 	 * find next empty vsi slot, looping back around if necessary
11506 	 */
11507 	i = pf->next_vsi;
11508 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11509 		i++;
11510 	if (i >= pf->num_alloc_vsi) {
11511 		i = 0;
11512 		while (i < pf->next_vsi && pf->vsi[i])
11513 			i++;
11514 	}
11515 
11516 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11517 		vsi_idx = i;             /* Found one! */
11518 	} else {
11519 		ret = -ENODEV;
11520 		goto unlock_pf;  /* out of VSI slots! */
11521 	}
11522 	pf->next_vsi = ++i;
11523 
11524 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11525 	if (!vsi) {
11526 		ret = -ENOMEM;
11527 		goto unlock_pf;
11528 	}
11529 	vsi->type = type;
11530 	vsi->back = pf;
11531 	set_bit(__I40E_VSI_DOWN, vsi->state);
11532 	vsi->flags = 0;
11533 	vsi->idx = vsi_idx;
11534 	vsi->int_rate_limit = 0;
11535 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11536 				pf->rss_table_size : 64;
11537 	vsi->netdev_registered = false;
11538 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11539 	hash_init(vsi->mac_filter_hash);
11540 	vsi->irqs_ready = false;
11541 
11542 	if (type == I40E_VSI_MAIN) {
11543 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11544 		if (!vsi->af_xdp_zc_qps)
11545 			goto err_rings;
11546 	}
11547 
11548 	ret = i40e_set_num_rings_in_vsi(vsi);
11549 	if (ret)
11550 		goto err_rings;
11551 
11552 	ret = i40e_vsi_alloc_arrays(vsi, true);
11553 	if (ret)
11554 		goto err_rings;
11555 
11556 	/* Setup default MSIX irq handler for VSI */
11557 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11558 
11559 	/* Initialize VSI lock */
11560 	spin_lock_init(&vsi->mac_filter_hash_lock);
11561 	pf->vsi[vsi_idx] = vsi;
11562 	ret = vsi_idx;
11563 	goto unlock_pf;
11564 
11565 err_rings:
11566 	bitmap_free(vsi->af_xdp_zc_qps);
11567 	pf->next_vsi = i - 1;
11568 	kfree(vsi);
11569 unlock_pf:
11570 	mutex_unlock(&pf->switch_mutex);
11571 	return ret;
11572 }
11573 
11574 /**
11575  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11576  * @vsi: VSI pointer
11577  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11578  *
11579  * On error: returns error code (negative)
11580  * On success: returns 0
11581  **/
11582 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11583 {
11584 	/* free the ring and vector containers */
11585 	if (free_qvectors) {
11586 		kfree(vsi->q_vectors);
11587 		vsi->q_vectors = NULL;
11588 	}
11589 	kfree(vsi->tx_rings);
11590 	vsi->tx_rings = NULL;
11591 	vsi->rx_rings = NULL;
11592 	vsi->xdp_rings = NULL;
11593 }
11594 
11595 /**
11596  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11597  * and lookup table
11598  * @vsi: Pointer to VSI structure
11599  */
11600 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11601 {
11602 	if (!vsi)
11603 		return;
11604 
11605 	kfree(vsi->rss_hkey_user);
11606 	vsi->rss_hkey_user = NULL;
11607 
11608 	kfree(vsi->rss_lut_user);
11609 	vsi->rss_lut_user = NULL;
11610 }
11611 
11612 /**
11613  * i40e_vsi_clear - Deallocate the VSI provided
11614  * @vsi: the VSI being un-configured
11615  **/
11616 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11617 {
11618 	struct i40e_pf *pf;
11619 
11620 	if (!vsi)
11621 		return 0;
11622 
11623 	if (!vsi->back)
11624 		goto free_vsi;
11625 	pf = vsi->back;
11626 
11627 	mutex_lock(&pf->switch_mutex);
11628 	if (!pf->vsi[vsi->idx]) {
11629 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11630 			vsi->idx, vsi->idx, vsi->type);
11631 		goto unlock_vsi;
11632 	}
11633 
11634 	if (pf->vsi[vsi->idx] != vsi) {
11635 		dev_err(&pf->pdev->dev,
11636 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11637 			pf->vsi[vsi->idx]->idx,
11638 			pf->vsi[vsi->idx]->type,
11639 			vsi->idx, vsi->type);
11640 		goto unlock_vsi;
11641 	}
11642 
11643 	/* updates the PF for this cleared vsi */
11644 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11645 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11646 
11647 	bitmap_free(vsi->af_xdp_zc_qps);
11648 	i40e_vsi_free_arrays(vsi, true);
11649 	i40e_clear_rss_config_user(vsi);
11650 
11651 	pf->vsi[vsi->idx] = NULL;
11652 	if (vsi->idx < pf->next_vsi)
11653 		pf->next_vsi = vsi->idx;
11654 
11655 unlock_vsi:
11656 	mutex_unlock(&pf->switch_mutex);
11657 free_vsi:
11658 	kfree(vsi);
11659 
11660 	return 0;
11661 }
11662 
11663 /**
11664  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11665  * @vsi: the VSI being cleaned
11666  **/
11667 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11668 {
11669 	int i;
11670 
11671 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11672 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11673 			kfree_rcu(vsi->tx_rings[i], rcu);
11674 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11675 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11676 			if (vsi->xdp_rings)
11677 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11678 		}
11679 	}
11680 }
11681 
11682 /**
11683  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11684  * @vsi: the VSI being configured
11685  **/
11686 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11687 {
11688 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11689 	struct i40e_pf *pf = vsi->back;
11690 	struct i40e_ring *ring;
11691 
11692 	/* Set basic values in the rings to be used later during open() */
11693 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11694 		/* allocate space for both Tx and Rx in one shot */
11695 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11696 		if (!ring)
11697 			goto err_out;
11698 
11699 		ring->queue_index = i;
11700 		ring->reg_idx = vsi->base_queue + i;
11701 		ring->ring_active = false;
11702 		ring->vsi = vsi;
11703 		ring->netdev = vsi->netdev;
11704 		ring->dev = &pf->pdev->dev;
11705 		ring->count = vsi->num_tx_desc;
11706 		ring->size = 0;
11707 		ring->dcb_tc = 0;
11708 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11709 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11710 		ring->itr_setting = pf->tx_itr_default;
11711 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11712 
11713 		if (!i40e_enabled_xdp_vsi(vsi))
11714 			goto setup_rx;
11715 
11716 		ring->queue_index = vsi->alloc_queue_pairs + i;
11717 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11718 		ring->ring_active = false;
11719 		ring->vsi = vsi;
11720 		ring->netdev = NULL;
11721 		ring->dev = &pf->pdev->dev;
11722 		ring->count = vsi->num_tx_desc;
11723 		ring->size = 0;
11724 		ring->dcb_tc = 0;
11725 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11726 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11727 		set_ring_xdp(ring);
11728 		ring->itr_setting = pf->tx_itr_default;
11729 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11730 
11731 setup_rx:
11732 		ring->queue_index = i;
11733 		ring->reg_idx = vsi->base_queue + i;
11734 		ring->ring_active = false;
11735 		ring->vsi = vsi;
11736 		ring->netdev = vsi->netdev;
11737 		ring->dev = &pf->pdev->dev;
11738 		ring->count = vsi->num_rx_desc;
11739 		ring->size = 0;
11740 		ring->dcb_tc = 0;
11741 		ring->itr_setting = pf->rx_itr_default;
11742 		WRITE_ONCE(vsi->rx_rings[i], ring);
11743 	}
11744 
11745 	return 0;
11746 
11747 err_out:
11748 	i40e_vsi_clear_rings(vsi);
11749 	return -ENOMEM;
11750 }
11751 
11752 /**
11753  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11754  * @pf: board private structure
11755  * @vectors: the number of MSI-X vectors to request
11756  *
11757  * Returns the number of vectors reserved, or error
11758  **/
11759 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11760 {
11761 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11762 					I40E_MIN_MSIX, vectors);
11763 	if (vectors < 0) {
11764 		dev_info(&pf->pdev->dev,
11765 			 "MSI-X vector reservation failed: %d\n", vectors);
11766 		vectors = 0;
11767 	}
11768 
11769 	return vectors;
11770 }
11771 
11772 /**
11773  * i40e_init_msix - Setup the MSIX capability
11774  * @pf: board private structure
11775  *
11776  * Work with the OS to set up the MSIX vectors needed.
11777  *
11778  * Returns the number of vectors reserved or negative on failure
11779  **/
11780 static int i40e_init_msix(struct i40e_pf *pf)
11781 {
11782 	struct i40e_hw *hw = &pf->hw;
11783 	int cpus, extra_vectors;
11784 	int vectors_left;
11785 	int v_budget, i;
11786 	int v_actual;
11787 	int iwarp_requested = 0;
11788 
11789 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11790 		return -ENODEV;
11791 
11792 	/* The number of vectors we'll request will be comprised of:
11793 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11794 	 *   - The number of LAN queue pairs
11795 	 *	- Queues being used for RSS.
11796 	 *		We don't need as many as max_rss_size vectors.
11797 	 *		use rss_size instead in the calculation since that
11798 	 *		is governed by number of cpus in the system.
11799 	 *	- assumes symmetric Tx/Rx pairing
11800 	 *   - The number of VMDq pairs
11801 	 *   - The CPU count within the NUMA node if iWARP is enabled
11802 	 * Once we count this up, try the request.
11803 	 *
11804 	 * If we can't get what we want, we'll simplify to nearly nothing
11805 	 * and try again.  If that still fails, we punt.
11806 	 */
11807 	vectors_left = hw->func_caps.num_msix_vectors;
11808 	v_budget = 0;
11809 
11810 	/* reserve one vector for miscellaneous handler */
11811 	if (vectors_left) {
11812 		v_budget++;
11813 		vectors_left--;
11814 	}
11815 
11816 	/* reserve some vectors for the main PF traffic queues. Initially we
11817 	 * only reserve at most 50% of the available vectors, in the case that
11818 	 * the number of online CPUs is large. This ensures that we can enable
11819 	 * extra features as well. Once we've enabled the other features, we
11820 	 * will use any remaining vectors to reach as close as we can to the
11821 	 * number of online CPUs.
11822 	 */
11823 	cpus = num_online_cpus();
11824 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11825 	vectors_left -= pf->num_lan_msix;
11826 
11827 	/* reserve one vector for sideband flow director */
11828 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11829 		if (vectors_left) {
11830 			pf->num_fdsb_msix = 1;
11831 			v_budget++;
11832 			vectors_left--;
11833 		} else {
11834 			pf->num_fdsb_msix = 0;
11835 		}
11836 	}
11837 
11838 	/* can we reserve enough for iWARP? */
11839 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11840 		iwarp_requested = pf->num_iwarp_msix;
11841 
11842 		if (!vectors_left)
11843 			pf->num_iwarp_msix = 0;
11844 		else if (vectors_left < pf->num_iwarp_msix)
11845 			pf->num_iwarp_msix = 1;
11846 		v_budget += pf->num_iwarp_msix;
11847 		vectors_left -= pf->num_iwarp_msix;
11848 	}
11849 
11850 	/* any vectors left over go for VMDq support */
11851 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11852 		if (!vectors_left) {
11853 			pf->num_vmdq_msix = 0;
11854 			pf->num_vmdq_qps = 0;
11855 		} else {
11856 			int vmdq_vecs_wanted =
11857 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11858 			int vmdq_vecs =
11859 				min_t(int, vectors_left, vmdq_vecs_wanted);
11860 
11861 			/* if we're short on vectors for what's desired, we limit
11862 			 * the queues per vmdq.  If this is still more than are
11863 			 * available, the user will need to change the number of
11864 			 * queues/vectors used by the PF later with the ethtool
11865 			 * channels command
11866 			 */
11867 			if (vectors_left < vmdq_vecs_wanted) {
11868 				pf->num_vmdq_qps = 1;
11869 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11870 				vmdq_vecs = min_t(int,
11871 						  vectors_left,
11872 						  vmdq_vecs_wanted);
11873 			}
11874 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11875 
11876 			v_budget += vmdq_vecs;
11877 			vectors_left -= vmdq_vecs;
11878 		}
11879 	}
11880 
11881 	/* On systems with a large number of SMP cores, we previously limited
11882 	 * the number of vectors for num_lan_msix to be at most 50% of the
11883 	 * available vectors, to allow for other features. Now, we add back
11884 	 * the remaining vectors. However, we ensure that the total
11885 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11886 	 * calculate the number of vectors we can add without going over the
11887 	 * cap of CPUs. For systems with a small number of CPUs this will be
11888 	 * zero.
11889 	 */
11890 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11891 	pf->num_lan_msix += extra_vectors;
11892 	vectors_left -= extra_vectors;
11893 
11894 	WARN(vectors_left < 0,
11895 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11896 
11897 	v_budget += pf->num_lan_msix;
11898 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11899 				   GFP_KERNEL);
11900 	if (!pf->msix_entries)
11901 		return -ENOMEM;
11902 
11903 	for (i = 0; i < v_budget; i++)
11904 		pf->msix_entries[i].entry = i;
11905 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11906 
11907 	if (v_actual < I40E_MIN_MSIX) {
11908 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11909 		kfree(pf->msix_entries);
11910 		pf->msix_entries = NULL;
11911 		pci_disable_msix(pf->pdev);
11912 		return -ENODEV;
11913 
11914 	} else if (v_actual == I40E_MIN_MSIX) {
11915 		/* Adjust for minimal MSIX use */
11916 		pf->num_vmdq_vsis = 0;
11917 		pf->num_vmdq_qps = 0;
11918 		pf->num_lan_qps = 1;
11919 		pf->num_lan_msix = 1;
11920 
11921 	} else if (v_actual != v_budget) {
11922 		/* If we have limited resources, we will start with no vectors
11923 		 * for the special features and then allocate vectors to some
11924 		 * of these features based on the policy and at the end disable
11925 		 * the features that did not get any vectors.
11926 		 */
11927 		int vec;
11928 
11929 		dev_info(&pf->pdev->dev,
11930 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11931 			 v_actual, v_budget);
11932 		/* reserve the misc vector */
11933 		vec = v_actual - 1;
11934 
11935 		/* Scale vector usage down */
11936 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11937 		pf->num_vmdq_vsis = 1;
11938 		pf->num_vmdq_qps = 1;
11939 
11940 		/* partition out the remaining vectors */
11941 		switch (vec) {
11942 		case 2:
11943 			pf->num_lan_msix = 1;
11944 			break;
11945 		case 3:
11946 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11947 				pf->num_lan_msix = 1;
11948 				pf->num_iwarp_msix = 1;
11949 			} else {
11950 				pf->num_lan_msix = 2;
11951 			}
11952 			break;
11953 		default:
11954 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11955 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11956 						 iwarp_requested);
11957 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11958 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11959 			} else {
11960 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11961 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11962 			}
11963 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11964 				pf->num_fdsb_msix = 1;
11965 				vec--;
11966 			}
11967 			pf->num_lan_msix = min_t(int,
11968 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11969 							      pf->num_lan_msix);
11970 			pf->num_lan_qps = pf->num_lan_msix;
11971 			break;
11972 		}
11973 	}
11974 
11975 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
11976 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11977 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
11978 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
11979 	}
11980 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
11981 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11982 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
11983 	}
11984 
11985 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
11986 	    pf->num_iwarp_msix == 0) {
11987 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11988 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
11989 	}
11990 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11991 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11992 		   pf->num_lan_msix,
11993 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11994 		   pf->num_fdsb_msix,
11995 		   pf->num_iwarp_msix);
11996 
11997 	return v_actual;
11998 }
11999 
12000 /**
12001  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12002  * @vsi: the VSI being configured
12003  * @v_idx: index of the vector in the vsi struct
12004  *
12005  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12006  **/
12007 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12008 {
12009 	struct i40e_q_vector *q_vector;
12010 
12011 	/* allocate q_vector */
12012 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
12013 	if (!q_vector)
12014 		return -ENOMEM;
12015 
12016 	q_vector->vsi = vsi;
12017 	q_vector->v_idx = v_idx;
12018 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12019 
12020 	if (vsi->netdev)
12021 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12022 
12023 	/* tie q_vector and vsi together */
12024 	vsi->q_vectors[v_idx] = q_vector;
12025 
12026 	return 0;
12027 }
12028 
12029 /**
12030  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12031  * @vsi: the VSI being configured
12032  *
12033  * We allocate one q_vector per queue interrupt.  If allocation fails we
12034  * return -ENOMEM.
12035  **/
12036 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12037 {
12038 	struct i40e_pf *pf = vsi->back;
12039 	int err, v_idx, num_q_vectors;
12040 
12041 	/* if not MSIX, give the one vector only to the LAN VSI */
12042 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12043 		num_q_vectors = vsi->num_q_vectors;
12044 	else if (vsi->type == I40E_VSI_MAIN)
12045 		num_q_vectors = 1;
12046 	else
12047 		return -EINVAL;
12048 
12049 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12050 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12051 		if (err)
12052 			goto err_out;
12053 	}
12054 
12055 	return 0;
12056 
12057 err_out:
12058 	while (v_idx--)
12059 		i40e_free_q_vector(vsi, v_idx);
12060 
12061 	return err;
12062 }
12063 
12064 /**
12065  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12066  * @pf: board private structure to initialize
12067  **/
12068 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12069 {
12070 	int vectors = 0;
12071 	ssize_t size;
12072 
12073 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12074 		vectors = i40e_init_msix(pf);
12075 		if (vectors < 0) {
12076 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12077 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12078 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12079 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12080 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12081 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12082 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12083 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12084 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12085 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12086 
12087 			/* rework the queue expectations without MSIX */
12088 			i40e_determine_queue_usage(pf);
12089 		}
12090 	}
12091 
12092 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12093 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12094 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12095 		vectors = pci_enable_msi(pf->pdev);
12096 		if (vectors < 0) {
12097 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12098 				 vectors);
12099 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12100 		}
12101 		vectors = 1;  /* one MSI or Legacy vector */
12102 	}
12103 
12104 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12105 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12106 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12107 
12108 	/* set up vector assignment tracking */
12109 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12110 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12111 	if (!pf->irq_pile)
12112 		return -ENOMEM;
12113 
12114 	pf->irq_pile->num_entries = vectors;
12115 
12116 	/* track first vector for misc interrupts, ignore return */
12117 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12118 
12119 	return 0;
12120 }
12121 
12122 /**
12123  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12124  * @pf: private board data structure
12125  *
12126  * Restore the interrupt scheme that was cleared when we suspended the
12127  * device. This should be called during resume to re-allocate the q_vectors
12128  * and reacquire IRQs.
12129  */
12130 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12131 {
12132 	struct i40e_vsi *vsi;
12133 	int err, i;
12134 
12135 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12136 	 * scheme. We need to re-enabled them here in order to attempt to
12137 	 * re-acquire the MSI or MSI-X vectors
12138 	 */
12139 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12140 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12141 
12142 	err = i40e_init_interrupt_scheme(pf);
12143 	if (err)
12144 		return err;
12145 
12146 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12147 	 * rings together again.
12148 	 */
12149 	i40e_pf_for_each_vsi(pf, i, vsi) {
12150 		err = i40e_vsi_alloc_q_vectors(vsi);
12151 		if (err)
12152 			goto err_unwind;
12153 
12154 		i40e_vsi_map_rings_to_vectors(vsi);
12155 	}
12156 
12157 	err = i40e_setup_misc_vector(pf);
12158 	if (err)
12159 		goto err_unwind;
12160 
12161 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12162 		i40e_client_update_msix_info(pf);
12163 
12164 	return 0;
12165 
12166 err_unwind:
12167 	while (i--) {
12168 		if (pf->vsi[i])
12169 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12170 	}
12171 
12172 	return err;
12173 }
12174 
12175 /**
12176  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12177  * non queue events in recovery mode
12178  * @pf: board private structure
12179  *
12180  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12181  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12182  * This is handled differently than in recovery mode since no Tx/Rx resources
12183  * are being allocated.
12184  **/
12185 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12186 {
12187 	int err;
12188 
12189 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12190 		err = i40e_setup_misc_vector(pf);
12191 
12192 		if (err) {
12193 			dev_info(&pf->pdev->dev,
12194 				 "MSI-X misc vector request failed, error %d\n",
12195 				 err);
12196 			return err;
12197 		}
12198 	} else {
12199 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12200 
12201 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12202 				  pf->int_name, pf);
12203 
12204 		if (err) {
12205 			dev_info(&pf->pdev->dev,
12206 				 "MSI/legacy misc vector request failed, error %d\n",
12207 				 err);
12208 			return err;
12209 		}
12210 		i40e_enable_misc_int_causes(pf);
12211 		i40e_irq_dynamic_enable_icr0(pf);
12212 	}
12213 
12214 	return 0;
12215 }
12216 
12217 /**
12218  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12219  * @pf: board private structure
12220  *
12221  * This sets up the handler for MSIX 0, which is used to manage the
12222  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12223  * when in MSI or Legacy interrupt mode.
12224  **/
12225 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12226 {
12227 	struct i40e_hw *hw = &pf->hw;
12228 	int err = 0;
12229 
12230 	/* Only request the IRQ once, the first time through. */
12231 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12232 		err = request_irq(pf->msix_entries[0].vector,
12233 				  i40e_intr, 0, pf->int_name, pf);
12234 		if (err) {
12235 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12236 			dev_info(&pf->pdev->dev,
12237 				 "request_irq for %s failed: %d\n",
12238 				 pf->int_name, err);
12239 			return -EFAULT;
12240 		}
12241 	}
12242 
12243 	i40e_enable_misc_int_causes(pf);
12244 
12245 	/* associate no queues to the misc vector */
12246 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12247 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12248 
12249 	i40e_flush(hw);
12250 
12251 	i40e_irq_dynamic_enable_icr0(pf);
12252 
12253 	return err;
12254 }
12255 
12256 /**
12257  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12258  * @vsi: Pointer to vsi structure
12259  * @seed: Buffter to store the hash keys
12260  * @lut: Buffer to store the lookup table entries
12261  * @lut_size: Size of buffer to store the lookup table entries
12262  *
12263  * Return 0 on success, negative on failure
12264  */
12265 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12266 			   u8 *lut, u16 lut_size)
12267 {
12268 	struct i40e_pf *pf = vsi->back;
12269 	struct i40e_hw *hw = &pf->hw;
12270 	int ret = 0;
12271 
12272 	if (seed) {
12273 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12274 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12275 		if (ret) {
12276 			dev_info(&pf->pdev->dev,
12277 				 "Cannot get RSS key, err %pe aq_err %s\n",
12278 				 ERR_PTR(ret),
12279 				 i40e_aq_str(&pf->hw,
12280 					     pf->hw.aq.asq_last_status));
12281 			return ret;
12282 		}
12283 	}
12284 
12285 	if (lut) {
12286 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12287 
12288 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12289 		if (ret) {
12290 			dev_info(&pf->pdev->dev,
12291 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12292 				 ERR_PTR(ret),
12293 				 i40e_aq_str(&pf->hw,
12294 					     pf->hw.aq.asq_last_status));
12295 			return ret;
12296 		}
12297 	}
12298 
12299 	return ret;
12300 }
12301 
12302 /**
12303  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12304  * @vsi: Pointer to vsi structure
12305  * @seed: RSS hash seed
12306  * @lut: Lookup table
12307  * @lut_size: Lookup table size
12308  *
12309  * Returns 0 on success, negative on failure
12310  **/
12311 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12312 			       const u8 *lut, u16 lut_size)
12313 {
12314 	struct i40e_pf *pf = vsi->back;
12315 	struct i40e_hw *hw = &pf->hw;
12316 	u16 vf_id = vsi->vf_id;
12317 	u8 i;
12318 
12319 	/* Fill out hash function seed */
12320 	if (seed) {
12321 		u32 *seed_dw = (u32 *)seed;
12322 
12323 		if (vsi->type == I40E_VSI_MAIN) {
12324 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12325 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12326 		} else if (vsi->type == I40E_VSI_SRIOV) {
12327 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12328 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12329 		} else {
12330 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12331 		}
12332 	}
12333 
12334 	if (lut) {
12335 		u32 *lut_dw = (u32 *)lut;
12336 
12337 		if (vsi->type == I40E_VSI_MAIN) {
12338 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12339 				return -EINVAL;
12340 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12341 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12342 		} else if (vsi->type == I40E_VSI_SRIOV) {
12343 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12344 				return -EINVAL;
12345 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12346 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12347 		} else {
12348 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12349 		}
12350 	}
12351 	i40e_flush(hw);
12352 
12353 	return 0;
12354 }
12355 
12356 /**
12357  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12358  * @vsi: Pointer to VSI structure
12359  * @seed: Buffer to store the keys
12360  * @lut: Buffer to store the lookup table entries
12361  * @lut_size: Size of buffer to store the lookup table entries
12362  *
12363  * Returns 0 on success, negative on failure
12364  */
12365 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12366 			    u8 *lut, u16 lut_size)
12367 {
12368 	struct i40e_pf *pf = vsi->back;
12369 	struct i40e_hw *hw = &pf->hw;
12370 	u16 i;
12371 
12372 	if (seed) {
12373 		u32 *seed_dw = (u32 *)seed;
12374 
12375 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12376 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12377 	}
12378 	if (lut) {
12379 		u32 *lut_dw = (u32 *)lut;
12380 
12381 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12382 			return -EINVAL;
12383 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12384 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12385 	}
12386 
12387 	return 0;
12388 }
12389 
12390 /**
12391  * i40e_config_rss - Configure RSS keys and lut
12392  * @vsi: Pointer to VSI structure
12393  * @seed: RSS hash seed
12394  * @lut: Lookup table
12395  * @lut_size: Lookup table size
12396  *
12397  * Returns 0 on success, negative on failure
12398  */
12399 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12400 {
12401 	struct i40e_pf *pf = vsi->back;
12402 
12403 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12404 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12405 	else
12406 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12407 }
12408 
12409 /**
12410  * i40e_get_rss - Get RSS keys and lut
12411  * @vsi: Pointer to VSI structure
12412  * @seed: Buffer to store the keys
12413  * @lut: Buffer to store the lookup table entries
12414  * @lut_size: Size of buffer to store the lookup table entries
12415  *
12416  * Returns 0 on success, negative on failure
12417  */
12418 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12419 {
12420 	struct i40e_pf *pf = vsi->back;
12421 
12422 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12423 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12424 	else
12425 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12426 }
12427 
12428 /**
12429  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12430  * @pf: Pointer to board private structure
12431  * @lut: Lookup table
12432  * @rss_table_size: Lookup table size
12433  * @rss_size: Range of queue number for hashing
12434  */
12435 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12436 		       u16 rss_table_size, u16 rss_size)
12437 {
12438 	u16 i;
12439 
12440 	for (i = 0; i < rss_table_size; i++)
12441 		lut[i] = i % rss_size;
12442 }
12443 
12444 /**
12445  * i40e_pf_config_rss - Prepare for RSS if used
12446  * @pf: board private structure
12447  **/
12448 static int i40e_pf_config_rss(struct i40e_pf *pf)
12449 {
12450 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12451 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12452 	u8 *lut;
12453 	struct i40e_hw *hw = &pf->hw;
12454 	u32 reg_val;
12455 	u64 hena;
12456 	int ret;
12457 
12458 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12459 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12460 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12461 	hena |= i40e_pf_get_default_rss_hena(pf);
12462 
12463 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12464 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12465 
12466 	/* Determine the RSS table size based on the hardware capabilities */
12467 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12468 	reg_val = (pf->rss_table_size == 512) ?
12469 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12470 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12471 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12472 
12473 	/* Determine the RSS size of the VSI */
12474 	if (!vsi->rss_size) {
12475 		u16 qcount;
12476 		/* If the firmware does something weird during VSI init, we
12477 		 * could end up with zero TCs. Check for that to avoid
12478 		 * divide-by-zero. It probably won't pass traffic, but it also
12479 		 * won't panic.
12480 		 */
12481 		qcount = vsi->num_queue_pairs /
12482 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12483 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12484 	}
12485 	if (!vsi->rss_size)
12486 		return -EINVAL;
12487 
12488 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12489 	if (!lut)
12490 		return -ENOMEM;
12491 
12492 	/* Use user configured lut if there is one, otherwise use default */
12493 	if (vsi->rss_lut_user)
12494 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12495 	else
12496 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12497 
12498 	/* Use user configured hash key if there is one, otherwise
12499 	 * use default.
12500 	 */
12501 	if (vsi->rss_hkey_user)
12502 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12503 	else
12504 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12505 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12506 	kfree(lut);
12507 
12508 	return ret;
12509 }
12510 
12511 /**
12512  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12513  * @pf: board private structure
12514  * @queue_count: the requested queue count for rss.
12515  *
12516  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12517  * count which may be different from the requested queue count.
12518  * Note: expects to be called while under rtnl_lock()
12519  **/
12520 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12521 {
12522 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12523 	int new_rss_size;
12524 
12525 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12526 		return 0;
12527 
12528 	queue_count = min_t(int, queue_count, num_online_cpus());
12529 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12530 
12531 	if (queue_count != vsi->num_queue_pairs) {
12532 		u16 qcount;
12533 
12534 		vsi->req_queue_pairs = queue_count;
12535 		i40e_prep_for_reset(pf);
12536 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12537 			return pf->alloc_rss_size;
12538 
12539 		pf->alloc_rss_size = new_rss_size;
12540 
12541 		i40e_reset_and_rebuild(pf, true, true);
12542 
12543 		/* Discard the user configured hash keys and lut, if less
12544 		 * queues are enabled.
12545 		 */
12546 		if (queue_count < vsi->rss_size) {
12547 			i40e_clear_rss_config_user(vsi);
12548 			dev_dbg(&pf->pdev->dev,
12549 				"discard user configured hash keys and lut\n");
12550 		}
12551 
12552 		/* Reset vsi->rss_size, as number of enabled queues changed */
12553 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12554 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12555 
12556 		i40e_pf_config_rss(pf);
12557 	}
12558 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12559 		 vsi->req_queue_pairs, pf->rss_size_max);
12560 	return pf->alloc_rss_size;
12561 }
12562 
12563 /**
12564  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12565  * @pf: board private structure
12566  **/
12567 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12568 {
12569 	bool min_valid, max_valid;
12570 	u32 max_bw, min_bw;
12571 	int status;
12572 
12573 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12574 					   &min_valid, &max_valid);
12575 
12576 	if (!status) {
12577 		if (min_valid)
12578 			pf->min_bw = min_bw;
12579 		if (max_valid)
12580 			pf->max_bw = max_bw;
12581 	}
12582 
12583 	return status;
12584 }
12585 
12586 /**
12587  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12588  * @pf: board private structure
12589  **/
12590 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12591 {
12592 	struct i40e_aqc_configure_partition_bw_data bw_data;
12593 	int status;
12594 
12595 	memset(&bw_data, 0, sizeof(bw_data));
12596 
12597 	/* Set the valid bit for this PF */
12598 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12599 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12600 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12601 
12602 	/* Set the new bandwidths */
12603 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12604 
12605 	return status;
12606 }
12607 
12608 /**
12609  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12610  * @pf: board private structure
12611  **/
12612 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12613 {
12614 	/* Commit temporary BW setting to permanent NVM image */
12615 	enum i40e_admin_queue_err last_aq_status;
12616 	u16 nvm_word;
12617 	int ret;
12618 
12619 	if (pf->hw.partition_id != 1) {
12620 		dev_info(&pf->pdev->dev,
12621 			 "Commit BW only works on partition 1! This is partition %d",
12622 			 pf->hw.partition_id);
12623 		ret = -EOPNOTSUPP;
12624 		goto bw_commit_out;
12625 	}
12626 
12627 	/* Acquire NVM for read access */
12628 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12629 	last_aq_status = pf->hw.aq.asq_last_status;
12630 	if (ret) {
12631 		dev_info(&pf->pdev->dev,
12632 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12633 			 ERR_PTR(ret),
12634 			 i40e_aq_str(&pf->hw, last_aq_status));
12635 		goto bw_commit_out;
12636 	}
12637 
12638 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12639 	ret = i40e_aq_read_nvm(&pf->hw,
12640 			       I40E_SR_NVM_CONTROL_WORD,
12641 			       0x10, sizeof(nvm_word), &nvm_word,
12642 			       false, NULL);
12643 	/* Save off last admin queue command status before releasing
12644 	 * the NVM
12645 	 */
12646 	last_aq_status = pf->hw.aq.asq_last_status;
12647 	i40e_release_nvm(&pf->hw);
12648 	if (ret) {
12649 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12650 			 ERR_PTR(ret),
12651 			 i40e_aq_str(&pf->hw, last_aq_status));
12652 		goto bw_commit_out;
12653 	}
12654 
12655 	/* Wait a bit for NVM release to complete */
12656 	msleep(50);
12657 
12658 	/* Acquire NVM for write access */
12659 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12660 	last_aq_status = pf->hw.aq.asq_last_status;
12661 	if (ret) {
12662 		dev_info(&pf->pdev->dev,
12663 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12664 			 ERR_PTR(ret),
12665 			 i40e_aq_str(&pf->hw, last_aq_status));
12666 		goto bw_commit_out;
12667 	}
12668 	/* Write it back out unchanged to initiate update NVM,
12669 	 * which will force a write of the shadow (alt) RAM to
12670 	 * the NVM - thus storing the bandwidth values permanently.
12671 	 */
12672 	ret = i40e_aq_update_nvm(&pf->hw,
12673 				 I40E_SR_NVM_CONTROL_WORD,
12674 				 0x10, sizeof(nvm_word),
12675 				 &nvm_word, true, 0, NULL);
12676 	/* Save off last admin queue command status before releasing
12677 	 * the NVM
12678 	 */
12679 	last_aq_status = pf->hw.aq.asq_last_status;
12680 	i40e_release_nvm(&pf->hw);
12681 	if (ret)
12682 		dev_info(&pf->pdev->dev,
12683 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12684 			 ERR_PTR(ret),
12685 			 i40e_aq_str(&pf->hw, last_aq_status));
12686 bw_commit_out:
12687 
12688 	return ret;
12689 }
12690 
12691 /**
12692  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12693  * if total port shutdown feature is enabled for this PF
12694  * @pf: board private structure
12695  **/
12696 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12697 {
12698 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12699 #define I40E_FEATURES_ENABLE_PTR		0x2A
12700 #define I40E_CURRENT_SETTING_PTR		0x2B
12701 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12702 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12703 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12704 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12705 	u16 sr_emp_sr_settings_ptr = 0;
12706 	u16 features_enable = 0;
12707 	u16 link_behavior = 0;
12708 	int read_status = 0;
12709 	bool ret = false;
12710 
12711 	read_status = i40e_read_nvm_word(&pf->hw,
12712 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12713 					 &sr_emp_sr_settings_ptr);
12714 	if (read_status)
12715 		goto err_nvm;
12716 	read_status = i40e_read_nvm_word(&pf->hw,
12717 					 sr_emp_sr_settings_ptr +
12718 					 I40E_FEATURES_ENABLE_PTR,
12719 					 &features_enable);
12720 	if (read_status)
12721 		goto err_nvm;
12722 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12723 		read_status = i40e_read_nvm_module_data(&pf->hw,
12724 							I40E_SR_EMP_SR_SETTINGS_PTR,
12725 							I40E_CURRENT_SETTING_PTR,
12726 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12727 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12728 							&link_behavior);
12729 		if (read_status)
12730 			goto err_nvm;
12731 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12732 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12733 	}
12734 	return ret;
12735 
12736 err_nvm:
12737 	dev_warn(&pf->pdev->dev,
12738 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12739 		 ERR_PTR(read_status));
12740 	return ret;
12741 }
12742 
12743 /**
12744  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12745  * @pf: board private structure to initialize
12746  *
12747  * i40e_sw_init initializes the Adapter private data structure.
12748  * Fields are initialized based on PCI device information and
12749  * OS network device settings (MTU size).
12750  **/
12751 static int i40e_sw_init(struct i40e_pf *pf)
12752 {
12753 	int err = 0;
12754 	int size;
12755 	u16 pow;
12756 
12757 	/* Set default capability flags */
12758 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12759 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12760 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12761 
12762 	/* Set default ITR */
12763 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12764 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12765 
12766 	/* Depending on PF configurations, it is possible that the RSS
12767 	 * maximum might end up larger than the available queues
12768 	 */
12769 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12770 	pf->alloc_rss_size = 1;
12771 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12772 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12773 				 pf->hw.func_caps.num_tx_qp);
12774 
12775 	/* find the next higher power-of-2 of num cpus */
12776 	pow = roundup_pow_of_two(num_online_cpus());
12777 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12778 
12779 	if (pf->hw.func_caps.rss) {
12780 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12781 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12782 					   num_online_cpus());
12783 	}
12784 
12785 	/* MFP mode enabled */
12786 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12787 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12788 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12789 		if (i40e_get_partition_bw_setting(pf)) {
12790 			dev_warn(&pf->pdev->dev,
12791 				 "Could not get partition bw settings\n");
12792 		} else {
12793 			dev_info(&pf->pdev->dev,
12794 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12795 				 pf->min_bw, pf->max_bw);
12796 
12797 			/* nudge the Tx scheduler */
12798 			i40e_set_partition_bw_setting(pf);
12799 		}
12800 	}
12801 
12802 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12803 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12804 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12805 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12806 		    pf->hw.num_partitions > 1)
12807 			dev_info(&pf->pdev->dev,
12808 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12809 		else
12810 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12811 		pf->fdir_pf_filter_count =
12812 				 pf->hw.func_caps.fd_filters_guaranteed;
12813 		pf->hw.fdir_shared_filter_count =
12814 				 pf->hw.func_caps.fd_filters_best_effort;
12815 	}
12816 
12817 	/* Enable HW ATR eviction if possible */
12818 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12819 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12820 
12821 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12822 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12823 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12824 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12825 	}
12826 
12827 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12828 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12829 		/* IWARP needs one extra vector for CQP just like MISC.*/
12830 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12831 	}
12832 	/* Stopping FW LLDP engine is supported on XL710 and X722
12833 	 * starting from FW versions determined in i40e_init_adminq.
12834 	 * Stopping the FW LLDP engine is not supported on XL710
12835 	 * if NPAR is functioning so unset this hw flag in this case.
12836 	 */
12837 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12838 	    pf->hw.func_caps.npar_enable)
12839 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12840 
12841 #ifdef CONFIG_PCI_IOV
12842 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12843 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12844 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12845 		pf->num_req_vfs = min_t(int,
12846 					pf->hw.func_caps.num_vfs,
12847 					I40E_MAX_VF_COUNT);
12848 	}
12849 #endif /* CONFIG_PCI_IOV */
12850 	pf->lan_veb = I40E_NO_VEB;
12851 	pf->lan_vsi = I40E_NO_VSI;
12852 
12853 	/* By default FW has this off for performance reasons */
12854 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12855 
12856 	/* set up queue assignment tracking */
12857 	size = sizeof(struct i40e_lump_tracking)
12858 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12859 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12860 	if (!pf->qp_pile) {
12861 		err = -ENOMEM;
12862 		goto sw_init_done;
12863 	}
12864 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12865 
12866 	pf->tx_timeout_recovery_level = 1;
12867 
12868 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12869 	    i40e_is_total_port_shutdown_enabled(pf)) {
12870 		/* Link down on close must be on when total port shutdown
12871 		 * is enabled for a given port
12872 		 */
12873 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12874 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12875 		dev_info(&pf->pdev->dev,
12876 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12877 	}
12878 	mutex_init(&pf->switch_mutex);
12879 
12880 sw_init_done:
12881 	return err;
12882 }
12883 
12884 /**
12885  * i40e_set_ntuple - set the ntuple feature flag and take action
12886  * @pf: board private structure to initialize
12887  * @features: the feature set that the stack is suggesting
12888  *
12889  * returns a bool to indicate if reset needs to happen
12890  **/
12891 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12892 {
12893 	bool need_reset = false;
12894 
12895 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12896 	 * the state changed, we need to reset.
12897 	 */
12898 	if (features & NETIF_F_NTUPLE) {
12899 		/* Enable filters and mark for reset */
12900 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12901 			need_reset = true;
12902 		/* enable FD_SB only if there is MSI-X vector and no cloud
12903 		 * filters exist
12904 		 */
12905 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12906 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12907 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12908 		}
12909 	} else {
12910 		/* turn off filters, mark for reset and clear SW filter list */
12911 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12912 			need_reset = true;
12913 			i40e_fdir_filter_exit(pf);
12914 		}
12915 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12916 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12917 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12918 
12919 		/* reset fd counters */
12920 		pf->fd_add_err = 0;
12921 		pf->fd_atr_cnt = 0;
12922 		/* if ATR was auto disabled it can be re-enabled. */
12923 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12924 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12925 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12926 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12927 	}
12928 	return need_reset;
12929 }
12930 
12931 /**
12932  * i40e_clear_rss_lut - clear the rx hash lookup table
12933  * @vsi: the VSI being configured
12934  **/
12935 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12936 {
12937 	struct i40e_pf *pf = vsi->back;
12938 	struct i40e_hw *hw = &pf->hw;
12939 	u16 vf_id = vsi->vf_id;
12940 	u8 i;
12941 
12942 	if (vsi->type == I40E_VSI_MAIN) {
12943 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12944 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12945 	} else if (vsi->type == I40E_VSI_SRIOV) {
12946 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12947 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12948 	} else {
12949 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12950 	}
12951 }
12952 
12953 /**
12954  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12955  * @vsi: ptr to VSI
12956  * @ena: flag to indicate the on/off setting
12957  */
12958 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12959 {
12960 	bool if_running = netif_running(vsi->netdev) &&
12961 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12962 	int ret;
12963 
12964 	if (if_running)
12965 		i40e_down(vsi);
12966 
12967 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12968 	if (ret)
12969 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12970 	if (if_running)
12971 		i40e_up(vsi);
12972 
12973 	return ret;
12974 }
12975 
12976 /**
12977  * i40e_set_features - set the netdev feature flags
12978  * @netdev: ptr to the netdev being adjusted
12979  * @features: the feature set that the stack is suggesting
12980  * Note: expects to be called while under rtnl_lock()
12981  **/
12982 static int i40e_set_features(struct net_device *netdev,
12983 			     netdev_features_t features)
12984 {
12985 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12986 	struct i40e_vsi *vsi = np->vsi;
12987 	struct i40e_pf *pf = vsi->back;
12988 	bool need_reset;
12989 
12990 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12991 		i40e_pf_config_rss(pf);
12992 	else if (!(features & NETIF_F_RXHASH) &&
12993 		 netdev->features & NETIF_F_RXHASH)
12994 		i40e_clear_rss_lut(vsi);
12995 
12996 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12997 		i40e_vlan_stripping_enable(vsi);
12998 	else
12999 		i40e_vlan_stripping_disable(vsi);
13000 
13001 	if (!(features & NETIF_F_HW_TC) &&
13002 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13003 		dev_err(&pf->pdev->dev,
13004 			"Offloaded tc filters active, can't turn hw_tc_offload off");
13005 		return -EINVAL;
13006 	}
13007 
13008 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13009 		i40e_del_all_macvlans(vsi);
13010 
13011 	need_reset = i40e_set_ntuple(pf, features);
13012 
13013 	if (need_reset)
13014 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13015 
13016 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13017 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13018 
13019 	return 0;
13020 }
13021 
13022 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13023 				    unsigned int table, unsigned int idx,
13024 				    struct udp_tunnel_info *ti)
13025 {
13026 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13027 	struct i40e_hw *hw = &np->vsi->back->hw;
13028 	u8 type, filter_index;
13029 	int ret;
13030 
13031 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13032 						   I40E_AQC_TUNNEL_TYPE_NGE;
13033 
13034 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13035 				     NULL);
13036 	if (ret) {
13037 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13038 			    ERR_PTR(ret),
13039 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13040 		return -EIO;
13041 	}
13042 
13043 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13044 	return 0;
13045 }
13046 
13047 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13048 				      unsigned int table, unsigned int idx,
13049 				      struct udp_tunnel_info *ti)
13050 {
13051 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13052 	struct i40e_hw *hw = &np->vsi->back->hw;
13053 	int ret;
13054 
13055 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13056 	if (ret) {
13057 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13058 			    ERR_PTR(ret),
13059 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13060 		return -EIO;
13061 	}
13062 
13063 	return 0;
13064 }
13065 
13066 static int i40e_get_phys_port_id(struct net_device *netdev,
13067 				 struct netdev_phys_item_id *ppid)
13068 {
13069 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13070 	struct i40e_pf *pf = np->vsi->back;
13071 	struct i40e_hw *hw = &pf->hw;
13072 
13073 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13074 		return -EOPNOTSUPP;
13075 
13076 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13077 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13078 
13079 	return 0;
13080 }
13081 
13082 /**
13083  * i40e_ndo_fdb_add - add an entry to the hardware database
13084  * @ndm: the input from the stack
13085  * @tb: pointer to array of nladdr (unused)
13086  * @dev: the net device pointer
13087  * @addr: the MAC address entry being added
13088  * @vid: VLAN ID
13089  * @flags: instructions from stack about fdb operation
13090  * @extack: netlink extended ack, unused currently
13091  */
13092 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13093 			    struct net_device *dev,
13094 			    const unsigned char *addr, u16 vid,
13095 			    u16 flags,
13096 			    struct netlink_ext_ack *extack)
13097 {
13098 	struct i40e_netdev_priv *np = netdev_priv(dev);
13099 	struct i40e_pf *pf = np->vsi->back;
13100 	int err = 0;
13101 
13102 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13103 		return -EOPNOTSUPP;
13104 
13105 	if (vid) {
13106 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13107 		return -EINVAL;
13108 	}
13109 
13110 	/* Hardware does not support aging addresses so if a
13111 	 * ndm_state is given only allow permanent addresses
13112 	 */
13113 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13114 		netdev_info(dev, "FDB only supports static addresses\n");
13115 		return -EINVAL;
13116 	}
13117 
13118 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13119 		err = dev_uc_add_excl(dev, addr);
13120 	else if (is_multicast_ether_addr(addr))
13121 		err = dev_mc_add_excl(dev, addr);
13122 	else
13123 		err = -EINVAL;
13124 
13125 	/* Only return duplicate errors if NLM_F_EXCL is set */
13126 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13127 		err = 0;
13128 
13129 	return err;
13130 }
13131 
13132 /**
13133  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13134  * @dev: the netdev being configured
13135  * @nlh: RTNL message
13136  * @flags: bridge flags
13137  * @extack: netlink extended ack
13138  *
13139  * Inserts a new hardware bridge if not already created and
13140  * enables the bridging mode requested (VEB or VEPA). If the
13141  * hardware bridge has already been inserted and the request
13142  * is to change the mode then that requires a PF reset to
13143  * allow rebuild of the components with required hardware
13144  * bridge mode enabled.
13145  *
13146  * Note: expects to be called while under rtnl_lock()
13147  **/
13148 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13149 				   struct nlmsghdr *nlh,
13150 				   u16 flags,
13151 				   struct netlink_ext_ack *extack)
13152 {
13153 	struct i40e_netdev_priv *np = netdev_priv(dev);
13154 	struct i40e_vsi *vsi = np->vsi;
13155 	struct i40e_pf *pf = vsi->back;
13156 	struct nlattr *attr, *br_spec;
13157 	struct i40e_veb *veb;
13158 	int rem;
13159 
13160 	/* Only for PF VSI for now */
13161 	if (vsi->type != I40E_VSI_MAIN)
13162 		return -EOPNOTSUPP;
13163 
13164 	/* Find the HW bridge for PF VSI */
13165 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13166 
13167 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13168 	if (!br_spec)
13169 		return -EINVAL;
13170 
13171 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
13172 		__u16 mode = nla_get_u16(attr);
13173 
13174 		if ((mode != BRIDGE_MODE_VEPA) &&
13175 		    (mode != BRIDGE_MODE_VEB))
13176 			return -EINVAL;
13177 
13178 		/* Insert a new HW bridge */
13179 		if (!veb) {
13180 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
13181 					     vsi->tc_config.enabled_tc);
13182 			if (veb) {
13183 				veb->bridge_mode = mode;
13184 				i40e_config_bridge_mode(veb);
13185 			} else {
13186 				/* No Bridge HW offload available */
13187 				return -ENOENT;
13188 			}
13189 			break;
13190 		} else if (mode != veb->bridge_mode) {
13191 			/* Existing HW bridge but different mode needs reset */
13192 			veb->bridge_mode = mode;
13193 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13194 			if (mode == BRIDGE_MODE_VEB)
13195 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13196 			else
13197 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13198 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13199 			break;
13200 		}
13201 	}
13202 
13203 	return 0;
13204 }
13205 
13206 /**
13207  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13208  * @skb: skb buff
13209  * @pid: process id
13210  * @seq: RTNL message seq #
13211  * @dev: the netdev being configured
13212  * @filter_mask: unused
13213  * @nlflags: netlink flags passed in
13214  *
13215  * Return the mode in which the hardware bridge is operating in
13216  * i.e VEB or VEPA.
13217  **/
13218 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13219 				   struct net_device *dev,
13220 				   u32 __always_unused filter_mask,
13221 				   int nlflags)
13222 {
13223 	struct i40e_netdev_priv *np = netdev_priv(dev);
13224 	struct i40e_vsi *vsi = np->vsi;
13225 	struct i40e_pf *pf = vsi->back;
13226 	struct i40e_veb *veb;
13227 
13228 	/* Only for PF VSI for now */
13229 	if (vsi->type != I40E_VSI_MAIN)
13230 		return -EOPNOTSUPP;
13231 
13232 	/* Find the HW bridge for the PF VSI */
13233 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13234 	if (!veb)
13235 		return 0;
13236 
13237 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13238 				       0, 0, nlflags, filter_mask, NULL);
13239 }
13240 
13241 /**
13242  * i40e_features_check - Validate encapsulated packet conforms to limits
13243  * @skb: skb buff
13244  * @dev: This physical port's netdev
13245  * @features: Offload features that the stack believes apply
13246  **/
13247 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13248 					     struct net_device *dev,
13249 					     netdev_features_t features)
13250 {
13251 	size_t len;
13252 
13253 	/* No point in doing any of this if neither checksum nor GSO are
13254 	 * being requested for this frame.  We can rule out both by just
13255 	 * checking for CHECKSUM_PARTIAL
13256 	 */
13257 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13258 		return features;
13259 
13260 	/* We cannot support GSO if the MSS is going to be less than
13261 	 * 64 bytes.  If it is then we need to drop support for GSO.
13262 	 */
13263 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13264 		features &= ~NETIF_F_GSO_MASK;
13265 
13266 	/* MACLEN can support at most 63 words */
13267 	len = skb_network_offset(skb);
13268 	if (len & ~(63 * 2))
13269 		goto out_err;
13270 
13271 	/* IPLEN and EIPLEN can support at most 127 dwords */
13272 	len = skb_network_header_len(skb);
13273 	if (len & ~(127 * 4))
13274 		goto out_err;
13275 
13276 	if (skb->encapsulation) {
13277 		/* L4TUNLEN can support 127 words */
13278 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13279 		if (len & ~(127 * 2))
13280 			goto out_err;
13281 
13282 		/* IPLEN can support at most 127 dwords */
13283 		len = skb_inner_transport_header(skb) -
13284 		      skb_inner_network_header(skb);
13285 		if (len & ~(127 * 4))
13286 			goto out_err;
13287 	}
13288 
13289 	/* No need to validate L4LEN as TCP is the only protocol with a
13290 	 * flexible value and we support all possible values supported
13291 	 * by TCP, which is at most 15 dwords
13292 	 */
13293 
13294 	return features;
13295 out_err:
13296 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13297 }
13298 
13299 /**
13300  * i40e_xdp_setup - add/remove an XDP program
13301  * @vsi: VSI to changed
13302  * @prog: XDP program
13303  * @extack: netlink extended ack
13304  **/
13305 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13306 			  struct netlink_ext_ack *extack)
13307 {
13308 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13309 	struct i40e_pf *pf = vsi->back;
13310 	struct bpf_prog *old_prog;
13311 	bool need_reset;
13312 	int i;
13313 
13314 	/* VSI shall be deleted in a moment, block loading new programs */
13315 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13316 		return -EINVAL;
13317 
13318 	/* Don't allow frames that span over multiple buffers */
13319 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13320 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13321 		return -EINVAL;
13322 	}
13323 
13324 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13325 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13326 	if (need_reset)
13327 		i40e_prep_for_reset(pf);
13328 
13329 	old_prog = xchg(&vsi->xdp_prog, prog);
13330 
13331 	if (need_reset) {
13332 		if (!prog) {
13333 			xdp_features_clear_redirect_target(vsi->netdev);
13334 			/* Wait until ndo_xsk_wakeup completes. */
13335 			synchronize_rcu();
13336 		}
13337 		i40e_reset_and_rebuild(pf, true, true);
13338 	}
13339 
13340 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13341 		if (i40e_realloc_rx_bi_zc(vsi, true))
13342 			return -ENOMEM;
13343 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13344 		if (i40e_realloc_rx_bi_zc(vsi, false))
13345 			return -ENOMEM;
13346 	}
13347 
13348 	for (i = 0; i < vsi->num_queue_pairs; i++)
13349 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13350 
13351 	if (old_prog)
13352 		bpf_prog_put(old_prog);
13353 
13354 	/* Kick start the NAPI context if there is an AF_XDP socket open
13355 	 * on that queue id. This so that receiving will start.
13356 	 */
13357 	if (need_reset && prog) {
13358 		for (i = 0; i < vsi->num_queue_pairs; i++)
13359 			if (vsi->xdp_rings[i]->xsk_pool)
13360 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13361 						      XDP_WAKEUP_RX);
13362 		xdp_features_set_redirect_target(vsi->netdev, true);
13363 	}
13364 
13365 	return 0;
13366 }
13367 
13368 /**
13369  * i40e_enter_busy_conf - Enters busy config state
13370  * @vsi: vsi
13371  *
13372  * Returns 0 on success, <0 for failure.
13373  **/
13374 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13375 {
13376 	struct i40e_pf *pf = vsi->back;
13377 	int timeout = 50;
13378 
13379 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13380 		timeout--;
13381 		if (!timeout)
13382 			return -EBUSY;
13383 		usleep_range(1000, 2000);
13384 	}
13385 
13386 	return 0;
13387 }
13388 
13389 /**
13390  * i40e_exit_busy_conf - Exits busy config state
13391  * @vsi: vsi
13392  **/
13393 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13394 {
13395 	struct i40e_pf *pf = vsi->back;
13396 
13397 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13398 }
13399 
13400 /**
13401  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13402  * @vsi: vsi
13403  * @queue_pair: queue pair
13404  **/
13405 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13406 {
13407 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13408 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13409 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13410 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13411 	if (i40e_enabled_xdp_vsi(vsi)) {
13412 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13413 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13414 	}
13415 }
13416 
13417 /**
13418  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13419  * @vsi: vsi
13420  * @queue_pair: queue pair
13421  **/
13422 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13423 {
13424 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13425 	if (i40e_enabled_xdp_vsi(vsi)) {
13426 		/* Make sure that in-progress ndo_xdp_xmit calls are
13427 		 * completed.
13428 		 */
13429 		synchronize_rcu();
13430 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13431 	}
13432 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13433 }
13434 
13435 /**
13436  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13437  * @vsi: vsi
13438  * @queue_pair: queue pair
13439  * @enable: true for enable, false for disable
13440  **/
13441 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13442 					bool enable)
13443 {
13444 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13445 	struct i40e_q_vector *q_vector = rxr->q_vector;
13446 
13447 	if (!vsi->netdev)
13448 		return;
13449 
13450 	/* All rings in a qp belong to the same qvector. */
13451 	if (q_vector->rx.ring || q_vector->tx.ring) {
13452 		if (enable)
13453 			napi_enable(&q_vector->napi);
13454 		else
13455 			napi_disable(&q_vector->napi);
13456 	}
13457 }
13458 
13459 /**
13460  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13461  * @vsi: vsi
13462  * @queue_pair: queue pair
13463  * @enable: true for enable, false for disable
13464  *
13465  * Returns 0 on success, <0 on failure.
13466  **/
13467 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13468 					bool enable)
13469 {
13470 	struct i40e_pf *pf = vsi->back;
13471 	int pf_q, ret = 0;
13472 
13473 	pf_q = vsi->base_queue + queue_pair;
13474 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13475 				     false /*is xdp*/, enable);
13476 	if (ret) {
13477 		dev_info(&pf->pdev->dev,
13478 			 "VSI seid %d Tx ring %d %sable timeout\n",
13479 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13480 		return ret;
13481 	}
13482 
13483 	i40e_control_rx_q(pf, pf_q, enable);
13484 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13485 	if (ret) {
13486 		dev_info(&pf->pdev->dev,
13487 			 "VSI seid %d Rx ring %d %sable timeout\n",
13488 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13489 		return ret;
13490 	}
13491 
13492 	/* Due to HW errata, on Rx disable only, the register can
13493 	 * indicate done before it really is. Needs 50ms to be sure
13494 	 */
13495 	if (!enable)
13496 		mdelay(50);
13497 
13498 	if (!i40e_enabled_xdp_vsi(vsi))
13499 		return ret;
13500 
13501 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13502 				     pf_q + vsi->alloc_queue_pairs,
13503 				     true /*is xdp*/, enable);
13504 	if (ret) {
13505 		dev_info(&pf->pdev->dev,
13506 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13507 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13508 	}
13509 
13510 	return ret;
13511 }
13512 
13513 /**
13514  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13515  * @vsi: vsi
13516  * @queue_pair: queue_pair
13517  **/
13518 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13519 {
13520 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13521 	struct i40e_pf *pf = vsi->back;
13522 	struct i40e_hw *hw = &pf->hw;
13523 
13524 	/* All rings in a qp belong to the same qvector. */
13525 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13526 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13527 	else
13528 		i40e_irq_dynamic_enable_icr0(pf);
13529 
13530 	i40e_flush(hw);
13531 }
13532 
13533 /**
13534  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13535  * @vsi: vsi
13536  * @queue_pair: queue_pair
13537  **/
13538 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13539 {
13540 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13541 	struct i40e_pf *pf = vsi->back;
13542 	struct i40e_hw *hw = &pf->hw;
13543 
13544 	/* For simplicity, instead of removing the qp interrupt causes
13545 	 * from the interrupt linked list, we simply disable the interrupt, and
13546 	 * leave the list intact.
13547 	 *
13548 	 * All rings in a qp belong to the same qvector.
13549 	 */
13550 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13551 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13552 
13553 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13554 		i40e_flush(hw);
13555 		synchronize_irq(pf->msix_entries[intpf].vector);
13556 	} else {
13557 		/* Legacy and MSI mode - this stops all interrupt handling */
13558 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13559 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13560 		i40e_flush(hw);
13561 		synchronize_irq(pf->pdev->irq);
13562 	}
13563 }
13564 
13565 /**
13566  * i40e_queue_pair_disable - Disables a queue pair
13567  * @vsi: vsi
13568  * @queue_pair: queue pair
13569  *
13570  * Returns 0 on success, <0 on failure.
13571  **/
13572 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13573 {
13574 	int err;
13575 
13576 	err = i40e_enter_busy_conf(vsi);
13577 	if (err)
13578 		return err;
13579 
13580 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13581 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13582 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13583 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13584 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13585 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13586 
13587 	return err;
13588 }
13589 
13590 /**
13591  * i40e_queue_pair_enable - Enables a queue pair
13592  * @vsi: vsi
13593  * @queue_pair: queue pair
13594  *
13595  * Returns 0 on success, <0 on failure.
13596  **/
13597 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13598 {
13599 	int err;
13600 
13601 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13602 	if (err)
13603 		return err;
13604 
13605 	if (i40e_enabled_xdp_vsi(vsi)) {
13606 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13607 		if (err)
13608 			return err;
13609 	}
13610 
13611 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13612 	if (err)
13613 		return err;
13614 
13615 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13616 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13617 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13618 
13619 	i40e_exit_busy_conf(vsi);
13620 
13621 	return err;
13622 }
13623 
13624 /**
13625  * i40e_xdp - implements ndo_bpf for i40e
13626  * @dev: netdevice
13627  * @xdp: XDP command
13628  **/
13629 static int i40e_xdp(struct net_device *dev,
13630 		    struct netdev_bpf *xdp)
13631 {
13632 	struct i40e_netdev_priv *np = netdev_priv(dev);
13633 	struct i40e_vsi *vsi = np->vsi;
13634 
13635 	if (vsi->type != I40E_VSI_MAIN)
13636 		return -EINVAL;
13637 
13638 	switch (xdp->command) {
13639 	case XDP_SETUP_PROG:
13640 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13641 	case XDP_SETUP_XSK_POOL:
13642 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13643 					   xdp->xsk.queue_id);
13644 	default:
13645 		return -EINVAL;
13646 	}
13647 }
13648 
13649 static const struct net_device_ops i40e_netdev_ops = {
13650 	.ndo_open		= i40e_open,
13651 	.ndo_stop		= i40e_close,
13652 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13653 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13654 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13655 	.ndo_validate_addr	= eth_validate_addr,
13656 	.ndo_set_mac_address	= i40e_set_mac,
13657 	.ndo_change_mtu		= i40e_change_mtu,
13658 	.ndo_eth_ioctl		= i40e_ioctl,
13659 	.ndo_tx_timeout		= i40e_tx_timeout,
13660 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13661 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13662 #ifdef CONFIG_NET_POLL_CONTROLLER
13663 	.ndo_poll_controller	= i40e_netpoll,
13664 #endif
13665 	.ndo_setup_tc		= __i40e_setup_tc,
13666 	.ndo_select_queue	= i40e_lan_select_queue,
13667 	.ndo_set_features	= i40e_set_features,
13668 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13669 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13670 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13671 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13672 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13673 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13674 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13675 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13676 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13677 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13678 	.ndo_features_check	= i40e_features_check,
13679 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13680 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13681 	.ndo_bpf		= i40e_xdp,
13682 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13683 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13684 	.ndo_dfwd_add_station	= i40e_fwd_add,
13685 	.ndo_dfwd_del_station	= i40e_fwd_del,
13686 };
13687 
13688 /**
13689  * i40e_config_netdev - Setup the netdev flags
13690  * @vsi: the VSI being configured
13691  *
13692  * Returns 0 on success, negative value on failure
13693  **/
13694 static int i40e_config_netdev(struct i40e_vsi *vsi)
13695 {
13696 	struct i40e_pf *pf = vsi->back;
13697 	struct i40e_hw *hw = &pf->hw;
13698 	struct i40e_netdev_priv *np;
13699 	struct net_device *netdev;
13700 	u8 broadcast[ETH_ALEN];
13701 	u8 mac_addr[ETH_ALEN];
13702 	int etherdev_size;
13703 	netdev_features_t hw_enc_features;
13704 	netdev_features_t hw_features;
13705 
13706 	etherdev_size = sizeof(struct i40e_netdev_priv);
13707 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13708 	if (!netdev)
13709 		return -ENOMEM;
13710 
13711 	vsi->netdev = netdev;
13712 	np = netdev_priv(netdev);
13713 	np->vsi = vsi;
13714 
13715 	hw_enc_features = NETIF_F_SG			|
13716 			  NETIF_F_HW_CSUM		|
13717 			  NETIF_F_HIGHDMA		|
13718 			  NETIF_F_SOFT_FEATURES		|
13719 			  NETIF_F_TSO			|
13720 			  NETIF_F_TSO_ECN		|
13721 			  NETIF_F_TSO6			|
13722 			  NETIF_F_GSO_GRE		|
13723 			  NETIF_F_GSO_GRE_CSUM		|
13724 			  NETIF_F_GSO_PARTIAL		|
13725 			  NETIF_F_GSO_IPXIP4		|
13726 			  NETIF_F_GSO_IPXIP6		|
13727 			  NETIF_F_GSO_UDP_TUNNEL	|
13728 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13729 			  NETIF_F_GSO_UDP_L4		|
13730 			  NETIF_F_SCTP_CRC		|
13731 			  NETIF_F_RXHASH		|
13732 			  NETIF_F_RXCSUM		|
13733 			  0;
13734 
13735 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13736 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13737 
13738 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13739 
13740 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13741 
13742 	netdev->hw_enc_features |= hw_enc_features;
13743 
13744 	/* record features VLANs can make use of */
13745 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13746 
13747 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13748 				   NETIF_F_GSO_GRE_CSUM |	\
13749 				   NETIF_F_GSO_IPXIP4 |		\
13750 				   NETIF_F_GSO_IPXIP6 |		\
13751 				   NETIF_F_GSO_UDP_TUNNEL |	\
13752 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13753 
13754 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13755 	netdev->features |= NETIF_F_GSO_PARTIAL |
13756 			    I40E_GSO_PARTIAL_FEATURES;
13757 
13758 	netdev->mpls_features |= NETIF_F_SG;
13759 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13760 	netdev->mpls_features |= NETIF_F_TSO;
13761 	netdev->mpls_features |= NETIF_F_TSO6;
13762 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13763 
13764 	/* enable macvlan offloads */
13765 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13766 
13767 	hw_features = hw_enc_features		|
13768 		      NETIF_F_HW_VLAN_CTAG_TX	|
13769 		      NETIF_F_HW_VLAN_CTAG_RX;
13770 
13771 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13772 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13773 
13774 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13775 
13776 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13777 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13778 
13779 	netdev->features &= ~NETIF_F_HW_TC;
13780 
13781 	if (vsi->type == I40E_VSI_MAIN) {
13782 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13783 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13784 		/* The following steps are necessary for two reasons. First,
13785 		 * some older NVM configurations load a default MAC-VLAN
13786 		 * filter that will accept any tagged packet, and we want to
13787 		 * replace this with a normal filter. Additionally, it is
13788 		 * possible our MAC address was provided by the platform using
13789 		 * Open Firmware or similar.
13790 		 *
13791 		 * Thus, we need to remove the default filter and install one
13792 		 * specific to the MAC address.
13793 		 */
13794 		i40e_rm_default_mac_filter(vsi, mac_addr);
13795 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13796 		i40e_add_mac_filter(vsi, mac_addr);
13797 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13798 
13799 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13800 				       NETDEV_XDP_ACT_REDIRECT |
13801 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13802 				       NETDEV_XDP_ACT_RX_SG;
13803 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13804 	} else {
13805 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13806 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13807 		 * the end, which is 4 bytes long, so force truncation of the
13808 		 * original name by IFNAMSIZ - 4
13809 		 */
13810 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
13811 
13812 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
13813 			 main_vsi->netdev->name);
13814 		eth_random_addr(mac_addr);
13815 
13816 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13817 		i40e_add_mac_filter(vsi, mac_addr);
13818 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13819 	}
13820 
13821 	/* Add the broadcast filter so that we initially will receive
13822 	 * broadcast packets. Note that when a new VLAN is first added the
13823 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13824 	 * specific filters as part of transitioning into "vlan" operation.
13825 	 * When more VLANs are added, the driver will copy each existing MAC
13826 	 * filter and add it for the new VLAN.
13827 	 *
13828 	 * Broadcast filters are handled specially by
13829 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13830 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13831 	 * filter. The subtask will update the correct broadcast promiscuous
13832 	 * bits as VLANs become active or inactive.
13833 	 */
13834 	eth_broadcast_addr(broadcast);
13835 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13836 	i40e_add_mac_filter(vsi, broadcast);
13837 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13838 
13839 	eth_hw_addr_set(netdev, mac_addr);
13840 	ether_addr_copy(netdev->perm_addr, mac_addr);
13841 
13842 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13843 	netdev->neigh_priv_len = sizeof(u32) * 4;
13844 
13845 	netdev->priv_flags |= IFF_UNICAST_FLT;
13846 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13847 	/* Setup netdev TC information */
13848 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13849 
13850 	netdev->netdev_ops = &i40e_netdev_ops;
13851 	netdev->watchdog_timeo = 5 * HZ;
13852 	i40e_set_ethtool_ops(netdev);
13853 
13854 	/* MTU range: 68 - 9706 */
13855 	netdev->min_mtu = ETH_MIN_MTU;
13856 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13857 
13858 	return 0;
13859 }
13860 
13861 /**
13862  * i40e_vsi_delete - Delete a VSI from the switch
13863  * @vsi: the VSI being removed
13864  *
13865  * Returns 0 on success, negative value on failure
13866  **/
13867 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13868 {
13869 	/* remove default VSI is not allowed */
13870 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13871 		return;
13872 
13873 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13874 }
13875 
13876 /**
13877  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13878  * @vsi: the VSI being queried
13879  *
13880  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13881  **/
13882 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13883 {
13884 	struct i40e_veb *veb;
13885 	struct i40e_pf *pf = vsi->back;
13886 
13887 	/* Uplink is not a bridge so default to VEB */
13888 	if (vsi->veb_idx >= I40E_MAX_VEB)
13889 		return 1;
13890 
13891 	veb = pf->veb[vsi->veb_idx];
13892 	if (!veb) {
13893 		dev_info(&pf->pdev->dev,
13894 			 "There is no veb associated with the bridge\n");
13895 		return -ENOENT;
13896 	}
13897 
13898 	/* Uplink is a bridge in VEPA mode */
13899 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13900 		return 0;
13901 	} else {
13902 		/* Uplink is a bridge in VEB mode */
13903 		return 1;
13904 	}
13905 
13906 	/* VEPA is now default bridge, so return 0 */
13907 	return 0;
13908 }
13909 
13910 /**
13911  * i40e_add_vsi - Add a VSI to the switch
13912  * @vsi: the VSI being configured
13913  *
13914  * This initializes a VSI context depending on the VSI type to be added and
13915  * passes it down to the add_vsi aq command.
13916  **/
13917 static int i40e_add_vsi(struct i40e_vsi *vsi)
13918 {
13919 	int ret = -ENODEV;
13920 	struct i40e_pf *pf = vsi->back;
13921 	struct i40e_hw *hw = &pf->hw;
13922 	struct i40e_vsi_context ctxt;
13923 	struct i40e_mac_filter *f;
13924 	struct hlist_node *h;
13925 	int bkt;
13926 
13927 	u8 enabled_tc = 0x1; /* TC0 enabled */
13928 	int f_count = 0;
13929 
13930 	memset(&ctxt, 0, sizeof(ctxt));
13931 	switch (vsi->type) {
13932 	case I40E_VSI_MAIN:
13933 		/* The PF's main VSI is already setup as part of the
13934 		 * device initialization, so we'll not bother with
13935 		 * the add_vsi call, but we will retrieve the current
13936 		 * VSI context.
13937 		 */
13938 		ctxt.seid = pf->main_vsi_seid;
13939 		ctxt.pf_num = pf->hw.pf_id;
13940 		ctxt.vf_num = 0;
13941 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13942 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13943 		if (ret) {
13944 			dev_info(&pf->pdev->dev,
13945 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13946 				 ERR_PTR(ret),
13947 				 i40e_aq_str(&pf->hw,
13948 					     pf->hw.aq.asq_last_status));
13949 			return -ENOENT;
13950 		}
13951 		vsi->info = ctxt.info;
13952 		vsi->info.valid_sections = 0;
13953 
13954 		vsi->seid = ctxt.seid;
13955 		vsi->id = ctxt.vsi_number;
13956 
13957 		enabled_tc = i40e_pf_get_tc_map(pf);
13958 
13959 		/* Source pruning is enabled by default, so the flag is
13960 		 * negative logic - if it's set, we need to fiddle with
13961 		 * the VSI to disable source pruning.
13962 		 */
13963 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13964 			memset(&ctxt, 0, sizeof(ctxt));
13965 			ctxt.seid = pf->main_vsi_seid;
13966 			ctxt.pf_num = pf->hw.pf_id;
13967 			ctxt.vf_num = 0;
13968 			ctxt.info.valid_sections |=
13969 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13970 			ctxt.info.switch_id =
13971 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13972 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13973 			if (ret) {
13974 				dev_info(&pf->pdev->dev,
13975 					 "update vsi failed, err %d aq_err %s\n",
13976 					 ret,
13977 					 i40e_aq_str(&pf->hw,
13978 						     pf->hw.aq.asq_last_status));
13979 				ret = -ENOENT;
13980 				goto err;
13981 			}
13982 		}
13983 
13984 		/* MFP mode setup queue map and update VSI */
13985 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13986 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13987 			memset(&ctxt, 0, sizeof(ctxt));
13988 			ctxt.seid = pf->main_vsi_seid;
13989 			ctxt.pf_num = pf->hw.pf_id;
13990 			ctxt.vf_num = 0;
13991 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13992 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13993 			if (ret) {
13994 				dev_info(&pf->pdev->dev,
13995 					 "update vsi failed, err %pe aq_err %s\n",
13996 					 ERR_PTR(ret),
13997 					 i40e_aq_str(&pf->hw,
13998 						    pf->hw.aq.asq_last_status));
13999 				ret = -ENOENT;
14000 				goto err;
14001 			}
14002 			/* update the local VSI info queue map */
14003 			i40e_vsi_update_queue_map(vsi, &ctxt);
14004 			vsi->info.valid_sections = 0;
14005 		} else {
14006 			/* Default/Main VSI is only enabled for TC0
14007 			 * reconfigure it to enable all TCs that are
14008 			 * available on the port in SFP mode.
14009 			 * For MFP case the iSCSI PF would use this
14010 			 * flow to enable LAN+iSCSI TC.
14011 			 */
14012 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
14013 			if (ret) {
14014 				/* Single TC condition is not fatal,
14015 				 * message and continue
14016 				 */
14017 				dev_info(&pf->pdev->dev,
14018 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14019 					 enabled_tc,
14020 					 ERR_PTR(ret),
14021 					 i40e_aq_str(&pf->hw,
14022 						    pf->hw.aq.asq_last_status));
14023 			}
14024 		}
14025 		break;
14026 
14027 	case I40E_VSI_FDIR:
14028 		ctxt.pf_num = hw->pf_id;
14029 		ctxt.vf_num = 0;
14030 		ctxt.uplink_seid = vsi->uplink_seid;
14031 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14032 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14033 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
14034 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14035 			ctxt.info.valid_sections |=
14036 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14037 			ctxt.info.switch_id =
14038 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14039 		}
14040 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14041 		break;
14042 
14043 	case I40E_VSI_VMDQ2:
14044 		ctxt.pf_num = hw->pf_id;
14045 		ctxt.vf_num = 0;
14046 		ctxt.uplink_seid = vsi->uplink_seid;
14047 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14048 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14049 
14050 		/* This VSI is connected to VEB so the switch_id
14051 		 * should be set to zero by default.
14052 		 */
14053 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14054 			ctxt.info.valid_sections |=
14055 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14056 			ctxt.info.switch_id =
14057 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14058 		}
14059 
14060 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14061 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14062 		break;
14063 
14064 	case I40E_VSI_SRIOV:
14065 		ctxt.pf_num = hw->pf_id;
14066 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14067 		ctxt.uplink_seid = vsi->uplink_seid;
14068 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14069 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14070 
14071 		/* This VSI is connected to VEB so the switch_id
14072 		 * should be set to zero by default.
14073 		 */
14074 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14075 			ctxt.info.valid_sections |=
14076 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14077 			ctxt.info.switch_id =
14078 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14079 		}
14080 
14081 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14082 			ctxt.info.valid_sections |=
14083 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14084 			ctxt.info.queueing_opt_flags |=
14085 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14086 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14087 		}
14088 
14089 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14090 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14091 		if (pf->vf[vsi->vf_id].spoofchk) {
14092 			ctxt.info.valid_sections |=
14093 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14094 			ctxt.info.sec_flags |=
14095 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14096 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14097 		}
14098 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14099 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14100 		break;
14101 
14102 	case I40E_VSI_IWARP:
14103 		/* send down message to iWARP */
14104 		break;
14105 
14106 	default:
14107 		return -ENODEV;
14108 	}
14109 
14110 	if (vsi->type != I40E_VSI_MAIN) {
14111 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14112 		if (ret) {
14113 			dev_info(&vsi->back->pdev->dev,
14114 				 "add vsi failed, err %pe aq_err %s\n",
14115 				 ERR_PTR(ret),
14116 				 i40e_aq_str(&pf->hw,
14117 					     pf->hw.aq.asq_last_status));
14118 			ret = -ENOENT;
14119 			goto err;
14120 		}
14121 		vsi->info = ctxt.info;
14122 		vsi->info.valid_sections = 0;
14123 		vsi->seid = ctxt.seid;
14124 		vsi->id = ctxt.vsi_number;
14125 	}
14126 
14127 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14128 	vsi->active_filters = 0;
14129 	/* If macvlan filters already exist, force them to get loaded */
14130 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14131 		f->state = I40E_FILTER_NEW;
14132 		f_count++;
14133 	}
14134 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14135 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14136 
14137 	if (f_count) {
14138 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14139 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14140 	}
14141 
14142 	/* Update VSI BW information */
14143 	ret = i40e_vsi_get_bw_info(vsi);
14144 	if (ret) {
14145 		dev_info(&pf->pdev->dev,
14146 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14147 			 ERR_PTR(ret),
14148 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14149 		/* VSI is already added so not tearing that up */
14150 		ret = 0;
14151 	}
14152 
14153 err:
14154 	return ret;
14155 }
14156 
14157 /**
14158  * i40e_vsi_release - Delete a VSI and free its resources
14159  * @vsi: the VSI being removed
14160  *
14161  * Returns 0 on success or < 0 on error
14162  **/
14163 int i40e_vsi_release(struct i40e_vsi *vsi)
14164 {
14165 	struct i40e_mac_filter *f;
14166 	struct hlist_node *h;
14167 	struct i40e_veb *veb;
14168 	struct i40e_pf *pf;
14169 	u16 uplink_seid;
14170 	int i, n, bkt;
14171 
14172 	pf = vsi->back;
14173 
14174 	/* release of a VEB-owner or last VSI is not allowed */
14175 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14176 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14177 			 vsi->seid, vsi->uplink_seid);
14178 		return -ENODEV;
14179 	}
14180 	if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
14181 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14182 		return -ENODEV;
14183 	}
14184 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14185 	uplink_seid = vsi->uplink_seid;
14186 
14187 	if (vsi->type != I40E_VSI_SRIOV) {
14188 		if (vsi->netdev_registered) {
14189 			vsi->netdev_registered = false;
14190 			if (vsi->netdev) {
14191 				/* results in a call to i40e_close() */
14192 				unregister_netdev(vsi->netdev);
14193 			}
14194 		} else {
14195 			i40e_vsi_close(vsi);
14196 		}
14197 		i40e_vsi_disable_irq(vsi);
14198 	}
14199 
14200 	if (vsi->type == I40E_VSI_MAIN)
14201 		i40e_devlink_destroy_port(pf);
14202 
14203 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14204 
14205 	/* clear the sync flag on all filters */
14206 	if (vsi->netdev) {
14207 		__dev_uc_unsync(vsi->netdev, NULL);
14208 		__dev_mc_unsync(vsi->netdev, NULL);
14209 	}
14210 
14211 	/* make sure any remaining filters are marked for deletion */
14212 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14213 		__i40e_del_filter(vsi, f);
14214 
14215 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14216 
14217 	i40e_sync_vsi_filters(vsi);
14218 
14219 	i40e_vsi_delete(vsi);
14220 	i40e_vsi_free_q_vectors(vsi);
14221 	if (vsi->netdev) {
14222 		free_netdev(vsi->netdev);
14223 		vsi->netdev = NULL;
14224 	}
14225 	i40e_vsi_clear_rings(vsi);
14226 	i40e_vsi_clear(vsi);
14227 
14228 	/* If this was the last thing on the VEB, except for the
14229 	 * controlling VSI, remove the VEB, which puts the controlling
14230 	 * VSI onto the uplink port.
14231 	 *
14232 	 * Well, okay, there's one more exception here: don't remove
14233 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14234 	 * from up the network stack.
14235 	 */
14236 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14237 	if (veb && veb->uplink_seid) {
14238 		n = 0;
14239 
14240 		/* Count non-controlling VSIs present on  the VEB */
14241 		i40e_pf_for_each_vsi(pf, i, vsi)
14242 			if (vsi->uplink_seid == uplink_seid &&
14243 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14244 				n++;
14245 
14246 		/* If there is no VSI except the control one then release
14247 		 * the VEB and put the control VSI onto VEB uplink.
14248 		 */
14249 		if (!n)
14250 			i40e_veb_release(veb);
14251 	}
14252 
14253 	return 0;
14254 }
14255 
14256 /**
14257  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14258  * @vsi: ptr to the VSI
14259  *
14260  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14261  * corresponding SW VSI structure and initializes num_queue_pairs for the
14262  * newly allocated VSI.
14263  *
14264  * Returns 0 on success or negative on failure
14265  **/
14266 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14267 {
14268 	int ret = -ENOENT;
14269 	struct i40e_pf *pf = vsi->back;
14270 
14271 	if (vsi->q_vectors[0]) {
14272 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14273 			 vsi->seid);
14274 		return -EEXIST;
14275 	}
14276 
14277 	if (vsi->base_vector) {
14278 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14279 			 vsi->seid, vsi->base_vector);
14280 		return -EEXIST;
14281 	}
14282 
14283 	ret = i40e_vsi_alloc_q_vectors(vsi);
14284 	if (ret) {
14285 		dev_info(&pf->pdev->dev,
14286 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14287 			 vsi->num_q_vectors, vsi->seid, ret);
14288 		vsi->num_q_vectors = 0;
14289 		goto vector_setup_out;
14290 	}
14291 
14292 	/* In Legacy mode, we do not have to get any other vector since we
14293 	 * piggyback on the misc/ICR0 for queue interrupts.
14294 	*/
14295 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14296 		return ret;
14297 	if (vsi->num_q_vectors)
14298 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14299 						 vsi->num_q_vectors, vsi->idx);
14300 	if (vsi->base_vector < 0) {
14301 		dev_info(&pf->pdev->dev,
14302 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14303 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14304 		i40e_vsi_free_q_vectors(vsi);
14305 		ret = -ENOENT;
14306 		goto vector_setup_out;
14307 	}
14308 
14309 vector_setup_out:
14310 	return ret;
14311 }
14312 
14313 /**
14314  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14315  * @vsi: pointer to the vsi.
14316  *
14317  * This re-allocates a vsi's queue resources.
14318  *
14319  * Returns pointer to the successfully allocated and configured VSI sw struct
14320  * on success, otherwise returns NULL on failure.
14321  **/
14322 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14323 {
14324 	struct i40e_vsi *main_vsi;
14325 	u16 alloc_queue_pairs;
14326 	struct i40e_pf *pf;
14327 	int ret;
14328 
14329 	if (!vsi)
14330 		return NULL;
14331 
14332 	pf = vsi->back;
14333 
14334 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14335 	i40e_vsi_clear_rings(vsi);
14336 
14337 	i40e_vsi_free_arrays(vsi, false);
14338 	i40e_set_num_rings_in_vsi(vsi);
14339 	ret = i40e_vsi_alloc_arrays(vsi, false);
14340 	if (ret)
14341 		goto err_vsi;
14342 
14343 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14344 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14345 
14346 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14347 	if (ret < 0) {
14348 		dev_info(&pf->pdev->dev,
14349 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14350 			 alloc_queue_pairs, vsi->seid, ret);
14351 		goto err_vsi;
14352 	}
14353 	vsi->base_queue = ret;
14354 
14355 	/* Update the FW view of the VSI. Force a reset of TC and queue
14356 	 * layout configurations.
14357 	 */
14358 	main_vsi = i40e_pf_get_main_vsi(pf);
14359 	main_vsi->seid = pf->main_vsi_seid;
14360 	i40e_vsi_reconfig_tc(main_vsi);
14361 
14362 	if (vsi->type == I40E_VSI_MAIN)
14363 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14364 
14365 	/* assign it some queues */
14366 	ret = i40e_alloc_rings(vsi);
14367 	if (ret)
14368 		goto err_rings;
14369 
14370 	/* map all of the rings to the q_vectors */
14371 	i40e_vsi_map_rings_to_vectors(vsi);
14372 	return vsi;
14373 
14374 err_rings:
14375 	i40e_vsi_free_q_vectors(vsi);
14376 	if (vsi->netdev_registered) {
14377 		vsi->netdev_registered = false;
14378 		unregister_netdev(vsi->netdev);
14379 		free_netdev(vsi->netdev);
14380 		vsi->netdev = NULL;
14381 	}
14382 	if (vsi->type == I40E_VSI_MAIN)
14383 		i40e_devlink_destroy_port(pf);
14384 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14385 err_vsi:
14386 	i40e_vsi_clear(vsi);
14387 	return NULL;
14388 }
14389 
14390 /**
14391  * i40e_vsi_setup - Set up a VSI by a given type
14392  * @pf: board private structure
14393  * @type: VSI type
14394  * @uplink_seid: the switch element to link to
14395  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14396  *
14397  * This allocates the sw VSI structure and its queue resources, then add a VSI
14398  * to the identified VEB.
14399  *
14400  * Returns pointer to the successfully allocated and configure VSI sw struct on
14401  * success, otherwise returns NULL on failure.
14402  **/
14403 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14404 				u16 uplink_seid, u32 param1)
14405 {
14406 	struct i40e_vsi *vsi = NULL;
14407 	struct i40e_veb *veb = NULL;
14408 	u16 alloc_queue_pairs;
14409 	int v_idx;
14410 	int ret;
14411 
14412 	/* The requested uplink_seid must be either
14413 	 *     - the PF's port seid
14414 	 *              no VEB is needed because this is the PF
14415 	 *              or this is a Flow Director special case VSI
14416 	 *     - seid of an existing VEB
14417 	 *     - seid of a VSI that owns an existing VEB
14418 	 *     - seid of a VSI that doesn't own a VEB
14419 	 *              a new VEB is created and the VSI becomes the owner
14420 	 *     - seid of the PF VSI, which is what creates the first VEB
14421 	 *              this is a special case of the previous
14422 	 *
14423 	 * Find which uplink_seid we were given and create a new VEB if needed
14424 	 */
14425 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14426 	if (!veb && uplink_seid != pf->mac_seid) {
14427 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14428 		if (!vsi) {
14429 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14430 				 uplink_seid);
14431 			return NULL;
14432 		}
14433 
14434 		if (vsi->uplink_seid == pf->mac_seid)
14435 			veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
14436 					     vsi->tc_config.enabled_tc);
14437 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14438 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
14439 					     vsi->tc_config.enabled_tc);
14440 		if (veb) {
14441 			if (vsi->type != I40E_VSI_MAIN) {
14442 				dev_info(&vsi->back->pdev->dev,
14443 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14444 				return NULL;
14445 			}
14446 			/* We come up by default in VEPA mode if SRIOV is not
14447 			 * already enabled, in which case we can't force VEPA
14448 			 * mode.
14449 			 */
14450 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14451 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14452 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14453 			}
14454 			i40e_config_bridge_mode(veb);
14455 		}
14456 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14457 		if (!veb) {
14458 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14459 			return NULL;
14460 		}
14461 
14462 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14463 		uplink_seid = veb->seid;
14464 	}
14465 
14466 	/* get vsi sw struct */
14467 	v_idx = i40e_vsi_mem_alloc(pf, type);
14468 	if (v_idx < 0)
14469 		goto err_alloc;
14470 	vsi = pf->vsi[v_idx];
14471 	if (!vsi)
14472 		goto err_alloc;
14473 	vsi->type = type;
14474 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14475 
14476 	if (type == I40E_VSI_MAIN)
14477 		pf->lan_vsi = v_idx;
14478 	else if (type == I40E_VSI_SRIOV)
14479 		vsi->vf_id = param1;
14480 	/* assign it some queues */
14481 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14482 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14483 
14484 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14485 	if (ret < 0) {
14486 		dev_info(&pf->pdev->dev,
14487 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14488 			 alloc_queue_pairs, vsi->seid, ret);
14489 		goto err_vsi;
14490 	}
14491 	vsi->base_queue = ret;
14492 
14493 	/* get a VSI from the hardware */
14494 	vsi->uplink_seid = uplink_seid;
14495 	ret = i40e_add_vsi(vsi);
14496 	if (ret)
14497 		goto err_vsi;
14498 
14499 	switch (vsi->type) {
14500 	/* setup the netdev if needed */
14501 	case I40E_VSI_MAIN:
14502 	case I40E_VSI_VMDQ2:
14503 		ret = i40e_config_netdev(vsi);
14504 		if (ret)
14505 			goto err_netdev;
14506 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14507 		if (ret)
14508 			goto err_netdev;
14509 		if (vsi->type == I40E_VSI_MAIN) {
14510 			ret = i40e_devlink_create_port(pf);
14511 			if (ret)
14512 				goto err_netdev;
14513 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14514 		}
14515 		ret = register_netdev(vsi->netdev);
14516 		if (ret)
14517 			goto err_dl_port;
14518 		vsi->netdev_registered = true;
14519 		netif_carrier_off(vsi->netdev);
14520 #ifdef CONFIG_I40E_DCB
14521 		/* Setup DCB netlink interface */
14522 		i40e_dcbnl_setup(vsi);
14523 #endif /* CONFIG_I40E_DCB */
14524 		fallthrough;
14525 	case I40E_VSI_FDIR:
14526 		/* set up vectors and rings if needed */
14527 		ret = i40e_vsi_setup_vectors(vsi);
14528 		if (ret)
14529 			goto err_msix;
14530 
14531 		ret = i40e_alloc_rings(vsi);
14532 		if (ret)
14533 			goto err_rings;
14534 
14535 		/* map all of the rings to the q_vectors */
14536 		i40e_vsi_map_rings_to_vectors(vsi);
14537 
14538 		i40e_vsi_reset_stats(vsi);
14539 		break;
14540 	default:
14541 		/* no netdev or rings for the other VSI types */
14542 		break;
14543 	}
14544 
14545 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14546 	    vsi->type == I40E_VSI_VMDQ2) {
14547 		ret = i40e_vsi_config_rss(vsi);
14548 		if (ret)
14549 			goto err_config;
14550 	}
14551 	return vsi;
14552 
14553 err_config:
14554 	i40e_vsi_clear_rings(vsi);
14555 err_rings:
14556 	i40e_vsi_free_q_vectors(vsi);
14557 err_msix:
14558 	if (vsi->netdev_registered) {
14559 		vsi->netdev_registered = false;
14560 		unregister_netdev(vsi->netdev);
14561 		free_netdev(vsi->netdev);
14562 		vsi->netdev = NULL;
14563 	}
14564 err_dl_port:
14565 	if (vsi->type == I40E_VSI_MAIN)
14566 		i40e_devlink_destroy_port(pf);
14567 err_netdev:
14568 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14569 err_vsi:
14570 	i40e_vsi_clear(vsi);
14571 err_alloc:
14572 	return NULL;
14573 }
14574 
14575 /**
14576  * i40e_veb_get_bw_info - Query VEB BW information
14577  * @veb: the veb to query
14578  *
14579  * Query the Tx scheduler BW configuration data for given VEB
14580  **/
14581 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14582 {
14583 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14584 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14585 	struct i40e_pf *pf = veb->pf;
14586 	struct i40e_hw *hw = &pf->hw;
14587 	u32 tc_bw_max;
14588 	int ret = 0;
14589 	int i;
14590 
14591 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14592 						  &bw_data, NULL);
14593 	if (ret) {
14594 		dev_info(&pf->pdev->dev,
14595 			 "query veb bw config failed, err %pe aq_err %s\n",
14596 			 ERR_PTR(ret),
14597 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14598 		goto out;
14599 	}
14600 
14601 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14602 						   &ets_data, NULL);
14603 	if (ret) {
14604 		dev_info(&pf->pdev->dev,
14605 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14606 			 ERR_PTR(ret),
14607 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14608 		goto out;
14609 	}
14610 
14611 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14612 	veb->bw_max_quanta = ets_data.tc_bw_max;
14613 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14614 	veb->enabled_tc = ets_data.tc_valid_bits;
14615 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14616 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14617 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14618 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14619 		veb->bw_tc_limit_credits[i] =
14620 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14621 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14622 	}
14623 
14624 out:
14625 	return ret;
14626 }
14627 
14628 /**
14629  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14630  * @pf: board private structure
14631  *
14632  * On error: returns error code (negative)
14633  * On success: returns vsi index in PF (positive)
14634  **/
14635 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14636 {
14637 	int ret = -ENOENT;
14638 	struct i40e_veb *veb;
14639 	int i;
14640 
14641 	/* Need to protect the allocation of switch elements at the PF level */
14642 	mutex_lock(&pf->switch_mutex);
14643 
14644 	/* VEB list may be fragmented if VEB creation/destruction has
14645 	 * been happening.  We can afford to do a quick scan to look
14646 	 * for any free slots in the list.
14647 	 *
14648 	 * find next empty veb slot, looping back around if necessary
14649 	 */
14650 	i = 0;
14651 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14652 		i++;
14653 	if (i >= I40E_MAX_VEB) {
14654 		ret = -ENOMEM;
14655 		goto err_alloc_veb;  /* out of VEB slots! */
14656 	}
14657 
14658 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14659 	if (!veb) {
14660 		ret = -ENOMEM;
14661 		goto err_alloc_veb;
14662 	}
14663 	veb->pf = pf;
14664 	veb->idx = i;
14665 	veb->enabled_tc = 1;
14666 
14667 	pf->veb[i] = veb;
14668 	ret = i;
14669 err_alloc_veb:
14670 	mutex_unlock(&pf->switch_mutex);
14671 	return ret;
14672 }
14673 
14674 /**
14675  * i40e_switch_branch_release - Delete a branch of the switch tree
14676  * @branch: where to start deleting
14677  *
14678  * This uses recursion to find the tips of the branch to be
14679  * removed, deleting until we get back to and can delete this VEB.
14680  **/
14681 static void i40e_switch_branch_release(struct i40e_veb *branch)
14682 {
14683 	struct i40e_pf *pf = branch->pf;
14684 	u16 branch_seid = branch->seid;
14685 	u16 veb_idx = branch->idx;
14686 	struct i40e_vsi *vsi;
14687 	struct i40e_veb *veb;
14688 	int i;
14689 
14690 	/* release any VEBs on this VEB - RECURSION */
14691 	i40e_pf_for_each_veb(pf, i, veb)
14692 		if (veb->uplink_seid == branch->seid)
14693 			i40e_switch_branch_release(veb);
14694 
14695 	/* Release the VSIs on this VEB, but not the owner VSI.
14696 	 *
14697 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14698 	 *       the VEB itself, so don't use (*branch) after this loop.
14699 	 */
14700 	i40e_pf_for_each_vsi(pf, i, vsi)
14701 		if (vsi->uplink_seid == branch_seid &&
14702 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14703 			i40e_vsi_release(vsi);
14704 
14705 	/* There's one corner case where the VEB might not have been
14706 	 * removed, so double check it here and remove it if needed.
14707 	 * This case happens if the veb was created from the debugfs
14708 	 * commands and no VSIs were added to it.
14709 	 */
14710 	if (pf->veb[veb_idx])
14711 		i40e_veb_release(pf->veb[veb_idx]);
14712 }
14713 
14714 /**
14715  * i40e_veb_clear - remove veb struct
14716  * @veb: the veb to remove
14717  **/
14718 static void i40e_veb_clear(struct i40e_veb *veb)
14719 {
14720 	if (!veb)
14721 		return;
14722 
14723 	if (veb->pf) {
14724 		struct i40e_pf *pf = veb->pf;
14725 
14726 		mutex_lock(&pf->switch_mutex);
14727 		if (pf->veb[veb->idx] == veb)
14728 			pf->veb[veb->idx] = NULL;
14729 		mutex_unlock(&pf->switch_mutex);
14730 	}
14731 
14732 	kfree(veb);
14733 }
14734 
14735 /**
14736  * i40e_veb_release - Delete a VEB and free its resources
14737  * @veb: the VEB being removed
14738  **/
14739 void i40e_veb_release(struct i40e_veb *veb)
14740 {
14741 	struct i40e_vsi *vsi, *vsi_it;
14742 	struct i40e_pf *pf;
14743 	int i, n = 0;
14744 
14745 	pf = veb->pf;
14746 
14747 	/* find the remaining VSI and check for extras */
14748 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14749 		if (vsi_it->uplink_seid == veb->seid) {
14750 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14751 				vsi = vsi_it;
14752 			n++;
14753 		}
14754 
14755 	/* Floating VEB has to be empty and regular one must have
14756 	 * single owner VSI.
14757 	 */
14758 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14759 		dev_info(&pf->pdev->dev,
14760 			 "can't remove VEB %d with %d VSIs left\n",
14761 			 veb->seid, n);
14762 		return;
14763 	}
14764 
14765 	/* For regular VEB move the owner VSI to uplink port */
14766 	if (veb->uplink_seid) {
14767 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14768 		vsi->uplink_seid = veb->uplink_seid;
14769 		vsi->veb_idx = I40E_NO_VEB;
14770 	}
14771 
14772 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14773 	i40e_veb_clear(veb);
14774 }
14775 
14776 /**
14777  * i40e_add_veb - create the VEB in the switch
14778  * @veb: the VEB to be instantiated
14779  * @vsi: the controlling VSI
14780  **/
14781 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14782 {
14783 	struct i40e_pf *pf = veb->pf;
14784 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14785 	int ret;
14786 
14787 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14788 			      veb->enabled_tc, vsi ? false : true,
14789 			      &veb->seid, enable_stats, NULL);
14790 
14791 	/* get a VEB from the hardware */
14792 	if (ret) {
14793 		dev_info(&pf->pdev->dev,
14794 			 "couldn't add VEB, err %pe aq_err %s\n",
14795 			 ERR_PTR(ret),
14796 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14797 		return -EPERM;
14798 	}
14799 
14800 	/* get statistics counter */
14801 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14802 					 &veb->stats_idx, NULL, NULL, NULL);
14803 	if (ret) {
14804 		dev_info(&pf->pdev->dev,
14805 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14806 			 ERR_PTR(ret),
14807 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14808 		return -EPERM;
14809 	}
14810 	ret = i40e_veb_get_bw_info(veb);
14811 	if (ret) {
14812 		dev_info(&pf->pdev->dev,
14813 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14814 			 ERR_PTR(ret),
14815 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14816 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14817 		return -ENOENT;
14818 	}
14819 
14820 	if (vsi) {
14821 		vsi->uplink_seid = veb->seid;
14822 		vsi->veb_idx = veb->idx;
14823 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14824 	}
14825 
14826 	return 0;
14827 }
14828 
14829 /**
14830  * i40e_veb_setup - Set up a VEB
14831  * @pf: board private structure
14832  * @uplink_seid: the switch element to link to
14833  * @vsi_seid: the initial VSI seid
14834  * @enabled_tc: Enabled TC bit-map
14835  *
14836  * This allocates the sw VEB structure and links it into the switch
14837  * It is possible and legal for this to be a duplicate of an already
14838  * existing VEB.  It is also possible for both uplink and vsi seids
14839  * to be zero, in order to create a floating VEB.
14840  *
14841  * Returns pointer to the successfully allocated VEB sw struct on
14842  * success, otherwise returns NULL on failure.
14843  **/
14844 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
14845 				u16 vsi_seid, u8 enabled_tc)
14846 {
14847 	struct i40e_vsi *vsi = NULL;
14848 	struct i40e_veb *veb;
14849 	int veb_idx;
14850 	int ret;
14851 
14852 	/* if one seid is 0, the other must be 0 to create a floating relay */
14853 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14854 	    (uplink_seid + vsi_seid != 0)) {
14855 		dev_info(&pf->pdev->dev,
14856 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14857 			 uplink_seid, vsi_seid);
14858 		return NULL;
14859 	}
14860 
14861 	/* make sure there is such a vsi and uplink */
14862 	if (vsi_seid) {
14863 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14864 		if (!vsi) {
14865 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14866 				vsi_seid);
14867 			return NULL;
14868 		}
14869 	}
14870 
14871 	/* get veb sw struct */
14872 	veb_idx = i40e_veb_mem_alloc(pf);
14873 	if (veb_idx < 0)
14874 		goto err_alloc;
14875 	veb = pf->veb[veb_idx];
14876 	veb->uplink_seid = uplink_seid;
14877 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14878 
14879 	/* create the VEB in the switch */
14880 	ret = i40e_add_veb(veb, vsi);
14881 	if (ret)
14882 		goto err_veb;
14883 
14884 	if (vsi && vsi->idx == pf->lan_vsi)
14885 		pf->lan_veb = veb->idx;
14886 
14887 	return veb;
14888 
14889 err_veb:
14890 	i40e_veb_clear(veb);
14891 err_alloc:
14892 	return NULL;
14893 }
14894 
14895 /**
14896  * i40e_setup_pf_switch_element - set PF vars based on switch type
14897  * @pf: board private structure
14898  * @ele: element we are building info from
14899  * @num_reported: total number of elements
14900  * @printconfig: should we print the contents
14901  *
14902  * helper function to assist in extracting a few useful SEID values.
14903  **/
14904 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14905 				struct i40e_aqc_switch_config_element_resp *ele,
14906 				u16 num_reported, bool printconfig)
14907 {
14908 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14909 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14910 	u8 element_type = ele->element_type;
14911 	u16 seid = le16_to_cpu(ele->seid);
14912 	struct i40e_veb *veb;
14913 
14914 	if (printconfig)
14915 		dev_info(&pf->pdev->dev,
14916 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14917 			 element_type, seid, uplink_seid, downlink_seid);
14918 
14919 	switch (element_type) {
14920 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14921 		pf->mac_seid = seid;
14922 		break;
14923 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14924 		/* Main VEB? */
14925 		if (uplink_seid != pf->mac_seid)
14926 			break;
14927 		veb = i40e_pf_get_main_veb(pf);
14928 		if (!veb) {
14929 			int v;
14930 
14931 			/* find existing or else empty VEB */
14932 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14933 			if (veb) {
14934 				pf->lan_veb = veb->idx;
14935 			} else {
14936 				v = i40e_veb_mem_alloc(pf);
14937 				if (v < 0)
14938 					break;
14939 				pf->lan_veb = v;
14940 			}
14941 		}
14942 
14943 		/* Try to get again main VEB as pf->lan_veb may have changed */
14944 		veb = i40e_pf_get_main_veb(pf);
14945 		if (!veb)
14946 			break;
14947 
14948 		veb->seid = seid;
14949 		veb->uplink_seid = pf->mac_seid;
14950 		veb->pf = pf;
14951 		break;
14952 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14953 		if (num_reported != 1)
14954 			break;
14955 		/* This is immediately after a reset so we can assume this is
14956 		 * the PF's VSI
14957 		 */
14958 		pf->mac_seid = uplink_seid;
14959 		pf->main_vsi_seid = seid;
14960 		if (printconfig)
14961 			dev_info(&pf->pdev->dev,
14962 				 "pf_seid=%d main_vsi_seid=%d\n",
14963 				 downlink_seid, pf->main_vsi_seid);
14964 		break;
14965 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14966 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14967 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14968 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14969 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14970 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14971 		/* ignore these for now */
14972 		break;
14973 	default:
14974 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14975 			 element_type, seid);
14976 		break;
14977 	}
14978 }
14979 
14980 /**
14981  * i40e_fetch_switch_configuration - Get switch config from firmware
14982  * @pf: board private structure
14983  * @printconfig: should we print the contents
14984  *
14985  * Get the current switch configuration from the device and
14986  * extract a few useful SEID values.
14987  **/
14988 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14989 {
14990 	struct i40e_aqc_get_switch_config_resp *sw_config;
14991 	u16 next_seid = 0;
14992 	int ret = 0;
14993 	u8 *aq_buf;
14994 	int i;
14995 
14996 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14997 	if (!aq_buf)
14998 		return -ENOMEM;
14999 
15000 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15001 	do {
15002 		u16 num_reported, num_total;
15003 
15004 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15005 						I40E_AQ_LARGE_BUF,
15006 						&next_seid, NULL);
15007 		if (ret) {
15008 			dev_info(&pf->pdev->dev,
15009 				 "get switch config failed err %d aq_err %s\n",
15010 				 ret,
15011 				 i40e_aq_str(&pf->hw,
15012 					     pf->hw.aq.asq_last_status));
15013 			kfree(aq_buf);
15014 			return -ENOENT;
15015 		}
15016 
15017 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15018 		num_total = le16_to_cpu(sw_config->header.num_total);
15019 
15020 		if (printconfig)
15021 			dev_info(&pf->pdev->dev,
15022 				 "header: %d reported %d total\n",
15023 				 num_reported, num_total);
15024 
15025 		for (i = 0; i < num_reported; i++) {
15026 			struct i40e_aqc_switch_config_element_resp *ele =
15027 				&sw_config->element[i];
15028 
15029 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15030 						     printconfig);
15031 		}
15032 	} while (next_seid != 0);
15033 
15034 	kfree(aq_buf);
15035 	return ret;
15036 }
15037 
15038 /**
15039  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15040  * @pf: board private structure
15041  * @reinit: if the Main VSI needs to re-initialized.
15042  * @lock_acquired: indicates whether or not the lock has been acquired
15043  *
15044  * Returns 0 on success, negative value on failure
15045  **/
15046 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15047 {
15048 	struct i40e_vsi *main_vsi;
15049 	u16 flags = 0;
15050 	int ret;
15051 
15052 	/* find out what's out there already */
15053 	ret = i40e_fetch_switch_configuration(pf, false);
15054 	if (ret) {
15055 		dev_info(&pf->pdev->dev,
15056 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15057 			 ERR_PTR(ret),
15058 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15059 		return ret;
15060 	}
15061 	i40e_pf_reset_stats(pf);
15062 
15063 	/* set the switch config bit for the whole device to
15064 	 * support limited promisc or true promisc
15065 	 * when user requests promisc. The default is limited
15066 	 * promisc.
15067 	*/
15068 
15069 	if ((pf->hw.pf_id == 0) &&
15070 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
15071 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15072 		pf->last_sw_conf_flags = flags;
15073 	}
15074 
15075 	if (pf->hw.pf_id == 0) {
15076 		u16 valid_flags;
15077 
15078 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15079 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15080 						NULL);
15081 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15082 			dev_info(&pf->pdev->dev,
15083 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15084 				 ERR_PTR(ret),
15085 				 i40e_aq_str(&pf->hw,
15086 					     pf->hw.aq.asq_last_status));
15087 			/* not a fatal problem, just keep going */
15088 		}
15089 		pf->last_sw_conf_valid_flags = valid_flags;
15090 	}
15091 
15092 	/* first time setup */
15093 	main_vsi = i40e_pf_get_main_vsi(pf);
15094 	if (!main_vsi || reinit) {
15095 		struct i40e_veb *veb;
15096 		u16 uplink_seid;
15097 
15098 		/* Set up the PF VSI associated with the PF's main VSI
15099 		 * that is already in the HW switch
15100 		 */
15101 		veb = i40e_pf_get_main_veb(pf);
15102 		if (veb)
15103 			uplink_seid = veb->seid;
15104 		else
15105 			uplink_seid = pf->mac_seid;
15106 		if (!main_vsi)
15107 			main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
15108 						  uplink_seid, 0);
15109 		else if (reinit)
15110 			main_vsi = i40e_vsi_reinit_setup(main_vsi);
15111 		if (!main_vsi) {
15112 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15113 			i40e_cloud_filter_exit(pf);
15114 			i40e_fdir_teardown(pf);
15115 			return -EAGAIN;
15116 		}
15117 	} else {
15118 		/* force a reset of TC and queue layout configurations */
15119 		main_vsi->seid = pf->main_vsi_seid;
15120 		i40e_vsi_reconfig_tc(main_vsi);
15121 	}
15122 	i40e_vlan_stripping_disable(main_vsi);
15123 
15124 	i40e_fdir_sb_setup(pf);
15125 
15126 	/* Setup static PF queue filter control settings */
15127 	ret = i40e_setup_pf_filter_control(pf);
15128 	if (ret) {
15129 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15130 			 ret);
15131 		/* Failure here should not stop continuing other steps */
15132 	}
15133 
15134 	/* enable RSS in the HW, even for only one queue, as the stack can use
15135 	 * the hash
15136 	 */
15137 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15138 		i40e_pf_config_rss(pf);
15139 
15140 	/* fill in link information and enable LSE reporting */
15141 	i40e_link_event(pf);
15142 
15143 	i40e_ptp_init(pf);
15144 
15145 	if (!lock_acquired)
15146 		rtnl_lock();
15147 
15148 	/* repopulate tunnel port filters */
15149 	udp_tunnel_nic_reset_ntf(main_vsi->netdev);
15150 
15151 	if (!lock_acquired)
15152 		rtnl_unlock();
15153 
15154 	return ret;
15155 }
15156 
15157 /**
15158  * i40e_determine_queue_usage - Work out queue distribution
15159  * @pf: board private structure
15160  **/
15161 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15162 {
15163 	int queues_left;
15164 	int q_max;
15165 
15166 	pf->num_lan_qps = 0;
15167 
15168 	/* Find the max queues to be put into basic use.  We'll always be
15169 	 * using TC0, whether or not DCB is running, and TC0 will get the
15170 	 * big RSS set.
15171 	 */
15172 	queues_left = pf->hw.func_caps.num_tx_qp;
15173 
15174 	if ((queues_left == 1) ||
15175 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15176 		/* one qp for PF, no queues for anything else */
15177 		queues_left = 0;
15178 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15179 
15180 		/* make sure all the fancies are disabled */
15181 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15182 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15183 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15184 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15185 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15186 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15187 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15188 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15189 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15190 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15191 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15192 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15193 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15194 		/* one qp for PF */
15195 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15196 		queues_left -= pf->num_lan_qps;
15197 
15198 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15199 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15200 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15201 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15202 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15203 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15204 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15205 	} else {
15206 		/* Not enough queues for all TCs */
15207 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15208 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15209 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15210 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15211 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15212 		}
15213 
15214 		/* limit lan qps to the smaller of qps, cpus or msix */
15215 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15216 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15217 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15218 		pf->num_lan_qps = q_max;
15219 
15220 		queues_left -= pf->num_lan_qps;
15221 	}
15222 
15223 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15224 		if (queues_left > 1) {
15225 			queues_left -= 1; /* save 1 queue for FD */
15226 		} else {
15227 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15228 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15229 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15230 		}
15231 	}
15232 
15233 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15234 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15235 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15236 					(queues_left / pf->num_vf_qps));
15237 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15238 	}
15239 
15240 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15241 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15242 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15243 					  (queues_left / pf->num_vmdq_qps));
15244 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15245 	}
15246 
15247 	pf->queues_left = queues_left;
15248 	dev_dbg(&pf->pdev->dev,
15249 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15250 		pf->hw.func_caps.num_tx_qp,
15251 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15252 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15253 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15254 		queues_left);
15255 }
15256 
15257 /**
15258  * i40e_setup_pf_filter_control - Setup PF static filter control
15259  * @pf: PF to be setup
15260  *
15261  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15262  * settings. If PE/FCoE are enabled then it will also set the per PF
15263  * based filter sizes required for them. It also enables Flow director,
15264  * ethertype and macvlan type filter settings for the pf.
15265  *
15266  * Returns 0 on success, negative on failure
15267  **/
15268 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15269 {
15270 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15271 
15272 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15273 
15274 	/* Flow Director is enabled */
15275 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15276 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15277 		settings->enable_fdir = true;
15278 
15279 	/* Ethtype and MACVLAN filters enabled for PF */
15280 	settings->enable_ethtype = true;
15281 	settings->enable_macvlan = true;
15282 
15283 	if (i40e_set_filter_control(&pf->hw, settings))
15284 		return -ENOENT;
15285 
15286 	return 0;
15287 }
15288 
15289 #define INFO_STRING_LEN 255
15290 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15291 static void i40e_print_features(struct i40e_pf *pf)
15292 {
15293 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
15294 	struct i40e_hw *hw = &pf->hw;
15295 	char *buf;
15296 	int i;
15297 
15298 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15299 	if (!buf)
15300 		return;
15301 
15302 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15303 #ifdef CONFIG_PCI_IOV
15304 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15305 #endif
15306 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15307 		       pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
15308 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15309 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15310 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15311 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15312 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15313 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15314 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15315 	}
15316 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15317 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15318 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15319 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15320 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15321 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15322 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15323 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15324 	else
15325 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15326 
15327 	dev_info(&pf->pdev->dev, "%s\n", buf);
15328 	kfree(buf);
15329 	WARN_ON(i > INFO_STRING_LEN);
15330 }
15331 
15332 /**
15333  * i40e_get_platform_mac_addr - get platform-specific MAC address
15334  * @pdev: PCI device information struct
15335  * @pf: board private structure
15336  *
15337  * Look up the MAC address for the device. First we'll try
15338  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15339  * specific fallback. Otherwise, we'll default to the stored value in
15340  * firmware.
15341  **/
15342 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15343 {
15344 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15345 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15346 }
15347 
15348 /**
15349  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15350  * @fec_cfg: FEC option to set in flags
15351  * @flags: ptr to flags in which we set FEC option
15352  **/
15353 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15354 {
15355 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15356 		set_bit(I40E_FLAG_RS_FEC, flags);
15357 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15358 	}
15359 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15360 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15361 		set_bit(I40E_FLAG_RS_FEC, flags);
15362 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15363 	}
15364 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15365 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15366 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15367 		clear_bit(I40E_FLAG_RS_FEC, flags);
15368 	}
15369 	if (fec_cfg == 0) {
15370 		clear_bit(I40E_FLAG_RS_FEC, flags);
15371 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15372 	}
15373 }
15374 
15375 /**
15376  * i40e_check_recovery_mode - check if we are running transition firmware
15377  * @pf: board private structure
15378  *
15379  * Check registers indicating the firmware runs in recovery mode. Sets the
15380  * appropriate driver state.
15381  *
15382  * Returns true if the recovery mode was detected, false otherwise
15383  **/
15384 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15385 {
15386 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15387 
15388 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15389 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15390 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15391 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15392 
15393 		return true;
15394 	}
15395 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15396 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15397 
15398 	return false;
15399 }
15400 
15401 /**
15402  * i40e_pf_loop_reset - perform reset in a loop.
15403  * @pf: board private structure
15404  *
15405  * This function is useful when a NIC is about to enter recovery mode.
15406  * When a NIC's internal data structures are corrupted the NIC's
15407  * firmware is going to enter recovery mode.
15408  * Right after a POR it takes about 7 minutes for firmware to enter
15409  * recovery mode. Until that time a NIC is in some kind of intermediate
15410  * state. After that time period the NIC almost surely enters
15411  * recovery mode. The only way for a driver to detect intermediate
15412  * state is to issue a series of pf-resets and check a return value.
15413  * If a PF reset returns success then the firmware could be in recovery
15414  * mode so the caller of this code needs to check for recovery mode
15415  * if this function returns success. There is a little chance that
15416  * firmware will hang in intermediate state forever.
15417  * Since waiting 7 minutes is quite a lot of time this function waits
15418  * 10 seconds and then gives up by returning an error.
15419  *
15420  * Return 0 on success, negative on failure.
15421  **/
15422 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15423 {
15424 	/* wait max 10 seconds for PF reset to succeed */
15425 	const unsigned long time_end = jiffies + 10 * HZ;
15426 	struct i40e_hw *hw = &pf->hw;
15427 	int ret;
15428 
15429 	ret = i40e_pf_reset(hw);
15430 	while (ret != 0 && time_before(jiffies, time_end)) {
15431 		usleep_range(10000, 20000);
15432 		ret = i40e_pf_reset(hw);
15433 	}
15434 
15435 	if (ret == 0)
15436 		pf->pfr_count++;
15437 	else
15438 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15439 
15440 	return ret;
15441 }
15442 
15443 /**
15444  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15445  * @pf: board private structure
15446  *
15447  * Check FW registers to determine if FW issued unexpected EMP Reset.
15448  * Every time when unexpected EMP Reset occurs the FW increments
15449  * a counter of unexpected EMP Resets. When the counter reaches 10
15450  * the FW should enter the Recovery mode
15451  *
15452  * Returns true if FW issued unexpected EMP Reset
15453  **/
15454 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15455 {
15456 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15457 			   I40E_GL_FWSTS_FWS1B_MASK;
15458 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15459 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15460 }
15461 
15462 /**
15463  * i40e_handle_resets - handle EMP resets and PF resets
15464  * @pf: board private structure
15465  *
15466  * Handle both EMP resets and PF resets and conclude whether there are
15467  * any issues regarding these resets. If there are any issues then
15468  * generate log entry.
15469  *
15470  * Return 0 if NIC is healthy or negative value when there are issues
15471  * with resets
15472  **/
15473 static int i40e_handle_resets(struct i40e_pf *pf)
15474 {
15475 	const int pfr = i40e_pf_loop_reset(pf);
15476 	const bool is_empr = i40e_check_fw_empr(pf);
15477 
15478 	if (is_empr || pfr != 0)
15479 		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");
15480 
15481 	return is_empr ? -EIO : pfr;
15482 }
15483 
15484 /**
15485  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15486  * @pf: board private structure
15487  * @hw: ptr to the hardware info
15488  *
15489  * This function does a minimal setup of all subsystems needed for running
15490  * recovery mode.
15491  *
15492  * Returns 0 on success, negative on failure
15493  **/
15494 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15495 {
15496 	struct i40e_vsi *vsi;
15497 	int err;
15498 	int v_idx;
15499 
15500 	pci_set_drvdata(pf->pdev, pf);
15501 	pci_save_state(pf->pdev);
15502 
15503 	/* set up periodic task facility */
15504 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15505 	pf->service_timer_period = HZ;
15506 
15507 	INIT_WORK(&pf->service_task, i40e_service_task);
15508 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15509 
15510 	err = i40e_init_interrupt_scheme(pf);
15511 	if (err)
15512 		goto err_switch_setup;
15513 
15514 	/* The number of VSIs reported by the FW is the minimum guaranteed
15515 	 * to us; HW supports far more and we share the remaining pool with
15516 	 * the other PFs. We allocate space for more than the guarantee with
15517 	 * the understanding that we might not get them all later.
15518 	 */
15519 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15520 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15521 	else
15522 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15523 
15524 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15525 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15526 			  GFP_KERNEL);
15527 	if (!pf->vsi) {
15528 		err = -ENOMEM;
15529 		goto err_switch_setup;
15530 	}
15531 
15532 	/* We allocate one VSI which is needed as absolute minimum
15533 	 * in order to register the netdev
15534 	 */
15535 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15536 	if (v_idx < 0) {
15537 		err = v_idx;
15538 		goto err_switch_setup;
15539 	}
15540 	pf->lan_vsi = v_idx;
15541 	vsi = pf->vsi[v_idx];
15542 	if (!vsi) {
15543 		err = -EFAULT;
15544 		goto err_switch_setup;
15545 	}
15546 	vsi->alloc_queue_pairs = 1;
15547 	err = i40e_config_netdev(vsi);
15548 	if (err)
15549 		goto err_switch_setup;
15550 	err = register_netdev(vsi->netdev);
15551 	if (err)
15552 		goto err_switch_setup;
15553 	vsi->netdev_registered = true;
15554 	i40e_dbg_pf_init(pf);
15555 
15556 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15557 	if (err)
15558 		goto err_switch_setup;
15559 
15560 	/* tell the firmware that we're starting */
15561 	i40e_send_version(pf);
15562 
15563 	/* since everything's happy, start the service_task timer */
15564 	mod_timer(&pf->service_timer,
15565 		  round_jiffies(jiffies + pf->service_timer_period));
15566 
15567 	return 0;
15568 
15569 err_switch_setup:
15570 	i40e_reset_interrupt_capability(pf);
15571 	timer_shutdown_sync(&pf->service_timer);
15572 	i40e_shutdown_adminq(hw);
15573 	iounmap(hw->hw_addr);
15574 	pci_release_mem_regions(pf->pdev);
15575 	pci_disable_device(pf->pdev);
15576 	i40e_free_pf(pf);
15577 
15578 	return err;
15579 }
15580 
15581 /**
15582  * i40e_set_subsystem_device_id - set subsystem device id
15583  * @hw: pointer to the hardware info
15584  *
15585  * Set PCI subsystem device id either from a pci_dev structure or
15586  * a specific FW register.
15587  **/
15588 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15589 {
15590 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15591 
15592 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15593 		pf->pdev->subsystem_device :
15594 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15595 }
15596 
15597 /**
15598  * i40e_probe - Device initialization routine
15599  * @pdev: PCI device information struct
15600  * @ent: entry in i40e_pci_tbl
15601  *
15602  * i40e_probe initializes a PF identified by a pci_dev structure.
15603  * The OS initialization, configuring of the PF private structure,
15604  * and a hardware reset occur.
15605  *
15606  * Returns 0 on success, negative on failure
15607  **/
15608 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15609 {
15610 	struct i40e_aq_get_phy_abilities_resp abilities;
15611 #ifdef CONFIG_I40E_DCB
15612 	enum i40e_get_fw_lldp_status_resp lldp_status;
15613 #endif /* CONFIG_I40E_DCB */
15614 	struct i40e_vsi *vsi;
15615 	struct i40e_pf *pf;
15616 	struct i40e_hw *hw;
15617 	u16 wol_nvm_bits;
15618 	char nvm_ver[32];
15619 	u16 link_status;
15620 #ifdef CONFIG_I40E_DCB
15621 	int status;
15622 #endif /* CONFIG_I40E_DCB */
15623 	int err;
15624 	u32 val;
15625 
15626 	err = pci_enable_device_mem(pdev);
15627 	if (err)
15628 		return err;
15629 
15630 	/* set up for high or low dma */
15631 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15632 	if (err) {
15633 		dev_err(&pdev->dev,
15634 			"DMA configuration failed: 0x%x\n", err);
15635 		goto err_dma;
15636 	}
15637 
15638 	/* set up pci connections */
15639 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15640 	if (err) {
15641 		dev_info(&pdev->dev,
15642 			 "pci_request_selected_regions failed %d\n", err);
15643 		goto err_pci_reg;
15644 	}
15645 
15646 	pci_set_master(pdev);
15647 
15648 	/* Now that we have a PCI connection, we need to do the
15649 	 * low level device setup.  This is primarily setting up
15650 	 * the Admin Queue structures and then querying for the
15651 	 * device's current profile information.
15652 	 */
15653 	pf = i40e_alloc_pf(&pdev->dev);
15654 	if (!pf) {
15655 		err = -ENOMEM;
15656 		goto err_pf_alloc;
15657 	}
15658 	pf->next_vsi = 0;
15659 	pf->pdev = pdev;
15660 	set_bit(__I40E_DOWN, pf->state);
15661 
15662 	hw = &pf->hw;
15663 
15664 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15665 				I40E_MAX_CSR_SPACE);
15666 	/* We believe that the highest register to read is
15667 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15668 	 * is not less than that before mapping to prevent a
15669 	 * kernel panic.
15670 	 */
15671 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15672 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15673 			pf->ioremap_len);
15674 		err = -ENOMEM;
15675 		goto err_ioremap;
15676 	}
15677 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15678 	if (!hw->hw_addr) {
15679 		err = -EIO;
15680 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15681 			 (unsigned int)pci_resource_start(pdev, 0),
15682 			 pf->ioremap_len, err);
15683 		goto err_ioremap;
15684 	}
15685 	hw->vendor_id = pdev->vendor;
15686 	hw->device_id = pdev->device;
15687 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15688 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15689 	i40e_set_subsystem_device_id(hw);
15690 	hw->bus.device = PCI_SLOT(pdev->devfn);
15691 	hw->bus.func = PCI_FUNC(pdev->devfn);
15692 	hw->bus.bus_id = pdev->bus->number;
15693 
15694 	/* Select something other than the 802.1ad ethertype for the
15695 	 * switch to use internally and drop on ingress.
15696 	 */
15697 	hw->switch_tag = 0xffff;
15698 	hw->first_tag = ETH_P_8021AD;
15699 	hw->second_tag = ETH_P_8021Q;
15700 
15701 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15702 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15703 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15704 
15705 	/* set up the locks for the AQ, do this only once in probe
15706 	 * and destroy them only once in remove
15707 	 */
15708 	mutex_init(&hw->aq.asq_mutex);
15709 	mutex_init(&hw->aq.arq_mutex);
15710 
15711 	pf->msg_enable = netif_msg_init(debug,
15712 					NETIF_MSG_DRV |
15713 					NETIF_MSG_PROBE |
15714 					NETIF_MSG_LINK);
15715 	if (debug < -1)
15716 		pf->hw.debug_mask = debug;
15717 
15718 	/* do a special CORER for clearing PXE mode once at init */
15719 	if (hw->revision_id == 0 &&
15720 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15721 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15722 		i40e_flush(hw);
15723 		msleep(200);
15724 		pf->corer_count++;
15725 
15726 		i40e_clear_pxe_mode(hw);
15727 	}
15728 
15729 	/* Reset here to make sure all is clean and to define PF 'n' */
15730 	i40e_clear_hw(hw);
15731 
15732 	err = i40e_set_mac_type(hw);
15733 	if (err) {
15734 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15735 			 err);
15736 		goto err_pf_reset;
15737 	}
15738 
15739 	err = i40e_handle_resets(pf);
15740 	if (err)
15741 		goto err_pf_reset;
15742 
15743 	i40e_check_recovery_mode(pf);
15744 
15745 	if (is_kdump_kernel()) {
15746 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15747 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15748 	} else {
15749 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15750 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15751 	}
15752 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15753 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15754 
15755 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15756 		 "%s-%s:misc",
15757 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15758 
15759 	err = i40e_init_shared_code(hw);
15760 	if (err) {
15761 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15762 			 err);
15763 		goto err_pf_reset;
15764 	}
15765 
15766 	/* set up a default setting for link flow control */
15767 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15768 
15769 	err = i40e_init_adminq(hw);
15770 	if (err) {
15771 		if (err == -EIO)
15772 			dev_info(&pdev->dev,
15773 				 "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",
15774 				 hw->aq.api_maj_ver,
15775 				 hw->aq.api_min_ver,
15776 				 I40E_FW_API_VERSION_MAJOR,
15777 				 I40E_FW_MINOR_VERSION(hw));
15778 		else
15779 			dev_info(&pdev->dev,
15780 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15781 
15782 		goto err_pf_reset;
15783 	}
15784 	i40e_get_oem_version(hw);
15785 	i40e_get_pba_string(hw);
15786 
15787 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15788 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15789 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15790 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15791 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15792 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15793 		 hw->subsystem_device_id);
15794 
15795 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15796 				  I40E_FW_MINOR_VERSION(hw) + 1))
15797 		dev_dbg(&pdev->dev,
15798 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15799 			 hw->aq.api_maj_ver,
15800 			 hw->aq.api_min_ver,
15801 			 I40E_FW_API_VERSION_MAJOR,
15802 			 I40E_FW_MINOR_VERSION(hw));
15803 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15804 		dev_info(&pdev->dev,
15805 			 "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",
15806 			 hw->aq.api_maj_ver,
15807 			 hw->aq.api_min_ver,
15808 			 I40E_FW_API_VERSION_MAJOR,
15809 			 I40E_FW_MINOR_VERSION(hw));
15810 
15811 	i40e_verify_eeprom(pf);
15812 
15813 	/* Rev 0 hardware was never productized */
15814 	if (hw->revision_id < 1)
15815 		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");
15816 
15817 	i40e_clear_pxe_mode(hw);
15818 
15819 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15820 	if (err)
15821 		goto err_adminq_setup;
15822 
15823 	err = i40e_sw_init(pf);
15824 	if (err) {
15825 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15826 		goto err_sw_init;
15827 	}
15828 
15829 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15830 		return i40e_init_recovery_mode(pf, hw);
15831 
15832 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15833 				hw->func_caps.num_rx_qp, 0, 0);
15834 	if (err) {
15835 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15836 		goto err_init_lan_hmc;
15837 	}
15838 
15839 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15840 	if (err) {
15841 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15842 		err = -ENOENT;
15843 		goto err_configure_lan_hmc;
15844 	}
15845 
15846 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15847 	 * Ignore error return codes because if it was already disabled via
15848 	 * hardware settings this will fail
15849 	 */
15850 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15851 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15852 		i40e_aq_stop_lldp(hw, true, false, NULL);
15853 	}
15854 
15855 	/* allow a platform config to override the HW addr */
15856 	i40e_get_platform_mac_addr(pdev, pf);
15857 
15858 	if (!is_valid_ether_addr(hw->mac.addr)) {
15859 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15860 		err = -EIO;
15861 		goto err_mac_addr;
15862 	}
15863 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15864 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15865 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15866 	if (is_valid_ether_addr(hw->mac.port_addr))
15867 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15868 
15869 	i40e_ptp_alloc_pins(pf);
15870 	pci_set_drvdata(pdev, pf);
15871 	pci_save_state(pdev);
15872 
15873 #ifdef CONFIG_I40E_DCB
15874 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15875 	(!status &&
15876 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15877 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15878 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15879 	dev_info(&pdev->dev,
15880 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15881 			"FW LLDP is disabled\n" :
15882 			"FW LLDP is enabled\n");
15883 
15884 	/* Enable FW to write default DCB config on link-up */
15885 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15886 
15887 	err = i40e_init_pf_dcb(pf);
15888 	if (err) {
15889 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15890 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15891 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15892 		/* Continue without DCB enabled */
15893 	}
15894 #endif /* CONFIG_I40E_DCB */
15895 
15896 	/* set up periodic task facility */
15897 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15898 	pf->service_timer_period = HZ;
15899 
15900 	INIT_WORK(&pf->service_task, i40e_service_task);
15901 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15902 
15903 	/* NVM bit on means WoL disabled for the port */
15904 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15905 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15906 		pf->wol_en = false;
15907 	else
15908 		pf->wol_en = true;
15909 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15910 
15911 	/* set up the main switch operations */
15912 	i40e_determine_queue_usage(pf);
15913 	err = i40e_init_interrupt_scheme(pf);
15914 	if (err)
15915 		goto err_switch_setup;
15916 
15917 	/* Reduce Tx and Rx pairs for kdump
15918 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15919 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15920 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15921 	 */
15922 	if (is_kdump_kernel())
15923 		pf->num_lan_msix = 1;
15924 
15925 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15926 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15927 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15928 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15929 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15930 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15931 						    UDP_TUNNEL_TYPE_GENEVE;
15932 
15933 	/* The number of VSIs reported by the FW is the minimum guaranteed
15934 	 * to us; HW supports far more and we share the remaining pool with
15935 	 * the other PFs. We allocate space for more than the guarantee with
15936 	 * the understanding that we might not get them all later.
15937 	 */
15938 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15939 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15940 	else
15941 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15942 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15943 		dev_warn(&pf->pdev->dev,
15944 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15945 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15946 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15947 	}
15948 
15949 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15950 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15951 			  GFP_KERNEL);
15952 	if (!pf->vsi) {
15953 		err = -ENOMEM;
15954 		goto err_switch_setup;
15955 	}
15956 
15957 #ifdef CONFIG_PCI_IOV
15958 	/* prep for VF support */
15959 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15960 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15961 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15962 		if (pci_num_vf(pdev))
15963 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15964 	}
15965 #endif
15966 	err = i40e_setup_pf_switch(pf, false, false);
15967 	if (err) {
15968 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15969 		goto err_vsis;
15970 	}
15971 
15972 	vsi = i40e_pf_get_main_vsi(pf);
15973 	INIT_LIST_HEAD(&vsi->ch_list);
15974 
15975 	/* if FDIR VSI was set up, start it now */
15976 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
15977 	if (vsi)
15978 		i40e_vsi_open(vsi);
15979 
15980 	/* The driver only wants link up/down and module qualification
15981 	 * reports from firmware.  Note the negative logic.
15982 	 */
15983 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15984 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15985 					 I40E_AQ_EVENT_MEDIA_NA |
15986 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15987 	if (err)
15988 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15989 			 ERR_PTR(err),
15990 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15991 
15992 	/* Reconfigure hardware for allowing smaller MSS in the case
15993 	 * of TSO, so that we avoid the MDD being fired and causing
15994 	 * a reset in the case of small MSS+TSO.
15995 	 */
15996 	val = rd32(hw, I40E_REG_MSS);
15997 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15998 		val &= ~I40E_REG_MSS_MIN_MASK;
15999 		val |= I40E_64BYTE_MSS;
16000 		wr32(hw, I40E_REG_MSS, val);
16001 	}
16002 
16003 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
16004 		msleep(75);
16005 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16006 		if (err)
16007 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16008 				 ERR_PTR(err),
16009 				 i40e_aq_str(&pf->hw,
16010 					     pf->hw.aq.asq_last_status));
16011 	}
16012 	/* The main driver is (mostly) up and happy. We need to set this state
16013 	 * before setting up the misc vector or we get a race and the vector
16014 	 * ends up disabled forever.
16015 	 */
16016 	clear_bit(__I40E_DOWN, pf->state);
16017 
16018 	/* In case of MSIX we are going to setup the misc vector right here
16019 	 * to handle admin queue events etc. In case of legacy and MSI
16020 	 * the misc functionality and queue processing is combined in
16021 	 * the same vector and that gets setup at open.
16022 	 */
16023 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
16024 		err = i40e_setup_misc_vector(pf);
16025 		if (err) {
16026 			dev_info(&pdev->dev,
16027 				 "setup of misc vector failed: %d\n", err);
16028 			i40e_cloud_filter_exit(pf);
16029 			i40e_fdir_teardown(pf);
16030 			goto err_vsis;
16031 		}
16032 	}
16033 
16034 #ifdef CONFIG_PCI_IOV
16035 	/* prep for VF support */
16036 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
16037 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
16038 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16039 		/* disable link interrupts for VFs */
16040 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16041 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16042 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16043 		i40e_flush(hw);
16044 
16045 		if (pci_num_vf(pdev)) {
16046 			dev_info(&pdev->dev,
16047 				 "Active VFs found, allocating resources.\n");
16048 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16049 			if (err)
16050 				dev_info(&pdev->dev,
16051 					 "Error %d allocating resources for existing VFs\n",
16052 					 err);
16053 		}
16054 	}
16055 #endif /* CONFIG_PCI_IOV */
16056 
16057 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16058 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16059 						      pf->num_iwarp_msix,
16060 						      I40E_IWARP_IRQ_PILE_ID);
16061 		if (pf->iwarp_base_vector < 0) {
16062 			dev_info(&pdev->dev,
16063 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16064 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16065 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
16066 		}
16067 	}
16068 
16069 	i40e_dbg_pf_init(pf);
16070 
16071 	/* tell the firmware that we're starting */
16072 	i40e_send_version(pf);
16073 
16074 	/* since everything's happy, start the service_task timer */
16075 	mod_timer(&pf->service_timer,
16076 		  round_jiffies(jiffies + pf->service_timer_period));
16077 
16078 	/* add this PF to client device list and launch a client service task */
16079 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16080 		err = i40e_lan_add_device(pf);
16081 		if (err)
16082 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16083 				 err);
16084 	}
16085 
16086 #define PCI_SPEED_SIZE 8
16087 #define PCI_WIDTH_SIZE 8
16088 	/* Devices on the IOSF bus do not have this information
16089 	 * and will report PCI Gen 1 x 1 by default so don't bother
16090 	 * checking them.
16091 	 */
16092 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16093 		char speed[PCI_SPEED_SIZE] = "Unknown";
16094 		char width[PCI_WIDTH_SIZE] = "Unknown";
16095 
16096 		/* Get the negotiated link width and speed from PCI config
16097 		 * space
16098 		 */
16099 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16100 					  &link_status);
16101 
16102 		i40e_set_pci_config_data(hw, link_status);
16103 
16104 		switch (hw->bus.speed) {
16105 		case i40e_bus_speed_8000:
16106 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16107 		case i40e_bus_speed_5000:
16108 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16109 		case i40e_bus_speed_2500:
16110 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16111 		default:
16112 			break;
16113 		}
16114 		switch (hw->bus.width) {
16115 		case i40e_bus_width_pcie_x8:
16116 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16117 		case i40e_bus_width_pcie_x4:
16118 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16119 		case i40e_bus_width_pcie_x2:
16120 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16121 		case i40e_bus_width_pcie_x1:
16122 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16123 		default:
16124 			break;
16125 		}
16126 
16127 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16128 			 speed, width);
16129 
16130 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16131 		    hw->bus.speed < i40e_bus_speed_8000) {
16132 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16133 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16134 		}
16135 	}
16136 
16137 	/* get the requested speeds from the fw */
16138 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16139 	if (err)
16140 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16141 			ERR_PTR(err),
16142 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16143 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16144 
16145 	/* set the FEC config due to the board capabilities */
16146 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16147 
16148 	/* get the supported phy types from the fw */
16149 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16150 	if (err)
16151 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16152 			ERR_PTR(err),
16153 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16154 
16155 	/* make sure the MFS hasn't been set lower than the default */
16156 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16157 	val = FIELD_GET(I40E_PRTGL_SAH_MFS_MASK,
16158 			rd32(&pf->hw, I40E_PRTGL_SAH));
16159 	if (val < MAX_FRAME_SIZE_DEFAULT)
16160 		dev_warn(&pdev->dev, "MFS for port %x (%d) has been set below the default (%d)\n",
16161 			 pf->hw.port, val, MAX_FRAME_SIZE_DEFAULT);
16162 
16163 	/* Add a filter to drop all Flow control frames from any VSI from being
16164 	 * transmitted. By doing so we stop a malicious VF from sending out
16165 	 * PAUSE or PFC frames and potentially controlling traffic for other
16166 	 * PF/VF VSIs.
16167 	 * The FW can still send Flow control frames if enabled.
16168 	 */
16169 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16170 						       pf->main_vsi_seid);
16171 
16172 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16173 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16174 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16175 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16176 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16177 	/* print a string summarizing features */
16178 	i40e_print_features(pf);
16179 
16180 	i40e_devlink_register(pf);
16181 
16182 	return 0;
16183 
16184 	/* Unwind what we've done if something failed in the setup */
16185 err_vsis:
16186 	set_bit(__I40E_DOWN, pf->state);
16187 	i40e_clear_interrupt_scheme(pf);
16188 	kfree(pf->vsi);
16189 err_switch_setup:
16190 	i40e_reset_interrupt_capability(pf);
16191 	timer_shutdown_sync(&pf->service_timer);
16192 err_mac_addr:
16193 err_configure_lan_hmc:
16194 	(void)i40e_shutdown_lan_hmc(hw);
16195 err_init_lan_hmc:
16196 	kfree(pf->qp_pile);
16197 err_sw_init:
16198 err_adminq_setup:
16199 err_pf_reset:
16200 	iounmap(hw->hw_addr);
16201 err_ioremap:
16202 	i40e_free_pf(pf);
16203 err_pf_alloc:
16204 	pci_release_mem_regions(pdev);
16205 err_pci_reg:
16206 err_dma:
16207 	pci_disable_device(pdev);
16208 	return err;
16209 }
16210 
16211 /**
16212  * i40e_remove - Device removal routine
16213  * @pdev: PCI device information struct
16214  *
16215  * i40e_remove is called by the PCI subsystem to alert the driver
16216  * that is should release a PCI device.  This could be caused by a
16217  * Hot-Plug event, or because the driver is going to be removed from
16218  * memory.
16219  **/
16220 static void i40e_remove(struct pci_dev *pdev)
16221 {
16222 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16223 	struct i40e_hw *hw = &pf->hw;
16224 	struct i40e_vsi *vsi;
16225 	struct i40e_veb *veb;
16226 	int ret_code;
16227 	int i;
16228 
16229 	i40e_devlink_unregister(pf);
16230 
16231 	i40e_dbg_pf_exit(pf);
16232 
16233 	i40e_ptp_stop(pf);
16234 
16235 	/* Disable RSS in hw */
16236 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16237 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16238 
16239 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16240 	 * flags, once they are set, i40e_rebuild should not be called as
16241 	 * i40e_prep_for_reset always returns early.
16242 	 */
16243 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16244 		usleep_range(1000, 2000);
16245 	set_bit(__I40E_IN_REMOVE, pf->state);
16246 
16247 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16248 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16249 		i40e_free_vfs(pf);
16250 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16251 	}
16252 	/* no more scheduling of any task */
16253 	set_bit(__I40E_SUSPENDED, pf->state);
16254 	set_bit(__I40E_DOWN, pf->state);
16255 	if (pf->service_timer.function)
16256 		timer_shutdown_sync(&pf->service_timer);
16257 	if (pf->service_task.func)
16258 		cancel_work_sync(&pf->service_task);
16259 
16260 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16261 		struct i40e_vsi *vsi = pf->vsi[0];
16262 
16263 		/* We know that we have allocated only one vsi for this PF,
16264 		 * it was just for registering netdevice, so the interface
16265 		 * could be visible in the 'ifconfig' output
16266 		 */
16267 		unregister_netdev(vsi->netdev);
16268 		free_netdev(vsi->netdev);
16269 
16270 		goto unmap;
16271 	}
16272 
16273 	/* Client close must be called explicitly here because the timer
16274 	 * has been stopped.
16275 	 */
16276 	i40e_notify_client_of_netdev_close(pf, false);
16277 
16278 	i40e_fdir_teardown(pf);
16279 
16280 	/* If there is a switch structure or any orphans, remove them.
16281 	 * This will leave only the PF's VSI remaining.
16282 	 */
16283 	i40e_pf_for_each_veb(pf, i, veb)
16284 		if (veb->uplink_seid == pf->mac_seid ||
16285 		    veb->uplink_seid == 0)
16286 			i40e_switch_branch_release(veb);
16287 
16288 	/* Now we can shutdown the PF's VSIs, just before we kill
16289 	 * adminq and hmc.
16290 	 */
16291 	i40e_pf_for_each_vsi(pf, i, vsi) {
16292 		i40e_vsi_close(vsi);
16293 		i40e_vsi_release(vsi);
16294 		pf->vsi[i] = NULL;
16295 	}
16296 
16297 	i40e_cloud_filter_exit(pf);
16298 
16299 	/* remove attached clients */
16300 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16301 		ret_code = i40e_lan_del_device(pf);
16302 		if (ret_code)
16303 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16304 				 ret_code);
16305 	}
16306 
16307 	/* shutdown and destroy the HMC */
16308 	if (hw->hmc.hmc_obj) {
16309 		ret_code = i40e_shutdown_lan_hmc(hw);
16310 		if (ret_code)
16311 			dev_warn(&pdev->dev,
16312 				 "Failed to destroy the HMC resources: %d\n",
16313 				 ret_code);
16314 	}
16315 
16316 unmap:
16317 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16318 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16319 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16320 		free_irq(pf->pdev->irq, pf);
16321 
16322 	/* shutdown the adminq */
16323 	i40e_shutdown_adminq(hw);
16324 
16325 	/* destroy the locks only once, here */
16326 	mutex_destroy(&hw->aq.arq_mutex);
16327 	mutex_destroy(&hw->aq.asq_mutex);
16328 
16329 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16330 	rtnl_lock();
16331 	i40e_clear_interrupt_scheme(pf);
16332 	i40e_pf_for_each_vsi(pf, i, vsi) {
16333 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16334 			i40e_vsi_clear_rings(vsi);
16335 
16336 		i40e_vsi_clear(vsi);
16337 		pf->vsi[i] = NULL;
16338 	}
16339 	rtnl_unlock();
16340 
16341 	i40e_pf_for_each_veb(pf, i, veb) {
16342 		kfree(veb);
16343 		pf->veb[i] = NULL;
16344 	}
16345 
16346 	kfree(pf->qp_pile);
16347 	kfree(pf->vsi);
16348 
16349 	iounmap(hw->hw_addr);
16350 	i40e_free_pf(pf);
16351 	pci_release_mem_regions(pdev);
16352 
16353 	pci_disable_device(pdev);
16354 }
16355 
16356 /**
16357  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16358  * using the mac_address_write admin q function
16359  * @pf: pointer to i40e_pf struct
16360  **/
16361 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16362 {
16363 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
16364 	struct i40e_hw *hw = &pf->hw;
16365 	u8 mac_addr[6];
16366 	u16 flags = 0;
16367 	int ret;
16368 
16369 	/* Get current MAC address in case it's an LAA */
16370 	if (main_vsi && main_vsi->netdev) {
16371 		ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
16372 	} else {
16373 		dev_err(&pf->pdev->dev,
16374 			"Failed to retrieve MAC address; using default\n");
16375 		ether_addr_copy(mac_addr, hw->mac.addr);
16376 	}
16377 
16378 	/* The FW expects the mac address write cmd to first be called with
16379 	 * one of these flags before calling it again with the multicast
16380 	 * enable flags.
16381 	 */
16382 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16383 
16384 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16385 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16386 
16387 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16388 	if (ret) {
16389 		dev_err(&pf->pdev->dev,
16390 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16391 		return;
16392 	}
16393 
16394 	flags = I40E_AQC_MC_MAG_EN
16395 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16396 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16397 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16398 	if (ret)
16399 		dev_err(&pf->pdev->dev,
16400 			"Failed to enable Multicast Magic Packet wake up\n");
16401 }
16402 
16403 /**
16404  * i40e_io_suspend - suspend all IO operations
16405  * @pf: pointer to i40e_pf struct
16406  *
16407  **/
16408 static int i40e_io_suspend(struct i40e_pf *pf)
16409 {
16410 	struct i40e_hw *hw = &pf->hw;
16411 
16412 	set_bit(__I40E_DOWN, pf->state);
16413 
16414 	/* Ensure service task will not be running */
16415 	del_timer_sync(&pf->service_timer);
16416 	cancel_work_sync(&pf->service_task);
16417 
16418 	/* Client close must be called explicitly here because the timer
16419 	 * has been stopped.
16420 	 */
16421 	i40e_notify_client_of_netdev_close(pf, false);
16422 
16423 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16424 	    pf->wol_en)
16425 		i40e_enable_mc_magic_wake(pf);
16426 
16427 	/* Since we're going to destroy queues during the
16428 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16429 	 * whole section
16430 	 */
16431 	rtnl_lock();
16432 
16433 	i40e_prep_for_reset(pf);
16434 
16435 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16436 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16437 
16438 	/* Clear the interrupt scheme and release our IRQs so that the system
16439 	 * can safely hibernate even when there are a large number of CPUs.
16440 	 * Otherwise hibernation might fail when mapping all the vectors back
16441 	 * to CPU0.
16442 	 */
16443 	i40e_clear_interrupt_scheme(pf);
16444 
16445 	rtnl_unlock();
16446 
16447 	return 0;
16448 }
16449 
16450 /**
16451  * i40e_io_resume - resume IO operations
16452  * @pf: pointer to i40e_pf struct
16453  *
16454  **/
16455 static int i40e_io_resume(struct i40e_pf *pf)
16456 {
16457 	struct device *dev = &pf->pdev->dev;
16458 	int err;
16459 
16460 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16461 	 * since we're going to be restoring queues
16462 	 */
16463 	rtnl_lock();
16464 
16465 	/* We cleared the interrupt scheme when we suspended, so we need to
16466 	 * restore it now to resume device functionality.
16467 	 */
16468 	err = i40e_restore_interrupt_scheme(pf);
16469 	if (err) {
16470 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16471 			err);
16472 	}
16473 
16474 	clear_bit(__I40E_DOWN, pf->state);
16475 	i40e_reset_and_rebuild(pf, false, true);
16476 
16477 	rtnl_unlock();
16478 
16479 	/* Clear suspended state last after everything is recovered */
16480 	clear_bit(__I40E_SUSPENDED, pf->state);
16481 
16482 	/* Restart the service task */
16483 	mod_timer(&pf->service_timer,
16484 		  round_jiffies(jiffies + pf->service_timer_period));
16485 
16486 	return 0;
16487 }
16488 
16489 /**
16490  * i40e_pci_error_detected - warning that something funky happened in PCI land
16491  * @pdev: PCI device information struct
16492  * @error: the type of PCI error
16493  *
16494  * Called to warn that something happened and the error handling steps
16495  * are in progress.  Allows the driver to quiesce things, be ready for
16496  * remediation.
16497  **/
16498 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16499 						pci_channel_state_t error)
16500 {
16501 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16502 
16503 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16504 
16505 	if (!pf) {
16506 		dev_info(&pdev->dev,
16507 			 "Cannot recover - error happened during device probe\n");
16508 		return PCI_ERS_RESULT_DISCONNECT;
16509 	}
16510 
16511 	/* shutdown all operations */
16512 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16513 		i40e_io_suspend(pf);
16514 
16515 	/* Request a slot reset */
16516 	return PCI_ERS_RESULT_NEED_RESET;
16517 }
16518 
16519 /**
16520  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16521  * @pdev: PCI device information struct
16522  *
16523  * Called to find if the driver can work with the device now that
16524  * the pci slot has been reset.  If a basic connection seems good
16525  * (registers are readable and have sane content) then return a
16526  * happy little PCI_ERS_RESULT_xxx.
16527  **/
16528 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16529 {
16530 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16531 	pci_ers_result_t result;
16532 	u32 reg;
16533 
16534 	dev_dbg(&pdev->dev, "%s\n", __func__);
16535 	/* enable I/O and memory of the device  */
16536 	if (pci_enable_device(pdev)) {
16537 		dev_info(&pdev->dev,
16538 			 "Cannot re-enable PCI device after reset.\n");
16539 		result = PCI_ERS_RESULT_DISCONNECT;
16540 	} else {
16541 		pci_set_master(pdev);
16542 		pci_restore_state(pdev);
16543 		pci_save_state(pdev);
16544 		pci_wake_from_d3(pdev, false);
16545 
16546 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16547 		if (reg == 0)
16548 			result = PCI_ERS_RESULT_RECOVERED;
16549 		else
16550 			result = PCI_ERS_RESULT_DISCONNECT;
16551 	}
16552 
16553 	return result;
16554 }
16555 
16556 /**
16557  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16558  * @pdev: PCI device information struct
16559  */
16560 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16561 {
16562 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16563 
16564 	i40e_prep_for_reset(pf);
16565 }
16566 
16567 /**
16568  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16569  * @pdev: PCI device information struct
16570  */
16571 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16572 {
16573 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16574 
16575 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16576 		return;
16577 
16578 	i40e_reset_and_rebuild(pf, false, false);
16579 #ifdef CONFIG_PCI_IOV
16580 	i40e_restore_all_vfs_msi_state(pdev);
16581 #endif /* CONFIG_PCI_IOV */
16582 }
16583 
16584 /**
16585  * i40e_pci_error_resume - restart operations after PCI error recovery
16586  * @pdev: PCI device information struct
16587  *
16588  * Called to allow the driver to bring things back up after PCI error
16589  * and/or reset recovery has finished.
16590  **/
16591 static void i40e_pci_error_resume(struct pci_dev *pdev)
16592 {
16593 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16594 
16595 	dev_dbg(&pdev->dev, "%s\n", __func__);
16596 	if (test_bit(__I40E_SUSPENDED, pf->state))
16597 		return;
16598 
16599 	i40e_io_resume(pf);
16600 }
16601 
16602 /**
16603  * i40e_shutdown - PCI callback for shutting down
16604  * @pdev: PCI device information struct
16605  **/
16606 static void i40e_shutdown(struct pci_dev *pdev)
16607 {
16608 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16609 	struct i40e_hw *hw = &pf->hw;
16610 
16611 	set_bit(__I40E_SUSPENDED, pf->state);
16612 	set_bit(__I40E_DOWN, pf->state);
16613 
16614 	del_timer_sync(&pf->service_timer);
16615 	cancel_work_sync(&pf->service_task);
16616 	i40e_cloud_filter_exit(pf);
16617 	i40e_fdir_teardown(pf);
16618 
16619 	/* Client close must be called explicitly here because the timer
16620 	 * has been stopped.
16621 	 */
16622 	i40e_notify_client_of_netdev_close(pf, false);
16623 
16624 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16625 	    pf->wol_en)
16626 		i40e_enable_mc_magic_wake(pf);
16627 
16628 	i40e_prep_for_reset(pf);
16629 
16630 	wr32(hw, I40E_PFPM_APM,
16631 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16632 	wr32(hw, I40E_PFPM_WUFC,
16633 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16634 
16635 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16636 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16637 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16638 		free_irq(pf->pdev->irq, pf);
16639 
16640 	/* Since we're going to destroy queues during the
16641 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16642 	 * whole section
16643 	 */
16644 	rtnl_lock();
16645 	i40e_clear_interrupt_scheme(pf);
16646 	rtnl_unlock();
16647 
16648 	if (system_state == SYSTEM_POWER_OFF) {
16649 		pci_wake_from_d3(pdev, pf->wol_en);
16650 		pci_set_power_state(pdev, PCI_D3hot);
16651 	}
16652 }
16653 
16654 /**
16655  * i40e_suspend - PM callback for moving to D3
16656  * @dev: generic device information structure
16657  **/
16658 static int i40e_suspend(struct device *dev)
16659 {
16660 	struct i40e_pf *pf = dev_get_drvdata(dev);
16661 
16662 	/* If we're already suspended, then there is nothing to do */
16663 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16664 		return 0;
16665 	return i40e_io_suspend(pf);
16666 }
16667 
16668 /**
16669  * i40e_resume - PM callback for waking up from D3
16670  * @dev: generic device information structure
16671  **/
16672 static int i40e_resume(struct device *dev)
16673 {
16674 	struct i40e_pf *pf = dev_get_drvdata(dev);
16675 
16676 	/* If we're not suspended, then there is nothing to do */
16677 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16678 		return 0;
16679 	return i40e_io_resume(pf);
16680 }
16681 
16682 static const struct pci_error_handlers i40e_err_handler = {
16683 	.error_detected = i40e_pci_error_detected,
16684 	.slot_reset = i40e_pci_error_slot_reset,
16685 	.reset_prepare = i40e_pci_error_reset_prepare,
16686 	.reset_done = i40e_pci_error_reset_done,
16687 	.resume = i40e_pci_error_resume,
16688 };
16689 
16690 static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16691 
16692 static struct pci_driver i40e_driver = {
16693 	.name     = i40e_driver_name,
16694 	.id_table = i40e_pci_tbl,
16695 	.probe    = i40e_probe,
16696 	.remove   = i40e_remove,
16697 	.driver.pm = pm_sleep_ptr(&i40e_pm_ops),
16698 	.shutdown = i40e_shutdown,
16699 	.err_handler = &i40e_err_handler,
16700 	.sriov_configure = i40e_pci_sriov_configure,
16701 };
16702 
16703 /**
16704  * i40e_init_module - Driver registration routine
16705  *
16706  * i40e_init_module is the first routine called when the driver is
16707  * loaded. All it does is register with the PCI subsystem.
16708  **/
16709 static int __init i40e_init_module(void)
16710 {
16711 	int err;
16712 
16713 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16714 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16715 
16716 	/* There is no need to throttle the number of active tasks because
16717 	 * each device limits its own task using a state bit for scheduling
16718 	 * the service task, and the device tasks do not interfere with each
16719 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16720 	 * since we need to be able to guarantee forward progress even under
16721 	 * memory pressure.
16722 	 */
16723 	i40e_wq = alloc_workqueue("%s", 0, 0, i40e_driver_name);
16724 	if (!i40e_wq) {
16725 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16726 		return -ENOMEM;
16727 	}
16728 
16729 	i40e_dbg_init();
16730 	err = pci_register_driver(&i40e_driver);
16731 	if (err) {
16732 		destroy_workqueue(i40e_wq);
16733 		i40e_dbg_exit();
16734 		return err;
16735 	}
16736 
16737 	return 0;
16738 }
16739 module_init(i40e_init_module);
16740 
16741 /**
16742  * i40e_exit_module - Driver exit cleanup routine
16743  *
16744  * i40e_exit_module is called just before the driver is removed
16745  * from memory.
16746  **/
16747 static void __exit i40e_exit_module(void)
16748 {
16749 	pci_unregister_driver(&i40e_driver);
16750 	destroy_workqueue(i40e_wq);
16751 	ida_destroy(&i40e_client_ida);
16752 	i40e_dbg_exit();
16753 }
16754 module_exit(i40e_exit_module);
16755