xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision e28c5efc31397af17bc5a7d55b963f59bcde0166)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <generated/utsrelease.h>
5 #include <linux/crash_dump.h>
6 #include <linux/if_bridge.h>
7 #include <linux/if_macvlan.h>
8 #include <linux/module.h>
9 #include <net/pkt_cls.h>
10 #include <net/xdp_sock_drv.h>
11 
12 /* Local includes */
13 #include "i40e.h"
14 #include "i40e_devids.h"
15 #include "i40e_diag.h"
16 #include "i40e_lan_hmc.h"
17 #include "i40e_virtchnl_pf.h"
18 #include "i40e_xsk.h"
19 
20 /* All i40e tracepoints are defined by the include below, which
21  * must be included exactly once across the whole kernel with
22  * CREATE_TRACE_POINTS defined
23  */
24 #define CREATE_TRACE_POINTS
25 #include "i40e_trace.h"
26 
27 const char i40e_driver_name[] = "i40e";
28 static const char i40e_driver_string[] =
29 			"Intel(R) Ethernet Connection XL710 Network Driver";
30 
31 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
32 
33 /* a bit of forward declarations */
34 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
35 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
36 static int i40e_add_vsi(struct i40e_vsi *vsi);
37 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
38 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
39 static int i40e_setup_misc_vector(struct i40e_pf *pf);
40 static void i40e_determine_queue_usage(struct i40e_pf *pf);
41 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
42 static void i40e_prep_for_reset(struct i40e_pf *pf);
43 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
44 				   bool lock_acquired);
45 static int i40e_reset(struct i40e_pf *pf);
46 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
47 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
48 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
49 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
50 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
51 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
52 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
53 static int i40e_get_capabilities(struct i40e_pf *pf,
54 				 enum i40e_admin_queue_opc list_type);
55 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
56 
57 /* i40e_pci_tbl - PCI Device ID Table
58  *
59  * Last entry must be all 0s
60  *
61  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
62  *   Class, Class Mask, private data (not used) }
63  */
64 static const struct pci_device_id i40e_pci_tbl[] = {
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
91 	/* required last entry */
92 	{0, }
93 };
94 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95 
96 #define I40E_MAX_VF_COUNT 128
97 static int debug = -1;
98 module_param(debug, uint, 0);
99 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
100 
101 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103 MODULE_LICENSE("GPL v2");
104 
105 static struct workqueue_struct *i40e_wq;
106 
107 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
108 				  struct net_device *netdev, int delta)
109 {
110 	struct netdev_hw_addr_list *ha_list;
111 	struct netdev_hw_addr *ha;
112 
113 	if (!f || !netdev)
114 		return;
115 
116 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
117 		ha_list = &netdev->uc;
118 	else
119 		ha_list = &netdev->mc;
120 
121 	netdev_hw_addr_list_for_each(ha, ha_list) {
122 		if (ether_addr_equal(ha->addr, f->macaddr)) {
123 			ha->refcount += delta;
124 			if (ha->refcount <= 0)
125 				ha->refcount = 1;
126 			break;
127 		}
128 	}
129 }
130 
131 /**
132  * i40e_hw_to_dev - get device pointer from the hardware structure
133  * @hw: pointer to the device HW structure
134  **/
135 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
136 {
137 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
138 
139 	return &pf->pdev->dev;
140 }
141 
142 /**
143  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to fill out
146  * @size: size of memory requested
147  * @alignment: what to align the allocation to
148  **/
149 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
150 			  u64 size, u32 alignment)
151 {
152 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
153 
154 	mem->size = ALIGN(size, alignment);
155 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
156 				     GFP_KERNEL);
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_dma_mem - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
168 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
169 {
170 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
171 
172 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
173 	mem->va = NULL;
174 	mem->pa = 0;
175 	mem->size = 0;
176 
177 	return 0;
178 }
179 
180 /**
181  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
182  * @hw:   pointer to the HW structure
183  * @mem:  ptr to mem struct to fill out
184  * @size: size of memory requested
185  **/
186 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
187 			   u32 size)
188 {
189 	mem->size = size;
190 	mem->va = kzalloc(size, GFP_KERNEL);
191 
192 	if (!mem->va)
193 		return -ENOMEM;
194 
195 	return 0;
196 }
197 
198 /**
199  * i40e_free_virt_mem - OS specific memory free for shared code
200  * @hw:   pointer to the HW structure
201  * @mem:  ptr to mem struct to free
202  **/
203 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
204 {
205 	/* it's ok to kfree a NULL pointer */
206 	kfree(mem->va);
207 	mem->va = NULL;
208 	mem->size = 0;
209 
210 	return 0;
211 }
212 
213 /**
214  * i40e_get_lump - find a lump of free generic resource
215  * @pf: board private structure
216  * @pile: the pile of resource to search
217  * @needed: the number of items needed
218  * @id: an owner id to stick on the items assigned
219  *
220  * Returns the base item index of the lump, or negative for error
221  **/
222 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
223 			 u16 needed, u16 id)
224 {
225 	int ret = -ENOMEM;
226 	int i, j;
227 
228 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
229 		dev_info(&pf->pdev->dev,
230 			 "param err: pile=%s needed=%d id=0x%04x\n",
231 			 pile ? "<valid>" : "<null>", needed, id);
232 		return -EINVAL;
233 	}
234 
235 	/* Allocate last queue in the pile for FDIR VSI queue
236 	 * so it doesn't fragment the qp_pile
237 	 */
238 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
239 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
240 			dev_err(&pf->pdev->dev,
241 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
242 				pile->num_entries - 1);
243 			return -ENOMEM;
244 		}
245 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
246 		return pile->num_entries - 1;
247 	}
248 
249 	i = 0;
250 	while (i < pile->num_entries) {
251 		/* skip already allocated entries */
252 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
253 			i++;
254 			continue;
255 		}
256 
257 		/* do we have enough in this lump? */
258 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
259 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
260 				break;
261 		}
262 
263 		if (j == needed) {
264 			/* there was enough, so assign it to the requestor */
265 			for (j = 0; j < needed; j++)
266 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
267 			ret = i;
268 			break;
269 		}
270 
271 		/* not enough, so skip over it and continue looking */
272 		i += j;
273 	}
274 
275 	return ret;
276 }
277 
278 /**
279  * i40e_put_lump - return a lump of generic resource
280  * @pile: the pile of resource to search
281  * @index: the base item index
282  * @id: the owner id of the items assigned
283  *
284  * Returns the count of items in the lump
285  **/
286 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
287 {
288 	int valid_id = (id | I40E_PILE_VALID_BIT);
289 	int count = 0;
290 	u16 i;
291 
292 	if (!pile || index >= pile->num_entries)
293 		return -EINVAL;
294 
295 	for (i = index;
296 	     i < pile->num_entries && pile->list[i] == valid_id;
297 	     i++) {
298 		pile->list[i] = 0;
299 		count++;
300 	}
301 
302 
303 	return count;
304 }
305 
306 /**
307  * i40e_find_vsi_from_id - searches for the vsi with the given id
308  * @pf: the pf structure to search for the vsi
309  * @id: id of the vsi it is searching for
310  **/
311 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
312 {
313 	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 == pf->vsi[pf->lan_vsi]) {
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 == pf->vsi[pf->lan_vsi])
1238 		i40e_update_pf_stats(pf);
1239 
1240 	i40e_update_vsi_stats(vsi);
1241 }
1242 
1243 /**
1244  * i40e_count_filters - counts VSI mac filters
1245  * @vsi: the VSI to be searched
1246  *
1247  * Returns count of mac filters
1248  **/
1249 int i40e_count_filters(struct i40e_vsi *vsi)
1250 {
1251 	struct i40e_mac_filter *f;
1252 	struct hlist_node *h;
1253 	int bkt;
1254 	int cnt = 0;
1255 
1256 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1257 		++cnt;
1258 
1259 	return cnt;
1260 }
1261 
1262 /**
1263  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1264  * @vsi: the VSI to be searched
1265  * @macaddr: the MAC address
1266  * @vlan: the vlan
1267  *
1268  * Returns ptr to the filter object or NULL
1269  **/
1270 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1271 						const u8 *macaddr, s16 vlan)
1272 {
1273 	struct i40e_mac_filter *f;
1274 	u64 key;
1275 
1276 	if (!vsi || !macaddr)
1277 		return NULL;
1278 
1279 	key = i40e_addr_to_hkey(macaddr);
1280 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1281 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1282 		    (vlan == f->vlan))
1283 			return f;
1284 	}
1285 	return NULL;
1286 }
1287 
1288 /**
1289  * i40e_find_mac - Find a mac addr in the macvlan filters list
1290  * @vsi: the VSI to be searched
1291  * @macaddr: the MAC address we are searching for
1292  *
1293  * Returns the first filter with the provided MAC address or NULL if
1294  * MAC address was not found
1295  **/
1296 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1297 {
1298 	struct i40e_mac_filter *f;
1299 	u64 key;
1300 
1301 	if (!vsi || !macaddr)
1302 		return NULL;
1303 
1304 	key = i40e_addr_to_hkey(macaddr);
1305 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1306 		if ((ether_addr_equal(macaddr, f->macaddr)))
1307 			return f;
1308 	}
1309 	return NULL;
1310 }
1311 
1312 /**
1313  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1314  * @vsi: the VSI to be searched
1315  *
1316  * Returns true if VSI is in vlan mode or false otherwise
1317  **/
1318 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1319 {
1320 	/* If we have a PVID, always operate in VLAN mode */
1321 	if (vsi->info.pvid)
1322 		return true;
1323 
1324 	/* We need to operate in VLAN mode whenever we have any filters with
1325 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1326 	 * time, incurring search cost repeatedly. However, we can notice two
1327 	 * things:
1328 	 *
1329 	 * 1) the only place where we can gain a VLAN filter is in
1330 	 *    i40e_add_filter.
1331 	 *
1332 	 * 2) the only place where filters are actually removed is in
1333 	 *    i40e_sync_filters_subtask.
1334 	 *
1335 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1336 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1337 	 * we have to perform the full search after deleting filters in
1338 	 * i40e_sync_filters_subtask, but we already have to search
1339 	 * filters here and can perform the check at the same time. This
1340 	 * results in avoiding embedding a loop for VLAN mode inside another
1341 	 * loop over all the filters, and should maintain correctness as noted
1342 	 * above.
1343 	 */
1344 	return vsi->has_vlan_filter;
1345 }
1346 
1347 /**
1348  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1349  * @vsi: the VSI to configure
1350  * @tmp_add_list: list of filters ready to be added
1351  * @tmp_del_list: list of filters ready to be deleted
1352  * @vlan_filters: the number of active VLAN filters
1353  *
1354  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1355  * behave as expected. If we have any active VLAN filters remaining or about
1356  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1357  * so that they only match against untagged traffic. If we no longer have any
1358  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1359  * so that they match against both tagged and untagged traffic. In this way,
1360  * we ensure that we correctly receive the desired traffic. This ensures that
1361  * when we have an active VLAN we will receive only untagged traffic and
1362  * traffic matching active VLANs. If we have no active VLANs then we will
1363  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1364  *
1365  * Finally, in a similar fashion, this function also corrects filters when
1366  * there is an active PVID assigned to this VSI.
1367  *
1368  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1369  *
1370  * This function is only expected to be called from within
1371  * i40e_sync_vsi_filters.
1372  *
1373  * NOTE: This function expects to be called while under the
1374  * mac_filter_hash_lock
1375  */
1376 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1377 					 struct hlist_head *tmp_add_list,
1378 					 struct hlist_head *tmp_del_list,
1379 					 int vlan_filters)
1380 {
1381 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1382 	struct i40e_mac_filter *f, *add_head;
1383 	struct i40e_new_mac_filter *new;
1384 	struct hlist_node *h;
1385 	int bkt, new_vlan;
1386 
1387 	/* To determine if a particular filter needs to be replaced we
1388 	 * have the three following conditions:
1389 	 *
1390 	 * a) if we have a PVID assigned, then all filters which are
1391 	 *    not marked as VLAN=PVID must be replaced with filters that
1392 	 *    are.
1393 	 * b) otherwise, if we have any active VLANS, all filters
1394 	 *    which are marked as VLAN=-1 must be replaced with
1395 	 *    filters marked as VLAN=0
1396 	 * c) finally, if we do not have any active VLANS, all filters
1397 	 *    which are marked as VLAN=0 must be replaced with filters
1398 	 *    marked as VLAN=-1
1399 	 */
1400 
1401 	/* Update the filters about to be added in place */
1402 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1403 		if (pvid && new->f->vlan != pvid)
1404 			new->f->vlan = pvid;
1405 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1406 			new->f->vlan = 0;
1407 		else if (!vlan_filters && new->f->vlan == 0)
1408 			new->f->vlan = I40E_VLAN_ANY;
1409 	}
1410 
1411 	/* Update the remaining active filters */
1412 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1413 		/* Combine the checks for whether a filter needs to be changed
1414 		 * and then determine the new VLAN inside the if block, in
1415 		 * order to avoid duplicating code for adding the new filter
1416 		 * then deleting the old filter.
1417 		 */
1418 		if ((pvid && f->vlan != pvid) ||
1419 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1420 		    (!vlan_filters && f->vlan == 0)) {
1421 			/* Determine the new vlan we will be adding */
1422 			if (pvid)
1423 				new_vlan = pvid;
1424 			else if (vlan_filters)
1425 				new_vlan = 0;
1426 			else
1427 				new_vlan = I40E_VLAN_ANY;
1428 
1429 			/* Create the new filter */
1430 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1431 			if (!add_head)
1432 				return -ENOMEM;
1433 
1434 			/* Create a temporary i40e_new_mac_filter */
1435 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1436 			if (!new)
1437 				return -ENOMEM;
1438 
1439 			new->f = add_head;
1440 			new->state = add_head->state;
1441 
1442 			/* Add the new filter to the tmp list */
1443 			hlist_add_head(&new->hlist, tmp_add_list);
1444 
1445 			/* Put the original filter into the delete list */
1446 			f->state = I40E_FILTER_REMOVE;
1447 			hash_del(&f->hlist);
1448 			hlist_add_head(&f->hlist, tmp_del_list);
1449 		}
1450 	}
1451 
1452 	vsi->has_vlan_filter = !!vlan_filters;
1453 
1454 	return 0;
1455 }
1456 
1457 /**
1458  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1459  * @vsi: the vsi to configure
1460  * @new_mac: new mac filter to be added
1461  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1462  * @vlan_filters: the number of active VLAN filters
1463  * @trusted: flag if the VF is trusted
1464  *
1465  * Get new VLAN id based on current VLAN filters, trust, PVID
1466  * and vf-vlan-prune-disable flag.
1467  *
1468  * Returns the value of the new vlan filter or
1469  * the old value if no new filter is needed.
1470  */
1471 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1472 				struct i40e_new_mac_filter *new_mac,
1473 				struct i40e_mac_filter *f,
1474 				int vlan_filters,
1475 				bool trusted)
1476 {
1477 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1478 	struct i40e_pf *pf = vsi->back;
1479 	bool is_any;
1480 
1481 	if (new_mac)
1482 		f = new_mac->f;
1483 
1484 	if (pvid && f->vlan != pvid)
1485 		return pvid;
1486 
1487 	is_any = (trusted ||
1488 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1489 
1490 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1491 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1492 	    (is_any && !vlan_filters && f->vlan == 0)) {
1493 		if (is_any)
1494 			return I40E_VLAN_ANY;
1495 		else
1496 			return 0;
1497 	}
1498 
1499 	return f->vlan;
1500 }
1501 
1502 /**
1503  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1504  * @vsi: the vsi to configure
1505  * @tmp_add_list: list of filters ready to be added
1506  * @tmp_del_list: list of filters ready to be deleted
1507  * @vlan_filters: the number of active VLAN filters
1508  * @trusted: flag if the VF is trusted
1509  *
1510  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1511  * and vf-vlan-prune-disable flag.
1512  *
1513  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1514  *
1515  * This function is only expected to be called from within
1516  * i40e_sync_vsi_filters.
1517  *
1518  * NOTE: This function expects to be called while under the
1519  * mac_filter_hash_lock
1520  */
1521 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1522 					    struct hlist_head *tmp_add_list,
1523 					    struct hlist_head *tmp_del_list,
1524 					    int vlan_filters,
1525 					    bool trusted)
1526 {
1527 	struct i40e_mac_filter *f, *add_head;
1528 	struct i40e_new_mac_filter *new_mac;
1529 	struct hlist_node *h;
1530 	int bkt, new_vlan;
1531 
1532 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1533 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1534 							vlan_filters, trusted);
1535 	}
1536 
1537 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1538 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1539 						trusted);
1540 		if (new_vlan != f->vlan) {
1541 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1542 			if (!add_head)
1543 				return -ENOMEM;
1544 			/* Create a temporary i40e_new_mac_filter */
1545 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1546 			if (!new_mac)
1547 				return -ENOMEM;
1548 			new_mac->f = add_head;
1549 			new_mac->state = add_head->state;
1550 
1551 			/* Add the new filter to the tmp list */
1552 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1553 
1554 			/* Put the original filter into the delete list */
1555 			f->state = I40E_FILTER_REMOVE;
1556 			hash_del(&f->hlist);
1557 			hlist_add_head(&f->hlist, tmp_del_list);
1558 		}
1559 	}
1560 
1561 	vsi->has_vlan_filter = !!vlan_filters;
1562 	return 0;
1563 }
1564 
1565 /**
1566  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1567  * @vsi: the PF Main VSI - inappropriate for any other VSI
1568  * @macaddr: the MAC address
1569  *
1570  * Remove whatever filter the firmware set up so the driver can manage
1571  * its own filtering intelligently.
1572  **/
1573 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1574 {
1575 	struct i40e_aqc_remove_macvlan_element_data element;
1576 	struct i40e_pf *pf = vsi->back;
1577 
1578 	/* Only appropriate for the PF main VSI */
1579 	if (vsi->type != I40E_VSI_MAIN)
1580 		return;
1581 
1582 	memset(&element, 0, sizeof(element));
1583 	ether_addr_copy(element.mac_addr, macaddr);
1584 	element.vlan_tag = 0;
1585 	/* Ignore error returns, some firmware does it this way... */
1586 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1587 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1588 
1589 	memset(&element, 0, sizeof(element));
1590 	ether_addr_copy(element.mac_addr, macaddr);
1591 	element.vlan_tag = 0;
1592 	/* ...and some firmware does it this way. */
1593 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1594 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1595 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1596 }
1597 
1598 /**
1599  * i40e_add_filter - Add a mac/vlan filter to the VSI
1600  * @vsi: the VSI to be searched
1601  * @macaddr: the MAC address
1602  * @vlan: the vlan
1603  *
1604  * Returns ptr to the filter object or NULL when no memory available.
1605  *
1606  * NOTE: This function is expected to be called with mac_filter_hash_lock
1607  * being held.
1608  **/
1609 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1610 					const u8 *macaddr, s16 vlan)
1611 {
1612 	struct i40e_mac_filter *f;
1613 	u64 key;
1614 
1615 	if (!vsi || !macaddr)
1616 		return NULL;
1617 
1618 	f = i40e_find_filter(vsi, macaddr, vlan);
1619 	if (!f) {
1620 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1621 		if (!f)
1622 			return NULL;
1623 
1624 		/* Update the boolean indicating if we need to function in
1625 		 * VLAN mode.
1626 		 */
1627 		if (vlan >= 0)
1628 			vsi->has_vlan_filter = true;
1629 
1630 		ether_addr_copy(f->macaddr, macaddr);
1631 		f->vlan = vlan;
1632 		f->state = I40E_FILTER_NEW;
1633 		INIT_HLIST_NODE(&f->hlist);
1634 
1635 		key = i40e_addr_to_hkey(macaddr);
1636 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1637 
1638 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1639 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1640 	}
1641 
1642 	/* If we're asked to add a filter that has been marked for removal, it
1643 	 * is safe to simply restore it to active state. __i40e_del_filter
1644 	 * will have simply deleted any filters which were previously marked
1645 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1646 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1647 	 * task, just restore this filter to the ACTIVE state so that the
1648 	 * sync task leaves it in place
1649 	 */
1650 	if (f->state == I40E_FILTER_REMOVE)
1651 		f->state = I40E_FILTER_ACTIVE;
1652 
1653 	return f;
1654 }
1655 
1656 /**
1657  * __i40e_del_filter - Remove a specific filter from the VSI
1658  * @vsi: VSI to remove from
1659  * @f: the filter to remove from the list
1660  *
1661  * This function should be called instead of i40e_del_filter only if you know
1662  * the exact filter you will remove already, such as via i40e_find_filter or
1663  * i40e_find_mac.
1664  *
1665  * NOTE: This function is expected to be called with mac_filter_hash_lock
1666  * being held.
1667  * ANOTHER NOTE: This function MUST be called from within the context of
1668  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1669  * instead of list_for_each_entry().
1670  **/
1671 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1672 {
1673 	if (!f)
1674 		return;
1675 
1676 	/* If the filter was never added to firmware then we can just delete it
1677 	 * directly and we don't want to set the status to remove or else an
1678 	 * admin queue command will unnecessarily fire.
1679 	 */
1680 	if ((f->state == I40E_FILTER_FAILED) ||
1681 	    (f->state == I40E_FILTER_NEW)) {
1682 		hash_del(&f->hlist);
1683 		kfree(f);
1684 	} else {
1685 		f->state = I40E_FILTER_REMOVE;
1686 	}
1687 
1688 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1689 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1690 }
1691 
1692 /**
1693  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1694  * @vsi: the VSI to be searched
1695  * @macaddr: the MAC address
1696  * @vlan: the VLAN
1697  *
1698  * NOTE: This function is expected to be called with mac_filter_hash_lock
1699  * being held.
1700  * ANOTHER NOTE: This function MUST be called from within the context of
1701  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1702  * instead of list_for_each_entry().
1703  **/
1704 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1705 {
1706 	struct i40e_mac_filter *f;
1707 
1708 	if (!vsi || !macaddr)
1709 		return;
1710 
1711 	f = i40e_find_filter(vsi, macaddr, vlan);
1712 	__i40e_del_filter(vsi, f);
1713 }
1714 
1715 /**
1716  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1717  * @vsi: the VSI to be searched
1718  * @macaddr: the mac address to be filtered
1719  *
1720  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1721  * go through all the macvlan filters and add a macvlan filter for each
1722  * unique vlan that already exists. If a PVID has been assigned, instead only
1723  * add the macaddr to that VLAN.
1724  *
1725  * Returns last filter added on success, else NULL
1726  **/
1727 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1728 					    const u8 *macaddr)
1729 {
1730 	struct i40e_mac_filter *f, *add = NULL;
1731 	struct hlist_node *h;
1732 	int bkt;
1733 
1734 	if (vsi->info.pvid)
1735 		return i40e_add_filter(vsi, macaddr,
1736 				       le16_to_cpu(vsi->info.pvid));
1737 
1738 	if (!i40e_is_vsi_in_vlan(vsi))
1739 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1740 
1741 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1742 		if (f->state == I40E_FILTER_REMOVE)
1743 			continue;
1744 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1745 		if (!add)
1746 			return NULL;
1747 	}
1748 
1749 	return add;
1750 }
1751 
1752 /**
1753  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1754  * @vsi: the VSI to be searched
1755  * @macaddr: the mac address to be removed
1756  *
1757  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1758  * associated with.
1759  *
1760  * Returns 0 for success, or error
1761  **/
1762 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1763 {
1764 	struct i40e_mac_filter *f;
1765 	struct hlist_node *h;
1766 	bool found = false;
1767 	int bkt;
1768 
1769 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1770 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1771 		if (ether_addr_equal(macaddr, f->macaddr)) {
1772 			__i40e_del_filter(vsi, f);
1773 			found = true;
1774 		}
1775 	}
1776 
1777 	if (found)
1778 		return 0;
1779 	else
1780 		return -ENOENT;
1781 }
1782 
1783 /**
1784  * i40e_set_mac - NDO callback to set mac address
1785  * @netdev: network interface device structure
1786  * @p: pointer to an address structure
1787  *
1788  * Returns 0 on success, negative on failure
1789  **/
1790 static int i40e_set_mac(struct net_device *netdev, void *p)
1791 {
1792 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1793 	struct i40e_vsi *vsi = np->vsi;
1794 	struct i40e_pf *pf = vsi->back;
1795 	struct i40e_hw *hw = &pf->hw;
1796 	struct sockaddr *addr = p;
1797 
1798 	if (!is_valid_ether_addr(addr->sa_data))
1799 		return -EADDRNOTAVAIL;
1800 
1801 	if (test_bit(__I40E_DOWN, pf->state) ||
1802 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1803 		return -EADDRNOTAVAIL;
1804 
1805 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1806 		netdev_info(netdev, "returning to hw mac address %pM\n",
1807 			    hw->mac.addr);
1808 	else
1809 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1810 
1811 	/* Copy the address first, so that we avoid a possible race with
1812 	 * .set_rx_mode().
1813 	 * - Remove old address from MAC filter
1814 	 * - Copy new address
1815 	 * - Add new address to MAC filter
1816 	 */
1817 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1818 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1819 	eth_hw_addr_set(netdev, addr->sa_data);
1820 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1821 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1822 
1823 	if (vsi->type == I40E_VSI_MAIN) {
1824 		int ret;
1825 
1826 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1827 						addr->sa_data, NULL);
1828 		if (ret)
1829 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1830 				    ERR_PTR(ret),
1831 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1832 	}
1833 
1834 	/* schedule our worker thread which will take care of
1835 	 * applying the new filter changes
1836 	 */
1837 	i40e_service_event_schedule(pf);
1838 	return 0;
1839 }
1840 
1841 /**
1842  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1843  * @vsi: vsi structure
1844  * @seed: RSS hash seed
1845  * @lut: pointer to lookup table of lut_size
1846  * @lut_size: size of the lookup table
1847  **/
1848 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1849 			      u8 *lut, u16 lut_size)
1850 {
1851 	struct i40e_pf *pf = vsi->back;
1852 	struct i40e_hw *hw = &pf->hw;
1853 	int ret = 0;
1854 
1855 	if (seed) {
1856 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1857 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1858 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1859 		if (ret) {
1860 			dev_info(&pf->pdev->dev,
1861 				 "Cannot set RSS key, err %pe aq_err %s\n",
1862 				 ERR_PTR(ret),
1863 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1864 			return ret;
1865 		}
1866 	}
1867 	if (lut) {
1868 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1869 
1870 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1871 		if (ret) {
1872 			dev_info(&pf->pdev->dev,
1873 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1874 				 ERR_PTR(ret),
1875 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1876 			return ret;
1877 		}
1878 	}
1879 	return ret;
1880 }
1881 
1882 /**
1883  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1884  * @vsi: VSI structure
1885  **/
1886 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1887 {
1888 	struct i40e_pf *pf = vsi->back;
1889 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1890 	u8 *lut;
1891 	int ret;
1892 
1893 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1894 		return 0;
1895 	if (!vsi->rss_size)
1896 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1897 				      vsi->num_queue_pairs);
1898 	if (!vsi->rss_size)
1899 		return -EINVAL;
1900 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1901 	if (!lut)
1902 		return -ENOMEM;
1903 
1904 	/* Use the user configured hash keys and lookup table if there is one,
1905 	 * otherwise use default
1906 	 */
1907 	if (vsi->rss_lut_user)
1908 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1909 	else
1910 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1911 	if (vsi->rss_hkey_user)
1912 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1913 	else
1914 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1915 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1916 	kfree(lut);
1917 	return ret;
1918 }
1919 
1920 /**
1921  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1922  * @vsi: the VSI being configured,
1923  * @ctxt: VSI context structure
1924  * @enabled_tc: number of traffic classes to enable
1925  *
1926  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1927  **/
1928 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1929 					   struct i40e_vsi_context *ctxt,
1930 					   u8 enabled_tc)
1931 {
1932 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1933 	int i, override_q, pow, num_qps, ret;
1934 	u8 netdev_tc = 0, offset = 0;
1935 
1936 	if (vsi->type != I40E_VSI_MAIN)
1937 		return -EINVAL;
1938 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1939 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1940 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1941 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1942 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1943 
1944 	/* find the next higher power-of-2 of num queue pairs */
1945 	pow = ilog2(num_qps);
1946 	if (!is_power_of_2(num_qps))
1947 		pow++;
1948 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1949 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1950 
1951 	/* Setup queue offset/count for all TCs for given VSI */
1952 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1953 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1954 		/* See if the given TC is enabled for the given VSI */
1955 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1956 			offset = vsi->mqprio_qopt.qopt.offset[i];
1957 			qcount = vsi->mqprio_qopt.qopt.count[i];
1958 			if (qcount > max_qcount)
1959 				max_qcount = qcount;
1960 			vsi->tc_config.tc_info[i].qoffset = offset;
1961 			vsi->tc_config.tc_info[i].qcount = qcount;
1962 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1963 		} else {
1964 			/* TC is not enabled so set the offset to
1965 			 * default queue and allocate one queue
1966 			 * for the given TC.
1967 			 */
1968 			vsi->tc_config.tc_info[i].qoffset = 0;
1969 			vsi->tc_config.tc_info[i].qcount = 1;
1970 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1971 		}
1972 	}
1973 
1974 	/* Set actual Tx/Rx queue pairs */
1975 	vsi->num_queue_pairs = offset + qcount;
1976 
1977 	/* Setup queue TC[0].qmap for given VSI context */
1978 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1979 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1980 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1981 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1982 
1983 	/* Reconfigure RSS for main VSI with max queue count */
1984 	vsi->rss_size = max_qcount;
1985 	ret = i40e_vsi_config_rss(vsi);
1986 	if (ret) {
1987 		dev_info(&vsi->back->pdev->dev,
1988 			 "Failed to reconfig rss for num_queues (%u)\n",
1989 			 max_qcount);
1990 		return ret;
1991 	}
1992 	vsi->reconfig_rss = true;
1993 	dev_dbg(&vsi->back->pdev->dev,
1994 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1995 
1996 	/* Find queue count available for channel VSIs and starting offset
1997 	 * for channel VSIs
1998 	 */
1999 	override_q = vsi->mqprio_qopt.qopt.count[0];
2000 	if (override_q && override_q < vsi->num_queue_pairs) {
2001 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2002 		vsi->next_base_queue = override_q;
2003 	}
2004 	return 0;
2005 }
2006 
2007 /**
2008  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2009  * @vsi: the VSI being setup
2010  * @ctxt: VSI context structure
2011  * @enabled_tc: Enabled TCs bitmap
2012  * @is_add: True if called before Add VSI
2013  *
2014  * Setup VSI queue mapping for enabled traffic classes.
2015  **/
2016 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2017 				     struct i40e_vsi_context *ctxt,
2018 				     u8 enabled_tc,
2019 				     bool is_add)
2020 {
2021 	struct i40e_pf *pf = vsi->back;
2022 	u16 num_tc_qps = 0;
2023 	u16 sections = 0;
2024 	u8 netdev_tc = 0;
2025 	u16 numtc = 1;
2026 	u16 qcount;
2027 	u8 offset;
2028 	u16 qmap;
2029 	int i;
2030 
2031 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2032 	offset = 0;
2033 	/* zero out queue mapping, it will get updated on the end of the function */
2034 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2035 
2036 	if (vsi->type == I40E_VSI_MAIN) {
2037 		/* This code helps add more queue to the VSI if we have
2038 		 * more cores than RSS can support, the higher cores will
2039 		 * be served by ATR or other filters. Furthermore, the
2040 		 * non-zero req_queue_pairs says that user requested a new
2041 		 * queue count via ethtool's set_channels, so use this
2042 		 * value for queues distribution across traffic classes
2043 		 * We need at least one queue pair for the interface
2044 		 * to be usable as we see in else statement.
2045 		 */
2046 		if (vsi->req_queue_pairs > 0)
2047 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2048 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2049 			vsi->num_queue_pairs = pf->num_lan_msix;
2050 		else
2051 			vsi->num_queue_pairs = 1;
2052 	}
2053 
2054 	/* Number of queues per enabled TC */
2055 	if (vsi->type == I40E_VSI_MAIN ||
2056 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2057 		num_tc_qps = vsi->num_queue_pairs;
2058 	else
2059 		num_tc_qps = vsi->alloc_queue_pairs;
2060 
2061 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2062 		/* Find numtc from enabled TC bitmap */
2063 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2064 			if (enabled_tc & BIT(i)) /* TC is enabled */
2065 				numtc++;
2066 		}
2067 		if (!numtc) {
2068 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2069 			numtc = 1;
2070 		}
2071 		num_tc_qps = num_tc_qps / numtc;
2072 		num_tc_qps = min_t(int, num_tc_qps,
2073 				   i40e_pf_get_max_q_per_tc(pf));
2074 	}
2075 
2076 	vsi->tc_config.numtc = numtc;
2077 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2078 
2079 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2080 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2081 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2082 
2083 	/* Setup queue offset/count for all TCs for given VSI */
2084 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2085 		/* See if the given TC is enabled for the given VSI */
2086 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2087 			/* TC is enabled */
2088 			int pow, num_qps;
2089 
2090 			switch (vsi->type) {
2091 			case I40E_VSI_MAIN:
2092 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2093 					       pf->flags) &&
2094 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2095 					       pf->flags)) ||
2096 				    vsi->tc_config.enabled_tc != 1) {
2097 					qcount = min_t(int, pf->alloc_rss_size,
2098 						       num_tc_qps);
2099 					break;
2100 				}
2101 				fallthrough;
2102 			case I40E_VSI_FDIR:
2103 			case I40E_VSI_SRIOV:
2104 			case I40E_VSI_VMDQ2:
2105 			default:
2106 				qcount = num_tc_qps;
2107 				WARN_ON(i != 0);
2108 				break;
2109 			}
2110 			vsi->tc_config.tc_info[i].qoffset = offset;
2111 			vsi->tc_config.tc_info[i].qcount = qcount;
2112 
2113 			/* find the next higher power-of-2 of num queue pairs */
2114 			num_qps = qcount;
2115 			pow = 0;
2116 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2117 				pow++;
2118 				num_qps >>= 1;
2119 			}
2120 
2121 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2122 			qmap =
2123 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2124 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2125 
2126 			offset += qcount;
2127 		} else {
2128 			/* TC is not enabled so set the offset to
2129 			 * default queue and allocate one queue
2130 			 * for the given TC.
2131 			 */
2132 			vsi->tc_config.tc_info[i].qoffset = 0;
2133 			vsi->tc_config.tc_info[i].qcount = 1;
2134 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2135 
2136 			qmap = 0;
2137 		}
2138 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2139 	}
2140 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2141 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2142 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2143 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2144 		vsi->num_queue_pairs = offset;
2145 
2146 	/* Scheduler section valid can only be set for ADD VSI */
2147 	if (is_add) {
2148 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2149 
2150 		ctxt->info.up_enable_bits = enabled_tc;
2151 	}
2152 	if (vsi->type == I40E_VSI_SRIOV) {
2153 		ctxt->info.mapping_flags |=
2154 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2155 		for (i = 0; i < vsi->num_queue_pairs; i++)
2156 			ctxt->info.queue_mapping[i] =
2157 					       cpu_to_le16(vsi->base_queue + i);
2158 	} else {
2159 		ctxt->info.mapping_flags |=
2160 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2161 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2162 	}
2163 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2164 }
2165 
2166 /**
2167  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2168  * @netdev: the netdevice
2169  * @addr: address to add
2170  *
2171  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2172  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2173  */
2174 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2175 {
2176 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2177 	struct i40e_vsi *vsi = np->vsi;
2178 
2179 	if (i40e_add_mac_filter(vsi, addr))
2180 		return 0;
2181 	else
2182 		return -ENOMEM;
2183 }
2184 
2185 /**
2186  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2187  * @netdev: the netdevice
2188  * @addr: address to add
2189  *
2190  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2191  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2192  */
2193 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2194 {
2195 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2196 	struct i40e_vsi *vsi = np->vsi;
2197 
2198 	/* Under some circumstances, we might receive a request to delete
2199 	 * our own device address from our uc list. Because we store the
2200 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2201 	 * such requests and not delete our device address from this list.
2202 	 */
2203 	if (ether_addr_equal(addr, netdev->dev_addr))
2204 		return 0;
2205 
2206 	i40e_del_mac_filter(vsi, addr);
2207 
2208 	return 0;
2209 }
2210 
2211 /**
2212  * i40e_set_rx_mode - NDO callback to set the netdev filters
2213  * @netdev: network interface device structure
2214  **/
2215 static void i40e_set_rx_mode(struct net_device *netdev)
2216 {
2217 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2218 	struct i40e_vsi *vsi = np->vsi;
2219 
2220 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2221 
2222 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2223 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2224 
2225 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2226 
2227 	/* check for other flag changes */
2228 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2229 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2230 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2231 	}
2232 }
2233 
2234 /**
2235  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2236  * @vsi: Pointer to VSI struct
2237  * @from: Pointer to list which contains MAC filter entries - changes to
2238  *        those entries needs to be undone.
2239  *
2240  * MAC filter entries from this list were slated for deletion.
2241  **/
2242 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2243 					 struct hlist_head *from)
2244 {
2245 	struct i40e_mac_filter *f;
2246 	struct hlist_node *h;
2247 
2248 	hlist_for_each_entry_safe(f, h, from, hlist) {
2249 		u64 key = i40e_addr_to_hkey(f->macaddr);
2250 
2251 		/* Move the element back into MAC filter list*/
2252 		hlist_del(&f->hlist);
2253 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2254 	}
2255 }
2256 
2257 /**
2258  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2259  * @vsi: Pointer to vsi struct
2260  * @from: Pointer to list which contains MAC filter entries - changes to
2261  *        those entries needs to be undone.
2262  *
2263  * MAC filter entries from this list were slated for addition.
2264  **/
2265 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2266 					 struct hlist_head *from)
2267 {
2268 	struct i40e_new_mac_filter *new;
2269 	struct hlist_node *h;
2270 
2271 	hlist_for_each_entry_safe(new, h, from, hlist) {
2272 		/* We can simply free the wrapper structure */
2273 		hlist_del(&new->hlist);
2274 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2275 		kfree(new);
2276 	}
2277 }
2278 
2279 /**
2280  * i40e_next_filter - Get the next non-broadcast filter from a list
2281  * @next: pointer to filter in list
2282  *
2283  * Returns the next non-broadcast filter in the list. Required so that we
2284  * ignore broadcast filters within the list, since these are not handled via
2285  * the normal firmware update path.
2286  */
2287 static
2288 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2289 {
2290 	hlist_for_each_entry_continue(next, hlist) {
2291 		if (!is_broadcast_ether_addr(next->f->macaddr))
2292 			return next;
2293 	}
2294 
2295 	return NULL;
2296 }
2297 
2298 /**
2299  * i40e_update_filter_state - Update filter state based on return data
2300  * from firmware
2301  * @count: Number of filters added
2302  * @add_list: return data from fw
2303  * @add_head: pointer to first filter in current batch
2304  *
2305  * MAC filter entries from list were slated to be added to device. Returns
2306  * number of successful filters. Note that 0 does NOT mean success!
2307  **/
2308 static int
2309 i40e_update_filter_state(int count,
2310 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2311 			 struct i40e_new_mac_filter *add_head)
2312 {
2313 	int retval = 0;
2314 	int i;
2315 
2316 	for (i = 0; i < count; i++) {
2317 		/* Always check status of each filter. We don't need to check
2318 		 * the firmware return status because we pre-set the filter
2319 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2320 		 * request to the adminq. Thus, if it no longer matches then
2321 		 * we know the filter is active.
2322 		 */
2323 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2324 			add_head->state = I40E_FILTER_FAILED;
2325 		} else {
2326 			add_head->state = I40E_FILTER_ACTIVE;
2327 			retval++;
2328 		}
2329 
2330 		add_head = i40e_next_filter(add_head);
2331 		if (!add_head)
2332 			break;
2333 	}
2334 
2335 	return retval;
2336 }
2337 
2338 /**
2339  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2340  * @vsi: ptr to the VSI
2341  * @vsi_name: name to display in messages
2342  * @list: the list of filters to send to firmware
2343  * @num_del: the number of filters to delete
2344  * @retval: Set to -EIO on failure to delete
2345  *
2346  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2347  * *retval instead of a return value so that success does not force ret_val to
2348  * be set to 0. This ensures that a sequence of calls to this function
2349  * preserve the previous value of *retval on successful delete.
2350  */
2351 static
2352 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2353 			  struct i40e_aqc_remove_macvlan_element_data *list,
2354 			  int num_del, int *retval)
2355 {
2356 	struct i40e_hw *hw = &vsi->back->hw;
2357 	enum i40e_admin_queue_err aq_status;
2358 	int aq_ret;
2359 
2360 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2361 					   &aq_status);
2362 
2363 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2364 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2365 		*retval = -EIO;
2366 		dev_info(&vsi->back->pdev->dev,
2367 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2368 			 vsi_name, ERR_PTR(aq_ret),
2369 			 i40e_aq_str(hw, aq_status));
2370 	}
2371 }
2372 
2373 /**
2374  * i40e_aqc_add_filters - Request firmware to add a set of filters
2375  * @vsi: ptr to the VSI
2376  * @vsi_name: name to display in messages
2377  * @list: the list of filters to send to firmware
2378  * @add_head: Position in the add hlist
2379  * @num_add: the number of filters to add
2380  *
2381  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2382  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2383  * space for more filters.
2384  */
2385 static
2386 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2387 			  struct i40e_aqc_add_macvlan_element_data *list,
2388 			  struct i40e_new_mac_filter *add_head,
2389 			  int num_add)
2390 {
2391 	struct i40e_hw *hw = &vsi->back->hw;
2392 	enum i40e_admin_queue_err aq_status;
2393 	int fcnt;
2394 
2395 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2396 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2397 
2398 	if (fcnt != num_add) {
2399 		if (vsi->type == I40E_VSI_MAIN) {
2400 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2401 			dev_warn(&vsi->back->pdev->dev,
2402 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2403 				 i40e_aq_str(hw, aq_status), vsi_name);
2404 		} else if (vsi->type == I40E_VSI_SRIOV ||
2405 			   vsi->type == I40E_VSI_VMDQ1 ||
2406 			   vsi->type == I40E_VSI_VMDQ2) {
2407 			dev_warn(&vsi->back->pdev->dev,
2408 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2409 				 i40e_aq_str(hw, aq_status), vsi_name,
2410 					     vsi_name);
2411 		} else {
2412 			dev_warn(&vsi->back->pdev->dev,
2413 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2414 				 i40e_aq_str(hw, aq_status), vsi_name,
2415 					     vsi->type);
2416 		}
2417 	}
2418 }
2419 
2420 /**
2421  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2422  * @vsi: pointer to the VSI
2423  * @vsi_name: the VSI name
2424  * @f: filter data
2425  *
2426  * This function sets or clears the promiscuous broadcast flags for VLAN
2427  * filters in order to properly receive broadcast frames. Assumes that only
2428  * broadcast filters are passed.
2429  *
2430  * Returns status indicating success or failure;
2431  **/
2432 static int
2433 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2434 			  struct i40e_mac_filter *f)
2435 {
2436 	bool enable = f->state == I40E_FILTER_NEW;
2437 	struct i40e_hw *hw = &vsi->back->hw;
2438 	int aq_ret;
2439 
2440 	if (f->vlan == I40E_VLAN_ANY) {
2441 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2442 						   vsi->seid,
2443 						   enable,
2444 						   NULL);
2445 	} else {
2446 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2447 							    vsi->seid,
2448 							    enable,
2449 							    f->vlan,
2450 							    NULL);
2451 	}
2452 
2453 	if (aq_ret) {
2454 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2455 		dev_warn(&vsi->back->pdev->dev,
2456 			 "Error %s, forcing overflow promiscuous on %s\n",
2457 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2458 			 vsi_name);
2459 	}
2460 
2461 	return aq_ret;
2462 }
2463 
2464 /**
2465  * i40e_set_promiscuous - set promiscuous mode
2466  * @pf: board private structure
2467  * @promisc: promisc on or off
2468  *
2469  * There are different ways of setting promiscuous mode on a PF depending on
2470  * what state/environment we're in.  This identifies and sets it appropriately.
2471  * Returns 0 on success.
2472  **/
2473 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2474 {
2475 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2476 	struct i40e_hw *hw = &pf->hw;
2477 	int aq_ret;
2478 
2479 	if (vsi->type == I40E_VSI_MAIN &&
2480 	    pf->lan_veb != I40E_NO_VEB &&
2481 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2482 		/* set defport ON for Main VSI instead of true promisc
2483 		 * this way we will get all unicast/multicast and VLAN
2484 		 * promisc behavior but will not get VF or VMDq traffic
2485 		 * replicated on the Main VSI.
2486 		 */
2487 		if (promisc)
2488 			aq_ret = i40e_aq_set_default_vsi(hw,
2489 							 vsi->seid,
2490 							 NULL);
2491 		else
2492 			aq_ret = i40e_aq_clear_default_vsi(hw,
2493 							   vsi->seid,
2494 							   NULL);
2495 		if (aq_ret) {
2496 			dev_info(&pf->pdev->dev,
2497 				 "Set default VSI failed, err %pe, aq_err %s\n",
2498 				 ERR_PTR(aq_ret),
2499 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2500 		}
2501 	} else {
2502 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2503 						  hw,
2504 						  vsi->seid,
2505 						  promisc, NULL,
2506 						  true);
2507 		if (aq_ret) {
2508 			dev_info(&pf->pdev->dev,
2509 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2510 				 ERR_PTR(aq_ret),
2511 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2512 		}
2513 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2514 						  hw,
2515 						  vsi->seid,
2516 						  promisc, NULL);
2517 		if (aq_ret) {
2518 			dev_info(&pf->pdev->dev,
2519 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2520 				 ERR_PTR(aq_ret),
2521 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2522 		}
2523 	}
2524 
2525 	if (!aq_ret)
2526 		pf->cur_promisc = promisc;
2527 
2528 	return aq_ret;
2529 }
2530 
2531 /**
2532  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2533  * @vsi: ptr to the VSI
2534  *
2535  * Push any outstanding VSI filter changes through the AdminQ.
2536  *
2537  * Returns 0 or error value
2538  **/
2539 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2540 {
2541 	struct hlist_head tmp_add_list, tmp_del_list;
2542 	struct i40e_mac_filter *f;
2543 	struct i40e_new_mac_filter *new, *add_head = NULL;
2544 	struct i40e_hw *hw = &vsi->back->hw;
2545 	bool old_overflow, new_overflow;
2546 	unsigned int failed_filters = 0;
2547 	unsigned int vlan_filters = 0;
2548 	char vsi_name[16] = "PF";
2549 	int filter_list_len = 0;
2550 	u32 changed_flags = 0;
2551 	struct hlist_node *h;
2552 	struct i40e_pf *pf;
2553 	int num_add = 0;
2554 	int num_del = 0;
2555 	int aq_ret = 0;
2556 	int retval = 0;
2557 	u16 cmd_flags;
2558 	int list_size;
2559 	int bkt;
2560 
2561 	/* empty array typed pointers, kcalloc later */
2562 	struct i40e_aqc_add_macvlan_element_data *add_list;
2563 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2564 
2565 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2566 		usleep_range(1000, 2000);
2567 	pf = vsi->back;
2568 
2569 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2570 
2571 	if (vsi->netdev) {
2572 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2573 		vsi->current_netdev_flags = vsi->netdev->flags;
2574 	}
2575 
2576 	INIT_HLIST_HEAD(&tmp_add_list);
2577 	INIT_HLIST_HEAD(&tmp_del_list);
2578 
2579 	if (vsi->type == I40E_VSI_SRIOV)
2580 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2581 	else if (vsi->type != I40E_VSI_MAIN)
2582 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2583 
2584 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2585 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2586 
2587 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2588 		/* Create a list of filters to delete. */
2589 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2590 			if (f->state == I40E_FILTER_REMOVE) {
2591 				/* Move the element into temporary del_list */
2592 				hash_del(&f->hlist);
2593 				hlist_add_head(&f->hlist, &tmp_del_list);
2594 
2595 				/* Avoid counting removed filters */
2596 				continue;
2597 			}
2598 			if (f->state == I40E_FILTER_NEW) {
2599 				/* Create a temporary i40e_new_mac_filter */
2600 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2601 				if (!new)
2602 					goto err_no_memory_locked;
2603 
2604 				/* Store pointer to the real filter */
2605 				new->f = f;
2606 				new->state = f->state;
2607 
2608 				/* Add it to the hash list */
2609 				hlist_add_head(&new->hlist, &tmp_add_list);
2610 			}
2611 
2612 			/* Count the number of active (current and new) VLAN
2613 			 * filters we have now. Does not count filters which
2614 			 * are marked for deletion.
2615 			 */
2616 			if (f->vlan > 0)
2617 				vlan_filters++;
2618 		}
2619 
2620 		if (vsi->type != I40E_VSI_SRIOV)
2621 			retval = i40e_correct_mac_vlan_filters
2622 				(vsi, &tmp_add_list, &tmp_del_list,
2623 				 vlan_filters);
2624 		else if (pf->vf)
2625 			retval = i40e_correct_vf_mac_vlan_filters
2626 				(vsi, &tmp_add_list, &tmp_del_list,
2627 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2628 
2629 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2630 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2631 
2632 		if (retval)
2633 			goto err_no_memory_locked;
2634 
2635 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2636 	}
2637 
2638 	/* Now process 'del_list' outside the lock */
2639 	if (!hlist_empty(&tmp_del_list)) {
2640 		filter_list_len = hw->aq.asq_buf_size /
2641 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2642 		list_size = filter_list_len *
2643 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2644 		del_list = kzalloc(list_size, GFP_ATOMIC);
2645 		if (!del_list)
2646 			goto err_no_memory;
2647 
2648 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2649 			cmd_flags = 0;
2650 
2651 			/* handle broadcast filters by updating the broadcast
2652 			 * promiscuous flag and release filter list.
2653 			 */
2654 			if (is_broadcast_ether_addr(f->macaddr)) {
2655 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2656 
2657 				hlist_del(&f->hlist);
2658 				kfree(f);
2659 				continue;
2660 			}
2661 
2662 			/* add to delete list */
2663 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2664 			if (f->vlan == I40E_VLAN_ANY) {
2665 				del_list[num_del].vlan_tag = 0;
2666 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2667 			} else {
2668 				del_list[num_del].vlan_tag =
2669 					cpu_to_le16((u16)(f->vlan));
2670 			}
2671 
2672 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2673 			del_list[num_del].flags = cmd_flags;
2674 			num_del++;
2675 
2676 			/* flush a full buffer */
2677 			if (num_del == filter_list_len) {
2678 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2679 						     num_del, &retval);
2680 				memset(del_list, 0, list_size);
2681 				num_del = 0;
2682 			}
2683 			/* Release memory for MAC filter entries which were
2684 			 * synced up with HW.
2685 			 */
2686 			hlist_del(&f->hlist);
2687 			kfree(f);
2688 		}
2689 
2690 		if (num_del) {
2691 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2692 					     num_del, &retval);
2693 		}
2694 
2695 		kfree(del_list);
2696 		del_list = NULL;
2697 	}
2698 
2699 	if (!hlist_empty(&tmp_add_list)) {
2700 		/* Do all the adds now. */
2701 		filter_list_len = hw->aq.asq_buf_size /
2702 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2703 		list_size = filter_list_len *
2704 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2705 		add_list = kzalloc(list_size, GFP_ATOMIC);
2706 		if (!add_list)
2707 			goto err_no_memory;
2708 
2709 		num_add = 0;
2710 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2711 			/* handle broadcast filters by updating the broadcast
2712 			 * promiscuous flag instead of adding a MAC filter.
2713 			 */
2714 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2715 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2716 							      new->f))
2717 					new->state = I40E_FILTER_FAILED;
2718 				else
2719 					new->state = I40E_FILTER_ACTIVE;
2720 				continue;
2721 			}
2722 
2723 			/* add to add array */
2724 			if (num_add == 0)
2725 				add_head = new;
2726 			cmd_flags = 0;
2727 			ether_addr_copy(add_list[num_add].mac_addr,
2728 					new->f->macaddr);
2729 			if (new->f->vlan == I40E_VLAN_ANY) {
2730 				add_list[num_add].vlan_tag = 0;
2731 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2732 			} else {
2733 				add_list[num_add].vlan_tag =
2734 					cpu_to_le16((u16)(new->f->vlan));
2735 			}
2736 			add_list[num_add].queue_number = 0;
2737 			/* set invalid match method for later detection */
2738 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2739 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2740 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2741 			num_add++;
2742 
2743 			/* flush a full buffer */
2744 			if (num_add == filter_list_len) {
2745 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2746 						     add_head, num_add);
2747 				memset(add_list, 0, list_size);
2748 				num_add = 0;
2749 			}
2750 		}
2751 		if (num_add) {
2752 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2753 					     num_add);
2754 		}
2755 		/* Now move all of the filters from the temp add list back to
2756 		 * the VSI's list.
2757 		 */
2758 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2759 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2760 			/* Only update the state if we're still NEW */
2761 			if (new->f->state == I40E_FILTER_NEW)
2762 				new->f->state = new->state;
2763 			hlist_del(&new->hlist);
2764 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2765 			kfree(new);
2766 		}
2767 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2768 		kfree(add_list);
2769 		add_list = NULL;
2770 	}
2771 
2772 	/* Determine the number of active and failed filters. */
2773 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2774 	vsi->active_filters = 0;
2775 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2776 		if (f->state == I40E_FILTER_ACTIVE)
2777 			vsi->active_filters++;
2778 		else if (f->state == I40E_FILTER_FAILED)
2779 			failed_filters++;
2780 	}
2781 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2782 
2783 	/* Check if we are able to exit overflow promiscuous mode. We can
2784 	 * safely exit if we didn't just enter, we no longer have any failed
2785 	 * filters, and we have reduced filters below the threshold value.
2786 	 */
2787 	if (old_overflow && !failed_filters &&
2788 	    vsi->active_filters < vsi->promisc_threshold) {
2789 		dev_info(&pf->pdev->dev,
2790 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2791 			 vsi_name);
2792 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2793 		vsi->promisc_threshold = 0;
2794 	}
2795 
2796 	/* if the VF is not trusted do not do promisc */
2797 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2798 	    !pf->vf[vsi->vf_id].trusted) {
2799 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2800 		goto out;
2801 	}
2802 
2803 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2804 
2805 	/* If we are entering overflow promiscuous, we need to calculate a new
2806 	 * threshold for when we are safe to exit
2807 	 */
2808 	if (!old_overflow && new_overflow)
2809 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2810 
2811 	/* check for changes in promiscuous modes */
2812 	if (changed_flags & IFF_ALLMULTI) {
2813 		bool cur_multipromisc;
2814 
2815 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2816 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2817 							       vsi->seid,
2818 							       cur_multipromisc,
2819 							       NULL);
2820 		if (aq_ret) {
2821 			retval = i40e_aq_rc_to_posix(aq_ret,
2822 						     hw->aq.asq_last_status);
2823 			dev_info(&pf->pdev->dev,
2824 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2825 				 vsi_name,
2826 				 ERR_PTR(aq_ret),
2827 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2828 		} else {
2829 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2830 				 cur_multipromisc ? "entering" : "leaving");
2831 		}
2832 	}
2833 
2834 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2835 		bool cur_promisc;
2836 
2837 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2838 			       new_overflow);
2839 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2840 		if (aq_ret) {
2841 			retval = i40e_aq_rc_to_posix(aq_ret,
2842 						     hw->aq.asq_last_status);
2843 			dev_info(&pf->pdev->dev,
2844 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2845 				 cur_promisc ? "on" : "off",
2846 				 vsi_name,
2847 				 ERR_PTR(aq_ret),
2848 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2849 		}
2850 	}
2851 out:
2852 	/* if something went wrong then set the changed flag so we try again */
2853 	if (retval)
2854 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2855 
2856 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2857 	return retval;
2858 
2859 err_no_memory:
2860 	/* Restore elements on the temporary add and delete lists */
2861 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2862 err_no_memory_locked:
2863 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2864 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2865 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2866 
2867 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2868 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2869 	return -ENOMEM;
2870 }
2871 
2872 /**
2873  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2874  * @pf: board private structure
2875  **/
2876 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2877 {
2878 	struct i40e_vsi *vsi;
2879 	int v;
2880 
2881 	if (!pf)
2882 		return;
2883 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2884 		return;
2885 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2886 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2887 		return;
2888 	}
2889 
2890 	i40e_pf_for_each_vsi(pf, v, vsi) {
2891 		if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2892 		    !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
2893 			int ret = i40e_sync_vsi_filters(vsi);
2894 
2895 			if (ret) {
2896 				/* come back and try again later */
2897 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2898 					pf->state);
2899 				break;
2900 			}
2901 		}
2902 	}
2903 }
2904 
2905 /**
2906  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2907  *
2908  * @vsi: VSI to calculate rx_buf_len from
2909  */
2910 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2911 {
2912 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2913 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2914 
2915 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2916 }
2917 
2918 /**
2919  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2920  * @vsi: the vsi
2921  * @xdp_prog: XDP program
2922  **/
2923 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2924 				   struct bpf_prog *xdp_prog)
2925 {
2926 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2927 	u16 chain_len;
2928 
2929 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2930 		chain_len = 1;
2931 	else
2932 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2933 
2934 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2935 }
2936 
2937 /**
2938  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2939  * @netdev: network interface device structure
2940  * @new_mtu: new value for maximum frame size
2941  *
2942  * Returns 0 on success, negative on failure
2943  **/
2944 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2945 {
2946 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2947 	struct i40e_vsi *vsi = np->vsi;
2948 	struct i40e_pf *pf = vsi->back;
2949 	int frame_size;
2950 
2951 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2952 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2953 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2954 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2955 		return -EINVAL;
2956 	}
2957 
2958 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2959 		   netdev->mtu, new_mtu);
2960 	netdev->mtu = new_mtu;
2961 	if (netif_running(netdev))
2962 		i40e_vsi_reinit_locked(vsi);
2963 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2964 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2965 	return 0;
2966 }
2967 
2968 /**
2969  * i40e_ioctl - Access the hwtstamp interface
2970  * @netdev: network interface device structure
2971  * @ifr: interface request data
2972  * @cmd: ioctl command
2973  **/
2974 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2975 {
2976 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2977 	struct i40e_pf *pf = np->vsi->back;
2978 
2979 	switch (cmd) {
2980 	case SIOCGHWTSTAMP:
2981 		return i40e_ptp_get_ts_config(pf, ifr);
2982 	case SIOCSHWTSTAMP:
2983 		return i40e_ptp_set_ts_config(pf, ifr);
2984 	default:
2985 		return -EOPNOTSUPP;
2986 	}
2987 }
2988 
2989 /**
2990  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2991  * @vsi: the vsi being adjusted
2992  **/
2993 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2994 {
2995 	struct i40e_vsi_context ctxt;
2996 	int ret;
2997 
2998 	/* Don't modify stripping options if a port VLAN is active */
2999 	if (vsi->info.pvid)
3000 		return;
3001 
3002 	if ((vsi->info.valid_sections &
3003 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3004 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3005 		return;  /* already enabled */
3006 
3007 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3008 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3009 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3010 
3011 	ctxt.seid = vsi->seid;
3012 	ctxt.info = vsi->info;
3013 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3014 	if (ret) {
3015 		dev_info(&vsi->back->pdev->dev,
3016 			 "update vlan stripping failed, err %pe aq_err %s\n",
3017 			 ERR_PTR(ret),
3018 			 i40e_aq_str(&vsi->back->hw,
3019 				     vsi->back->hw.aq.asq_last_status));
3020 	}
3021 }
3022 
3023 /**
3024  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3025  * @vsi: the vsi being adjusted
3026  **/
3027 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3028 {
3029 	struct i40e_vsi_context ctxt;
3030 	int ret;
3031 
3032 	/* Don't modify stripping options if a port VLAN is active */
3033 	if (vsi->info.pvid)
3034 		return;
3035 
3036 	if ((vsi->info.valid_sections &
3037 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3038 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3039 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3040 		return;  /* already disabled */
3041 
3042 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3043 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3044 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3045 
3046 	ctxt.seid = vsi->seid;
3047 	ctxt.info = vsi->info;
3048 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3049 	if (ret) {
3050 		dev_info(&vsi->back->pdev->dev,
3051 			 "update vlan stripping failed, err %pe aq_err %s\n",
3052 			 ERR_PTR(ret),
3053 			 i40e_aq_str(&vsi->back->hw,
3054 				     vsi->back->hw.aq.asq_last_status));
3055 	}
3056 }
3057 
3058 /**
3059  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3060  * @vsi: the vsi being configured
3061  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3062  *
3063  * This is a helper function for adding a new MAC/VLAN filter with the
3064  * specified VLAN for each existing MAC address already in the hash table.
3065  * This function does *not* perform any accounting to update filters based on
3066  * VLAN mode.
3067  *
3068  * NOTE: this function expects to be called while under the
3069  * mac_filter_hash_lock
3070  **/
3071 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3072 {
3073 	struct i40e_mac_filter *f, *add_f;
3074 	struct hlist_node *h;
3075 	int bkt;
3076 
3077 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3078 		/* If we're asked to add a filter that has been marked for
3079 		 * removal, it is safe to simply restore it to active state.
3080 		 * __i40e_del_filter will have simply deleted any filters which
3081 		 * were previously marked NEW or FAILED, so if it is currently
3082 		 * marked REMOVE it must have previously been ACTIVE. Since we
3083 		 * haven't yet run the sync filters task, just restore this
3084 		 * filter to the ACTIVE state so that the sync task leaves it
3085 		 * in place.
3086 		 */
3087 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3088 			f->state = I40E_FILTER_ACTIVE;
3089 			continue;
3090 		} else if (f->state == I40E_FILTER_REMOVE) {
3091 			continue;
3092 		}
3093 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3094 		if (!add_f) {
3095 			dev_info(&vsi->back->pdev->dev,
3096 				 "Could not add vlan filter %d for %pM\n",
3097 				 vid, f->macaddr);
3098 			return -ENOMEM;
3099 		}
3100 	}
3101 
3102 	return 0;
3103 }
3104 
3105 /**
3106  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3107  * @vsi: the VSI being configured
3108  * @vid: VLAN id to be added
3109  **/
3110 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3111 {
3112 	int err;
3113 
3114 	if (vsi->info.pvid)
3115 		return -EINVAL;
3116 
3117 	/* The network stack will attempt to add VID=0, with the intention to
3118 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3119 	 * these packets by default when configured to receive untagged
3120 	 * packets, so we don't need to add a filter for this case.
3121 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3122 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3123 	 * Thus, we do not want to actually add a filter for VID=0
3124 	 */
3125 	if (!vid)
3126 		return 0;
3127 
3128 	/* Locked once because all functions invoked below iterates list*/
3129 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3130 	err = i40e_add_vlan_all_mac(vsi, vid);
3131 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3132 	if (err)
3133 		return err;
3134 
3135 	/* schedule our worker thread which will take care of
3136 	 * applying the new filter changes
3137 	 */
3138 	i40e_service_event_schedule(vsi->back);
3139 	return 0;
3140 }
3141 
3142 /**
3143  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3144  * @vsi: the vsi being configured
3145  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3146  *
3147  * This function should be used to remove all VLAN filters which match the
3148  * given VID. It does not schedule the service event and does not take the
3149  * mac_filter_hash_lock so it may be combined with other operations under
3150  * a single invocation of the mac_filter_hash_lock.
3151  *
3152  * NOTE: this function expects to be called while under the
3153  * mac_filter_hash_lock
3154  */
3155 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3156 {
3157 	struct i40e_mac_filter *f;
3158 	struct hlist_node *h;
3159 	int bkt;
3160 
3161 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3162 		if (f->vlan == vid)
3163 			__i40e_del_filter(vsi, f);
3164 	}
3165 }
3166 
3167 /**
3168  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3169  * @vsi: the VSI being configured
3170  * @vid: VLAN id to be removed
3171  **/
3172 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3173 {
3174 	if (!vid || vsi->info.pvid)
3175 		return;
3176 
3177 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3178 	i40e_rm_vlan_all_mac(vsi, vid);
3179 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3180 
3181 	/* schedule our worker thread which will take care of
3182 	 * applying the new filter changes
3183 	 */
3184 	i40e_service_event_schedule(vsi->back);
3185 }
3186 
3187 /**
3188  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3189  * @netdev: network interface to be adjusted
3190  * @proto: unused protocol value
3191  * @vid: vlan id to be added
3192  *
3193  * net_device_ops implementation for adding vlan ids
3194  **/
3195 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3196 				__always_unused __be16 proto, u16 vid)
3197 {
3198 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3199 	struct i40e_vsi *vsi = np->vsi;
3200 	int ret = 0;
3201 
3202 	if (vid >= VLAN_N_VID)
3203 		return -EINVAL;
3204 
3205 	ret = i40e_vsi_add_vlan(vsi, vid);
3206 	if (!ret)
3207 		set_bit(vid, vsi->active_vlans);
3208 
3209 	return ret;
3210 }
3211 
3212 /**
3213  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3214  * @netdev: network interface to be adjusted
3215  * @proto: unused protocol value
3216  * @vid: vlan id to be added
3217  **/
3218 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3219 				    __always_unused __be16 proto, u16 vid)
3220 {
3221 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3222 	struct i40e_vsi *vsi = np->vsi;
3223 
3224 	if (vid >= VLAN_N_VID)
3225 		return;
3226 	set_bit(vid, vsi->active_vlans);
3227 }
3228 
3229 /**
3230  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3231  * @netdev: network interface to be adjusted
3232  * @proto: unused protocol value
3233  * @vid: vlan id to be removed
3234  *
3235  * net_device_ops implementation for removing vlan ids
3236  **/
3237 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3238 				 __always_unused __be16 proto, u16 vid)
3239 {
3240 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3241 	struct i40e_vsi *vsi = np->vsi;
3242 
3243 	/* return code is ignored as there is nothing a user
3244 	 * can do about failure to remove and a log message was
3245 	 * already printed from the other function
3246 	 */
3247 	i40e_vsi_kill_vlan(vsi, vid);
3248 
3249 	clear_bit(vid, vsi->active_vlans);
3250 
3251 	return 0;
3252 }
3253 
3254 /**
3255  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3256  * @vsi: the vsi being brought back up
3257  **/
3258 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3259 {
3260 	u16 vid;
3261 
3262 	if (!vsi->netdev)
3263 		return;
3264 
3265 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3266 		i40e_vlan_stripping_enable(vsi);
3267 	else
3268 		i40e_vlan_stripping_disable(vsi);
3269 
3270 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3271 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3272 					vid);
3273 }
3274 
3275 /**
3276  * i40e_vsi_add_pvid - Add pvid for the VSI
3277  * @vsi: the vsi being adjusted
3278  * @vid: the vlan id to set as a PVID
3279  **/
3280 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3281 {
3282 	struct i40e_vsi_context ctxt;
3283 	int ret;
3284 
3285 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3286 	vsi->info.pvid = cpu_to_le16(vid);
3287 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3288 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3289 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3290 
3291 	ctxt.seid = vsi->seid;
3292 	ctxt.info = vsi->info;
3293 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3294 	if (ret) {
3295 		dev_info(&vsi->back->pdev->dev,
3296 			 "add pvid failed, err %pe aq_err %s\n",
3297 			 ERR_PTR(ret),
3298 			 i40e_aq_str(&vsi->back->hw,
3299 				     vsi->back->hw.aq.asq_last_status));
3300 		return -ENOENT;
3301 	}
3302 
3303 	return 0;
3304 }
3305 
3306 /**
3307  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3308  * @vsi: the vsi being adjusted
3309  *
3310  * Just use the vlan_rx_register() service to put it back to normal
3311  **/
3312 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3313 {
3314 	vsi->info.pvid = 0;
3315 
3316 	i40e_vlan_stripping_disable(vsi);
3317 }
3318 
3319 /**
3320  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3321  * @vsi: ptr to the VSI
3322  *
3323  * If this function returns with an error, then it's possible one or
3324  * more of the rings is populated (while the rest are not).  It is the
3325  * callers duty to clean those orphaned rings.
3326  *
3327  * Return 0 on success, negative on failure
3328  **/
3329 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3330 {
3331 	int i, err = 0;
3332 
3333 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3334 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3335 
3336 	if (!i40e_enabled_xdp_vsi(vsi))
3337 		return err;
3338 
3339 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3340 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3341 
3342 	return err;
3343 }
3344 
3345 /**
3346  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3347  * @vsi: ptr to the VSI
3348  *
3349  * Free VSI's transmit software resources
3350  **/
3351 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3352 {
3353 	int i;
3354 
3355 	if (vsi->tx_rings) {
3356 		for (i = 0; i < vsi->num_queue_pairs; i++)
3357 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3358 				i40e_free_tx_resources(vsi->tx_rings[i]);
3359 	}
3360 
3361 	if (vsi->xdp_rings) {
3362 		for (i = 0; i < vsi->num_queue_pairs; i++)
3363 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3364 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3365 	}
3366 }
3367 
3368 /**
3369  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3370  * @vsi: ptr to the VSI
3371  *
3372  * If this function returns with an error, then it's possible one or
3373  * more of the rings is populated (while the rest are not).  It is the
3374  * callers duty to clean those orphaned rings.
3375  *
3376  * Return 0 on success, negative on failure
3377  **/
3378 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3379 {
3380 	int i, err = 0;
3381 
3382 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3383 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3384 	return err;
3385 }
3386 
3387 /**
3388  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3389  * @vsi: ptr to the VSI
3390  *
3391  * Free all receive software resources
3392  **/
3393 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3394 {
3395 	int i;
3396 
3397 	if (!vsi->rx_rings)
3398 		return;
3399 
3400 	for (i = 0; i < vsi->num_queue_pairs; i++)
3401 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3402 			i40e_free_rx_resources(vsi->rx_rings[i]);
3403 }
3404 
3405 /**
3406  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3407  * @ring: The Tx ring to configure
3408  *
3409  * This enables/disables XPS for a given Tx descriptor ring
3410  * based on the TCs enabled for the VSI that ring belongs to.
3411  **/
3412 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3413 {
3414 	int cpu;
3415 
3416 	if (!ring->q_vector || !ring->netdev || ring->ch)
3417 		return;
3418 
3419 	/* We only initialize XPS once, so as not to overwrite user settings */
3420 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3421 		return;
3422 
3423 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3424 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3425 			    ring->queue_index);
3426 }
3427 
3428 /**
3429  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3430  * @ring: The Tx or Rx ring
3431  *
3432  * Returns the AF_XDP buffer pool or NULL.
3433  **/
3434 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3435 {
3436 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3437 	int qid = ring->queue_index;
3438 
3439 	if (ring_is_xdp(ring))
3440 		qid -= ring->vsi->alloc_queue_pairs;
3441 
3442 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3443 		return NULL;
3444 
3445 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3446 }
3447 
3448 /**
3449  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3450  * @ring: The Tx ring to configure
3451  *
3452  * Configure the Tx descriptor ring in the HMC context.
3453  **/
3454 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3455 {
3456 	struct i40e_vsi *vsi = ring->vsi;
3457 	u16 pf_q = vsi->base_queue + ring->queue_index;
3458 	struct i40e_hw *hw = &vsi->back->hw;
3459 	struct i40e_hmc_obj_txq tx_ctx;
3460 	u32 qtx_ctl = 0;
3461 	int err = 0;
3462 
3463 	if (ring_is_xdp(ring))
3464 		ring->xsk_pool = i40e_xsk_pool(ring);
3465 
3466 	/* some ATR related tx ring init */
3467 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3468 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3469 		ring->atr_count = 0;
3470 	} else {
3471 		ring->atr_sample_rate = 0;
3472 	}
3473 
3474 	/* configure XPS */
3475 	i40e_config_xps_tx_ring(ring);
3476 
3477 	/* clear the context structure first */
3478 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3479 
3480 	tx_ctx.new_context = 1;
3481 	tx_ctx.base = (ring->dma / 128);
3482 	tx_ctx.qlen = ring->count;
3483 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3484 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3485 		tx_ctx.fd_ena = 1;
3486 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3487 		tx_ctx.timesync_ena = 1;
3488 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3489 	if (vsi->type != I40E_VSI_FDIR)
3490 		tx_ctx.head_wb_ena = 1;
3491 	tx_ctx.head_wb_addr = ring->dma +
3492 			      (ring->count * sizeof(struct i40e_tx_desc));
3493 
3494 	/* As part of VSI creation/update, FW allocates certain
3495 	 * Tx arbitration queue sets for each TC enabled for
3496 	 * the VSI. The FW returns the handles to these queue
3497 	 * sets as part of the response buffer to Add VSI,
3498 	 * Update VSI, etc. AQ commands. It is expected that
3499 	 * these queue set handles be associated with the Tx
3500 	 * queues by the driver as part of the TX queue context
3501 	 * initialization. This has to be done regardless of
3502 	 * DCB as by default everything is mapped to TC0.
3503 	 */
3504 
3505 	if (ring->ch)
3506 		tx_ctx.rdylist =
3507 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3508 
3509 	else
3510 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3511 
3512 	tx_ctx.rdylist_act = 0;
3513 
3514 	/* clear the context in the HMC */
3515 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3516 	if (err) {
3517 		dev_info(&vsi->back->pdev->dev,
3518 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3519 			 ring->queue_index, pf_q, err);
3520 		return -ENOMEM;
3521 	}
3522 
3523 	/* set the context in the HMC */
3524 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3525 	if (err) {
3526 		dev_info(&vsi->back->pdev->dev,
3527 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3528 			 ring->queue_index, pf_q, err);
3529 		return -ENOMEM;
3530 	}
3531 
3532 	/* Now associate this queue with this PCI function */
3533 	if (ring->ch) {
3534 		if (ring->ch->type == I40E_VSI_VMDQ2)
3535 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3536 		else
3537 			return -EINVAL;
3538 
3539 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3540 				      ring->ch->vsi_number);
3541 	} else {
3542 		if (vsi->type == I40E_VSI_VMDQ2) {
3543 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3544 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3545 					      vsi->id);
3546 		} else {
3547 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3548 		}
3549 	}
3550 
3551 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3552 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3553 	i40e_flush(hw);
3554 
3555 	/* cache tail off for easier writes later */
3556 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3557 
3558 	return 0;
3559 }
3560 
3561 /**
3562  * i40e_rx_offset - Return expected offset into page to access data
3563  * @rx_ring: Ring we are requesting offset of
3564  *
3565  * Returns the offset value for ring into the data buffer.
3566  */
3567 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3568 {
3569 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3570 }
3571 
3572 /**
3573  * i40e_configure_rx_ring - Configure a receive ring context
3574  * @ring: The Rx ring to configure
3575  *
3576  * Configure the Rx descriptor ring in the HMC context.
3577  **/
3578 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3579 {
3580 	struct i40e_vsi *vsi = ring->vsi;
3581 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3582 	u16 pf_q = vsi->base_queue + ring->queue_index;
3583 	struct i40e_hw *hw = &vsi->back->hw;
3584 	struct i40e_hmc_obj_rxq rx_ctx;
3585 	int err = 0;
3586 	bool ok;
3587 
3588 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3589 
3590 	/* clear the context structure first */
3591 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3592 
3593 	ring->rx_buf_len = vsi->rx_buf_len;
3594 
3595 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3596 	if (ring->vsi->type != I40E_VSI_MAIN)
3597 		goto skip;
3598 
3599 	if (!xdp_rxq_info_is_reg(&ring->xdp_rxq)) {
3600 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3601 					 ring->queue_index,
3602 					 ring->q_vector->napi.napi_id,
3603 					 ring->rx_buf_len);
3604 		if (err)
3605 			return err;
3606 	}
3607 
3608 	ring->xsk_pool = i40e_xsk_pool(ring);
3609 	if (ring->xsk_pool) {
3610 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3611 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3612 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3613 					 ring->queue_index,
3614 					 ring->q_vector->napi.napi_id,
3615 					 ring->rx_buf_len);
3616 		if (err)
3617 			return err;
3618 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3619 						 MEM_TYPE_XSK_BUFF_POOL,
3620 						 NULL);
3621 		if (err)
3622 			return err;
3623 		dev_info(&vsi->back->pdev->dev,
3624 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3625 			 ring->queue_index);
3626 
3627 	} else {
3628 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3629 						 MEM_TYPE_PAGE_SHARED,
3630 						 NULL);
3631 		if (err)
3632 			return err;
3633 	}
3634 
3635 skip:
3636 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3637 
3638 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3639 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3640 
3641 	rx_ctx.base = (ring->dma / 128);
3642 	rx_ctx.qlen = ring->count;
3643 
3644 	/* use 16 byte descriptors */
3645 	rx_ctx.dsize = 0;
3646 
3647 	/* descriptor type is always zero
3648 	 * rx_ctx.dtype = 0;
3649 	 */
3650 	rx_ctx.hsplit_0 = 0;
3651 
3652 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3653 	if (hw->revision_id == 0)
3654 		rx_ctx.lrxqthresh = 0;
3655 	else
3656 		rx_ctx.lrxqthresh = 1;
3657 	rx_ctx.crcstrip = 1;
3658 	rx_ctx.l2tsel = 1;
3659 	/* this controls whether VLAN is stripped from inner headers */
3660 	rx_ctx.showiv = 0;
3661 	/* set the prefena field to 1 because the manual says to */
3662 	rx_ctx.prefena = 1;
3663 
3664 	/* clear the context in the HMC */
3665 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3666 	if (err) {
3667 		dev_info(&vsi->back->pdev->dev,
3668 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3669 			 ring->queue_index, pf_q, err);
3670 		return -ENOMEM;
3671 	}
3672 
3673 	/* set the context in the HMC */
3674 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3675 	if (err) {
3676 		dev_info(&vsi->back->pdev->dev,
3677 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3678 			 ring->queue_index, pf_q, err);
3679 		return -ENOMEM;
3680 	}
3681 
3682 	/* configure Rx buffer alignment */
3683 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3684 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3685 			dev_info(&vsi->back->pdev->dev,
3686 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3687 			return -EOPNOTSUPP;
3688 		}
3689 		clear_ring_build_skb_enabled(ring);
3690 	} else {
3691 		set_ring_build_skb_enabled(ring);
3692 	}
3693 
3694 	ring->rx_offset = i40e_rx_offset(ring);
3695 
3696 	/* cache tail for quicker writes, and clear the reg before use */
3697 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3698 	writel(0, ring->tail);
3699 
3700 	if (ring->xsk_pool) {
3701 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3702 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3703 	} else {
3704 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3705 	}
3706 	if (!ok) {
3707 		/* Log this in case the user has forgotten to give the kernel
3708 		 * any buffers, even later in the application.
3709 		 */
3710 		dev_info(&vsi->back->pdev->dev,
3711 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3712 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3713 			 ring->queue_index, pf_q);
3714 	}
3715 
3716 	return 0;
3717 }
3718 
3719 /**
3720  * i40e_vsi_configure_tx - Configure the VSI for Tx
3721  * @vsi: VSI structure describing this set of rings and resources
3722  *
3723  * Configure the Tx VSI for operation.
3724  **/
3725 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3726 {
3727 	int err = 0;
3728 	u16 i;
3729 
3730 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3731 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3732 
3733 	if (err || !i40e_enabled_xdp_vsi(vsi))
3734 		return err;
3735 
3736 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3737 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3738 
3739 	return err;
3740 }
3741 
3742 /**
3743  * i40e_vsi_configure_rx - Configure the VSI for Rx
3744  * @vsi: the VSI being configured
3745  *
3746  * Configure the Rx VSI for operation.
3747  **/
3748 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3749 {
3750 	int err = 0;
3751 	u16 i;
3752 
3753 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3754 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3755 
3756 #if (PAGE_SIZE < 8192)
3757 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3758 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3759 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3760 		vsi->max_frame = vsi->rx_buf_len;
3761 	}
3762 #endif
3763 
3764 	/* set up individual rings */
3765 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3766 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3767 
3768 	return err;
3769 }
3770 
3771 /**
3772  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3773  * @vsi: ptr to the VSI
3774  **/
3775 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3776 {
3777 	struct i40e_ring *tx_ring, *rx_ring;
3778 	u16 qoffset, qcount;
3779 	int i, n;
3780 
3781 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3782 		/* Reset the TC information */
3783 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3784 			rx_ring = vsi->rx_rings[i];
3785 			tx_ring = vsi->tx_rings[i];
3786 			rx_ring->dcb_tc = 0;
3787 			tx_ring->dcb_tc = 0;
3788 		}
3789 		return;
3790 	}
3791 
3792 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3793 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3794 			continue;
3795 
3796 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3797 		qcount = vsi->tc_config.tc_info[n].qcount;
3798 		for (i = qoffset; i < (qoffset + qcount); i++) {
3799 			rx_ring = vsi->rx_rings[i];
3800 			tx_ring = vsi->tx_rings[i];
3801 			rx_ring->dcb_tc = n;
3802 			tx_ring->dcb_tc = n;
3803 		}
3804 	}
3805 }
3806 
3807 /**
3808  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3809  * @vsi: ptr to the VSI
3810  **/
3811 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3812 {
3813 	if (vsi->netdev)
3814 		i40e_set_rx_mode(vsi->netdev);
3815 }
3816 
3817 /**
3818  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3819  * @pf: Pointer to the targeted PF
3820  *
3821  * Set all flow director counters to 0.
3822  */
3823 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3824 {
3825 	pf->fd_tcp4_filter_cnt = 0;
3826 	pf->fd_udp4_filter_cnt = 0;
3827 	pf->fd_sctp4_filter_cnt = 0;
3828 	pf->fd_ip4_filter_cnt = 0;
3829 	pf->fd_tcp6_filter_cnt = 0;
3830 	pf->fd_udp6_filter_cnt = 0;
3831 	pf->fd_sctp6_filter_cnt = 0;
3832 	pf->fd_ip6_filter_cnt = 0;
3833 }
3834 
3835 /**
3836  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3837  * @vsi: Pointer to the targeted VSI
3838  *
3839  * This function replays the hlist on the hw where all the SB Flow Director
3840  * filters were saved.
3841  **/
3842 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3843 {
3844 	struct i40e_fdir_filter *filter;
3845 	struct i40e_pf *pf = vsi->back;
3846 	struct hlist_node *node;
3847 
3848 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3849 		return;
3850 
3851 	/* Reset FDir counters as we're replaying all existing filters */
3852 	i40e_reset_fdir_filter_cnt(pf);
3853 
3854 	hlist_for_each_entry_safe(filter, node,
3855 				  &pf->fdir_filter_list, fdir_node) {
3856 		i40e_add_del_fdir(vsi, filter, true);
3857 	}
3858 }
3859 
3860 /**
3861  * i40e_vsi_configure - Set up the VSI for action
3862  * @vsi: the VSI being configured
3863  **/
3864 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3865 {
3866 	int err;
3867 
3868 	i40e_set_vsi_rx_mode(vsi);
3869 	i40e_restore_vlan(vsi);
3870 	i40e_vsi_config_dcb_rings(vsi);
3871 	err = i40e_vsi_configure_tx(vsi);
3872 	if (!err)
3873 		err = i40e_vsi_configure_rx(vsi);
3874 
3875 	return err;
3876 }
3877 
3878 /**
3879  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3880  * @vsi: the VSI being configured
3881  **/
3882 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3883 {
3884 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3885 	struct i40e_pf *pf = vsi->back;
3886 	struct i40e_hw *hw = &pf->hw;
3887 	u16 vector;
3888 	int i, q;
3889 	u32 qp;
3890 
3891 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3892 	 * and PFINT_LNKLSTn registers, e.g.:
3893 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3894 	 */
3895 	qp = vsi->base_queue;
3896 	vector = vsi->base_vector;
3897 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3898 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3899 
3900 		q_vector->rx.next_update = jiffies + 1;
3901 		q_vector->rx.target_itr =
3902 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3903 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3904 		     q_vector->rx.target_itr >> 1);
3905 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3906 
3907 		q_vector->tx.next_update = jiffies + 1;
3908 		q_vector->tx.target_itr =
3909 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3910 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3911 		     q_vector->tx.target_itr >> 1);
3912 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3913 
3914 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3915 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3916 
3917 		/* begin of linked list for RX queue assigned to this vector */
3918 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3919 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3920 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3921 			u32 val;
3922 
3923 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3924 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3925 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3926 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3927 			      (I40E_QUEUE_TYPE_TX <<
3928 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3929 
3930 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3931 
3932 			if (has_xdp) {
3933 				/* TX queue with next queue set to TX */
3934 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3935 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3936 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3937 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3938 				      (I40E_QUEUE_TYPE_TX <<
3939 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3940 
3941 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3942 			}
3943 			/* TX queue with next RX or end of linked list */
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 + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3948 			      (I40E_QUEUE_TYPE_RX <<
3949 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3950 
3951 			/* Terminate the linked list */
3952 			if (q == (q_vector->num_ringpairs - 1))
3953 				val |= (I40E_QUEUE_END_OF_LIST <<
3954 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3955 
3956 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3957 			qp++;
3958 		}
3959 	}
3960 
3961 	i40e_flush(hw);
3962 }
3963 
3964 /**
3965  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3966  * @pf: pointer to private device data structure
3967  **/
3968 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3969 {
3970 	struct i40e_hw *hw = &pf->hw;
3971 	u32 val;
3972 
3973 	/* clear things first */
3974 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3975 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3976 
3977 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3978 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3979 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3980 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3981 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3982 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3983 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3984 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3985 
3986 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3987 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3988 
3989 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
3990 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3991 
3992 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3993 
3994 	/* SW_ITR_IDX = 0, but don't change INTENA */
3995 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3996 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3997 
3998 	/* OTHER_ITR_IDX = 0 */
3999 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
4000 }
4001 
4002 /**
4003  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
4004  * @vsi: the VSI being configured
4005  **/
4006 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
4007 {
4008 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
4009 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4010 	struct i40e_pf *pf = vsi->back;
4011 	struct i40e_hw *hw = &pf->hw;
4012 
4013 	/* set the ITR configuration */
4014 	q_vector->rx.next_update = jiffies + 1;
4015 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4016 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4017 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4018 	q_vector->tx.next_update = jiffies + 1;
4019 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4020 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4021 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4022 
4023 	i40e_enable_misc_int_causes(pf);
4024 
4025 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4026 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4027 
4028 	/* Associate the queue pair to the vector and enable the queue
4029 	 * interrupt RX queue in linked list with next queue set to TX
4030 	 */
4031 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4032 
4033 	if (i40e_enabled_xdp_vsi(vsi)) {
4034 		/* TX queue in linked list with next queue set to TX */
4035 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4036 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4037 	}
4038 
4039 	/* last TX queue so the next RX queue doesn't matter */
4040 	wr32(hw, I40E_QINT_TQCTL(0),
4041 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4042 	i40e_flush(hw);
4043 }
4044 
4045 /**
4046  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4047  * @pf: board private structure
4048  **/
4049 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4050 {
4051 	struct i40e_hw *hw = &pf->hw;
4052 
4053 	wr32(hw, I40E_PFINT_DYN_CTL0,
4054 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4055 	i40e_flush(hw);
4056 }
4057 
4058 /**
4059  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4060  * @pf: board private structure
4061  **/
4062 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4063 {
4064 	struct i40e_hw *hw = &pf->hw;
4065 	u32 val;
4066 
4067 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4068 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4069 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4070 
4071 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4072 	i40e_flush(hw);
4073 }
4074 
4075 /**
4076  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4077  * @irq: interrupt number
4078  * @data: pointer to a q_vector
4079  **/
4080 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4081 {
4082 	struct i40e_q_vector *q_vector = data;
4083 
4084 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4085 		return IRQ_HANDLED;
4086 
4087 	napi_schedule_irqoff(&q_vector->napi);
4088 
4089 	return IRQ_HANDLED;
4090 }
4091 
4092 /**
4093  * i40e_irq_affinity_notify - Callback for affinity changes
4094  * @notify: context as to what irq was changed
4095  * @mask: the new affinity mask
4096  *
4097  * This is a callback function used by the irq_set_affinity_notifier function
4098  * so that we may register to receive changes to the irq affinity masks.
4099  **/
4100 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4101 				     const cpumask_t *mask)
4102 {
4103 	struct i40e_q_vector *q_vector =
4104 		container_of(notify, struct i40e_q_vector, affinity_notify);
4105 
4106 	cpumask_copy(&q_vector->affinity_mask, mask);
4107 }
4108 
4109 /**
4110  * i40e_irq_affinity_release - Callback for affinity notifier release
4111  * @ref: internal core kernel usage
4112  *
4113  * This is a callback function used by the irq_set_affinity_notifier function
4114  * to inform the current notification subscriber that they will no longer
4115  * receive notifications.
4116  **/
4117 static void i40e_irq_affinity_release(struct kref *ref) {}
4118 
4119 /**
4120  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4121  * @vsi: the VSI being configured
4122  * @basename: name for the vector
4123  *
4124  * Allocates MSI-X vectors and requests interrupts from the kernel.
4125  **/
4126 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4127 {
4128 	int q_vectors = vsi->num_q_vectors;
4129 	struct i40e_pf *pf = vsi->back;
4130 	int base = vsi->base_vector;
4131 	int rx_int_idx = 0;
4132 	int tx_int_idx = 0;
4133 	int vector, err;
4134 	int irq_num;
4135 	int cpu;
4136 
4137 	for (vector = 0; vector < q_vectors; vector++) {
4138 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4139 
4140 		irq_num = pf->msix_entries[base + vector].vector;
4141 
4142 		if (q_vector->tx.ring && q_vector->rx.ring) {
4143 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4144 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4145 			tx_int_idx++;
4146 		} else if (q_vector->rx.ring) {
4147 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4148 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4149 		} else if (q_vector->tx.ring) {
4150 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4151 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4152 		} else {
4153 			/* skip this unused q_vector */
4154 			continue;
4155 		}
4156 		err = request_irq(irq_num,
4157 				  vsi->irq_handler,
4158 				  0,
4159 				  q_vector->name,
4160 				  q_vector);
4161 		if (err) {
4162 			dev_info(&pf->pdev->dev,
4163 				 "MSIX request_irq failed, error: %d\n", err);
4164 			goto free_queue_irqs;
4165 		}
4166 
4167 		/* register for affinity change notifications */
4168 		q_vector->irq_num = irq_num;
4169 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4170 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4171 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4172 		/* Spread affinity hints out across online CPUs.
4173 		 *
4174 		 * get_cpu_mask returns a static constant mask with
4175 		 * a permanent lifetime so it's ok to pass to
4176 		 * irq_update_affinity_hint without making a copy.
4177 		 */
4178 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4179 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4180 	}
4181 
4182 	vsi->irqs_ready = true;
4183 	return 0;
4184 
4185 free_queue_irqs:
4186 	while (vector) {
4187 		vector--;
4188 		irq_num = pf->msix_entries[base + vector].vector;
4189 		irq_set_affinity_notifier(irq_num, NULL);
4190 		irq_update_affinity_hint(irq_num, NULL);
4191 		free_irq(irq_num, &vsi->q_vectors[vector]);
4192 	}
4193 	return err;
4194 }
4195 
4196 /**
4197  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4198  * @vsi: the VSI being un-configured
4199  **/
4200 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4201 {
4202 	struct i40e_pf *pf = vsi->back;
4203 	struct i40e_hw *hw = &pf->hw;
4204 	int base = vsi->base_vector;
4205 	int i;
4206 
4207 	/* disable interrupt causation from each queue */
4208 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4209 		u32 val;
4210 
4211 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4212 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4213 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4214 
4215 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4216 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4217 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4218 
4219 		if (!i40e_enabled_xdp_vsi(vsi))
4220 			continue;
4221 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4222 	}
4223 
4224 	/* disable each interrupt */
4225 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4226 		for (i = vsi->base_vector;
4227 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4228 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4229 
4230 		i40e_flush(hw);
4231 		for (i = 0; i < vsi->num_q_vectors; i++)
4232 			synchronize_irq(pf->msix_entries[i + base].vector);
4233 	} else {
4234 		/* Legacy and MSI mode - this stops all interrupt handling */
4235 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4236 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4237 		i40e_flush(hw);
4238 		synchronize_irq(pf->pdev->irq);
4239 	}
4240 }
4241 
4242 /**
4243  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4244  * @vsi: the VSI being configured
4245  **/
4246 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4247 {
4248 	struct i40e_pf *pf = vsi->back;
4249 	int i;
4250 
4251 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4252 		for (i = 0; i < vsi->num_q_vectors; i++)
4253 			i40e_irq_dynamic_enable(vsi, i);
4254 	} else {
4255 		i40e_irq_dynamic_enable_icr0(pf);
4256 	}
4257 
4258 	i40e_flush(&pf->hw);
4259 	return 0;
4260 }
4261 
4262 /**
4263  * i40e_free_misc_vector - Free the vector that handles non-queue events
4264  * @pf: board private structure
4265  **/
4266 static void i40e_free_misc_vector(struct i40e_pf *pf)
4267 {
4268 	/* Disable ICR 0 */
4269 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4270 	i40e_flush(&pf->hw);
4271 
4272 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4273 		free_irq(pf->msix_entries[0].vector, pf);
4274 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4275 	}
4276 }
4277 
4278 /**
4279  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4280  * @irq: interrupt number
4281  * @data: pointer to a q_vector
4282  *
4283  * This is the handler used for all MSI/Legacy interrupts, and deals
4284  * with both queue and non-queue interrupts.  This is also used in
4285  * MSIX mode to handle the non-queue interrupts.
4286  **/
4287 static irqreturn_t i40e_intr(int irq, void *data)
4288 {
4289 	struct i40e_pf *pf = (struct i40e_pf *)data;
4290 	struct i40e_hw *hw = &pf->hw;
4291 	irqreturn_t ret = IRQ_NONE;
4292 	u32 icr0, icr0_remaining;
4293 	u32 val, ena_mask;
4294 
4295 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4296 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4297 
4298 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4299 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4300 		goto enable_intr;
4301 
4302 	/* if interrupt but no bits showing, must be SWINT */
4303 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4304 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4305 		pf->sw_int_count++;
4306 
4307 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4308 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4309 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4310 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4311 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4312 	}
4313 
4314 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4315 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4316 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4317 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4318 
4319 		/* We do not have a way to disarm Queue causes while leaving
4320 		 * interrupt enabled for all other causes, ideally
4321 		 * interrupt should be disabled while we are in NAPI but
4322 		 * this is not a performance path and napi_schedule()
4323 		 * can deal with rescheduling.
4324 		 */
4325 		if (!test_bit(__I40E_DOWN, pf->state))
4326 			napi_schedule_irqoff(&q_vector->napi);
4327 	}
4328 
4329 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4330 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4331 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4332 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4333 	}
4334 
4335 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4336 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4337 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4338 	}
4339 
4340 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4341 		/* disable any further VFLR event notifications */
4342 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4343 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4344 
4345 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4346 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4347 		} else {
4348 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4349 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4350 		}
4351 	}
4352 
4353 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4354 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4355 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4356 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4357 		val = rd32(hw, I40E_GLGEN_RSTAT);
4358 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4359 		if (val == I40E_RESET_CORER) {
4360 			pf->corer_count++;
4361 		} else if (val == I40E_RESET_GLOBR) {
4362 			pf->globr_count++;
4363 		} else if (val == I40E_RESET_EMPR) {
4364 			pf->empr_count++;
4365 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4366 		}
4367 	}
4368 
4369 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4370 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4371 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4372 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4373 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4374 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4375 	}
4376 
4377 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4378 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4379 
4380 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4381 			schedule_work(&pf->ptp_extts0_work);
4382 
4383 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4384 			i40e_ptp_tx_hwtstamp(pf);
4385 
4386 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4387 	}
4388 
4389 	/* If a critical error is pending we have no choice but to reset the
4390 	 * device.
4391 	 * Report and mask out any remaining unexpected interrupts.
4392 	 */
4393 	icr0_remaining = icr0 & ena_mask;
4394 	if (icr0_remaining) {
4395 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4396 			 icr0_remaining);
4397 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4398 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4399 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4400 			dev_info(&pf->pdev->dev, "device will be reset\n");
4401 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4402 			i40e_service_event_schedule(pf);
4403 		}
4404 		ena_mask &= ~icr0_remaining;
4405 	}
4406 	ret = IRQ_HANDLED;
4407 
4408 enable_intr:
4409 	/* re-enable interrupt causes */
4410 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4411 	if (!test_bit(__I40E_DOWN, pf->state) ||
4412 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4413 		i40e_service_event_schedule(pf);
4414 		i40e_irq_dynamic_enable_icr0(pf);
4415 	}
4416 
4417 	return ret;
4418 }
4419 
4420 /**
4421  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4422  * @tx_ring:  tx ring to clean
4423  * @budget:   how many cleans we're allowed
4424  *
4425  * Returns true if there's any budget left (e.g. the clean is finished)
4426  **/
4427 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4428 {
4429 	struct i40e_vsi *vsi = tx_ring->vsi;
4430 	u16 i = tx_ring->next_to_clean;
4431 	struct i40e_tx_buffer *tx_buf;
4432 	struct i40e_tx_desc *tx_desc;
4433 
4434 	tx_buf = &tx_ring->tx_bi[i];
4435 	tx_desc = I40E_TX_DESC(tx_ring, i);
4436 	i -= tx_ring->count;
4437 
4438 	do {
4439 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4440 
4441 		/* if next_to_watch is not set then there is no work pending */
4442 		if (!eop_desc)
4443 			break;
4444 
4445 		/* prevent any other reads prior to eop_desc */
4446 		smp_rmb();
4447 
4448 		/* if the descriptor isn't done, no work yet to do */
4449 		if (!(eop_desc->cmd_type_offset_bsz &
4450 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4451 			break;
4452 
4453 		/* clear next_to_watch to prevent false hangs */
4454 		tx_buf->next_to_watch = NULL;
4455 
4456 		tx_desc->buffer_addr = 0;
4457 		tx_desc->cmd_type_offset_bsz = 0;
4458 		/* move past filter desc */
4459 		tx_buf++;
4460 		tx_desc++;
4461 		i++;
4462 		if (unlikely(!i)) {
4463 			i -= tx_ring->count;
4464 			tx_buf = tx_ring->tx_bi;
4465 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4466 		}
4467 		/* unmap skb header data */
4468 		dma_unmap_single(tx_ring->dev,
4469 				 dma_unmap_addr(tx_buf, dma),
4470 				 dma_unmap_len(tx_buf, len),
4471 				 DMA_TO_DEVICE);
4472 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4473 			kfree(tx_buf->raw_buf);
4474 
4475 		tx_buf->raw_buf = NULL;
4476 		tx_buf->tx_flags = 0;
4477 		tx_buf->next_to_watch = NULL;
4478 		dma_unmap_len_set(tx_buf, len, 0);
4479 		tx_desc->buffer_addr = 0;
4480 		tx_desc->cmd_type_offset_bsz = 0;
4481 
4482 		/* move us past the eop_desc for start of next FD desc */
4483 		tx_buf++;
4484 		tx_desc++;
4485 		i++;
4486 		if (unlikely(!i)) {
4487 			i -= tx_ring->count;
4488 			tx_buf = tx_ring->tx_bi;
4489 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4490 		}
4491 
4492 		/* update budget accounting */
4493 		budget--;
4494 	} while (likely(budget));
4495 
4496 	i += tx_ring->count;
4497 	tx_ring->next_to_clean = i;
4498 
4499 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4500 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4501 
4502 	return budget > 0;
4503 }
4504 
4505 /**
4506  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4507  * @irq: interrupt number
4508  * @data: pointer to a q_vector
4509  **/
4510 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4511 {
4512 	struct i40e_q_vector *q_vector = data;
4513 	struct i40e_vsi *vsi;
4514 
4515 	if (!q_vector->tx.ring)
4516 		return IRQ_HANDLED;
4517 
4518 	vsi = q_vector->tx.ring->vsi;
4519 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4520 
4521 	return IRQ_HANDLED;
4522 }
4523 
4524 /**
4525  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4526  * @vsi: the VSI being configured
4527  * @v_idx: vector index
4528  * @qp_idx: queue pair index
4529  **/
4530 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4531 {
4532 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4533 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4534 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4535 
4536 	tx_ring->q_vector = q_vector;
4537 	tx_ring->next = q_vector->tx.ring;
4538 	q_vector->tx.ring = tx_ring;
4539 	q_vector->tx.count++;
4540 
4541 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4542 	if (i40e_enabled_xdp_vsi(vsi)) {
4543 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4544 
4545 		xdp_ring->q_vector = q_vector;
4546 		xdp_ring->next = q_vector->tx.ring;
4547 		q_vector->tx.ring = xdp_ring;
4548 		q_vector->tx.count++;
4549 	}
4550 
4551 	rx_ring->q_vector = q_vector;
4552 	rx_ring->next = q_vector->rx.ring;
4553 	q_vector->rx.ring = rx_ring;
4554 	q_vector->rx.count++;
4555 }
4556 
4557 /**
4558  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4559  * @vsi: the VSI being configured
4560  *
4561  * This function maps descriptor rings to the queue-specific vectors
4562  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4563  * one vector per queue pair, but on a constrained vector budget, we
4564  * group the queue pairs as "efficiently" as possible.
4565  **/
4566 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4567 {
4568 	int qp_remaining = vsi->num_queue_pairs;
4569 	int q_vectors = vsi->num_q_vectors;
4570 	int num_ringpairs;
4571 	int v_start = 0;
4572 	int qp_idx = 0;
4573 
4574 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4575 	 * group them so there are multiple queues per vector.
4576 	 * It is also important to go through all the vectors available to be
4577 	 * sure that if we don't use all the vectors, that the remaining vectors
4578 	 * are cleared. This is especially important when decreasing the
4579 	 * number of queues in use.
4580 	 */
4581 	for (; v_start < q_vectors; v_start++) {
4582 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4583 
4584 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4585 
4586 		q_vector->num_ringpairs = num_ringpairs;
4587 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4588 
4589 		q_vector->rx.count = 0;
4590 		q_vector->tx.count = 0;
4591 		q_vector->rx.ring = NULL;
4592 		q_vector->tx.ring = NULL;
4593 
4594 		while (num_ringpairs--) {
4595 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4596 			qp_idx++;
4597 			qp_remaining--;
4598 		}
4599 	}
4600 }
4601 
4602 /**
4603  * i40e_vsi_request_irq - Request IRQ from the OS
4604  * @vsi: the VSI being configured
4605  * @basename: name for the vector
4606  **/
4607 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4608 {
4609 	struct i40e_pf *pf = vsi->back;
4610 	int err;
4611 
4612 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4613 		err = i40e_vsi_request_irq_msix(vsi, basename);
4614 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4615 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4616 				  pf->int_name, pf);
4617 	else
4618 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4619 				  pf->int_name, pf);
4620 
4621 	if (err)
4622 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4623 
4624 	return err;
4625 }
4626 
4627 #ifdef CONFIG_NET_POLL_CONTROLLER
4628 /**
4629  * i40e_netpoll - A Polling 'interrupt' handler
4630  * @netdev: network interface device structure
4631  *
4632  * This is used by netconsole to send skbs without having to re-enable
4633  * interrupts.  It's not called while the normal interrupt routine is executing.
4634  **/
4635 static void i40e_netpoll(struct net_device *netdev)
4636 {
4637 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4638 	struct i40e_vsi *vsi = np->vsi;
4639 	struct i40e_pf *pf = vsi->back;
4640 	int i;
4641 
4642 	/* if interface is down do nothing */
4643 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4644 		return;
4645 
4646 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4647 		for (i = 0; i < vsi->num_q_vectors; i++)
4648 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4649 	} else {
4650 		i40e_intr(pf->pdev->irq, netdev);
4651 	}
4652 }
4653 #endif
4654 
4655 #define I40E_QTX_ENA_WAIT_COUNT 50
4656 
4657 /**
4658  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4659  * @pf: the PF being configured
4660  * @pf_q: the PF queue
4661  * @enable: enable or disable state of the queue
4662  *
4663  * This routine will wait for the given Tx queue of the PF to reach the
4664  * enabled or disabled state.
4665  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4666  * multiple retries; else will return 0 in case of success.
4667  **/
4668 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4669 {
4670 	int i;
4671 	u32 tx_reg;
4672 
4673 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4674 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4675 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4676 			break;
4677 
4678 		usleep_range(10, 20);
4679 	}
4680 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4681 		return -ETIMEDOUT;
4682 
4683 	return 0;
4684 }
4685 
4686 /**
4687  * i40e_control_tx_q - Start or stop a particular Tx queue
4688  * @pf: the PF structure
4689  * @pf_q: the PF queue to configure
4690  * @enable: start or stop the queue
4691  *
4692  * This function enables or disables a single queue. Note that any delay
4693  * required after the operation is expected to be handled by the caller of
4694  * this function.
4695  **/
4696 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4697 {
4698 	struct i40e_hw *hw = &pf->hw;
4699 	u32 tx_reg;
4700 	int i;
4701 
4702 	/* warn the TX unit of coming changes */
4703 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4704 	if (!enable)
4705 		usleep_range(10, 20);
4706 
4707 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4708 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4709 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4710 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4711 			break;
4712 		usleep_range(1000, 2000);
4713 	}
4714 
4715 	/* Skip if the queue is already in the requested state */
4716 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4717 		return;
4718 
4719 	/* turn on/off the queue */
4720 	if (enable) {
4721 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4722 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4723 	} else {
4724 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4725 	}
4726 
4727 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4728 }
4729 
4730 /**
4731  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4732  * @seid: VSI SEID
4733  * @pf: the PF structure
4734  * @pf_q: the PF queue to configure
4735  * @is_xdp: true if the queue is used for XDP
4736  * @enable: start or stop the queue
4737  **/
4738 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4739 			   bool is_xdp, bool enable)
4740 {
4741 	int ret;
4742 
4743 	i40e_control_tx_q(pf, pf_q, enable);
4744 
4745 	/* wait for the change to finish */
4746 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4747 	if (ret) {
4748 		dev_info(&pf->pdev->dev,
4749 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4750 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4751 			 (enable ? "en" : "dis"));
4752 	}
4753 
4754 	return ret;
4755 }
4756 
4757 /**
4758  * i40e_vsi_enable_tx - Start a VSI's rings
4759  * @vsi: the VSI being configured
4760  **/
4761 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4762 {
4763 	struct i40e_pf *pf = vsi->back;
4764 	int i, pf_q, ret = 0;
4765 
4766 	pf_q = vsi->base_queue;
4767 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4768 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4769 					     pf_q,
4770 					     false /*is xdp*/, true);
4771 		if (ret)
4772 			break;
4773 
4774 		if (!i40e_enabled_xdp_vsi(vsi))
4775 			continue;
4776 
4777 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4778 					     pf_q + vsi->alloc_queue_pairs,
4779 					     true /*is xdp*/, true);
4780 		if (ret)
4781 			break;
4782 	}
4783 	return ret;
4784 }
4785 
4786 /**
4787  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4788  * @pf: the PF being configured
4789  * @pf_q: the PF queue
4790  * @enable: enable or disable state of the queue
4791  *
4792  * This routine will wait for the given Rx queue of the PF to reach the
4793  * enabled or disabled state.
4794  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4795  * multiple retries; else will return 0 in case of success.
4796  **/
4797 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4798 {
4799 	int i;
4800 	u32 rx_reg;
4801 
4802 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4803 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4804 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4805 			break;
4806 
4807 		usleep_range(10, 20);
4808 	}
4809 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4810 		return -ETIMEDOUT;
4811 
4812 	return 0;
4813 }
4814 
4815 /**
4816  * i40e_control_rx_q - Start or stop a particular Rx queue
4817  * @pf: the PF structure
4818  * @pf_q: the PF queue to configure
4819  * @enable: start or stop the queue
4820  *
4821  * This function enables or disables a single queue. Note that
4822  * any delay required after the operation is expected to be
4823  * handled by the caller of this function.
4824  **/
4825 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4826 {
4827 	struct i40e_hw *hw = &pf->hw;
4828 	u32 rx_reg;
4829 	int i;
4830 
4831 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4832 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4833 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4834 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4835 			break;
4836 		usleep_range(1000, 2000);
4837 	}
4838 
4839 	/* Skip if the queue is already in the requested state */
4840 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4841 		return;
4842 
4843 	/* turn on/off the queue */
4844 	if (enable)
4845 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4846 	else
4847 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4848 
4849 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4850 }
4851 
4852 /**
4853  * i40e_control_wait_rx_q
4854  * @pf: the PF structure
4855  * @pf_q: queue being configured
4856  * @enable: start or stop the rings
4857  *
4858  * This function enables or disables a single queue along with waiting
4859  * for the change to finish. The caller of this function should handle
4860  * the delays needed in the case of disabling queues.
4861  **/
4862 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4863 {
4864 	int ret = 0;
4865 
4866 	i40e_control_rx_q(pf, pf_q, enable);
4867 
4868 	/* wait for the change to finish */
4869 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4870 	if (ret)
4871 		return ret;
4872 
4873 	return ret;
4874 }
4875 
4876 /**
4877  * i40e_vsi_enable_rx - Start a VSI's rings
4878  * @vsi: the VSI being configured
4879  **/
4880 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4881 {
4882 	struct i40e_pf *pf = vsi->back;
4883 	int i, pf_q, ret = 0;
4884 
4885 	pf_q = vsi->base_queue;
4886 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4887 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4888 		if (ret) {
4889 			dev_info(&pf->pdev->dev,
4890 				 "VSI seid %d Rx ring %d enable timeout\n",
4891 				 vsi->seid, pf_q);
4892 			break;
4893 		}
4894 	}
4895 
4896 	return ret;
4897 }
4898 
4899 /**
4900  * i40e_vsi_start_rings - Start a VSI's rings
4901  * @vsi: the VSI being configured
4902  **/
4903 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4904 {
4905 	int ret = 0;
4906 
4907 	/* do rx first for enable and last for disable */
4908 	ret = i40e_vsi_enable_rx(vsi);
4909 	if (ret)
4910 		return ret;
4911 	ret = i40e_vsi_enable_tx(vsi);
4912 
4913 	return ret;
4914 }
4915 
4916 #define I40E_DISABLE_TX_GAP_MSEC	50
4917 
4918 /**
4919  * i40e_vsi_stop_rings - Stop a VSI's rings
4920  * @vsi: the VSI being configured
4921  **/
4922 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4923 {
4924 	struct i40e_pf *pf = vsi->back;
4925 	u32 pf_q, tx_q_end, rx_q_end;
4926 
4927 	/* When port TX is suspended, don't wait */
4928 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4929 		return i40e_vsi_stop_rings_no_wait(vsi);
4930 
4931 	tx_q_end = vsi->base_queue +
4932 		vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
4933 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4934 		i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
4935 
4936 	rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
4937 	for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
4938 		i40e_control_rx_q(pf, pf_q, false);
4939 
4940 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4941 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4942 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4943 
4944 	i40e_vsi_wait_queues_disabled(vsi);
4945 }
4946 
4947 /**
4948  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4949  * @vsi: the VSI being shutdown
4950  *
4951  * This function stops all the rings for a VSI but does not delay to verify
4952  * that rings have been disabled. It is expected that the caller is shutting
4953  * down multiple VSIs at once and will delay together for all the VSIs after
4954  * initiating the shutdown. This is particularly useful for shutting down lots
4955  * of VFs together. Otherwise, a large delay can be incurred while configuring
4956  * each VSI in serial.
4957  **/
4958 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4959 {
4960 	struct i40e_pf *pf = vsi->back;
4961 	int i, pf_q;
4962 
4963 	pf_q = vsi->base_queue;
4964 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4965 		i40e_control_tx_q(pf, pf_q, false);
4966 		i40e_control_rx_q(pf, pf_q, false);
4967 	}
4968 }
4969 
4970 /**
4971  * i40e_vsi_free_irq - Free the irq association with the OS
4972  * @vsi: the VSI being configured
4973  **/
4974 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4975 {
4976 	struct i40e_pf *pf = vsi->back;
4977 	struct i40e_hw *hw = &pf->hw;
4978 	int base = vsi->base_vector;
4979 	u32 val, qp;
4980 	int i;
4981 
4982 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4983 		if (!vsi->q_vectors)
4984 			return;
4985 
4986 		if (!vsi->irqs_ready)
4987 			return;
4988 
4989 		vsi->irqs_ready = false;
4990 		for (i = 0; i < vsi->num_q_vectors; i++) {
4991 			int irq_num;
4992 			u16 vector;
4993 
4994 			vector = i + base;
4995 			irq_num = pf->msix_entries[vector].vector;
4996 
4997 			/* free only the irqs that were actually requested */
4998 			if (!vsi->q_vectors[i] ||
4999 			    !vsi->q_vectors[i]->num_ringpairs)
5000 				continue;
5001 
5002 			/* clear the affinity notifier in the IRQ descriptor */
5003 			irq_set_affinity_notifier(irq_num, NULL);
5004 			/* remove our suggested affinity mask for this IRQ */
5005 			irq_update_affinity_hint(irq_num, NULL);
5006 			free_irq(irq_num, vsi->q_vectors[i]);
5007 
5008 			/* Tear down the interrupt queue link list
5009 			 *
5010 			 * We know that they come in pairs and always
5011 			 * the Rx first, then the Tx.  To clear the
5012 			 * link list, stick the EOL value into the
5013 			 * next_q field of the registers.
5014 			 */
5015 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5016 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
5017 				       val);
5018 			val |= I40E_QUEUE_END_OF_LIST
5019 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5020 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5021 
5022 			while (qp != I40E_QUEUE_END_OF_LIST) {
5023 				u32 next;
5024 
5025 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5026 
5027 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5028 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5029 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5030 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5031 
5032 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5033 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5034 
5035 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5036 
5037 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5038 
5039 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5040 						 val);
5041 
5042 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5043 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5044 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5045 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5046 
5047 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5048 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5049 
5050 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5051 				qp = next;
5052 			}
5053 		}
5054 	} else {
5055 		free_irq(pf->pdev->irq, pf);
5056 
5057 		val = rd32(hw, I40E_PFINT_LNKLST0);
5058 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5059 		val |= I40E_QUEUE_END_OF_LIST
5060 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5061 		wr32(hw, I40E_PFINT_LNKLST0, val);
5062 
5063 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5064 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5065 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5066 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5067 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5068 
5069 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5070 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5071 
5072 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5073 
5074 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5075 
5076 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5077 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5078 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5079 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5080 
5081 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5082 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5083 
5084 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5085 	}
5086 }
5087 
5088 /**
5089  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5090  * @vsi: the VSI being configured
5091  * @v_idx: Index of vector to be freed
5092  *
5093  * This function frees the memory allocated to the q_vector.  In addition if
5094  * NAPI is enabled it will delete any references to the NAPI struct prior
5095  * to freeing the q_vector.
5096  **/
5097 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5098 {
5099 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5100 	struct i40e_ring *ring;
5101 
5102 	if (!q_vector)
5103 		return;
5104 
5105 	/* disassociate q_vector from rings */
5106 	i40e_for_each_ring(ring, q_vector->tx)
5107 		ring->q_vector = NULL;
5108 
5109 	i40e_for_each_ring(ring, q_vector->rx)
5110 		ring->q_vector = NULL;
5111 
5112 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5113 	if (vsi->netdev)
5114 		netif_napi_del(&q_vector->napi);
5115 
5116 	vsi->q_vectors[v_idx] = NULL;
5117 
5118 	kfree_rcu(q_vector, rcu);
5119 }
5120 
5121 /**
5122  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5123  * @vsi: the VSI being un-configured
5124  *
5125  * This frees the memory allocated to the q_vectors and
5126  * deletes references to the NAPI struct.
5127  **/
5128 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5129 {
5130 	int v_idx;
5131 
5132 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5133 		i40e_free_q_vector(vsi, v_idx);
5134 }
5135 
5136 /**
5137  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5138  * @pf: board private structure
5139  **/
5140 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5141 {
5142 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5143 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5144 		pci_disable_msix(pf->pdev);
5145 		kfree(pf->msix_entries);
5146 		pf->msix_entries = NULL;
5147 		kfree(pf->irq_pile);
5148 		pf->irq_pile = NULL;
5149 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5150 		pci_disable_msi(pf->pdev);
5151 	}
5152 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5153 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5154 }
5155 
5156 /**
5157  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5158  * @pf: board private structure
5159  *
5160  * We go through and clear interrupt specific resources and reset the structure
5161  * to pre-load conditions
5162  **/
5163 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5164 {
5165 	struct i40e_vsi *vsi;
5166 	int i;
5167 
5168 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5169 		i40e_free_misc_vector(pf);
5170 
5171 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5172 		      I40E_IWARP_IRQ_PILE_ID);
5173 
5174 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5175 
5176 	i40e_pf_for_each_vsi(pf, i, vsi)
5177 		i40e_vsi_free_q_vectors(vsi);
5178 
5179 	i40e_reset_interrupt_capability(pf);
5180 }
5181 
5182 /**
5183  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5184  * @vsi: the VSI being configured
5185  **/
5186 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5187 {
5188 	int q_idx;
5189 
5190 	if (!vsi->netdev)
5191 		return;
5192 
5193 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5194 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5195 
5196 		if (q_vector->rx.ring || q_vector->tx.ring)
5197 			napi_enable(&q_vector->napi);
5198 	}
5199 }
5200 
5201 /**
5202  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5203  * @vsi: the VSI being configured
5204  **/
5205 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5206 {
5207 	int q_idx;
5208 
5209 	if (!vsi->netdev)
5210 		return;
5211 
5212 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5213 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5214 
5215 		if (q_vector->rx.ring || q_vector->tx.ring)
5216 			napi_disable(&q_vector->napi);
5217 	}
5218 }
5219 
5220 /**
5221  * i40e_vsi_close - Shut down a VSI
5222  * @vsi: the vsi to be quelled
5223  **/
5224 static void i40e_vsi_close(struct i40e_vsi *vsi)
5225 {
5226 	struct i40e_pf *pf = vsi->back;
5227 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5228 		i40e_down(vsi);
5229 	i40e_vsi_free_irq(vsi);
5230 	i40e_vsi_free_tx_resources(vsi);
5231 	i40e_vsi_free_rx_resources(vsi);
5232 	vsi->current_netdev_flags = 0;
5233 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5234 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5235 		set_bit(__I40E_CLIENT_RESET, pf->state);
5236 }
5237 
5238 /**
5239  * i40e_quiesce_vsi - Pause a given VSI
5240  * @vsi: the VSI being paused
5241  **/
5242 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5243 {
5244 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5245 		return;
5246 
5247 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5248 	if (vsi->netdev && netif_running(vsi->netdev))
5249 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5250 	else
5251 		i40e_vsi_close(vsi);
5252 }
5253 
5254 /**
5255  * i40e_unquiesce_vsi - Resume a given VSI
5256  * @vsi: the VSI being resumed
5257  **/
5258 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5259 {
5260 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5261 		return;
5262 
5263 	if (vsi->netdev && netif_running(vsi->netdev))
5264 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5265 	else
5266 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5267 }
5268 
5269 /**
5270  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5271  * @pf: the PF
5272  **/
5273 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5274 {
5275 	struct i40e_vsi *vsi;
5276 	int v;
5277 
5278 	i40e_pf_for_each_vsi(pf, v, vsi)
5279 		i40e_quiesce_vsi(vsi);
5280 }
5281 
5282 /**
5283  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5284  * @pf: the PF
5285  **/
5286 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5287 {
5288 	struct i40e_vsi *vsi;
5289 	int v;
5290 
5291 	i40e_pf_for_each_vsi(pf, v, vsi)
5292 		i40e_unquiesce_vsi(vsi);
5293 }
5294 
5295 /**
5296  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5297  * @vsi: the VSI being configured
5298  *
5299  * Wait until all queues on a given VSI have been disabled.
5300  **/
5301 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5302 {
5303 	struct i40e_pf *pf = vsi->back;
5304 	int i, pf_q, ret;
5305 
5306 	pf_q = vsi->base_queue;
5307 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5308 		/* Check and wait for the Tx queue */
5309 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5310 		if (ret) {
5311 			dev_info(&pf->pdev->dev,
5312 				 "VSI seid %d Tx ring %d disable timeout\n",
5313 				 vsi->seid, pf_q);
5314 			return ret;
5315 		}
5316 
5317 		if (!i40e_enabled_xdp_vsi(vsi))
5318 			goto wait_rx;
5319 
5320 		/* Check and wait for the XDP Tx queue */
5321 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5322 				       false);
5323 		if (ret) {
5324 			dev_info(&pf->pdev->dev,
5325 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5326 				 vsi->seid, pf_q);
5327 			return ret;
5328 		}
5329 wait_rx:
5330 		/* Check and wait for the Rx queue */
5331 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5332 		if (ret) {
5333 			dev_info(&pf->pdev->dev,
5334 				 "VSI seid %d Rx ring %d disable timeout\n",
5335 				 vsi->seid, pf_q);
5336 			return ret;
5337 		}
5338 	}
5339 
5340 	return 0;
5341 }
5342 
5343 #ifdef CONFIG_I40E_DCB
5344 /**
5345  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5346  * @pf: the PF
5347  *
5348  * This function waits for the queues to be in disabled state for all the
5349  * VSIs that are managed by this PF.
5350  **/
5351 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5352 {
5353 	struct i40e_vsi *vsi;
5354 	int v, ret = 0;
5355 
5356 	i40e_pf_for_each_vsi(pf, v, vsi) {
5357 		ret = i40e_vsi_wait_queues_disabled(vsi);
5358 		if (ret)
5359 			break;
5360 	}
5361 
5362 	return ret;
5363 }
5364 
5365 #endif
5366 
5367 /**
5368  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5369  * @pf: pointer to PF
5370  *
5371  * Get TC map for ISCSI PF type that will include iSCSI TC
5372  * and LAN TC.
5373  **/
5374 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5375 {
5376 	struct i40e_dcb_app_priority_table app;
5377 	struct i40e_hw *hw = &pf->hw;
5378 	u8 enabled_tc = 1; /* TC0 is always enabled */
5379 	u8 tc, i;
5380 	/* Get the iSCSI APP TLV */
5381 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5382 
5383 	for (i = 0; i < dcbcfg->numapps; i++) {
5384 		app = dcbcfg->app[i];
5385 		if (app.selector == I40E_APP_SEL_TCPIP &&
5386 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5387 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5388 			enabled_tc |= BIT(tc);
5389 			break;
5390 		}
5391 	}
5392 
5393 	return enabled_tc;
5394 }
5395 
5396 /**
5397  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5398  * @dcbcfg: the corresponding DCBx configuration structure
5399  *
5400  * Return the number of TCs from given DCBx configuration
5401  **/
5402 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5403 {
5404 	int i, tc_unused = 0;
5405 	u8 num_tc = 0;
5406 	u8 ret = 0;
5407 
5408 	/* Scan the ETS Config Priority Table to find
5409 	 * traffic class enabled for a given priority
5410 	 * and create a bitmask of enabled TCs
5411 	 */
5412 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5413 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5414 
5415 	/* Now scan the bitmask to check for
5416 	 * contiguous TCs starting with TC0
5417 	 */
5418 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5419 		if (num_tc & BIT(i)) {
5420 			if (!tc_unused) {
5421 				ret++;
5422 			} else {
5423 				pr_err("Non-contiguous TC - Disabling DCB\n");
5424 				return 1;
5425 			}
5426 		} else {
5427 			tc_unused = 1;
5428 		}
5429 	}
5430 
5431 	/* There is always at least TC0 */
5432 	if (!ret)
5433 		ret = 1;
5434 
5435 	return ret;
5436 }
5437 
5438 /**
5439  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5440  * @dcbcfg: the corresponding DCBx configuration structure
5441  *
5442  * Query the current DCB configuration and return the number of
5443  * traffic classes enabled from the given DCBX config
5444  **/
5445 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5446 {
5447 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5448 	u8 enabled_tc = 1;
5449 	u8 i;
5450 
5451 	for (i = 0; i < num_tc; i++)
5452 		enabled_tc |= BIT(i);
5453 
5454 	return enabled_tc;
5455 }
5456 
5457 /**
5458  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5459  * @pf: PF being queried
5460  *
5461  * Query the current MQPRIO configuration and return the number of
5462  * traffic classes enabled.
5463  **/
5464 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5465 {
5466 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5467 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5468 	u8 enabled_tc = 1, i;
5469 
5470 	for (i = 1; i < num_tc; i++)
5471 		enabled_tc |= BIT(i);
5472 	return enabled_tc;
5473 }
5474 
5475 /**
5476  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5477  * @pf: PF being queried
5478  *
5479  * Return number of traffic classes enabled for the given PF
5480  **/
5481 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5482 {
5483 	struct i40e_hw *hw = &pf->hw;
5484 	u8 i, enabled_tc = 1;
5485 	u8 num_tc = 0;
5486 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5487 
5488 	if (i40e_is_tc_mqprio_enabled(pf))
5489 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5490 
5491 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5492 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5493 		return 1;
5494 
5495 	/* SFP mode will be enabled for all TCs on port */
5496 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5497 		return i40e_dcb_get_num_tc(dcbcfg);
5498 
5499 	/* MFP mode return count of enabled TCs for this PF */
5500 	if (pf->hw.func_caps.iscsi)
5501 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5502 	else
5503 		return 1; /* Only TC0 */
5504 
5505 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5506 		if (enabled_tc & BIT(i))
5507 			num_tc++;
5508 	}
5509 	return num_tc;
5510 }
5511 
5512 /**
5513  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5514  * @pf: PF being queried
5515  *
5516  * Return a bitmap for enabled traffic classes for this PF.
5517  **/
5518 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5519 {
5520 	if (i40e_is_tc_mqprio_enabled(pf))
5521 		return i40e_mqprio_get_enabled_tc(pf);
5522 
5523 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5524 	 * default TC
5525 	 */
5526 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5527 		return I40E_DEFAULT_TRAFFIC_CLASS;
5528 
5529 	/* SFP mode we want PF to be enabled for all TCs */
5530 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5531 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5532 
5533 	/* MFP enabled and iSCSI PF type */
5534 	if (pf->hw.func_caps.iscsi)
5535 		return i40e_get_iscsi_tc_map(pf);
5536 	else
5537 		return I40E_DEFAULT_TRAFFIC_CLASS;
5538 }
5539 
5540 /**
5541  * i40e_vsi_get_bw_info - Query VSI BW Information
5542  * @vsi: the VSI being queried
5543  *
5544  * Returns 0 on success, negative value on failure
5545  **/
5546 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5547 {
5548 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5549 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5550 	struct i40e_pf *pf = vsi->back;
5551 	struct i40e_hw *hw = &pf->hw;
5552 	u32 tc_bw_max;
5553 	int ret;
5554 	int i;
5555 
5556 	/* Get the VSI level BW configuration */
5557 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5558 	if (ret) {
5559 		dev_info(&pf->pdev->dev,
5560 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5561 			 ERR_PTR(ret),
5562 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5563 		return -EINVAL;
5564 	}
5565 
5566 	/* Get the VSI level BW configuration per TC */
5567 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5568 					       NULL);
5569 	if (ret) {
5570 		dev_info(&pf->pdev->dev,
5571 			 "couldn't get PF vsi ets 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 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5578 		dev_info(&pf->pdev->dev,
5579 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5580 			 bw_config.tc_valid_bits,
5581 			 bw_ets_config.tc_valid_bits);
5582 		/* Still continuing */
5583 	}
5584 
5585 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5586 	vsi->bw_max_quanta = bw_config.max_bw;
5587 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5588 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5589 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5590 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5591 		vsi->bw_ets_limit_credits[i] =
5592 					le16_to_cpu(bw_ets_config.credits[i]);
5593 		/* 3 bits out of 4 for each TC */
5594 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5595 	}
5596 
5597 	return 0;
5598 }
5599 
5600 /**
5601  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5602  * @vsi: the VSI being configured
5603  * @enabled_tc: TC bitmap
5604  * @bw_share: BW shared credits per TC
5605  *
5606  * Returns 0 on success, negative value on failure
5607  **/
5608 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5609 				       u8 *bw_share)
5610 {
5611 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5612 	struct i40e_pf *pf = vsi->back;
5613 	int ret;
5614 	int i;
5615 
5616 	/* There is no need to reset BW when mqprio mode is on.  */
5617 	if (i40e_is_tc_mqprio_enabled(pf))
5618 		return 0;
5619 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5620 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5621 		if (ret)
5622 			dev_info(&pf->pdev->dev,
5623 				 "Failed to reset tx rate for vsi->seid %u\n",
5624 				 vsi->seid);
5625 		return ret;
5626 	}
5627 	memset(&bw_data, 0, sizeof(bw_data));
5628 	bw_data.tc_valid_bits = enabled_tc;
5629 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5630 		bw_data.tc_bw_credits[i] = bw_share[i];
5631 
5632 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5633 	if (ret) {
5634 		dev_info(&pf->pdev->dev,
5635 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5636 			 pf->hw.aq.asq_last_status);
5637 		return -EINVAL;
5638 	}
5639 
5640 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5641 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5642 
5643 	return 0;
5644 }
5645 
5646 /**
5647  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5648  * @vsi: the VSI being configured
5649  * @enabled_tc: TC map to be enabled
5650  *
5651  **/
5652 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5653 {
5654 	struct net_device *netdev = vsi->netdev;
5655 	struct i40e_pf *pf = vsi->back;
5656 	struct i40e_hw *hw = &pf->hw;
5657 	u8 netdev_tc = 0;
5658 	int i;
5659 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5660 
5661 	if (!netdev)
5662 		return;
5663 
5664 	if (!enabled_tc) {
5665 		netdev_reset_tc(netdev);
5666 		return;
5667 	}
5668 
5669 	/* Set up actual enabled TCs on the VSI */
5670 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5671 		return;
5672 
5673 	/* set per TC queues for the VSI */
5674 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5675 		/* Only set TC queues for enabled tcs
5676 		 *
5677 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5678 		 * enabled_tc bitmap would be 0x00001001; the driver
5679 		 * will set the numtc for netdev as 2 that will be
5680 		 * referenced by the netdev layer as TC 0 and 1.
5681 		 */
5682 		if (vsi->tc_config.enabled_tc & BIT(i))
5683 			netdev_set_tc_queue(netdev,
5684 					vsi->tc_config.tc_info[i].netdev_tc,
5685 					vsi->tc_config.tc_info[i].qcount,
5686 					vsi->tc_config.tc_info[i].qoffset);
5687 	}
5688 
5689 	if (i40e_is_tc_mqprio_enabled(pf))
5690 		return;
5691 
5692 	/* Assign UP2TC map for the VSI */
5693 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5694 		/* Get the actual TC# for the UP */
5695 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5696 		/* Get the mapped netdev TC# for the UP */
5697 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5698 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5699 	}
5700 }
5701 
5702 /**
5703  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5704  * @vsi: the VSI being configured
5705  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5706  **/
5707 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5708 				      struct i40e_vsi_context *ctxt)
5709 {
5710 	/* copy just the sections touched not the entire info
5711 	 * since not all sections are valid as returned by
5712 	 * update vsi params
5713 	 */
5714 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5715 	memcpy(&vsi->info.queue_mapping,
5716 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5717 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5718 	       sizeof(vsi->info.tc_mapping));
5719 }
5720 
5721 /**
5722  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5723  * @vsi: the VSI being reconfigured
5724  * @vsi_offset: offset from main VF VSI
5725  */
5726 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5727 {
5728 	struct i40e_vsi_context ctxt = {};
5729 	struct i40e_pf *pf;
5730 	struct i40e_hw *hw;
5731 	int ret;
5732 
5733 	if (!vsi)
5734 		return -EINVAL;
5735 	pf = vsi->back;
5736 	hw = &pf->hw;
5737 
5738 	ctxt.seid = vsi->seid;
5739 	ctxt.pf_num = hw->pf_id;
5740 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5741 	ctxt.uplink_seid = vsi->uplink_seid;
5742 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5743 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5744 	ctxt.info = vsi->info;
5745 
5746 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5747 				 false);
5748 	if (vsi->reconfig_rss) {
5749 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5750 				      vsi->num_queue_pairs);
5751 		ret = i40e_vsi_config_rss(vsi);
5752 		if (ret) {
5753 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5754 			return ret;
5755 		}
5756 		vsi->reconfig_rss = false;
5757 	}
5758 
5759 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5760 	if (ret) {
5761 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5762 			 ERR_PTR(ret),
5763 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5764 		return ret;
5765 	}
5766 	/* update the local VSI info with updated queue map */
5767 	i40e_vsi_update_queue_map(vsi, &ctxt);
5768 	vsi->info.valid_sections = 0;
5769 
5770 	return ret;
5771 }
5772 
5773 /**
5774  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5775  * @vsi: VSI to be configured
5776  * @enabled_tc: TC bitmap
5777  *
5778  * This configures a particular VSI for TCs that are mapped to the
5779  * given TC bitmap. It uses default bandwidth share for TCs across
5780  * VSIs to configure TC for a particular VSI.
5781  *
5782  * NOTE:
5783  * It is expected that the VSI queues have been quisced before calling
5784  * this function.
5785  **/
5786 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5787 {
5788 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5789 	struct i40e_pf *pf = vsi->back;
5790 	struct i40e_hw *hw = &pf->hw;
5791 	struct i40e_vsi_context ctxt;
5792 	int ret = 0;
5793 	int i;
5794 
5795 	/* Check if enabled_tc is same as existing or new TCs */
5796 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5797 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5798 		return ret;
5799 
5800 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5801 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5802 		if (enabled_tc & BIT(i))
5803 			bw_share[i] = 1;
5804 	}
5805 
5806 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5807 	if (ret) {
5808 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5809 
5810 		dev_info(&pf->pdev->dev,
5811 			 "Failed configuring TC map %d for VSI %d\n",
5812 			 enabled_tc, vsi->seid);
5813 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5814 						  &bw_config, NULL);
5815 		if (ret) {
5816 			dev_info(&pf->pdev->dev,
5817 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5818 				 ERR_PTR(ret),
5819 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5820 			goto out;
5821 		}
5822 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5823 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5824 
5825 			if (!valid_tc)
5826 				valid_tc = bw_config.tc_valid_bits;
5827 			/* Always enable TC0, no matter what */
5828 			valid_tc |= 1;
5829 			dev_info(&pf->pdev->dev,
5830 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5831 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5832 			enabled_tc = valid_tc;
5833 		}
5834 
5835 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5836 		if (ret) {
5837 			dev_err(&pf->pdev->dev,
5838 				"Unable to  configure TC map %d for VSI %d\n",
5839 				enabled_tc, vsi->seid);
5840 			goto out;
5841 		}
5842 	}
5843 
5844 	/* Update Queue Pairs Mapping for currently enabled UPs */
5845 	ctxt.seid = vsi->seid;
5846 	ctxt.pf_num = vsi->back->hw.pf_id;
5847 	ctxt.vf_num = 0;
5848 	ctxt.uplink_seid = vsi->uplink_seid;
5849 	ctxt.info = vsi->info;
5850 	if (i40e_is_tc_mqprio_enabled(pf)) {
5851 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5852 		if (ret)
5853 			goto out;
5854 	} else {
5855 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5856 	}
5857 
5858 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5859 	 * queues changed.
5860 	 */
5861 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5862 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5863 				      vsi->num_queue_pairs);
5864 		ret = i40e_vsi_config_rss(vsi);
5865 		if (ret) {
5866 			dev_info(&vsi->back->pdev->dev,
5867 				 "Failed to reconfig rss for num_queues\n");
5868 			return ret;
5869 		}
5870 		vsi->reconfig_rss = false;
5871 	}
5872 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5873 		ctxt.info.valid_sections |=
5874 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5875 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5876 	}
5877 
5878 	/* Update the VSI after updating the VSI queue-mapping
5879 	 * information
5880 	 */
5881 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5882 	if (ret) {
5883 		dev_info(&pf->pdev->dev,
5884 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5885 			 ERR_PTR(ret),
5886 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5887 		goto out;
5888 	}
5889 	/* update the local VSI info with updated queue map */
5890 	i40e_vsi_update_queue_map(vsi, &ctxt);
5891 	vsi->info.valid_sections = 0;
5892 
5893 	/* Update current VSI BW information */
5894 	ret = i40e_vsi_get_bw_info(vsi);
5895 	if (ret) {
5896 		dev_info(&pf->pdev->dev,
5897 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5898 			 ERR_PTR(ret),
5899 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5900 		goto out;
5901 	}
5902 
5903 	/* Update the netdev TC setup */
5904 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5905 out:
5906 	return ret;
5907 }
5908 
5909 /**
5910  * i40e_get_link_speed - Returns link speed for the interface
5911  * @vsi: VSI to be configured
5912  *
5913  **/
5914 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5915 {
5916 	struct i40e_pf *pf = vsi->back;
5917 
5918 	switch (pf->hw.phy.link_info.link_speed) {
5919 	case I40E_LINK_SPEED_40GB:
5920 		return 40000;
5921 	case I40E_LINK_SPEED_25GB:
5922 		return 25000;
5923 	case I40E_LINK_SPEED_20GB:
5924 		return 20000;
5925 	case I40E_LINK_SPEED_10GB:
5926 		return 10000;
5927 	case I40E_LINK_SPEED_1GB:
5928 		return 1000;
5929 	default:
5930 		return -EINVAL;
5931 	}
5932 }
5933 
5934 /**
5935  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5936  * @vsi: Pointer to vsi structure
5937  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5938  *
5939  * Helper function to convert units before send to set BW limit
5940  **/
5941 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5942 {
5943 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5944 		dev_warn(&vsi->back->pdev->dev,
5945 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5946 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5947 	} else {
5948 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5949 	}
5950 
5951 	return max_tx_rate;
5952 }
5953 
5954 /**
5955  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5956  * @vsi: VSI to be configured
5957  * @seid: seid of the channel/VSI
5958  * @max_tx_rate: max TX rate to be configured as BW limit
5959  *
5960  * Helper function to set BW limit for a given VSI
5961  **/
5962 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5963 {
5964 	struct i40e_pf *pf = vsi->back;
5965 	u64 credits = 0;
5966 	int speed = 0;
5967 	int ret = 0;
5968 
5969 	speed = i40e_get_link_speed(vsi);
5970 	if (max_tx_rate > speed) {
5971 		dev_err(&pf->pdev->dev,
5972 			"Invalid max tx rate %llu specified for VSI seid %d.",
5973 			max_tx_rate, seid);
5974 		return -EINVAL;
5975 	}
5976 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5977 		dev_warn(&pf->pdev->dev,
5978 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5979 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5980 	}
5981 
5982 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5983 	credits = max_tx_rate;
5984 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5985 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5986 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5987 	if (ret)
5988 		dev_err(&pf->pdev->dev,
5989 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5990 			max_tx_rate, seid, ERR_PTR(ret),
5991 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5992 	return ret;
5993 }
5994 
5995 /**
5996  * i40e_remove_queue_channels - Remove queue channels for the TCs
5997  * @vsi: VSI to be configured
5998  *
5999  * Remove queue channels for the TCs
6000  **/
6001 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6002 {
6003 	enum i40e_admin_queue_err last_aq_status;
6004 	struct i40e_cloud_filter *cfilter;
6005 	struct i40e_channel *ch, *ch_tmp;
6006 	struct i40e_pf *pf = vsi->back;
6007 	struct hlist_node *node;
6008 	int ret, i;
6009 
6010 	/* Reset rss size that was stored when reconfiguring rss for
6011 	 * channel VSIs with non-power-of-2 queue count.
6012 	 */
6013 	vsi->current_rss_size = 0;
6014 
6015 	/* perform cleanup for channels if they exist */
6016 	if (list_empty(&vsi->ch_list))
6017 		return;
6018 
6019 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6020 		struct i40e_vsi *p_vsi;
6021 
6022 		list_del(&ch->list);
6023 		p_vsi = ch->parent_vsi;
6024 		if (!p_vsi || !ch->initialized) {
6025 			kfree(ch);
6026 			continue;
6027 		}
6028 		/* Reset queue contexts */
6029 		for (i = 0; i < ch->num_queue_pairs; i++) {
6030 			struct i40e_ring *tx_ring, *rx_ring;
6031 			u16 pf_q;
6032 
6033 			pf_q = ch->base_queue + i;
6034 			tx_ring = vsi->tx_rings[pf_q];
6035 			tx_ring->ch = NULL;
6036 
6037 			rx_ring = vsi->rx_rings[pf_q];
6038 			rx_ring->ch = NULL;
6039 		}
6040 
6041 		/* Reset BW configured for this VSI via mqprio */
6042 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6043 		if (ret)
6044 			dev_info(&vsi->back->pdev->dev,
6045 				 "Failed to reset tx rate for ch->seid %u\n",
6046 				 ch->seid);
6047 
6048 		/* delete cloud filters associated with this channel */
6049 		hlist_for_each_entry_safe(cfilter, node,
6050 					  &pf->cloud_filter_list, cloud_node) {
6051 			if (cfilter->seid != ch->seid)
6052 				continue;
6053 
6054 			hash_del(&cfilter->cloud_node);
6055 			if (cfilter->dst_port)
6056 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6057 									cfilter,
6058 									false);
6059 			else
6060 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6061 								false);
6062 			last_aq_status = pf->hw.aq.asq_last_status;
6063 			if (ret)
6064 				dev_info(&pf->pdev->dev,
6065 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6066 					 ERR_PTR(ret),
6067 					 i40e_aq_str(&pf->hw, last_aq_status));
6068 			kfree(cfilter);
6069 		}
6070 
6071 		/* delete VSI from FW */
6072 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6073 					     NULL);
6074 		if (ret)
6075 			dev_err(&vsi->back->pdev->dev,
6076 				"unable to remove channel (%d) for parent VSI(%d)\n",
6077 				ch->seid, p_vsi->seid);
6078 		kfree(ch);
6079 	}
6080 	INIT_LIST_HEAD(&vsi->ch_list);
6081 }
6082 
6083 /**
6084  * i40e_get_max_queues_for_channel
6085  * @vsi: ptr to VSI to which channels are associated with
6086  *
6087  * Helper function which returns max value among the queue counts set on the
6088  * channels/TCs created.
6089  **/
6090 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6091 {
6092 	struct i40e_channel *ch, *ch_tmp;
6093 	int max = 0;
6094 
6095 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6096 		if (!ch->initialized)
6097 			continue;
6098 		if (ch->num_queue_pairs > max)
6099 			max = ch->num_queue_pairs;
6100 	}
6101 
6102 	return max;
6103 }
6104 
6105 /**
6106  * i40e_validate_num_queues - validate num_queues w.r.t channel
6107  * @pf: ptr to PF device
6108  * @num_queues: number of queues
6109  * @vsi: the parent VSI
6110  * @reconfig_rss: indicates should the RSS be reconfigured or not
6111  *
6112  * This function validates number of queues in the context of new channel
6113  * which is being established and determines if RSS should be reconfigured
6114  * or not for parent VSI.
6115  **/
6116 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6117 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6118 {
6119 	int max_ch_queues;
6120 
6121 	if (!reconfig_rss)
6122 		return -EINVAL;
6123 
6124 	*reconfig_rss = false;
6125 	if (vsi->current_rss_size) {
6126 		if (num_queues > vsi->current_rss_size) {
6127 			dev_dbg(&pf->pdev->dev,
6128 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6129 				num_queues, vsi->current_rss_size);
6130 			return -EINVAL;
6131 		} else if ((num_queues < vsi->current_rss_size) &&
6132 			   (!is_power_of_2(num_queues))) {
6133 			dev_dbg(&pf->pdev->dev,
6134 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6135 				num_queues, vsi->current_rss_size);
6136 			return -EINVAL;
6137 		}
6138 	}
6139 
6140 	if (!is_power_of_2(num_queues)) {
6141 		/* Find the max num_queues configured for channel if channel
6142 		 * exist.
6143 		 * if channel exist, then enforce 'num_queues' to be more than
6144 		 * max ever queues configured for channel.
6145 		 */
6146 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6147 		if (num_queues < max_ch_queues) {
6148 			dev_dbg(&pf->pdev->dev,
6149 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6150 				num_queues, max_ch_queues);
6151 			return -EINVAL;
6152 		}
6153 		*reconfig_rss = true;
6154 	}
6155 
6156 	return 0;
6157 }
6158 
6159 /**
6160  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6161  * @vsi: the VSI being setup
6162  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6163  *
6164  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6165  **/
6166 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6167 {
6168 	struct i40e_pf *pf = vsi->back;
6169 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6170 	struct i40e_hw *hw = &pf->hw;
6171 	int local_rss_size;
6172 	u8 *lut;
6173 	int ret;
6174 
6175 	if (!vsi->rss_size)
6176 		return -EINVAL;
6177 
6178 	if (rss_size > vsi->rss_size)
6179 		return -EINVAL;
6180 
6181 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6182 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6183 	if (!lut)
6184 		return -ENOMEM;
6185 
6186 	/* Ignoring user configured lut if there is one */
6187 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6188 
6189 	/* Use user configured hash key if there is one, otherwise
6190 	 * use default.
6191 	 */
6192 	if (vsi->rss_hkey_user)
6193 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6194 	else
6195 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6196 
6197 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6198 	if (ret) {
6199 		dev_info(&pf->pdev->dev,
6200 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6201 			 ERR_PTR(ret),
6202 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6203 		kfree(lut);
6204 		return ret;
6205 	}
6206 	kfree(lut);
6207 
6208 	/* Do the update w.r.t. storing rss_size */
6209 	if (!vsi->orig_rss_size)
6210 		vsi->orig_rss_size = vsi->rss_size;
6211 	vsi->current_rss_size = local_rss_size;
6212 
6213 	return ret;
6214 }
6215 
6216 /**
6217  * i40e_channel_setup_queue_map - Setup a channel queue map
6218  * @pf: ptr to PF device
6219  * @ctxt: VSI context structure
6220  * @ch: ptr to channel structure
6221  *
6222  * Setup queue map for a specific channel
6223  **/
6224 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6225 					 struct i40e_vsi_context *ctxt,
6226 					 struct i40e_channel *ch)
6227 {
6228 	u16 qcount, qmap, sections = 0;
6229 	u8 offset = 0;
6230 	int pow;
6231 
6232 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6233 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6234 
6235 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6236 	ch->num_queue_pairs = qcount;
6237 
6238 	/* find the next higher power-of-2 of num queue pairs */
6239 	pow = ilog2(qcount);
6240 	if (!is_power_of_2(qcount))
6241 		pow++;
6242 
6243 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6244 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6245 
6246 	/* Setup queue TC[0].qmap for given VSI context */
6247 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6248 
6249 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6250 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6251 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6252 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6253 }
6254 
6255 /**
6256  * i40e_add_channel - add a channel by adding VSI
6257  * @pf: ptr to PF device
6258  * @uplink_seid: underlying HW switching element (VEB) ID
6259  * @ch: ptr to channel structure
6260  *
6261  * Add a channel (VSI) using add_vsi and queue_map
6262  **/
6263 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6264 			    struct i40e_channel *ch)
6265 {
6266 	struct i40e_hw *hw = &pf->hw;
6267 	struct i40e_vsi_context ctxt;
6268 	u8 enabled_tc = 0x1; /* TC0 enabled */
6269 	int ret;
6270 
6271 	if (ch->type != I40E_VSI_VMDQ2) {
6272 		dev_info(&pf->pdev->dev,
6273 			 "add new vsi failed, ch->type %d\n", ch->type);
6274 		return -EINVAL;
6275 	}
6276 
6277 	memset(&ctxt, 0, sizeof(ctxt));
6278 	ctxt.pf_num = hw->pf_id;
6279 	ctxt.vf_num = 0;
6280 	ctxt.uplink_seid = uplink_seid;
6281 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6282 	if (ch->type == I40E_VSI_VMDQ2)
6283 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6284 
6285 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6286 		ctxt.info.valid_sections |=
6287 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6288 		ctxt.info.switch_id =
6289 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6290 	}
6291 
6292 	/* Set queue map for a given VSI context */
6293 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6294 
6295 	/* Now time to create VSI */
6296 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6297 	if (ret) {
6298 		dev_info(&pf->pdev->dev,
6299 			 "add new vsi failed, err %pe aq_err %s\n",
6300 			 ERR_PTR(ret),
6301 			 i40e_aq_str(&pf->hw,
6302 				     pf->hw.aq.asq_last_status));
6303 		return -ENOENT;
6304 	}
6305 
6306 	/* Success, update channel, set enabled_tc only if the channel
6307 	 * is not a macvlan
6308 	 */
6309 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6310 	ch->seid = ctxt.seid;
6311 	ch->vsi_number = ctxt.vsi_number;
6312 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6313 
6314 	/* copy just the sections touched not the entire info
6315 	 * since not all sections are valid as returned by
6316 	 * update vsi params
6317 	 */
6318 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6319 	memcpy(&ch->info.queue_mapping,
6320 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6321 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6322 	       sizeof(ctxt.info.tc_mapping));
6323 
6324 	return 0;
6325 }
6326 
6327 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6328 				  u8 *bw_share)
6329 {
6330 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6331 	int ret;
6332 	int i;
6333 
6334 	memset(&bw_data, 0, sizeof(bw_data));
6335 	bw_data.tc_valid_bits = ch->enabled_tc;
6336 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6337 		bw_data.tc_bw_credits[i] = bw_share[i];
6338 
6339 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6340 				       &bw_data, NULL);
6341 	if (ret) {
6342 		dev_info(&vsi->back->pdev->dev,
6343 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6344 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6345 		return -EINVAL;
6346 	}
6347 
6348 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6349 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6350 
6351 	return 0;
6352 }
6353 
6354 /**
6355  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6356  * @pf: ptr to PF device
6357  * @vsi: the VSI being setup
6358  * @ch: ptr to channel structure
6359  *
6360  * Configure TX rings associated with channel (VSI) since queues are being
6361  * from parent VSI.
6362  **/
6363 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6364 				       struct i40e_vsi *vsi,
6365 				       struct i40e_channel *ch)
6366 {
6367 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6368 	int ret;
6369 	int i;
6370 
6371 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6372 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6373 		if (ch->enabled_tc & BIT(i))
6374 			bw_share[i] = 1;
6375 	}
6376 
6377 	/* configure BW for new VSI */
6378 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6379 	if (ret) {
6380 		dev_info(&vsi->back->pdev->dev,
6381 			 "Failed configuring TC map %d for channel (seid %u)\n",
6382 			 ch->enabled_tc, ch->seid);
6383 		return ret;
6384 	}
6385 
6386 	for (i = 0; i < ch->num_queue_pairs; i++) {
6387 		struct i40e_ring *tx_ring, *rx_ring;
6388 		u16 pf_q;
6389 
6390 		pf_q = ch->base_queue + i;
6391 
6392 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6393 		 * context
6394 		 */
6395 		tx_ring = vsi->tx_rings[pf_q];
6396 		tx_ring->ch = ch;
6397 
6398 		/* Get the RX ring ptr */
6399 		rx_ring = vsi->rx_rings[pf_q];
6400 		rx_ring->ch = ch;
6401 	}
6402 
6403 	return 0;
6404 }
6405 
6406 /**
6407  * i40e_setup_hw_channel - setup new channel
6408  * @pf: ptr to PF device
6409  * @vsi: the VSI being setup
6410  * @ch: ptr to channel structure
6411  * @uplink_seid: underlying HW switching element (VEB) ID
6412  * @type: type of channel to be created (VMDq2/VF)
6413  *
6414  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6415  * and configures TX rings accordingly
6416  **/
6417 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6418 					struct i40e_vsi *vsi,
6419 					struct i40e_channel *ch,
6420 					u16 uplink_seid, u8 type)
6421 {
6422 	int ret;
6423 
6424 	ch->initialized = false;
6425 	ch->base_queue = vsi->next_base_queue;
6426 	ch->type = type;
6427 
6428 	/* Proceed with creation of channel (VMDq2) VSI */
6429 	ret = i40e_add_channel(pf, uplink_seid, ch);
6430 	if (ret) {
6431 		dev_info(&pf->pdev->dev,
6432 			 "failed to add_channel using uplink_seid %u\n",
6433 			 uplink_seid);
6434 		return ret;
6435 	}
6436 
6437 	/* Mark the successful creation of channel */
6438 	ch->initialized = true;
6439 
6440 	/* Reconfigure TX queues using QTX_CTL register */
6441 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6442 	if (ret) {
6443 		dev_info(&pf->pdev->dev,
6444 			 "failed to configure TX rings for channel %u\n",
6445 			 ch->seid);
6446 		return ret;
6447 	}
6448 
6449 	/* update 'next_base_queue' */
6450 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6451 	dev_dbg(&pf->pdev->dev,
6452 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6453 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6454 		ch->num_queue_pairs,
6455 		vsi->next_base_queue);
6456 	return ret;
6457 }
6458 
6459 /**
6460  * i40e_setup_channel - setup new channel using uplink element
6461  * @pf: ptr to PF device
6462  * @vsi: pointer to the VSI to set up the channel within
6463  * @ch: ptr to channel structure
6464  *
6465  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6466  * and uplink switching element (uplink_seid)
6467  **/
6468 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6469 			       struct i40e_channel *ch)
6470 {
6471 	u8 vsi_type;
6472 	u16 seid;
6473 	int ret;
6474 
6475 	if (vsi->type == I40E_VSI_MAIN) {
6476 		vsi_type = I40E_VSI_VMDQ2;
6477 	} else {
6478 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6479 			vsi->type);
6480 		return false;
6481 	}
6482 
6483 	/* underlying switching element */
6484 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6485 
6486 	/* create channel (VSI), configure TX rings */
6487 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6488 	if (ret) {
6489 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6490 		return false;
6491 	}
6492 
6493 	return ch->initialized ? true : false;
6494 }
6495 
6496 /**
6497  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6498  * @vsi: ptr to VSI which has PF backing
6499  *
6500  * Sets up switch mode correctly if it needs to be changed and perform
6501  * what are allowed modes.
6502  **/
6503 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6504 {
6505 	u8 mode;
6506 	struct i40e_pf *pf = vsi->back;
6507 	struct i40e_hw *hw = &pf->hw;
6508 	int ret;
6509 
6510 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6511 	if (ret)
6512 		return -EINVAL;
6513 
6514 	if (hw->dev_caps.switch_mode) {
6515 		/* if switch mode is set, support mode2 (non-tunneled for
6516 		 * cloud filter) for now
6517 		 */
6518 		u32 switch_mode = hw->dev_caps.switch_mode &
6519 				  I40E_SWITCH_MODE_MASK;
6520 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6521 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6522 				return 0;
6523 			dev_err(&pf->pdev->dev,
6524 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6525 				hw->dev_caps.switch_mode);
6526 			return -EINVAL;
6527 		}
6528 	}
6529 
6530 	/* Set Bit 7 to be valid */
6531 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6532 
6533 	/* Set L4type for TCP support */
6534 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6535 
6536 	/* Set cloud filter mode */
6537 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6538 
6539 	/* Prep mode field for set_switch_config */
6540 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6541 					pf->last_sw_conf_valid_flags,
6542 					mode, NULL);
6543 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6544 		dev_err(&pf->pdev->dev,
6545 			"couldn't set switch config bits, err %pe aq_err %s\n",
6546 			ERR_PTR(ret),
6547 			i40e_aq_str(hw,
6548 				    hw->aq.asq_last_status));
6549 
6550 	return ret;
6551 }
6552 
6553 /**
6554  * i40e_create_queue_channel - function to create channel
6555  * @vsi: VSI to be configured
6556  * @ch: ptr to channel (it contains channel specific params)
6557  *
6558  * This function creates channel (VSI) using num_queues specified by user,
6559  * reconfigs RSS if needed.
6560  **/
6561 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6562 			      struct i40e_channel *ch)
6563 {
6564 	struct i40e_pf *pf = vsi->back;
6565 	bool reconfig_rss;
6566 	int err;
6567 
6568 	if (!ch)
6569 		return -EINVAL;
6570 
6571 	if (!ch->num_queue_pairs) {
6572 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6573 			ch->num_queue_pairs);
6574 		return -EINVAL;
6575 	}
6576 
6577 	/* validate user requested num_queues for channel */
6578 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6579 				       &reconfig_rss);
6580 	if (err) {
6581 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6582 			 ch->num_queue_pairs);
6583 		return -EINVAL;
6584 	}
6585 
6586 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6587 	 * VSI to be added switch to VEB mode.
6588 	 */
6589 
6590 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6591 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6592 
6593 		if (vsi->type == I40E_VSI_MAIN) {
6594 			if (i40e_is_tc_mqprio_enabled(pf))
6595 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6596 			else
6597 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6598 		}
6599 		/* now onwards for main VSI, number of queues will be value
6600 		 * of TC0's queue count
6601 		 */
6602 	}
6603 
6604 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6605 	 * it should be more than num_queues
6606 	 */
6607 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6608 		dev_dbg(&pf->pdev->dev,
6609 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6610 			vsi->cnt_q_avail, ch->num_queue_pairs);
6611 		return -EINVAL;
6612 	}
6613 
6614 	/* reconfig_rss only if vsi type is MAIN_VSI */
6615 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6616 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6617 		if (err) {
6618 			dev_info(&pf->pdev->dev,
6619 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6620 				 ch->num_queue_pairs);
6621 			return -EINVAL;
6622 		}
6623 	}
6624 
6625 	if (!i40e_setup_channel(pf, vsi, ch)) {
6626 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6627 		return -EINVAL;
6628 	}
6629 
6630 	dev_info(&pf->pdev->dev,
6631 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6632 		 ch->seid, ch->num_queue_pairs);
6633 
6634 	/* configure VSI for BW limit */
6635 	if (ch->max_tx_rate) {
6636 		u64 credits = ch->max_tx_rate;
6637 
6638 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6639 			return -EINVAL;
6640 
6641 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6642 		dev_dbg(&pf->pdev->dev,
6643 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6644 			ch->max_tx_rate,
6645 			credits,
6646 			ch->seid);
6647 	}
6648 
6649 	/* in case of VF, this will be main SRIOV VSI */
6650 	ch->parent_vsi = vsi;
6651 
6652 	/* and update main_vsi's count for queue_available to use */
6653 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6654 
6655 	return 0;
6656 }
6657 
6658 /**
6659  * i40e_configure_queue_channels - Add queue channel for the given TCs
6660  * @vsi: VSI to be configured
6661  *
6662  * Configures queue channel mapping to the given TCs
6663  **/
6664 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6665 {
6666 	struct i40e_channel *ch;
6667 	u64 max_rate = 0;
6668 	int ret = 0, i;
6669 
6670 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6671 	vsi->tc_seid_map[0] = vsi->seid;
6672 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6673 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6674 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6675 			if (!ch) {
6676 				ret = -ENOMEM;
6677 				goto err_free;
6678 			}
6679 
6680 			INIT_LIST_HEAD(&ch->list);
6681 			ch->num_queue_pairs =
6682 				vsi->tc_config.tc_info[i].qcount;
6683 			ch->base_queue =
6684 				vsi->tc_config.tc_info[i].qoffset;
6685 
6686 			/* Bandwidth limit through tc interface is in bytes/s,
6687 			 * change to Mbit/s
6688 			 */
6689 			max_rate = vsi->mqprio_qopt.max_rate[i];
6690 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6691 			ch->max_tx_rate = max_rate;
6692 
6693 			list_add_tail(&ch->list, &vsi->ch_list);
6694 
6695 			ret = i40e_create_queue_channel(vsi, ch);
6696 			if (ret) {
6697 				dev_err(&vsi->back->pdev->dev,
6698 					"Failed creating queue channel with TC%d: queues %d\n",
6699 					i, ch->num_queue_pairs);
6700 				goto err_free;
6701 			}
6702 			vsi->tc_seid_map[i] = ch->seid;
6703 		}
6704 	}
6705 
6706 	/* reset to reconfigure TX queue contexts */
6707 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6708 	return ret;
6709 
6710 err_free:
6711 	i40e_remove_queue_channels(vsi);
6712 	return ret;
6713 }
6714 
6715 /**
6716  * i40e_veb_config_tc - Configure TCs for given VEB
6717  * @veb: given VEB
6718  * @enabled_tc: TC bitmap
6719  *
6720  * Configures given TC bitmap for VEB (switching) element
6721  **/
6722 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6723 {
6724 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6725 	struct i40e_pf *pf = veb->pf;
6726 	int ret = 0;
6727 	int i;
6728 
6729 	/* No TCs or already enabled TCs just return */
6730 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6731 		return ret;
6732 
6733 	bw_data.tc_valid_bits = enabled_tc;
6734 	/* bw_data.absolute_credits is not set (relative) */
6735 
6736 	/* Enable ETS TCs with equal BW Share for now */
6737 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6738 		if (enabled_tc & BIT(i))
6739 			bw_data.tc_bw_share_credits[i] = 1;
6740 	}
6741 
6742 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6743 						   &bw_data, NULL);
6744 	if (ret) {
6745 		dev_info(&pf->pdev->dev,
6746 			 "VEB bw config failed, err %pe aq_err %s\n",
6747 			 ERR_PTR(ret),
6748 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6749 		goto out;
6750 	}
6751 
6752 	/* Update the BW information */
6753 	ret = i40e_veb_get_bw_info(veb);
6754 	if (ret) {
6755 		dev_info(&pf->pdev->dev,
6756 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6757 			 ERR_PTR(ret),
6758 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6759 	}
6760 
6761 out:
6762 	return ret;
6763 }
6764 
6765 #ifdef CONFIG_I40E_DCB
6766 /**
6767  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6768  * @pf: PF struct
6769  *
6770  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6771  * the caller would've quiesce all the VSIs before calling
6772  * this function
6773  **/
6774 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6775 {
6776 	struct i40e_vsi *vsi;
6777 	struct i40e_veb *veb;
6778 	u8 tc_map = 0;
6779 	int ret;
6780 	int v;
6781 
6782 	/* Enable the TCs available on PF to all VEBs */
6783 	tc_map = i40e_pf_get_tc_map(pf);
6784 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6785 		return;
6786 
6787 	i40e_pf_for_each_veb(pf, v, veb) {
6788 		ret = i40e_veb_config_tc(veb, tc_map);
6789 		if (ret) {
6790 			dev_info(&pf->pdev->dev,
6791 				 "Failed configuring TC for VEB seid=%d\n",
6792 				 veb->seid);
6793 			/* Will try to configure as many components */
6794 		}
6795 	}
6796 
6797 	/* Update each VSI */
6798 	i40e_pf_for_each_vsi(pf, v, vsi) {
6799 		/* - Enable all TCs for the LAN VSI
6800 		 * - For all others keep them at TC0 for now
6801 		 */
6802 		if (v == pf->lan_vsi)
6803 			tc_map = i40e_pf_get_tc_map(pf);
6804 		else
6805 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6806 
6807 		ret = i40e_vsi_config_tc(vsi, tc_map);
6808 		if (ret) {
6809 			dev_info(&pf->pdev->dev,
6810 				 "Failed configuring TC for VSI seid=%d\n",
6811 				 vsi->seid);
6812 			/* Will try to configure as many components */
6813 		} else {
6814 			/* Re-configure VSI vectors based on updated TC map */
6815 			i40e_vsi_map_rings_to_vectors(vsi);
6816 			if (vsi->netdev)
6817 				i40e_dcbnl_set_all(vsi);
6818 		}
6819 	}
6820 }
6821 
6822 /**
6823  * i40e_resume_port_tx - Resume port Tx
6824  * @pf: PF struct
6825  *
6826  * Resume a port's Tx and issue a PF reset in case of failure to
6827  * resume.
6828  **/
6829 static int i40e_resume_port_tx(struct i40e_pf *pf)
6830 {
6831 	struct i40e_hw *hw = &pf->hw;
6832 	int ret;
6833 
6834 	ret = i40e_aq_resume_port_tx(hw, NULL);
6835 	if (ret) {
6836 		dev_info(&pf->pdev->dev,
6837 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6838 			  ERR_PTR(ret),
6839 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6840 		/* Schedule PF reset to recover */
6841 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6842 		i40e_service_event_schedule(pf);
6843 	}
6844 
6845 	return ret;
6846 }
6847 
6848 /**
6849  * i40e_suspend_port_tx - Suspend port Tx
6850  * @pf: PF struct
6851  *
6852  * Suspend a port's Tx and issue a PF reset in case of failure.
6853  **/
6854 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6855 {
6856 	struct i40e_hw *hw = &pf->hw;
6857 	int ret;
6858 
6859 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6860 	if (ret) {
6861 		dev_info(&pf->pdev->dev,
6862 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6863 			 ERR_PTR(ret),
6864 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6865 		/* Schedule PF reset to recover */
6866 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6867 		i40e_service_event_schedule(pf);
6868 	}
6869 
6870 	return ret;
6871 }
6872 
6873 /**
6874  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6875  * @pf: PF being configured
6876  * @new_cfg: New DCBX configuration
6877  *
6878  * Program DCB settings into HW and reconfigure VEB/VSIs on
6879  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6880  **/
6881 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6882 				  struct i40e_dcbx_config *new_cfg)
6883 {
6884 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6885 	int ret;
6886 
6887 	/* Check if need reconfiguration */
6888 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6889 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6890 		return 0;
6891 	}
6892 
6893 	/* Config change disable all VSIs */
6894 	i40e_pf_quiesce_all_vsi(pf);
6895 
6896 	/* Copy the new config to the current config */
6897 	*old_cfg = *new_cfg;
6898 	old_cfg->etsrec = old_cfg->etscfg;
6899 	ret = i40e_set_dcb_config(&pf->hw);
6900 	if (ret) {
6901 		dev_info(&pf->pdev->dev,
6902 			 "Set DCB Config failed, err %pe aq_err %s\n",
6903 			 ERR_PTR(ret),
6904 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6905 		goto out;
6906 	}
6907 
6908 	/* Changes in configuration update VEB/VSI */
6909 	i40e_dcb_reconfigure(pf);
6910 out:
6911 	/* In case of reset do not try to resume anything */
6912 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6913 		/* Re-start the VSIs if disabled */
6914 		ret = i40e_resume_port_tx(pf);
6915 		/* In case of error no point in resuming VSIs */
6916 		if (ret)
6917 			goto err;
6918 		i40e_pf_unquiesce_all_vsi(pf);
6919 	}
6920 err:
6921 	return ret;
6922 }
6923 
6924 /**
6925  * i40e_hw_dcb_config - Program new DCBX settings into HW
6926  * @pf: PF being configured
6927  * @new_cfg: New DCBX configuration
6928  *
6929  * Program DCB settings into HW and reconfigure VEB/VSIs on
6930  * given PF
6931  **/
6932 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6933 {
6934 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6935 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6936 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6937 	struct i40e_dcbx_config *old_cfg;
6938 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6939 	struct i40e_rx_pb_config pb_cfg;
6940 	struct i40e_hw *hw = &pf->hw;
6941 	u8 num_ports = hw->num_ports;
6942 	bool need_reconfig;
6943 	int ret = -EINVAL;
6944 	u8 lltc_map = 0;
6945 	u8 tc_map = 0;
6946 	u8 new_numtc;
6947 	u8 i;
6948 
6949 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6950 	/* Un-pack information to Program ETS HW via shared API
6951 	 * numtc, tcmap
6952 	 * LLTC map
6953 	 * ETS/NON-ETS arbiter mode
6954 	 * max exponent (credit refills)
6955 	 * Total number of ports
6956 	 * PFC priority bit-map
6957 	 * Priority Table
6958 	 * BW % per TC
6959 	 * Arbiter mode between UPs sharing same TC
6960 	 * TSA table (ETS or non-ETS)
6961 	 * EEE enabled or not
6962 	 * MFS TC table
6963 	 */
6964 
6965 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6966 
6967 	memset(&ets_data, 0, sizeof(ets_data));
6968 	for (i = 0; i < new_numtc; i++) {
6969 		tc_map |= BIT(i);
6970 		switch (new_cfg->etscfg.tsatable[i]) {
6971 		case I40E_IEEE_TSA_ETS:
6972 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6973 			ets_data.tc_bw_share_credits[i] =
6974 					new_cfg->etscfg.tcbwtable[i];
6975 			break;
6976 		case I40E_IEEE_TSA_STRICT:
6977 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6978 			lltc_map |= BIT(i);
6979 			ets_data.tc_bw_share_credits[i] =
6980 					I40E_DCB_STRICT_PRIO_CREDITS;
6981 			break;
6982 		default:
6983 			/* Invalid TSA type */
6984 			need_reconfig = false;
6985 			goto out;
6986 		}
6987 	}
6988 
6989 	old_cfg = &hw->local_dcbx_config;
6990 	/* Check if need reconfiguration */
6991 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6992 
6993 	/* If needed, enable/disable frame tagging, disable all VSIs
6994 	 * and suspend port tx
6995 	 */
6996 	if (need_reconfig) {
6997 		/* Enable DCB tagging only when more than one TC */
6998 		if (new_numtc > 1)
6999 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7000 		else
7001 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7002 
7003 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7004 		/* Reconfiguration needed quiesce all VSIs */
7005 		i40e_pf_quiesce_all_vsi(pf);
7006 		ret = i40e_suspend_port_tx(pf);
7007 		if (ret)
7008 			goto err;
7009 	}
7010 
7011 	/* Configure Port ETS Tx Scheduler */
7012 	ets_data.tc_valid_bits = tc_map;
7013 	ets_data.tc_strict_priority_flags = lltc_map;
7014 	ret = i40e_aq_config_switch_comp_ets
7015 		(hw, pf->mac_seid, &ets_data,
7016 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7017 	if (ret) {
7018 		dev_info(&pf->pdev->dev,
7019 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7020 			 ERR_PTR(ret),
7021 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7022 		goto out;
7023 	}
7024 
7025 	/* Configure Rx ETS HW */
7026 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7027 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7028 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7029 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7030 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7031 				   lltc_map);
7032 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7033 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7034 				     prio_type);
7035 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7036 			       new_cfg->etscfg.prioritytable);
7037 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7038 
7039 	/* Configure Rx Packet Buffers in HW */
7040 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7041 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7042 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7043 	}
7044 
7045 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7046 					 false, new_cfg->pfc.pfcenable,
7047 					 mfs_tc, &pb_cfg);
7048 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7049 
7050 	/* Update the local Rx Packet buffer config */
7051 	pf->pb_cfg = pb_cfg;
7052 
7053 	/* Inform the FW about changes to DCB configuration */
7054 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7055 	if (ret) {
7056 		dev_info(&pf->pdev->dev,
7057 			 "DCB Updated failed, err %pe aq_err %s\n",
7058 			 ERR_PTR(ret),
7059 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7060 		goto out;
7061 	}
7062 
7063 	/* Update the port DCBx configuration */
7064 	*old_cfg = *new_cfg;
7065 
7066 	/* Changes in configuration update VEB/VSI */
7067 	i40e_dcb_reconfigure(pf);
7068 out:
7069 	/* Re-start the VSIs if disabled */
7070 	if (need_reconfig) {
7071 		ret = i40e_resume_port_tx(pf);
7072 
7073 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7074 		/* In case of error no point in resuming VSIs */
7075 		if (ret)
7076 			goto err;
7077 
7078 		/* Wait for the PF's queues to be disabled */
7079 		ret = i40e_pf_wait_queues_disabled(pf);
7080 		if (ret) {
7081 			/* Schedule PF reset to recover */
7082 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7083 			i40e_service_event_schedule(pf);
7084 			goto err;
7085 		} else {
7086 			i40e_pf_unquiesce_all_vsi(pf);
7087 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7088 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7089 		}
7090 		/* registers are set, lets apply */
7091 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7092 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7093 	}
7094 
7095 err:
7096 	return ret;
7097 }
7098 
7099 /**
7100  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7101  * @pf: PF being queried
7102  *
7103  * Set default DCB configuration in case DCB is to be done in SW.
7104  **/
7105 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7106 {
7107 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7108 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7109 	struct i40e_hw *hw = &pf->hw;
7110 	int err;
7111 
7112 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7113 		/* Update the local cached instance with TC0 ETS */
7114 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7115 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7116 		pf->tmp_cfg.etscfg.maxtcs = 0;
7117 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7118 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7119 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7120 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7121 		/* FW needs one App to configure HW */
7122 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7123 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7124 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7125 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7126 
7127 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7128 	}
7129 
7130 	memset(&ets_data, 0, sizeof(ets_data));
7131 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7132 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7133 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7134 
7135 	/* Enable ETS on the Physical port */
7136 	err = i40e_aq_config_switch_comp_ets
7137 		(hw, pf->mac_seid, &ets_data,
7138 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7139 	if (err) {
7140 		dev_info(&pf->pdev->dev,
7141 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7142 			 ERR_PTR(err),
7143 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7144 		err = -ENOENT;
7145 		goto out;
7146 	}
7147 
7148 	/* Update the local cached instance with TC0 ETS */
7149 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7150 	dcb_cfg->etscfg.cbs = 0;
7151 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7152 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7153 
7154 out:
7155 	return err;
7156 }
7157 
7158 /**
7159  * i40e_init_pf_dcb - Initialize DCB configuration
7160  * @pf: PF being configured
7161  *
7162  * Query the current DCB configuration and cache it
7163  * in the hardware structure
7164  **/
7165 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7166 {
7167 	struct i40e_hw *hw = &pf->hw;
7168 	int err;
7169 
7170 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7171 	 * Also do not enable DCBx if FW LLDP agent is disabled
7172 	 */
7173 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7174 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7175 		err = -EOPNOTSUPP;
7176 		goto out;
7177 	}
7178 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7179 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7180 		err = i40e_dcb_sw_default_config(pf);
7181 		if (err) {
7182 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7183 			goto out;
7184 		}
7185 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7186 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7187 			       DCB_CAP_DCBX_VER_IEEE;
7188 		/* at init capable but disabled */
7189 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7190 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7191 		goto out;
7192 	}
7193 	err = i40e_init_dcb(hw, true);
7194 	if (!err) {
7195 		/* Device/Function is not DCBX capable */
7196 		if ((!hw->func_caps.dcb) ||
7197 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7198 			dev_info(&pf->pdev->dev,
7199 				 "DCBX offload is not supported or is disabled for this PF.\n");
7200 		} else {
7201 			/* When status is not DISABLED then DCBX in FW */
7202 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7203 				       DCB_CAP_DCBX_VER_IEEE;
7204 
7205 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7206 			/* Enable DCB tagging only when more than one TC
7207 			 * or explicitly disable if only one TC
7208 			 */
7209 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7210 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7211 			else
7212 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7213 			dev_dbg(&pf->pdev->dev,
7214 				"DCBX offload is supported for this PF.\n");
7215 		}
7216 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7217 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7218 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7219 	} else {
7220 		dev_info(&pf->pdev->dev,
7221 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7222 			 ERR_PTR(err),
7223 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7224 	}
7225 
7226 out:
7227 	return err;
7228 }
7229 #endif /* CONFIG_I40E_DCB */
7230 
7231 /**
7232  * i40e_print_link_message - print link up or down
7233  * @vsi: the VSI for which link needs a message
7234  * @isup: true of link is up, false otherwise
7235  */
7236 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7237 {
7238 	enum i40e_aq_link_speed new_speed;
7239 	struct i40e_pf *pf = vsi->back;
7240 	char *speed = "Unknown";
7241 	char *fc = "Unknown";
7242 	char *fec = "";
7243 	char *req_fec = "";
7244 	char *an = "";
7245 
7246 	if (isup)
7247 		new_speed = pf->hw.phy.link_info.link_speed;
7248 	else
7249 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7250 
7251 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7252 		return;
7253 	vsi->current_isup = isup;
7254 	vsi->current_speed = new_speed;
7255 	if (!isup) {
7256 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7257 		return;
7258 	}
7259 
7260 	/* Warn user if link speed on NPAR enabled partition is not at
7261 	 * least 10GB
7262 	 */
7263 	if (pf->hw.func_caps.npar_enable &&
7264 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7265 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7266 		netdev_warn(vsi->netdev,
7267 			    "The partition detected link speed that is less than 10Gbps\n");
7268 
7269 	switch (pf->hw.phy.link_info.link_speed) {
7270 	case I40E_LINK_SPEED_40GB:
7271 		speed = "40 G";
7272 		break;
7273 	case I40E_LINK_SPEED_20GB:
7274 		speed = "20 G";
7275 		break;
7276 	case I40E_LINK_SPEED_25GB:
7277 		speed = "25 G";
7278 		break;
7279 	case I40E_LINK_SPEED_10GB:
7280 		speed = "10 G";
7281 		break;
7282 	case I40E_LINK_SPEED_5GB:
7283 		speed = "5 G";
7284 		break;
7285 	case I40E_LINK_SPEED_2_5GB:
7286 		speed = "2.5 G";
7287 		break;
7288 	case I40E_LINK_SPEED_1GB:
7289 		speed = "1000 M";
7290 		break;
7291 	case I40E_LINK_SPEED_100MB:
7292 		speed = "100 M";
7293 		break;
7294 	default:
7295 		break;
7296 	}
7297 
7298 	switch (pf->hw.fc.current_mode) {
7299 	case I40E_FC_FULL:
7300 		fc = "RX/TX";
7301 		break;
7302 	case I40E_FC_TX_PAUSE:
7303 		fc = "TX";
7304 		break;
7305 	case I40E_FC_RX_PAUSE:
7306 		fc = "RX";
7307 		break;
7308 	default:
7309 		fc = "None";
7310 		break;
7311 	}
7312 
7313 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7314 		req_fec = "None";
7315 		fec = "None";
7316 		an = "False";
7317 
7318 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7319 			an = "True";
7320 
7321 		if (pf->hw.phy.link_info.fec_info &
7322 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7323 			fec = "CL74 FC-FEC/BASE-R";
7324 		else if (pf->hw.phy.link_info.fec_info &
7325 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7326 			fec = "CL108 RS-FEC";
7327 
7328 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7329 		 * both RS and FC are requested
7330 		 */
7331 		if (vsi->back->hw.phy.link_info.req_fec_info &
7332 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7333 			if (vsi->back->hw.phy.link_info.req_fec_info &
7334 			    I40E_AQ_REQUEST_FEC_RS)
7335 				req_fec = "CL108 RS-FEC";
7336 			else
7337 				req_fec = "CL74 FC-FEC/BASE-R";
7338 		}
7339 		netdev_info(vsi->netdev,
7340 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7341 			    speed, req_fec, fec, an, fc);
7342 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7343 		req_fec = "None";
7344 		fec = "None";
7345 		an = "False";
7346 
7347 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7348 			an = "True";
7349 
7350 		if (pf->hw.phy.link_info.fec_info &
7351 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7352 			fec = "CL74 FC-FEC/BASE-R";
7353 
7354 		if (pf->hw.phy.link_info.req_fec_info &
7355 		    I40E_AQ_REQUEST_FEC_KR)
7356 			req_fec = "CL74 FC-FEC/BASE-R";
7357 
7358 		netdev_info(vsi->netdev,
7359 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7360 			    speed, req_fec, fec, an, fc);
7361 	} else {
7362 		netdev_info(vsi->netdev,
7363 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7364 			    speed, fc);
7365 	}
7366 
7367 }
7368 
7369 /**
7370  * i40e_up_complete - Finish the last steps of bringing up a connection
7371  * @vsi: the VSI being configured
7372  **/
7373 static int i40e_up_complete(struct i40e_vsi *vsi)
7374 {
7375 	struct i40e_pf *pf = vsi->back;
7376 	int err;
7377 
7378 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7379 		i40e_vsi_configure_msix(vsi);
7380 	else
7381 		i40e_configure_msi_and_legacy(vsi);
7382 
7383 	/* start rings */
7384 	err = i40e_vsi_start_rings(vsi);
7385 	if (err)
7386 		return err;
7387 
7388 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7389 	i40e_napi_enable_all(vsi);
7390 	i40e_vsi_enable_irq(vsi);
7391 
7392 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7393 	    (vsi->netdev)) {
7394 		i40e_print_link_message(vsi, true);
7395 		netif_tx_start_all_queues(vsi->netdev);
7396 		netif_carrier_on(vsi->netdev);
7397 	}
7398 
7399 	/* replay FDIR SB filters */
7400 	if (vsi->type == I40E_VSI_FDIR) {
7401 		/* reset fd counters */
7402 		pf->fd_add_err = 0;
7403 		pf->fd_atr_cnt = 0;
7404 		i40e_fdir_filter_restore(vsi);
7405 	}
7406 
7407 	/* On the next run of the service_task, notify any clients of the new
7408 	 * opened netdev
7409 	 */
7410 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7411 	i40e_service_event_schedule(pf);
7412 
7413 	return 0;
7414 }
7415 
7416 /**
7417  * i40e_vsi_reinit_locked - Reset the VSI
7418  * @vsi: the VSI being configured
7419  *
7420  * Rebuild the ring structs after some configuration
7421  * has changed, e.g. MTU size.
7422  **/
7423 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7424 {
7425 	struct i40e_pf *pf = vsi->back;
7426 
7427 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7428 		usleep_range(1000, 2000);
7429 	i40e_down(vsi);
7430 
7431 	i40e_up(vsi);
7432 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7433 }
7434 
7435 /**
7436  * i40e_force_link_state - Force the link status
7437  * @pf: board private structure
7438  * @is_up: whether the link state should be forced up or down
7439  **/
7440 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7441 {
7442 	struct i40e_aq_get_phy_abilities_resp abilities;
7443 	struct i40e_aq_set_phy_config config = {0};
7444 	bool non_zero_phy_type = is_up;
7445 	struct i40e_hw *hw = &pf->hw;
7446 	u64 mask;
7447 	u8 speed;
7448 	int err;
7449 
7450 	/* Card might've been put in an unstable state by other drivers
7451 	 * and applications, which causes incorrect speed values being
7452 	 * set on startup. In order to clear speed registers, we call
7453 	 * get_phy_capabilities twice, once to get initial state of
7454 	 * available speeds, and once to get current PHY config.
7455 	 */
7456 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7457 					   NULL);
7458 	if (err) {
7459 		dev_err(&pf->pdev->dev,
7460 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7461 			ERR_PTR(err),
7462 			i40e_aq_str(hw, hw->aq.asq_last_status));
7463 		return err;
7464 	}
7465 	speed = abilities.link_speed;
7466 
7467 	/* Get the current phy config */
7468 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7469 					   NULL);
7470 	if (err) {
7471 		dev_err(&pf->pdev->dev,
7472 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7473 			ERR_PTR(err),
7474 			i40e_aq_str(hw, hw->aq.asq_last_status));
7475 		return err;
7476 	}
7477 
7478 	/* If link needs to go up, but was not forced to go down,
7479 	 * and its speed values are OK, no need for a flap
7480 	 * if non_zero_phy_type was set, still need to force up
7481 	 */
7482 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7483 		non_zero_phy_type = true;
7484 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7485 		return 0;
7486 
7487 	/* To force link we need to set bits for all supported PHY types,
7488 	 * but there are now more than 32, so we need to split the bitmap
7489 	 * across two fields.
7490 	 */
7491 	mask = I40E_PHY_TYPES_BITMASK;
7492 	config.phy_type =
7493 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7494 	config.phy_type_ext =
7495 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7496 	/* Copy the old settings, except of phy_type */
7497 	config.abilities = abilities.abilities;
7498 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7499 		if (is_up)
7500 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7501 		else
7502 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7503 	}
7504 	if (abilities.link_speed != 0)
7505 		config.link_speed = abilities.link_speed;
7506 	else
7507 		config.link_speed = speed;
7508 	config.eee_capability = abilities.eee_capability;
7509 	config.eeer = abilities.eeer_val;
7510 	config.low_power_ctrl = abilities.d3_lpan;
7511 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7512 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7513 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7514 
7515 	if (err) {
7516 		dev_err(&pf->pdev->dev,
7517 			"set phy config ret =  %pe last_status =  %s\n",
7518 			ERR_PTR(err),
7519 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7520 		return err;
7521 	}
7522 
7523 	/* Update the link info */
7524 	err = i40e_update_link_info(hw);
7525 	if (err) {
7526 		/* Wait a little bit (on 40G cards it sometimes takes a really
7527 		 * long time for link to come back from the atomic reset)
7528 		 * and try once more
7529 		 */
7530 		msleep(1000);
7531 		i40e_update_link_info(hw);
7532 	}
7533 
7534 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7535 
7536 	return 0;
7537 }
7538 
7539 /**
7540  * i40e_up - Bring the connection back up after being down
7541  * @vsi: the VSI being configured
7542  **/
7543 int i40e_up(struct i40e_vsi *vsi)
7544 {
7545 	int err;
7546 
7547 	if (vsi->type == I40E_VSI_MAIN &&
7548 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7549 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7550 		i40e_force_link_state(vsi->back, true);
7551 
7552 	err = i40e_vsi_configure(vsi);
7553 	if (!err)
7554 		err = i40e_up_complete(vsi);
7555 
7556 	return err;
7557 }
7558 
7559 /**
7560  * i40e_down - Shutdown the connection processing
7561  * @vsi: the VSI being stopped
7562  **/
7563 void i40e_down(struct i40e_vsi *vsi)
7564 {
7565 	int i;
7566 
7567 	/* It is assumed that the caller of this function
7568 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7569 	 */
7570 	if (vsi->netdev) {
7571 		netif_carrier_off(vsi->netdev);
7572 		netif_tx_disable(vsi->netdev);
7573 	}
7574 	i40e_vsi_disable_irq(vsi);
7575 	i40e_vsi_stop_rings(vsi);
7576 	if (vsi->type == I40E_VSI_MAIN &&
7577 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7578 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7579 		i40e_force_link_state(vsi->back, false);
7580 	i40e_napi_disable_all(vsi);
7581 
7582 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7583 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7584 		if (i40e_enabled_xdp_vsi(vsi)) {
7585 			/* Make sure that in-progress ndo_xdp_xmit and
7586 			 * ndo_xsk_wakeup calls are completed.
7587 			 */
7588 			synchronize_rcu();
7589 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7590 		}
7591 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7592 	}
7593 
7594 }
7595 
7596 /**
7597  * i40e_validate_mqprio_qopt- validate queue mapping info
7598  * @vsi: the VSI being configured
7599  * @mqprio_qopt: queue parametrs
7600  **/
7601 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7602 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7603 {
7604 	u64 sum_max_rate = 0;
7605 	u64 max_rate = 0;
7606 	int i;
7607 
7608 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7609 	    mqprio_qopt->qopt.num_tc < 1 ||
7610 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7611 		return -EINVAL;
7612 	for (i = 0; ; i++) {
7613 		if (!mqprio_qopt->qopt.count[i])
7614 			return -EINVAL;
7615 		if (mqprio_qopt->min_rate[i]) {
7616 			dev_err(&vsi->back->pdev->dev,
7617 				"Invalid min tx rate (greater than 0) specified\n");
7618 			return -EINVAL;
7619 		}
7620 		max_rate = mqprio_qopt->max_rate[i];
7621 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7622 		sum_max_rate += max_rate;
7623 
7624 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7625 			break;
7626 		if (mqprio_qopt->qopt.offset[i + 1] !=
7627 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7628 			return -EINVAL;
7629 	}
7630 	if (vsi->num_queue_pairs <
7631 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7632 		dev_err(&vsi->back->pdev->dev,
7633 			"Failed to create traffic channel, insufficient number of queues.\n");
7634 		return -EINVAL;
7635 	}
7636 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7637 		dev_err(&vsi->back->pdev->dev,
7638 			"Invalid max tx rate specified\n");
7639 		return -EINVAL;
7640 	}
7641 	return 0;
7642 }
7643 
7644 /**
7645  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7646  * @vsi: the VSI being configured
7647  **/
7648 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7649 {
7650 	u16 qcount;
7651 	int i;
7652 
7653 	/* Only TC0 is enabled */
7654 	vsi->tc_config.numtc = 1;
7655 	vsi->tc_config.enabled_tc = 1;
7656 	qcount = min_t(int, vsi->alloc_queue_pairs,
7657 		       i40e_pf_get_max_q_per_tc(vsi->back));
7658 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7659 		/* For the TC that is not enabled set the offset to default
7660 		 * queue and allocate one queue for the given TC.
7661 		 */
7662 		vsi->tc_config.tc_info[i].qoffset = 0;
7663 		if (i == 0)
7664 			vsi->tc_config.tc_info[i].qcount = qcount;
7665 		else
7666 			vsi->tc_config.tc_info[i].qcount = 1;
7667 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7668 	}
7669 }
7670 
7671 /**
7672  * i40e_del_macvlan_filter
7673  * @hw: pointer to the HW structure
7674  * @seid: seid of the channel VSI
7675  * @macaddr: the mac address to apply as a filter
7676  * @aq_err: store the admin Q error
7677  *
7678  * This function deletes a mac filter on the channel VSI which serves as the
7679  * macvlan. Returns 0 on success.
7680  **/
7681 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7682 				   const u8 *macaddr, int *aq_err)
7683 {
7684 	struct i40e_aqc_remove_macvlan_element_data element;
7685 	int status;
7686 
7687 	memset(&element, 0, sizeof(element));
7688 	ether_addr_copy(element.mac_addr, macaddr);
7689 	element.vlan_tag = 0;
7690 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7691 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7692 	*aq_err = hw->aq.asq_last_status;
7693 
7694 	return status;
7695 }
7696 
7697 /**
7698  * i40e_add_macvlan_filter
7699  * @hw: pointer to the HW structure
7700  * @seid: seid of the channel VSI
7701  * @macaddr: the mac address to apply as a filter
7702  * @aq_err: store the admin Q error
7703  *
7704  * This function adds a mac filter on the channel VSI which serves as the
7705  * macvlan. Returns 0 on success.
7706  **/
7707 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7708 				   const u8 *macaddr, int *aq_err)
7709 {
7710 	struct i40e_aqc_add_macvlan_element_data element;
7711 	u16 cmd_flags = 0;
7712 	int status;
7713 
7714 	ether_addr_copy(element.mac_addr, macaddr);
7715 	element.vlan_tag = 0;
7716 	element.queue_number = 0;
7717 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7718 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7719 	element.flags = cpu_to_le16(cmd_flags);
7720 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7721 	*aq_err = hw->aq.asq_last_status;
7722 
7723 	return status;
7724 }
7725 
7726 /**
7727  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7728  * @vsi: the VSI we want to access
7729  * @ch: the channel we want to access
7730  */
7731 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7732 {
7733 	struct i40e_ring *tx_ring, *rx_ring;
7734 	u16 pf_q;
7735 	int i;
7736 
7737 	for (i = 0; i < ch->num_queue_pairs; i++) {
7738 		pf_q = ch->base_queue + i;
7739 		tx_ring = vsi->tx_rings[pf_q];
7740 		tx_ring->ch = NULL;
7741 		rx_ring = vsi->rx_rings[pf_q];
7742 		rx_ring->ch = NULL;
7743 	}
7744 }
7745 
7746 /**
7747  * i40e_free_macvlan_channels
7748  * @vsi: the VSI we want to access
7749  *
7750  * This function frees the Qs of the channel VSI from
7751  * the stack and also deletes the channel VSIs which
7752  * serve as macvlans.
7753  */
7754 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7755 {
7756 	struct i40e_channel *ch, *ch_tmp;
7757 	int ret;
7758 
7759 	if (list_empty(&vsi->macvlan_list))
7760 		return;
7761 
7762 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7763 		struct i40e_vsi *parent_vsi;
7764 
7765 		if (i40e_is_channel_macvlan(ch)) {
7766 			i40e_reset_ch_rings(vsi, ch);
7767 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7768 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7769 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7770 			kfree(ch->fwd);
7771 			ch->fwd = NULL;
7772 		}
7773 
7774 		list_del(&ch->list);
7775 		parent_vsi = ch->parent_vsi;
7776 		if (!parent_vsi || !ch->initialized) {
7777 			kfree(ch);
7778 			continue;
7779 		}
7780 
7781 		/* remove the VSI */
7782 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7783 					     NULL);
7784 		if (ret)
7785 			dev_err(&vsi->back->pdev->dev,
7786 				"unable to remove channel (%d) for parent VSI(%d)\n",
7787 				ch->seid, parent_vsi->seid);
7788 		kfree(ch);
7789 	}
7790 	vsi->macvlan_cnt = 0;
7791 }
7792 
7793 /**
7794  * i40e_fwd_ring_up - bring the macvlan device up
7795  * @vsi: the VSI we want to access
7796  * @vdev: macvlan netdevice
7797  * @fwd: the private fwd structure
7798  */
7799 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7800 			    struct i40e_fwd_adapter *fwd)
7801 {
7802 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7803 	int ret = 0, num_tc = 1,  i, aq_err;
7804 	struct i40e_pf *pf = vsi->back;
7805 	struct i40e_hw *hw = &pf->hw;
7806 
7807 	/* Go through the list and find an available channel */
7808 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7809 		if (!i40e_is_channel_macvlan(iter)) {
7810 			iter->fwd = fwd;
7811 			/* record configuration for macvlan interface in vdev */
7812 			for (i = 0; i < num_tc; i++)
7813 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7814 							     i,
7815 							     iter->num_queue_pairs,
7816 							     iter->base_queue);
7817 			for (i = 0; i < iter->num_queue_pairs; i++) {
7818 				struct i40e_ring *tx_ring, *rx_ring;
7819 				u16 pf_q;
7820 
7821 				pf_q = iter->base_queue + i;
7822 
7823 				/* Get to TX ring ptr */
7824 				tx_ring = vsi->tx_rings[pf_q];
7825 				tx_ring->ch = iter;
7826 
7827 				/* Get the RX ring ptr */
7828 				rx_ring = vsi->rx_rings[pf_q];
7829 				rx_ring->ch = iter;
7830 			}
7831 			ch = iter;
7832 			break;
7833 		}
7834 	}
7835 
7836 	if (!ch)
7837 		return -EINVAL;
7838 
7839 	/* Guarantee all rings are updated before we update the
7840 	 * MAC address filter.
7841 	 */
7842 	wmb();
7843 
7844 	/* Add a mac filter */
7845 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7846 	if (ret) {
7847 		/* if we cannot add the MAC rule then disable the offload */
7848 		macvlan_release_l2fw_offload(vdev);
7849 		for (i = 0; i < ch->num_queue_pairs; i++) {
7850 			struct i40e_ring *rx_ring;
7851 			u16 pf_q;
7852 
7853 			pf_q = ch->base_queue + i;
7854 			rx_ring = vsi->rx_rings[pf_q];
7855 			rx_ring->netdev = NULL;
7856 		}
7857 		dev_info(&pf->pdev->dev,
7858 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7859 			  ERR_PTR(ret),
7860 			  i40e_aq_str(hw, aq_err));
7861 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7862 	}
7863 
7864 	return ret;
7865 }
7866 
7867 /**
7868  * i40e_setup_macvlans - create the channels which will be macvlans
7869  * @vsi: the VSI we want to access
7870  * @macvlan_cnt: no. of macvlans to be setup
7871  * @qcnt: no. of Qs per macvlan
7872  * @vdev: macvlan netdevice
7873  */
7874 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7875 			       struct net_device *vdev)
7876 {
7877 	struct i40e_pf *pf = vsi->back;
7878 	struct i40e_hw *hw = &pf->hw;
7879 	struct i40e_vsi_context ctxt;
7880 	u16 sections, qmap, num_qps;
7881 	struct i40e_channel *ch;
7882 	int i, pow, ret = 0;
7883 	u8 offset = 0;
7884 
7885 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7886 		return -EINVAL;
7887 
7888 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7889 
7890 	/* find the next higher power-of-2 of num queue pairs */
7891 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7892 
7893 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7894 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7895 
7896 	/* Setup context bits for the main VSI */
7897 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7898 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7899 	memset(&ctxt, 0, sizeof(ctxt));
7900 	ctxt.seid = vsi->seid;
7901 	ctxt.pf_num = vsi->back->hw.pf_id;
7902 	ctxt.vf_num = 0;
7903 	ctxt.uplink_seid = vsi->uplink_seid;
7904 	ctxt.info = vsi->info;
7905 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7906 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7907 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7908 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7909 
7910 	/* Reconfigure RSS for main VSI with new max queue count */
7911 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7912 	ret = i40e_vsi_config_rss(vsi);
7913 	if (ret) {
7914 		dev_info(&pf->pdev->dev,
7915 			 "Failed to reconfig RSS for num_queues (%u)\n",
7916 			 vsi->rss_size);
7917 		return ret;
7918 	}
7919 	vsi->reconfig_rss = true;
7920 	dev_dbg(&vsi->back->pdev->dev,
7921 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7922 	vsi->next_base_queue = num_qps;
7923 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7924 
7925 	/* Update the VSI after updating the VSI queue-mapping
7926 	 * information
7927 	 */
7928 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7929 	if (ret) {
7930 		dev_info(&pf->pdev->dev,
7931 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7932 			 ERR_PTR(ret),
7933 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7934 		return ret;
7935 	}
7936 	/* update the local VSI info with updated queue map */
7937 	i40e_vsi_update_queue_map(vsi, &ctxt);
7938 	vsi->info.valid_sections = 0;
7939 
7940 	/* Create channels for macvlans */
7941 	INIT_LIST_HEAD(&vsi->macvlan_list);
7942 	for (i = 0; i < macvlan_cnt; i++) {
7943 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7944 		if (!ch) {
7945 			ret = -ENOMEM;
7946 			goto err_free;
7947 		}
7948 		INIT_LIST_HEAD(&ch->list);
7949 		ch->num_queue_pairs = qcnt;
7950 		if (!i40e_setup_channel(pf, vsi, ch)) {
7951 			ret = -EINVAL;
7952 			kfree(ch);
7953 			goto err_free;
7954 		}
7955 		ch->parent_vsi = vsi;
7956 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7957 		vsi->macvlan_cnt++;
7958 		list_add_tail(&ch->list, &vsi->macvlan_list);
7959 	}
7960 
7961 	return ret;
7962 
7963 err_free:
7964 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7965 	i40e_free_macvlan_channels(vsi);
7966 
7967 	return ret;
7968 }
7969 
7970 /**
7971  * i40e_fwd_add - configure macvlans
7972  * @netdev: net device to configure
7973  * @vdev: macvlan netdevice
7974  **/
7975 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7976 {
7977 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7978 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7979 	struct i40e_vsi *vsi = np->vsi;
7980 	struct i40e_pf *pf = vsi->back;
7981 	struct i40e_fwd_adapter *fwd;
7982 	int avail_macvlan, ret;
7983 
7984 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
7985 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7986 		return ERR_PTR(-EINVAL);
7987 	}
7988 	if (i40e_is_tc_mqprio_enabled(pf)) {
7989 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7990 		return ERR_PTR(-EINVAL);
7991 	}
7992 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7993 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7994 		return ERR_PTR(-EINVAL);
7995 	}
7996 
7997 	/* The macvlan device has to be a single Q device so that the
7998 	 * tc_to_txq field can be reused to pick the tx queue.
7999 	 */
8000 	if (netif_is_multiqueue(vdev))
8001 		return ERR_PTR(-ERANGE);
8002 
8003 	if (!vsi->macvlan_cnt) {
8004 		/* reserve bit 0 for the pf device */
8005 		set_bit(0, vsi->fwd_bitmask);
8006 
8007 		/* Try to reserve as many queues as possible for macvlans. First
8008 		 * reserve 3/4th of max vectors, then half, then quarter and
8009 		 * calculate Qs per macvlan as you go
8010 		 */
8011 		vectors = pf->num_lan_msix;
8012 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8013 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8014 			q_per_macvlan = 4;
8015 			macvlan_cnt = (vectors - 32) / 4;
8016 		} else if (vectors <= 64 && vectors > 32) {
8017 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8018 			q_per_macvlan = 2;
8019 			macvlan_cnt = (vectors - 16) / 2;
8020 		} else if (vectors <= 32 && vectors > 16) {
8021 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8022 			q_per_macvlan = 1;
8023 			macvlan_cnt = vectors - 16;
8024 		} else if (vectors <= 16 && vectors > 8) {
8025 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8026 			q_per_macvlan = 1;
8027 			macvlan_cnt = vectors - 8;
8028 		} else {
8029 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8030 			q_per_macvlan = 1;
8031 			macvlan_cnt = vectors - 1;
8032 		}
8033 
8034 		if (macvlan_cnt == 0)
8035 			return ERR_PTR(-EBUSY);
8036 
8037 		/* Quiesce VSI queues */
8038 		i40e_quiesce_vsi(vsi);
8039 
8040 		/* sets up the macvlans but does not "enable" them */
8041 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8042 					  vdev);
8043 		if (ret)
8044 			return ERR_PTR(ret);
8045 
8046 		/* Unquiesce VSI */
8047 		i40e_unquiesce_vsi(vsi);
8048 	}
8049 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8050 					    vsi->macvlan_cnt);
8051 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8052 		return ERR_PTR(-EBUSY);
8053 
8054 	/* create the fwd struct */
8055 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8056 	if (!fwd)
8057 		return ERR_PTR(-ENOMEM);
8058 
8059 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8060 	fwd->bit_no = avail_macvlan;
8061 	netdev_set_sb_channel(vdev, avail_macvlan);
8062 	fwd->netdev = vdev;
8063 
8064 	if (!netif_running(netdev))
8065 		return fwd;
8066 
8067 	/* Set fwd ring up */
8068 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8069 	if (ret) {
8070 		/* unbind the queues and drop the subordinate channel config */
8071 		netdev_unbind_sb_channel(netdev, vdev);
8072 		netdev_set_sb_channel(vdev, 0);
8073 
8074 		kfree(fwd);
8075 		return ERR_PTR(-EINVAL);
8076 	}
8077 
8078 	return fwd;
8079 }
8080 
8081 /**
8082  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8083  * @vsi: the VSI we want to access
8084  */
8085 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8086 {
8087 	struct i40e_channel *ch, *ch_tmp;
8088 	struct i40e_pf *pf = vsi->back;
8089 	struct i40e_hw *hw = &pf->hw;
8090 	int aq_err, ret = 0;
8091 
8092 	if (list_empty(&vsi->macvlan_list))
8093 		return;
8094 
8095 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8096 		if (i40e_is_channel_macvlan(ch)) {
8097 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8098 						      i40e_channel_mac(ch),
8099 						      &aq_err);
8100 			if (!ret) {
8101 				/* Reset queue contexts */
8102 				i40e_reset_ch_rings(vsi, ch);
8103 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8104 				netdev_unbind_sb_channel(vsi->netdev,
8105 							 ch->fwd->netdev);
8106 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8107 				kfree(ch->fwd);
8108 				ch->fwd = NULL;
8109 			}
8110 		}
8111 	}
8112 }
8113 
8114 /**
8115  * i40e_fwd_del - delete macvlan interfaces
8116  * @netdev: net device to configure
8117  * @vdev: macvlan netdevice
8118  */
8119 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8120 {
8121 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8122 	struct i40e_fwd_adapter *fwd = vdev;
8123 	struct i40e_channel *ch, *ch_tmp;
8124 	struct i40e_vsi *vsi = np->vsi;
8125 	struct i40e_pf *pf = vsi->back;
8126 	struct i40e_hw *hw = &pf->hw;
8127 	int aq_err, ret = 0;
8128 
8129 	/* Find the channel associated with the macvlan and del mac filter */
8130 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8131 		if (i40e_is_channel_macvlan(ch) &&
8132 		    ether_addr_equal(i40e_channel_mac(ch),
8133 				     fwd->netdev->dev_addr)) {
8134 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8135 						      i40e_channel_mac(ch),
8136 						      &aq_err);
8137 			if (!ret) {
8138 				/* Reset queue contexts */
8139 				i40e_reset_ch_rings(vsi, ch);
8140 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8141 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8142 				netdev_set_sb_channel(fwd->netdev, 0);
8143 				kfree(ch->fwd);
8144 				ch->fwd = NULL;
8145 			} else {
8146 				dev_info(&pf->pdev->dev,
8147 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8148 					  ERR_PTR(ret),
8149 					  i40e_aq_str(hw, aq_err));
8150 			}
8151 			break;
8152 		}
8153 	}
8154 }
8155 
8156 /**
8157  * i40e_setup_tc - configure multiple traffic classes
8158  * @netdev: net device to configure
8159  * @type_data: tc offload data
8160  **/
8161 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8162 {
8163 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8164 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8165 	struct i40e_vsi *vsi = np->vsi;
8166 	struct i40e_pf *pf = vsi->back;
8167 	u8 enabled_tc = 0, num_tc, hw;
8168 	bool need_reset = false;
8169 	int old_queue_pairs;
8170 	int ret = -EINVAL;
8171 	u16 mode;
8172 	int i;
8173 
8174 	old_queue_pairs = vsi->num_queue_pairs;
8175 	num_tc = mqprio_qopt->qopt.num_tc;
8176 	hw = mqprio_qopt->qopt.hw;
8177 	mode = mqprio_qopt->mode;
8178 	if (!hw) {
8179 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8180 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8181 		goto config_tc;
8182 	}
8183 
8184 	/* Check if MFP enabled */
8185 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8186 		netdev_info(netdev,
8187 			    "Configuring TC not supported in MFP mode\n");
8188 		return ret;
8189 	}
8190 	switch (mode) {
8191 	case TC_MQPRIO_MODE_DCB:
8192 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8193 
8194 		/* Check if DCB enabled to continue */
8195 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8196 			netdev_info(netdev,
8197 				    "DCB is not enabled for adapter\n");
8198 			return ret;
8199 		}
8200 
8201 		/* Check whether tc count is within enabled limit */
8202 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8203 			netdev_info(netdev,
8204 				    "TC count greater than enabled on link for adapter\n");
8205 			return ret;
8206 		}
8207 		break;
8208 	case TC_MQPRIO_MODE_CHANNEL:
8209 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8210 			netdev_info(netdev,
8211 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8212 			return ret;
8213 		}
8214 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8215 			return ret;
8216 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8217 		if (ret)
8218 			return ret;
8219 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8220 		       sizeof(*mqprio_qopt));
8221 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8222 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8223 		break;
8224 	default:
8225 		return -EINVAL;
8226 	}
8227 
8228 config_tc:
8229 	/* Generate TC map for number of tc requested */
8230 	for (i = 0; i < num_tc; i++)
8231 		enabled_tc |= BIT(i);
8232 
8233 	/* Requesting same TC configuration as already enabled */
8234 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8235 	    mode != TC_MQPRIO_MODE_CHANNEL)
8236 		return 0;
8237 
8238 	/* Quiesce VSI queues */
8239 	i40e_quiesce_vsi(vsi);
8240 
8241 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8242 		i40e_remove_queue_channels(vsi);
8243 
8244 	/* Configure VSI for enabled TCs */
8245 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8246 	if (ret) {
8247 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8248 			    vsi->seid);
8249 		need_reset = true;
8250 		goto exit;
8251 	} else if (enabled_tc &&
8252 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8253 		netdev_info(netdev,
8254 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8255 			    vsi->tc_config.tc_info[0].qcount);
8256 		ret = -EINVAL;
8257 		need_reset = true;
8258 		goto exit;
8259 	}
8260 
8261 	dev_info(&vsi->back->pdev->dev,
8262 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8263 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8264 
8265 	if (i40e_is_tc_mqprio_enabled(pf)) {
8266 		if (vsi->mqprio_qopt.max_rate[0]) {
8267 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8268 						  vsi->mqprio_qopt.max_rate[0]);
8269 
8270 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8271 			if (!ret) {
8272 				u64 credits = max_tx_rate;
8273 
8274 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8275 				dev_dbg(&vsi->back->pdev->dev,
8276 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8277 					max_tx_rate,
8278 					credits,
8279 					vsi->seid);
8280 			} else {
8281 				need_reset = true;
8282 				goto exit;
8283 			}
8284 		}
8285 		ret = i40e_configure_queue_channels(vsi);
8286 		if (ret) {
8287 			vsi->num_queue_pairs = old_queue_pairs;
8288 			netdev_info(netdev,
8289 				    "Failed configuring queue channels\n");
8290 			need_reset = true;
8291 			goto exit;
8292 		}
8293 	}
8294 
8295 exit:
8296 	/* Reset the configuration data to defaults, only TC0 is enabled */
8297 	if (need_reset) {
8298 		i40e_vsi_set_default_tc_config(vsi);
8299 		need_reset = false;
8300 	}
8301 
8302 	/* Unquiesce VSI */
8303 	i40e_unquiesce_vsi(vsi);
8304 	return ret;
8305 }
8306 
8307 /**
8308  * i40e_set_cld_element - sets cloud filter element data
8309  * @filter: cloud filter rule
8310  * @cld: ptr to cloud filter element data
8311  *
8312  * This is helper function to copy data into cloud filter element
8313  **/
8314 static inline void
8315 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8316 		     struct i40e_aqc_cloud_filters_element_data *cld)
8317 {
8318 	u32 ipa;
8319 	int i;
8320 
8321 	memset(cld, 0, sizeof(*cld));
8322 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8323 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8324 
8325 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8326 		return;
8327 
8328 	if (filter->n_proto == ETH_P_IPV6) {
8329 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8330 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8331 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8332 
8333 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8334 		}
8335 	} else {
8336 		ipa = be32_to_cpu(filter->dst_ipv4);
8337 
8338 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8339 	}
8340 
8341 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8342 
8343 	/* tenant_id is not supported by FW now, once the support is enabled
8344 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8345 	 */
8346 	if (filter->tenant_id)
8347 		return;
8348 }
8349 
8350 /**
8351  * i40e_add_del_cloud_filter - Add/del cloud filter
8352  * @vsi: pointer to VSI
8353  * @filter: cloud filter rule
8354  * @add: if true, add, if false, delete
8355  *
8356  * Add or delete a cloud filter for a specific flow spec.
8357  * Returns 0 if the filter were successfully added.
8358  **/
8359 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8360 			      struct i40e_cloud_filter *filter, bool add)
8361 {
8362 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8363 	struct i40e_pf *pf = vsi->back;
8364 	int ret;
8365 	static const u16 flag_table[128] = {
8366 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8367 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8368 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8369 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8370 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8371 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8372 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8373 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8374 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8375 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8376 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8377 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8378 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8379 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8380 	};
8381 
8382 	if (filter->flags >= ARRAY_SIZE(flag_table))
8383 		return -EIO;
8384 
8385 	memset(&cld_filter, 0, sizeof(cld_filter));
8386 
8387 	/* copy element needed to add cloud filter from filter */
8388 	i40e_set_cld_element(filter, &cld_filter);
8389 
8390 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8391 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8392 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8393 
8394 	if (filter->n_proto == ETH_P_IPV6)
8395 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8396 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8397 	else
8398 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8399 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8400 
8401 	if (add)
8402 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8403 						&cld_filter, 1);
8404 	else
8405 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8406 						&cld_filter, 1);
8407 	if (ret)
8408 		dev_dbg(&pf->pdev->dev,
8409 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8410 			add ? "add" : "delete", filter->dst_port, ret,
8411 			pf->hw.aq.asq_last_status);
8412 	else
8413 		dev_info(&pf->pdev->dev,
8414 			 "%s cloud filter for VSI: %d\n",
8415 			 add ? "Added" : "Deleted", filter->seid);
8416 	return ret;
8417 }
8418 
8419 /**
8420  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8421  * @vsi: pointer to VSI
8422  * @filter: cloud filter rule
8423  * @add: if true, add, if false, delete
8424  *
8425  * Add or delete a cloud filter for a specific flow spec using big buffer.
8426  * Returns 0 if the filter were successfully added.
8427  **/
8428 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8429 				      struct i40e_cloud_filter *filter,
8430 				      bool add)
8431 {
8432 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8433 	struct i40e_pf *pf = vsi->back;
8434 	int ret;
8435 
8436 	/* Both (src/dst) valid mac_addr are not supported */
8437 	if ((is_valid_ether_addr(filter->dst_mac) &&
8438 	     is_valid_ether_addr(filter->src_mac)) ||
8439 	    (is_multicast_ether_addr(filter->dst_mac) &&
8440 	     is_multicast_ether_addr(filter->src_mac)))
8441 		return -EOPNOTSUPP;
8442 
8443 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8444 	 * ports are not supported via big buffer now.
8445 	 */
8446 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8447 		return -EOPNOTSUPP;
8448 
8449 	/* adding filter using src_port/src_ip is not supported at this stage */
8450 	if (filter->src_port ||
8451 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8452 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8453 		return -EOPNOTSUPP;
8454 
8455 	memset(&cld_filter, 0, sizeof(cld_filter));
8456 
8457 	/* copy element needed to add cloud filter from filter */
8458 	i40e_set_cld_element(filter, &cld_filter.element);
8459 
8460 	if (is_valid_ether_addr(filter->dst_mac) ||
8461 	    is_valid_ether_addr(filter->src_mac) ||
8462 	    is_multicast_ether_addr(filter->dst_mac) ||
8463 	    is_multicast_ether_addr(filter->src_mac)) {
8464 		/* MAC + IP : unsupported mode */
8465 		if (filter->dst_ipv4)
8466 			return -EOPNOTSUPP;
8467 
8468 		/* since we validated that L4 port must be valid before
8469 		 * we get here, start with respective "flags" value
8470 		 * and update if vlan is present or not
8471 		 */
8472 		cld_filter.element.flags =
8473 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8474 
8475 		if (filter->vlan_id) {
8476 			cld_filter.element.flags =
8477 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8478 		}
8479 
8480 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8481 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8482 		cld_filter.element.flags =
8483 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8484 		if (filter->n_proto == ETH_P_IPV6)
8485 			cld_filter.element.flags |=
8486 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8487 		else
8488 			cld_filter.element.flags |=
8489 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8490 	} else {
8491 		dev_err(&pf->pdev->dev,
8492 			"either mac or ip has to be valid for cloud filter\n");
8493 		return -EINVAL;
8494 	}
8495 
8496 	/* Now copy L4 port in Byte 6..7 in general fields */
8497 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8498 						be16_to_cpu(filter->dst_port);
8499 
8500 	if (add) {
8501 		/* Validate current device switch mode, change if necessary */
8502 		ret = i40e_validate_and_set_switch_mode(vsi);
8503 		if (ret) {
8504 			dev_err(&pf->pdev->dev,
8505 				"failed to set switch mode, ret %d\n",
8506 				ret);
8507 			return ret;
8508 		}
8509 
8510 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8511 						   &cld_filter, 1);
8512 	} else {
8513 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8514 						   &cld_filter, 1);
8515 	}
8516 
8517 	if (ret)
8518 		dev_dbg(&pf->pdev->dev,
8519 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8520 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8521 	else
8522 		dev_info(&pf->pdev->dev,
8523 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8524 			 add ? "add" : "delete", filter->seid,
8525 			 ntohs(filter->dst_port));
8526 	return ret;
8527 }
8528 
8529 /**
8530  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8531  * @vsi: Pointer to VSI
8532  * @f: Pointer to struct flow_cls_offload
8533  * @filter: Pointer to cloud filter structure
8534  *
8535  **/
8536 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8537 				 struct flow_cls_offload *f,
8538 				 struct i40e_cloud_filter *filter)
8539 {
8540 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8541 	struct flow_dissector *dissector = rule->match.dissector;
8542 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8543 	struct i40e_pf *pf = vsi->back;
8544 	u8 field_flags = 0;
8545 
8546 	if (dissector->used_keys &
8547 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8548 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8549 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8550 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8551 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8552 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8553 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8554 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8555 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8556 			dissector->used_keys);
8557 		return -EOPNOTSUPP;
8558 	}
8559 
8560 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8561 		struct flow_match_enc_keyid match;
8562 
8563 		flow_rule_match_enc_keyid(rule, &match);
8564 		if (match.mask->keyid != 0)
8565 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8566 
8567 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8568 	}
8569 
8570 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8571 		struct flow_match_basic match;
8572 
8573 		flow_rule_match_basic(rule, &match);
8574 		n_proto_key = ntohs(match.key->n_proto);
8575 		n_proto_mask = ntohs(match.mask->n_proto);
8576 
8577 		if (n_proto_key == ETH_P_ALL) {
8578 			n_proto_key = 0;
8579 			n_proto_mask = 0;
8580 		}
8581 		filter->n_proto = n_proto_key & n_proto_mask;
8582 		filter->ip_proto = match.key->ip_proto;
8583 	}
8584 
8585 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8586 		struct flow_match_eth_addrs match;
8587 
8588 		flow_rule_match_eth_addrs(rule, &match);
8589 
8590 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8591 		if (!is_zero_ether_addr(match.mask->dst)) {
8592 			if (is_broadcast_ether_addr(match.mask->dst)) {
8593 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8594 			} else {
8595 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8596 					match.mask->dst);
8597 				return -EIO;
8598 			}
8599 		}
8600 
8601 		if (!is_zero_ether_addr(match.mask->src)) {
8602 			if (is_broadcast_ether_addr(match.mask->src)) {
8603 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8604 			} else {
8605 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8606 					match.mask->src);
8607 				return -EIO;
8608 			}
8609 		}
8610 		ether_addr_copy(filter->dst_mac, match.key->dst);
8611 		ether_addr_copy(filter->src_mac, match.key->src);
8612 	}
8613 
8614 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8615 		struct flow_match_vlan match;
8616 
8617 		flow_rule_match_vlan(rule, &match);
8618 		if (match.mask->vlan_id) {
8619 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8620 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8621 
8622 			} else {
8623 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8624 					match.mask->vlan_id);
8625 				return -EIO;
8626 			}
8627 		}
8628 
8629 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8630 	}
8631 
8632 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8633 		struct flow_match_control match;
8634 
8635 		flow_rule_match_control(rule, &match);
8636 		addr_type = match.key->addr_type;
8637 	}
8638 
8639 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8640 		struct flow_match_ipv4_addrs match;
8641 
8642 		flow_rule_match_ipv4_addrs(rule, &match);
8643 		if (match.mask->dst) {
8644 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8645 				field_flags |= I40E_CLOUD_FIELD_IIP;
8646 			} else {
8647 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8648 					&match.mask->dst);
8649 				return -EIO;
8650 			}
8651 		}
8652 
8653 		if (match.mask->src) {
8654 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8655 				field_flags |= I40E_CLOUD_FIELD_IIP;
8656 			} else {
8657 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8658 					&match.mask->src);
8659 				return -EIO;
8660 			}
8661 		}
8662 
8663 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8664 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8665 			return -EIO;
8666 		}
8667 		filter->dst_ipv4 = match.key->dst;
8668 		filter->src_ipv4 = match.key->src;
8669 	}
8670 
8671 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8672 		struct flow_match_ipv6_addrs match;
8673 
8674 		flow_rule_match_ipv6_addrs(rule, &match);
8675 
8676 		/* src and dest IPV6 address should not be LOOPBACK
8677 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8678 		 */
8679 		if (ipv6_addr_loopback(&match.key->dst) ||
8680 		    ipv6_addr_loopback(&match.key->src)) {
8681 			dev_err(&pf->pdev->dev,
8682 				"Bad ipv6, addr is LOOPBACK\n");
8683 			return -EIO;
8684 		}
8685 		if (!ipv6_addr_any(&match.mask->dst) ||
8686 		    !ipv6_addr_any(&match.mask->src))
8687 			field_flags |= I40E_CLOUD_FIELD_IIP;
8688 
8689 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8690 		       sizeof(filter->src_ipv6));
8691 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8692 		       sizeof(filter->dst_ipv6));
8693 	}
8694 
8695 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8696 		struct flow_match_ports match;
8697 
8698 		flow_rule_match_ports(rule, &match);
8699 		if (match.mask->src) {
8700 			if (match.mask->src == cpu_to_be16(0xffff)) {
8701 				field_flags |= I40E_CLOUD_FIELD_IIP;
8702 			} else {
8703 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8704 					be16_to_cpu(match.mask->src));
8705 				return -EIO;
8706 			}
8707 		}
8708 
8709 		if (match.mask->dst) {
8710 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8711 				field_flags |= I40E_CLOUD_FIELD_IIP;
8712 			} else {
8713 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8714 					be16_to_cpu(match.mask->dst));
8715 				return -EIO;
8716 			}
8717 		}
8718 
8719 		filter->dst_port = match.key->dst;
8720 		filter->src_port = match.key->src;
8721 
8722 		switch (filter->ip_proto) {
8723 		case IPPROTO_TCP:
8724 		case IPPROTO_UDP:
8725 			break;
8726 		default:
8727 			dev_err(&pf->pdev->dev,
8728 				"Only UDP and TCP transport are supported\n");
8729 			return -EINVAL;
8730 		}
8731 	}
8732 	filter->flags = field_flags;
8733 	return 0;
8734 }
8735 
8736 /**
8737  * i40e_handle_tclass: Forward to a traffic class on the device
8738  * @vsi: Pointer to VSI
8739  * @tc: traffic class index on the device
8740  * @filter: Pointer to cloud filter structure
8741  *
8742  **/
8743 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8744 			      struct i40e_cloud_filter *filter)
8745 {
8746 	struct i40e_channel *ch, *ch_tmp;
8747 
8748 	/* direct to a traffic class on the same device */
8749 	if (tc == 0) {
8750 		filter->seid = vsi->seid;
8751 		return 0;
8752 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8753 		if (!filter->dst_port) {
8754 			dev_err(&vsi->back->pdev->dev,
8755 				"Specify destination port to direct to traffic class that is not default\n");
8756 			return -EINVAL;
8757 		}
8758 		if (list_empty(&vsi->ch_list))
8759 			return -EINVAL;
8760 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8761 					 list) {
8762 			if (ch->seid == vsi->tc_seid_map[tc])
8763 				filter->seid = ch->seid;
8764 		}
8765 		return 0;
8766 	}
8767 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8768 	return -EINVAL;
8769 }
8770 
8771 /**
8772  * i40e_configure_clsflower - Configure tc flower filters
8773  * @vsi: Pointer to VSI
8774  * @cls_flower: Pointer to struct flow_cls_offload
8775  *
8776  **/
8777 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8778 				    struct flow_cls_offload *cls_flower)
8779 {
8780 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8781 	struct i40e_cloud_filter *filter = NULL;
8782 	struct i40e_pf *pf = vsi->back;
8783 	int err = 0;
8784 
8785 	if (tc < 0) {
8786 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8787 		return -EOPNOTSUPP;
8788 	}
8789 
8790 	if (!tc) {
8791 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8792 		return -EINVAL;
8793 	}
8794 
8795 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8796 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8797 		return -EBUSY;
8798 
8799 	if (pf->fdir_pf_active_filters ||
8800 	    (!hlist_empty(&pf->fdir_filter_list))) {
8801 		dev_err(&vsi->back->pdev->dev,
8802 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8803 		return -EINVAL;
8804 	}
8805 
8806 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8807 		dev_err(&vsi->back->pdev->dev,
8808 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8809 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8810 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8811 	}
8812 
8813 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8814 	if (!filter)
8815 		return -ENOMEM;
8816 
8817 	filter->cookie = cls_flower->cookie;
8818 
8819 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8820 	if (err < 0)
8821 		goto err;
8822 
8823 	err = i40e_handle_tclass(vsi, tc, filter);
8824 	if (err < 0)
8825 		goto err;
8826 
8827 	/* Add cloud filter */
8828 	if (filter->dst_port)
8829 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8830 	else
8831 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8832 
8833 	if (err) {
8834 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8835 			err);
8836 		goto err;
8837 	}
8838 
8839 	/* add filter to the ordered list */
8840 	INIT_HLIST_NODE(&filter->cloud_node);
8841 
8842 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8843 
8844 	pf->num_cloud_filters++;
8845 
8846 	return err;
8847 err:
8848 	kfree(filter);
8849 	return err;
8850 }
8851 
8852 /**
8853  * i40e_find_cloud_filter - Find the could filter in the list
8854  * @vsi: Pointer to VSI
8855  * @cookie: filter specific cookie
8856  *
8857  **/
8858 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8859 							unsigned long *cookie)
8860 {
8861 	struct i40e_cloud_filter *filter = NULL;
8862 	struct hlist_node *node2;
8863 
8864 	hlist_for_each_entry_safe(filter, node2,
8865 				  &vsi->back->cloud_filter_list, cloud_node)
8866 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8867 			return filter;
8868 	return NULL;
8869 }
8870 
8871 /**
8872  * i40e_delete_clsflower - Remove tc flower filters
8873  * @vsi: Pointer to VSI
8874  * @cls_flower: Pointer to struct flow_cls_offload
8875  *
8876  **/
8877 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8878 				 struct flow_cls_offload *cls_flower)
8879 {
8880 	struct i40e_cloud_filter *filter = NULL;
8881 	struct i40e_pf *pf = vsi->back;
8882 	int err = 0;
8883 
8884 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8885 
8886 	if (!filter)
8887 		return -EINVAL;
8888 
8889 	hash_del(&filter->cloud_node);
8890 
8891 	if (filter->dst_port)
8892 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8893 	else
8894 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8895 
8896 	kfree(filter);
8897 	if (err) {
8898 		dev_err(&pf->pdev->dev,
8899 			"Failed to delete cloud filter, err %pe\n",
8900 			ERR_PTR(err));
8901 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8902 	}
8903 
8904 	pf->num_cloud_filters--;
8905 	if (!pf->num_cloud_filters)
8906 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8907 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8908 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8909 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8910 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8911 		}
8912 	return 0;
8913 }
8914 
8915 /**
8916  * i40e_setup_tc_cls_flower - flower classifier offloads
8917  * @np: net device to configure
8918  * @cls_flower: offload data
8919  **/
8920 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8921 				    struct flow_cls_offload *cls_flower)
8922 {
8923 	struct i40e_vsi *vsi = np->vsi;
8924 
8925 	switch (cls_flower->command) {
8926 	case FLOW_CLS_REPLACE:
8927 		return i40e_configure_clsflower(vsi, cls_flower);
8928 	case FLOW_CLS_DESTROY:
8929 		return i40e_delete_clsflower(vsi, cls_flower);
8930 	case FLOW_CLS_STATS:
8931 		return -EOPNOTSUPP;
8932 	default:
8933 		return -EOPNOTSUPP;
8934 	}
8935 }
8936 
8937 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8938 				  void *cb_priv)
8939 {
8940 	struct i40e_netdev_priv *np = cb_priv;
8941 
8942 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8943 		return -EOPNOTSUPP;
8944 
8945 	switch (type) {
8946 	case TC_SETUP_CLSFLOWER:
8947 		return i40e_setup_tc_cls_flower(np, type_data);
8948 
8949 	default:
8950 		return -EOPNOTSUPP;
8951 	}
8952 }
8953 
8954 static LIST_HEAD(i40e_block_cb_list);
8955 
8956 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8957 			   void *type_data)
8958 {
8959 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8960 
8961 	switch (type) {
8962 	case TC_SETUP_QDISC_MQPRIO:
8963 		return i40e_setup_tc(netdev, type_data);
8964 	case TC_SETUP_BLOCK:
8965 		return flow_block_cb_setup_simple(type_data,
8966 						  &i40e_block_cb_list,
8967 						  i40e_setup_tc_block_cb,
8968 						  np, np, true);
8969 	default:
8970 		return -EOPNOTSUPP;
8971 	}
8972 }
8973 
8974 /**
8975  * i40e_open - Called when a network interface is made active
8976  * @netdev: network interface device structure
8977  *
8978  * The open entry point is called when a network interface is made
8979  * active by the system (IFF_UP).  At this point all resources needed
8980  * for transmit and receive operations are allocated, the interrupt
8981  * handler is registered with the OS, the netdev watchdog subtask is
8982  * enabled, and the stack is notified that the interface is ready.
8983  *
8984  * Returns 0 on success, negative value on failure
8985  **/
8986 int i40e_open(struct net_device *netdev)
8987 {
8988 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8989 	struct i40e_vsi *vsi = np->vsi;
8990 	struct i40e_pf *pf = vsi->back;
8991 	int err;
8992 
8993 	/* disallow open during test or if eeprom is broken */
8994 	if (test_bit(__I40E_TESTING, pf->state) ||
8995 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8996 		return -EBUSY;
8997 
8998 	netif_carrier_off(netdev);
8999 
9000 	if (i40e_force_link_state(pf, true))
9001 		return -EAGAIN;
9002 
9003 	err = i40e_vsi_open(vsi);
9004 	if (err)
9005 		return err;
9006 
9007 	/* configure global TSO hardware offload settings */
9008 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9009 						       TCP_FLAG_FIN) >> 16);
9010 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9011 						       TCP_FLAG_FIN |
9012 						       TCP_FLAG_CWR) >> 16);
9013 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9014 	udp_tunnel_get_rx_info(netdev);
9015 
9016 	return 0;
9017 }
9018 
9019 /**
9020  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9021  * @vsi: vsi structure
9022  *
9023  * This updates netdev's number of tx/rx queues
9024  *
9025  * Returns status of setting tx/rx queues
9026  **/
9027 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9028 {
9029 	int ret;
9030 
9031 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9032 					   vsi->num_queue_pairs);
9033 	if (ret)
9034 		return ret;
9035 
9036 	return netif_set_real_num_tx_queues(vsi->netdev,
9037 					    vsi->num_queue_pairs);
9038 }
9039 
9040 /**
9041  * i40e_vsi_open -
9042  * @vsi: the VSI to open
9043  *
9044  * Finish initialization of the VSI.
9045  *
9046  * Returns 0 on success, negative value on failure
9047  *
9048  * Note: expects to be called while under rtnl_lock()
9049  **/
9050 int i40e_vsi_open(struct i40e_vsi *vsi)
9051 {
9052 	struct i40e_pf *pf = vsi->back;
9053 	char int_name[I40E_INT_NAME_STR_LEN];
9054 	int err;
9055 
9056 	/* allocate descriptors */
9057 	err = i40e_vsi_setup_tx_resources(vsi);
9058 	if (err)
9059 		goto err_setup_tx;
9060 	err = i40e_vsi_setup_rx_resources(vsi);
9061 	if (err)
9062 		goto err_setup_rx;
9063 
9064 	err = i40e_vsi_configure(vsi);
9065 	if (err)
9066 		goto err_setup_rx;
9067 
9068 	if (vsi->netdev) {
9069 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9070 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9071 		err = i40e_vsi_request_irq(vsi, int_name);
9072 		if (err)
9073 			goto err_setup_rx;
9074 
9075 		/* Notify the stack of the actual queue counts. */
9076 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9077 		if (err)
9078 			goto err_set_queues;
9079 
9080 	} else if (vsi->type == I40E_VSI_FDIR) {
9081 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9082 			 dev_driver_string(&pf->pdev->dev),
9083 			 dev_name(&pf->pdev->dev));
9084 		err = i40e_vsi_request_irq(vsi, int_name);
9085 		if (err)
9086 			goto err_setup_rx;
9087 
9088 	} else {
9089 		err = -EINVAL;
9090 		goto err_setup_rx;
9091 	}
9092 
9093 	err = i40e_up_complete(vsi);
9094 	if (err)
9095 		goto err_up_complete;
9096 
9097 	return 0;
9098 
9099 err_up_complete:
9100 	i40e_down(vsi);
9101 err_set_queues:
9102 	i40e_vsi_free_irq(vsi);
9103 err_setup_rx:
9104 	i40e_vsi_free_rx_resources(vsi);
9105 err_setup_tx:
9106 	i40e_vsi_free_tx_resources(vsi);
9107 	if (vsi == pf->vsi[pf->lan_vsi])
9108 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9109 
9110 	return err;
9111 }
9112 
9113 /**
9114  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9115  * @pf: Pointer to PF
9116  *
9117  * This function destroys the hlist where all the Flow Director
9118  * filters were saved.
9119  **/
9120 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9121 {
9122 	struct i40e_fdir_filter *filter;
9123 	struct i40e_flex_pit *pit_entry, *tmp;
9124 	struct hlist_node *node2;
9125 
9126 	hlist_for_each_entry_safe(filter, node2,
9127 				  &pf->fdir_filter_list, fdir_node) {
9128 		hlist_del(&filter->fdir_node);
9129 		kfree(filter);
9130 	}
9131 
9132 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9133 		list_del(&pit_entry->list);
9134 		kfree(pit_entry);
9135 	}
9136 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9137 
9138 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9139 		list_del(&pit_entry->list);
9140 		kfree(pit_entry);
9141 	}
9142 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9143 
9144 	pf->fdir_pf_active_filters = 0;
9145 	i40e_reset_fdir_filter_cnt(pf);
9146 
9147 	/* Reprogram the default input set for TCP/IPv4 */
9148 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9149 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9150 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9151 
9152 	/* Reprogram the default input set for TCP/IPv6 */
9153 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9154 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9155 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9156 
9157 	/* Reprogram the default input set for UDP/IPv4 */
9158 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9159 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9160 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9161 
9162 	/* Reprogram the default input set for UDP/IPv6 */
9163 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9164 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9165 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9166 
9167 	/* Reprogram the default input set for SCTP/IPv4 */
9168 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9169 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9170 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9171 
9172 	/* Reprogram the default input set for SCTP/IPv6 */
9173 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9174 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9175 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9176 
9177 	/* Reprogram the default input set for Other/IPv4 */
9178 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9179 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9180 
9181 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9182 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9183 
9184 	/* Reprogram the default input set for Other/IPv6 */
9185 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9186 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9187 
9188 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9189 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9190 }
9191 
9192 /**
9193  * i40e_cloud_filter_exit - Cleans up the cloud filters
9194  * @pf: Pointer to PF
9195  *
9196  * This function destroys the hlist where all the cloud filters
9197  * were saved.
9198  **/
9199 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9200 {
9201 	struct i40e_cloud_filter *cfilter;
9202 	struct hlist_node *node;
9203 
9204 	hlist_for_each_entry_safe(cfilter, node,
9205 				  &pf->cloud_filter_list, cloud_node) {
9206 		hlist_del(&cfilter->cloud_node);
9207 		kfree(cfilter);
9208 	}
9209 	pf->num_cloud_filters = 0;
9210 
9211 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9212 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9213 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9214 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9215 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9216 	}
9217 }
9218 
9219 /**
9220  * i40e_close - Disables a network interface
9221  * @netdev: network interface device structure
9222  *
9223  * The close entry point is called when an interface is de-activated
9224  * by the OS.  The hardware is still under the driver's control, but
9225  * this netdev interface is disabled.
9226  *
9227  * Returns 0, this is not allowed to fail
9228  **/
9229 int i40e_close(struct net_device *netdev)
9230 {
9231 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9232 	struct i40e_vsi *vsi = np->vsi;
9233 
9234 	i40e_vsi_close(vsi);
9235 
9236 	return 0;
9237 }
9238 
9239 /**
9240  * i40e_do_reset - Start a PF or Core Reset sequence
9241  * @pf: board private structure
9242  * @reset_flags: which reset is requested
9243  * @lock_acquired: indicates whether or not the lock has been acquired
9244  * before this function was called.
9245  *
9246  * The essential difference in resets is that the PF Reset
9247  * doesn't clear the packet buffers, doesn't reset the PE
9248  * firmware, and doesn't bother the other PFs on the chip.
9249  **/
9250 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9251 {
9252 	struct i40e_vsi *vsi;
9253 	u32 val;
9254 	int i;
9255 
9256 	/* do the biggest reset indicated */
9257 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9258 
9259 		/* Request a Global Reset
9260 		 *
9261 		 * This will start the chip's countdown to the actual full
9262 		 * chip reset event, and a warning interrupt to be sent
9263 		 * to all PFs, including the requestor.  Our handler
9264 		 * for the warning interrupt will deal with the shutdown
9265 		 * and recovery of the switch setup.
9266 		 */
9267 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9268 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9269 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9270 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9271 
9272 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9273 
9274 		/* Request a Core Reset
9275 		 *
9276 		 * Same as Global Reset, except does *not* include the MAC/PHY
9277 		 */
9278 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9279 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9280 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9281 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9282 		i40e_flush(&pf->hw);
9283 
9284 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9285 
9286 		/* Request a PF Reset
9287 		 *
9288 		 * Resets only the PF-specific registers
9289 		 *
9290 		 * This goes directly to the tear-down and rebuild of
9291 		 * the switch, since we need to do all the recovery as
9292 		 * for the Core Reset.
9293 		 */
9294 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9295 		i40e_handle_reset_warning(pf, lock_acquired);
9296 
9297 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9298 		/* Request a PF Reset
9299 		 *
9300 		 * Resets PF and reinitializes PFs VSI.
9301 		 */
9302 		i40e_prep_for_reset(pf);
9303 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9304 		dev_info(&pf->pdev->dev,
9305 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9306 			 "FW LLDP is disabled\n" :
9307 			 "FW LLDP is enabled\n");
9308 
9309 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9310 		/* Find the VSI(s) that requested a re-init */
9311 		dev_info(&pf->pdev->dev, "VSI reinit requested\n");
9312 
9313 		i40e_pf_for_each_vsi(pf, i, vsi) {
9314 			if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9315 					       vsi->state))
9316 				i40e_vsi_reinit_locked(vsi);
9317 		}
9318 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9319 		/* Find the VSI(s) that needs to be brought down */
9320 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9321 
9322 		i40e_pf_for_each_vsi(pf, i, vsi) {
9323 			if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9324 					       vsi->state)) {
9325 				set_bit(__I40E_VSI_DOWN, vsi->state);
9326 				i40e_down(vsi);
9327 			}
9328 		}
9329 	} else {
9330 		dev_info(&pf->pdev->dev,
9331 			 "bad reset request 0x%08x\n", reset_flags);
9332 	}
9333 }
9334 
9335 #ifdef CONFIG_I40E_DCB
9336 /**
9337  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9338  * @pf: board private structure
9339  * @old_cfg: current DCB config
9340  * @new_cfg: new DCB config
9341  **/
9342 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9343 			    struct i40e_dcbx_config *old_cfg,
9344 			    struct i40e_dcbx_config *new_cfg)
9345 {
9346 	bool need_reconfig = false;
9347 
9348 	/* Check if ETS configuration has changed */
9349 	if (memcmp(&new_cfg->etscfg,
9350 		   &old_cfg->etscfg,
9351 		   sizeof(new_cfg->etscfg))) {
9352 		/* If Priority Table has changed reconfig is needed */
9353 		if (memcmp(&new_cfg->etscfg.prioritytable,
9354 			   &old_cfg->etscfg.prioritytable,
9355 			   sizeof(new_cfg->etscfg.prioritytable))) {
9356 			need_reconfig = true;
9357 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9358 		}
9359 
9360 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9361 			   &old_cfg->etscfg.tcbwtable,
9362 			   sizeof(new_cfg->etscfg.tcbwtable)))
9363 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9364 
9365 		if (memcmp(&new_cfg->etscfg.tsatable,
9366 			   &old_cfg->etscfg.tsatable,
9367 			   sizeof(new_cfg->etscfg.tsatable)))
9368 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9369 	}
9370 
9371 	/* Check if PFC configuration has changed */
9372 	if (memcmp(&new_cfg->pfc,
9373 		   &old_cfg->pfc,
9374 		   sizeof(new_cfg->pfc))) {
9375 		need_reconfig = true;
9376 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9377 	}
9378 
9379 	/* Check if APP Table has changed */
9380 	if (memcmp(&new_cfg->app,
9381 		   &old_cfg->app,
9382 		   sizeof(new_cfg->app))) {
9383 		need_reconfig = true;
9384 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9385 	}
9386 
9387 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9388 	return need_reconfig;
9389 }
9390 
9391 /**
9392  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9393  * @pf: board private structure
9394  * @e: event info posted on ARQ
9395  **/
9396 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9397 				  struct i40e_arq_event_info *e)
9398 {
9399 	struct i40e_aqc_lldp_get_mib *mib =
9400 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9401 	struct i40e_hw *hw = &pf->hw;
9402 	struct i40e_dcbx_config tmp_dcbx_cfg;
9403 	bool need_reconfig = false;
9404 	int ret = 0;
9405 	u8 type;
9406 
9407 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9408 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9409 	    (hw->phy.link_info.link_speed &
9410 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9411 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9412 		/* let firmware decide if the DCB should be disabled */
9413 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9414 
9415 	/* Not DCB capable or capability disabled */
9416 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9417 		return ret;
9418 
9419 	/* Ignore if event is not for Nearest Bridge */
9420 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9421 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9422 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9423 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9424 		return ret;
9425 
9426 	/* Check MIB Type and return if event for Remote MIB update */
9427 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9428 	dev_dbg(&pf->pdev->dev,
9429 		"LLDP event mib type %s\n", type ? "remote" : "local");
9430 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9431 		/* Update the remote cached instance and return */
9432 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9433 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9434 				&hw->remote_dcbx_config);
9435 		goto exit;
9436 	}
9437 
9438 	/* Store the old configuration */
9439 	tmp_dcbx_cfg = hw->local_dcbx_config;
9440 
9441 	/* Reset the old DCBx configuration data */
9442 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9443 	/* Get updated DCBX data from firmware */
9444 	ret = i40e_get_dcb_config(&pf->hw);
9445 	if (ret) {
9446 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9447 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9448 		    (hw->phy.link_info.link_speed &
9449 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9450 			dev_warn(&pf->pdev->dev,
9451 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9452 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9453 		} else {
9454 			dev_info(&pf->pdev->dev,
9455 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9456 				 ERR_PTR(ret),
9457 				 i40e_aq_str(&pf->hw,
9458 					     pf->hw.aq.asq_last_status));
9459 		}
9460 		goto exit;
9461 	}
9462 
9463 	/* No change detected in DCBX configs */
9464 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9465 		    sizeof(tmp_dcbx_cfg))) {
9466 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9467 		goto exit;
9468 	}
9469 
9470 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9471 					       &hw->local_dcbx_config);
9472 
9473 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9474 
9475 	if (!need_reconfig)
9476 		goto exit;
9477 
9478 	/* Enable DCB tagging only when more than one TC */
9479 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9480 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9481 	else
9482 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9483 
9484 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9485 	/* Reconfiguration needed quiesce all VSIs */
9486 	i40e_pf_quiesce_all_vsi(pf);
9487 
9488 	/* Changes in configuration update VEB/VSI */
9489 	i40e_dcb_reconfigure(pf);
9490 
9491 	ret = i40e_resume_port_tx(pf);
9492 
9493 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9494 	/* In case of error no point in resuming VSIs */
9495 	if (ret)
9496 		goto exit;
9497 
9498 	/* Wait for the PF's queues to be disabled */
9499 	ret = i40e_pf_wait_queues_disabled(pf);
9500 	if (ret) {
9501 		/* Schedule PF reset to recover */
9502 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9503 		i40e_service_event_schedule(pf);
9504 	} else {
9505 		i40e_pf_unquiesce_all_vsi(pf);
9506 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9507 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9508 	}
9509 
9510 exit:
9511 	return ret;
9512 }
9513 #endif /* CONFIG_I40E_DCB */
9514 
9515 /**
9516  * i40e_do_reset_safe - Protected reset path for userland calls.
9517  * @pf: board private structure
9518  * @reset_flags: which reset is requested
9519  *
9520  **/
9521 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9522 {
9523 	rtnl_lock();
9524 	i40e_do_reset(pf, reset_flags, true);
9525 	rtnl_unlock();
9526 }
9527 
9528 /**
9529  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9530  * @pf: board private structure
9531  * @e: event info posted on ARQ
9532  *
9533  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9534  * and VF queues
9535  **/
9536 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9537 					   struct i40e_arq_event_info *e)
9538 {
9539 	struct i40e_aqc_lan_overflow *data =
9540 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9541 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9542 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9543 	struct i40e_hw *hw = &pf->hw;
9544 	struct i40e_vf *vf;
9545 	u16 vf_id;
9546 
9547 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9548 		queue, qtx_ctl);
9549 
9550 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9551 	    I40E_QTX_CTL_VF_QUEUE)
9552 		return;
9553 
9554 	/* Queue belongs to VF, find the VF and issue VF reset */
9555 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9556 	vf_id -= hw->func_caps.vf_base_id;
9557 	vf = &pf->vf[vf_id];
9558 	i40e_vc_notify_vf_reset(vf);
9559 	/* Allow VF to process pending reset notification */
9560 	msleep(20);
9561 	i40e_reset_vf(vf, false);
9562 }
9563 
9564 /**
9565  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9566  * @pf: board private structure
9567  **/
9568 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9569 {
9570 	u32 val, fcnt_prog;
9571 
9572 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9573 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9574 	return fcnt_prog;
9575 }
9576 
9577 /**
9578  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9579  * @pf: board private structure
9580  **/
9581 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9582 {
9583 	u32 val, fcnt_prog;
9584 
9585 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9586 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9587 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9588 	return fcnt_prog;
9589 }
9590 
9591 /**
9592  * i40e_get_global_fd_count - Get total FD filters programmed on device
9593  * @pf: board private structure
9594  **/
9595 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9596 {
9597 	u32 val, fcnt_prog;
9598 
9599 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9600 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9601 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9602 	return fcnt_prog;
9603 }
9604 
9605 /**
9606  * i40e_reenable_fdir_sb - Restore FDir SB capability
9607  * @pf: board private structure
9608  **/
9609 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9610 {
9611 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9612 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9613 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9614 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9615 }
9616 
9617 /**
9618  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9619  * @pf: board private structure
9620  **/
9621 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9622 {
9623 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9624 		/* ATR uses the same filtering logic as SB rules. It only
9625 		 * functions properly if the input set mask is at the default
9626 		 * settings. It is safe to restore the default input set
9627 		 * because there are no active TCPv4 filter rules.
9628 		 */
9629 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9630 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9631 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9632 
9633 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9634 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9635 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9636 	}
9637 }
9638 
9639 /**
9640  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9641  * @pf: board private structure
9642  * @filter: FDir filter to remove
9643  */
9644 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9645 				       struct i40e_fdir_filter *filter)
9646 {
9647 	/* Update counters */
9648 	pf->fdir_pf_active_filters--;
9649 	pf->fd_inv = 0;
9650 
9651 	switch (filter->flow_type) {
9652 	case TCP_V4_FLOW:
9653 		pf->fd_tcp4_filter_cnt--;
9654 		break;
9655 	case UDP_V4_FLOW:
9656 		pf->fd_udp4_filter_cnt--;
9657 		break;
9658 	case SCTP_V4_FLOW:
9659 		pf->fd_sctp4_filter_cnt--;
9660 		break;
9661 	case TCP_V6_FLOW:
9662 		pf->fd_tcp6_filter_cnt--;
9663 		break;
9664 	case UDP_V6_FLOW:
9665 		pf->fd_udp6_filter_cnt--;
9666 		break;
9667 	case SCTP_V6_FLOW:
9668 		pf->fd_udp6_filter_cnt--;
9669 		break;
9670 	case IP_USER_FLOW:
9671 		switch (filter->ipl4_proto) {
9672 		case IPPROTO_TCP:
9673 			pf->fd_tcp4_filter_cnt--;
9674 			break;
9675 		case IPPROTO_UDP:
9676 			pf->fd_udp4_filter_cnt--;
9677 			break;
9678 		case IPPROTO_SCTP:
9679 			pf->fd_sctp4_filter_cnt--;
9680 			break;
9681 		case IPPROTO_IP:
9682 			pf->fd_ip4_filter_cnt--;
9683 			break;
9684 		}
9685 		break;
9686 	case IPV6_USER_FLOW:
9687 		switch (filter->ipl4_proto) {
9688 		case IPPROTO_TCP:
9689 			pf->fd_tcp6_filter_cnt--;
9690 			break;
9691 		case IPPROTO_UDP:
9692 			pf->fd_udp6_filter_cnt--;
9693 			break;
9694 		case IPPROTO_SCTP:
9695 			pf->fd_sctp6_filter_cnt--;
9696 			break;
9697 		case IPPROTO_IP:
9698 			pf->fd_ip6_filter_cnt--;
9699 			break;
9700 		}
9701 		break;
9702 	}
9703 
9704 	/* Remove the filter from the list and free memory */
9705 	hlist_del(&filter->fdir_node);
9706 	kfree(filter);
9707 }
9708 
9709 /**
9710  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9711  * @pf: board private structure
9712  **/
9713 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9714 {
9715 	struct i40e_fdir_filter *filter;
9716 	u32 fcnt_prog, fcnt_avail;
9717 	struct hlist_node *node;
9718 
9719 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9720 		return;
9721 
9722 	/* Check if we have enough room to re-enable FDir SB capability. */
9723 	fcnt_prog = i40e_get_global_fd_count(pf);
9724 	fcnt_avail = pf->fdir_pf_filter_count;
9725 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9726 	    (pf->fd_add_err == 0) ||
9727 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9728 		i40e_reenable_fdir_sb(pf);
9729 
9730 	/* We should wait for even more space before re-enabling ATR.
9731 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9732 	 * rules active.
9733 	 */
9734 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9735 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9736 		i40e_reenable_fdir_atr(pf);
9737 
9738 	/* if hw had a problem adding a filter, delete it */
9739 	if (pf->fd_inv > 0) {
9740 		hlist_for_each_entry_safe(filter, node,
9741 					  &pf->fdir_filter_list, fdir_node)
9742 			if (filter->fd_id == pf->fd_inv)
9743 				i40e_delete_invalid_filter(pf, filter);
9744 	}
9745 }
9746 
9747 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9748 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9749 /**
9750  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9751  * @pf: board private structure
9752  **/
9753 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9754 {
9755 	unsigned long min_flush_time;
9756 	int flush_wait_retry = 50;
9757 	bool disable_atr = false;
9758 	int fd_room;
9759 	int reg;
9760 
9761 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9762 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9763 		return;
9764 
9765 	/* If the flush is happening too quick and we have mostly SB rules we
9766 	 * should not re-enable ATR for some time.
9767 	 */
9768 	min_flush_time = pf->fd_flush_timestamp +
9769 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9770 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9771 
9772 	if (!(time_after(jiffies, min_flush_time)) &&
9773 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9774 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9775 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9776 		disable_atr = true;
9777 	}
9778 
9779 	pf->fd_flush_timestamp = jiffies;
9780 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9781 	/* flush all filters */
9782 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9783 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9784 	i40e_flush(&pf->hw);
9785 	pf->fd_flush_cnt++;
9786 	pf->fd_add_err = 0;
9787 	do {
9788 		/* Check FD flush status every 5-6msec */
9789 		usleep_range(5000, 6000);
9790 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9791 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9792 			break;
9793 	} while (flush_wait_retry--);
9794 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9795 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9796 	} else {
9797 		/* replay sideband filters */
9798 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9799 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9800 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9801 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9802 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9803 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9804 	}
9805 }
9806 
9807 /**
9808  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9809  * @pf: board private structure
9810  **/
9811 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9812 {
9813 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9814 }
9815 
9816 /**
9817  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9818  * @pf: board private structure
9819  **/
9820 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9821 {
9822 
9823 	/* if interface is down do nothing */
9824 	if (test_bit(__I40E_DOWN, pf->state))
9825 		return;
9826 
9827 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9828 		i40e_fdir_flush_and_replay(pf);
9829 
9830 	i40e_fdir_check_and_reenable(pf);
9831 
9832 }
9833 
9834 /**
9835  * i40e_vsi_link_event - notify VSI of a link event
9836  * @vsi: vsi to be notified
9837  * @link_up: link up or down
9838  **/
9839 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9840 {
9841 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9842 		return;
9843 
9844 	switch (vsi->type) {
9845 	case I40E_VSI_MAIN:
9846 		if (!vsi->netdev || !vsi->netdev_registered)
9847 			break;
9848 
9849 		if (link_up) {
9850 			netif_carrier_on(vsi->netdev);
9851 			netif_tx_wake_all_queues(vsi->netdev);
9852 		} else {
9853 			netif_carrier_off(vsi->netdev);
9854 			netif_tx_stop_all_queues(vsi->netdev);
9855 		}
9856 		break;
9857 
9858 	case I40E_VSI_SRIOV:
9859 	case I40E_VSI_VMDQ2:
9860 	case I40E_VSI_CTRL:
9861 	case I40E_VSI_IWARP:
9862 	case I40E_VSI_MIRROR:
9863 	default:
9864 		/* there is no notification for other VSIs */
9865 		break;
9866 	}
9867 }
9868 
9869 /**
9870  * i40e_veb_link_event - notify elements on the veb of a link event
9871  * @veb: veb to be notified
9872  * @link_up: link up or down
9873  **/
9874 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9875 {
9876 	struct i40e_vsi *vsi;
9877 	struct i40e_pf *pf;
9878 	int i;
9879 
9880 	if (!veb || !veb->pf)
9881 		return;
9882 	pf = veb->pf;
9883 
9884 	/* Send link event to contained VSIs */
9885 	i40e_pf_for_each_vsi(pf, i, vsi)
9886 		if (vsi->uplink_seid == veb->seid)
9887 			i40e_vsi_link_event(vsi, link_up);
9888 }
9889 
9890 /**
9891  * i40e_link_event - Update netif_carrier status
9892  * @pf: board private structure
9893  **/
9894 static void i40e_link_event(struct i40e_pf *pf)
9895 {
9896 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9897 	u8 new_link_speed, old_link_speed;
9898 	bool new_link, old_link;
9899 	int status;
9900 #ifdef CONFIG_I40E_DCB
9901 	int err;
9902 #endif /* CONFIG_I40E_DCB */
9903 
9904 	/* set this to force the get_link_status call to refresh state */
9905 	pf->hw.phy.get_link_info = true;
9906 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9907 	status = i40e_get_link_status(&pf->hw, &new_link);
9908 
9909 	/* On success, disable temp link polling */
9910 	if (status == 0) {
9911 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9912 	} else {
9913 		/* Enable link polling temporarily until i40e_get_link_status
9914 		 * returns 0
9915 		 */
9916 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9917 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9918 			status);
9919 		return;
9920 	}
9921 
9922 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9923 	new_link_speed = pf->hw.phy.link_info.link_speed;
9924 
9925 	if (new_link == old_link &&
9926 	    new_link_speed == old_link_speed &&
9927 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9928 	     new_link == netif_carrier_ok(vsi->netdev)))
9929 		return;
9930 
9931 	i40e_print_link_message(vsi, new_link);
9932 
9933 	/* Notify the base of the switch tree connected to
9934 	 * the link.  Floating VEBs are not notified.
9935 	 */
9936 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9937 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9938 	else
9939 		i40e_vsi_link_event(vsi, new_link);
9940 
9941 	if (pf->vf)
9942 		i40e_vc_notify_link_state(pf);
9943 
9944 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9945 		i40e_ptp_set_increment(pf);
9946 #ifdef CONFIG_I40E_DCB
9947 	if (new_link == old_link)
9948 		return;
9949 	/* Not SW DCB so firmware will take care of default settings */
9950 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9951 		return;
9952 
9953 	/* We cover here only link down, as after link up in case of SW DCB
9954 	 * SW LLDP agent will take care of setting it up
9955 	 */
9956 	if (!new_link) {
9957 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9958 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9959 		err = i40e_dcb_sw_default_config(pf);
9960 		if (err) {
9961 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9962 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9963 		} else {
9964 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9965 				       DCB_CAP_DCBX_VER_IEEE;
9966 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9967 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9968 		}
9969 	}
9970 #endif /* CONFIG_I40E_DCB */
9971 }
9972 
9973 /**
9974  * i40e_watchdog_subtask - periodic checks not using event driven response
9975  * @pf: board private structure
9976  **/
9977 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9978 {
9979 	struct i40e_vsi *vsi;
9980 	struct i40e_veb *veb;
9981 	int i;
9982 
9983 	/* if interface is down do nothing */
9984 	if (test_bit(__I40E_DOWN, pf->state) ||
9985 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9986 		return;
9987 
9988 	/* make sure we don't do these things too often */
9989 	if (time_before(jiffies, (pf->service_timer_previous +
9990 				  pf->service_timer_period)))
9991 		return;
9992 	pf->service_timer_previous = jiffies;
9993 
9994 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
9995 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9996 		i40e_link_event(pf);
9997 
9998 	/* Update the stats for active netdevs so the network stack
9999 	 * can look at updated numbers whenever it cares to
10000 	 */
10001 	i40e_pf_for_each_vsi(pf, i, vsi)
10002 		if (vsi->netdev)
10003 			i40e_update_stats(vsi);
10004 
10005 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10006 		/* Update the stats for the active switching components */
10007 		i40e_pf_for_each_veb(pf, i, veb)
10008 			i40e_update_veb_stats(veb);
10009 	}
10010 
10011 	i40e_ptp_rx_hang(pf);
10012 	i40e_ptp_tx_hang(pf);
10013 }
10014 
10015 /**
10016  * i40e_reset_subtask - Set up for resetting the device and driver
10017  * @pf: board private structure
10018  **/
10019 static void i40e_reset_subtask(struct i40e_pf *pf)
10020 {
10021 	u32 reset_flags = 0;
10022 
10023 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10024 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10025 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10026 	}
10027 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10028 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10029 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10030 	}
10031 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10032 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10033 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10034 	}
10035 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10036 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10037 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10038 	}
10039 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10040 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10041 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10042 	}
10043 
10044 	/* If there's a recovery already waiting, it takes
10045 	 * precedence before starting a new reset sequence.
10046 	 */
10047 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10048 		i40e_prep_for_reset(pf);
10049 		i40e_reset(pf);
10050 		i40e_rebuild(pf, false, false);
10051 	}
10052 
10053 	/* If we're already down or resetting, just bail */
10054 	if (reset_flags &&
10055 	    !test_bit(__I40E_DOWN, pf->state) &&
10056 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10057 		i40e_do_reset(pf, reset_flags, false);
10058 	}
10059 }
10060 
10061 /**
10062  * i40e_handle_link_event - Handle link event
10063  * @pf: board private structure
10064  * @e: event info posted on ARQ
10065  **/
10066 static void i40e_handle_link_event(struct i40e_pf *pf,
10067 				   struct i40e_arq_event_info *e)
10068 {
10069 	struct i40e_aqc_get_link_status *status =
10070 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10071 
10072 	/* Do a new status request to re-enable LSE reporting
10073 	 * and load new status information into the hw struct
10074 	 * This completely ignores any state information
10075 	 * in the ARQ event info, instead choosing to always
10076 	 * issue the AQ update link status command.
10077 	 */
10078 	i40e_link_event(pf);
10079 
10080 	/* Check if module meets thermal requirements */
10081 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10082 		dev_err(&pf->pdev->dev,
10083 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10084 		dev_err(&pf->pdev->dev,
10085 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10086 	} else {
10087 		/* check for unqualified module, if link is down, suppress
10088 		 * the message if link was forced to be down.
10089 		 */
10090 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10091 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10092 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10093 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10094 			dev_err(&pf->pdev->dev,
10095 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10096 			dev_err(&pf->pdev->dev,
10097 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10098 		}
10099 	}
10100 }
10101 
10102 /**
10103  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10104  * @pf: board private structure
10105  **/
10106 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10107 {
10108 	struct i40e_arq_event_info event;
10109 	struct i40e_hw *hw = &pf->hw;
10110 	u16 pending, i = 0;
10111 	u16 opcode;
10112 	u32 oldval;
10113 	int ret;
10114 	u32 val;
10115 
10116 	/* Do not run clean AQ when PF reset fails */
10117 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10118 		return;
10119 
10120 	/* check for error indications */
10121 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10122 	oldval = val;
10123 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10124 		if (hw->debug_mask & I40E_DEBUG_AQ)
10125 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10126 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10127 	}
10128 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10129 		if (hw->debug_mask & I40E_DEBUG_AQ)
10130 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10131 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10132 		pf->arq_overflows++;
10133 	}
10134 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10135 		if (hw->debug_mask & I40E_DEBUG_AQ)
10136 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10137 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10138 	}
10139 	if (oldval != val)
10140 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10141 
10142 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10143 	oldval = val;
10144 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10145 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10146 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10147 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10148 	}
10149 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10150 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10151 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10152 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10153 	}
10154 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10155 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10156 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10157 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10158 	}
10159 	if (oldval != val)
10160 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10161 
10162 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10163 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10164 	if (!event.msg_buf)
10165 		return;
10166 
10167 	do {
10168 		ret = i40e_clean_arq_element(hw, &event, &pending);
10169 		if (ret == -EALREADY)
10170 			break;
10171 		else if (ret) {
10172 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10173 			break;
10174 		}
10175 
10176 		opcode = le16_to_cpu(event.desc.opcode);
10177 		switch (opcode) {
10178 
10179 		case i40e_aqc_opc_get_link_status:
10180 			rtnl_lock();
10181 			i40e_handle_link_event(pf, &event);
10182 			rtnl_unlock();
10183 			break;
10184 		case i40e_aqc_opc_send_msg_to_pf:
10185 			ret = i40e_vc_process_vf_msg(pf,
10186 					le16_to_cpu(event.desc.retval),
10187 					le32_to_cpu(event.desc.cookie_high),
10188 					le32_to_cpu(event.desc.cookie_low),
10189 					event.msg_buf,
10190 					event.msg_len);
10191 			break;
10192 		case i40e_aqc_opc_lldp_update_mib:
10193 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10194 #ifdef CONFIG_I40E_DCB
10195 			rtnl_lock();
10196 			i40e_handle_lldp_event(pf, &event);
10197 			rtnl_unlock();
10198 #endif /* CONFIG_I40E_DCB */
10199 			break;
10200 		case i40e_aqc_opc_event_lan_overflow:
10201 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10202 			i40e_handle_lan_overflow_event(pf, &event);
10203 			break;
10204 		case i40e_aqc_opc_send_msg_to_peer:
10205 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10206 			break;
10207 		case i40e_aqc_opc_nvm_erase:
10208 		case i40e_aqc_opc_nvm_update:
10209 		case i40e_aqc_opc_oem_post_update:
10210 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10211 				   "ARQ NVM operation 0x%04x completed\n",
10212 				   opcode);
10213 			break;
10214 		default:
10215 			dev_info(&pf->pdev->dev,
10216 				 "ARQ: Unknown event 0x%04x ignored\n",
10217 				 opcode);
10218 			break;
10219 		}
10220 	} while (i++ < I40E_AQ_WORK_LIMIT);
10221 
10222 	if (i < I40E_AQ_WORK_LIMIT)
10223 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10224 
10225 	/* re-enable Admin queue interrupt cause */
10226 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10227 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10228 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10229 	i40e_flush(hw);
10230 
10231 	kfree(event.msg_buf);
10232 }
10233 
10234 /**
10235  * i40e_verify_eeprom - make sure eeprom is good to use
10236  * @pf: board private structure
10237  **/
10238 static void i40e_verify_eeprom(struct i40e_pf *pf)
10239 {
10240 	int err;
10241 
10242 	err = i40e_diag_eeprom_test(&pf->hw);
10243 	if (err) {
10244 		/* retry in case of garbage read */
10245 		err = i40e_diag_eeprom_test(&pf->hw);
10246 		if (err) {
10247 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10248 				 err);
10249 			set_bit(__I40E_BAD_EEPROM, pf->state);
10250 		}
10251 	}
10252 
10253 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10254 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10255 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10256 	}
10257 }
10258 
10259 /**
10260  * i40e_enable_pf_switch_lb
10261  * @pf: pointer to the PF structure
10262  *
10263  * enable switch loop back or die - no point in a return value
10264  **/
10265 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10266 {
10267 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10268 	struct i40e_vsi_context ctxt;
10269 	int ret;
10270 
10271 	ctxt.seid = pf->main_vsi_seid;
10272 	ctxt.pf_num = pf->hw.pf_id;
10273 	ctxt.vf_num = 0;
10274 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10275 	if (ret) {
10276 		dev_info(&pf->pdev->dev,
10277 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10278 			 ERR_PTR(ret),
10279 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10280 		return;
10281 	}
10282 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10283 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10284 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10285 
10286 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10287 	if (ret) {
10288 		dev_info(&pf->pdev->dev,
10289 			 "update vsi switch failed, err %pe aq_err %s\n",
10290 			 ERR_PTR(ret),
10291 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10292 	}
10293 }
10294 
10295 /**
10296  * i40e_disable_pf_switch_lb
10297  * @pf: pointer to the PF structure
10298  *
10299  * disable switch loop back or die - no point in a return value
10300  **/
10301 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10302 {
10303 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10304 	struct i40e_vsi_context ctxt;
10305 	int ret;
10306 
10307 	ctxt.seid = pf->main_vsi_seid;
10308 	ctxt.pf_num = pf->hw.pf_id;
10309 	ctxt.vf_num = 0;
10310 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10311 	if (ret) {
10312 		dev_info(&pf->pdev->dev,
10313 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10314 			 ERR_PTR(ret),
10315 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10316 		return;
10317 	}
10318 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10319 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10320 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10321 
10322 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10323 	if (ret) {
10324 		dev_info(&pf->pdev->dev,
10325 			 "update vsi switch failed, err %pe aq_err %s\n",
10326 			 ERR_PTR(ret),
10327 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10328 	}
10329 }
10330 
10331 /**
10332  * i40e_config_bridge_mode - Configure the HW bridge mode
10333  * @veb: pointer to the bridge instance
10334  *
10335  * Configure the loop back mode for the LAN VSI that is downlink to the
10336  * specified HW bridge instance. It is expected this function is called
10337  * when a new HW bridge is instantiated.
10338  **/
10339 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10340 {
10341 	struct i40e_pf *pf = veb->pf;
10342 
10343 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10344 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10345 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10346 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10347 		i40e_disable_pf_switch_lb(pf);
10348 	else
10349 		i40e_enable_pf_switch_lb(pf);
10350 }
10351 
10352 /**
10353  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10354  * @veb: pointer to the VEB instance
10355  *
10356  * This is a function that builds the attached VSIs. We track the connections
10357  * through our own index numbers because the seid's from the HW could change
10358  * across the reset.
10359  **/
10360 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10361 {
10362 	struct i40e_vsi *ctl_vsi = NULL;
10363 	struct i40e_pf *pf = veb->pf;
10364 	struct i40e_vsi *vsi;
10365 	int v, ret;
10366 
10367 	/* As we do not maintain PV (port virtualizer) switch element then
10368 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10369 	 * and its control VSI is the main one.
10370 	 */
10371 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10372 		dev_err(&pf->pdev->dev,
10373 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10374 		return -ENOENT;
10375 	}
10376 
10377 	if (veb->uplink_seid == pf->mac_seid) {
10378 		/* Check that the LAN VSI has VEB owning flag set */
10379 		ctl_vsi = pf->vsi[pf->lan_vsi];
10380 
10381 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10382 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10383 			dev_err(&pf->pdev->dev,
10384 				"Invalid control VSI for VEB %d\n", veb->idx);
10385 			return -ENOENT;
10386 		}
10387 
10388 		/* Add the control VSI to switch */
10389 		ret = i40e_add_vsi(ctl_vsi);
10390 		if (ret) {
10391 			dev_err(&pf->pdev->dev,
10392 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10393 				veb->idx, ret);
10394 			return ret;
10395 		}
10396 
10397 		i40e_vsi_reset_stats(ctl_vsi);
10398 	}
10399 
10400 	/* create the VEB in the switch and move the VSI onto the VEB */
10401 	ret = i40e_add_veb(veb, ctl_vsi);
10402 	if (ret)
10403 		return ret;
10404 
10405 	if (veb->uplink_seid) {
10406 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10407 			veb->bridge_mode = BRIDGE_MODE_VEB;
10408 		else
10409 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10410 		i40e_config_bridge_mode(veb);
10411 	}
10412 
10413 	/* create the remaining VSIs attached to this VEB */
10414 	i40e_pf_for_each_vsi(pf, v, vsi) {
10415 		if (vsi == ctl_vsi)
10416 			continue;
10417 
10418 		if (vsi->veb_idx == veb->idx) {
10419 			vsi->uplink_seid = veb->seid;
10420 			ret = i40e_add_vsi(vsi);
10421 			if (ret) {
10422 				dev_info(&pf->pdev->dev,
10423 					 "rebuild of vsi_idx %d failed: %d\n",
10424 					 v, ret);
10425 				return ret;
10426 			}
10427 			i40e_vsi_reset_stats(vsi);
10428 		}
10429 	}
10430 
10431 	return ret;
10432 }
10433 
10434 /**
10435  * i40e_get_capabilities - get info about the HW
10436  * @pf: the PF struct
10437  * @list_type: AQ capability to be queried
10438  **/
10439 static int i40e_get_capabilities(struct i40e_pf *pf,
10440 				 enum i40e_admin_queue_opc list_type)
10441 {
10442 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10443 	u16 data_size;
10444 	int buf_len;
10445 	int err;
10446 
10447 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10448 	do {
10449 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10450 		if (!cap_buf)
10451 			return -ENOMEM;
10452 
10453 		/* this loads the data into the hw struct for us */
10454 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10455 						    &data_size, list_type,
10456 						    NULL);
10457 		/* data loaded, buffer no longer needed */
10458 		kfree(cap_buf);
10459 
10460 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10461 			/* retry with a larger buffer */
10462 			buf_len = data_size;
10463 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10464 			dev_info(&pf->pdev->dev,
10465 				 "capability discovery failed, err %pe aq_err %s\n",
10466 				 ERR_PTR(err),
10467 				 i40e_aq_str(&pf->hw,
10468 					     pf->hw.aq.asq_last_status));
10469 			return -ENODEV;
10470 		}
10471 	} while (err);
10472 
10473 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10474 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10475 			dev_info(&pf->pdev->dev,
10476 				 "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",
10477 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10478 				 pf->hw.func_caps.num_msix_vectors,
10479 				 pf->hw.func_caps.num_msix_vectors_vf,
10480 				 pf->hw.func_caps.fd_filters_guaranteed,
10481 				 pf->hw.func_caps.fd_filters_best_effort,
10482 				 pf->hw.func_caps.num_tx_qp,
10483 				 pf->hw.func_caps.num_vsis);
10484 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10485 			dev_info(&pf->pdev->dev,
10486 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10487 				 pf->hw.dev_caps.switch_mode,
10488 				 pf->hw.dev_caps.valid_functions);
10489 			dev_info(&pf->pdev->dev,
10490 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10491 				 pf->hw.dev_caps.sr_iov_1_1,
10492 				 pf->hw.dev_caps.num_vfs);
10493 			dev_info(&pf->pdev->dev,
10494 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10495 				 pf->hw.dev_caps.num_vsis,
10496 				 pf->hw.dev_caps.num_rx_qp,
10497 				 pf->hw.dev_caps.num_tx_qp);
10498 		}
10499 	}
10500 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10501 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10502 		       + pf->hw.func_caps.num_vfs)
10503 		if (pf->hw.revision_id == 0 &&
10504 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10505 			dev_info(&pf->pdev->dev,
10506 				 "got num_vsis %d, setting num_vsis to %d\n",
10507 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10508 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10509 		}
10510 	}
10511 	return 0;
10512 }
10513 
10514 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10515 
10516 /**
10517  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10518  * @pf: board private structure
10519  **/
10520 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10521 {
10522 	struct i40e_vsi *vsi;
10523 
10524 	/* quick workaround for an NVM issue that leaves a critical register
10525 	 * uninitialized
10526 	 */
10527 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10528 		static const u32 hkey[] = {
10529 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10530 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10531 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10532 			0x95b3a76d};
10533 		int i;
10534 
10535 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10536 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10537 	}
10538 
10539 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10540 		return;
10541 
10542 	/* find existing VSI and see if it needs configuring */
10543 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10544 
10545 	/* create a new VSI if none exists */
10546 	if (!vsi) {
10547 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10548 				     pf->vsi[pf->lan_vsi]->seid, 0);
10549 		if (!vsi) {
10550 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10551 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10552 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10553 			return;
10554 		}
10555 	}
10556 
10557 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10558 }
10559 
10560 /**
10561  * i40e_fdir_teardown - release the Flow Director resources
10562  * @pf: board private structure
10563  **/
10564 static void i40e_fdir_teardown(struct i40e_pf *pf)
10565 {
10566 	struct i40e_vsi *vsi;
10567 
10568 	i40e_fdir_filter_exit(pf);
10569 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10570 	if (vsi)
10571 		i40e_vsi_release(vsi);
10572 }
10573 
10574 /**
10575  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10576  * @vsi: PF main vsi
10577  * @seid: seid of main or channel VSIs
10578  *
10579  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10580  * existed before reset
10581  **/
10582 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10583 {
10584 	struct i40e_cloud_filter *cfilter;
10585 	struct i40e_pf *pf = vsi->back;
10586 	struct hlist_node *node;
10587 	int ret;
10588 
10589 	/* Add cloud filters back if they exist */
10590 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10591 				  cloud_node) {
10592 		if (cfilter->seid != seid)
10593 			continue;
10594 
10595 		if (cfilter->dst_port)
10596 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10597 								true);
10598 		else
10599 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10600 
10601 		if (ret) {
10602 			dev_dbg(&pf->pdev->dev,
10603 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10604 				ERR_PTR(ret),
10605 				i40e_aq_str(&pf->hw,
10606 					    pf->hw.aq.asq_last_status));
10607 			return ret;
10608 		}
10609 	}
10610 	return 0;
10611 }
10612 
10613 /**
10614  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10615  * @vsi: PF main vsi
10616  *
10617  * Rebuilds channel VSIs if they existed before reset
10618  **/
10619 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10620 {
10621 	struct i40e_channel *ch, *ch_tmp;
10622 	int ret;
10623 
10624 	if (list_empty(&vsi->ch_list))
10625 		return 0;
10626 
10627 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10628 		if (!ch->initialized)
10629 			break;
10630 		/* Proceed with creation of channel (VMDq2) VSI */
10631 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10632 		if (ret) {
10633 			dev_info(&vsi->back->pdev->dev,
10634 				 "failed to rebuild channels using uplink_seid %u\n",
10635 				 vsi->uplink_seid);
10636 			return ret;
10637 		}
10638 		/* Reconfigure TX queues using QTX_CTL register */
10639 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10640 		if (ret) {
10641 			dev_info(&vsi->back->pdev->dev,
10642 				 "failed to configure TX rings for channel %u\n",
10643 				 ch->seid);
10644 			return ret;
10645 		}
10646 		/* update 'next_base_queue' */
10647 		vsi->next_base_queue = vsi->next_base_queue +
10648 							ch->num_queue_pairs;
10649 		if (ch->max_tx_rate) {
10650 			u64 credits = ch->max_tx_rate;
10651 
10652 			if (i40e_set_bw_limit(vsi, ch->seid,
10653 					      ch->max_tx_rate))
10654 				return -EINVAL;
10655 
10656 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10657 			dev_dbg(&vsi->back->pdev->dev,
10658 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10659 				ch->max_tx_rate,
10660 				credits,
10661 				ch->seid);
10662 		}
10663 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10664 		if (ret) {
10665 			dev_dbg(&vsi->back->pdev->dev,
10666 				"Failed to rebuild cloud filters for channel VSI %u\n",
10667 				ch->seid);
10668 			return ret;
10669 		}
10670 	}
10671 	return 0;
10672 }
10673 
10674 /**
10675  * i40e_clean_xps_state - clean xps state for every tx_ring
10676  * @vsi: ptr to the VSI
10677  **/
10678 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10679 {
10680 	int i;
10681 
10682 	if (vsi->tx_rings)
10683 		for (i = 0; i < vsi->num_queue_pairs; i++)
10684 			if (vsi->tx_rings[i])
10685 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10686 					  vsi->tx_rings[i]->state);
10687 }
10688 
10689 /**
10690  * i40e_prep_for_reset - prep for the core to reset
10691  * @pf: board private structure
10692  *
10693  * Close up the VFs and other things in prep for PF Reset.
10694   **/
10695 static void i40e_prep_for_reset(struct i40e_pf *pf)
10696 {
10697 	struct i40e_hw *hw = &pf->hw;
10698 	struct i40e_vsi *vsi;
10699 	int ret = 0;
10700 	u32 v;
10701 
10702 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10703 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10704 		return;
10705 	if (i40e_check_asq_alive(&pf->hw))
10706 		i40e_vc_notify_reset(pf);
10707 
10708 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10709 
10710 	/* quiesce the VSIs and their queues that are not already DOWN */
10711 	i40e_pf_quiesce_all_vsi(pf);
10712 
10713 	i40e_pf_for_each_vsi(pf, v, vsi) {
10714 		i40e_clean_xps_state(vsi);
10715 		vsi->seid = 0;
10716 	}
10717 
10718 	i40e_shutdown_adminq(&pf->hw);
10719 
10720 	/* call shutdown HMC */
10721 	if (hw->hmc.hmc_obj) {
10722 		ret = i40e_shutdown_lan_hmc(hw);
10723 		if (ret)
10724 			dev_warn(&pf->pdev->dev,
10725 				 "shutdown_lan_hmc failed: %d\n", ret);
10726 	}
10727 
10728 	/* Save the current PTP time so that we can restore the time after the
10729 	 * reset completes.
10730 	 */
10731 	i40e_ptp_save_hw_time(pf);
10732 }
10733 
10734 /**
10735  * i40e_send_version - update firmware with driver version
10736  * @pf: PF struct
10737  */
10738 static void i40e_send_version(struct i40e_pf *pf)
10739 {
10740 	struct i40e_driver_version dv;
10741 
10742 	dv.major_version = 0xff;
10743 	dv.minor_version = 0xff;
10744 	dv.build_version = 0xff;
10745 	dv.subbuild_version = 0;
10746 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10747 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10748 }
10749 
10750 /**
10751  * i40e_get_oem_version - get OEM specific version information
10752  * @hw: pointer to the hardware structure
10753  **/
10754 static void i40e_get_oem_version(struct i40e_hw *hw)
10755 {
10756 	u16 block_offset = 0xffff;
10757 	u16 block_length = 0;
10758 	u16 capabilities = 0;
10759 	u16 gen_snap = 0;
10760 	u16 release = 0;
10761 
10762 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10763 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10764 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10765 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10766 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10767 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10768 #define I40E_NVM_OEM_LENGTH			3
10769 
10770 	/* Check if pointer to OEM version block is valid. */
10771 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10772 	if (block_offset == 0xffff)
10773 		return;
10774 
10775 	/* Check if OEM version block has correct length. */
10776 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10777 			   &block_length);
10778 	if (block_length < I40E_NVM_OEM_LENGTH)
10779 		return;
10780 
10781 	/* Check if OEM version format is as expected. */
10782 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10783 			   &capabilities);
10784 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10785 		return;
10786 
10787 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10788 			   &gen_snap);
10789 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10790 			   &release);
10791 	hw->nvm.oem_ver =
10792 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10793 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10794 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10795 }
10796 
10797 /**
10798  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10799  * @pf: board private structure
10800  **/
10801 static int i40e_reset(struct i40e_pf *pf)
10802 {
10803 	struct i40e_hw *hw = &pf->hw;
10804 	int ret;
10805 
10806 	ret = i40e_pf_reset(hw);
10807 	if (ret) {
10808 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10809 		set_bit(__I40E_RESET_FAILED, pf->state);
10810 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10811 	} else {
10812 		pf->pfr_count++;
10813 	}
10814 	return ret;
10815 }
10816 
10817 /**
10818  * i40e_rebuild - rebuild using a saved config
10819  * @pf: board private structure
10820  * @reinit: if the Main VSI needs to re-initialized.
10821  * @lock_acquired: indicates whether or not the lock has been acquired
10822  * before this function was called.
10823  **/
10824 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10825 {
10826 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10827 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10828 	struct i40e_hw *hw = &pf->hw;
10829 	struct i40e_veb *veb;
10830 	int ret;
10831 	u32 val;
10832 	int v;
10833 
10834 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10835 	    is_recovery_mode_reported)
10836 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10837 
10838 	if (test_bit(__I40E_DOWN, pf->state) &&
10839 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10840 		goto clear_recovery;
10841 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10842 
10843 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10844 	ret = i40e_init_adminq(&pf->hw);
10845 	if (ret) {
10846 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10847 			 ERR_PTR(ret),
10848 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10849 		goto clear_recovery;
10850 	}
10851 	i40e_get_oem_version(&pf->hw);
10852 
10853 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10854 		/* The following delay is necessary for firmware update. */
10855 		mdelay(1000);
10856 	}
10857 
10858 	/* re-verify the eeprom if we just had an EMP reset */
10859 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10860 		i40e_verify_eeprom(pf);
10861 
10862 	/* if we are going out of or into recovery mode we have to act
10863 	 * accordingly with regard to resources initialization
10864 	 * and deinitialization
10865 	 */
10866 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10867 		if (i40e_get_capabilities(pf,
10868 					  i40e_aqc_opc_list_func_capabilities))
10869 			goto end_unlock;
10870 
10871 		if (is_recovery_mode_reported) {
10872 			/* we're staying in recovery mode so we'll reinitialize
10873 			 * misc vector here
10874 			 */
10875 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10876 				goto end_unlock;
10877 		} else {
10878 			if (!lock_acquired)
10879 				rtnl_lock();
10880 			/* we're going out of recovery mode so we'll free
10881 			 * the IRQ allocated specifically for recovery mode
10882 			 * and restore the interrupt scheme
10883 			 */
10884 			free_irq(pf->pdev->irq, pf);
10885 			i40e_clear_interrupt_scheme(pf);
10886 			if (i40e_restore_interrupt_scheme(pf))
10887 				goto end_unlock;
10888 		}
10889 
10890 		/* tell the firmware that we're starting */
10891 		i40e_send_version(pf);
10892 
10893 		/* bail out in case recovery mode was detected, as there is
10894 		 * no need for further configuration.
10895 		 */
10896 		goto end_unlock;
10897 	}
10898 
10899 	i40e_clear_pxe_mode(hw);
10900 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10901 	if (ret)
10902 		goto end_core_reset;
10903 
10904 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10905 				hw->func_caps.num_rx_qp, 0, 0);
10906 	if (ret) {
10907 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10908 		goto end_core_reset;
10909 	}
10910 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10911 	if (ret) {
10912 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10913 		goto end_core_reset;
10914 	}
10915 
10916 #ifdef CONFIG_I40E_DCB
10917 	/* Enable FW to write a default DCB config on link-up
10918 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10919 	 * is not supported with new link speed
10920 	 */
10921 	if (i40e_is_tc_mqprio_enabled(pf)) {
10922 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10923 	} else {
10924 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10925 		    (hw->phy.link_info.link_speed &
10926 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10927 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10928 			dev_warn(&pf->pdev->dev,
10929 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10930 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10931 		} else {
10932 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10933 			ret = i40e_init_pf_dcb(pf);
10934 			if (ret) {
10935 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10936 					 ret);
10937 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10938 				/* Continue without DCB enabled */
10939 			}
10940 		}
10941 	}
10942 
10943 #endif /* CONFIG_I40E_DCB */
10944 	if (!lock_acquired)
10945 		rtnl_lock();
10946 	ret = i40e_setup_pf_switch(pf, reinit, true);
10947 	if (ret)
10948 		goto end_unlock;
10949 
10950 	/* The driver only wants link up/down and module qualification
10951 	 * reports from firmware.  Note the negative logic.
10952 	 */
10953 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10954 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10955 					 I40E_AQ_EVENT_MEDIA_NA |
10956 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10957 	if (ret)
10958 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10959 			 ERR_PTR(ret),
10960 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10961 
10962 	/* Rebuild the VSIs and VEBs that existed before reset.
10963 	 * They are still in our local switch element arrays, so only
10964 	 * need to rebuild the switch model in the HW.
10965 	 *
10966 	 * If there were VEBs but the reconstitution failed, we'll try
10967 	 * to recover minimal use by getting the basic PF VSI working.
10968 	 */
10969 	if (vsi->uplink_seid != pf->mac_seid) {
10970 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10971 
10972 		/* Rebuild VEBs */
10973 		i40e_pf_for_each_veb(pf, v, veb) {
10974 			ret = i40e_reconstitute_veb(veb);
10975 			if (!ret)
10976 				continue;
10977 
10978 			/* If Main VEB failed, we're in deep doodoo,
10979 			 * so give up rebuilding the switch and set up
10980 			 * for minimal rebuild of PF VSI.
10981 			 * If orphan failed, we'll report the error
10982 			 * but try to keep going.
10983 			 */
10984 			if (veb->uplink_seid == pf->mac_seid) {
10985 				dev_info(&pf->pdev->dev,
10986 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10987 					 ret);
10988 				vsi->uplink_seid = pf->mac_seid;
10989 				break;
10990 			} else if (veb->uplink_seid == 0) {
10991 				dev_info(&pf->pdev->dev,
10992 					 "rebuild of orphan VEB failed: %d\n",
10993 					 ret);
10994 			}
10995 		}
10996 	}
10997 
10998 	if (vsi->uplink_seid == pf->mac_seid) {
10999 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11000 		/* no VEB, so rebuild only the Main VSI */
11001 		ret = i40e_add_vsi(vsi);
11002 		if (ret) {
11003 			dev_info(&pf->pdev->dev,
11004 				 "rebuild of Main VSI failed: %d\n", ret);
11005 			goto end_unlock;
11006 		}
11007 	}
11008 
11009 	if (vsi->mqprio_qopt.max_rate[0]) {
11010 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11011 						  vsi->mqprio_qopt.max_rate[0]);
11012 		u64 credits = 0;
11013 
11014 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11015 		if (ret)
11016 			goto end_unlock;
11017 
11018 		credits = max_tx_rate;
11019 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11020 		dev_dbg(&vsi->back->pdev->dev,
11021 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11022 			max_tx_rate,
11023 			credits,
11024 			vsi->seid);
11025 	}
11026 
11027 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11028 	if (ret)
11029 		goto end_unlock;
11030 
11031 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11032 	 * for this main VSI if they exist
11033 	 */
11034 	ret = i40e_rebuild_channels(vsi);
11035 	if (ret)
11036 		goto end_unlock;
11037 
11038 	/* Reconfigure hardware for allowing smaller MSS in the case
11039 	 * of TSO, so that we avoid the MDD being fired and causing
11040 	 * a reset in the case of small MSS+TSO.
11041 	 */
11042 #define I40E_REG_MSS          0x000E64DC
11043 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11044 #define I40E_64BYTE_MSS       0x400000
11045 	val = rd32(hw, I40E_REG_MSS);
11046 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11047 		val &= ~I40E_REG_MSS_MIN_MASK;
11048 		val |= I40E_64BYTE_MSS;
11049 		wr32(hw, I40E_REG_MSS, val);
11050 	}
11051 
11052 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11053 		msleep(75);
11054 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11055 		if (ret)
11056 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11057 				 ERR_PTR(ret),
11058 				 i40e_aq_str(&pf->hw,
11059 					     pf->hw.aq.asq_last_status));
11060 	}
11061 	/* reinit the misc interrupt */
11062 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11063 		ret = i40e_setup_misc_vector(pf);
11064 		if (ret)
11065 			goto end_unlock;
11066 	}
11067 
11068 	/* Add a filter to drop all Flow control frames from any VSI from being
11069 	 * transmitted. By doing so we stop a malicious VF from sending out
11070 	 * PAUSE or PFC frames and potentially controlling traffic for other
11071 	 * PF/VF VSIs.
11072 	 * The FW can still send Flow control frames if enabled.
11073 	 */
11074 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11075 						       pf->main_vsi_seid);
11076 
11077 	/* restart the VSIs that were rebuilt and running before the reset */
11078 	i40e_pf_unquiesce_all_vsi(pf);
11079 
11080 	/* Release the RTNL lock before we start resetting VFs */
11081 	if (!lock_acquired)
11082 		rtnl_unlock();
11083 
11084 	/* Restore promiscuous settings */
11085 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11086 	if (ret)
11087 		dev_warn(&pf->pdev->dev,
11088 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11089 			 pf->cur_promisc ? "on" : "off",
11090 			 ERR_PTR(ret),
11091 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11092 
11093 	i40e_reset_all_vfs(pf, true);
11094 
11095 	/* tell the firmware that we're starting */
11096 	i40e_send_version(pf);
11097 
11098 	/* We've already released the lock, so don't do it again */
11099 	goto end_core_reset;
11100 
11101 end_unlock:
11102 	if (!lock_acquired)
11103 		rtnl_unlock();
11104 end_core_reset:
11105 	clear_bit(__I40E_RESET_FAILED, pf->state);
11106 clear_recovery:
11107 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11108 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11109 }
11110 
11111 /**
11112  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11113  * @pf: board private structure
11114  * @reinit: if the Main VSI needs to re-initialized.
11115  * @lock_acquired: indicates whether or not the lock has been acquired
11116  * before this function was called.
11117  **/
11118 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11119 				   bool lock_acquired)
11120 {
11121 	int ret;
11122 
11123 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11124 		return;
11125 	/* Now we wait for GRST to settle out.
11126 	 * We don't have to delete the VEBs or VSIs from the hw switch
11127 	 * because the reset will make them disappear.
11128 	 */
11129 	ret = i40e_reset(pf);
11130 	if (!ret)
11131 		i40e_rebuild(pf, reinit, lock_acquired);
11132 }
11133 
11134 /**
11135  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11136  * @pf: board private structure
11137  *
11138  * Close up the VFs and other things in prep for a Core Reset,
11139  * then get ready to rebuild the world.
11140  * @lock_acquired: indicates whether or not the lock has been acquired
11141  * before this function was called.
11142  **/
11143 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11144 {
11145 	i40e_prep_for_reset(pf);
11146 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11147 }
11148 
11149 /**
11150  * i40e_handle_mdd_event
11151  * @pf: pointer to the PF structure
11152  *
11153  * Called from the MDD irq handler to identify possibly malicious vfs
11154  **/
11155 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11156 {
11157 	struct i40e_hw *hw = &pf->hw;
11158 	bool mdd_detected = false;
11159 	struct i40e_vf *vf;
11160 	u32 reg;
11161 	int i;
11162 
11163 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11164 		return;
11165 
11166 	/* find what triggered the MDD event */
11167 	reg = rd32(hw, I40E_GL_MDET_TX);
11168 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11169 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11170 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11171 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11172 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11173 				pf->hw.func_caps.base_queue;
11174 		if (netif_msg_tx_err(pf))
11175 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11176 				 event, queue, pf_num, vf_num);
11177 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11178 		mdd_detected = true;
11179 	}
11180 	reg = rd32(hw, I40E_GL_MDET_RX);
11181 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11182 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11183 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11184 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11185 				pf->hw.func_caps.base_queue;
11186 		if (netif_msg_rx_err(pf))
11187 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11188 				 event, queue, func);
11189 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11190 		mdd_detected = true;
11191 	}
11192 
11193 	if (mdd_detected) {
11194 		reg = rd32(hw, I40E_PF_MDET_TX);
11195 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11196 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11197 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11198 		}
11199 		reg = rd32(hw, I40E_PF_MDET_RX);
11200 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11201 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11202 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11203 		}
11204 	}
11205 
11206 	/* see if one of the VFs needs its hand slapped */
11207 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11208 		vf = &(pf->vf[i]);
11209 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11210 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11211 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11212 			vf->num_mdd_events++;
11213 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11214 				 i);
11215 			dev_info(&pf->pdev->dev,
11216 				 "Use PF Control I/F to re-enable the VF\n");
11217 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11218 		}
11219 
11220 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11221 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11222 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11223 			vf->num_mdd_events++;
11224 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11225 				 i);
11226 			dev_info(&pf->pdev->dev,
11227 				 "Use PF Control I/F to re-enable the VF\n");
11228 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11229 		}
11230 	}
11231 
11232 	/* re-enable mdd interrupt cause */
11233 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11234 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11235 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11236 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11237 	i40e_flush(hw);
11238 }
11239 
11240 /**
11241  * i40e_service_task - Run the driver's async subtasks
11242  * @work: pointer to work_struct containing our data
11243  **/
11244 static void i40e_service_task(struct work_struct *work)
11245 {
11246 	struct i40e_pf *pf = container_of(work,
11247 					  struct i40e_pf,
11248 					  service_task);
11249 	unsigned long start_time = jiffies;
11250 
11251 	/* don't bother with service tasks if a reset is in progress */
11252 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11253 	    test_bit(__I40E_SUSPENDED, pf->state))
11254 		return;
11255 
11256 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11257 		return;
11258 
11259 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11260 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11261 		i40e_sync_filters_subtask(pf);
11262 		i40e_reset_subtask(pf);
11263 		i40e_handle_mdd_event(pf);
11264 		i40e_vc_process_vflr_event(pf);
11265 		i40e_watchdog_subtask(pf);
11266 		i40e_fdir_reinit_subtask(pf);
11267 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11268 			/* Client subtask will reopen next time through. */
11269 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11270 							   true);
11271 		} else {
11272 			i40e_client_subtask(pf);
11273 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11274 					       pf->state))
11275 				i40e_notify_client_of_l2_param_changes(
11276 								pf->vsi[pf->lan_vsi]);
11277 		}
11278 		i40e_sync_filters_subtask(pf);
11279 	} else {
11280 		i40e_reset_subtask(pf);
11281 	}
11282 
11283 	i40e_clean_adminq_subtask(pf);
11284 
11285 	/* flush memory to make sure state is correct before next watchdog */
11286 	smp_mb__before_atomic();
11287 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11288 
11289 	/* If the tasks have taken longer than one timer cycle or there
11290 	 * is more work to be done, reschedule the service task now
11291 	 * rather than wait for the timer to tick again.
11292 	 */
11293 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11294 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11295 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11296 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11297 		i40e_service_event_schedule(pf);
11298 }
11299 
11300 /**
11301  * i40e_service_timer - timer callback
11302  * @t: timer list pointer
11303  **/
11304 static void i40e_service_timer(struct timer_list *t)
11305 {
11306 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11307 
11308 	mod_timer(&pf->service_timer,
11309 		  round_jiffies(jiffies + pf->service_timer_period));
11310 	i40e_service_event_schedule(pf);
11311 }
11312 
11313 /**
11314  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11315  * @vsi: the VSI being configured
11316  **/
11317 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11318 {
11319 	struct i40e_pf *pf = vsi->back;
11320 
11321 	switch (vsi->type) {
11322 	case I40E_VSI_MAIN:
11323 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11324 		if (!vsi->num_tx_desc)
11325 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11326 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11327 		if (!vsi->num_rx_desc)
11328 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11329 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11330 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11331 			vsi->num_q_vectors = pf->num_lan_msix;
11332 		else
11333 			vsi->num_q_vectors = 1;
11334 
11335 		break;
11336 
11337 	case I40E_VSI_FDIR:
11338 		vsi->alloc_queue_pairs = 1;
11339 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11340 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11341 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11342 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11343 		vsi->num_q_vectors = pf->num_fdsb_msix;
11344 		break;
11345 
11346 	case I40E_VSI_VMDQ2:
11347 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11348 		if (!vsi->num_tx_desc)
11349 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11350 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11351 		if (!vsi->num_rx_desc)
11352 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11353 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11354 		vsi->num_q_vectors = pf->num_vmdq_msix;
11355 		break;
11356 
11357 	case I40E_VSI_SRIOV:
11358 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11359 		if (!vsi->num_tx_desc)
11360 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11361 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11362 		if (!vsi->num_rx_desc)
11363 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11364 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11365 		break;
11366 
11367 	default:
11368 		WARN_ON(1);
11369 		return -ENODATA;
11370 	}
11371 
11372 	if (is_kdump_kernel()) {
11373 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11374 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11375 	}
11376 
11377 	return 0;
11378 }
11379 
11380 /**
11381  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11382  * @vsi: VSI pointer
11383  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11384  *
11385  * On error: returns error code (negative)
11386  * On success: returns 0
11387  **/
11388 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11389 {
11390 	struct i40e_ring **next_rings;
11391 	int size;
11392 	int ret = 0;
11393 
11394 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11395 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11396 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11397 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11398 	if (!vsi->tx_rings)
11399 		return -ENOMEM;
11400 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11401 	if (i40e_enabled_xdp_vsi(vsi)) {
11402 		vsi->xdp_rings = next_rings;
11403 		next_rings += vsi->alloc_queue_pairs;
11404 	}
11405 	vsi->rx_rings = next_rings;
11406 
11407 	if (alloc_qvectors) {
11408 		/* allocate memory for q_vector pointers */
11409 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11410 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11411 		if (!vsi->q_vectors) {
11412 			ret = -ENOMEM;
11413 			goto err_vectors;
11414 		}
11415 	}
11416 	return ret;
11417 
11418 err_vectors:
11419 	kfree(vsi->tx_rings);
11420 	return ret;
11421 }
11422 
11423 /**
11424  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11425  * @pf: board private structure
11426  * @type: type of VSI
11427  *
11428  * On error: returns error code (negative)
11429  * On success: returns vsi index in PF (positive)
11430  **/
11431 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11432 {
11433 	int ret = -ENODEV;
11434 	struct i40e_vsi *vsi;
11435 	int vsi_idx;
11436 	int i;
11437 
11438 	/* Need to protect the allocation of the VSIs at the PF level */
11439 	mutex_lock(&pf->switch_mutex);
11440 
11441 	/* VSI list may be fragmented if VSI creation/destruction has
11442 	 * been happening.  We can afford to do a quick scan to look
11443 	 * for any free VSIs in the list.
11444 	 *
11445 	 * find next empty vsi slot, looping back around if necessary
11446 	 */
11447 	i = pf->next_vsi;
11448 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11449 		i++;
11450 	if (i >= pf->num_alloc_vsi) {
11451 		i = 0;
11452 		while (i < pf->next_vsi && pf->vsi[i])
11453 			i++;
11454 	}
11455 
11456 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11457 		vsi_idx = i;             /* Found one! */
11458 	} else {
11459 		ret = -ENODEV;
11460 		goto unlock_pf;  /* out of VSI slots! */
11461 	}
11462 	pf->next_vsi = ++i;
11463 
11464 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11465 	if (!vsi) {
11466 		ret = -ENOMEM;
11467 		goto unlock_pf;
11468 	}
11469 	vsi->type = type;
11470 	vsi->back = pf;
11471 	set_bit(__I40E_VSI_DOWN, vsi->state);
11472 	vsi->flags = 0;
11473 	vsi->idx = vsi_idx;
11474 	vsi->int_rate_limit = 0;
11475 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11476 				pf->rss_table_size : 64;
11477 	vsi->netdev_registered = false;
11478 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11479 	hash_init(vsi->mac_filter_hash);
11480 	vsi->irqs_ready = false;
11481 
11482 	if (type == I40E_VSI_MAIN) {
11483 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11484 		if (!vsi->af_xdp_zc_qps)
11485 			goto err_rings;
11486 	}
11487 
11488 	ret = i40e_set_num_rings_in_vsi(vsi);
11489 	if (ret)
11490 		goto err_rings;
11491 
11492 	ret = i40e_vsi_alloc_arrays(vsi, true);
11493 	if (ret)
11494 		goto err_rings;
11495 
11496 	/* Setup default MSIX irq handler for VSI */
11497 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11498 
11499 	/* Initialize VSI lock */
11500 	spin_lock_init(&vsi->mac_filter_hash_lock);
11501 	pf->vsi[vsi_idx] = vsi;
11502 	ret = vsi_idx;
11503 	goto unlock_pf;
11504 
11505 err_rings:
11506 	bitmap_free(vsi->af_xdp_zc_qps);
11507 	pf->next_vsi = i - 1;
11508 	kfree(vsi);
11509 unlock_pf:
11510 	mutex_unlock(&pf->switch_mutex);
11511 	return ret;
11512 }
11513 
11514 /**
11515  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11516  * @vsi: VSI pointer
11517  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11518  *
11519  * On error: returns error code (negative)
11520  * On success: returns 0
11521  **/
11522 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11523 {
11524 	/* free the ring and vector containers */
11525 	if (free_qvectors) {
11526 		kfree(vsi->q_vectors);
11527 		vsi->q_vectors = NULL;
11528 	}
11529 	kfree(vsi->tx_rings);
11530 	vsi->tx_rings = NULL;
11531 	vsi->rx_rings = NULL;
11532 	vsi->xdp_rings = NULL;
11533 }
11534 
11535 /**
11536  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11537  * and lookup table
11538  * @vsi: Pointer to VSI structure
11539  */
11540 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11541 {
11542 	if (!vsi)
11543 		return;
11544 
11545 	kfree(vsi->rss_hkey_user);
11546 	vsi->rss_hkey_user = NULL;
11547 
11548 	kfree(vsi->rss_lut_user);
11549 	vsi->rss_lut_user = NULL;
11550 }
11551 
11552 /**
11553  * i40e_vsi_clear - Deallocate the VSI provided
11554  * @vsi: the VSI being un-configured
11555  **/
11556 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11557 {
11558 	struct i40e_pf *pf;
11559 
11560 	if (!vsi)
11561 		return 0;
11562 
11563 	if (!vsi->back)
11564 		goto free_vsi;
11565 	pf = vsi->back;
11566 
11567 	mutex_lock(&pf->switch_mutex);
11568 	if (!pf->vsi[vsi->idx]) {
11569 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11570 			vsi->idx, vsi->idx, vsi->type);
11571 		goto unlock_vsi;
11572 	}
11573 
11574 	if (pf->vsi[vsi->idx] != vsi) {
11575 		dev_err(&pf->pdev->dev,
11576 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11577 			pf->vsi[vsi->idx]->idx,
11578 			pf->vsi[vsi->idx]->type,
11579 			vsi->idx, vsi->type);
11580 		goto unlock_vsi;
11581 	}
11582 
11583 	/* updates the PF for this cleared vsi */
11584 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11585 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11586 
11587 	bitmap_free(vsi->af_xdp_zc_qps);
11588 	i40e_vsi_free_arrays(vsi, true);
11589 	i40e_clear_rss_config_user(vsi);
11590 
11591 	pf->vsi[vsi->idx] = NULL;
11592 	if (vsi->idx < pf->next_vsi)
11593 		pf->next_vsi = vsi->idx;
11594 
11595 unlock_vsi:
11596 	mutex_unlock(&pf->switch_mutex);
11597 free_vsi:
11598 	kfree(vsi);
11599 
11600 	return 0;
11601 }
11602 
11603 /**
11604  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11605  * @vsi: the VSI being cleaned
11606  **/
11607 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11608 {
11609 	int i;
11610 
11611 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11612 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11613 			kfree_rcu(vsi->tx_rings[i], rcu);
11614 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11615 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11616 			if (vsi->xdp_rings)
11617 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11618 		}
11619 	}
11620 }
11621 
11622 /**
11623  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11624  * @vsi: the VSI being configured
11625  **/
11626 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11627 {
11628 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11629 	struct i40e_pf *pf = vsi->back;
11630 	struct i40e_ring *ring;
11631 
11632 	/* Set basic values in the rings to be used later during open() */
11633 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11634 		/* allocate space for both Tx and Rx in one shot */
11635 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11636 		if (!ring)
11637 			goto err_out;
11638 
11639 		ring->queue_index = i;
11640 		ring->reg_idx = vsi->base_queue + i;
11641 		ring->ring_active = false;
11642 		ring->vsi = vsi;
11643 		ring->netdev = vsi->netdev;
11644 		ring->dev = &pf->pdev->dev;
11645 		ring->count = vsi->num_tx_desc;
11646 		ring->size = 0;
11647 		ring->dcb_tc = 0;
11648 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11649 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11650 		ring->itr_setting = pf->tx_itr_default;
11651 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11652 
11653 		if (!i40e_enabled_xdp_vsi(vsi))
11654 			goto setup_rx;
11655 
11656 		ring->queue_index = vsi->alloc_queue_pairs + i;
11657 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11658 		ring->ring_active = false;
11659 		ring->vsi = vsi;
11660 		ring->netdev = NULL;
11661 		ring->dev = &pf->pdev->dev;
11662 		ring->count = vsi->num_tx_desc;
11663 		ring->size = 0;
11664 		ring->dcb_tc = 0;
11665 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11666 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11667 		set_ring_xdp(ring);
11668 		ring->itr_setting = pf->tx_itr_default;
11669 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11670 
11671 setup_rx:
11672 		ring->queue_index = i;
11673 		ring->reg_idx = vsi->base_queue + i;
11674 		ring->ring_active = false;
11675 		ring->vsi = vsi;
11676 		ring->netdev = vsi->netdev;
11677 		ring->dev = &pf->pdev->dev;
11678 		ring->count = vsi->num_rx_desc;
11679 		ring->size = 0;
11680 		ring->dcb_tc = 0;
11681 		ring->itr_setting = pf->rx_itr_default;
11682 		WRITE_ONCE(vsi->rx_rings[i], ring);
11683 	}
11684 
11685 	return 0;
11686 
11687 err_out:
11688 	i40e_vsi_clear_rings(vsi);
11689 	return -ENOMEM;
11690 }
11691 
11692 /**
11693  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11694  * @pf: board private structure
11695  * @vectors: the number of MSI-X vectors to request
11696  *
11697  * Returns the number of vectors reserved, or error
11698  **/
11699 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11700 {
11701 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11702 					I40E_MIN_MSIX, vectors);
11703 	if (vectors < 0) {
11704 		dev_info(&pf->pdev->dev,
11705 			 "MSI-X vector reservation failed: %d\n", vectors);
11706 		vectors = 0;
11707 	}
11708 
11709 	return vectors;
11710 }
11711 
11712 /**
11713  * i40e_init_msix - Setup the MSIX capability
11714  * @pf: board private structure
11715  *
11716  * Work with the OS to set up the MSIX vectors needed.
11717  *
11718  * Returns the number of vectors reserved or negative on failure
11719  **/
11720 static int i40e_init_msix(struct i40e_pf *pf)
11721 {
11722 	struct i40e_hw *hw = &pf->hw;
11723 	int cpus, extra_vectors;
11724 	int vectors_left;
11725 	int v_budget, i;
11726 	int v_actual;
11727 	int iwarp_requested = 0;
11728 
11729 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11730 		return -ENODEV;
11731 
11732 	/* The number of vectors we'll request will be comprised of:
11733 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11734 	 *   - The number of LAN queue pairs
11735 	 *	- Queues being used for RSS.
11736 	 *		We don't need as many as max_rss_size vectors.
11737 	 *		use rss_size instead in the calculation since that
11738 	 *		is governed by number of cpus in the system.
11739 	 *	- assumes symmetric Tx/Rx pairing
11740 	 *   - The number of VMDq pairs
11741 	 *   - The CPU count within the NUMA node if iWARP is enabled
11742 	 * Once we count this up, try the request.
11743 	 *
11744 	 * If we can't get what we want, we'll simplify to nearly nothing
11745 	 * and try again.  If that still fails, we punt.
11746 	 */
11747 	vectors_left = hw->func_caps.num_msix_vectors;
11748 	v_budget = 0;
11749 
11750 	/* reserve one vector for miscellaneous handler */
11751 	if (vectors_left) {
11752 		v_budget++;
11753 		vectors_left--;
11754 	}
11755 
11756 	/* reserve some vectors for the main PF traffic queues. Initially we
11757 	 * only reserve at most 50% of the available vectors, in the case that
11758 	 * the number of online CPUs is large. This ensures that we can enable
11759 	 * extra features as well. Once we've enabled the other features, we
11760 	 * will use any remaining vectors to reach as close as we can to the
11761 	 * number of online CPUs.
11762 	 */
11763 	cpus = num_online_cpus();
11764 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11765 	vectors_left -= pf->num_lan_msix;
11766 
11767 	/* reserve one vector for sideband flow director */
11768 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11769 		if (vectors_left) {
11770 			pf->num_fdsb_msix = 1;
11771 			v_budget++;
11772 			vectors_left--;
11773 		} else {
11774 			pf->num_fdsb_msix = 0;
11775 		}
11776 	}
11777 
11778 	/* can we reserve enough for iWARP? */
11779 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11780 		iwarp_requested = pf->num_iwarp_msix;
11781 
11782 		if (!vectors_left)
11783 			pf->num_iwarp_msix = 0;
11784 		else if (vectors_left < pf->num_iwarp_msix)
11785 			pf->num_iwarp_msix = 1;
11786 		v_budget += pf->num_iwarp_msix;
11787 		vectors_left -= pf->num_iwarp_msix;
11788 	}
11789 
11790 	/* any vectors left over go for VMDq support */
11791 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11792 		if (!vectors_left) {
11793 			pf->num_vmdq_msix = 0;
11794 			pf->num_vmdq_qps = 0;
11795 		} else {
11796 			int vmdq_vecs_wanted =
11797 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11798 			int vmdq_vecs =
11799 				min_t(int, vectors_left, vmdq_vecs_wanted);
11800 
11801 			/* if we're short on vectors for what's desired, we limit
11802 			 * the queues per vmdq.  If this is still more than are
11803 			 * available, the user will need to change the number of
11804 			 * queues/vectors used by the PF later with the ethtool
11805 			 * channels command
11806 			 */
11807 			if (vectors_left < vmdq_vecs_wanted) {
11808 				pf->num_vmdq_qps = 1;
11809 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11810 				vmdq_vecs = min_t(int,
11811 						  vectors_left,
11812 						  vmdq_vecs_wanted);
11813 			}
11814 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11815 
11816 			v_budget += vmdq_vecs;
11817 			vectors_left -= vmdq_vecs;
11818 		}
11819 	}
11820 
11821 	/* On systems with a large number of SMP cores, we previously limited
11822 	 * the number of vectors for num_lan_msix to be at most 50% of the
11823 	 * available vectors, to allow for other features. Now, we add back
11824 	 * the remaining vectors. However, we ensure that the total
11825 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11826 	 * calculate the number of vectors we can add without going over the
11827 	 * cap of CPUs. For systems with a small number of CPUs this will be
11828 	 * zero.
11829 	 */
11830 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11831 	pf->num_lan_msix += extra_vectors;
11832 	vectors_left -= extra_vectors;
11833 
11834 	WARN(vectors_left < 0,
11835 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11836 
11837 	v_budget += pf->num_lan_msix;
11838 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11839 				   GFP_KERNEL);
11840 	if (!pf->msix_entries)
11841 		return -ENOMEM;
11842 
11843 	for (i = 0; i < v_budget; i++)
11844 		pf->msix_entries[i].entry = i;
11845 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11846 
11847 	if (v_actual < I40E_MIN_MSIX) {
11848 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11849 		kfree(pf->msix_entries);
11850 		pf->msix_entries = NULL;
11851 		pci_disable_msix(pf->pdev);
11852 		return -ENODEV;
11853 
11854 	} else if (v_actual == I40E_MIN_MSIX) {
11855 		/* Adjust for minimal MSIX use */
11856 		pf->num_vmdq_vsis = 0;
11857 		pf->num_vmdq_qps = 0;
11858 		pf->num_lan_qps = 1;
11859 		pf->num_lan_msix = 1;
11860 
11861 	} else if (v_actual != v_budget) {
11862 		/* If we have limited resources, we will start with no vectors
11863 		 * for the special features and then allocate vectors to some
11864 		 * of these features based on the policy and at the end disable
11865 		 * the features that did not get any vectors.
11866 		 */
11867 		int vec;
11868 
11869 		dev_info(&pf->pdev->dev,
11870 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11871 			 v_actual, v_budget);
11872 		/* reserve the misc vector */
11873 		vec = v_actual - 1;
11874 
11875 		/* Scale vector usage down */
11876 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11877 		pf->num_vmdq_vsis = 1;
11878 		pf->num_vmdq_qps = 1;
11879 
11880 		/* partition out the remaining vectors */
11881 		switch (vec) {
11882 		case 2:
11883 			pf->num_lan_msix = 1;
11884 			break;
11885 		case 3:
11886 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11887 				pf->num_lan_msix = 1;
11888 				pf->num_iwarp_msix = 1;
11889 			} else {
11890 				pf->num_lan_msix = 2;
11891 			}
11892 			break;
11893 		default:
11894 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11895 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11896 						 iwarp_requested);
11897 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11898 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11899 			} else {
11900 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11901 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11902 			}
11903 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11904 				pf->num_fdsb_msix = 1;
11905 				vec--;
11906 			}
11907 			pf->num_lan_msix = min_t(int,
11908 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11909 							      pf->num_lan_msix);
11910 			pf->num_lan_qps = pf->num_lan_msix;
11911 			break;
11912 		}
11913 	}
11914 
11915 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
11916 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11917 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
11918 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
11919 	}
11920 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
11921 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11922 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
11923 	}
11924 
11925 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
11926 	    pf->num_iwarp_msix == 0) {
11927 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11928 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
11929 	}
11930 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11931 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11932 		   pf->num_lan_msix,
11933 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11934 		   pf->num_fdsb_msix,
11935 		   pf->num_iwarp_msix);
11936 
11937 	return v_actual;
11938 }
11939 
11940 /**
11941  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11942  * @vsi: the VSI being configured
11943  * @v_idx: index of the vector in the vsi struct
11944  *
11945  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11946  **/
11947 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11948 {
11949 	struct i40e_q_vector *q_vector;
11950 
11951 	/* allocate q_vector */
11952 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11953 	if (!q_vector)
11954 		return -ENOMEM;
11955 
11956 	q_vector->vsi = vsi;
11957 	q_vector->v_idx = v_idx;
11958 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11959 
11960 	if (vsi->netdev)
11961 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11962 
11963 	/* tie q_vector and vsi together */
11964 	vsi->q_vectors[v_idx] = q_vector;
11965 
11966 	return 0;
11967 }
11968 
11969 /**
11970  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11971  * @vsi: the VSI being configured
11972  *
11973  * We allocate one q_vector per queue interrupt.  If allocation fails we
11974  * return -ENOMEM.
11975  **/
11976 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11977 {
11978 	struct i40e_pf *pf = vsi->back;
11979 	int err, v_idx, num_q_vectors;
11980 
11981 	/* if not MSIX, give the one vector only to the LAN VSI */
11982 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11983 		num_q_vectors = vsi->num_q_vectors;
11984 	else if (vsi == pf->vsi[pf->lan_vsi])
11985 		num_q_vectors = 1;
11986 	else
11987 		return -EINVAL;
11988 
11989 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11990 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11991 		if (err)
11992 			goto err_out;
11993 	}
11994 
11995 	return 0;
11996 
11997 err_out:
11998 	while (v_idx--)
11999 		i40e_free_q_vector(vsi, v_idx);
12000 
12001 	return err;
12002 }
12003 
12004 /**
12005  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12006  * @pf: board private structure to initialize
12007  **/
12008 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12009 {
12010 	int vectors = 0;
12011 	ssize_t size;
12012 
12013 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12014 		vectors = i40e_init_msix(pf);
12015 		if (vectors < 0) {
12016 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12017 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12018 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12019 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12020 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12021 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12022 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12023 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12024 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12025 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12026 
12027 			/* rework the queue expectations without MSIX */
12028 			i40e_determine_queue_usage(pf);
12029 		}
12030 	}
12031 
12032 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12033 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12034 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12035 		vectors = pci_enable_msi(pf->pdev);
12036 		if (vectors < 0) {
12037 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12038 				 vectors);
12039 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12040 		}
12041 		vectors = 1;  /* one MSI or Legacy vector */
12042 	}
12043 
12044 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12045 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12046 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12047 
12048 	/* set up vector assignment tracking */
12049 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12050 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12051 	if (!pf->irq_pile)
12052 		return -ENOMEM;
12053 
12054 	pf->irq_pile->num_entries = vectors;
12055 
12056 	/* track first vector for misc interrupts, ignore return */
12057 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12058 
12059 	return 0;
12060 }
12061 
12062 /**
12063  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12064  * @pf: private board data structure
12065  *
12066  * Restore the interrupt scheme that was cleared when we suspended the
12067  * device. This should be called during resume to re-allocate the q_vectors
12068  * and reacquire IRQs.
12069  */
12070 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12071 {
12072 	struct i40e_vsi *vsi;
12073 	int err, i;
12074 
12075 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12076 	 * scheme. We need to re-enabled them here in order to attempt to
12077 	 * re-acquire the MSI or MSI-X vectors
12078 	 */
12079 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12080 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12081 
12082 	err = i40e_init_interrupt_scheme(pf);
12083 	if (err)
12084 		return err;
12085 
12086 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12087 	 * rings together again.
12088 	 */
12089 	i40e_pf_for_each_vsi(pf, i, vsi) {
12090 		err = i40e_vsi_alloc_q_vectors(vsi);
12091 		if (err)
12092 			goto err_unwind;
12093 
12094 		i40e_vsi_map_rings_to_vectors(vsi);
12095 	}
12096 
12097 	err = i40e_setup_misc_vector(pf);
12098 	if (err)
12099 		goto err_unwind;
12100 
12101 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12102 		i40e_client_update_msix_info(pf);
12103 
12104 	return 0;
12105 
12106 err_unwind:
12107 	while (i--) {
12108 		if (pf->vsi[i])
12109 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12110 	}
12111 
12112 	return err;
12113 }
12114 
12115 /**
12116  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12117  * non queue events in recovery mode
12118  * @pf: board private structure
12119  *
12120  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12121  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12122  * This is handled differently than in recovery mode since no Tx/Rx resources
12123  * are being allocated.
12124  **/
12125 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12126 {
12127 	int err;
12128 
12129 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12130 		err = i40e_setup_misc_vector(pf);
12131 
12132 		if (err) {
12133 			dev_info(&pf->pdev->dev,
12134 				 "MSI-X misc vector request failed, error %d\n",
12135 				 err);
12136 			return err;
12137 		}
12138 	} else {
12139 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12140 
12141 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12142 				  pf->int_name, pf);
12143 
12144 		if (err) {
12145 			dev_info(&pf->pdev->dev,
12146 				 "MSI/legacy misc vector request failed, error %d\n",
12147 				 err);
12148 			return err;
12149 		}
12150 		i40e_enable_misc_int_causes(pf);
12151 		i40e_irq_dynamic_enable_icr0(pf);
12152 	}
12153 
12154 	return 0;
12155 }
12156 
12157 /**
12158  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12159  * @pf: board private structure
12160  *
12161  * This sets up the handler for MSIX 0, which is used to manage the
12162  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12163  * when in MSI or Legacy interrupt mode.
12164  **/
12165 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12166 {
12167 	struct i40e_hw *hw = &pf->hw;
12168 	int err = 0;
12169 
12170 	/* Only request the IRQ once, the first time through. */
12171 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12172 		err = request_irq(pf->msix_entries[0].vector,
12173 				  i40e_intr, 0, pf->int_name, pf);
12174 		if (err) {
12175 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12176 			dev_info(&pf->pdev->dev,
12177 				 "request_irq for %s failed: %d\n",
12178 				 pf->int_name, err);
12179 			return -EFAULT;
12180 		}
12181 	}
12182 
12183 	i40e_enable_misc_int_causes(pf);
12184 
12185 	/* associate no queues to the misc vector */
12186 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12187 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12188 
12189 	i40e_flush(hw);
12190 
12191 	i40e_irq_dynamic_enable_icr0(pf);
12192 
12193 	return err;
12194 }
12195 
12196 /**
12197  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12198  * @vsi: Pointer to vsi structure
12199  * @seed: Buffter to store the hash keys
12200  * @lut: Buffer to store the lookup table entries
12201  * @lut_size: Size of buffer to store the lookup table entries
12202  *
12203  * Return 0 on success, negative on failure
12204  */
12205 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12206 			   u8 *lut, u16 lut_size)
12207 {
12208 	struct i40e_pf *pf = vsi->back;
12209 	struct i40e_hw *hw = &pf->hw;
12210 	int ret = 0;
12211 
12212 	if (seed) {
12213 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12214 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12215 		if (ret) {
12216 			dev_info(&pf->pdev->dev,
12217 				 "Cannot get RSS key, err %pe aq_err %s\n",
12218 				 ERR_PTR(ret),
12219 				 i40e_aq_str(&pf->hw,
12220 					     pf->hw.aq.asq_last_status));
12221 			return ret;
12222 		}
12223 	}
12224 
12225 	if (lut) {
12226 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12227 
12228 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12229 		if (ret) {
12230 			dev_info(&pf->pdev->dev,
12231 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12232 				 ERR_PTR(ret),
12233 				 i40e_aq_str(&pf->hw,
12234 					     pf->hw.aq.asq_last_status));
12235 			return ret;
12236 		}
12237 	}
12238 
12239 	return ret;
12240 }
12241 
12242 /**
12243  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12244  * @vsi: Pointer to vsi structure
12245  * @seed: RSS hash seed
12246  * @lut: Lookup table
12247  * @lut_size: Lookup table size
12248  *
12249  * Returns 0 on success, negative on failure
12250  **/
12251 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12252 			       const u8 *lut, u16 lut_size)
12253 {
12254 	struct i40e_pf *pf = vsi->back;
12255 	struct i40e_hw *hw = &pf->hw;
12256 	u16 vf_id = vsi->vf_id;
12257 	u8 i;
12258 
12259 	/* Fill out hash function seed */
12260 	if (seed) {
12261 		u32 *seed_dw = (u32 *)seed;
12262 
12263 		if (vsi->type == I40E_VSI_MAIN) {
12264 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12265 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12266 		} else if (vsi->type == I40E_VSI_SRIOV) {
12267 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12268 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12269 		} else {
12270 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12271 		}
12272 	}
12273 
12274 	if (lut) {
12275 		u32 *lut_dw = (u32 *)lut;
12276 
12277 		if (vsi->type == I40E_VSI_MAIN) {
12278 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12279 				return -EINVAL;
12280 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12281 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12282 		} else if (vsi->type == I40E_VSI_SRIOV) {
12283 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12284 				return -EINVAL;
12285 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12286 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12287 		} else {
12288 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12289 		}
12290 	}
12291 	i40e_flush(hw);
12292 
12293 	return 0;
12294 }
12295 
12296 /**
12297  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12298  * @vsi: Pointer to VSI structure
12299  * @seed: Buffer to store the keys
12300  * @lut: Buffer to store the lookup table entries
12301  * @lut_size: Size of buffer to store the lookup table entries
12302  *
12303  * Returns 0 on success, negative on failure
12304  */
12305 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12306 			    u8 *lut, u16 lut_size)
12307 {
12308 	struct i40e_pf *pf = vsi->back;
12309 	struct i40e_hw *hw = &pf->hw;
12310 	u16 i;
12311 
12312 	if (seed) {
12313 		u32 *seed_dw = (u32 *)seed;
12314 
12315 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12316 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12317 	}
12318 	if (lut) {
12319 		u32 *lut_dw = (u32 *)lut;
12320 
12321 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12322 			return -EINVAL;
12323 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12324 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12325 	}
12326 
12327 	return 0;
12328 }
12329 
12330 /**
12331  * i40e_config_rss - Configure RSS keys and lut
12332  * @vsi: Pointer to VSI structure
12333  * @seed: RSS hash seed
12334  * @lut: Lookup table
12335  * @lut_size: Lookup table size
12336  *
12337  * Returns 0 on success, negative on failure
12338  */
12339 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12340 {
12341 	struct i40e_pf *pf = vsi->back;
12342 
12343 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12344 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12345 	else
12346 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12347 }
12348 
12349 /**
12350  * i40e_get_rss - Get RSS keys and lut
12351  * @vsi: Pointer to VSI structure
12352  * @seed: Buffer to store the keys
12353  * @lut: Buffer to store the lookup table entries
12354  * @lut_size: Size of buffer to store the lookup table entries
12355  *
12356  * Returns 0 on success, negative on failure
12357  */
12358 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12359 {
12360 	struct i40e_pf *pf = vsi->back;
12361 
12362 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12363 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12364 	else
12365 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12366 }
12367 
12368 /**
12369  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12370  * @pf: Pointer to board private structure
12371  * @lut: Lookup table
12372  * @rss_table_size: Lookup table size
12373  * @rss_size: Range of queue number for hashing
12374  */
12375 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12376 		       u16 rss_table_size, u16 rss_size)
12377 {
12378 	u16 i;
12379 
12380 	for (i = 0; i < rss_table_size; i++)
12381 		lut[i] = i % rss_size;
12382 }
12383 
12384 /**
12385  * i40e_pf_config_rss - Prepare for RSS if used
12386  * @pf: board private structure
12387  **/
12388 static int i40e_pf_config_rss(struct i40e_pf *pf)
12389 {
12390 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12391 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12392 	u8 *lut;
12393 	struct i40e_hw *hw = &pf->hw;
12394 	u32 reg_val;
12395 	u64 hena;
12396 	int ret;
12397 
12398 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12399 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12400 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12401 	hena |= i40e_pf_get_default_rss_hena(pf);
12402 
12403 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12404 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12405 
12406 	/* Determine the RSS table size based on the hardware capabilities */
12407 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12408 	reg_val = (pf->rss_table_size == 512) ?
12409 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12410 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12411 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12412 
12413 	/* Determine the RSS size of the VSI */
12414 	if (!vsi->rss_size) {
12415 		u16 qcount;
12416 		/* If the firmware does something weird during VSI init, we
12417 		 * could end up with zero TCs. Check for that to avoid
12418 		 * divide-by-zero. It probably won't pass traffic, but it also
12419 		 * won't panic.
12420 		 */
12421 		qcount = vsi->num_queue_pairs /
12422 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12423 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12424 	}
12425 	if (!vsi->rss_size)
12426 		return -EINVAL;
12427 
12428 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12429 	if (!lut)
12430 		return -ENOMEM;
12431 
12432 	/* Use user configured lut if there is one, otherwise use default */
12433 	if (vsi->rss_lut_user)
12434 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12435 	else
12436 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12437 
12438 	/* Use user configured hash key if there is one, otherwise
12439 	 * use default.
12440 	 */
12441 	if (vsi->rss_hkey_user)
12442 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12443 	else
12444 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12445 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12446 	kfree(lut);
12447 
12448 	return ret;
12449 }
12450 
12451 /**
12452  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12453  * @pf: board private structure
12454  * @queue_count: the requested queue count for rss.
12455  *
12456  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12457  * count which may be different from the requested queue count.
12458  * Note: expects to be called while under rtnl_lock()
12459  **/
12460 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12461 {
12462 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12463 	int new_rss_size;
12464 
12465 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12466 		return 0;
12467 
12468 	queue_count = min_t(int, queue_count, num_online_cpus());
12469 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12470 
12471 	if (queue_count != vsi->num_queue_pairs) {
12472 		u16 qcount;
12473 
12474 		vsi->req_queue_pairs = queue_count;
12475 		i40e_prep_for_reset(pf);
12476 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12477 			return pf->alloc_rss_size;
12478 
12479 		pf->alloc_rss_size = new_rss_size;
12480 
12481 		i40e_reset_and_rebuild(pf, true, true);
12482 
12483 		/* Discard the user configured hash keys and lut, if less
12484 		 * queues are enabled.
12485 		 */
12486 		if (queue_count < vsi->rss_size) {
12487 			i40e_clear_rss_config_user(vsi);
12488 			dev_dbg(&pf->pdev->dev,
12489 				"discard user configured hash keys and lut\n");
12490 		}
12491 
12492 		/* Reset vsi->rss_size, as number of enabled queues changed */
12493 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12494 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12495 
12496 		i40e_pf_config_rss(pf);
12497 	}
12498 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12499 		 vsi->req_queue_pairs, pf->rss_size_max);
12500 	return pf->alloc_rss_size;
12501 }
12502 
12503 /**
12504  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12505  * @pf: board private structure
12506  **/
12507 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12508 {
12509 	bool min_valid, max_valid;
12510 	u32 max_bw, min_bw;
12511 	int status;
12512 
12513 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12514 					   &min_valid, &max_valid);
12515 
12516 	if (!status) {
12517 		if (min_valid)
12518 			pf->min_bw = min_bw;
12519 		if (max_valid)
12520 			pf->max_bw = max_bw;
12521 	}
12522 
12523 	return status;
12524 }
12525 
12526 /**
12527  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12528  * @pf: board private structure
12529  **/
12530 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12531 {
12532 	struct i40e_aqc_configure_partition_bw_data bw_data;
12533 	int status;
12534 
12535 	memset(&bw_data, 0, sizeof(bw_data));
12536 
12537 	/* Set the valid bit for this PF */
12538 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12539 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12540 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12541 
12542 	/* Set the new bandwidths */
12543 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12544 
12545 	return status;
12546 }
12547 
12548 /**
12549  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12550  * @pf: board private structure
12551  **/
12552 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12553 {
12554 	/* Commit temporary BW setting to permanent NVM image */
12555 	enum i40e_admin_queue_err last_aq_status;
12556 	u16 nvm_word;
12557 	int ret;
12558 
12559 	if (pf->hw.partition_id != 1) {
12560 		dev_info(&pf->pdev->dev,
12561 			 "Commit BW only works on partition 1! This is partition %d",
12562 			 pf->hw.partition_id);
12563 		ret = -EOPNOTSUPP;
12564 		goto bw_commit_out;
12565 	}
12566 
12567 	/* Acquire NVM for read access */
12568 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12569 	last_aq_status = pf->hw.aq.asq_last_status;
12570 	if (ret) {
12571 		dev_info(&pf->pdev->dev,
12572 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12573 			 ERR_PTR(ret),
12574 			 i40e_aq_str(&pf->hw, last_aq_status));
12575 		goto bw_commit_out;
12576 	}
12577 
12578 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12579 	ret = i40e_aq_read_nvm(&pf->hw,
12580 			       I40E_SR_NVM_CONTROL_WORD,
12581 			       0x10, sizeof(nvm_word), &nvm_word,
12582 			       false, NULL);
12583 	/* Save off last admin queue command status before releasing
12584 	 * the NVM
12585 	 */
12586 	last_aq_status = pf->hw.aq.asq_last_status;
12587 	i40e_release_nvm(&pf->hw);
12588 	if (ret) {
12589 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12590 			 ERR_PTR(ret),
12591 			 i40e_aq_str(&pf->hw, last_aq_status));
12592 		goto bw_commit_out;
12593 	}
12594 
12595 	/* Wait a bit for NVM release to complete */
12596 	msleep(50);
12597 
12598 	/* Acquire NVM for write access */
12599 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12600 	last_aq_status = pf->hw.aq.asq_last_status;
12601 	if (ret) {
12602 		dev_info(&pf->pdev->dev,
12603 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12604 			 ERR_PTR(ret),
12605 			 i40e_aq_str(&pf->hw, last_aq_status));
12606 		goto bw_commit_out;
12607 	}
12608 	/* Write it back out unchanged to initiate update NVM,
12609 	 * which will force a write of the shadow (alt) RAM to
12610 	 * the NVM - thus storing the bandwidth values permanently.
12611 	 */
12612 	ret = i40e_aq_update_nvm(&pf->hw,
12613 				 I40E_SR_NVM_CONTROL_WORD,
12614 				 0x10, sizeof(nvm_word),
12615 				 &nvm_word, true, 0, NULL);
12616 	/* Save off last admin queue command status before releasing
12617 	 * the NVM
12618 	 */
12619 	last_aq_status = pf->hw.aq.asq_last_status;
12620 	i40e_release_nvm(&pf->hw);
12621 	if (ret)
12622 		dev_info(&pf->pdev->dev,
12623 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12624 			 ERR_PTR(ret),
12625 			 i40e_aq_str(&pf->hw, last_aq_status));
12626 bw_commit_out:
12627 
12628 	return ret;
12629 }
12630 
12631 /**
12632  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12633  * if total port shutdown feature is enabled for this PF
12634  * @pf: board private structure
12635  **/
12636 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12637 {
12638 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12639 #define I40E_FEATURES_ENABLE_PTR		0x2A
12640 #define I40E_CURRENT_SETTING_PTR		0x2B
12641 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12642 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12643 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12644 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12645 	u16 sr_emp_sr_settings_ptr = 0;
12646 	u16 features_enable = 0;
12647 	u16 link_behavior = 0;
12648 	int read_status = 0;
12649 	bool ret = false;
12650 
12651 	read_status = i40e_read_nvm_word(&pf->hw,
12652 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12653 					 &sr_emp_sr_settings_ptr);
12654 	if (read_status)
12655 		goto err_nvm;
12656 	read_status = i40e_read_nvm_word(&pf->hw,
12657 					 sr_emp_sr_settings_ptr +
12658 					 I40E_FEATURES_ENABLE_PTR,
12659 					 &features_enable);
12660 	if (read_status)
12661 		goto err_nvm;
12662 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12663 		read_status = i40e_read_nvm_module_data(&pf->hw,
12664 							I40E_SR_EMP_SR_SETTINGS_PTR,
12665 							I40E_CURRENT_SETTING_PTR,
12666 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12667 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12668 							&link_behavior);
12669 		if (read_status)
12670 			goto err_nvm;
12671 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12672 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12673 	}
12674 	return ret;
12675 
12676 err_nvm:
12677 	dev_warn(&pf->pdev->dev,
12678 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12679 		 ERR_PTR(read_status));
12680 	return ret;
12681 }
12682 
12683 /**
12684  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12685  * @pf: board private structure to initialize
12686  *
12687  * i40e_sw_init initializes the Adapter private data structure.
12688  * Fields are initialized based on PCI device information and
12689  * OS network device settings (MTU size).
12690  **/
12691 static int i40e_sw_init(struct i40e_pf *pf)
12692 {
12693 	int err = 0;
12694 	int size;
12695 	u16 pow;
12696 
12697 	/* Set default capability flags */
12698 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12699 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12700 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12701 
12702 	/* Set default ITR */
12703 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12704 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12705 
12706 	/* Depending on PF configurations, it is possible that the RSS
12707 	 * maximum might end up larger than the available queues
12708 	 */
12709 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12710 	pf->alloc_rss_size = 1;
12711 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12712 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12713 				 pf->hw.func_caps.num_tx_qp);
12714 
12715 	/* find the next higher power-of-2 of num cpus */
12716 	pow = roundup_pow_of_two(num_online_cpus());
12717 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12718 
12719 	if (pf->hw.func_caps.rss) {
12720 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12721 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12722 					   num_online_cpus());
12723 	}
12724 
12725 	/* MFP mode enabled */
12726 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12727 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12728 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12729 		if (i40e_get_partition_bw_setting(pf)) {
12730 			dev_warn(&pf->pdev->dev,
12731 				 "Could not get partition bw settings\n");
12732 		} else {
12733 			dev_info(&pf->pdev->dev,
12734 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12735 				 pf->min_bw, pf->max_bw);
12736 
12737 			/* nudge the Tx scheduler */
12738 			i40e_set_partition_bw_setting(pf);
12739 		}
12740 	}
12741 
12742 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12743 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12744 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12745 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12746 		    pf->hw.num_partitions > 1)
12747 			dev_info(&pf->pdev->dev,
12748 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12749 		else
12750 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12751 		pf->fdir_pf_filter_count =
12752 				 pf->hw.func_caps.fd_filters_guaranteed;
12753 		pf->hw.fdir_shared_filter_count =
12754 				 pf->hw.func_caps.fd_filters_best_effort;
12755 	}
12756 
12757 	/* Enable HW ATR eviction if possible */
12758 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12759 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12760 
12761 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12762 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12763 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12764 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12765 	}
12766 
12767 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12768 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12769 		/* IWARP needs one extra vector for CQP just like MISC.*/
12770 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12771 	}
12772 	/* Stopping FW LLDP engine is supported on XL710 and X722
12773 	 * starting from FW versions determined in i40e_init_adminq.
12774 	 * Stopping the FW LLDP engine is not supported on XL710
12775 	 * if NPAR is functioning so unset this hw flag in this case.
12776 	 */
12777 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12778 	    pf->hw.func_caps.npar_enable)
12779 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12780 
12781 #ifdef CONFIG_PCI_IOV
12782 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12783 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12784 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12785 		pf->num_req_vfs = min_t(int,
12786 					pf->hw.func_caps.num_vfs,
12787 					I40E_MAX_VF_COUNT);
12788 	}
12789 #endif /* CONFIG_PCI_IOV */
12790 	pf->lan_veb = I40E_NO_VEB;
12791 	pf->lan_vsi = I40E_NO_VSI;
12792 
12793 	/* By default FW has this off for performance reasons */
12794 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12795 
12796 	/* set up queue assignment tracking */
12797 	size = sizeof(struct i40e_lump_tracking)
12798 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12799 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12800 	if (!pf->qp_pile) {
12801 		err = -ENOMEM;
12802 		goto sw_init_done;
12803 	}
12804 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12805 
12806 	pf->tx_timeout_recovery_level = 1;
12807 
12808 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12809 	    i40e_is_total_port_shutdown_enabled(pf)) {
12810 		/* Link down on close must be on when total port shutdown
12811 		 * is enabled for a given port
12812 		 */
12813 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12814 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12815 		dev_info(&pf->pdev->dev,
12816 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12817 	}
12818 	mutex_init(&pf->switch_mutex);
12819 
12820 sw_init_done:
12821 	return err;
12822 }
12823 
12824 /**
12825  * i40e_set_ntuple - set the ntuple feature flag and take action
12826  * @pf: board private structure to initialize
12827  * @features: the feature set that the stack is suggesting
12828  *
12829  * returns a bool to indicate if reset needs to happen
12830  **/
12831 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12832 {
12833 	bool need_reset = false;
12834 
12835 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12836 	 * the state changed, we need to reset.
12837 	 */
12838 	if (features & NETIF_F_NTUPLE) {
12839 		/* Enable filters and mark for reset */
12840 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12841 			need_reset = true;
12842 		/* enable FD_SB only if there is MSI-X vector and no cloud
12843 		 * filters exist
12844 		 */
12845 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12846 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12847 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12848 		}
12849 	} else {
12850 		/* turn off filters, mark for reset and clear SW filter list */
12851 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12852 			need_reset = true;
12853 			i40e_fdir_filter_exit(pf);
12854 		}
12855 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12856 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12857 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12858 
12859 		/* reset fd counters */
12860 		pf->fd_add_err = 0;
12861 		pf->fd_atr_cnt = 0;
12862 		/* if ATR was auto disabled it can be re-enabled. */
12863 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12864 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12865 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12866 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12867 	}
12868 	return need_reset;
12869 }
12870 
12871 /**
12872  * i40e_clear_rss_lut - clear the rx hash lookup table
12873  * @vsi: the VSI being configured
12874  **/
12875 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12876 {
12877 	struct i40e_pf *pf = vsi->back;
12878 	struct i40e_hw *hw = &pf->hw;
12879 	u16 vf_id = vsi->vf_id;
12880 	u8 i;
12881 
12882 	if (vsi->type == I40E_VSI_MAIN) {
12883 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12884 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12885 	} else if (vsi->type == I40E_VSI_SRIOV) {
12886 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12887 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12888 	} else {
12889 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12890 	}
12891 }
12892 
12893 /**
12894  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12895  * @vsi: ptr to VSI
12896  * @ena: flag to indicate the on/off setting
12897  */
12898 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12899 {
12900 	bool if_running = netif_running(vsi->netdev) &&
12901 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12902 	int ret;
12903 
12904 	if (if_running)
12905 		i40e_down(vsi);
12906 
12907 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12908 	if (ret)
12909 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12910 	if (if_running)
12911 		i40e_up(vsi);
12912 
12913 	return ret;
12914 }
12915 
12916 /**
12917  * i40e_set_features - set the netdev feature flags
12918  * @netdev: ptr to the netdev being adjusted
12919  * @features: the feature set that the stack is suggesting
12920  * Note: expects to be called while under rtnl_lock()
12921  **/
12922 static int i40e_set_features(struct net_device *netdev,
12923 			     netdev_features_t features)
12924 {
12925 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12926 	struct i40e_vsi *vsi = np->vsi;
12927 	struct i40e_pf *pf = vsi->back;
12928 	bool need_reset;
12929 
12930 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12931 		i40e_pf_config_rss(pf);
12932 	else if (!(features & NETIF_F_RXHASH) &&
12933 		 netdev->features & NETIF_F_RXHASH)
12934 		i40e_clear_rss_lut(vsi);
12935 
12936 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12937 		i40e_vlan_stripping_enable(vsi);
12938 	else
12939 		i40e_vlan_stripping_disable(vsi);
12940 
12941 	if (!(features & NETIF_F_HW_TC) &&
12942 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12943 		dev_err(&pf->pdev->dev,
12944 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12945 		return -EINVAL;
12946 	}
12947 
12948 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12949 		i40e_del_all_macvlans(vsi);
12950 
12951 	need_reset = i40e_set_ntuple(pf, features);
12952 
12953 	if (need_reset)
12954 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12955 
12956 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12957 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12958 
12959 	return 0;
12960 }
12961 
12962 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12963 				    unsigned int table, unsigned int idx,
12964 				    struct udp_tunnel_info *ti)
12965 {
12966 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12967 	struct i40e_hw *hw = &np->vsi->back->hw;
12968 	u8 type, filter_index;
12969 	int ret;
12970 
12971 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12972 						   I40E_AQC_TUNNEL_TYPE_NGE;
12973 
12974 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12975 				     NULL);
12976 	if (ret) {
12977 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
12978 			    ERR_PTR(ret),
12979 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12980 		return -EIO;
12981 	}
12982 
12983 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12984 	return 0;
12985 }
12986 
12987 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12988 				      unsigned int table, unsigned int idx,
12989 				      struct udp_tunnel_info *ti)
12990 {
12991 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12992 	struct i40e_hw *hw = &np->vsi->back->hw;
12993 	int ret;
12994 
12995 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12996 	if (ret) {
12997 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
12998 			    ERR_PTR(ret),
12999 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13000 		return -EIO;
13001 	}
13002 
13003 	return 0;
13004 }
13005 
13006 static int i40e_get_phys_port_id(struct net_device *netdev,
13007 				 struct netdev_phys_item_id *ppid)
13008 {
13009 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13010 	struct i40e_pf *pf = np->vsi->back;
13011 	struct i40e_hw *hw = &pf->hw;
13012 
13013 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13014 		return -EOPNOTSUPP;
13015 
13016 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13017 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13018 
13019 	return 0;
13020 }
13021 
13022 /**
13023  * i40e_ndo_fdb_add - add an entry to the hardware database
13024  * @ndm: the input from the stack
13025  * @tb: pointer to array of nladdr (unused)
13026  * @dev: the net device pointer
13027  * @addr: the MAC address entry being added
13028  * @vid: VLAN ID
13029  * @flags: instructions from stack about fdb operation
13030  * @extack: netlink extended ack, unused currently
13031  */
13032 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13033 			    struct net_device *dev,
13034 			    const unsigned char *addr, u16 vid,
13035 			    u16 flags,
13036 			    struct netlink_ext_ack *extack)
13037 {
13038 	struct i40e_netdev_priv *np = netdev_priv(dev);
13039 	struct i40e_pf *pf = np->vsi->back;
13040 	int err = 0;
13041 
13042 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13043 		return -EOPNOTSUPP;
13044 
13045 	if (vid) {
13046 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13047 		return -EINVAL;
13048 	}
13049 
13050 	/* Hardware does not support aging addresses so if a
13051 	 * ndm_state is given only allow permanent addresses
13052 	 */
13053 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13054 		netdev_info(dev, "FDB only supports static addresses\n");
13055 		return -EINVAL;
13056 	}
13057 
13058 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13059 		err = dev_uc_add_excl(dev, addr);
13060 	else if (is_multicast_ether_addr(addr))
13061 		err = dev_mc_add_excl(dev, addr);
13062 	else
13063 		err = -EINVAL;
13064 
13065 	/* Only return duplicate errors if NLM_F_EXCL is set */
13066 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13067 		err = 0;
13068 
13069 	return err;
13070 }
13071 
13072 /**
13073  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13074  * @dev: the netdev being configured
13075  * @nlh: RTNL message
13076  * @flags: bridge flags
13077  * @extack: netlink extended ack
13078  *
13079  * Inserts a new hardware bridge if not already created and
13080  * enables the bridging mode requested (VEB or VEPA). If the
13081  * hardware bridge has already been inserted and the request
13082  * is to change the mode then that requires a PF reset to
13083  * allow rebuild of the components with required hardware
13084  * bridge mode enabled.
13085  *
13086  * Note: expects to be called while under rtnl_lock()
13087  **/
13088 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13089 				   struct nlmsghdr *nlh,
13090 				   u16 flags,
13091 				   struct netlink_ext_ack *extack)
13092 {
13093 	struct i40e_netdev_priv *np = netdev_priv(dev);
13094 	struct i40e_vsi *vsi = np->vsi;
13095 	struct i40e_pf *pf = vsi->back;
13096 	struct nlattr *attr, *br_spec;
13097 	struct i40e_veb *veb;
13098 	int rem;
13099 
13100 	/* Only for PF VSI for now */
13101 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13102 		return -EOPNOTSUPP;
13103 
13104 	/* Find the HW bridge for PF VSI */
13105 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13106 
13107 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13108 	if (!br_spec)
13109 		return -EINVAL;
13110 
13111 	nla_for_each_nested(attr, br_spec, rem) {
13112 		__u16 mode;
13113 
13114 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13115 			continue;
13116 
13117 		mode = nla_get_u16(attr);
13118 		if ((mode != BRIDGE_MODE_VEPA) &&
13119 		    (mode != BRIDGE_MODE_VEB))
13120 			return -EINVAL;
13121 
13122 		/* Insert a new HW bridge */
13123 		if (!veb) {
13124 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13125 					     vsi->tc_config.enabled_tc);
13126 			if (veb) {
13127 				veb->bridge_mode = mode;
13128 				i40e_config_bridge_mode(veb);
13129 			} else {
13130 				/* No Bridge HW offload available */
13131 				return -ENOENT;
13132 			}
13133 			break;
13134 		} else if (mode != veb->bridge_mode) {
13135 			/* Existing HW bridge but different mode needs reset */
13136 			veb->bridge_mode = mode;
13137 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13138 			if (mode == BRIDGE_MODE_VEB)
13139 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13140 			else
13141 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13142 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13143 			break;
13144 		}
13145 	}
13146 
13147 	return 0;
13148 }
13149 
13150 /**
13151  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13152  * @skb: skb buff
13153  * @pid: process id
13154  * @seq: RTNL message seq #
13155  * @dev: the netdev being configured
13156  * @filter_mask: unused
13157  * @nlflags: netlink flags passed in
13158  *
13159  * Return the mode in which the hardware bridge is operating in
13160  * i.e VEB or VEPA.
13161  **/
13162 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13163 				   struct net_device *dev,
13164 				   u32 __always_unused filter_mask,
13165 				   int nlflags)
13166 {
13167 	struct i40e_netdev_priv *np = netdev_priv(dev);
13168 	struct i40e_vsi *vsi = np->vsi;
13169 	struct i40e_pf *pf = vsi->back;
13170 	struct i40e_veb *veb;
13171 
13172 	/* Only for PF VSI for now */
13173 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13174 		return -EOPNOTSUPP;
13175 
13176 	/* Find the HW bridge for the PF VSI */
13177 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13178 	if (!veb)
13179 		return 0;
13180 
13181 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13182 				       0, 0, nlflags, filter_mask, NULL);
13183 }
13184 
13185 /**
13186  * i40e_features_check - Validate encapsulated packet conforms to limits
13187  * @skb: skb buff
13188  * @dev: This physical port's netdev
13189  * @features: Offload features that the stack believes apply
13190  **/
13191 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13192 					     struct net_device *dev,
13193 					     netdev_features_t features)
13194 {
13195 	size_t len;
13196 
13197 	/* No point in doing any of this if neither checksum nor GSO are
13198 	 * being requested for this frame.  We can rule out both by just
13199 	 * checking for CHECKSUM_PARTIAL
13200 	 */
13201 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13202 		return features;
13203 
13204 	/* We cannot support GSO if the MSS is going to be less than
13205 	 * 64 bytes.  If it is then we need to drop support for GSO.
13206 	 */
13207 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13208 		features &= ~NETIF_F_GSO_MASK;
13209 
13210 	/* MACLEN can support at most 63 words */
13211 	len = skb_network_offset(skb);
13212 	if (len & ~(63 * 2))
13213 		goto out_err;
13214 
13215 	/* IPLEN and EIPLEN can support at most 127 dwords */
13216 	len = skb_network_header_len(skb);
13217 	if (len & ~(127 * 4))
13218 		goto out_err;
13219 
13220 	if (skb->encapsulation) {
13221 		/* L4TUNLEN can support 127 words */
13222 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13223 		if (len & ~(127 * 2))
13224 			goto out_err;
13225 
13226 		/* IPLEN can support at most 127 dwords */
13227 		len = skb_inner_transport_header(skb) -
13228 		      skb_inner_network_header(skb);
13229 		if (len & ~(127 * 4))
13230 			goto out_err;
13231 	}
13232 
13233 	/* No need to validate L4LEN as TCP is the only protocol with a
13234 	 * flexible value and we support all possible values supported
13235 	 * by TCP, which is at most 15 dwords
13236 	 */
13237 
13238 	return features;
13239 out_err:
13240 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13241 }
13242 
13243 /**
13244  * i40e_xdp_setup - add/remove an XDP program
13245  * @vsi: VSI to changed
13246  * @prog: XDP program
13247  * @extack: netlink extended ack
13248  **/
13249 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13250 			  struct netlink_ext_ack *extack)
13251 {
13252 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13253 	struct i40e_pf *pf = vsi->back;
13254 	struct bpf_prog *old_prog;
13255 	bool need_reset;
13256 	int i;
13257 
13258 	/* Don't allow frames that span over multiple buffers */
13259 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13260 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13261 		return -EINVAL;
13262 	}
13263 
13264 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13265 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13266 
13267 	if (need_reset)
13268 		i40e_prep_for_reset(pf);
13269 
13270 	/* VSI shall be deleted in a moment, just return EINVAL */
13271 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13272 		return -EINVAL;
13273 
13274 	old_prog = xchg(&vsi->xdp_prog, prog);
13275 
13276 	if (need_reset) {
13277 		if (!prog) {
13278 			xdp_features_clear_redirect_target(vsi->netdev);
13279 			/* Wait until ndo_xsk_wakeup completes. */
13280 			synchronize_rcu();
13281 		}
13282 		i40e_reset_and_rebuild(pf, true, true);
13283 	}
13284 
13285 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13286 		if (i40e_realloc_rx_bi_zc(vsi, true))
13287 			return -ENOMEM;
13288 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13289 		if (i40e_realloc_rx_bi_zc(vsi, false))
13290 			return -ENOMEM;
13291 	}
13292 
13293 	for (i = 0; i < vsi->num_queue_pairs; i++)
13294 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13295 
13296 	if (old_prog)
13297 		bpf_prog_put(old_prog);
13298 
13299 	/* Kick start the NAPI context if there is an AF_XDP socket open
13300 	 * on that queue id. This so that receiving will start.
13301 	 */
13302 	if (need_reset && prog) {
13303 		for (i = 0; i < vsi->num_queue_pairs; i++)
13304 			if (vsi->xdp_rings[i]->xsk_pool)
13305 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13306 						      XDP_WAKEUP_RX);
13307 		xdp_features_set_redirect_target(vsi->netdev, true);
13308 	}
13309 
13310 	return 0;
13311 }
13312 
13313 /**
13314  * i40e_enter_busy_conf - Enters busy config state
13315  * @vsi: vsi
13316  *
13317  * Returns 0 on success, <0 for failure.
13318  **/
13319 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13320 {
13321 	struct i40e_pf *pf = vsi->back;
13322 	int timeout = 50;
13323 
13324 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13325 		timeout--;
13326 		if (!timeout)
13327 			return -EBUSY;
13328 		usleep_range(1000, 2000);
13329 	}
13330 
13331 	return 0;
13332 }
13333 
13334 /**
13335  * i40e_exit_busy_conf - Exits busy config state
13336  * @vsi: vsi
13337  **/
13338 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13339 {
13340 	struct i40e_pf *pf = vsi->back;
13341 
13342 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13343 }
13344 
13345 /**
13346  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13347  * @vsi: vsi
13348  * @queue_pair: queue pair
13349  **/
13350 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13351 {
13352 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13353 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13354 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13355 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13356 	if (i40e_enabled_xdp_vsi(vsi)) {
13357 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13358 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13359 	}
13360 }
13361 
13362 /**
13363  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13364  * @vsi: vsi
13365  * @queue_pair: queue pair
13366  **/
13367 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13368 {
13369 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13370 	if (i40e_enabled_xdp_vsi(vsi)) {
13371 		/* Make sure that in-progress ndo_xdp_xmit calls are
13372 		 * completed.
13373 		 */
13374 		synchronize_rcu();
13375 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13376 	}
13377 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13378 }
13379 
13380 /**
13381  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13382  * @vsi: vsi
13383  * @queue_pair: queue pair
13384  * @enable: true for enable, false for disable
13385  **/
13386 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13387 					bool enable)
13388 {
13389 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13390 	struct i40e_q_vector *q_vector = rxr->q_vector;
13391 
13392 	if (!vsi->netdev)
13393 		return;
13394 
13395 	/* All rings in a qp belong to the same qvector. */
13396 	if (q_vector->rx.ring || q_vector->tx.ring) {
13397 		if (enable)
13398 			napi_enable(&q_vector->napi);
13399 		else
13400 			napi_disable(&q_vector->napi);
13401 	}
13402 }
13403 
13404 /**
13405  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13406  * @vsi: vsi
13407  * @queue_pair: queue pair
13408  * @enable: true for enable, false for disable
13409  *
13410  * Returns 0 on success, <0 on failure.
13411  **/
13412 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13413 					bool enable)
13414 {
13415 	struct i40e_pf *pf = vsi->back;
13416 	int pf_q, ret = 0;
13417 
13418 	pf_q = vsi->base_queue + queue_pair;
13419 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13420 				     false /*is xdp*/, enable);
13421 	if (ret) {
13422 		dev_info(&pf->pdev->dev,
13423 			 "VSI seid %d Tx ring %d %sable timeout\n",
13424 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13425 		return ret;
13426 	}
13427 
13428 	i40e_control_rx_q(pf, pf_q, enable);
13429 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13430 	if (ret) {
13431 		dev_info(&pf->pdev->dev,
13432 			 "VSI seid %d Rx ring %d %sable timeout\n",
13433 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13434 		return ret;
13435 	}
13436 
13437 	/* Due to HW errata, on Rx disable only, the register can
13438 	 * indicate done before it really is. Needs 50ms to be sure
13439 	 */
13440 	if (!enable)
13441 		mdelay(50);
13442 
13443 	if (!i40e_enabled_xdp_vsi(vsi))
13444 		return ret;
13445 
13446 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13447 				     pf_q + vsi->alloc_queue_pairs,
13448 				     true /*is xdp*/, enable);
13449 	if (ret) {
13450 		dev_info(&pf->pdev->dev,
13451 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13452 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13453 	}
13454 
13455 	return ret;
13456 }
13457 
13458 /**
13459  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13460  * @vsi: vsi
13461  * @queue_pair: queue_pair
13462  **/
13463 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13464 {
13465 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13466 	struct i40e_pf *pf = vsi->back;
13467 	struct i40e_hw *hw = &pf->hw;
13468 
13469 	/* All rings in a qp belong to the same qvector. */
13470 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13471 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13472 	else
13473 		i40e_irq_dynamic_enable_icr0(pf);
13474 
13475 	i40e_flush(hw);
13476 }
13477 
13478 /**
13479  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13480  * @vsi: vsi
13481  * @queue_pair: queue_pair
13482  **/
13483 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13484 {
13485 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13486 	struct i40e_pf *pf = vsi->back;
13487 	struct i40e_hw *hw = &pf->hw;
13488 
13489 	/* For simplicity, instead of removing the qp interrupt causes
13490 	 * from the interrupt linked list, we simply disable the interrupt, and
13491 	 * leave the list intact.
13492 	 *
13493 	 * All rings in a qp belong to the same qvector.
13494 	 */
13495 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13496 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13497 
13498 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13499 		i40e_flush(hw);
13500 		synchronize_irq(pf->msix_entries[intpf].vector);
13501 	} else {
13502 		/* Legacy and MSI mode - this stops all interrupt handling */
13503 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13504 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13505 		i40e_flush(hw);
13506 		synchronize_irq(pf->pdev->irq);
13507 	}
13508 }
13509 
13510 /**
13511  * i40e_queue_pair_disable - Disables a queue pair
13512  * @vsi: vsi
13513  * @queue_pair: queue pair
13514  *
13515  * Returns 0 on success, <0 on failure.
13516  **/
13517 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13518 {
13519 	int err;
13520 
13521 	err = i40e_enter_busy_conf(vsi);
13522 	if (err)
13523 		return err;
13524 
13525 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13526 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13527 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13528 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13529 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13530 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13531 
13532 	return err;
13533 }
13534 
13535 /**
13536  * i40e_queue_pair_enable - Enables a queue pair
13537  * @vsi: vsi
13538  * @queue_pair: queue pair
13539  *
13540  * Returns 0 on success, <0 on failure.
13541  **/
13542 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13543 {
13544 	int err;
13545 
13546 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13547 	if (err)
13548 		return err;
13549 
13550 	if (i40e_enabled_xdp_vsi(vsi)) {
13551 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13552 		if (err)
13553 			return err;
13554 	}
13555 
13556 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13557 	if (err)
13558 		return err;
13559 
13560 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13561 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13562 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13563 
13564 	i40e_exit_busy_conf(vsi);
13565 
13566 	return err;
13567 }
13568 
13569 /**
13570  * i40e_xdp - implements ndo_bpf for i40e
13571  * @dev: netdevice
13572  * @xdp: XDP command
13573  **/
13574 static int i40e_xdp(struct net_device *dev,
13575 		    struct netdev_bpf *xdp)
13576 {
13577 	struct i40e_netdev_priv *np = netdev_priv(dev);
13578 	struct i40e_vsi *vsi = np->vsi;
13579 
13580 	if (vsi->type != I40E_VSI_MAIN)
13581 		return -EINVAL;
13582 
13583 	switch (xdp->command) {
13584 	case XDP_SETUP_PROG:
13585 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13586 	case XDP_SETUP_XSK_POOL:
13587 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13588 					   xdp->xsk.queue_id);
13589 	default:
13590 		return -EINVAL;
13591 	}
13592 }
13593 
13594 static const struct net_device_ops i40e_netdev_ops = {
13595 	.ndo_open		= i40e_open,
13596 	.ndo_stop		= i40e_close,
13597 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13598 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13599 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13600 	.ndo_validate_addr	= eth_validate_addr,
13601 	.ndo_set_mac_address	= i40e_set_mac,
13602 	.ndo_change_mtu		= i40e_change_mtu,
13603 	.ndo_eth_ioctl		= i40e_ioctl,
13604 	.ndo_tx_timeout		= i40e_tx_timeout,
13605 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13606 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13607 #ifdef CONFIG_NET_POLL_CONTROLLER
13608 	.ndo_poll_controller	= i40e_netpoll,
13609 #endif
13610 	.ndo_setup_tc		= __i40e_setup_tc,
13611 	.ndo_select_queue	= i40e_lan_select_queue,
13612 	.ndo_set_features	= i40e_set_features,
13613 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13614 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13615 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13616 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13617 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13618 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13619 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13620 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13621 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13622 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13623 	.ndo_features_check	= i40e_features_check,
13624 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13625 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13626 	.ndo_bpf		= i40e_xdp,
13627 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13628 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13629 	.ndo_dfwd_add_station	= i40e_fwd_add,
13630 	.ndo_dfwd_del_station	= i40e_fwd_del,
13631 };
13632 
13633 /**
13634  * i40e_config_netdev - Setup the netdev flags
13635  * @vsi: the VSI being configured
13636  *
13637  * Returns 0 on success, negative value on failure
13638  **/
13639 static int i40e_config_netdev(struct i40e_vsi *vsi)
13640 {
13641 	struct i40e_pf *pf = vsi->back;
13642 	struct i40e_hw *hw = &pf->hw;
13643 	struct i40e_netdev_priv *np;
13644 	struct net_device *netdev;
13645 	u8 broadcast[ETH_ALEN];
13646 	u8 mac_addr[ETH_ALEN];
13647 	int etherdev_size;
13648 	netdev_features_t hw_enc_features;
13649 	netdev_features_t hw_features;
13650 
13651 	etherdev_size = sizeof(struct i40e_netdev_priv);
13652 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13653 	if (!netdev)
13654 		return -ENOMEM;
13655 
13656 	vsi->netdev = netdev;
13657 	np = netdev_priv(netdev);
13658 	np->vsi = vsi;
13659 
13660 	hw_enc_features = NETIF_F_SG			|
13661 			  NETIF_F_HW_CSUM		|
13662 			  NETIF_F_HIGHDMA		|
13663 			  NETIF_F_SOFT_FEATURES		|
13664 			  NETIF_F_TSO			|
13665 			  NETIF_F_TSO_ECN		|
13666 			  NETIF_F_TSO6			|
13667 			  NETIF_F_GSO_GRE		|
13668 			  NETIF_F_GSO_GRE_CSUM		|
13669 			  NETIF_F_GSO_PARTIAL		|
13670 			  NETIF_F_GSO_IPXIP4		|
13671 			  NETIF_F_GSO_IPXIP6		|
13672 			  NETIF_F_GSO_UDP_TUNNEL	|
13673 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13674 			  NETIF_F_GSO_UDP_L4		|
13675 			  NETIF_F_SCTP_CRC		|
13676 			  NETIF_F_RXHASH		|
13677 			  NETIF_F_RXCSUM		|
13678 			  0;
13679 
13680 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13681 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13682 
13683 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13684 
13685 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13686 
13687 	netdev->hw_enc_features |= hw_enc_features;
13688 
13689 	/* record features VLANs can make use of */
13690 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13691 
13692 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13693 				   NETIF_F_GSO_GRE_CSUM |	\
13694 				   NETIF_F_GSO_IPXIP4 |		\
13695 				   NETIF_F_GSO_IPXIP6 |		\
13696 				   NETIF_F_GSO_UDP_TUNNEL |	\
13697 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13698 
13699 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13700 	netdev->features |= NETIF_F_GSO_PARTIAL |
13701 			    I40E_GSO_PARTIAL_FEATURES;
13702 
13703 	netdev->mpls_features |= NETIF_F_SG;
13704 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13705 	netdev->mpls_features |= NETIF_F_TSO;
13706 	netdev->mpls_features |= NETIF_F_TSO6;
13707 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13708 
13709 	/* enable macvlan offloads */
13710 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13711 
13712 	hw_features = hw_enc_features		|
13713 		      NETIF_F_HW_VLAN_CTAG_TX	|
13714 		      NETIF_F_HW_VLAN_CTAG_RX;
13715 
13716 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13717 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13718 
13719 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13720 
13721 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13722 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13723 
13724 	netdev->features &= ~NETIF_F_HW_TC;
13725 
13726 	if (vsi->type == I40E_VSI_MAIN) {
13727 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13728 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13729 		/* The following steps are necessary for two reasons. First,
13730 		 * some older NVM configurations load a default MAC-VLAN
13731 		 * filter that will accept any tagged packet, and we want to
13732 		 * replace this with a normal filter. Additionally, it is
13733 		 * possible our MAC address was provided by the platform using
13734 		 * Open Firmware or similar.
13735 		 *
13736 		 * Thus, we need to remove the default filter and install one
13737 		 * specific to the MAC address.
13738 		 */
13739 		i40e_rm_default_mac_filter(vsi, mac_addr);
13740 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13741 		i40e_add_mac_filter(vsi, mac_addr);
13742 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13743 
13744 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13745 				       NETDEV_XDP_ACT_REDIRECT |
13746 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13747 				       NETDEV_XDP_ACT_RX_SG;
13748 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13749 	} else {
13750 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13751 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13752 		 * the end, which is 4 bytes long, so force truncation of the
13753 		 * original name by IFNAMSIZ - 4
13754 		 */
13755 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13756 			 IFNAMSIZ - 4,
13757 			 pf->vsi[pf->lan_vsi]->netdev->name);
13758 		eth_random_addr(mac_addr);
13759 
13760 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13761 		i40e_add_mac_filter(vsi, mac_addr);
13762 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13763 	}
13764 
13765 	/* Add the broadcast filter so that we initially will receive
13766 	 * broadcast packets. Note that when a new VLAN is first added the
13767 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13768 	 * specific filters as part of transitioning into "vlan" operation.
13769 	 * When more VLANs are added, the driver will copy each existing MAC
13770 	 * filter and add it for the new VLAN.
13771 	 *
13772 	 * Broadcast filters are handled specially by
13773 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13774 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13775 	 * filter. The subtask will update the correct broadcast promiscuous
13776 	 * bits as VLANs become active or inactive.
13777 	 */
13778 	eth_broadcast_addr(broadcast);
13779 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13780 	i40e_add_mac_filter(vsi, broadcast);
13781 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13782 
13783 	eth_hw_addr_set(netdev, mac_addr);
13784 	ether_addr_copy(netdev->perm_addr, mac_addr);
13785 
13786 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13787 	netdev->neigh_priv_len = sizeof(u32) * 4;
13788 
13789 	netdev->priv_flags |= IFF_UNICAST_FLT;
13790 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13791 	/* Setup netdev TC information */
13792 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13793 
13794 	netdev->netdev_ops = &i40e_netdev_ops;
13795 	netdev->watchdog_timeo = 5 * HZ;
13796 	i40e_set_ethtool_ops(netdev);
13797 
13798 	/* MTU range: 68 - 9706 */
13799 	netdev->min_mtu = ETH_MIN_MTU;
13800 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13801 
13802 	return 0;
13803 }
13804 
13805 /**
13806  * i40e_vsi_delete - Delete a VSI from the switch
13807  * @vsi: the VSI being removed
13808  *
13809  * Returns 0 on success, negative value on failure
13810  **/
13811 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13812 {
13813 	/* remove default VSI is not allowed */
13814 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13815 		return;
13816 
13817 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13818 }
13819 
13820 /**
13821  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13822  * @vsi: the VSI being queried
13823  *
13824  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13825  **/
13826 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13827 {
13828 	struct i40e_veb *veb;
13829 	struct i40e_pf *pf = vsi->back;
13830 
13831 	/* Uplink is not a bridge so default to VEB */
13832 	if (vsi->veb_idx >= I40E_MAX_VEB)
13833 		return 1;
13834 
13835 	veb = pf->veb[vsi->veb_idx];
13836 	if (!veb) {
13837 		dev_info(&pf->pdev->dev,
13838 			 "There is no veb associated with the bridge\n");
13839 		return -ENOENT;
13840 	}
13841 
13842 	/* Uplink is a bridge in VEPA mode */
13843 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13844 		return 0;
13845 	} else {
13846 		/* Uplink is a bridge in VEB mode */
13847 		return 1;
13848 	}
13849 
13850 	/* VEPA is now default bridge, so return 0 */
13851 	return 0;
13852 }
13853 
13854 /**
13855  * i40e_add_vsi - Add a VSI to the switch
13856  * @vsi: the VSI being configured
13857  *
13858  * This initializes a VSI context depending on the VSI type to be added and
13859  * passes it down to the add_vsi aq command.
13860  **/
13861 static int i40e_add_vsi(struct i40e_vsi *vsi)
13862 {
13863 	int ret = -ENODEV;
13864 	struct i40e_pf *pf = vsi->back;
13865 	struct i40e_hw *hw = &pf->hw;
13866 	struct i40e_vsi_context ctxt;
13867 	struct i40e_mac_filter *f;
13868 	struct hlist_node *h;
13869 	int bkt;
13870 
13871 	u8 enabled_tc = 0x1; /* TC0 enabled */
13872 	int f_count = 0;
13873 
13874 	memset(&ctxt, 0, sizeof(ctxt));
13875 	switch (vsi->type) {
13876 	case I40E_VSI_MAIN:
13877 		/* The PF's main VSI is already setup as part of the
13878 		 * device initialization, so we'll not bother with
13879 		 * the add_vsi call, but we will retrieve the current
13880 		 * VSI context.
13881 		 */
13882 		ctxt.seid = pf->main_vsi_seid;
13883 		ctxt.pf_num = pf->hw.pf_id;
13884 		ctxt.vf_num = 0;
13885 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13886 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13887 		if (ret) {
13888 			dev_info(&pf->pdev->dev,
13889 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13890 				 ERR_PTR(ret),
13891 				 i40e_aq_str(&pf->hw,
13892 					     pf->hw.aq.asq_last_status));
13893 			return -ENOENT;
13894 		}
13895 		vsi->info = ctxt.info;
13896 		vsi->info.valid_sections = 0;
13897 
13898 		vsi->seid = ctxt.seid;
13899 		vsi->id = ctxt.vsi_number;
13900 
13901 		enabled_tc = i40e_pf_get_tc_map(pf);
13902 
13903 		/* Source pruning is enabled by default, so the flag is
13904 		 * negative logic - if it's set, we need to fiddle with
13905 		 * the VSI to disable source pruning.
13906 		 */
13907 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13908 			memset(&ctxt, 0, sizeof(ctxt));
13909 			ctxt.seid = pf->main_vsi_seid;
13910 			ctxt.pf_num = pf->hw.pf_id;
13911 			ctxt.vf_num = 0;
13912 			ctxt.info.valid_sections |=
13913 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13914 			ctxt.info.switch_id =
13915 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13916 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13917 			if (ret) {
13918 				dev_info(&pf->pdev->dev,
13919 					 "update vsi failed, err %d aq_err %s\n",
13920 					 ret,
13921 					 i40e_aq_str(&pf->hw,
13922 						     pf->hw.aq.asq_last_status));
13923 				ret = -ENOENT;
13924 				goto err;
13925 			}
13926 		}
13927 
13928 		/* MFP mode setup queue map and update VSI */
13929 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13930 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13931 			memset(&ctxt, 0, sizeof(ctxt));
13932 			ctxt.seid = pf->main_vsi_seid;
13933 			ctxt.pf_num = pf->hw.pf_id;
13934 			ctxt.vf_num = 0;
13935 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13936 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13937 			if (ret) {
13938 				dev_info(&pf->pdev->dev,
13939 					 "update vsi failed, err %pe aq_err %s\n",
13940 					 ERR_PTR(ret),
13941 					 i40e_aq_str(&pf->hw,
13942 						    pf->hw.aq.asq_last_status));
13943 				ret = -ENOENT;
13944 				goto err;
13945 			}
13946 			/* update the local VSI info queue map */
13947 			i40e_vsi_update_queue_map(vsi, &ctxt);
13948 			vsi->info.valid_sections = 0;
13949 		} else {
13950 			/* Default/Main VSI is only enabled for TC0
13951 			 * reconfigure it to enable all TCs that are
13952 			 * available on the port in SFP mode.
13953 			 * For MFP case the iSCSI PF would use this
13954 			 * flow to enable LAN+iSCSI TC.
13955 			 */
13956 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13957 			if (ret) {
13958 				/* Single TC condition is not fatal,
13959 				 * message and continue
13960 				 */
13961 				dev_info(&pf->pdev->dev,
13962 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
13963 					 enabled_tc,
13964 					 ERR_PTR(ret),
13965 					 i40e_aq_str(&pf->hw,
13966 						    pf->hw.aq.asq_last_status));
13967 			}
13968 		}
13969 		break;
13970 
13971 	case I40E_VSI_FDIR:
13972 		ctxt.pf_num = hw->pf_id;
13973 		ctxt.vf_num = 0;
13974 		ctxt.uplink_seid = vsi->uplink_seid;
13975 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13976 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13977 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
13978 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13979 			ctxt.info.valid_sections |=
13980 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13981 			ctxt.info.switch_id =
13982 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13983 		}
13984 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13985 		break;
13986 
13987 	case I40E_VSI_VMDQ2:
13988 		ctxt.pf_num = hw->pf_id;
13989 		ctxt.vf_num = 0;
13990 		ctxt.uplink_seid = vsi->uplink_seid;
13991 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13992 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13993 
13994 		/* This VSI is connected to VEB so the switch_id
13995 		 * should be set to zero by default.
13996 		 */
13997 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13998 			ctxt.info.valid_sections |=
13999 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14000 			ctxt.info.switch_id =
14001 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14002 		}
14003 
14004 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14005 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14006 		break;
14007 
14008 	case I40E_VSI_SRIOV:
14009 		ctxt.pf_num = hw->pf_id;
14010 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14011 		ctxt.uplink_seid = vsi->uplink_seid;
14012 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14013 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14014 
14015 		/* This VSI is connected to VEB so the switch_id
14016 		 * should be set to zero by default.
14017 		 */
14018 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14019 			ctxt.info.valid_sections |=
14020 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14021 			ctxt.info.switch_id =
14022 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14023 		}
14024 
14025 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14026 			ctxt.info.valid_sections |=
14027 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14028 			ctxt.info.queueing_opt_flags |=
14029 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14030 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14031 		}
14032 
14033 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14034 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14035 		if (pf->vf[vsi->vf_id].spoofchk) {
14036 			ctxt.info.valid_sections |=
14037 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14038 			ctxt.info.sec_flags |=
14039 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14040 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14041 		}
14042 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14043 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14044 		break;
14045 
14046 	case I40E_VSI_IWARP:
14047 		/* send down message to iWARP */
14048 		break;
14049 
14050 	default:
14051 		return -ENODEV;
14052 	}
14053 
14054 	if (vsi->type != I40E_VSI_MAIN) {
14055 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14056 		if (ret) {
14057 			dev_info(&vsi->back->pdev->dev,
14058 				 "add vsi failed, err %pe aq_err %s\n",
14059 				 ERR_PTR(ret),
14060 				 i40e_aq_str(&pf->hw,
14061 					     pf->hw.aq.asq_last_status));
14062 			ret = -ENOENT;
14063 			goto err;
14064 		}
14065 		vsi->info = ctxt.info;
14066 		vsi->info.valid_sections = 0;
14067 		vsi->seid = ctxt.seid;
14068 		vsi->id = ctxt.vsi_number;
14069 	}
14070 
14071 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14072 	vsi->active_filters = 0;
14073 	/* If macvlan filters already exist, force them to get loaded */
14074 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14075 		f->state = I40E_FILTER_NEW;
14076 		f_count++;
14077 	}
14078 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14079 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14080 
14081 	if (f_count) {
14082 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14083 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14084 	}
14085 
14086 	/* Update VSI BW information */
14087 	ret = i40e_vsi_get_bw_info(vsi);
14088 	if (ret) {
14089 		dev_info(&pf->pdev->dev,
14090 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14091 			 ERR_PTR(ret),
14092 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14093 		/* VSI is already added so not tearing that up */
14094 		ret = 0;
14095 	}
14096 
14097 err:
14098 	return ret;
14099 }
14100 
14101 /**
14102  * i40e_vsi_release - Delete a VSI and free its resources
14103  * @vsi: the VSI being removed
14104  *
14105  * Returns 0 on success or < 0 on error
14106  **/
14107 int i40e_vsi_release(struct i40e_vsi *vsi)
14108 {
14109 	struct i40e_mac_filter *f;
14110 	struct hlist_node *h;
14111 	struct i40e_veb *veb;
14112 	struct i40e_pf *pf;
14113 	u16 uplink_seid;
14114 	int i, n, bkt;
14115 
14116 	pf = vsi->back;
14117 
14118 	/* release of a VEB-owner or last VSI is not allowed */
14119 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14120 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14121 			 vsi->seid, vsi->uplink_seid);
14122 		return -ENODEV;
14123 	}
14124 	if (vsi == pf->vsi[pf->lan_vsi] &&
14125 	    !test_bit(__I40E_DOWN, pf->state)) {
14126 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14127 		return -ENODEV;
14128 	}
14129 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14130 	uplink_seid = vsi->uplink_seid;
14131 
14132 	if (vsi->type != I40E_VSI_SRIOV) {
14133 		if (vsi->netdev_registered) {
14134 			vsi->netdev_registered = false;
14135 			if (vsi->netdev) {
14136 				/* results in a call to i40e_close() */
14137 				unregister_netdev(vsi->netdev);
14138 			}
14139 		} else {
14140 			i40e_vsi_close(vsi);
14141 		}
14142 		i40e_vsi_disable_irq(vsi);
14143 	}
14144 
14145 	if (vsi->type == I40E_VSI_MAIN)
14146 		i40e_devlink_destroy_port(pf);
14147 
14148 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14149 
14150 	/* clear the sync flag on all filters */
14151 	if (vsi->netdev) {
14152 		__dev_uc_unsync(vsi->netdev, NULL);
14153 		__dev_mc_unsync(vsi->netdev, NULL);
14154 	}
14155 
14156 	/* make sure any remaining filters are marked for deletion */
14157 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14158 		__i40e_del_filter(vsi, f);
14159 
14160 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14161 
14162 	i40e_sync_vsi_filters(vsi);
14163 
14164 	i40e_vsi_delete(vsi);
14165 	i40e_vsi_free_q_vectors(vsi);
14166 	if (vsi->netdev) {
14167 		free_netdev(vsi->netdev);
14168 		vsi->netdev = NULL;
14169 	}
14170 	i40e_vsi_clear_rings(vsi);
14171 	i40e_vsi_clear(vsi);
14172 
14173 	/* If this was the last thing on the VEB, except for the
14174 	 * controlling VSI, remove the VEB, which puts the controlling
14175 	 * VSI onto the uplink port.
14176 	 *
14177 	 * Well, okay, there's one more exception here: don't remove
14178 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14179 	 * from up the network stack.
14180 	 */
14181 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14182 	if (veb && veb->uplink_seid) {
14183 		n = 0;
14184 
14185 		/* Count non-controlling VSIs present on  the VEB */
14186 		i40e_pf_for_each_vsi(pf, i, vsi)
14187 			if (vsi->uplink_seid == uplink_seid &&
14188 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14189 				n++;
14190 
14191 		/* If there is no VSI except the control one then release
14192 		 * the VEB and put the control VSI onto VEB uplink.
14193 		 */
14194 		if (!n)
14195 			i40e_veb_release(veb);
14196 	}
14197 
14198 	return 0;
14199 }
14200 
14201 /**
14202  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14203  * @vsi: ptr to the VSI
14204  *
14205  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14206  * corresponding SW VSI structure and initializes num_queue_pairs for the
14207  * newly allocated VSI.
14208  *
14209  * Returns 0 on success or negative on failure
14210  **/
14211 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14212 {
14213 	int ret = -ENOENT;
14214 	struct i40e_pf *pf = vsi->back;
14215 
14216 	if (vsi->q_vectors[0]) {
14217 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14218 			 vsi->seid);
14219 		return -EEXIST;
14220 	}
14221 
14222 	if (vsi->base_vector) {
14223 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14224 			 vsi->seid, vsi->base_vector);
14225 		return -EEXIST;
14226 	}
14227 
14228 	ret = i40e_vsi_alloc_q_vectors(vsi);
14229 	if (ret) {
14230 		dev_info(&pf->pdev->dev,
14231 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14232 			 vsi->num_q_vectors, vsi->seid, ret);
14233 		vsi->num_q_vectors = 0;
14234 		goto vector_setup_out;
14235 	}
14236 
14237 	/* In Legacy mode, we do not have to get any other vector since we
14238 	 * piggyback on the misc/ICR0 for queue interrupts.
14239 	*/
14240 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14241 		return ret;
14242 	if (vsi->num_q_vectors)
14243 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14244 						 vsi->num_q_vectors, vsi->idx);
14245 	if (vsi->base_vector < 0) {
14246 		dev_info(&pf->pdev->dev,
14247 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14248 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14249 		i40e_vsi_free_q_vectors(vsi);
14250 		ret = -ENOENT;
14251 		goto vector_setup_out;
14252 	}
14253 
14254 vector_setup_out:
14255 	return ret;
14256 }
14257 
14258 /**
14259  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14260  * @vsi: pointer to the vsi.
14261  *
14262  * This re-allocates a vsi's queue resources.
14263  *
14264  * Returns pointer to the successfully allocated and configured VSI sw struct
14265  * on success, otherwise returns NULL on failure.
14266  **/
14267 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14268 {
14269 	u16 alloc_queue_pairs;
14270 	struct i40e_pf *pf;
14271 	u8 enabled_tc;
14272 	int ret;
14273 
14274 	if (!vsi)
14275 		return NULL;
14276 
14277 	pf = vsi->back;
14278 
14279 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14280 	i40e_vsi_clear_rings(vsi);
14281 
14282 	i40e_vsi_free_arrays(vsi, false);
14283 	i40e_set_num_rings_in_vsi(vsi);
14284 	ret = i40e_vsi_alloc_arrays(vsi, false);
14285 	if (ret)
14286 		goto err_vsi;
14287 
14288 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14289 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14290 
14291 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14292 	if (ret < 0) {
14293 		dev_info(&pf->pdev->dev,
14294 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14295 			 alloc_queue_pairs, vsi->seid, ret);
14296 		goto err_vsi;
14297 	}
14298 	vsi->base_queue = ret;
14299 
14300 	/* Update the FW view of the VSI. Force a reset of TC and queue
14301 	 * layout configurations.
14302 	 */
14303 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14304 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14305 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14306 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14307 	if (vsi->type == I40E_VSI_MAIN)
14308 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14309 
14310 	/* assign it some queues */
14311 	ret = i40e_alloc_rings(vsi);
14312 	if (ret)
14313 		goto err_rings;
14314 
14315 	/* map all of the rings to the q_vectors */
14316 	i40e_vsi_map_rings_to_vectors(vsi);
14317 	return vsi;
14318 
14319 err_rings:
14320 	i40e_vsi_free_q_vectors(vsi);
14321 	if (vsi->netdev_registered) {
14322 		vsi->netdev_registered = false;
14323 		unregister_netdev(vsi->netdev);
14324 		free_netdev(vsi->netdev);
14325 		vsi->netdev = NULL;
14326 	}
14327 	if (vsi->type == I40E_VSI_MAIN)
14328 		i40e_devlink_destroy_port(pf);
14329 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14330 err_vsi:
14331 	i40e_vsi_clear(vsi);
14332 	return NULL;
14333 }
14334 
14335 /**
14336  * i40e_vsi_setup - Set up a VSI by a given type
14337  * @pf: board private structure
14338  * @type: VSI type
14339  * @uplink_seid: the switch element to link to
14340  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14341  *
14342  * This allocates the sw VSI structure and its queue resources, then add a VSI
14343  * to the identified VEB.
14344  *
14345  * Returns pointer to the successfully allocated and configure VSI sw struct on
14346  * success, otherwise returns NULL on failure.
14347  **/
14348 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14349 				u16 uplink_seid, u32 param1)
14350 {
14351 	struct i40e_vsi *vsi = NULL;
14352 	struct i40e_veb *veb = NULL;
14353 	u16 alloc_queue_pairs;
14354 	int v_idx;
14355 	int ret;
14356 
14357 	/* The requested uplink_seid must be either
14358 	 *     - the PF's port seid
14359 	 *              no VEB is needed because this is the PF
14360 	 *              or this is a Flow Director special case VSI
14361 	 *     - seid of an existing VEB
14362 	 *     - seid of a VSI that owns an existing VEB
14363 	 *     - seid of a VSI that doesn't own a VEB
14364 	 *              a new VEB is created and the VSI becomes the owner
14365 	 *     - seid of the PF VSI, which is what creates the first VEB
14366 	 *              this is a special case of the previous
14367 	 *
14368 	 * Find which uplink_seid we were given and create a new VEB if needed
14369 	 */
14370 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14371 	if (!veb && uplink_seid != pf->mac_seid) {
14372 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14373 		if (!vsi) {
14374 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14375 				 uplink_seid);
14376 			return NULL;
14377 		}
14378 
14379 		if (vsi->uplink_seid == pf->mac_seid)
14380 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14381 					     vsi->tc_config.enabled_tc);
14382 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14383 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14384 					     vsi->tc_config.enabled_tc);
14385 		if (veb) {
14386 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14387 				dev_info(&vsi->back->pdev->dev,
14388 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14389 				return NULL;
14390 			}
14391 			/* We come up by default in VEPA mode if SRIOV is not
14392 			 * already enabled, in which case we can't force VEPA
14393 			 * mode.
14394 			 */
14395 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14396 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14397 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14398 			}
14399 			i40e_config_bridge_mode(veb);
14400 		}
14401 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14402 		if (!veb) {
14403 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14404 			return NULL;
14405 		}
14406 
14407 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14408 		uplink_seid = veb->seid;
14409 	}
14410 
14411 	/* get vsi sw struct */
14412 	v_idx = i40e_vsi_mem_alloc(pf, type);
14413 	if (v_idx < 0)
14414 		goto err_alloc;
14415 	vsi = pf->vsi[v_idx];
14416 	if (!vsi)
14417 		goto err_alloc;
14418 	vsi->type = type;
14419 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14420 
14421 	if (type == I40E_VSI_MAIN)
14422 		pf->lan_vsi = v_idx;
14423 	else if (type == I40E_VSI_SRIOV)
14424 		vsi->vf_id = param1;
14425 	/* assign it some queues */
14426 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14427 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14428 
14429 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14430 	if (ret < 0) {
14431 		dev_info(&pf->pdev->dev,
14432 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14433 			 alloc_queue_pairs, vsi->seid, ret);
14434 		goto err_vsi;
14435 	}
14436 	vsi->base_queue = ret;
14437 
14438 	/* get a VSI from the hardware */
14439 	vsi->uplink_seid = uplink_seid;
14440 	ret = i40e_add_vsi(vsi);
14441 	if (ret)
14442 		goto err_vsi;
14443 
14444 	switch (vsi->type) {
14445 	/* setup the netdev if needed */
14446 	case I40E_VSI_MAIN:
14447 	case I40E_VSI_VMDQ2:
14448 		ret = i40e_config_netdev(vsi);
14449 		if (ret)
14450 			goto err_netdev;
14451 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14452 		if (ret)
14453 			goto err_netdev;
14454 		if (vsi->type == I40E_VSI_MAIN) {
14455 			ret = i40e_devlink_create_port(pf);
14456 			if (ret)
14457 				goto err_netdev;
14458 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14459 		}
14460 		ret = register_netdev(vsi->netdev);
14461 		if (ret)
14462 			goto err_dl_port;
14463 		vsi->netdev_registered = true;
14464 		netif_carrier_off(vsi->netdev);
14465 #ifdef CONFIG_I40E_DCB
14466 		/* Setup DCB netlink interface */
14467 		i40e_dcbnl_setup(vsi);
14468 #endif /* CONFIG_I40E_DCB */
14469 		fallthrough;
14470 	case I40E_VSI_FDIR:
14471 		/* set up vectors and rings if needed */
14472 		ret = i40e_vsi_setup_vectors(vsi);
14473 		if (ret)
14474 			goto err_msix;
14475 
14476 		ret = i40e_alloc_rings(vsi);
14477 		if (ret)
14478 			goto err_rings;
14479 
14480 		/* map all of the rings to the q_vectors */
14481 		i40e_vsi_map_rings_to_vectors(vsi);
14482 
14483 		i40e_vsi_reset_stats(vsi);
14484 		break;
14485 	default:
14486 		/* no netdev or rings for the other VSI types */
14487 		break;
14488 	}
14489 
14490 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14491 	    vsi->type == I40E_VSI_VMDQ2) {
14492 		ret = i40e_vsi_config_rss(vsi);
14493 		if (ret)
14494 			goto err_config;
14495 	}
14496 	return vsi;
14497 
14498 err_config:
14499 	i40e_vsi_clear_rings(vsi);
14500 err_rings:
14501 	i40e_vsi_free_q_vectors(vsi);
14502 err_msix:
14503 	if (vsi->netdev_registered) {
14504 		vsi->netdev_registered = false;
14505 		unregister_netdev(vsi->netdev);
14506 		free_netdev(vsi->netdev);
14507 		vsi->netdev = NULL;
14508 	}
14509 err_dl_port:
14510 	if (vsi->type == I40E_VSI_MAIN)
14511 		i40e_devlink_destroy_port(pf);
14512 err_netdev:
14513 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14514 err_vsi:
14515 	i40e_vsi_clear(vsi);
14516 err_alloc:
14517 	return NULL;
14518 }
14519 
14520 /**
14521  * i40e_veb_get_bw_info - Query VEB BW information
14522  * @veb: the veb to query
14523  *
14524  * Query the Tx scheduler BW configuration data for given VEB
14525  **/
14526 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14527 {
14528 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14529 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14530 	struct i40e_pf *pf = veb->pf;
14531 	struct i40e_hw *hw = &pf->hw;
14532 	u32 tc_bw_max;
14533 	int ret = 0;
14534 	int i;
14535 
14536 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14537 						  &bw_data, NULL);
14538 	if (ret) {
14539 		dev_info(&pf->pdev->dev,
14540 			 "query veb bw config failed, err %pe aq_err %s\n",
14541 			 ERR_PTR(ret),
14542 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14543 		goto out;
14544 	}
14545 
14546 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14547 						   &ets_data, NULL);
14548 	if (ret) {
14549 		dev_info(&pf->pdev->dev,
14550 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14551 			 ERR_PTR(ret),
14552 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14553 		goto out;
14554 	}
14555 
14556 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14557 	veb->bw_max_quanta = ets_data.tc_bw_max;
14558 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14559 	veb->enabled_tc = ets_data.tc_valid_bits;
14560 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14561 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14562 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14563 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14564 		veb->bw_tc_limit_credits[i] =
14565 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14566 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14567 	}
14568 
14569 out:
14570 	return ret;
14571 }
14572 
14573 /**
14574  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14575  * @pf: board private structure
14576  *
14577  * On error: returns error code (negative)
14578  * On success: returns vsi index in PF (positive)
14579  **/
14580 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14581 {
14582 	int ret = -ENOENT;
14583 	struct i40e_veb *veb;
14584 	int i;
14585 
14586 	/* Need to protect the allocation of switch elements at the PF level */
14587 	mutex_lock(&pf->switch_mutex);
14588 
14589 	/* VEB list may be fragmented if VEB creation/destruction has
14590 	 * been happening.  We can afford to do a quick scan to look
14591 	 * for any free slots in the list.
14592 	 *
14593 	 * find next empty veb slot, looping back around if necessary
14594 	 */
14595 	i = 0;
14596 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14597 		i++;
14598 	if (i >= I40E_MAX_VEB) {
14599 		ret = -ENOMEM;
14600 		goto err_alloc_veb;  /* out of VEB slots! */
14601 	}
14602 
14603 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14604 	if (!veb) {
14605 		ret = -ENOMEM;
14606 		goto err_alloc_veb;
14607 	}
14608 	veb->pf = pf;
14609 	veb->idx = i;
14610 	veb->enabled_tc = 1;
14611 
14612 	pf->veb[i] = veb;
14613 	ret = i;
14614 err_alloc_veb:
14615 	mutex_unlock(&pf->switch_mutex);
14616 	return ret;
14617 }
14618 
14619 /**
14620  * i40e_switch_branch_release - Delete a branch of the switch tree
14621  * @branch: where to start deleting
14622  *
14623  * This uses recursion to find the tips of the branch to be
14624  * removed, deleting until we get back to and can delete this VEB.
14625  **/
14626 static void i40e_switch_branch_release(struct i40e_veb *branch)
14627 {
14628 	struct i40e_pf *pf = branch->pf;
14629 	u16 branch_seid = branch->seid;
14630 	u16 veb_idx = branch->idx;
14631 	struct i40e_vsi *vsi;
14632 	struct i40e_veb *veb;
14633 	int i;
14634 
14635 	/* release any VEBs on this VEB - RECURSION */
14636 	i40e_pf_for_each_veb(pf, i, veb)
14637 		if (veb->uplink_seid == branch->seid)
14638 			i40e_switch_branch_release(veb);
14639 
14640 	/* Release the VSIs on this VEB, but not the owner VSI.
14641 	 *
14642 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14643 	 *       the VEB itself, so don't use (*branch) after this loop.
14644 	 */
14645 	i40e_pf_for_each_vsi(pf, i, vsi)
14646 		if (vsi->uplink_seid == branch_seid &&
14647 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14648 			i40e_vsi_release(vsi);
14649 
14650 	/* There's one corner case where the VEB might not have been
14651 	 * removed, so double check it here and remove it if needed.
14652 	 * This case happens if the veb was created from the debugfs
14653 	 * commands and no VSIs were added to it.
14654 	 */
14655 	if (pf->veb[veb_idx])
14656 		i40e_veb_release(pf->veb[veb_idx]);
14657 }
14658 
14659 /**
14660  * i40e_veb_clear - remove veb struct
14661  * @veb: the veb to remove
14662  **/
14663 static void i40e_veb_clear(struct i40e_veb *veb)
14664 {
14665 	if (!veb)
14666 		return;
14667 
14668 	if (veb->pf) {
14669 		struct i40e_pf *pf = veb->pf;
14670 
14671 		mutex_lock(&pf->switch_mutex);
14672 		if (pf->veb[veb->idx] == veb)
14673 			pf->veb[veb->idx] = NULL;
14674 		mutex_unlock(&pf->switch_mutex);
14675 	}
14676 
14677 	kfree(veb);
14678 }
14679 
14680 /**
14681  * i40e_veb_release - Delete a VEB and free its resources
14682  * @veb: the VEB being removed
14683  **/
14684 void i40e_veb_release(struct i40e_veb *veb)
14685 {
14686 	struct i40e_vsi *vsi, *vsi_it;
14687 	struct i40e_pf *pf;
14688 	int i, n = 0;
14689 
14690 	pf = veb->pf;
14691 
14692 	/* find the remaining VSI and check for extras */
14693 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14694 		if (vsi_it->uplink_seid == veb->seid) {
14695 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14696 				vsi = vsi_it;
14697 			n++;
14698 		}
14699 
14700 	/* Floating VEB has to be empty and regular one must have
14701 	 * single owner VSI.
14702 	 */
14703 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14704 		dev_info(&pf->pdev->dev,
14705 			 "can't remove VEB %d with %d VSIs left\n",
14706 			 veb->seid, n);
14707 		return;
14708 	}
14709 
14710 	/* For regular VEB move the owner VSI to uplink port */
14711 	if (veb->uplink_seid) {
14712 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14713 		vsi->uplink_seid = veb->uplink_seid;
14714 		vsi->veb_idx = I40E_NO_VEB;
14715 	}
14716 
14717 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14718 	i40e_veb_clear(veb);
14719 }
14720 
14721 /**
14722  * i40e_add_veb - create the VEB in the switch
14723  * @veb: the VEB to be instantiated
14724  * @vsi: the controlling VSI
14725  **/
14726 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14727 {
14728 	struct i40e_pf *pf = veb->pf;
14729 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14730 	int ret;
14731 
14732 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14733 			      veb->enabled_tc, vsi ? false : true,
14734 			      &veb->seid, enable_stats, NULL);
14735 
14736 	/* get a VEB from the hardware */
14737 	if (ret) {
14738 		dev_info(&pf->pdev->dev,
14739 			 "couldn't add VEB, err %pe aq_err %s\n",
14740 			 ERR_PTR(ret),
14741 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14742 		return -EPERM;
14743 	}
14744 
14745 	/* get statistics counter */
14746 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14747 					 &veb->stats_idx, NULL, NULL, NULL);
14748 	if (ret) {
14749 		dev_info(&pf->pdev->dev,
14750 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14751 			 ERR_PTR(ret),
14752 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14753 		return -EPERM;
14754 	}
14755 	ret = i40e_veb_get_bw_info(veb);
14756 	if (ret) {
14757 		dev_info(&pf->pdev->dev,
14758 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14759 			 ERR_PTR(ret),
14760 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14761 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14762 		return -ENOENT;
14763 	}
14764 
14765 	if (vsi) {
14766 		vsi->uplink_seid = veb->seid;
14767 		vsi->veb_idx = veb->idx;
14768 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14769 	}
14770 
14771 	return 0;
14772 }
14773 
14774 /**
14775  * i40e_veb_setup - Set up a VEB
14776  * @pf: board private structure
14777  * @flags: VEB setup flags
14778  * @uplink_seid: the switch element to link to
14779  * @vsi_seid: the initial VSI seid
14780  * @enabled_tc: Enabled TC bit-map
14781  *
14782  * This allocates the sw VEB structure and links it into the switch
14783  * It is possible and legal for this to be a duplicate of an already
14784  * existing VEB.  It is also possible for both uplink and vsi seids
14785  * to be zero, in order to create a floating VEB.
14786  *
14787  * Returns pointer to the successfully allocated VEB sw struct on
14788  * success, otherwise returns NULL on failure.
14789  **/
14790 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14791 				u16 uplink_seid, u16 vsi_seid,
14792 				u8 enabled_tc)
14793 {
14794 	struct i40e_vsi *vsi = NULL;
14795 	struct i40e_veb *veb;
14796 	int veb_idx;
14797 	int ret;
14798 
14799 	/* if one seid is 0, the other must be 0 to create a floating relay */
14800 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14801 	    (uplink_seid + vsi_seid != 0)) {
14802 		dev_info(&pf->pdev->dev,
14803 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14804 			 uplink_seid, vsi_seid);
14805 		return NULL;
14806 	}
14807 
14808 	/* make sure there is such a vsi and uplink */
14809 	if (vsi_seid) {
14810 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14811 		if (!vsi) {
14812 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14813 				vsi_seid);
14814 			return NULL;
14815 		}
14816 	}
14817 
14818 	/* get veb sw struct */
14819 	veb_idx = i40e_veb_mem_alloc(pf);
14820 	if (veb_idx < 0)
14821 		goto err_alloc;
14822 	veb = pf->veb[veb_idx];
14823 	veb->flags = flags;
14824 	veb->uplink_seid = uplink_seid;
14825 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14826 
14827 	/* create the VEB in the switch */
14828 	ret = i40e_add_veb(veb, vsi);
14829 	if (ret)
14830 		goto err_veb;
14831 
14832 	if (vsi && vsi->idx == pf->lan_vsi)
14833 		pf->lan_veb = veb->idx;
14834 
14835 	return veb;
14836 
14837 err_veb:
14838 	i40e_veb_clear(veb);
14839 err_alloc:
14840 	return NULL;
14841 }
14842 
14843 /**
14844  * i40e_setup_pf_switch_element - set PF vars based on switch type
14845  * @pf: board private structure
14846  * @ele: element we are building info from
14847  * @num_reported: total number of elements
14848  * @printconfig: should we print the contents
14849  *
14850  * helper function to assist in extracting a few useful SEID values.
14851  **/
14852 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14853 				struct i40e_aqc_switch_config_element_resp *ele,
14854 				u16 num_reported, bool printconfig)
14855 {
14856 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14857 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14858 	u8 element_type = ele->element_type;
14859 	u16 seid = le16_to_cpu(ele->seid);
14860 	struct i40e_veb *veb;
14861 
14862 	if (printconfig)
14863 		dev_info(&pf->pdev->dev,
14864 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14865 			 element_type, seid, uplink_seid, downlink_seid);
14866 
14867 	switch (element_type) {
14868 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14869 		pf->mac_seid = seid;
14870 		break;
14871 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14872 		/* Main VEB? */
14873 		if (uplink_seid != pf->mac_seid)
14874 			break;
14875 		if (pf->lan_veb >= I40E_MAX_VEB) {
14876 			int v;
14877 
14878 			/* find existing or else empty VEB */
14879 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14880 			if (veb) {
14881 				pf->lan_veb = veb->idx;
14882 			} else {
14883 				v = i40e_veb_mem_alloc(pf);
14884 				if (v < 0)
14885 					break;
14886 				pf->lan_veb = v;
14887 			}
14888 		}
14889 		if (pf->lan_veb >= I40E_MAX_VEB)
14890 			break;
14891 
14892 		pf->veb[pf->lan_veb]->seid = seid;
14893 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14894 		pf->veb[pf->lan_veb]->pf = pf;
14895 		break;
14896 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14897 		if (num_reported != 1)
14898 			break;
14899 		/* This is immediately after a reset so we can assume this is
14900 		 * the PF's VSI
14901 		 */
14902 		pf->mac_seid = uplink_seid;
14903 		pf->main_vsi_seid = seid;
14904 		if (printconfig)
14905 			dev_info(&pf->pdev->dev,
14906 				 "pf_seid=%d main_vsi_seid=%d\n",
14907 				 downlink_seid, pf->main_vsi_seid);
14908 		break;
14909 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14910 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14911 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14912 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14913 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14914 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14915 		/* ignore these for now */
14916 		break;
14917 	default:
14918 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14919 			 element_type, seid);
14920 		break;
14921 	}
14922 }
14923 
14924 /**
14925  * i40e_fetch_switch_configuration - Get switch config from firmware
14926  * @pf: board private structure
14927  * @printconfig: should we print the contents
14928  *
14929  * Get the current switch configuration from the device and
14930  * extract a few useful SEID values.
14931  **/
14932 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14933 {
14934 	struct i40e_aqc_get_switch_config_resp *sw_config;
14935 	u16 next_seid = 0;
14936 	int ret = 0;
14937 	u8 *aq_buf;
14938 	int i;
14939 
14940 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14941 	if (!aq_buf)
14942 		return -ENOMEM;
14943 
14944 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14945 	do {
14946 		u16 num_reported, num_total;
14947 
14948 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14949 						I40E_AQ_LARGE_BUF,
14950 						&next_seid, NULL);
14951 		if (ret) {
14952 			dev_info(&pf->pdev->dev,
14953 				 "get switch config failed err %d aq_err %s\n",
14954 				 ret,
14955 				 i40e_aq_str(&pf->hw,
14956 					     pf->hw.aq.asq_last_status));
14957 			kfree(aq_buf);
14958 			return -ENOENT;
14959 		}
14960 
14961 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14962 		num_total = le16_to_cpu(sw_config->header.num_total);
14963 
14964 		if (printconfig)
14965 			dev_info(&pf->pdev->dev,
14966 				 "header: %d reported %d total\n",
14967 				 num_reported, num_total);
14968 
14969 		for (i = 0; i < num_reported; i++) {
14970 			struct i40e_aqc_switch_config_element_resp *ele =
14971 				&sw_config->element[i];
14972 
14973 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14974 						     printconfig);
14975 		}
14976 	} while (next_seid != 0);
14977 
14978 	kfree(aq_buf);
14979 	return ret;
14980 }
14981 
14982 /**
14983  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
14984  * @pf: board private structure
14985  * @reinit: if the Main VSI needs to re-initialized.
14986  * @lock_acquired: indicates whether or not the lock has been acquired
14987  *
14988  * Returns 0 on success, negative value on failure
14989  **/
14990 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
14991 {
14992 	u16 flags = 0;
14993 	int ret;
14994 
14995 	/* find out what's out there already */
14996 	ret = i40e_fetch_switch_configuration(pf, false);
14997 	if (ret) {
14998 		dev_info(&pf->pdev->dev,
14999 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15000 			 ERR_PTR(ret),
15001 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15002 		return ret;
15003 	}
15004 	i40e_pf_reset_stats(pf);
15005 
15006 	/* set the switch config bit for the whole device to
15007 	 * support limited promisc or true promisc
15008 	 * when user requests promisc. The default is limited
15009 	 * promisc.
15010 	*/
15011 
15012 	if ((pf->hw.pf_id == 0) &&
15013 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
15014 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15015 		pf->last_sw_conf_flags = flags;
15016 	}
15017 
15018 	if (pf->hw.pf_id == 0) {
15019 		u16 valid_flags;
15020 
15021 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15022 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15023 						NULL);
15024 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15025 			dev_info(&pf->pdev->dev,
15026 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15027 				 ERR_PTR(ret),
15028 				 i40e_aq_str(&pf->hw,
15029 					     pf->hw.aq.asq_last_status));
15030 			/* not a fatal problem, just keep going */
15031 		}
15032 		pf->last_sw_conf_valid_flags = valid_flags;
15033 	}
15034 
15035 	/* first time setup */
15036 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15037 		struct i40e_vsi *vsi = NULL;
15038 		u16 uplink_seid;
15039 
15040 		/* Set up the PF VSI associated with the PF's main VSI
15041 		 * that is already in the HW switch
15042 		 */
15043 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15044 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15045 		else
15046 			uplink_seid = pf->mac_seid;
15047 		if (pf->lan_vsi == I40E_NO_VSI)
15048 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15049 		else if (reinit)
15050 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15051 		if (!vsi) {
15052 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15053 			i40e_cloud_filter_exit(pf);
15054 			i40e_fdir_teardown(pf);
15055 			return -EAGAIN;
15056 		}
15057 	} else {
15058 		/* force a reset of TC and queue layout configurations */
15059 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15060 
15061 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15062 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15063 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15064 	}
15065 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15066 
15067 	i40e_fdir_sb_setup(pf);
15068 
15069 	/* Setup static PF queue filter control settings */
15070 	ret = i40e_setup_pf_filter_control(pf);
15071 	if (ret) {
15072 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15073 			 ret);
15074 		/* Failure here should not stop continuing other steps */
15075 	}
15076 
15077 	/* enable RSS in the HW, even for only one queue, as the stack can use
15078 	 * the hash
15079 	 */
15080 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15081 		i40e_pf_config_rss(pf);
15082 
15083 	/* fill in link information and enable LSE reporting */
15084 	i40e_link_event(pf);
15085 
15086 	i40e_ptp_init(pf);
15087 
15088 	if (!lock_acquired)
15089 		rtnl_lock();
15090 
15091 	/* repopulate tunnel port filters */
15092 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15093 
15094 	if (!lock_acquired)
15095 		rtnl_unlock();
15096 
15097 	return ret;
15098 }
15099 
15100 /**
15101  * i40e_determine_queue_usage - Work out queue distribution
15102  * @pf: board private structure
15103  **/
15104 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15105 {
15106 	int queues_left;
15107 	int q_max;
15108 
15109 	pf->num_lan_qps = 0;
15110 
15111 	/* Find the max queues to be put into basic use.  We'll always be
15112 	 * using TC0, whether or not DCB is running, and TC0 will get the
15113 	 * big RSS set.
15114 	 */
15115 	queues_left = pf->hw.func_caps.num_tx_qp;
15116 
15117 	if ((queues_left == 1) ||
15118 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15119 		/* one qp for PF, no queues for anything else */
15120 		queues_left = 0;
15121 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15122 
15123 		/* make sure all the fancies are disabled */
15124 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15125 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15126 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15127 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15128 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15129 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15130 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15131 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15132 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15133 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15134 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15135 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15136 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15137 		/* one qp for PF */
15138 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15139 		queues_left -= pf->num_lan_qps;
15140 
15141 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15142 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15143 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15144 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15145 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15146 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15147 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15148 	} else {
15149 		/* Not enough queues for all TCs */
15150 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15151 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15152 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15153 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15154 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15155 		}
15156 
15157 		/* limit lan qps to the smaller of qps, cpus or msix */
15158 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15159 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15160 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15161 		pf->num_lan_qps = q_max;
15162 
15163 		queues_left -= pf->num_lan_qps;
15164 	}
15165 
15166 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15167 		if (queues_left > 1) {
15168 			queues_left -= 1; /* save 1 queue for FD */
15169 		} else {
15170 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15171 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15172 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15173 		}
15174 	}
15175 
15176 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15177 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15178 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15179 					(queues_left / pf->num_vf_qps));
15180 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15181 	}
15182 
15183 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15184 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15185 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15186 					  (queues_left / pf->num_vmdq_qps));
15187 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15188 	}
15189 
15190 	pf->queues_left = queues_left;
15191 	dev_dbg(&pf->pdev->dev,
15192 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15193 		pf->hw.func_caps.num_tx_qp,
15194 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15195 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15196 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15197 		queues_left);
15198 }
15199 
15200 /**
15201  * i40e_setup_pf_filter_control - Setup PF static filter control
15202  * @pf: PF to be setup
15203  *
15204  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15205  * settings. If PE/FCoE are enabled then it will also set the per PF
15206  * based filter sizes required for them. It also enables Flow director,
15207  * ethertype and macvlan type filter settings for the pf.
15208  *
15209  * Returns 0 on success, negative on failure
15210  **/
15211 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15212 {
15213 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15214 
15215 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15216 
15217 	/* Flow Director is enabled */
15218 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15219 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15220 		settings->enable_fdir = true;
15221 
15222 	/* Ethtype and MACVLAN filters enabled for PF */
15223 	settings->enable_ethtype = true;
15224 	settings->enable_macvlan = true;
15225 
15226 	if (i40e_set_filter_control(&pf->hw, settings))
15227 		return -ENOENT;
15228 
15229 	return 0;
15230 }
15231 
15232 #define INFO_STRING_LEN 255
15233 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15234 static void i40e_print_features(struct i40e_pf *pf)
15235 {
15236 	struct i40e_hw *hw = &pf->hw;
15237 	char *buf;
15238 	int i;
15239 
15240 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15241 	if (!buf)
15242 		return;
15243 
15244 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15245 #ifdef CONFIG_PCI_IOV
15246 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15247 #endif
15248 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15249 		      pf->hw.func_caps.num_vsis,
15250 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15251 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15252 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15253 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15254 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15255 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15256 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15257 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15258 	}
15259 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15260 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15261 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15262 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15263 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15264 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15265 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15266 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15267 	else
15268 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15269 
15270 	dev_info(&pf->pdev->dev, "%s\n", buf);
15271 	kfree(buf);
15272 	WARN_ON(i > INFO_STRING_LEN);
15273 }
15274 
15275 /**
15276  * i40e_get_platform_mac_addr - get platform-specific MAC address
15277  * @pdev: PCI device information struct
15278  * @pf: board private structure
15279  *
15280  * Look up the MAC address for the device. First we'll try
15281  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15282  * specific fallback. Otherwise, we'll default to the stored value in
15283  * firmware.
15284  **/
15285 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15286 {
15287 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15288 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15289 }
15290 
15291 /**
15292  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15293  * @fec_cfg: FEC option to set in flags
15294  * @flags: ptr to flags in which we set FEC option
15295  **/
15296 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15297 {
15298 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15299 		set_bit(I40E_FLAG_RS_FEC, flags);
15300 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15301 	}
15302 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15303 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15304 		set_bit(I40E_FLAG_RS_FEC, flags);
15305 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15306 	}
15307 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15308 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15309 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15310 		clear_bit(I40E_FLAG_RS_FEC, flags);
15311 	}
15312 	if (fec_cfg == 0) {
15313 		clear_bit(I40E_FLAG_RS_FEC, flags);
15314 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15315 	}
15316 }
15317 
15318 /**
15319  * i40e_check_recovery_mode - check if we are running transition firmware
15320  * @pf: board private structure
15321  *
15322  * Check registers indicating the firmware runs in recovery mode. Sets the
15323  * appropriate driver state.
15324  *
15325  * Returns true if the recovery mode was detected, false otherwise
15326  **/
15327 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15328 {
15329 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15330 
15331 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15332 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15333 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15334 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15335 
15336 		return true;
15337 	}
15338 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15339 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15340 
15341 	return false;
15342 }
15343 
15344 /**
15345  * i40e_pf_loop_reset - perform reset in a loop.
15346  * @pf: board private structure
15347  *
15348  * This function is useful when a NIC is about to enter recovery mode.
15349  * When a NIC's internal data structures are corrupted the NIC's
15350  * firmware is going to enter recovery mode.
15351  * Right after a POR it takes about 7 minutes for firmware to enter
15352  * recovery mode. Until that time a NIC is in some kind of intermediate
15353  * state. After that time period the NIC almost surely enters
15354  * recovery mode. The only way for a driver to detect intermediate
15355  * state is to issue a series of pf-resets and check a return value.
15356  * If a PF reset returns success then the firmware could be in recovery
15357  * mode so the caller of this code needs to check for recovery mode
15358  * if this function returns success. There is a little chance that
15359  * firmware will hang in intermediate state forever.
15360  * Since waiting 7 minutes is quite a lot of time this function waits
15361  * 10 seconds and then gives up by returning an error.
15362  *
15363  * Return 0 on success, negative on failure.
15364  **/
15365 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15366 {
15367 	/* wait max 10 seconds for PF reset to succeed */
15368 	const unsigned long time_end = jiffies + 10 * HZ;
15369 	struct i40e_hw *hw = &pf->hw;
15370 	int ret;
15371 
15372 	ret = i40e_pf_reset(hw);
15373 	while (ret != 0 && time_before(jiffies, time_end)) {
15374 		usleep_range(10000, 20000);
15375 		ret = i40e_pf_reset(hw);
15376 	}
15377 
15378 	if (ret == 0)
15379 		pf->pfr_count++;
15380 	else
15381 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15382 
15383 	return ret;
15384 }
15385 
15386 /**
15387  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15388  * @pf: board private structure
15389  *
15390  * Check FW registers to determine if FW issued unexpected EMP Reset.
15391  * Every time when unexpected EMP Reset occurs the FW increments
15392  * a counter of unexpected EMP Resets. When the counter reaches 10
15393  * the FW should enter the Recovery mode
15394  *
15395  * Returns true if FW issued unexpected EMP Reset
15396  **/
15397 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15398 {
15399 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15400 			   I40E_GL_FWSTS_FWS1B_MASK;
15401 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15402 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15403 }
15404 
15405 /**
15406  * i40e_handle_resets - handle EMP resets and PF resets
15407  * @pf: board private structure
15408  *
15409  * Handle both EMP resets and PF resets and conclude whether there are
15410  * any issues regarding these resets. If there are any issues then
15411  * generate log entry.
15412  *
15413  * Return 0 if NIC is healthy or negative value when there are issues
15414  * with resets
15415  **/
15416 static int i40e_handle_resets(struct i40e_pf *pf)
15417 {
15418 	const int pfr = i40e_pf_loop_reset(pf);
15419 	const bool is_empr = i40e_check_fw_empr(pf);
15420 
15421 	if (is_empr || pfr != 0)
15422 		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");
15423 
15424 	return is_empr ? -EIO : pfr;
15425 }
15426 
15427 /**
15428  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15429  * @pf: board private structure
15430  * @hw: ptr to the hardware info
15431  *
15432  * This function does a minimal setup of all subsystems needed for running
15433  * recovery mode.
15434  *
15435  * Returns 0 on success, negative on failure
15436  **/
15437 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15438 {
15439 	struct i40e_vsi *vsi;
15440 	int err;
15441 	int v_idx;
15442 
15443 	pci_set_drvdata(pf->pdev, pf);
15444 	pci_save_state(pf->pdev);
15445 
15446 	/* set up periodic task facility */
15447 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15448 	pf->service_timer_period = HZ;
15449 
15450 	INIT_WORK(&pf->service_task, i40e_service_task);
15451 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15452 
15453 	err = i40e_init_interrupt_scheme(pf);
15454 	if (err)
15455 		goto err_switch_setup;
15456 
15457 	/* The number of VSIs reported by the FW is the minimum guaranteed
15458 	 * to us; HW supports far more and we share the remaining pool with
15459 	 * the other PFs. We allocate space for more than the guarantee with
15460 	 * the understanding that we might not get them all later.
15461 	 */
15462 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15463 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15464 	else
15465 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15466 
15467 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15468 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15469 			  GFP_KERNEL);
15470 	if (!pf->vsi) {
15471 		err = -ENOMEM;
15472 		goto err_switch_setup;
15473 	}
15474 
15475 	/* We allocate one VSI which is needed as absolute minimum
15476 	 * in order to register the netdev
15477 	 */
15478 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15479 	if (v_idx < 0) {
15480 		err = v_idx;
15481 		goto err_switch_setup;
15482 	}
15483 	pf->lan_vsi = v_idx;
15484 	vsi = pf->vsi[v_idx];
15485 	if (!vsi) {
15486 		err = -EFAULT;
15487 		goto err_switch_setup;
15488 	}
15489 	vsi->alloc_queue_pairs = 1;
15490 	err = i40e_config_netdev(vsi);
15491 	if (err)
15492 		goto err_switch_setup;
15493 	err = register_netdev(vsi->netdev);
15494 	if (err)
15495 		goto err_switch_setup;
15496 	vsi->netdev_registered = true;
15497 	i40e_dbg_pf_init(pf);
15498 
15499 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15500 	if (err)
15501 		goto err_switch_setup;
15502 
15503 	/* tell the firmware that we're starting */
15504 	i40e_send_version(pf);
15505 
15506 	/* since everything's happy, start the service_task timer */
15507 	mod_timer(&pf->service_timer,
15508 		  round_jiffies(jiffies + pf->service_timer_period));
15509 
15510 	return 0;
15511 
15512 err_switch_setup:
15513 	i40e_reset_interrupt_capability(pf);
15514 	timer_shutdown_sync(&pf->service_timer);
15515 	i40e_shutdown_adminq(hw);
15516 	iounmap(hw->hw_addr);
15517 	pci_release_mem_regions(pf->pdev);
15518 	pci_disable_device(pf->pdev);
15519 	i40e_free_pf(pf);
15520 
15521 	return err;
15522 }
15523 
15524 /**
15525  * i40e_set_subsystem_device_id - set subsystem device id
15526  * @hw: pointer to the hardware info
15527  *
15528  * Set PCI subsystem device id either from a pci_dev structure or
15529  * a specific FW register.
15530  **/
15531 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15532 {
15533 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15534 
15535 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15536 		pf->pdev->subsystem_device :
15537 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15538 }
15539 
15540 /**
15541  * i40e_probe - Device initialization routine
15542  * @pdev: PCI device information struct
15543  * @ent: entry in i40e_pci_tbl
15544  *
15545  * i40e_probe initializes a PF identified by a pci_dev structure.
15546  * The OS initialization, configuring of the PF private structure,
15547  * and a hardware reset occur.
15548  *
15549  * Returns 0 on success, negative on failure
15550  **/
15551 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15552 {
15553 	struct i40e_aq_get_phy_abilities_resp abilities;
15554 #ifdef CONFIG_I40E_DCB
15555 	enum i40e_get_fw_lldp_status_resp lldp_status;
15556 #endif /* CONFIG_I40E_DCB */
15557 	struct i40e_vsi *vsi;
15558 	struct i40e_pf *pf;
15559 	struct i40e_hw *hw;
15560 	u16 wol_nvm_bits;
15561 	char nvm_ver[32];
15562 	u16 link_status;
15563 #ifdef CONFIG_I40E_DCB
15564 	int status;
15565 #endif /* CONFIG_I40E_DCB */
15566 	int err;
15567 	u32 val;
15568 
15569 	err = pci_enable_device_mem(pdev);
15570 	if (err)
15571 		return err;
15572 
15573 	/* set up for high or low dma */
15574 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15575 	if (err) {
15576 		dev_err(&pdev->dev,
15577 			"DMA configuration failed: 0x%x\n", err);
15578 		goto err_dma;
15579 	}
15580 
15581 	/* set up pci connections */
15582 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15583 	if (err) {
15584 		dev_info(&pdev->dev,
15585 			 "pci_request_selected_regions failed %d\n", err);
15586 		goto err_pci_reg;
15587 	}
15588 
15589 	pci_set_master(pdev);
15590 
15591 	/* Now that we have a PCI connection, we need to do the
15592 	 * low level device setup.  This is primarily setting up
15593 	 * the Admin Queue structures and then querying for the
15594 	 * device's current profile information.
15595 	 */
15596 	pf = i40e_alloc_pf(&pdev->dev);
15597 	if (!pf) {
15598 		err = -ENOMEM;
15599 		goto err_pf_alloc;
15600 	}
15601 	pf->next_vsi = 0;
15602 	pf->pdev = pdev;
15603 	set_bit(__I40E_DOWN, pf->state);
15604 
15605 	hw = &pf->hw;
15606 
15607 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15608 				I40E_MAX_CSR_SPACE);
15609 	/* We believe that the highest register to read is
15610 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15611 	 * is not less than that before mapping to prevent a
15612 	 * kernel panic.
15613 	 */
15614 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15615 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15616 			pf->ioremap_len);
15617 		err = -ENOMEM;
15618 		goto err_ioremap;
15619 	}
15620 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15621 	if (!hw->hw_addr) {
15622 		err = -EIO;
15623 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15624 			 (unsigned int)pci_resource_start(pdev, 0),
15625 			 pf->ioremap_len, err);
15626 		goto err_ioremap;
15627 	}
15628 	hw->vendor_id = pdev->vendor;
15629 	hw->device_id = pdev->device;
15630 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15631 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15632 	i40e_set_subsystem_device_id(hw);
15633 	hw->bus.device = PCI_SLOT(pdev->devfn);
15634 	hw->bus.func = PCI_FUNC(pdev->devfn);
15635 	hw->bus.bus_id = pdev->bus->number;
15636 
15637 	/* Select something other than the 802.1ad ethertype for the
15638 	 * switch to use internally and drop on ingress.
15639 	 */
15640 	hw->switch_tag = 0xffff;
15641 	hw->first_tag = ETH_P_8021AD;
15642 	hw->second_tag = ETH_P_8021Q;
15643 
15644 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15645 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15646 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15647 
15648 	/* set up the locks for the AQ, do this only once in probe
15649 	 * and destroy them only once in remove
15650 	 */
15651 	mutex_init(&hw->aq.asq_mutex);
15652 	mutex_init(&hw->aq.arq_mutex);
15653 
15654 	pf->msg_enable = netif_msg_init(debug,
15655 					NETIF_MSG_DRV |
15656 					NETIF_MSG_PROBE |
15657 					NETIF_MSG_LINK);
15658 	if (debug < -1)
15659 		pf->hw.debug_mask = debug;
15660 
15661 	/* do a special CORER for clearing PXE mode once at init */
15662 	if (hw->revision_id == 0 &&
15663 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15664 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15665 		i40e_flush(hw);
15666 		msleep(200);
15667 		pf->corer_count++;
15668 
15669 		i40e_clear_pxe_mode(hw);
15670 	}
15671 
15672 	/* Reset here to make sure all is clean and to define PF 'n' */
15673 	i40e_clear_hw(hw);
15674 
15675 	err = i40e_set_mac_type(hw);
15676 	if (err) {
15677 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15678 			 err);
15679 		goto err_pf_reset;
15680 	}
15681 
15682 	err = i40e_handle_resets(pf);
15683 	if (err)
15684 		goto err_pf_reset;
15685 
15686 	i40e_check_recovery_mode(pf);
15687 
15688 	if (is_kdump_kernel()) {
15689 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15690 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15691 	} else {
15692 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15693 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15694 	}
15695 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15696 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15697 
15698 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15699 		 "%s-%s:misc",
15700 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15701 
15702 	err = i40e_init_shared_code(hw);
15703 	if (err) {
15704 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15705 			 err);
15706 		goto err_pf_reset;
15707 	}
15708 
15709 	/* set up a default setting for link flow control */
15710 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15711 
15712 	err = i40e_init_adminq(hw);
15713 	if (err) {
15714 		if (err == -EIO)
15715 			dev_info(&pdev->dev,
15716 				 "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",
15717 				 hw->aq.api_maj_ver,
15718 				 hw->aq.api_min_ver,
15719 				 I40E_FW_API_VERSION_MAJOR,
15720 				 I40E_FW_MINOR_VERSION(hw));
15721 		else
15722 			dev_info(&pdev->dev,
15723 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15724 
15725 		goto err_pf_reset;
15726 	}
15727 	i40e_get_oem_version(hw);
15728 	i40e_get_pba_string(hw);
15729 
15730 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15731 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15732 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15733 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15734 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15735 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15736 		 hw->subsystem_device_id);
15737 
15738 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15739 				  I40E_FW_MINOR_VERSION(hw) + 1))
15740 		dev_dbg(&pdev->dev,
15741 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15742 			 hw->aq.api_maj_ver,
15743 			 hw->aq.api_min_ver,
15744 			 I40E_FW_API_VERSION_MAJOR,
15745 			 I40E_FW_MINOR_VERSION(hw));
15746 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15747 		dev_info(&pdev->dev,
15748 			 "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",
15749 			 hw->aq.api_maj_ver,
15750 			 hw->aq.api_min_ver,
15751 			 I40E_FW_API_VERSION_MAJOR,
15752 			 I40E_FW_MINOR_VERSION(hw));
15753 
15754 	i40e_verify_eeprom(pf);
15755 
15756 	/* Rev 0 hardware was never productized */
15757 	if (hw->revision_id < 1)
15758 		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");
15759 
15760 	i40e_clear_pxe_mode(hw);
15761 
15762 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15763 	if (err)
15764 		goto err_adminq_setup;
15765 
15766 	err = i40e_sw_init(pf);
15767 	if (err) {
15768 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15769 		goto err_sw_init;
15770 	}
15771 
15772 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15773 		return i40e_init_recovery_mode(pf, hw);
15774 
15775 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15776 				hw->func_caps.num_rx_qp, 0, 0);
15777 	if (err) {
15778 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15779 		goto err_init_lan_hmc;
15780 	}
15781 
15782 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15783 	if (err) {
15784 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15785 		err = -ENOENT;
15786 		goto err_configure_lan_hmc;
15787 	}
15788 
15789 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15790 	 * Ignore error return codes because if it was already disabled via
15791 	 * hardware settings this will fail
15792 	 */
15793 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15794 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15795 		i40e_aq_stop_lldp(hw, true, false, NULL);
15796 	}
15797 
15798 	/* allow a platform config to override the HW addr */
15799 	i40e_get_platform_mac_addr(pdev, pf);
15800 
15801 	if (!is_valid_ether_addr(hw->mac.addr)) {
15802 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15803 		err = -EIO;
15804 		goto err_mac_addr;
15805 	}
15806 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15807 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15808 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15809 	if (is_valid_ether_addr(hw->mac.port_addr))
15810 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15811 
15812 	i40e_ptp_alloc_pins(pf);
15813 	pci_set_drvdata(pdev, pf);
15814 	pci_save_state(pdev);
15815 
15816 #ifdef CONFIG_I40E_DCB
15817 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15818 	(!status &&
15819 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15820 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15821 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15822 	dev_info(&pdev->dev,
15823 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15824 			"FW LLDP is disabled\n" :
15825 			"FW LLDP is enabled\n");
15826 
15827 	/* Enable FW to write default DCB config on link-up */
15828 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15829 
15830 	err = i40e_init_pf_dcb(pf);
15831 	if (err) {
15832 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15833 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15834 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15835 		/* Continue without DCB enabled */
15836 	}
15837 #endif /* CONFIG_I40E_DCB */
15838 
15839 	/* set up periodic task facility */
15840 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15841 	pf->service_timer_period = HZ;
15842 
15843 	INIT_WORK(&pf->service_task, i40e_service_task);
15844 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15845 
15846 	/* NVM bit on means WoL disabled for the port */
15847 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15848 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15849 		pf->wol_en = false;
15850 	else
15851 		pf->wol_en = true;
15852 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15853 
15854 	/* set up the main switch operations */
15855 	i40e_determine_queue_usage(pf);
15856 	err = i40e_init_interrupt_scheme(pf);
15857 	if (err)
15858 		goto err_switch_setup;
15859 
15860 	/* Reduce Tx and Rx pairs for kdump
15861 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15862 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15863 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15864 	 */
15865 	if (is_kdump_kernel())
15866 		pf->num_lan_msix = 1;
15867 
15868 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15869 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15870 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15871 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15872 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15873 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15874 						    UDP_TUNNEL_TYPE_GENEVE;
15875 
15876 	/* The number of VSIs reported by the FW is the minimum guaranteed
15877 	 * to us; HW supports far more and we share the remaining pool with
15878 	 * the other PFs. We allocate space for more than the guarantee with
15879 	 * the understanding that we might not get them all later.
15880 	 */
15881 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15882 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15883 	else
15884 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15885 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15886 		dev_warn(&pf->pdev->dev,
15887 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15888 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15889 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15890 	}
15891 
15892 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15893 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15894 			  GFP_KERNEL);
15895 	if (!pf->vsi) {
15896 		err = -ENOMEM;
15897 		goto err_switch_setup;
15898 	}
15899 
15900 #ifdef CONFIG_PCI_IOV
15901 	/* prep for VF support */
15902 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15903 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15904 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15905 		if (pci_num_vf(pdev))
15906 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15907 	}
15908 #endif
15909 	err = i40e_setup_pf_switch(pf, false, false);
15910 	if (err) {
15911 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15912 		goto err_vsis;
15913 	}
15914 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15915 
15916 	/* if FDIR VSI was set up, start it now */
15917 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
15918 	if (vsi)
15919 		i40e_vsi_open(vsi);
15920 
15921 	/* The driver only wants link up/down and module qualification
15922 	 * reports from firmware.  Note the negative logic.
15923 	 */
15924 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15925 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15926 					 I40E_AQ_EVENT_MEDIA_NA |
15927 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15928 	if (err)
15929 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15930 			 ERR_PTR(err),
15931 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15932 
15933 	/* Reconfigure hardware for allowing smaller MSS in the case
15934 	 * of TSO, so that we avoid the MDD being fired and causing
15935 	 * a reset in the case of small MSS+TSO.
15936 	 */
15937 	val = rd32(hw, I40E_REG_MSS);
15938 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15939 		val &= ~I40E_REG_MSS_MIN_MASK;
15940 		val |= I40E_64BYTE_MSS;
15941 		wr32(hw, I40E_REG_MSS, val);
15942 	}
15943 
15944 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
15945 		msleep(75);
15946 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15947 		if (err)
15948 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
15949 				 ERR_PTR(err),
15950 				 i40e_aq_str(&pf->hw,
15951 					     pf->hw.aq.asq_last_status));
15952 	}
15953 	/* The main driver is (mostly) up and happy. We need to set this state
15954 	 * before setting up the misc vector or we get a race and the vector
15955 	 * ends up disabled forever.
15956 	 */
15957 	clear_bit(__I40E_DOWN, pf->state);
15958 
15959 	/* In case of MSIX we are going to setup the misc vector right here
15960 	 * to handle admin queue events etc. In case of legacy and MSI
15961 	 * the misc functionality and queue processing is combined in
15962 	 * the same vector and that gets setup at open.
15963 	 */
15964 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15965 		err = i40e_setup_misc_vector(pf);
15966 		if (err) {
15967 			dev_info(&pdev->dev,
15968 				 "setup of misc vector failed: %d\n", err);
15969 			i40e_cloud_filter_exit(pf);
15970 			i40e_fdir_teardown(pf);
15971 			goto err_vsis;
15972 		}
15973 	}
15974 
15975 #ifdef CONFIG_PCI_IOV
15976 	/* prep for VF support */
15977 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15978 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15979 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15980 		/* disable link interrupts for VFs */
15981 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
15982 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
15983 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
15984 		i40e_flush(hw);
15985 
15986 		if (pci_num_vf(pdev)) {
15987 			dev_info(&pdev->dev,
15988 				 "Active VFs found, allocating resources.\n");
15989 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
15990 			if (err)
15991 				dev_info(&pdev->dev,
15992 					 "Error %d allocating resources for existing VFs\n",
15993 					 err);
15994 		}
15995 	}
15996 #endif /* CONFIG_PCI_IOV */
15997 
15998 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
15999 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16000 						      pf->num_iwarp_msix,
16001 						      I40E_IWARP_IRQ_PILE_ID);
16002 		if (pf->iwarp_base_vector < 0) {
16003 			dev_info(&pdev->dev,
16004 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16005 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16006 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
16007 		}
16008 	}
16009 
16010 	i40e_dbg_pf_init(pf);
16011 
16012 	/* tell the firmware that we're starting */
16013 	i40e_send_version(pf);
16014 
16015 	/* since everything's happy, start the service_task timer */
16016 	mod_timer(&pf->service_timer,
16017 		  round_jiffies(jiffies + pf->service_timer_period));
16018 
16019 	/* add this PF to client device list and launch a client service task */
16020 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16021 		err = i40e_lan_add_device(pf);
16022 		if (err)
16023 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16024 				 err);
16025 	}
16026 
16027 #define PCI_SPEED_SIZE 8
16028 #define PCI_WIDTH_SIZE 8
16029 	/* Devices on the IOSF bus do not have this information
16030 	 * and will report PCI Gen 1 x 1 by default so don't bother
16031 	 * checking them.
16032 	 */
16033 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16034 		char speed[PCI_SPEED_SIZE] = "Unknown";
16035 		char width[PCI_WIDTH_SIZE] = "Unknown";
16036 
16037 		/* Get the negotiated link width and speed from PCI config
16038 		 * space
16039 		 */
16040 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16041 					  &link_status);
16042 
16043 		i40e_set_pci_config_data(hw, link_status);
16044 
16045 		switch (hw->bus.speed) {
16046 		case i40e_bus_speed_8000:
16047 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16048 		case i40e_bus_speed_5000:
16049 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16050 		case i40e_bus_speed_2500:
16051 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16052 		default:
16053 			break;
16054 		}
16055 		switch (hw->bus.width) {
16056 		case i40e_bus_width_pcie_x8:
16057 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16058 		case i40e_bus_width_pcie_x4:
16059 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16060 		case i40e_bus_width_pcie_x2:
16061 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16062 		case i40e_bus_width_pcie_x1:
16063 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16064 		default:
16065 			break;
16066 		}
16067 
16068 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16069 			 speed, width);
16070 
16071 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16072 		    hw->bus.speed < i40e_bus_speed_8000) {
16073 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16074 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16075 		}
16076 	}
16077 
16078 	/* get the requested speeds from the fw */
16079 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16080 	if (err)
16081 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16082 			ERR_PTR(err),
16083 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16084 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16085 
16086 	/* set the FEC config due to the board capabilities */
16087 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16088 
16089 	/* get the supported phy types from the fw */
16090 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16091 	if (err)
16092 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16093 			ERR_PTR(err),
16094 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16095 
16096 	/* make sure the MFS hasn't been set lower than the default */
16097 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16098 	val = FIELD_GET(I40E_PRTGL_SAH_MFS_MASK,
16099 			rd32(&pf->hw, I40E_PRTGL_SAH));
16100 	if (val < MAX_FRAME_SIZE_DEFAULT)
16101 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16102 			 pf->hw.port, val);
16103 
16104 	/* Add a filter to drop all Flow control frames from any VSI from being
16105 	 * transmitted. By doing so we stop a malicious VF from sending out
16106 	 * PAUSE or PFC frames and potentially controlling traffic for other
16107 	 * PF/VF VSIs.
16108 	 * The FW can still send Flow control frames if enabled.
16109 	 */
16110 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16111 						       pf->main_vsi_seid);
16112 
16113 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16114 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16115 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16116 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16117 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16118 	/* print a string summarizing features */
16119 	i40e_print_features(pf);
16120 
16121 	i40e_devlink_register(pf);
16122 
16123 	return 0;
16124 
16125 	/* Unwind what we've done if something failed in the setup */
16126 err_vsis:
16127 	set_bit(__I40E_DOWN, pf->state);
16128 	i40e_clear_interrupt_scheme(pf);
16129 	kfree(pf->vsi);
16130 err_switch_setup:
16131 	i40e_reset_interrupt_capability(pf);
16132 	timer_shutdown_sync(&pf->service_timer);
16133 err_mac_addr:
16134 err_configure_lan_hmc:
16135 	(void)i40e_shutdown_lan_hmc(hw);
16136 err_init_lan_hmc:
16137 	kfree(pf->qp_pile);
16138 err_sw_init:
16139 err_adminq_setup:
16140 err_pf_reset:
16141 	iounmap(hw->hw_addr);
16142 err_ioremap:
16143 	i40e_free_pf(pf);
16144 err_pf_alloc:
16145 	pci_release_mem_regions(pdev);
16146 err_pci_reg:
16147 err_dma:
16148 	pci_disable_device(pdev);
16149 	return err;
16150 }
16151 
16152 /**
16153  * i40e_remove - Device removal routine
16154  * @pdev: PCI device information struct
16155  *
16156  * i40e_remove is called by the PCI subsystem to alert the driver
16157  * that is should release a PCI device.  This could be caused by a
16158  * Hot-Plug event, or because the driver is going to be removed from
16159  * memory.
16160  **/
16161 static void i40e_remove(struct pci_dev *pdev)
16162 {
16163 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16164 	struct i40e_hw *hw = &pf->hw;
16165 	struct i40e_vsi *vsi;
16166 	struct i40e_veb *veb;
16167 	int ret_code;
16168 	int i;
16169 
16170 	i40e_devlink_unregister(pf);
16171 
16172 	i40e_dbg_pf_exit(pf);
16173 
16174 	i40e_ptp_stop(pf);
16175 
16176 	/* Disable RSS in hw */
16177 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16178 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16179 
16180 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16181 	 * flags, once they are set, i40e_rebuild should not be called as
16182 	 * i40e_prep_for_reset always returns early.
16183 	 */
16184 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16185 		usleep_range(1000, 2000);
16186 	set_bit(__I40E_IN_REMOVE, pf->state);
16187 
16188 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16189 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16190 		i40e_free_vfs(pf);
16191 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16192 	}
16193 	/* no more scheduling of any task */
16194 	set_bit(__I40E_SUSPENDED, pf->state);
16195 	set_bit(__I40E_DOWN, pf->state);
16196 	if (pf->service_timer.function)
16197 		timer_shutdown_sync(&pf->service_timer);
16198 	if (pf->service_task.func)
16199 		cancel_work_sync(&pf->service_task);
16200 
16201 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16202 		struct i40e_vsi *vsi = pf->vsi[0];
16203 
16204 		/* We know that we have allocated only one vsi for this PF,
16205 		 * it was just for registering netdevice, so the interface
16206 		 * could be visible in the 'ifconfig' output
16207 		 */
16208 		unregister_netdev(vsi->netdev);
16209 		free_netdev(vsi->netdev);
16210 
16211 		goto unmap;
16212 	}
16213 
16214 	/* Client close must be called explicitly here because the timer
16215 	 * has been stopped.
16216 	 */
16217 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16218 
16219 	i40e_fdir_teardown(pf);
16220 
16221 	/* If there is a switch structure or any orphans, remove them.
16222 	 * This will leave only the PF's VSI remaining.
16223 	 */
16224 	i40e_pf_for_each_veb(pf, i, veb)
16225 		if (veb->uplink_seid == pf->mac_seid ||
16226 		    veb->uplink_seid == 0)
16227 			i40e_switch_branch_release(veb);
16228 
16229 	/* Now we can shutdown the PF's VSIs, just before we kill
16230 	 * adminq and hmc.
16231 	 */
16232 	i40e_pf_for_each_vsi(pf, i, vsi) {
16233 		i40e_vsi_close(vsi);
16234 		i40e_vsi_release(vsi);
16235 		pf->vsi[i] = NULL;
16236 	}
16237 
16238 	i40e_cloud_filter_exit(pf);
16239 
16240 	/* remove attached clients */
16241 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16242 		ret_code = i40e_lan_del_device(pf);
16243 		if (ret_code)
16244 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16245 				 ret_code);
16246 	}
16247 
16248 	/* shutdown and destroy the HMC */
16249 	if (hw->hmc.hmc_obj) {
16250 		ret_code = i40e_shutdown_lan_hmc(hw);
16251 		if (ret_code)
16252 			dev_warn(&pdev->dev,
16253 				 "Failed to destroy the HMC resources: %d\n",
16254 				 ret_code);
16255 	}
16256 
16257 unmap:
16258 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16259 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16260 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16261 		free_irq(pf->pdev->irq, pf);
16262 
16263 	/* shutdown the adminq */
16264 	i40e_shutdown_adminq(hw);
16265 
16266 	/* destroy the locks only once, here */
16267 	mutex_destroy(&hw->aq.arq_mutex);
16268 	mutex_destroy(&hw->aq.asq_mutex);
16269 
16270 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16271 	rtnl_lock();
16272 	i40e_clear_interrupt_scheme(pf);
16273 	i40e_pf_for_each_vsi(pf, i, vsi) {
16274 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16275 			i40e_vsi_clear_rings(vsi);
16276 
16277 		i40e_vsi_clear(vsi);
16278 		pf->vsi[i] = NULL;
16279 	}
16280 	rtnl_unlock();
16281 
16282 	i40e_pf_for_each_veb(pf, i, veb) {
16283 		kfree(veb);
16284 		pf->veb[i] = NULL;
16285 	}
16286 
16287 	kfree(pf->qp_pile);
16288 	kfree(pf->vsi);
16289 
16290 	iounmap(hw->hw_addr);
16291 	i40e_free_pf(pf);
16292 	pci_release_mem_regions(pdev);
16293 
16294 	pci_disable_device(pdev);
16295 }
16296 
16297 /**
16298  * i40e_pci_error_detected - warning that something funky happened in PCI land
16299  * @pdev: PCI device information struct
16300  * @error: the type of PCI error
16301  *
16302  * Called to warn that something happened and the error handling steps
16303  * are in progress.  Allows the driver to quiesce things, be ready for
16304  * remediation.
16305  **/
16306 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16307 						pci_channel_state_t error)
16308 {
16309 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16310 
16311 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16312 
16313 	if (!pf) {
16314 		dev_info(&pdev->dev,
16315 			 "Cannot recover - error happened during device probe\n");
16316 		return PCI_ERS_RESULT_DISCONNECT;
16317 	}
16318 
16319 	/* shutdown all operations */
16320 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16321 		i40e_prep_for_reset(pf);
16322 
16323 	/* Request a slot reset */
16324 	return PCI_ERS_RESULT_NEED_RESET;
16325 }
16326 
16327 /**
16328  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16329  * @pdev: PCI device information struct
16330  *
16331  * Called to find if the driver can work with the device now that
16332  * the pci slot has been reset.  If a basic connection seems good
16333  * (registers are readable and have sane content) then return a
16334  * happy little PCI_ERS_RESULT_xxx.
16335  **/
16336 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16337 {
16338 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16339 	pci_ers_result_t result;
16340 	u32 reg;
16341 
16342 	dev_dbg(&pdev->dev, "%s\n", __func__);
16343 	if (pci_enable_device_mem(pdev)) {
16344 		dev_info(&pdev->dev,
16345 			 "Cannot re-enable PCI device after reset.\n");
16346 		result = PCI_ERS_RESULT_DISCONNECT;
16347 	} else {
16348 		pci_set_master(pdev);
16349 		pci_restore_state(pdev);
16350 		pci_save_state(pdev);
16351 		pci_wake_from_d3(pdev, false);
16352 
16353 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16354 		if (reg == 0)
16355 			result = PCI_ERS_RESULT_RECOVERED;
16356 		else
16357 			result = PCI_ERS_RESULT_DISCONNECT;
16358 	}
16359 
16360 	return result;
16361 }
16362 
16363 /**
16364  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16365  * @pdev: PCI device information struct
16366  */
16367 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16368 {
16369 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16370 
16371 	i40e_prep_for_reset(pf);
16372 }
16373 
16374 /**
16375  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16376  * @pdev: PCI device information struct
16377  */
16378 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16379 {
16380 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16381 
16382 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16383 		return;
16384 
16385 	i40e_reset_and_rebuild(pf, false, false);
16386 #ifdef CONFIG_PCI_IOV
16387 	i40e_restore_all_vfs_msi_state(pdev);
16388 #endif /* CONFIG_PCI_IOV */
16389 }
16390 
16391 /**
16392  * i40e_pci_error_resume - restart operations after PCI error recovery
16393  * @pdev: PCI device information struct
16394  *
16395  * Called to allow the driver to bring things back up after PCI error
16396  * and/or reset recovery has finished.
16397  **/
16398 static void i40e_pci_error_resume(struct pci_dev *pdev)
16399 {
16400 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16401 
16402 	dev_dbg(&pdev->dev, "%s\n", __func__);
16403 	if (test_bit(__I40E_SUSPENDED, pf->state))
16404 		return;
16405 
16406 	i40e_handle_reset_warning(pf, false);
16407 }
16408 
16409 /**
16410  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16411  * using the mac_address_write admin q function
16412  * @pf: pointer to i40e_pf struct
16413  **/
16414 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16415 {
16416 	struct i40e_hw *hw = &pf->hw;
16417 	u8 mac_addr[6];
16418 	u16 flags = 0;
16419 	int ret;
16420 
16421 	/* Get current MAC address in case it's an LAA */
16422 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16423 		ether_addr_copy(mac_addr,
16424 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16425 	} else {
16426 		dev_err(&pf->pdev->dev,
16427 			"Failed to retrieve MAC address; using default\n");
16428 		ether_addr_copy(mac_addr, hw->mac.addr);
16429 	}
16430 
16431 	/* The FW expects the mac address write cmd to first be called with
16432 	 * one of these flags before calling it again with the multicast
16433 	 * enable flags.
16434 	 */
16435 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16436 
16437 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16438 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16439 
16440 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16441 	if (ret) {
16442 		dev_err(&pf->pdev->dev,
16443 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16444 		return;
16445 	}
16446 
16447 	flags = I40E_AQC_MC_MAG_EN
16448 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16449 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16450 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16451 	if (ret)
16452 		dev_err(&pf->pdev->dev,
16453 			"Failed to enable Multicast Magic Packet wake up\n");
16454 }
16455 
16456 /**
16457  * i40e_shutdown - PCI callback for shutting down
16458  * @pdev: PCI device information struct
16459  **/
16460 static void i40e_shutdown(struct pci_dev *pdev)
16461 {
16462 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16463 	struct i40e_hw *hw = &pf->hw;
16464 
16465 	set_bit(__I40E_SUSPENDED, pf->state);
16466 	set_bit(__I40E_DOWN, pf->state);
16467 
16468 	del_timer_sync(&pf->service_timer);
16469 	cancel_work_sync(&pf->service_task);
16470 	i40e_cloud_filter_exit(pf);
16471 	i40e_fdir_teardown(pf);
16472 
16473 	/* Client close must be called explicitly here because the timer
16474 	 * has been stopped.
16475 	 */
16476 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16477 
16478 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16479 	    pf->wol_en)
16480 		i40e_enable_mc_magic_wake(pf);
16481 
16482 	i40e_prep_for_reset(pf);
16483 
16484 	wr32(hw, I40E_PFPM_APM,
16485 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16486 	wr32(hw, I40E_PFPM_WUFC,
16487 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16488 
16489 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16490 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16491 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16492 		free_irq(pf->pdev->irq, pf);
16493 
16494 	/* Since we're going to destroy queues during the
16495 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16496 	 * whole section
16497 	 */
16498 	rtnl_lock();
16499 	i40e_clear_interrupt_scheme(pf);
16500 	rtnl_unlock();
16501 
16502 	if (system_state == SYSTEM_POWER_OFF) {
16503 		pci_wake_from_d3(pdev, pf->wol_en);
16504 		pci_set_power_state(pdev, PCI_D3hot);
16505 	}
16506 }
16507 
16508 /**
16509  * i40e_suspend - PM callback for moving to D3
16510  * @dev: generic device information structure
16511  **/
16512 static int __maybe_unused i40e_suspend(struct device *dev)
16513 {
16514 	struct i40e_pf *pf = dev_get_drvdata(dev);
16515 	struct i40e_hw *hw = &pf->hw;
16516 
16517 	/* If we're already suspended, then there is nothing to do */
16518 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16519 		return 0;
16520 
16521 	set_bit(__I40E_DOWN, pf->state);
16522 
16523 	/* Ensure service task will not be running */
16524 	del_timer_sync(&pf->service_timer);
16525 	cancel_work_sync(&pf->service_task);
16526 
16527 	/* Client close must be called explicitly here because the timer
16528 	 * has been stopped.
16529 	 */
16530 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16531 
16532 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16533 	    pf->wol_en)
16534 		i40e_enable_mc_magic_wake(pf);
16535 
16536 	/* Since we're going to destroy queues during the
16537 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16538 	 * whole section
16539 	 */
16540 	rtnl_lock();
16541 
16542 	i40e_prep_for_reset(pf);
16543 
16544 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16545 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16546 
16547 	/* Clear the interrupt scheme and release our IRQs so that the system
16548 	 * can safely hibernate even when there are a large number of CPUs.
16549 	 * Otherwise hibernation might fail when mapping all the vectors back
16550 	 * to CPU0.
16551 	 */
16552 	i40e_clear_interrupt_scheme(pf);
16553 
16554 	rtnl_unlock();
16555 
16556 	return 0;
16557 }
16558 
16559 /**
16560  * i40e_resume - PM callback for waking up from D3
16561  * @dev: generic device information structure
16562  **/
16563 static int __maybe_unused i40e_resume(struct device *dev)
16564 {
16565 	struct i40e_pf *pf = dev_get_drvdata(dev);
16566 	int err;
16567 
16568 	/* If we're not suspended, then there is nothing to do */
16569 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16570 		return 0;
16571 
16572 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16573 	 * since we're going to be restoring queues
16574 	 */
16575 	rtnl_lock();
16576 
16577 	/* We cleared the interrupt scheme when we suspended, so we need to
16578 	 * restore it now to resume device functionality.
16579 	 */
16580 	err = i40e_restore_interrupt_scheme(pf);
16581 	if (err) {
16582 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16583 			err);
16584 	}
16585 
16586 	clear_bit(__I40E_DOWN, pf->state);
16587 	i40e_reset_and_rebuild(pf, false, true);
16588 
16589 	rtnl_unlock();
16590 
16591 	/* Clear suspended state last after everything is recovered */
16592 	clear_bit(__I40E_SUSPENDED, pf->state);
16593 
16594 	/* Restart the service task */
16595 	mod_timer(&pf->service_timer,
16596 		  round_jiffies(jiffies + pf->service_timer_period));
16597 
16598 	return 0;
16599 }
16600 
16601 static const struct pci_error_handlers i40e_err_handler = {
16602 	.error_detected = i40e_pci_error_detected,
16603 	.slot_reset = i40e_pci_error_slot_reset,
16604 	.reset_prepare = i40e_pci_error_reset_prepare,
16605 	.reset_done = i40e_pci_error_reset_done,
16606 	.resume = i40e_pci_error_resume,
16607 };
16608 
16609 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16610 
16611 static struct pci_driver i40e_driver = {
16612 	.name     = i40e_driver_name,
16613 	.id_table = i40e_pci_tbl,
16614 	.probe    = i40e_probe,
16615 	.remove   = i40e_remove,
16616 	.driver   = {
16617 		.pm = &i40e_pm_ops,
16618 	},
16619 	.shutdown = i40e_shutdown,
16620 	.err_handler = &i40e_err_handler,
16621 	.sriov_configure = i40e_pci_sriov_configure,
16622 };
16623 
16624 /**
16625  * i40e_init_module - Driver registration routine
16626  *
16627  * i40e_init_module is the first routine called when the driver is
16628  * loaded. All it does is register with the PCI subsystem.
16629  **/
16630 static int __init i40e_init_module(void)
16631 {
16632 	int err;
16633 
16634 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16635 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16636 
16637 	/* There is no need to throttle the number of active tasks because
16638 	 * each device limits its own task using a state bit for scheduling
16639 	 * the service task, and the device tasks do not interfere with each
16640 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16641 	 * since we need to be able to guarantee forward progress even under
16642 	 * memory pressure.
16643 	 */
16644 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16645 	if (!i40e_wq) {
16646 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16647 		return -ENOMEM;
16648 	}
16649 
16650 	i40e_dbg_init();
16651 	err = pci_register_driver(&i40e_driver);
16652 	if (err) {
16653 		destroy_workqueue(i40e_wq);
16654 		i40e_dbg_exit();
16655 		return err;
16656 	}
16657 
16658 	return 0;
16659 }
16660 module_init(i40e_init_module);
16661 
16662 /**
16663  * i40e_exit_module - Driver exit cleanup routine
16664  *
16665  * i40e_exit_module is called just before the driver is removed
16666  * from memory.
16667  **/
16668 static void __exit i40e_exit_module(void)
16669 {
16670 	pci_unregister_driver(&i40e_driver);
16671 	destroy_workqueue(i40e_wq);
16672 	ida_destroy(&i40e_client_ida);
16673 	i40e_dbg_exit();
16674 }
16675 module_exit(i40e_exit_module);
16676