xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 5f346a6e5970229c19c059e8fa62c3dbdde56e7b)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <generated/utsrelease.h>
5 #include <linux/crash_dump.h>
6 #include <linux/if_bridge.h>
7 #include <linux/if_macvlan.h>
8 #include <linux/module.h>
9 #include <net/pkt_cls.h>
10 #include <net/xdp_sock_drv.h>
11 
12 /* Local includes */
13 #include "i40e.h"
14 #include "i40e_devids.h"
15 #include "i40e_diag.h"
16 #include "i40e_lan_hmc.h"
17 #include "i40e_virtchnl_pf.h"
18 #include "i40e_xsk.h"
19 
20 /* All i40e tracepoints are defined by the include below, which
21  * must be included exactly once across the whole kernel with
22  * CREATE_TRACE_POINTS defined
23  */
24 #define CREATE_TRACE_POINTS
25 #include "i40e_trace.h"
26 
27 const char i40e_driver_name[] = "i40e";
28 static const char i40e_driver_string[] =
29 			"Intel(R) Ethernet Connection XL710 Network Driver";
30 
31 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
32 
33 /* a bit of forward declarations */
34 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
35 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
36 static int i40e_add_vsi(struct i40e_vsi *vsi);
37 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
38 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
39 static int i40e_setup_misc_vector(struct i40e_pf *pf);
40 static void i40e_determine_queue_usage(struct i40e_pf *pf);
41 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
42 static void i40e_prep_for_reset(struct i40e_pf *pf);
43 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
44 				   bool lock_acquired);
45 static int i40e_reset(struct i40e_pf *pf);
46 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
47 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
48 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
49 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
50 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
51 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
52 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
53 static int i40e_get_capabilities(struct i40e_pf *pf,
54 				 enum i40e_admin_queue_opc list_type);
55 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
56 
57 /* i40e_pci_tbl - PCI Device ID Table
58  *
59  * Last entry must be all 0s
60  *
61  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
62  *   Class, Class Mask, private data (not used) }
63  */
64 static const struct pci_device_id i40e_pci_tbl[] = {
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
88 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
89 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
90 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
91 	/* required last entry */
92 	{0, }
93 };
94 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
95 
96 #define I40E_MAX_VF_COUNT 128
97 static int debug = -1;
98 module_param(debug, uint, 0);
99 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
100 
101 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
102 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
103 MODULE_LICENSE("GPL v2");
104 
105 static struct workqueue_struct *i40e_wq;
106 
107 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
108 				  struct net_device *netdev, int delta)
109 {
110 	struct netdev_hw_addr_list *ha_list;
111 	struct netdev_hw_addr *ha;
112 
113 	if (!f || !netdev)
114 		return;
115 
116 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
117 		ha_list = &netdev->uc;
118 	else
119 		ha_list = &netdev->mc;
120 
121 	netdev_hw_addr_list_for_each(ha, ha_list) {
122 		if (ether_addr_equal(ha->addr, f->macaddr)) {
123 			ha->refcount += delta;
124 			if (ha->refcount <= 0)
125 				ha->refcount = 1;
126 			break;
127 		}
128 	}
129 }
130 
131 /**
132  * i40e_hw_to_dev - get device pointer from the hardware structure
133  * @hw: pointer to the device HW structure
134  **/
135 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
136 {
137 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
138 
139 	return &pf->pdev->dev;
140 }
141 
142 /**
143  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
144  * @hw:   pointer to the HW structure
145  * @mem:  ptr to mem struct to fill out
146  * @size: size of memory requested
147  * @alignment: what to align the allocation to
148  **/
149 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
150 			  u64 size, u32 alignment)
151 {
152 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
153 
154 	mem->size = ALIGN(size, alignment);
155 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
156 				     GFP_KERNEL);
157 	if (!mem->va)
158 		return -ENOMEM;
159 
160 	return 0;
161 }
162 
163 /**
164  * i40e_free_dma_mem - OS specific memory free for shared code
165  * @hw:   pointer to the HW structure
166  * @mem:  ptr to mem struct to free
167  **/
168 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
169 {
170 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
171 
172 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
173 	mem->va = NULL;
174 	mem->pa = 0;
175 	mem->size = 0;
176 
177 	return 0;
178 }
179 
180 /**
181  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
182  * @hw:   pointer to the HW structure
183  * @mem:  ptr to mem struct to fill out
184  * @size: size of memory requested
185  **/
186 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
187 			   u32 size)
188 {
189 	mem->size = size;
190 	mem->va = kzalloc(size, GFP_KERNEL);
191 
192 	if (!mem->va)
193 		return -ENOMEM;
194 
195 	return 0;
196 }
197 
198 /**
199  * i40e_free_virt_mem - OS specific memory free for shared code
200  * @hw:   pointer to the HW structure
201  * @mem:  ptr to mem struct to free
202  **/
203 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
204 {
205 	/* it's ok to kfree a NULL pointer */
206 	kfree(mem->va);
207 	mem->va = NULL;
208 	mem->size = 0;
209 
210 	return 0;
211 }
212 
213 /**
214  * i40e_get_lump - find a lump of free generic resource
215  * @pf: board private structure
216  * @pile: the pile of resource to search
217  * @needed: the number of items needed
218  * @id: an owner id to stick on the items assigned
219  *
220  * Returns the base item index of the lump, or negative for error
221  **/
222 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
223 			 u16 needed, u16 id)
224 {
225 	int ret = -ENOMEM;
226 	int i, j;
227 
228 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
229 		dev_info(&pf->pdev->dev,
230 			 "param err: pile=%s needed=%d id=0x%04x\n",
231 			 pile ? "<valid>" : "<null>", needed, id);
232 		return -EINVAL;
233 	}
234 
235 	/* Allocate last queue in the pile for FDIR VSI queue
236 	 * so it doesn't fragment the qp_pile
237 	 */
238 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
239 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
240 			dev_err(&pf->pdev->dev,
241 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
242 				pile->num_entries - 1);
243 			return -ENOMEM;
244 		}
245 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
246 		return pile->num_entries - 1;
247 	}
248 
249 	i = 0;
250 	while (i < pile->num_entries) {
251 		/* skip already allocated entries */
252 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
253 			i++;
254 			continue;
255 		}
256 
257 		/* do we have enough in this lump? */
258 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
259 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
260 				break;
261 		}
262 
263 		if (j == needed) {
264 			/* there was enough, so assign it to the requestor */
265 			for (j = 0; j < needed; j++)
266 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
267 			ret = i;
268 			break;
269 		}
270 
271 		/* not enough, so skip over it and continue looking */
272 		i += j;
273 	}
274 
275 	return ret;
276 }
277 
278 /**
279  * i40e_put_lump - return a lump of generic resource
280  * @pile: the pile of resource to search
281  * @index: the base item index
282  * @id: the owner id of the items assigned
283  *
284  * Returns the count of items in the lump
285  **/
286 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
287 {
288 	int valid_id = (id | I40E_PILE_VALID_BIT);
289 	int count = 0;
290 	u16 i;
291 
292 	if (!pile || index >= pile->num_entries)
293 		return -EINVAL;
294 
295 	for (i = index;
296 	     i < pile->num_entries && pile->list[i] == valid_id;
297 	     i++) {
298 		pile->list[i] = 0;
299 		count++;
300 	}
301 
302 
303 	return count;
304 }
305 
306 /**
307  * i40e_find_vsi_from_id - searches for the vsi with the given id
308  * @pf: the pf structure to search for the vsi
309  * @id: id of the vsi it is searching for
310  **/
311 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
312 {
313 	int i;
314 
315 	for (i = 0; i < pf->num_alloc_vsi; i++)
316 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
317 			return pf->vsi[i];
318 
319 	return NULL;
320 }
321 
322 /**
323  * i40e_service_event_schedule - Schedule the service task to wake up
324  * @pf: board private structure
325  *
326  * If not already scheduled, this puts the task into the work queue
327  **/
328 void i40e_service_event_schedule(struct i40e_pf *pf)
329 {
330 	if ((!test_bit(__I40E_DOWN, pf->state) &&
331 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
332 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
333 		queue_work(i40e_wq, &pf->service_task);
334 }
335 
336 /**
337  * i40e_tx_timeout - Respond to a Tx Hang
338  * @netdev: network interface device structure
339  * @txqueue: queue number timing out
340  *
341  * If any port has noticed a Tx timeout, it is likely that the whole
342  * device is munged, not just the one netdev port, so go for the full
343  * reset.
344  **/
345 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
346 {
347 	struct i40e_netdev_priv *np = netdev_priv(netdev);
348 	struct i40e_vsi *vsi = np->vsi;
349 	struct i40e_pf *pf = vsi->back;
350 	struct i40e_ring *tx_ring = NULL;
351 	unsigned int i;
352 	u32 head, val;
353 
354 	pf->tx_timeout_count++;
355 
356 	/* with txqueue index, find the tx_ring struct */
357 	for (i = 0; i < vsi->num_queue_pairs; i++) {
358 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
359 			if (txqueue ==
360 			    vsi->tx_rings[i]->queue_index) {
361 				tx_ring = vsi->tx_rings[i];
362 				break;
363 			}
364 		}
365 	}
366 
367 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
368 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
369 	else if (time_before(jiffies,
370 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
371 		return;   /* don't do any new action before the next timeout */
372 
373 	/* don't kick off another recovery if one is already pending */
374 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
375 		return;
376 
377 	if (tx_ring) {
378 		head = i40e_get_head(tx_ring);
379 		/* Read interrupt register */
380 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
381 			val = rd32(&pf->hw,
382 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
383 						tx_ring->vsi->base_vector - 1));
384 		else
385 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
386 
387 		netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
388 			    vsi->seid, txqueue, tx_ring->next_to_clean,
389 			    head, tx_ring->next_to_use,
390 			    readl(tx_ring->tail), val);
391 	}
392 
393 	pf->tx_timeout_last_recovery = jiffies;
394 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
395 		    pf->tx_timeout_recovery_level, txqueue);
396 
397 	switch (pf->tx_timeout_recovery_level) {
398 	case 1:
399 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
400 		break;
401 	case 2:
402 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
403 		break;
404 	case 3:
405 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
406 		break;
407 	default:
408 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
409 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
410 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
411 		break;
412 	}
413 
414 	i40e_service_event_schedule(pf);
415 	pf->tx_timeout_recovery_level++;
416 }
417 
418 /**
419  * i40e_get_vsi_stats_struct - Get System Network Statistics
420  * @vsi: the VSI we care about
421  *
422  * Returns the address of the device statistics structure.
423  * The statistics are actually updated from the service task.
424  **/
425 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
426 {
427 	return &vsi->net_stats;
428 }
429 
430 /**
431  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
432  * @ring: Tx ring to get statistics from
433  * @stats: statistics entry to be updated
434  **/
435 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
436 					    struct rtnl_link_stats64 *stats)
437 {
438 	u64 bytes, packets;
439 	unsigned int start;
440 
441 	do {
442 		start = u64_stats_fetch_begin(&ring->syncp);
443 		packets = ring->stats.packets;
444 		bytes   = ring->stats.bytes;
445 	} while (u64_stats_fetch_retry(&ring->syncp, start));
446 
447 	stats->tx_packets += packets;
448 	stats->tx_bytes   += bytes;
449 }
450 
451 /**
452  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
453  * @netdev: network interface device structure
454  * @stats: data structure to store statistics
455  *
456  * Returns the address of the device statistics structure.
457  * The statistics are actually updated from the service task.
458  **/
459 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
460 				  struct rtnl_link_stats64 *stats)
461 {
462 	struct i40e_netdev_priv *np = netdev_priv(netdev);
463 	struct i40e_vsi *vsi = np->vsi;
464 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
465 	struct i40e_ring *ring;
466 	int i;
467 
468 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
469 		return;
470 
471 	if (!vsi->tx_rings)
472 		return;
473 
474 	rcu_read_lock();
475 	for (i = 0; i < vsi->num_queue_pairs; i++) {
476 		u64 bytes, packets;
477 		unsigned int start;
478 
479 		ring = READ_ONCE(vsi->tx_rings[i]);
480 		if (!ring)
481 			continue;
482 		i40e_get_netdev_stats_struct_tx(ring, stats);
483 
484 		if (i40e_enabled_xdp_vsi(vsi)) {
485 			ring = READ_ONCE(vsi->xdp_rings[i]);
486 			if (!ring)
487 				continue;
488 			i40e_get_netdev_stats_struct_tx(ring, stats);
489 		}
490 
491 		ring = READ_ONCE(vsi->rx_rings[i]);
492 		if (!ring)
493 			continue;
494 		do {
495 			start   = u64_stats_fetch_begin(&ring->syncp);
496 			packets = ring->stats.packets;
497 			bytes   = ring->stats.bytes;
498 		} while (u64_stats_fetch_retry(&ring->syncp, start));
499 
500 		stats->rx_packets += packets;
501 		stats->rx_bytes   += bytes;
502 
503 	}
504 	rcu_read_unlock();
505 
506 	/* following stats updated by i40e_watchdog_subtask() */
507 	stats->multicast	= vsi_stats->multicast;
508 	stats->tx_errors	= vsi_stats->tx_errors;
509 	stats->tx_dropped	= vsi_stats->tx_dropped;
510 	stats->rx_errors	= vsi_stats->rx_errors;
511 	stats->rx_dropped	= vsi_stats->rx_dropped;
512 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
513 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
514 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
515 }
516 
517 /**
518  * i40e_vsi_reset_stats - Resets all stats of the given vsi
519  * @vsi: the VSI to have its stats reset
520  **/
521 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
522 {
523 	struct rtnl_link_stats64 *ns;
524 	int i;
525 
526 	if (!vsi)
527 		return;
528 
529 	ns = i40e_get_vsi_stats_struct(vsi);
530 	memset(ns, 0, sizeof(*ns));
531 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
532 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
533 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
534 	if (vsi->rx_rings && vsi->rx_rings[0]) {
535 		for (i = 0; i < vsi->num_queue_pairs; i++) {
536 			memset(&vsi->rx_rings[i]->stats, 0,
537 			       sizeof(vsi->rx_rings[i]->stats));
538 			memset(&vsi->rx_rings[i]->rx_stats, 0,
539 			       sizeof(vsi->rx_rings[i]->rx_stats));
540 			memset(&vsi->tx_rings[i]->stats, 0,
541 			       sizeof(vsi->tx_rings[i]->stats));
542 			memset(&vsi->tx_rings[i]->tx_stats, 0,
543 			       sizeof(vsi->tx_rings[i]->tx_stats));
544 		}
545 	}
546 	vsi->stat_offsets_loaded = false;
547 }
548 
549 /**
550  * i40e_pf_reset_stats - Reset all of the stats for the given PF
551  * @pf: the PF to be reset
552  **/
553 void i40e_pf_reset_stats(struct i40e_pf *pf)
554 {
555 	int i;
556 
557 	memset(&pf->stats, 0, sizeof(pf->stats));
558 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
559 	pf->stat_offsets_loaded = false;
560 
561 	for (i = 0; i < I40E_MAX_VEB; i++) {
562 		if (pf->veb[i]) {
563 			memset(&pf->veb[i]->stats, 0,
564 			       sizeof(pf->veb[i]->stats));
565 			memset(&pf->veb[i]->stats_offsets, 0,
566 			       sizeof(pf->veb[i]->stats_offsets));
567 			memset(&pf->veb[i]->tc_stats, 0,
568 			       sizeof(pf->veb[i]->tc_stats));
569 			memset(&pf->veb[i]->tc_stats_offsets, 0,
570 			       sizeof(pf->veb[i]->tc_stats_offsets));
571 			pf->veb[i]->stat_offsets_loaded = false;
572 		}
573 	}
574 	pf->hw_csum_rx_error = 0;
575 }
576 
577 /**
578  * i40e_compute_pci_to_hw_id - compute index form PCI function.
579  * @vsi: ptr to the VSI to read from.
580  * @hw: ptr to the hardware info.
581  **/
582 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
583 {
584 	int pf_count = i40e_get_pf_count(hw);
585 
586 	if (vsi->type == I40E_VSI_SRIOV)
587 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
588 
589 	return hw->port + BIT(7);
590 }
591 
592 /**
593  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
594  * @hw: ptr to the hardware info.
595  * @hireg: the high 32 bit reg to read.
596  * @loreg: the low 32 bit reg to read.
597  * @offset_loaded: has the initial offset been loaded yet.
598  * @offset: ptr to current offset value.
599  * @stat: ptr to the stat.
600  *
601  * Since the device stats are not reset at PFReset, they will not
602  * be zeroed when the driver starts.  We'll save the first values read
603  * and use them as offsets to be subtracted from the raw values in order
604  * to report stats that count from zero.
605  **/
606 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
607 			       bool offset_loaded, u64 *offset, u64 *stat)
608 {
609 	u64 new_data;
610 
611 	new_data = rd64(hw, loreg);
612 
613 	if (!offset_loaded || new_data < *offset)
614 		*offset = new_data;
615 	*stat = new_data - *offset;
616 }
617 
618 /**
619  * i40e_stat_update48 - read and update a 48 bit stat from the chip
620  * @hw: ptr to the hardware info
621  * @hireg: the high 32 bit reg to read
622  * @loreg: the low 32 bit reg to read
623  * @offset_loaded: has the initial offset been loaded yet
624  * @offset: ptr to current offset value
625  * @stat: ptr to the stat
626  *
627  * Since the device stats are not reset at PFReset, they likely will not
628  * be zeroed when the driver starts.  We'll save the first values read
629  * and use them as offsets to be subtracted from the raw values in order
630  * to report stats that count from zero.  In the process, we also manage
631  * the potential roll-over.
632  **/
633 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
634 			       bool offset_loaded, u64 *offset, u64 *stat)
635 {
636 	u64 new_data;
637 
638 	if (hw->device_id == I40E_DEV_ID_QEMU) {
639 		new_data = rd32(hw, loreg);
640 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
641 	} else {
642 		new_data = rd64(hw, loreg);
643 	}
644 	if (!offset_loaded)
645 		*offset = new_data;
646 	if (likely(new_data >= *offset))
647 		*stat = new_data - *offset;
648 	else
649 		*stat = (new_data + BIT_ULL(48)) - *offset;
650 	*stat &= 0xFFFFFFFFFFFFULL;
651 }
652 
653 /**
654  * i40e_stat_update32 - read and update a 32 bit stat from the chip
655  * @hw: ptr to the hardware info
656  * @reg: the hw reg to read
657  * @offset_loaded: has the initial offset been loaded yet
658  * @offset: ptr to current offset value
659  * @stat: ptr to the stat
660  **/
661 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
662 			       bool offset_loaded, u64 *offset, u64 *stat)
663 {
664 	u32 new_data;
665 
666 	new_data = rd32(hw, reg);
667 	if (!offset_loaded)
668 		*offset = new_data;
669 	if (likely(new_data >= *offset))
670 		*stat = (u32)(new_data - *offset);
671 	else
672 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
673 }
674 
675 /**
676  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
677  * @hw: ptr to the hardware info
678  * @reg: the hw reg to read and clear
679  * @stat: ptr to the stat
680  **/
681 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
682 {
683 	u32 new_data = rd32(hw, reg);
684 
685 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
686 	*stat += new_data;
687 }
688 
689 /**
690  * i40e_stats_update_rx_discards - update rx_discards.
691  * @vsi: ptr to the VSI to be updated.
692  * @hw: ptr to the hardware info.
693  * @stat_idx: VSI's stat_counter_idx.
694  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
695  * @stat_offset: ptr to stat_offset to store first read of specific register.
696  * @stat: ptr to VSI's stat to be updated.
697  **/
698 static void
699 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
700 			      int stat_idx, bool offset_loaded,
701 			      struct i40e_eth_stats *stat_offset,
702 			      struct i40e_eth_stats *stat)
703 {
704 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
705 			   &stat_offset->rx_discards, &stat->rx_discards);
706 	i40e_stat_update64(hw,
707 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
708 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
709 			   offset_loaded, &stat_offset->rx_discards_other,
710 			   &stat->rx_discards_other);
711 }
712 
713 /**
714  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
715  * @vsi: the VSI to be updated
716  **/
717 void i40e_update_eth_stats(struct i40e_vsi *vsi)
718 {
719 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
720 	struct i40e_pf *pf = vsi->back;
721 	struct i40e_hw *hw = &pf->hw;
722 	struct i40e_eth_stats *oes;
723 	struct i40e_eth_stats *es;     /* device's eth stats */
724 
725 	es = &vsi->eth_stats;
726 	oes = &vsi->eth_stats_offsets;
727 
728 	/* Gather up the stats that the hw collects */
729 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
730 			   vsi->stat_offsets_loaded,
731 			   &oes->tx_errors, &es->tx_errors);
732 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
733 			   vsi->stat_offsets_loaded,
734 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
735 
736 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
737 			   I40E_GLV_GORCL(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->rx_bytes, &es->rx_bytes);
740 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
741 			   I40E_GLV_UPRCL(stat_idx),
742 			   vsi->stat_offsets_loaded,
743 			   &oes->rx_unicast, &es->rx_unicast);
744 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
745 			   I40E_GLV_MPRCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->rx_multicast, &es->rx_multicast);
748 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
749 			   I40E_GLV_BPRCL(stat_idx),
750 			   vsi->stat_offsets_loaded,
751 			   &oes->rx_broadcast, &es->rx_broadcast);
752 
753 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
754 			   I40E_GLV_GOTCL(stat_idx),
755 			   vsi->stat_offsets_loaded,
756 			   &oes->tx_bytes, &es->tx_bytes);
757 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
758 			   I40E_GLV_UPTCL(stat_idx),
759 			   vsi->stat_offsets_loaded,
760 			   &oes->tx_unicast, &es->tx_unicast);
761 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
762 			   I40E_GLV_MPTCL(stat_idx),
763 			   vsi->stat_offsets_loaded,
764 			   &oes->tx_multicast, &es->tx_multicast);
765 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
766 			   I40E_GLV_BPTCL(stat_idx),
767 			   vsi->stat_offsets_loaded,
768 			   &oes->tx_broadcast, &es->tx_broadcast);
769 
770 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
771 				      vsi->stat_offsets_loaded, oes, es);
772 
773 	vsi->stat_offsets_loaded = true;
774 }
775 
776 /**
777  * i40e_update_veb_stats - Update Switch component statistics
778  * @veb: the VEB being updated
779  **/
780 void i40e_update_veb_stats(struct i40e_veb *veb)
781 {
782 	struct i40e_pf *pf = veb->pf;
783 	struct i40e_hw *hw = &pf->hw;
784 	struct i40e_eth_stats *oes;
785 	struct i40e_eth_stats *es;     /* device's eth stats */
786 	struct i40e_veb_tc_stats *veb_oes;
787 	struct i40e_veb_tc_stats *veb_es;
788 	int i, idx = 0;
789 
790 	idx = veb->stats_idx;
791 	es = &veb->stats;
792 	oes = &veb->stats_offsets;
793 	veb_es = &veb->tc_stats;
794 	veb_oes = &veb->tc_stats_offsets;
795 
796 	/* Gather up the stats that the hw collects */
797 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->tx_discards, &es->tx_discards);
800 	if (hw->revision_id > 0)
801 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
802 				   veb->stat_offsets_loaded,
803 				   &oes->rx_unknown_protocol,
804 				   &es->rx_unknown_protocol);
805 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
806 			   veb->stat_offsets_loaded,
807 			   &oes->rx_bytes, &es->rx_bytes);
808 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
809 			   veb->stat_offsets_loaded,
810 			   &oes->rx_unicast, &es->rx_unicast);
811 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
812 			   veb->stat_offsets_loaded,
813 			   &oes->rx_multicast, &es->rx_multicast);
814 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
815 			   veb->stat_offsets_loaded,
816 			   &oes->rx_broadcast, &es->rx_broadcast);
817 
818 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
819 			   veb->stat_offsets_loaded,
820 			   &oes->tx_bytes, &es->tx_bytes);
821 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
822 			   veb->stat_offsets_loaded,
823 			   &oes->tx_unicast, &es->tx_unicast);
824 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
825 			   veb->stat_offsets_loaded,
826 			   &oes->tx_multicast, &es->tx_multicast);
827 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
828 			   veb->stat_offsets_loaded,
829 			   &oes->tx_broadcast, &es->tx_broadcast);
830 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
831 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
832 				   I40E_GLVEBTC_RPCL(i, idx),
833 				   veb->stat_offsets_loaded,
834 				   &veb_oes->tc_rx_packets[i],
835 				   &veb_es->tc_rx_packets[i]);
836 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
837 				   I40E_GLVEBTC_RBCL(i, idx),
838 				   veb->stat_offsets_loaded,
839 				   &veb_oes->tc_rx_bytes[i],
840 				   &veb_es->tc_rx_bytes[i]);
841 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
842 				   I40E_GLVEBTC_TPCL(i, idx),
843 				   veb->stat_offsets_loaded,
844 				   &veb_oes->tc_tx_packets[i],
845 				   &veb_es->tc_tx_packets[i]);
846 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
847 				   I40E_GLVEBTC_TBCL(i, idx),
848 				   veb->stat_offsets_loaded,
849 				   &veb_oes->tc_tx_bytes[i],
850 				   &veb_es->tc_tx_bytes[i]);
851 	}
852 	veb->stat_offsets_loaded = true;
853 }
854 
855 /**
856  * i40e_update_vsi_stats - Update the vsi statistics counters.
857  * @vsi: the VSI to be updated
858  *
859  * There are a few instances where we store the same stat in a
860  * couple of different structs.  This is partly because we have
861  * the netdev stats that need to be filled out, which is slightly
862  * different from the "eth_stats" defined by the chip and used in
863  * VF communications.  We sort it out here.
864  **/
865 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
866 {
867 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
868 	struct i40e_pf *pf = vsi->back;
869 	struct rtnl_link_stats64 *ons;
870 	struct rtnl_link_stats64 *ns;   /* netdev stats */
871 	struct i40e_eth_stats *oes;
872 	struct i40e_eth_stats *es;     /* device's eth stats */
873 	u64 tx_restart, tx_busy;
874 	struct i40e_ring *p;
875 	u64 bytes, packets;
876 	unsigned int start;
877 	u64 tx_linearize;
878 	u64 tx_force_wb;
879 	u64 tx_stopped;
880 	u64 rx_p, rx_b;
881 	u64 tx_p, tx_b;
882 	u16 q;
883 
884 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
885 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
886 		return;
887 
888 	ns = i40e_get_vsi_stats_struct(vsi);
889 	ons = &vsi->net_stats_offsets;
890 	es = &vsi->eth_stats;
891 	oes = &vsi->eth_stats_offsets;
892 
893 	/* Gather up the netdev and vsi stats that the driver collects
894 	 * on the fly during packet processing
895 	 */
896 	rx_b = rx_p = 0;
897 	tx_b = tx_p = 0;
898 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
899 	tx_stopped = 0;
900 	rx_page = 0;
901 	rx_buf = 0;
902 	rx_reuse = 0;
903 	rx_alloc = 0;
904 	rx_waive = 0;
905 	rx_busy = 0;
906 	rcu_read_lock();
907 	for (q = 0; q < vsi->num_queue_pairs; q++) {
908 		/* locate Tx ring */
909 		p = READ_ONCE(vsi->tx_rings[q]);
910 		if (!p)
911 			continue;
912 
913 		do {
914 			start = u64_stats_fetch_begin(&p->syncp);
915 			packets = p->stats.packets;
916 			bytes = p->stats.bytes;
917 		} while (u64_stats_fetch_retry(&p->syncp, start));
918 		tx_b += bytes;
919 		tx_p += packets;
920 		tx_restart += p->tx_stats.restart_queue;
921 		tx_busy += p->tx_stats.tx_busy;
922 		tx_linearize += p->tx_stats.tx_linearize;
923 		tx_force_wb += p->tx_stats.tx_force_wb;
924 		tx_stopped += p->tx_stats.tx_stopped;
925 
926 		/* locate Rx ring */
927 		p = READ_ONCE(vsi->rx_rings[q]);
928 		if (!p)
929 			continue;
930 
931 		do {
932 			start = u64_stats_fetch_begin(&p->syncp);
933 			packets = p->stats.packets;
934 			bytes = p->stats.bytes;
935 		} while (u64_stats_fetch_retry(&p->syncp, start));
936 		rx_b += bytes;
937 		rx_p += packets;
938 		rx_buf += p->rx_stats.alloc_buff_failed;
939 		rx_page += p->rx_stats.alloc_page_failed;
940 		rx_reuse += p->rx_stats.page_reuse_count;
941 		rx_alloc += p->rx_stats.page_alloc_count;
942 		rx_waive += p->rx_stats.page_waive_count;
943 		rx_busy += p->rx_stats.page_busy_count;
944 
945 		if (i40e_enabled_xdp_vsi(vsi)) {
946 			/* locate XDP ring */
947 			p = READ_ONCE(vsi->xdp_rings[q]);
948 			if (!p)
949 				continue;
950 
951 			do {
952 				start = u64_stats_fetch_begin(&p->syncp);
953 				packets = p->stats.packets;
954 				bytes = p->stats.bytes;
955 			} while (u64_stats_fetch_retry(&p->syncp, start));
956 			tx_b += bytes;
957 			tx_p += packets;
958 			tx_restart += p->tx_stats.restart_queue;
959 			tx_busy += p->tx_stats.tx_busy;
960 			tx_linearize += p->tx_stats.tx_linearize;
961 			tx_force_wb += p->tx_stats.tx_force_wb;
962 		}
963 	}
964 	rcu_read_unlock();
965 	vsi->tx_restart = tx_restart;
966 	vsi->tx_busy = tx_busy;
967 	vsi->tx_linearize = tx_linearize;
968 	vsi->tx_force_wb = tx_force_wb;
969 	vsi->tx_stopped = tx_stopped;
970 	vsi->rx_page_failed = rx_page;
971 	vsi->rx_buf_failed = rx_buf;
972 	vsi->rx_page_reuse = rx_reuse;
973 	vsi->rx_page_alloc = rx_alloc;
974 	vsi->rx_page_waive = rx_waive;
975 	vsi->rx_page_busy = rx_busy;
976 
977 	ns->rx_packets = rx_p;
978 	ns->rx_bytes = rx_b;
979 	ns->tx_packets = tx_p;
980 	ns->tx_bytes = tx_b;
981 
982 	/* update netdev stats from eth stats */
983 	i40e_update_eth_stats(vsi);
984 	ons->tx_errors = oes->tx_errors;
985 	ns->tx_errors = es->tx_errors;
986 	ons->multicast = oes->rx_multicast;
987 	ns->multicast = es->rx_multicast;
988 	ons->rx_dropped = oes->rx_discards_other;
989 	ns->rx_dropped = es->rx_discards_other;
990 	ons->rx_missed_errors = oes->rx_discards;
991 	ns->rx_missed_errors = es->rx_discards;
992 	ons->tx_dropped = oes->tx_discards;
993 	ns->tx_dropped = es->tx_discards;
994 
995 	/* pull in a couple PF stats if this is the main vsi */
996 	if (vsi == pf->vsi[pf->lan_vsi]) {
997 		ns->rx_crc_errors = pf->stats.crc_errors;
998 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
999 		ns->rx_length_errors = pf->stats.rx_length_errors;
1000 	}
1001 }
1002 
1003 /**
1004  * i40e_update_pf_stats - Update the PF statistics counters.
1005  * @pf: the PF to be updated
1006  **/
1007 static void i40e_update_pf_stats(struct i40e_pf *pf)
1008 {
1009 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1010 	struct i40e_hw_port_stats *nsd = &pf->stats;
1011 	struct i40e_hw *hw = &pf->hw;
1012 	u32 val;
1013 	int i;
1014 
1015 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1016 			   I40E_GLPRT_GORCL(hw->port),
1017 			   pf->stat_offsets_loaded,
1018 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1019 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1020 			   I40E_GLPRT_GOTCL(hw->port),
1021 			   pf->stat_offsets_loaded,
1022 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1023 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1024 			   pf->stat_offsets_loaded,
1025 			   &osd->eth.rx_discards,
1026 			   &nsd->eth.rx_discards);
1027 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1028 			   I40E_GLPRT_UPRCL(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->eth.rx_unicast,
1031 			   &nsd->eth.rx_unicast);
1032 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1033 			   I40E_GLPRT_MPRCL(hw->port),
1034 			   pf->stat_offsets_loaded,
1035 			   &osd->eth.rx_multicast,
1036 			   &nsd->eth.rx_multicast);
1037 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1038 			   I40E_GLPRT_BPRCL(hw->port),
1039 			   pf->stat_offsets_loaded,
1040 			   &osd->eth.rx_broadcast,
1041 			   &nsd->eth.rx_broadcast);
1042 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1043 			   I40E_GLPRT_UPTCL(hw->port),
1044 			   pf->stat_offsets_loaded,
1045 			   &osd->eth.tx_unicast,
1046 			   &nsd->eth.tx_unicast);
1047 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1048 			   I40E_GLPRT_MPTCL(hw->port),
1049 			   pf->stat_offsets_loaded,
1050 			   &osd->eth.tx_multicast,
1051 			   &nsd->eth.tx_multicast);
1052 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1053 			   I40E_GLPRT_BPTCL(hw->port),
1054 			   pf->stat_offsets_loaded,
1055 			   &osd->eth.tx_broadcast,
1056 			   &nsd->eth.tx_broadcast);
1057 
1058 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1059 			   pf->stat_offsets_loaded,
1060 			   &osd->tx_dropped_link_down,
1061 			   &nsd->tx_dropped_link_down);
1062 
1063 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1064 			   pf->stat_offsets_loaded,
1065 			   &osd->crc_errors, &nsd->crc_errors);
1066 
1067 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1068 			   pf->stat_offsets_loaded,
1069 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->mac_local_faults,
1074 			   &nsd->mac_local_faults);
1075 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->mac_remote_faults,
1078 			   &nsd->mac_remote_faults);
1079 
1080 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1081 			   pf->stat_offsets_loaded,
1082 			   &osd->rx_length_errors,
1083 			   &nsd->rx_length_errors);
1084 
1085 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1086 			   pf->stat_offsets_loaded,
1087 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1088 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1091 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1092 			   pf->stat_offsets_loaded,
1093 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1094 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1095 			   pf->stat_offsets_loaded,
1096 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1097 
1098 	for (i = 0; i < 8; i++) {
1099 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1100 				   pf->stat_offsets_loaded,
1101 				   &osd->priority_xoff_rx[i],
1102 				   &nsd->priority_xoff_rx[i]);
1103 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1104 				   pf->stat_offsets_loaded,
1105 				   &osd->priority_xon_rx[i],
1106 				   &nsd->priority_xon_rx[i]);
1107 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1108 				   pf->stat_offsets_loaded,
1109 				   &osd->priority_xon_tx[i],
1110 				   &nsd->priority_xon_tx[i]);
1111 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1112 				   pf->stat_offsets_loaded,
1113 				   &osd->priority_xoff_tx[i],
1114 				   &nsd->priority_xoff_tx[i]);
1115 		i40e_stat_update32(hw,
1116 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1117 				   pf->stat_offsets_loaded,
1118 				   &osd->priority_xon_2_xoff[i],
1119 				   &nsd->priority_xon_2_xoff[i]);
1120 	}
1121 
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1123 			   I40E_GLPRT_PRC64L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_64, &nsd->rx_size_64);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1127 			   I40E_GLPRT_PRC127L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_127, &nsd->rx_size_127);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1131 			   I40E_GLPRT_PRC255L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_255, &nsd->rx_size_255);
1134 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1135 			   I40E_GLPRT_PRC511L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->rx_size_511, &nsd->rx_size_511);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1139 			   I40E_GLPRT_PRC1023L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1143 			   I40E_GLPRT_PRC1522L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1146 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1147 			   I40E_GLPRT_PRC9522L(hw->port),
1148 			   pf->stat_offsets_loaded,
1149 			   &osd->rx_size_big, &nsd->rx_size_big);
1150 
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1152 			   I40E_GLPRT_PTC64L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_64, &nsd->tx_size_64);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1156 			   I40E_GLPRT_PTC127L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_127, &nsd->tx_size_127);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1160 			   I40E_GLPRT_PTC255L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_255, &nsd->tx_size_255);
1163 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1164 			   I40E_GLPRT_PTC511L(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->tx_size_511, &nsd->tx_size_511);
1167 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1168 			   I40E_GLPRT_PTC1023L(hw->port),
1169 			   pf->stat_offsets_loaded,
1170 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1171 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1172 			   I40E_GLPRT_PTC1522L(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1175 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1176 			   I40E_GLPRT_PTC9522L(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->tx_size_big, &nsd->tx_size_big);
1179 
1180 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1181 			   pf->stat_offsets_loaded,
1182 			   &osd->rx_undersize, &nsd->rx_undersize);
1183 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1184 			   pf->stat_offsets_loaded,
1185 			   &osd->rx_fragments, &nsd->rx_fragments);
1186 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1187 			   pf->stat_offsets_loaded,
1188 			   &osd->rx_oversize, &nsd->rx_oversize);
1189 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1190 			   pf->stat_offsets_loaded,
1191 			   &osd->rx_jabber, &nsd->rx_jabber);
1192 
1193 	/* FDIR stats */
1194 	i40e_stat_update_and_clear32(hw,
1195 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1196 			&nsd->fd_atr_match);
1197 	i40e_stat_update_and_clear32(hw,
1198 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1199 			&nsd->fd_sb_match);
1200 	i40e_stat_update_and_clear32(hw,
1201 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1202 			&nsd->fd_atr_tunnel_match);
1203 
1204 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1205 	nsd->tx_lpi_status =
1206 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1207 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1208 	nsd->rx_lpi_status =
1209 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1210 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1211 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1212 			   pf->stat_offsets_loaded,
1213 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1214 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1215 			   pf->stat_offsets_loaded,
1216 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1217 
1218 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1219 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1220 		nsd->fd_sb_status = true;
1221 	else
1222 		nsd->fd_sb_status = false;
1223 
1224 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1225 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1226 		nsd->fd_atr_status = true;
1227 	else
1228 		nsd->fd_atr_status = false;
1229 
1230 	pf->stat_offsets_loaded = true;
1231 }
1232 
1233 /**
1234  * i40e_update_stats - Update the various statistics counters.
1235  * @vsi: the VSI to be updated
1236  *
1237  * Update the various stats for this VSI and its related entities.
1238  **/
1239 void i40e_update_stats(struct i40e_vsi *vsi)
1240 {
1241 	struct i40e_pf *pf = vsi->back;
1242 
1243 	if (vsi == pf->vsi[pf->lan_vsi])
1244 		i40e_update_pf_stats(pf);
1245 
1246 	i40e_update_vsi_stats(vsi);
1247 }
1248 
1249 /**
1250  * i40e_count_filters - counts VSI mac filters
1251  * @vsi: the VSI to be searched
1252  *
1253  * Returns count of mac filters
1254  **/
1255 int i40e_count_filters(struct i40e_vsi *vsi)
1256 {
1257 	struct i40e_mac_filter *f;
1258 	struct hlist_node *h;
1259 	int bkt;
1260 	int cnt = 0;
1261 
1262 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1263 		++cnt;
1264 
1265 	return cnt;
1266 }
1267 
1268 /**
1269  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1270  * @vsi: the VSI to be searched
1271  * @macaddr: the MAC address
1272  * @vlan: the vlan
1273  *
1274  * Returns ptr to the filter object or NULL
1275  **/
1276 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1277 						const u8 *macaddr, s16 vlan)
1278 {
1279 	struct i40e_mac_filter *f;
1280 	u64 key;
1281 
1282 	if (!vsi || !macaddr)
1283 		return NULL;
1284 
1285 	key = i40e_addr_to_hkey(macaddr);
1286 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1287 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1288 		    (vlan == f->vlan))
1289 			return f;
1290 	}
1291 	return NULL;
1292 }
1293 
1294 /**
1295  * i40e_find_mac - Find a mac addr in the macvlan filters list
1296  * @vsi: the VSI to be searched
1297  * @macaddr: the MAC address we are searching for
1298  *
1299  * Returns the first filter with the provided MAC address or NULL if
1300  * MAC address was not found
1301  **/
1302 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1303 {
1304 	struct i40e_mac_filter *f;
1305 	u64 key;
1306 
1307 	if (!vsi || !macaddr)
1308 		return NULL;
1309 
1310 	key = i40e_addr_to_hkey(macaddr);
1311 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1312 		if ((ether_addr_equal(macaddr, f->macaddr)))
1313 			return f;
1314 	}
1315 	return NULL;
1316 }
1317 
1318 /**
1319  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1320  * @vsi: the VSI to be searched
1321  *
1322  * Returns true if VSI is in vlan mode or false otherwise
1323  **/
1324 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1325 {
1326 	/* If we have a PVID, always operate in VLAN mode */
1327 	if (vsi->info.pvid)
1328 		return true;
1329 
1330 	/* We need to operate in VLAN mode whenever we have any filters with
1331 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1332 	 * time, incurring search cost repeatedly. However, we can notice two
1333 	 * things:
1334 	 *
1335 	 * 1) the only place where we can gain a VLAN filter is in
1336 	 *    i40e_add_filter.
1337 	 *
1338 	 * 2) the only place where filters are actually removed is in
1339 	 *    i40e_sync_filters_subtask.
1340 	 *
1341 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1342 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1343 	 * we have to perform the full search after deleting filters in
1344 	 * i40e_sync_filters_subtask, but we already have to search
1345 	 * filters here and can perform the check at the same time. This
1346 	 * results in avoiding embedding a loop for VLAN mode inside another
1347 	 * loop over all the filters, and should maintain correctness as noted
1348 	 * above.
1349 	 */
1350 	return vsi->has_vlan_filter;
1351 }
1352 
1353 /**
1354  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1355  * @vsi: the VSI to configure
1356  * @tmp_add_list: list of filters ready to be added
1357  * @tmp_del_list: list of filters ready to be deleted
1358  * @vlan_filters: the number of active VLAN filters
1359  *
1360  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1361  * behave as expected. If we have any active VLAN filters remaining or about
1362  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1363  * so that they only match against untagged traffic. If we no longer have any
1364  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1365  * so that they match against both tagged and untagged traffic. In this way,
1366  * we ensure that we correctly receive the desired traffic. This ensures that
1367  * when we have an active VLAN we will receive only untagged traffic and
1368  * traffic matching active VLANs. If we have no active VLANs then we will
1369  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1370  *
1371  * Finally, in a similar fashion, this function also corrects filters when
1372  * there is an active PVID assigned to this VSI.
1373  *
1374  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1375  *
1376  * This function is only expected to be called from within
1377  * i40e_sync_vsi_filters.
1378  *
1379  * NOTE: This function expects to be called while under the
1380  * mac_filter_hash_lock
1381  */
1382 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1383 					 struct hlist_head *tmp_add_list,
1384 					 struct hlist_head *tmp_del_list,
1385 					 int vlan_filters)
1386 {
1387 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1388 	struct i40e_mac_filter *f, *add_head;
1389 	struct i40e_new_mac_filter *new;
1390 	struct hlist_node *h;
1391 	int bkt, new_vlan;
1392 
1393 	/* To determine if a particular filter needs to be replaced we
1394 	 * have the three following conditions:
1395 	 *
1396 	 * a) if we have a PVID assigned, then all filters which are
1397 	 *    not marked as VLAN=PVID must be replaced with filters that
1398 	 *    are.
1399 	 * b) otherwise, if we have any active VLANS, all filters
1400 	 *    which are marked as VLAN=-1 must be replaced with
1401 	 *    filters marked as VLAN=0
1402 	 * c) finally, if we do not have any active VLANS, all filters
1403 	 *    which are marked as VLAN=0 must be replaced with filters
1404 	 *    marked as VLAN=-1
1405 	 */
1406 
1407 	/* Update the filters about to be added in place */
1408 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1409 		if (pvid && new->f->vlan != pvid)
1410 			new->f->vlan = pvid;
1411 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1412 			new->f->vlan = 0;
1413 		else if (!vlan_filters && new->f->vlan == 0)
1414 			new->f->vlan = I40E_VLAN_ANY;
1415 	}
1416 
1417 	/* Update the remaining active filters */
1418 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1419 		/* Combine the checks for whether a filter needs to be changed
1420 		 * and then determine the new VLAN inside the if block, in
1421 		 * order to avoid duplicating code for adding the new filter
1422 		 * then deleting the old filter.
1423 		 */
1424 		if ((pvid && f->vlan != pvid) ||
1425 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1426 		    (!vlan_filters && f->vlan == 0)) {
1427 			/* Determine the new vlan we will be adding */
1428 			if (pvid)
1429 				new_vlan = pvid;
1430 			else if (vlan_filters)
1431 				new_vlan = 0;
1432 			else
1433 				new_vlan = I40E_VLAN_ANY;
1434 
1435 			/* Create the new filter */
1436 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1437 			if (!add_head)
1438 				return -ENOMEM;
1439 
1440 			/* Create a temporary i40e_new_mac_filter */
1441 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1442 			if (!new)
1443 				return -ENOMEM;
1444 
1445 			new->f = add_head;
1446 			new->state = add_head->state;
1447 
1448 			/* Add the new filter to the tmp list */
1449 			hlist_add_head(&new->hlist, tmp_add_list);
1450 
1451 			/* Put the original filter into the delete list */
1452 			f->state = I40E_FILTER_REMOVE;
1453 			hash_del(&f->hlist);
1454 			hlist_add_head(&f->hlist, tmp_del_list);
1455 		}
1456 	}
1457 
1458 	vsi->has_vlan_filter = !!vlan_filters;
1459 
1460 	return 0;
1461 }
1462 
1463 /**
1464  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1465  * @vsi: the vsi to configure
1466  * @new_mac: new mac filter to be added
1467  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1468  * @vlan_filters: the number of active VLAN filters
1469  * @trusted: flag if the VF is trusted
1470  *
1471  * Get new VLAN id based on current VLAN filters, trust, PVID
1472  * and vf-vlan-prune-disable flag.
1473  *
1474  * Returns the value of the new vlan filter or
1475  * the old value if no new filter is needed.
1476  */
1477 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1478 				struct i40e_new_mac_filter *new_mac,
1479 				struct i40e_mac_filter *f,
1480 				int vlan_filters,
1481 				bool trusted)
1482 {
1483 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1484 	struct i40e_pf *pf = vsi->back;
1485 	bool is_any;
1486 
1487 	if (new_mac)
1488 		f = new_mac->f;
1489 
1490 	if (pvid && f->vlan != pvid)
1491 		return pvid;
1492 
1493 	is_any = (trusted ||
1494 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1495 
1496 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1497 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1498 	    (is_any && !vlan_filters && f->vlan == 0)) {
1499 		if (is_any)
1500 			return I40E_VLAN_ANY;
1501 		else
1502 			return 0;
1503 	}
1504 
1505 	return f->vlan;
1506 }
1507 
1508 /**
1509  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1510  * @vsi: the vsi to configure
1511  * @tmp_add_list: list of filters ready to be added
1512  * @tmp_del_list: list of filters ready to be deleted
1513  * @vlan_filters: the number of active VLAN filters
1514  * @trusted: flag if the VF is trusted
1515  *
1516  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1517  * and vf-vlan-prune-disable flag.
1518  *
1519  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1520  *
1521  * This function is only expected to be called from within
1522  * i40e_sync_vsi_filters.
1523  *
1524  * NOTE: This function expects to be called while under the
1525  * mac_filter_hash_lock
1526  */
1527 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1528 					    struct hlist_head *tmp_add_list,
1529 					    struct hlist_head *tmp_del_list,
1530 					    int vlan_filters,
1531 					    bool trusted)
1532 {
1533 	struct i40e_mac_filter *f, *add_head;
1534 	struct i40e_new_mac_filter *new_mac;
1535 	struct hlist_node *h;
1536 	int bkt, new_vlan;
1537 
1538 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1539 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1540 							vlan_filters, trusted);
1541 	}
1542 
1543 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1544 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1545 						trusted);
1546 		if (new_vlan != f->vlan) {
1547 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1548 			if (!add_head)
1549 				return -ENOMEM;
1550 			/* Create a temporary i40e_new_mac_filter */
1551 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1552 			if (!new_mac)
1553 				return -ENOMEM;
1554 			new_mac->f = add_head;
1555 			new_mac->state = add_head->state;
1556 
1557 			/* Add the new filter to the tmp list */
1558 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1559 
1560 			/* Put the original filter into the delete list */
1561 			f->state = I40E_FILTER_REMOVE;
1562 			hash_del(&f->hlist);
1563 			hlist_add_head(&f->hlist, tmp_del_list);
1564 		}
1565 	}
1566 
1567 	vsi->has_vlan_filter = !!vlan_filters;
1568 	return 0;
1569 }
1570 
1571 /**
1572  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1573  * @vsi: the PF Main VSI - inappropriate for any other VSI
1574  * @macaddr: the MAC address
1575  *
1576  * Remove whatever filter the firmware set up so the driver can manage
1577  * its own filtering intelligently.
1578  **/
1579 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1580 {
1581 	struct i40e_aqc_remove_macvlan_element_data element;
1582 	struct i40e_pf *pf = vsi->back;
1583 
1584 	/* Only appropriate for the PF main VSI */
1585 	if (vsi->type != I40E_VSI_MAIN)
1586 		return;
1587 
1588 	memset(&element, 0, sizeof(element));
1589 	ether_addr_copy(element.mac_addr, macaddr);
1590 	element.vlan_tag = 0;
1591 	/* Ignore error returns, some firmware does it this way... */
1592 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1593 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1594 
1595 	memset(&element, 0, sizeof(element));
1596 	ether_addr_copy(element.mac_addr, macaddr);
1597 	element.vlan_tag = 0;
1598 	/* ...and some firmware does it this way. */
1599 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1600 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1601 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1602 }
1603 
1604 /**
1605  * i40e_add_filter - Add a mac/vlan filter to the VSI
1606  * @vsi: the VSI to be searched
1607  * @macaddr: the MAC address
1608  * @vlan: the vlan
1609  *
1610  * Returns ptr to the filter object or NULL when no memory available.
1611  *
1612  * NOTE: This function is expected to be called with mac_filter_hash_lock
1613  * being held.
1614  **/
1615 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1616 					const u8 *macaddr, s16 vlan)
1617 {
1618 	struct i40e_mac_filter *f;
1619 	u64 key;
1620 
1621 	if (!vsi || !macaddr)
1622 		return NULL;
1623 
1624 	f = i40e_find_filter(vsi, macaddr, vlan);
1625 	if (!f) {
1626 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1627 		if (!f)
1628 			return NULL;
1629 
1630 		/* Update the boolean indicating if we need to function in
1631 		 * VLAN mode.
1632 		 */
1633 		if (vlan >= 0)
1634 			vsi->has_vlan_filter = true;
1635 
1636 		ether_addr_copy(f->macaddr, macaddr);
1637 		f->vlan = vlan;
1638 		f->state = I40E_FILTER_NEW;
1639 		INIT_HLIST_NODE(&f->hlist);
1640 
1641 		key = i40e_addr_to_hkey(macaddr);
1642 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1643 
1644 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1645 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1646 	}
1647 
1648 	/* If we're asked to add a filter that has been marked for removal, it
1649 	 * is safe to simply restore it to active state. __i40e_del_filter
1650 	 * will have simply deleted any filters which were previously marked
1651 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1652 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1653 	 * task, just restore this filter to the ACTIVE state so that the
1654 	 * sync task leaves it in place
1655 	 */
1656 	if (f->state == I40E_FILTER_REMOVE)
1657 		f->state = I40E_FILTER_ACTIVE;
1658 
1659 	return f;
1660 }
1661 
1662 /**
1663  * __i40e_del_filter - Remove a specific filter from the VSI
1664  * @vsi: VSI to remove from
1665  * @f: the filter to remove from the list
1666  *
1667  * This function should be called instead of i40e_del_filter only if you know
1668  * the exact filter you will remove already, such as via i40e_find_filter or
1669  * i40e_find_mac.
1670  *
1671  * NOTE: This function is expected to be called with mac_filter_hash_lock
1672  * being held.
1673  * ANOTHER NOTE: This function MUST be called from within the context of
1674  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1675  * instead of list_for_each_entry().
1676  **/
1677 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1678 {
1679 	if (!f)
1680 		return;
1681 
1682 	/* If the filter was never added to firmware then we can just delete it
1683 	 * directly and we don't want to set the status to remove or else an
1684 	 * admin queue command will unnecessarily fire.
1685 	 */
1686 	if ((f->state == I40E_FILTER_FAILED) ||
1687 	    (f->state == I40E_FILTER_NEW)) {
1688 		hash_del(&f->hlist);
1689 		kfree(f);
1690 	} else {
1691 		f->state = I40E_FILTER_REMOVE;
1692 	}
1693 
1694 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1695 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1696 }
1697 
1698 /**
1699  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1700  * @vsi: the VSI to be searched
1701  * @macaddr: the MAC address
1702  * @vlan: the VLAN
1703  *
1704  * NOTE: This function is expected to be called with mac_filter_hash_lock
1705  * being held.
1706  * ANOTHER NOTE: This function MUST be called from within the context of
1707  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1708  * instead of list_for_each_entry().
1709  **/
1710 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1711 {
1712 	struct i40e_mac_filter *f;
1713 
1714 	if (!vsi || !macaddr)
1715 		return;
1716 
1717 	f = i40e_find_filter(vsi, macaddr, vlan);
1718 	__i40e_del_filter(vsi, f);
1719 }
1720 
1721 /**
1722  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1723  * @vsi: the VSI to be searched
1724  * @macaddr: the mac address to be filtered
1725  *
1726  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1727  * go through all the macvlan filters and add a macvlan filter for each
1728  * unique vlan that already exists. If a PVID has been assigned, instead only
1729  * add the macaddr to that VLAN.
1730  *
1731  * Returns last filter added on success, else NULL
1732  **/
1733 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1734 					    const u8 *macaddr)
1735 {
1736 	struct i40e_mac_filter *f, *add = NULL;
1737 	struct hlist_node *h;
1738 	int bkt;
1739 
1740 	if (vsi->info.pvid)
1741 		return i40e_add_filter(vsi, macaddr,
1742 				       le16_to_cpu(vsi->info.pvid));
1743 
1744 	if (!i40e_is_vsi_in_vlan(vsi))
1745 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1746 
1747 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1748 		if (f->state == I40E_FILTER_REMOVE)
1749 			continue;
1750 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1751 		if (!add)
1752 			return NULL;
1753 	}
1754 
1755 	return add;
1756 }
1757 
1758 /**
1759  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1760  * @vsi: the VSI to be searched
1761  * @macaddr: the mac address to be removed
1762  *
1763  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1764  * associated with.
1765  *
1766  * Returns 0 for success, or error
1767  **/
1768 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1769 {
1770 	struct i40e_mac_filter *f;
1771 	struct hlist_node *h;
1772 	bool found = false;
1773 	int bkt;
1774 
1775 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1776 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1777 		if (ether_addr_equal(macaddr, f->macaddr)) {
1778 			__i40e_del_filter(vsi, f);
1779 			found = true;
1780 		}
1781 	}
1782 
1783 	if (found)
1784 		return 0;
1785 	else
1786 		return -ENOENT;
1787 }
1788 
1789 /**
1790  * i40e_set_mac - NDO callback to set mac address
1791  * @netdev: network interface device structure
1792  * @p: pointer to an address structure
1793  *
1794  * Returns 0 on success, negative on failure
1795  **/
1796 static int i40e_set_mac(struct net_device *netdev, void *p)
1797 {
1798 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1799 	struct i40e_vsi *vsi = np->vsi;
1800 	struct i40e_pf *pf = vsi->back;
1801 	struct i40e_hw *hw = &pf->hw;
1802 	struct sockaddr *addr = p;
1803 
1804 	if (!is_valid_ether_addr(addr->sa_data))
1805 		return -EADDRNOTAVAIL;
1806 
1807 	if (test_bit(__I40E_DOWN, pf->state) ||
1808 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1809 		return -EADDRNOTAVAIL;
1810 
1811 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1812 		netdev_info(netdev, "returning to hw mac address %pM\n",
1813 			    hw->mac.addr);
1814 	else
1815 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1816 
1817 	/* Copy the address first, so that we avoid a possible race with
1818 	 * .set_rx_mode().
1819 	 * - Remove old address from MAC filter
1820 	 * - Copy new address
1821 	 * - Add new address to MAC filter
1822 	 */
1823 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1824 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1825 	eth_hw_addr_set(netdev, addr->sa_data);
1826 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1827 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1828 
1829 	if (vsi->type == I40E_VSI_MAIN) {
1830 		int ret;
1831 
1832 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1833 						addr->sa_data, NULL);
1834 		if (ret)
1835 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1836 				    ERR_PTR(ret),
1837 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1838 	}
1839 
1840 	/* schedule our worker thread which will take care of
1841 	 * applying the new filter changes
1842 	 */
1843 	i40e_service_event_schedule(pf);
1844 	return 0;
1845 }
1846 
1847 /**
1848  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1849  * @vsi: vsi structure
1850  * @seed: RSS hash seed
1851  * @lut: pointer to lookup table of lut_size
1852  * @lut_size: size of the lookup table
1853  **/
1854 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1855 			      u8 *lut, u16 lut_size)
1856 {
1857 	struct i40e_pf *pf = vsi->back;
1858 	struct i40e_hw *hw = &pf->hw;
1859 	int ret = 0;
1860 
1861 	if (seed) {
1862 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1863 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1864 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1865 		if (ret) {
1866 			dev_info(&pf->pdev->dev,
1867 				 "Cannot set RSS key, err %pe aq_err %s\n",
1868 				 ERR_PTR(ret),
1869 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1870 			return ret;
1871 		}
1872 	}
1873 	if (lut) {
1874 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1875 
1876 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1877 		if (ret) {
1878 			dev_info(&pf->pdev->dev,
1879 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1880 				 ERR_PTR(ret),
1881 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1882 			return ret;
1883 		}
1884 	}
1885 	return ret;
1886 }
1887 
1888 /**
1889  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1890  * @vsi: VSI structure
1891  **/
1892 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1893 {
1894 	struct i40e_pf *pf = vsi->back;
1895 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1896 	u8 *lut;
1897 	int ret;
1898 
1899 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1900 		return 0;
1901 	if (!vsi->rss_size)
1902 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1903 				      vsi->num_queue_pairs);
1904 	if (!vsi->rss_size)
1905 		return -EINVAL;
1906 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1907 	if (!lut)
1908 		return -ENOMEM;
1909 
1910 	/* Use the user configured hash keys and lookup table if there is one,
1911 	 * otherwise use default
1912 	 */
1913 	if (vsi->rss_lut_user)
1914 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1915 	else
1916 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1917 	if (vsi->rss_hkey_user)
1918 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1919 	else
1920 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1921 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1922 	kfree(lut);
1923 	return ret;
1924 }
1925 
1926 /**
1927  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1928  * @vsi: the VSI being configured,
1929  * @ctxt: VSI context structure
1930  * @enabled_tc: number of traffic classes to enable
1931  *
1932  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1933  **/
1934 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1935 					   struct i40e_vsi_context *ctxt,
1936 					   u8 enabled_tc)
1937 {
1938 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1939 	int i, override_q, pow, num_qps, ret;
1940 	u8 netdev_tc = 0, offset = 0;
1941 
1942 	if (vsi->type != I40E_VSI_MAIN)
1943 		return -EINVAL;
1944 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1945 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1946 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1947 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1948 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1949 
1950 	/* find the next higher power-of-2 of num queue pairs */
1951 	pow = ilog2(num_qps);
1952 	if (!is_power_of_2(num_qps))
1953 		pow++;
1954 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1955 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1956 
1957 	/* Setup queue offset/count for all TCs for given VSI */
1958 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1959 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1960 		/* See if the given TC is enabled for the given VSI */
1961 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1962 			offset = vsi->mqprio_qopt.qopt.offset[i];
1963 			qcount = vsi->mqprio_qopt.qopt.count[i];
1964 			if (qcount > max_qcount)
1965 				max_qcount = qcount;
1966 			vsi->tc_config.tc_info[i].qoffset = offset;
1967 			vsi->tc_config.tc_info[i].qcount = qcount;
1968 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1969 		} else {
1970 			/* TC is not enabled so set the offset to
1971 			 * default queue and allocate one queue
1972 			 * for the given TC.
1973 			 */
1974 			vsi->tc_config.tc_info[i].qoffset = 0;
1975 			vsi->tc_config.tc_info[i].qcount = 1;
1976 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1977 		}
1978 	}
1979 
1980 	/* Set actual Tx/Rx queue pairs */
1981 	vsi->num_queue_pairs = offset + qcount;
1982 
1983 	/* Setup queue TC[0].qmap for given VSI context */
1984 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1985 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1986 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1987 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1988 
1989 	/* Reconfigure RSS for main VSI with max queue count */
1990 	vsi->rss_size = max_qcount;
1991 	ret = i40e_vsi_config_rss(vsi);
1992 	if (ret) {
1993 		dev_info(&vsi->back->pdev->dev,
1994 			 "Failed to reconfig rss for num_queues (%u)\n",
1995 			 max_qcount);
1996 		return ret;
1997 	}
1998 	vsi->reconfig_rss = true;
1999 	dev_dbg(&vsi->back->pdev->dev,
2000 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
2001 
2002 	/* Find queue count available for channel VSIs and starting offset
2003 	 * for channel VSIs
2004 	 */
2005 	override_q = vsi->mqprio_qopt.qopt.count[0];
2006 	if (override_q && override_q < vsi->num_queue_pairs) {
2007 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2008 		vsi->next_base_queue = override_q;
2009 	}
2010 	return 0;
2011 }
2012 
2013 /**
2014  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2015  * @vsi: the VSI being setup
2016  * @ctxt: VSI context structure
2017  * @enabled_tc: Enabled TCs bitmap
2018  * @is_add: True if called before Add VSI
2019  *
2020  * Setup VSI queue mapping for enabled traffic classes.
2021  **/
2022 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2023 				     struct i40e_vsi_context *ctxt,
2024 				     u8 enabled_tc,
2025 				     bool is_add)
2026 {
2027 	struct i40e_pf *pf = vsi->back;
2028 	u16 num_tc_qps = 0;
2029 	u16 sections = 0;
2030 	u8 netdev_tc = 0;
2031 	u16 numtc = 1;
2032 	u16 qcount;
2033 	u8 offset;
2034 	u16 qmap;
2035 	int i;
2036 
2037 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2038 	offset = 0;
2039 	/* zero out queue mapping, it will get updated on the end of the function */
2040 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2041 
2042 	if (vsi->type == I40E_VSI_MAIN) {
2043 		/* This code helps add more queue to the VSI if we have
2044 		 * more cores than RSS can support, the higher cores will
2045 		 * be served by ATR or other filters. Furthermore, the
2046 		 * non-zero req_queue_pairs says that user requested a new
2047 		 * queue count via ethtool's set_channels, so use this
2048 		 * value for queues distribution across traffic classes
2049 		 * We need at least one queue pair for the interface
2050 		 * to be usable as we see in else statement.
2051 		 */
2052 		if (vsi->req_queue_pairs > 0)
2053 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2054 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2055 			vsi->num_queue_pairs = pf->num_lan_msix;
2056 		else
2057 			vsi->num_queue_pairs = 1;
2058 	}
2059 
2060 	/* Number of queues per enabled TC */
2061 	if (vsi->type == I40E_VSI_MAIN ||
2062 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2063 		num_tc_qps = vsi->num_queue_pairs;
2064 	else
2065 		num_tc_qps = vsi->alloc_queue_pairs;
2066 
2067 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2068 		/* Find numtc from enabled TC bitmap */
2069 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2070 			if (enabled_tc & BIT(i)) /* TC is enabled */
2071 				numtc++;
2072 		}
2073 		if (!numtc) {
2074 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2075 			numtc = 1;
2076 		}
2077 		num_tc_qps = num_tc_qps / numtc;
2078 		num_tc_qps = min_t(int, num_tc_qps,
2079 				   i40e_pf_get_max_q_per_tc(pf));
2080 	}
2081 
2082 	vsi->tc_config.numtc = numtc;
2083 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2084 
2085 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2086 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2087 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2088 
2089 	/* Setup queue offset/count for all TCs for given VSI */
2090 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2091 		/* See if the given TC is enabled for the given VSI */
2092 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2093 			/* TC is enabled */
2094 			int pow, num_qps;
2095 
2096 			switch (vsi->type) {
2097 			case I40E_VSI_MAIN:
2098 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2099 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2100 				    vsi->tc_config.enabled_tc != 1) {
2101 					qcount = min_t(int, pf->alloc_rss_size,
2102 						       num_tc_qps);
2103 					break;
2104 				}
2105 				fallthrough;
2106 			case I40E_VSI_FDIR:
2107 			case I40E_VSI_SRIOV:
2108 			case I40E_VSI_VMDQ2:
2109 			default:
2110 				qcount = num_tc_qps;
2111 				WARN_ON(i != 0);
2112 				break;
2113 			}
2114 			vsi->tc_config.tc_info[i].qoffset = offset;
2115 			vsi->tc_config.tc_info[i].qcount = qcount;
2116 
2117 			/* find the next higher power-of-2 of num queue pairs */
2118 			num_qps = qcount;
2119 			pow = 0;
2120 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2121 				pow++;
2122 				num_qps >>= 1;
2123 			}
2124 
2125 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2126 			qmap =
2127 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2128 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2129 
2130 			offset += qcount;
2131 		} else {
2132 			/* TC is not enabled so set the offset to
2133 			 * default queue and allocate one queue
2134 			 * for the given TC.
2135 			 */
2136 			vsi->tc_config.tc_info[i].qoffset = 0;
2137 			vsi->tc_config.tc_info[i].qcount = 1;
2138 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2139 
2140 			qmap = 0;
2141 		}
2142 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2143 	}
2144 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2145 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2146 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2147 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2148 		vsi->num_queue_pairs = offset;
2149 
2150 	/* Scheduler section valid can only be set for ADD VSI */
2151 	if (is_add) {
2152 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2153 
2154 		ctxt->info.up_enable_bits = enabled_tc;
2155 	}
2156 	if (vsi->type == I40E_VSI_SRIOV) {
2157 		ctxt->info.mapping_flags |=
2158 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2159 		for (i = 0; i < vsi->num_queue_pairs; i++)
2160 			ctxt->info.queue_mapping[i] =
2161 					       cpu_to_le16(vsi->base_queue + i);
2162 	} else {
2163 		ctxt->info.mapping_flags |=
2164 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2165 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2166 	}
2167 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2168 }
2169 
2170 /**
2171  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2172  * @netdev: the netdevice
2173  * @addr: address to add
2174  *
2175  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2176  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2177  */
2178 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2179 {
2180 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2181 	struct i40e_vsi *vsi = np->vsi;
2182 
2183 	if (i40e_add_mac_filter(vsi, addr))
2184 		return 0;
2185 	else
2186 		return -ENOMEM;
2187 }
2188 
2189 /**
2190  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2191  * @netdev: the netdevice
2192  * @addr: address to add
2193  *
2194  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2195  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2196  */
2197 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2198 {
2199 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2200 	struct i40e_vsi *vsi = np->vsi;
2201 
2202 	/* Under some circumstances, we might receive a request to delete
2203 	 * our own device address from our uc list. Because we store the
2204 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2205 	 * such requests and not delete our device address from this list.
2206 	 */
2207 	if (ether_addr_equal(addr, netdev->dev_addr))
2208 		return 0;
2209 
2210 	i40e_del_mac_filter(vsi, addr);
2211 
2212 	return 0;
2213 }
2214 
2215 /**
2216  * i40e_set_rx_mode - NDO callback to set the netdev filters
2217  * @netdev: network interface device structure
2218  **/
2219 static void i40e_set_rx_mode(struct net_device *netdev)
2220 {
2221 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2222 	struct i40e_vsi *vsi = np->vsi;
2223 
2224 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2225 
2226 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2227 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2228 
2229 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2230 
2231 	/* check for other flag changes */
2232 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2233 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2234 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2235 	}
2236 }
2237 
2238 /**
2239  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2240  * @vsi: Pointer to VSI struct
2241  * @from: Pointer to list which contains MAC filter entries - changes to
2242  *        those entries needs to be undone.
2243  *
2244  * MAC filter entries from this list were slated for deletion.
2245  **/
2246 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2247 					 struct hlist_head *from)
2248 {
2249 	struct i40e_mac_filter *f;
2250 	struct hlist_node *h;
2251 
2252 	hlist_for_each_entry_safe(f, h, from, hlist) {
2253 		u64 key = i40e_addr_to_hkey(f->macaddr);
2254 
2255 		/* Move the element back into MAC filter list*/
2256 		hlist_del(&f->hlist);
2257 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2258 	}
2259 }
2260 
2261 /**
2262  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2263  * @vsi: Pointer to vsi struct
2264  * @from: Pointer to list which contains MAC filter entries - changes to
2265  *        those entries needs to be undone.
2266  *
2267  * MAC filter entries from this list were slated for addition.
2268  **/
2269 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2270 					 struct hlist_head *from)
2271 {
2272 	struct i40e_new_mac_filter *new;
2273 	struct hlist_node *h;
2274 
2275 	hlist_for_each_entry_safe(new, h, from, hlist) {
2276 		/* We can simply free the wrapper structure */
2277 		hlist_del(&new->hlist);
2278 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2279 		kfree(new);
2280 	}
2281 }
2282 
2283 /**
2284  * i40e_next_filter - Get the next non-broadcast filter from a list
2285  * @next: pointer to filter in list
2286  *
2287  * Returns the next non-broadcast filter in the list. Required so that we
2288  * ignore broadcast filters within the list, since these are not handled via
2289  * the normal firmware update path.
2290  */
2291 static
2292 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2293 {
2294 	hlist_for_each_entry_continue(next, hlist) {
2295 		if (!is_broadcast_ether_addr(next->f->macaddr))
2296 			return next;
2297 	}
2298 
2299 	return NULL;
2300 }
2301 
2302 /**
2303  * i40e_update_filter_state - Update filter state based on return data
2304  * from firmware
2305  * @count: Number of filters added
2306  * @add_list: return data from fw
2307  * @add_head: pointer to first filter in current batch
2308  *
2309  * MAC filter entries from list were slated to be added to device. Returns
2310  * number of successful filters. Note that 0 does NOT mean success!
2311  **/
2312 static int
2313 i40e_update_filter_state(int count,
2314 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2315 			 struct i40e_new_mac_filter *add_head)
2316 {
2317 	int retval = 0;
2318 	int i;
2319 
2320 	for (i = 0; i < count; i++) {
2321 		/* Always check status of each filter. We don't need to check
2322 		 * the firmware return status because we pre-set the filter
2323 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2324 		 * request to the adminq. Thus, if it no longer matches then
2325 		 * we know the filter is active.
2326 		 */
2327 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2328 			add_head->state = I40E_FILTER_FAILED;
2329 		} else {
2330 			add_head->state = I40E_FILTER_ACTIVE;
2331 			retval++;
2332 		}
2333 
2334 		add_head = i40e_next_filter(add_head);
2335 		if (!add_head)
2336 			break;
2337 	}
2338 
2339 	return retval;
2340 }
2341 
2342 /**
2343  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2344  * @vsi: ptr to the VSI
2345  * @vsi_name: name to display in messages
2346  * @list: the list of filters to send to firmware
2347  * @num_del: the number of filters to delete
2348  * @retval: Set to -EIO on failure to delete
2349  *
2350  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2351  * *retval instead of a return value so that success does not force ret_val to
2352  * be set to 0. This ensures that a sequence of calls to this function
2353  * preserve the previous value of *retval on successful delete.
2354  */
2355 static
2356 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2357 			  struct i40e_aqc_remove_macvlan_element_data *list,
2358 			  int num_del, int *retval)
2359 {
2360 	struct i40e_hw *hw = &vsi->back->hw;
2361 	enum i40e_admin_queue_err aq_status;
2362 	int aq_ret;
2363 
2364 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2365 					   &aq_status);
2366 
2367 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2368 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2369 		*retval = -EIO;
2370 		dev_info(&vsi->back->pdev->dev,
2371 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2372 			 vsi_name, ERR_PTR(aq_ret),
2373 			 i40e_aq_str(hw, aq_status));
2374 	}
2375 }
2376 
2377 /**
2378  * i40e_aqc_add_filters - Request firmware to add a set of filters
2379  * @vsi: ptr to the VSI
2380  * @vsi_name: name to display in messages
2381  * @list: the list of filters to send to firmware
2382  * @add_head: Position in the add hlist
2383  * @num_add: the number of filters to add
2384  *
2385  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2386  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2387  * space for more filters.
2388  */
2389 static
2390 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2391 			  struct i40e_aqc_add_macvlan_element_data *list,
2392 			  struct i40e_new_mac_filter *add_head,
2393 			  int num_add)
2394 {
2395 	struct i40e_hw *hw = &vsi->back->hw;
2396 	enum i40e_admin_queue_err aq_status;
2397 	int fcnt;
2398 
2399 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2400 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2401 
2402 	if (fcnt != num_add) {
2403 		if (vsi->type == I40E_VSI_MAIN) {
2404 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2405 			dev_warn(&vsi->back->pdev->dev,
2406 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2407 				 i40e_aq_str(hw, aq_status), vsi_name);
2408 		} else if (vsi->type == I40E_VSI_SRIOV ||
2409 			   vsi->type == I40E_VSI_VMDQ1 ||
2410 			   vsi->type == I40E_VSI_VMDQ2) {
2411 			dev_warn(&vsi->back->pdev->dev,
2412 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2413 				 i40e_aq_str(hw, aq_status), vsi_name,
2414 					     vsi_name);
2415 		} else {
2416 			dev_warn(&vsi->back->pdev->dev,
2417 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2418 				 i40e_aq_str(hw, aq_status), vsi_name,
2419 					     vsi->type);
2420 		}
2421 	}
2422 }
2423 
2424 /**
2425  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2426  * @vsi: pointer to the VSI
2427  * @vsi_name: the VSI name
2428  * @f: filter data
2429  *
2430  * This function sets or clears the promiscuous broadcast flags for VLAN
2431  * filters in order to properly receive broadcast frames. Assumes that only
2432  * broadcast filters are passed.
2433  *
2434  * Returns status indicating success or failure;
2435  **/
2436 static int
2437 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2438 			  struct i40e_mac_filter *f)
2439 {
2440 	bool enable = f->state == I40E_FILTER_NEW;
2441 	struct i40e_hw *hw = &vsi->back->hw;
2442 	int aq_ret;
2443 
2444 	if (f->vlan == I40E_VLAN_ANY) {
2445 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2446 						   vsi->seid,
2447 						   enable,
2448 						   NULL);
2449 	} else {
2450 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2451 							    vsi->seid,
2452 							    enable,
2453 							    f->vlan,
2454 							    NULL);
2455 	}
2456 
2457 	if (aq_ret) {
2458 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2459 		dev_warn(&vsi->back->pdev->dev,
2460 			 "Error %s, forcing overflow promiscuous on %s\n",
2461 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2462 			 vsi_name);
2463 	}
2464 
2465 	return aq_ret;
2466 }
2467 
2468 /**
2469  * i40e_set_promiscuous - set promiscuous mode
2470  * @pf: board private structure
2471  * @promisc: promisc on or off
2472  *
2473  * There are different ways of setting promiscuous mode on a PF depending on
2474  * what state/environment we're in.  This identifies and sets it appropriately.
2475  * Returns 0 on success.
2476  **/
2477 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2478 {
2479 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2480 	struct i40e_hw *hw = &pf->hw;
2481 	int aq_ret;
2482 
2483 	if (vsi->type == I40E_VSI_MAIN &&
2484 	    pf->lan_veb != I40E_NO_VEB &&
2485 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2486 		/* set defport ON for Main VSI instead of true promisc
2487 		 * this way we will get all unicast/multicast and VLAN
2488 		 * promisc behavior but will not get VF or VMDq traffic
2489 		 * replicated on the Main VSI.
2490 		 */
2491 		if (promisc)
2492 			aq_ret = i40e_aq_set_default_vsi(hw,
2493 							 vsi->seid,
2494 							 NULL);
2495 		else
2496 			aq_ret = i40e_aq_clear_default_vsi(hw,
2497 							   vsi->seid,
2498 							   NULL);
2499 		if (aq_ret) {
2500 			dev_info(&pf->pdev->dev,
2501 				 "Set default VSI failed, err %pe, aq_err %s\n",
2502 				 ERR_PTR(aq_ret),
2503 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2504 		}
2505 	} else {
2506 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2507 						  hw,
2508 						  vsi->seid,
2509 						  promisc, NULL,
2510 						  true);
2511 		if (aq_ret) {
2512 			dev_info(&pf->pdev->dev,
2513 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2514 				 ERR_PTR(aq_ret),
2515 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2516 		}
2517 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2518 						  hw,
2519 						  vsi->seid,
2520 						  promisc, NULL);
2521 		if (aq_ret) {
2522 			dev_info(&pf->pdev->dev,
2523 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2524 				 ERR_PTR(aq_ret),
2525 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2526 		}
2527 	}
2528 
2529 	if (!aq_ret)
2530 		pf->cur_promisc = promisc;
2531 
2532 	return aq_ret;
2533 }
2534 
2535 /**
2536  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2537  * @vsi: ptr to the VSI
2538  *
2539  * Push any outstanding VSI filter changes through the AdminQ.
2540  *
2541  * Returns 0 or error value
2542  **/
2543 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2544 {
2545 	struct hlist_head tmp_add_list, tmp_del_list;
2546 	struct i40e_mac_filter *f;
2547 	struct i40e_new_mac_filter *new, *add_head = NULL;
2548 	struct i40e_hw *hw = &vsi->back->hw;
2549 	bool old_overflow, new_overflow;
2550 	unsigned int failed_filters = 0;
2551 	unsigned int vlan_filters = 0;
2552 	char vsi_name[16] = "PF";
2553 	int filter_list_len = 0;
2554 	u32 changed_flags = 0;
2555 	struct hlist_node *h;
2556 	struct i40e_pf *pf;
2557 	int num_add = 0;
2558 	int num_del = 0;
2559 	int aq_ret = 0;
2560 	int retval = 0;
2561 	u16 cmd_flags;
2562 	int list_size;
2563 	int bkt;
2564 
2565 	/* empty array typed pointers, kcalloc later */
2566 	struct i40e_aqc_add_macvlan_element_data *add_list;
2567 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2568 
2569 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2570 		usleep_range(1000, 2000);
2571 	pf = vsi->back;
2572 
2573 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2574 
2575 	if (vsi->netdev) {
2576 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2577 		vsi->current_netdev_flags = vsi->netdev->flags;
2578 	}
2579 
2580 	INIT_HLIST_HEAD(&tmp_add_list);
2581 	INIT_HLIST_HEAD(&tmp_del_list);
2582 
2583 	if (vsi->type == I40E_VSI_SRIOV)
2584 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2585 	else if (vsi->type != I40E_VSI_MAIN)
2586 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2587 
2588 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2589 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2590 
2591 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2592 		/* Create a list of filters to delete. */
2593 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2594 			if (f->state == I40E_FILTER_REMOVE) {
2595 				/* Move the element into temporary del_list */
2596 				hash_del(&f->hlist);
2597 				hlist_add_head(&f->hlist, &tmp_del_list);
2598 
2599 				/* Avoid counting removed filters */
2600 				continue;
2601 			}
2602 			if (f->state == I40E_FILTER_NEW) {
2603 				/* Create a temporary i40e_new_mac_filter */
2604 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2605 				if (!new)
2606 					goto err_no_memory_locked;
2607 
2608 				/* Store pointer to the real filter */
2609 				new->f = f;
2610 				new->state = f->state;
2611 
2612 				/* Add it to the hash list */
2613 				hlist_add_head(&new->hlist, &tmp_add_list);
2614 			}
2615 
2616 			/* Count the number of active (current and new) VLAN
2617 			 * filters we have now. Does not count filters which
2618 			 * are marked for deletion.
2619 			 */
2620 			if (f->vlan > 0)
2621 				vlan_filters++;
2622 		}
2623 
2624 		if (vsi->type != I40E_VSI_SRIOV)
2625 			retval = i40e_correct_mac_vlan_filters
2626 				(vsi, &tmp_add_list, &tmp_del_list,
2627 				 vlan_filters);
2628 		else if (pf->vf)
2629 			retval = i40e_correct_vf_mac_vlan_filters
2630 				(vsi, &tmp_add_list, &tmp_del_list,
2631 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2632 
2633 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2634 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2635 
2636 		if (retval)
2637 			goto err_no_memory_locked;
2638 
2639 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2640 	}
2641 
2642 	/* Now process 'del_list' outside the lock */
2643 	if (!hlist_empty(&tmp_del_list)) {
2644 		filter_list_len = hw->aq.asq_buf_size /
2645 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2646 		list_size = filter_list_len *
2647 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2648 		del_list = kzalloc(list_size, GFP_ATOMIC);
2649 		if (!del_list)
2650 			goto err_no_memory;
2651 
2652 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2653 			cmd_flags = 0;
2654 
2655 			/* handle broadcast filters by updating the broadcast
2656 			 * promiscuous flag and release filter list.
2657 			 */
2658 			if (is_broadcast_ether_addr(f->macaddr)) {
2659 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2660 
2661 				hlist_del(&f->hlist);
2662 				kfree(f);
2663 				continue;
2664 			}
2665 
2666 			/* add to delete list */
2667 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2668 			if (f->vlan == I40E_VLAN_ANY) {
2669 				del_list[num_del].vlan_tag = 0;
2670 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2671 			} else {
2672 				del_list[num_del].vlan_tag =
2673 					cpu_to_le16((u16)(f->vlan));
2674 			}
2675 
2676 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2677 			del_list[num_del].flags = cmd_flags;
2678 			num_del++;
2679 
2680 			/* flush a full buffer */
2681 			if (num_del == filter_list_len) {
2682 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2683 						     num_del, &retval);
2684 				memset(del_list, 0, list_size);
2685 				num_del = 0;
2686 			}
2687 			/* Release memory for MAC filter entries which were
2688 			 * synced up with HW.
2689 			 */
2690 			hlist_del(&f->hlist);
2691 			kfree(f);
2692 		}
2693 
2694 		if (num_del) {
2695 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2696 					     num_del, &retval);
2697 		}
2698 
2699 		kfree(del_list);
2700 		del_list = NULL;
2701 	}
2702 
2703 	if (!hlist_empty(&tmp_add_list)) {
2704 		/* Do all the adds now. */
2705 		filter_list_len = hw->aq.asq_buf_size /
2706 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2707 		list_size = filter_list_len *
2708 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2709 		add_list = kzalloc(list_size, GFP_ATOMIC);
2710 		if (!add_list)
2711 			goto err_no_memory;
2712 
2713 		num_add = 0;
2714 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2715 			/* handle broadcast filters by updating the broadcast
2716 			 * promiscuous flag instead of adding a MAC filter.
2717 			 */
2718 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2719 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2720 							      new->f))
2721 					new->state = I40E_FILTER_FAILED;
2722 				else
2723 					new->state = I40E_FILTER_ACTIVE;
2724 				continue;
2725 			}
2726 
2727 			/* add to add array */
2728 			if (num_add == 0)
2729 				add_head = new;
2730 			cmd_flags = 0;
2731 			ether_addr_copy(add_list[num_add].mac_addr,
2732 					new->f->macaddr);
2733 			if (new->f->vlan == I40E_VLAN_ANY) {
2734 				add_list[num_add].vlan_tag = 0;
2735 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2736 			} else {
2737 				add_list[num_add].vlan_tag =
2738 					cpu_to_le16((u16)(new->f->vlan));
2739 			}
2740 			add_list[num_add].queue_number = 0;
2741 			/* set invalid match method for later detection */
2742 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2743 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2744 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2745 			num_add++;
2746 
2747 			/* flush a full buffer */
2748 			if (num_add == filter_list_len) {
2749 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2750 						     add_head, num_add);
2751 				memset(add_list, 0, list_size);
2752 				num_add = 0;
2753 			}
2754 		}
2755 		if (num_add) {
2756 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2757 					     num_add);
2758 		}
2759 		/* Now move all of the filters from the temp add list back to
2760 		 * the VSI's list.
2761 		 */
2762 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2763 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2764 			/* Only update the state if we're still NEW */
2765 			if (new->f->state == I40E_FILTER_NEW)
2766 				new->f->state = new->state;
2767 			hlist_del(&new->hlist);
2768 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2769 			kfree(new);
2770 		}
2771 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2772 		kfree(add_list);
2773 		add_list = NULL;
2774 	}
2775 
2776 	/* Determine the number of active and failed filters. */
2777 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2778 	vsi->active_filters = 0;
2779 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2780 		if (f->state == I40E_FILTER_ACTIVE)
2781 			vsi->active_filters++;
2782 		else if (f->state == I40E_FILTER_FAILED)
2783 			failed_filters++;
2784 	}
2785 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2786 
2787 	/* Check if we are able to exit overflow promiscuous mode. We can
2788 	 * safely exit if we didn't just enter, we no longer have any failed
2789 	 * filters, and we have reduced filters below the threshold value.
2790 	 */
2791 	if (old_overflow && !failed_filters &&
2792 	    vsi->active_filters < vsi->promisc_threshold) {
2793 		dev_info(&pf->pdev->dev,
2794 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2795 			 vsi_name);
2796 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2797 		vsi->promisc_threshold = 0;
2798 	}
2799 
2800 	/* if the VF is not trusted do not do promisc */
2801 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2802 	    !pf->vf[vsi->vf_id].trusted) {
2803 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2804 		goto out;
2805 	}
2806 
2807 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2808 
2809 	/* If we are entering overflow promiscuous, we need to calculate a new
2810 	 * threshold for when we are safe to exit
2811 	 */
2812 	if (!old_overflow && new_overflow)
2813 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2814 
2815 	/* check for changes in promiscuous modes */
2816 	if (changed_flags & IFF_ALLMULTI) {
2817 		bool cur_multipromisc;
2818 
2819 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2820 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2821 							       vsi->seid,
2822 							       cur_multipromisc,
2823 							       NULL);
2824 		if (aq_ret) {
2825 			retval = i40e_aq_rc_to_posix(aq_ret,
2826 						     hw->aq.asq_last_status);
2827 			dev_info(&pf->pdev->dev,
2828 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2829 				 vsi_name,
2830 				 ERR_PTR(aq_ret),
2831 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2832 		} else {
2833 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2834 				 cur_multipromisc ? "entering" : "leaving");
2835 		}
2836 	}
2837 
2838 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2839 		bool cur_promisc;
2840 
2841 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2842 			       new_overflow);
2843 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2844 		if (aq_ret) {
2845 			retval = i40e_aq_rc_to_posix(aq_ret,
2846 						     hw->aq.asq_last_status);
2847 			dev_info(&pf->pdev->dev,
2848 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2849 				 cur_promisc ? "on" : "off",
2850 				 vsi_name,
2851 				 ERR_PTR(aq_ret),
2852 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2853 		}
2854 	}
2855 out:
2856 	/* if something went wrong then set the changed flag so we try again */
2857 	if (retval)
2858 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2859 
2860 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2861 	return retval;
2862 
2863 err_no_memory:
2864 	/* Restore elements on the temporary add and delete lists */
2865 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2866 err_no_memory_locked:
2867 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2868 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2869 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2870 
2871 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2872 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2873 	return -ENOMEM;
2874 }
2875 
2876 /**
2877  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2878  * @pf: board private structure
2879  **/
2880 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2881 {
2882 	int v;
2883 
2884 	if (!pf)
2885 		return;
2886 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2887 		return;
2888 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2889 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2890 		return;
2891 	}
2892 
2893 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2894 		if (pf->vsi[v] &&
2895 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2896 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2897 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2898 
2899 			if (ret) {
2900 				/* come back and try again later */
2901 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2902 					pf->state);
2903 				break;
2904 			}
2905 		}
2906 	}
2907 }
2908 
2909 /**
2910  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2911  *
2912  * @vsi: VSI to calculate rx_buf_len from
2913  */
2914 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2915 {
2916 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2917 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2918 
2919 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2920 }
2921 
2922 /**
2923  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2924  * @vsi: the vsi
2925  * @xdp_prog: XDP program
2926  **/
2927 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2928 				   struct bpf_prog *xdp_prog)
2929 {
2930 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2931 	u16 chain_len;
2932 
2933 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2934 		chain_len = 1;
2935 	else
2936 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2937 
2938 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2939 }
2940 
2941 /**
2942  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2943  * @netdev: network interface device structure
2944  * @new_mtu: new value for maximum frame size
2945  *
2946  * Returns 0 on success, negative on failure
2947  **/
2948 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2949 {
2950 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2951 	struct i40e_vsi *vsi = np->vsi;
2952 	struct i40e_pf *pf = vsi->back;
2953 	int frame_size;
2954 
2955 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2956 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2957 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2958 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2959 		return -EINVAL;
2960 	}
2961 
2962 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2963 		   netdev->mtu, new_mtu);
2964 	netdev->mtu = new_mtu;
2965 	if (netif_running(netdev))
2966 		i40e_vsi_reinit_locked(vsi);
2967 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2968 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2969 	return 0;
2970 }
2971 
2972 /**
2973  * i40e_ioctl - Access the hwtstamp interface
2974  * @netdev: network interface device structure
2975  * @ifr: interface request data
2976  * @cmd: ioctl command
2977  **/
2978 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2979 {
2980 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2981 	struct i40e_pf *pf = np->vsi->back;
2982 
2983 	switch (cmd) {
2984 	case SIOCGHWTSTAMP:
2985 		return i40e_ptp_get_ts_config(pf, ifr);
2986 	case SIOCSHWTSTAMP:
2987 		return i40e_ptp_set_ts_config(pf, ifr);
2988 	default:
2989 		return -EOPNOTSUPP;
2990 	}
2991 }
2992 
2993 /**
2994  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2995  * @vsi: the vsi being adjusted
2996  **/
2997 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2998 {
2999 	struct i40e_vsi_context ctxt;
3000 	int ret;
3001 
3002 	/* Don't modify stripping options if a port VLAN is active */
3003 	if (vsi->info.pvid)
3004 		return;
3005 
3006 	if ((vsi->info.valid_sections &
3007 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3008 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3009 		return;  /* already enabled */
3010 
3011 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3012 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3013 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3014 
3015 	ctxt.seid = vsi->seid;
3016 	ctxt.info = vsi->info;
3017 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3018 	if (ret) {
3019 		dev_info(&vsi->back->pdev->dev,
3020 			 "update vlan stripping failed, err %pe aq_err %s\n",
3021 			 ERR_PTR(ret),
3022 			 i40e_aq_str(&vsi->back->hw,
3023 				     vsi->back->hw.aq.asq_last_status));
3024 	}
3025 }
3026 
3027 /**
3028  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3029  * @vsi: the vsi being adjusted
3030  **/
3031 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3032 {
3033 	struct i40e_vsi_context ctxt;
3034 	int ret;
3035 
3036 	/* Don't modify stripping options if a port VLAN is active */
3037 	if (vsi->info.pvid)
3038 		return;
3039 
3040 	if ((vsi->info.valid_sections &
3041 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3042 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3043 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3044 		return;  /* already disabled */
3045 
3046 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3047 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3048 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3049 
3050 	ctxt.seid = vsi->seid;
3051 	ctxt.info = vsi->info;
3052 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3053 	if (ret) {
3054 		dev_info(&vsi->back->pdev->dev,
3055 			 "update vlan stripping failed, err %pe aq_err %s\n",
3056 			 ERR_PTR(ret),
3057 			 i40e_aq_str(&vsi->back->hw,
3058 				     vsi->back->hw.aq.asq_last_status));
3059 	}
3060 }
3061 
3062 /**
3063  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3064  * @vsi: the vsi being configured
3065  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3066  *
3067  * This is a helper function for adding a new MAC/VLAN filter with the
3068  * specified VLAN for each existing MAC address already in the hash table.
3069  * This function does *not* perform any accounting to update filters based on
3070  * VLAN mode.
3071  *
3072  * NOTE: this function expects to be called while under the
3073  * mac_filter_hash_lock
3074  **/
3075 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3076 {
3077 	struct i40e_mac_filter *f, *add_f;
3078 	struct hlist_node *h;
3079 	int bkt;
3080 
3081 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3082 		/* If we're asked to add a filter that has been marked for
3083 		 * removal, it is safe to simply restore it to active state.
3084 		 * __i40e_del_filter will have simply deleted any filters which
3085 		 * were previously marked NEW or FAILED, so if it is currently
3086 		 * marked REMOVE it must have previously been ACTIVE. Since we
3087 		 * haven't yet run the sync filters task, just restore this
3088 		 * filter to the ACTIVE state so that the sync task leaves it
3089 		 * in place.
3090 		 */
3091 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3092 			f->state = I40E_FILTER_ACTIVE;
3093 			continue;
3094 		} else if (f->state == I40E_FILTER_REMOVE) {
3095 			continue;
3096 		}
3097 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3098 		if (!add_f) {
3099 			dev_info(&vsi->back->pdev->dev,
3100 				 "Could not add vlan filter %d for %pM\n",
3101 				 vid, f->macaddr);
3102 			return -ENOMEM;
3103 		}
3104 	}
3105 
3106 	return 0;
3107 }
3108 
3109 /**
3110  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3111  * @vsi: the VSI being configured
3112  * @vid: VLAN id to be added
3113  **/
3114 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3115 {
3116 	int err;
3117 
3118 	if (vsi->info.pvid)
3119 		return -EINVAL;
3120 
3121 	/* The network stack will attempt to add VID=0, with the intention to
3122 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3123 	 * these packets by default when configured to receive untagged
3124 	 * packets, so we don't need to add a filter for this case.
3125 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3126 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3127 	 * Thus, we do not want to actually add a filter for VID=0
3128 	 */
3129 	if (!vid)
3130 		return 0;
3131 
3132 	/* Locked once because all functions invoked below iterates list*/
3133 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3134 	err = i40e_add_vlan_all_mac(vsi, vid);
3135 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3136 	if (err)
3137 		return err;
3138 
3139 	/* schedule our worker thread which will take care of
3140 	 * applying the new filter changes
3141 	 */
3142 	i40e_service_event_schedule(vsi->back);
3143 	return 0;
3144 }
3145 
3146 /**
3147  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3148  * @vsi: the vsi being configured
3149  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3150  *
3151  * This function should be used to remove all VLAN filters which match the
3152  * given VID. It does not schedule the service event and does not take the
3153  * mac_filter_hash_lock so it may be combined with other operations under
3154  * a single invocation of the mac_filter_hash_lock.
3155  *
3156  * NOTE: this function expects to be called while under the
3157  * mac_filter_hash_lock
3158  */
3159 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3160 {
3161 	struct i40e_mac_filter *f;
3162 	struct hlist_node *h;
3163 	int bkt;
3164 
3165 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3166 		if (f->vlan == vid)
3167 			__i40e_del_filter(vsi, f);
3168 	}
3169 }
3170 
3171 /**
3172  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3173  * @vsi: the VSI being configured
3174  * @vid: VLAN id to be removed
3175  **/
3176 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3177 {
3178 	if (!vid || vsi->info.pvid)
3179 		return;
3180 
3181 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3182 	i40e_rm_vlan_all_mac(vsi, vid);
3183 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3184 
3185 	/* schedule our worker thread which will take care of
3186 	 * applying the new filter changes
3187 	 */
3188 	i40e_service_event_schedule(vsi->back);
3189 }
3190 
3191 /**
3192  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3193  * @netdev: network interface to be adjusted
3194  * @proto: unused protocol value
3195  * @vid: vlan id to be added
3196  *
3197  * net_device_ops implementation for adding vlan ids
3198  **/
3199 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3200 				__always_unused __be16 proto, u16 vid)
3201 {
3202 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3203 	struct i40e_vsi *vsi = np->vsi;
3204 	int ret = 0;
3205 
3206 	if (vid >= VLAN_N_VID)
3207 		return -EINVAL;
3208 
3209 	ret = i40e_vsi_add_vlan(vsi, vid);
3210 	if (!ret)
3211 		set_bit(vid, vsi->active_vlans);
3212 
3213 	return ret;
3214 }
3215 
3216 /**
3217  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3218  * @netdev: network interface to be adjusted
3219  * @proto: unused protocol value
3220  * @vid: vlan id to be added
3221  **/
3222 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3223 				    __always_unused __be16 proto, u16 vid)
3224 {
3225 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3226 	struct i40e_vsi *vsi = np->vsi;
3227 
3228 	if (vid >= VLAN_N_VID)
3229 		return;
3230 	set_bit(vid, vsi->active_vlans);
3231 }
3232 
3233 /**
3234  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3235  * @netdev: network interface to be adjusted
3236  * @proto: unused protocol value
3237  * @vid: vlan id to be removed
3238  *
3239  * net_device_ops implementation for removing vlan ids
3240  **/
3241 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3242 				 __always_unused __be16 proto, u16 vid)
3243 {
3244 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3245 	struct i40e_vsi *vsi = np->vsi;
3246 
3247 	/* return code is ignored as there is nothing a user
3248 	 * can do about failure to remove and a log message was
3249 	 * already printed from the other function
3250 	 */
3251 	i40e_vsi_kill_vlan(vsi, vid);
3252 
3253 	clear_bit(vid, vsi->active_vlans);
3254 
3255 	return 0;
3256 }
3257 
3258 /**
3259  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3260  * @vsi: the vsi being brought back up
3261  **/
3262 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3263 {
3264 	u16 vid;
3265 
3266 	if (!vsi->netdev)
3267 		return;
3268 
3269 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3270 		i40e_vlan_stripping_enable(vsi);
3271 	else
3272 		i40e_vlan_stripping_disable(vsi);
3273 
3274 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3275 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3276 					vid);
3277 }
3278 
3279 /**
3280  * i40e_vsi_add_pvid - Add pvid for the VSI
3281  * @vsi: the vsi being adjusted
3282  * @vid: the vlan id to set as a PVID
3283  **/
3284 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3285 {
3286 	struct i40e_vsi_context ctxt;
3287 	int ret;
3288 
3289 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3290 	vsi->info.pvid = cpu_to_le16(vid);
3291 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3292 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3293 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3294 
3295 	ctxt.seid = vsi->seid;
3296 	ctxt.info = vsi->info;
3297 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3298 	if (ret) {
3299 		dev_info(&vsi->back->pdev->dev,
3300 			 "add pvid failed, err %pe aq_err %s\n",
3301 			 ERR_PTR(ret),
3302 			 i40e_aq_str(&vsi->back->hw,
3303 				     vsi->back->hw.aq.asq_last_status));
3304 		return -ENOENT;
3305 	}
3306 
3307 	return 0;
3308 }
3309 
3310 /**
3311  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3312  * @vsi: the vsi being adjusted
3313  *
3314  * Just use the vlan_rx_register() service to put it back to normal
3315  **/
3316 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3317 {
3318 	vsi->info.pvid = 0;
3319 
3320 	i40e_vlan_stripping_disable(vsi);
3321 }
3322 
3323 /**
3324  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3325  * @vsi: ptr to the VSI
3326  *
3327  * If this function returns with an error, then it's possible one or
3328  * more of the rings is populated (while the rest are not).  It is the
3329  * callers duty to clean those orphaned rings.
3330  *
3331  * Return 0 on success, negative on failure
3332  **/
3333 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3334 {
3335 	int i, err = 0;
3336 
3337 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3338 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3339 
3340 	if (!i40e_enabled_xdp_vsi(vsi))
3341 		return err;
3342 
3343 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3344 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3345 
3346 	return err;
3347 }
3348 
3349 /**
3350  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3351  * @vsi: ptr to the VSI
3352  *
3353  * Free VSI's transmit software resources
3354  **/
3355 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3356 {
3357 	int i;
3358 
3359 	if (vsi->tx_rings) {
3360 		for (i = 0; i < vsi->num_queue_pairs; i++)
3361 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3362 				i40e_free_tx_resources(vsi->tx_rings[i]);
3363 	}
3364 
3365 	if (vsi->xdp_rings) {
3366 		for (i = 0; i < vsi->num_queue_pairs; i++)
3367 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3368 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3369 	}
3370 }
3371 
3372 /**
3373  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3374  * @vsi: ptr to the VSI
3375  *
3376  * If this function returns with an error, then it's possible one or
3377  * more of the rings is populated (while the rest are not).  It is the
3378  * callers duty to clean those orphaned rings.
3379  *
3380  * Return 0 on success, negative on failure
3381  **/
3382 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3383 {
3384 	int i, err = 0;
3385 
3386 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3387 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3388 	return err;
3389 }
3390 
3391 /**
3392  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3393  * @vsi: ptr to the VSI
3394  *
3395  * Free all receive software resources
3396  **/
3397 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3398 {
3399 	int i;
3400 
3401 	if (!vsi->rx_rings)
3402 		return;
3403 
3404 	for (i = 0; i < vsi->num_queue_pairs; i++)
3405 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3406 			i40e_free_rx_resources(vsi->rx_rings[i]);
3407 }
3408 
3409 /**
3410  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3411  * @ring: The Tx ring to configure
3412  *
3413  * This enables/disables XPS for a given Tx descriptor ring
3414  * based on the TCs enabled for the VSI that ring belongs to.
3415  **/
3416 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3417 {
3418 	int cpu;
3419 
3420 	if (!ring->q_vector || !ring->netdev || ring->ch)
3421 		return;
3422 
3423 	/* We only initialize XPS once, so as not to overwrite user settings */
3424 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3425 		return;
3426 
3427 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3428 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3429 			    ring->queue_index);
3430 }
3431 
3432 /**
3433  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3434  * @ring: The Tx or Rx ring
3435  *
3436  * Returns the AF_XDP buffer pool or NULL.
3437  **/
3438 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3439 {
3440 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3441 	int qid = ring->queue_index;
3442 
3443 	if (ring_is_xdp(ring))
3444 		qid -= ring->vsi->alloc_queue_pairs;
3445 
3446 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3447 		return NULL;
3448 
3449 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3450 }
3451 
3452 /**
3453  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3454  * @ring: The Tx ring to configure
3455  *
3456  * Configure the Tx descriptor ring in the HMC context.
3457  **/
3458 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3459 {
3460 	struct i40e_vsi *vsi = ring->vsi;
3461 	u16 pf_q = vsi->base_queue + ring->queue_index;
3462 	struct i40e_hw *hw = &vsi->back->hw;
3463 	struct i40e_hmc_obj_txq tx_ctx;
3464 	u32 qtx_ctl = 0;
3465 	int err = 0;
3466 
3467 	if (ring_is_xdp(ring))
3468 		ring->xsk_pool = i40e_xsk_pool(ring);
3469 
3470 	/* some ATR related tx ring init */
3471 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3472 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3473 		ring->atr_count = 0;
3474 	} else {
3475 		ring->atr_sample_rate = 0;
3476 	}
3477 
3478 	/* configure XPS */
3479 	i40e_config_xps_tx_ring(ring);
3480 
3481 	/* clear the context structure first */
3482 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3483 
3484 	tx_ctx.new_context = 1;
3485 	tx_ctx.base = (ring->dma / 128);
3486 	tx_ctx.qlen = ring->count;
3487 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3488 					       I40E_FLAG_FD_ATR_ENABLED));
3489 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3490 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3491 	if (vsi->type != I40E_VSI_FDIR)
3492 		tx_ctx.head_wb_ena = 1;
3493 	tx_ctx.head_wb_addr = ring->dma +
3494 			      (ring->count * sizeof(struct i40e_tx_desc));
3495 
3496 	/* As part of VSI creation/update, FW allocates certain
3497 	 * Tx arbitration queue sets for each TC enabled for
3498 	 * the VSI. The FW returns the handles to these queue
3499 	 * sets as part of the response buffer to Add VSI,
3500 	 * Update VSI, etc. AQ commands. It is expected that
3501 	 * these queue set handles be associated with the Tx
3502 	 * queues by the driver as part of the TX queue context
3503 	 * initialization. This has to be done regardless of
3504 	 * DCB as by default everything is mapped to TC0.
3505 	 */
3506 
3507 	if (ring->ch)
3508 		tx_ctx.rdylist =
3509 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3510 
3511 	else
3512 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3513 
3514 	tx_ctx.rdylist_act = 0;
3515 
3516 	/* clear the context in the HMC */
3517 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3518 	if (err) {
3519 		dev_info(&vsi->back->pdev->dev,
3520 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3521 			 ring->queue_index, pf_q, err);
3522 		return -ENOMEM;
3523 	}
3524 
3525 	/* set the context in the HMC */
3526 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3527 	if (err) {
3528 		dev_info(&vsi->back->pdev->dev,
3529 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3530 			 ring->queue_index, pf_q, err);
3531 		return -ENOMEM;
3532 	}
3533 
3534 	/* Now associate this queue with this PCI function */
3535 	if (ring->ch) {
3536 		if (ring->ch->type == I40E_VSI_VMDQ2)
3537 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3538 		else
3539 			return -EINVAL;
3540 
3541 		qtx_ctl |= (ring->ch->vsi_number <<
3542 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3543 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3544 	} else {
3545 		if (vsi->type == I40E_VSI_VMDQ2) {
3546 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3547 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3548 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3549 		} else {
3550 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3551 		}
3552 	}
3553 
3554 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3555 		    I40E_QTX_CTL_PF_INDX_MASK);
3556 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3557 	i40e_flush(hw);
3558 
3559 	/* cache tail off for easier writes later */
3560 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3561 
3562 	return 0;
3563 }
3564 
3565 /**
3566  * i40e_rx_offset - Return expected offset into page to access data
3567  * @rx_ring: Ring we are requesting offset of
3568  *
3569  * Returns the offset value for ring into the data buffer.
3570  */
3571 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3572 {
3573 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3574 }
3575 
3576 /**
3577  * i40e_configure_rx_ring - Configure a receive ring context
3578  * @ring: The Rx ring to configure
3579  *
3580  * Configure the Rx descriptor ring in the HMC context.
3581  **/
3582 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3583 {
3584 	struct i40e_vsi *vsi = ring->vsi;
3585 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3586 	u16 pf_q = vsi->base_queue + ring->queue_index;
3587 	struct i40e_hw *hw = &vsi->back->hw;
3588 	struct i40e_hmc_obj_rxq rx_ctx;
3589 	int err = 0;
3590 	bool ok;
3591 	int ret;
3592 
3593 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3594 
3595 	/* clear the context structure first */
3596 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3597 
3598 	if (ring->vsi->type == I40E_VSI_MAIN)
3599 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3600 
3601 	ring->xsk_pool = i40e_xsk_pool(ring);
3602 	if (ring->xsk_pool) {
3603 		ring->rx_buf_len =
3604 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3605 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3606 						 MEM_TYPE_XSK_BUFF_POOL,
3607 						 NULL);
3608 		if (ret)
3609 			return ret;
3610 		dev_info(&vsi->back->pdev->dev,
3611 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3612 			 ring->queue_index);
3613 
3614 	} else {
3615 		ring->rx_buf_len = vsi->rx_buf_len;
3616 		if (ring->vsi->type == I40E_VSI_MAIN) {
3617 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3618 							 MEM_TYPE_PAGE_SHARED,
3619 							 NULL);
3620 			if (ret)
3621 				return ret;
3622 		}
3623 	}
3624 
3625 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3626 
3627 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3628 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3629 
3630 	rx_ctx.base = (ring->dma / 128);
3631 	rx_ctx.qlen = ring->count;
3632 
3633 	/* use 16 byte descriptors */
3634 	rx_ctx.dsize = 0;
3635 
3636 	/* descriptor type is always zero
3637 	 * rx_ctx.dtype = 0;
3638 	 */
3639 	rx_ctx.hsplit_0 = 0;
3640 
3641 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3642 	if (hw->revision_id == 0)
3643 		rx_ctx.lrxqthresh = 0;
3644 	else
3645 		rx_ctx.lrxqthresh = 1;
3646 	rx_ctx.crcstrip = 1;
3647 	rx_ctx.l2tsel = 1;
3648 	/* this controls whether VLAN is stripped from inner headers */
3649 	rx_ctx.showiv = 0;
3650 	/* set the prefena field to 1 because the manual says to */
3651 	rx_ctx.prefena = 1;
3652 
3653 	/* clear the context in the HMC */
3654 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3655 	if (err) {
3656 		dev_info(&vsi->back->pdev->dev,
3657 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3658 			 ring->queue_index, pf_q, err);
3659 		return -ENOMEM;
3660 	}
3661 
3662 	/* set the context in the HMC */
3663 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3664 	if (err) {
3665 		dev_info(&vsi->back->pdev->dev,
3666 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3667 			 ring->queue_index, pf_q, err);
3668 		return -ENOMEM;
3669 	}
3670 
3671 	/* configure Rx buffer alignment */
3672 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3673 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3674 			dev_info(&vsi->back->pdev->dev,
3675 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3676 			return -EOPNOTSUPP;
3677 		}
3678 		clear_ring_build_skb_enabled(ring);
3679 	} else {
3680 		set_ring_build_skb_enabled(ring);
3681 	}
3682 
3683 	ring->rx_offset = i40e_rx_offset(ring);
3684 
3685 	/* cache tail for quicker writes, and clear the reg before use */
3686 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3687 	writel(0, ring->tail);
3688 
3689 	if (ring->xsk_pool) {
3690 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3691 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3692 	} else {
3693 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3694 	}
3695 	if (!ok) {
3696 		/* Log this in case the user has forgotten to give the kernel
3697 		 * any buffers, even later in the application.
3698 		 */
3699 		dev_info(&vsi->back->pdev->dev,
3700 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3701 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3702 			 ring->queue_index, pf_q);
3703 	}
3704 
3705 	return 0;
3706 }
3707 
3708 /**
3709  * i40e_vsi_configure_tx - Configure the VSI for Tx
3710  * @vsi: VSI structure describing this set of rings and resources
3711  *
3712  * Configure the Tx VSI for operation.
3713  **/
3714 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3715 {
3716 	int err = 0;
3717 	u16 i;
3718 
3719 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3720 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3721 
3722 	if (err || !i40e_enabled_xdp_vsi(vsi))
3723 		return err;
3724 
3725 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3726 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3727 
3728 	return err;
3729 }
3730 
3731 /**
3732  * i40e_vsi_configure_rx - Configure the VSI for Rx
3733  * @vsi: the VSI being configured
3734  *
3735  * Configure the Rx VSI for operation.
3736  **/
3737 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3738 {
3739 	int err = 0;
3740 	u16 i;
3741 
3742 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3743 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3744 
3745 #if (PAGE_SIZE < 8192)
3746 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3747 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3748 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3749 		vsi->max_frame = vsi->rx_buf_len;
3750 	}
3751 #endif
3752 
3753 	/* set up individual rings */
3754 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3755 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3756 
3757 	return err;
3758 }
3759 
3760 /**
3761  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3762  * @vsi: ptr to the VSI
3763  **/
3764 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3765 {
3766 	struct i40e_ring *tx_ring, *rx_ring;
3767 	u16 qoffset, qcount;
3768 	int i, n;
3769 
3770 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3771 		/* Reset the TC information */
3772 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3773 			rx_ring = vsi->rx_rings[i];
3774 			tx_ring = vsi->tx_rings[i];
3775 			rx_ring->dcb_tc = 0;
3776 			tx_ring->dcb_tc = 0;
3777 		}
3778 		return;
3779 	}
3780 
3781 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3782 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3783 			continue;
3784 
3785 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3786 		qcount = vsi->tc_config.tc_info[n].qcount;
3787 		for (i = qoffset; i < (qoffset + qcount); i++) {
3788 			rx_ring = vsi->rx_rings[i];
3789 			tx_ring = vsi->tx_rings[i];
3790 			rx_ring->dcb_tc = n;
3791 			tx_ring->dcb_tc = n;
3792 		}
3793 	}
3794 }
3795 
3796 /**
3797  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3798  * @vsi: ptr to the VSI
3799  **/
3800 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3801 {
3802 	if (vsi->netdev)
3803 		i40e_set_rx_mode(vsi->netdev);
3804 }
3805 
3806 /**
3807  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3808  * @pf: Pointer to the targeted PF
3809  *
3810  * Set all flow director counters to 0.
3811  */
3812 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3813 {
3814 	pf->fd_tcp4_filter_cnt = 0;
3815 	pf->fd_udp4_filter_cnt = 0;
3816 	pf->fd_sctp4_filter_cnt = 0;
3817 	pf->fd_ip4_filter_cnt = 0;
3818 	pf->fd_tcp6_filter_cnt = 0;
3819 	pf->fd_udp6_filter_cnt = 0;
3820 	pf->fd_sctp6_filter_cnt = 0;
3821 	pf->fd_ip6_filter_cnt = 0;
3822 }
3823 
3824 /**
3825  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3826  * @vsi: Pointer to the targeted VSI
3827  *
3828  * This function replays the hlist on the hw where all the SB Flow Director
3829  * filters were saved.
3830  **/
3831 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3832 {
3833 	struct i40e_fdir_filter *filter;
3834 	struct i40e_pf *pf = vsi->back;
3835 	struct hlist_node *node;
3836 
3837 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3838 		return;
3839 
3840 	/* Reset FDir counters as we're replaying all existing filters */
3841 	i40e_reset_fdir_filter_cnt(pf);
3842 
3843 	hlist_for_each_entry_safe(filter, node,
3844 				  &pf->fdir_filter_list, fdir_node) {
3845 		i40e_add_del_fdir(vsi, filter, true);
3846 	}
3847 }
3848 
3849 /**
3850  * i40e_vsi_configure - Set up the VSI for action
3851  * @vsi: the VSI being configured
3852  **/
3853 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3854 {
3855 	int err;
3856 
3857 	i40e_set_vsi_rx_mode(vsi);
3858 	i40e_restore_vlan(vsi);
3859 	i40e_vsi_config_dcb_rings(vsi);
3860 	err = i40e_vsi_configure_tx(vsi);
3861 	if (!err)
3862 		err = i40e_vsi_configure_rx(vsi);
3863 
3864 	return err;
3865 }
3866 
3867 /**
3868  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3869  * @vsi: the VSI being configured
3870  **/
3871 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3872 {
3873 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3874 	struct i40e_pf *pf = vsi->back;
3875 	struct i40e_hw *hw = &pf->hw;
3876 	u16 vector;
3877 	int i, q;
3878 	u32 qp;
3879 
3880 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3881 	 * and PFINT_LNKLSTn registers, e.g.:
3882 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3883 	 */
3884 	qp = vsi->base_queue;
3885 	vector = vsi->base_vector;
3886 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3887 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3888 
3889 		q_vector->rx.next_update = jiffies + 1;
3890 		q_vector->rx.target_itr =
3891 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3892 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3893 		     q_vector->rx.target_itr >> 1);
3894 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3895 
3896 		q_vector->tx.next_update = jiffies + 1;
3897 		q_vector->tx.target_itr =
3898 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3899 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3900 		     q_vector->tx.target_itr >> 1);
3901 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3902 
3903 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3904 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3905 
3906 		/* begin of linked list for RX queue assigned to this vector */
3907 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3908 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3909 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3910 			u32 val;
3911 
3912 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3913 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3914 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3915 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3916 			      (I40E_QUEUE_TYPE_TX <<
3917 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3918 
3919 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3920 
3921 			if (has_xdp) {
3922 				/* TX queue with next queue set to TX */
3923 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3924 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3925 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3926 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3927 				      (I40E_QUEUE_TYPE_TX <<
3928 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3929 
3930 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3931 			}
3932 			/* TX queue with next RX or end of linked list */
3933 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3934 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3935 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3936 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3937 			      (I40E_QUEUE_TYPE_RX <<
3938 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3939 
3940 			/* Terminate the linked list */
3941 			if (q == (q_vector->num_ringpairs - 1))
3942 				val |= (I40E_QUEUE_END_OF_LIST <<
3943 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3944 
3945 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3946 			qp++;
3947 		}
3948 	}
3949 
3950 	i40e_flush(hw);
3951 }
3952 
3953 /**
3954  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3955  * @pf: pointer to private device data structure
3956  **/
3957 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3958 {
3959 	struct i40e_hw *hw = &pf->hw;
3960 	u32 val;
3961 
3962 	/* clear things first */
3963 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3964 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3965 
3966 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3967 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3968 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3969 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3970 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3971 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3972 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3973 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3974 
3975 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3976 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3977 
3978 	if (pf->flags & I40E_FLAG_PTP)
3979 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3980 
3981 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3982 
3983 	/* SW_ITR_IDX = 0, but don't change INTENA */
3984 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3985 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3986 
3987 	/* OTHER_ITR_IDX = 0 */
3988 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3989 }
3990 
3991 /**
3992  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3993  * @vsi: the VSI being configured
3994  **/
3995 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3996 {
3997 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3998 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3999 	struct i40e_pf *pf = vsi->back;
4000 	struct i40e_hw *hw = &pf->hw;
4001 
4002 	/* set the ITR configuration */
4003 	q_vector->rx.next_update = jiffies + 1;
4004 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4005 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4006 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4007 	q_vector->tx.next_update = jiffies + 1;
4008 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4009 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4010 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4011 
4012 	i40e_enable_misc_int_causes(pf);
4013 
4014 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4015 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4016 
4017 	/* Associate the queue pair to the vector and enable the queue
4018 	 * interrupt RX queue in linked list with next queue set to TX
4019 	 */
4020 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4021 
4022 	if (i40e_enabled_xdp_vsi(vsi)) {
4023 		/* TX queue in linked list with next queue set to TX */
4024 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4025 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4026 	}
4027 
4028 	/* last TX queue so the next RX queue doesn't matter */
4029 	wr32(hw, I40E_QINT_TQCTL(0),
4030 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4031 	i40e_flush(hw);
4032 }
4033 
4034 /**
4035  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4036  * @pf: board private structure
4037  **/
4038 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4039 {
4040 	struct i40e_hw *hw = &pf->hw;
4041 
4042 	wr32(hw, I40E_PFINT_DYN_CTL0,
4043 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4044 	i40e_flush(hw);
4045 }
4046 
4047 /**
4048  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4049  * @pf: board private structure
4050  **/
4051 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4052 {
4053 	struct i40e_hw *hw = &pf->hw;
4054 	u32 val;
4055 
4056 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4057 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4058 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4059 
4060 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4061 	i40e_flush(hw);
4062 }
4063 
4064 /**
4065  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4066  * @irq: interrupt number
4067  * @data: pointer to a q_vector
4068  **/
4069 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4070 {
4071 	struct i40e_q_vector *q_vector = data;
4072 
4073 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4074 		return IRQ_HANDLED;
4075 
4076 	napi_schedule_irqoff(&q_vector->napi);
4077 
4078 	return IRQ_HANDLED;
4079 }
4080 
4081 /**
4082  * i40e_irq_affinity_notify - Callback for affinity changes
4083  * @notify: context as to what irq was changed
4084  * @mask: the new affinity mask
4085  *
4086  * This is a callback function used by the irq_set_affinity_notifier function
4087  * so that we may register to receive changes to the irq affinity masks.
4088  **/
4089 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4090 				     const cpumask_t *mask)
4091 {
4092 	struct i40e_q_vector *q_vector =
4093 		container_of(notify, struct i40e_q_vector, affinity_notify);
4094 
4095 	cpumask_copy(&q_vector->affinity_mask, mask);
4096 }
4097 
4098 /**
4099  * i40e_irq_affinity_release - Callback for affinity notifier release
4100  * @ref: internal core kernel usage
4101  *
4102  * This is a callback function used by the irq_set_affinity_notifier function
4103  * to inform the current notification subscriber that they will no longer
4104  * receive notifications.
4105  **/
4106 static void i40e_irq_affinity_release(struct kref *ref) {}
4107 
4108 /**
4109  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4110  * @vsi: the VSI being configured
4111  * @basename: name for the vector
4112  *
4113  * Allocates MSI-X vectors and requests interrupts from the kernel.
4114  **/
4115 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4116 {
4117 	int q_vectors = vsi->num_q_vectors;
4118 	struct i40e_pf *pf = vsi->back;
4119 	int base = vsi->base_vector;
4120 	int rx_int_idx = 0;
4121 	int tx_int_idx = 0;
4122 	int vector, err;
4123 	int irq_num;
4124 	int cpu;
4125 
4126 	for (vector = 0; vector < q_vectors; vector++) {
4127 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4128 
4129 		irq_num = pf->msix_entries[base + vector].vector;
4130 
4131 		if (q_vector->tx.ring && q_vector->rx.ring) {
4132 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4133 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4134 			tx_int_idx++;
4135 		} else if (q_vector->rx.ring) {
4136 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4137 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4138 		} else if (q_vector->tx.ring) {
4139 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4140 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4141 		} else {
4142 			/* skip this unused q_vector */
4143 			continue;
4144 		}
4145 		err = request_irq(irq_num,
4146 				  vsi->irq_handler,
4147 				  0,
4148 				  q_vector->name,
4149 				  q_vector);
4150 		if (err) {
4151 			dev_info(&pf->pdev->dev,
4152 				 "MSIX request_irq failed, error: %d\n", err);
4153 			goto free_queue_irqs;
4154 		}
4155 
4156 		/* register for affinity change notifications */
4157 		q_vector->irq_num = irq_num;
4158 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4159 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4160 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4161 		/* Spread affinity hints out across online CPUs.
4162 		 *
4163 		 * get_cpu_mask returns a static constant mask with
4164 		 * a permanent lifetime so it's ok to pass to
4165 		 * irq_update_affinity_hint without making a copy.
4166 		 */
4167 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4168 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4169 	}
4170 
4171 	vsi->irqs_ready = true;
4172 	return 0;
4173 
4174 free_queue_irqs:
4175 	while (vector) {
4176 		vector--;
4177 		irq_num = pf->msix_entries[base + vector].vector;
4178 		irq_set_affinity_notifier(irq_num, NULL);
4179 		irq_update_affinity_hint(irq_num, NULL);
4180 		free_irq(irq_num, &vsi->q_vectors[vector]);
4181 	}
4182 	return err;
4183 }
4184 
4185 /**
4186  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4187  * @vsi: the VSI being un-configured
4188  **/
4189 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4190 {
4191 	struct i40e_pf *pf = vsi->back;
4192 	struct i40e_hw *hw = &pf->hw;
4193 	int base = vsi->base_vector;
4194 	int i;
4195 
4196 	/* disable interrupt causation from each queue */
4197 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4198 		u32 val;
4199 
4200 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4201 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4202 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4203 
4204 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4205 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4206 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4207 
4208 		if (!i40e_enabled_xdp_vsi(vsi))
4209 			continue;
4210 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4211 	}
4212 
4213 	/* disable each interrupt */
4214 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4215 		for (i = vsi->base_vector;
4216 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4217 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4218 
4219 		i40e_flush(hw);
4220 		for (i = 0; i < vsi->num_q_vectors; i++)
4221 			synchronize_irq(pf->msix_entries[i + base].vector);
4222 	} else {
4223 		/* Legacy and MSI mode - this stops all interrupt handling */
4224 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4225 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4226 		i40e_flush(hw);
4227 		synchronize_irq(pf->pdev->irq);
4228 	}
4229 }
4230 
4231 /**
4232  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4233  * @vsi: the VSI being configured
4234  **/
4235 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4236 {
4237 	struct i40e_pf *pf = vsi->back;
4238 	int i;
4239 
4240 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4241 		for (i = 0; i < vsi->num_q_vectors; i++)
4242 			i40e_irq_dynamic_enable(vsi, i);
4243 	} else {
4244 		i40e_irq_dynamic_enable_icr0(pf);
4245 	}
4246 
4247 	i40e_flush(&pf->hw);
4248 	return 0;
4249 }
4250 
4251 /**
4252  * i40e_free_misc_vector - Free the vector that handles non-queue events
4253  * @pf: board private structure
4254  **/
4255 static void i40e_free_misc_vector(struct i40e_pf *pf)
4256 {
4257 	/* Disable ICR 0 */
4258 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4259 	i40e_flush(&pf->hw);
4260 
4261 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4262 		free_irq(pf->msix_entries[0].vector, pf);
4263 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4264 	}
4265 }
4266 
4267 /**
4268  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4269  * @irq: interrupt number
4270  * @data: pointer to a q_vector
4271  *
4272  * This is the handler used for all MSI/Legacy interrupts, and deals
4273  * with both queue and non-queue interrupts.  This is also used in
4274  * MSIX mode to handle the non-queue interrupts.
4275  **/
4276 static irqreturn_t i40e_intr(int irq, void *data)
4277 {
4278 	struct i40e_pf *pf = (struct i40e_pf *)data;
4279 	struct i40e_hw *hw = &pf->hw;
4280 	irqreturn_t ret = IRQ_NONE;
4281 	u32 icr0, icr0_remaining;
4282 	u32 val, ena_mask;
4283 
4284 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4285 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4286 
4287 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4288 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4289 		goto enable_intr;
4290 
4291 	/* if interrupt but no bits showing, must be SWINT */
4292 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4293 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4294 		pf->sw_int_count++;
4295 
4296 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4297 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4298 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4299 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4300 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4301 	}
4302 
4303 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4304 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4305 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4306 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4307 
4308 		/* We do not have a way to disarm Queue causes while leaving
4309 		 * interrupt enabled for all other causes, ideally
4310 		 * interrupt should be disabled while we are in NAPI but
4311 		 * this is not a performance path and napi_schedule()
4312 		 * can deal with rescheduling.
4313 		 */
4314 		if (!test_bit(__I40E_DOWN, pf->state))
4315 			napi_schedule_irqoff(&q_vector->napi);
4316 	}
4317 
4318 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4319 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4320 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4321 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4322 	}
4323 
4324 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4325 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4326 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4327 	}
4328 
4329 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4330 		/* disable any further VFLR event notifications */
4331 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4332 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4333 
4334 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4335 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4336 		} else {
4337 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4338 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4339 		}
4340 	}
4341 
4342 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4343 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4344 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4345 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4346 		val = rd32(hw, I40E_GLGEN_RSTAT);
4347 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4348 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4349 		if (val == I40E_RESET_CORER) {
4350 			pf->corer_count++;
4351 		} else if (val == I40E_RESET_GLOBR) {
4352 			pf->globr_count++;
4353 		} else if (val == I40E_RESET_EMPR) {
4354 			pf->empr_count++;
4355 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4356 		}
4357 	}
4358 
4359 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4360 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4361 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4362 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4363 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4364 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4365 	}
4366 
4367 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4368 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4369 
4370 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4371 			schedule_work(&pf->ptp_extts0_work);
4372 
4373 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4374 			i40e_ptp_tx_hwtstamp(pf);
4375 
4376 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4377 	}
4378 
4379 	/* If a critical error is pending we have no choice but to reset the
4380 	 * device.
4381 	 * Report and mask out any remaining unexpected interrupts.
4382 	 */
4383 	icr0_remaining = icr0 & ena_mask;
4384 	if (icr0_remaining) {
4385 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4386 			 icr0_remaining);
4387 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4388 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4389 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4390 			dev_info(&pf->pdev->dev, "device will be reset\n");
4391 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4392 			i40e_service_event_schedule(pf);
4393 		}
4394 		ena_mask &= ~icr0_remaining;
4395 	}
4396 	ret = IRQ_HANDLED;
4397 
4398 enable_intr:
4399 	/* re-enable interrupt causes */
4400 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4401 	if (!test_bit(__I40E_DOWN, pf->state) ||
4402 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4403 		i40e_service_event_schedule(pf);
4404 		i40e_irq_dynamic_enable_icr0(pf);
4405 	}
4406 
4407 	return ret;
4408 }
4409 
4410 /**
4411  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4412  * @tx_ring:  tx ring to clean
4413  * @budget:   how many cleans we're allowed
4414  *
4415  * Returns true if there's any budget left (e.g. the clean is finished)
4416  **/
4417 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4418 {
4419 	struct i40e_vsi *vsi = tx_ring->vsi;
4420 	u16 i = tx_ring->next_to_clean;
4421 	struct i40e_tx_buffer *tx_buf;
4422 	struct i40e_tx_desc *tx_desc;
4423 
4424 	tx_buf = &tx_ring->tx_bi[i];
4425 	tx_desc = I40E_TX_DESC(tx_ring, i);
4426 	i -= tx_ring->count;
4427 
4428 	do {
4429 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4430 
4431 		/* if next_to_watch is not set then there is no work pending */
4432 		if (!eop_desc)
4433 			break;
4434 
4435 		/* prevent any other reads prior to eop_desc */
4436 		smp_rmb();
4437 
4438 		/* if the descriptor isn't done, no work yet to do */
4439 		if (!(eop_desc->cmd_type_offset_bsz &
4440 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4441 			break;
4442 
4443 		/* clear next_to_watch to prevent false hangs */
4444 		tx_buf->next_to_watch = NULL;
4445 
4446 		tx_desc->buffer_addr = 0;
4447 		tx_desc->cmd_type_offset_bsz = 0;
4448 		/* move past filter desc */
4449 		tx_buf++;
4450 		tx_desc++;
4451 		i++;
4452 		if (unlikely(!i)) {
4453 			i -= tx_ring->count;
4454 			tx_buf = tx_ring->tx_bi;
4455 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4456 		}
4457 		/* unmap skb header data */
4458 		dma_unmap_single(tx_ring->dev,
4459 				 dma_unmap_addr(tx_buf, dma),
4460 				 dma_unmap_len(tx_buf, len),
4461 				 DMA_TO_DEVICE);
4462 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4463 			kfree(tx_buf->raw_buf);
4464 
4465 		tx_buf->raw_buf = NULL;
4466 		tx_buf->tx_flags = 0;
4467 		tx_buf->next_to_watch = NULL;
4468 		dma_unmap_len_set(tx_buf, len, 0);
4469 		tx_desc->buffer_addr = 0;
4470 		tx_desc->cmd_type_offset_bsz = 0;
4471 
4472 		/* move us past the eop_desc for start of next FD desc */
4473 		tx_buf++;
4474 		tx_desc++;
4475 		i++;
4476 		if (unlikely(!i)) {
4477 			i -= tx_ring->count;
4478 			tx_buf = tx_ring->tx_bi;
4479 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4480 		}
4481 
4482 		/* update budget accounting */
4483 		budget--;
4484 	} while (likely(budget));
4485 
4486 	i += tx_ring->count;
4487 	tx_ring->next_to_clean = i;
4488 
4489 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4490 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4491 
4492 	return budget > 0;
4493 }
4494 
4495 /**
4496  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4497  * @irq: interrupt number
4498  * @data: pointer to a q_vector
4499  **/
4500 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4501 {
4502 	struct i40e_q_vector *q_vector = data;
4503 	struct i40e_vsi *vsi;
4504 
4505 	if (!q_vector->tx.ring)
4506 		return IRQ_HANDLED;
4507 
4508 	vsi = q_vector->tx.ring->vsi;
4509 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4510 
4511 	return IRQ_HANDLED;
4512 }
4513 
4514 /**
4515  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4516  * @vsi: the VSI being configured
4517  * @v_idx: vector index
4518  * @qp_idx: queue pair index
4519  **/
4520 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4521 {
4522 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4523 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4524 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4525 
4526 	tx_ring->q_vector = q_vector;
4527 	tx_ring->next = q_vector->tx.ring;
4528 	q_vector->tx.ring = tx_ring;
4529 	q_vector->tx.count++;
4530 
4531 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4532 	if (i40e_enabled_xdp_vsi(vsi)) {
4533 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4534 
4535 		xdp_ring->q_vector = q_vector;
4536 		xdp_ring->next = q_vector->tx.ring;
4537 		q_vector->tx.ring = xdp_ring;
4538 		q_vector->tx.count++;
4539 	}
4540 
4541 	rx_ring->q_vector = q_vector;
4542 	rx_ring->next = q_vector->rx.ring;
4543 	q_vector->rx.ring = rx_ring;
4544 	q_vector->rx.count++;
4545 }
4546 
4547 /**
4548  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4549  * @vsi: the VSI being configured
4550  *
4551  * This function maps descriptor rings to the queue-specific vectors
4552  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4553  * one vector per queue pair, but on a constrained vector budget, we
4554  * group the queue pairs as "efficiently" as possible.
4555  **/
4556 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4557 {
4558 	int qp_remaining = vsi->num_queue_pairs;
4559 	int q_vectors = vsi->num_q_vectors;
4560 	int num_ringpairs;
4561 	int v_start = 0;
4562 	int qp_idx = 0;
4563 
4564 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4565 	 * group them so there are multiple queues per vector.
4566 	 * It is also important to go through all the vectors available to be
4567 	 * sure that if we don't use all the vectors, that the remaining vectors
4568 	 * are cleared. This is especially important when decreasing the
4569 	 * number of queues in use.
4570 	 */
4571 	for (; v_start < q_vectors; v_start++) {
4572 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4573 
4574 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4575 
4576 		q_vector->num_ringpairs = num_ringpairs;
4577 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4578 
4579 		q_vector->rx.count = 0;
4580 		q_vector->tx.count = 0;
4581 		q_vector->rx.ring = NULL;
4582 		q_vector->tx.ring = NULL;
4583 
4584 		while (num_ringpairs--) {
4585 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4586 			qp_idx++;
4587 			qp_remaining--;
4588 		}
4589 	}
4590 }
4591 
4592 /**
4593  * i40e_vsi_request_irq - Request IRQ from the OS
4594  * @vsi: the VSI being configured
4595  * @basename: name for the vector
4596  **/
4597 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4598 {
4599 	struct i40e_pf *pf = vsi->back;
4600 	int err;
4601 
4602 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4603 		err = i40e_vsi_request_irq_msix(vsi, basename);
4604 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4605 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4606 				  pf->int_name, pf);
4607 	else
4608 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4609 				  pf->int_name, pf);
4610 
4611 	if (err)
4612 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4613 
4614 	return err;
4615 }
4616 
4617 #ifdef CONFIG_NET_POLL_CONTROLLER
4618 /**
4619  * i40e_netpoll - A Polling 'interrupt' handler
4620  * @netdev: network interface device structure
4621  *
4622  * This is used by netconsole to send skbs without having to re-enable
4623  * interrupts.  It's not called while the normal interrupt routine is executing.
4624  **/
4625 static void i40e_netpoll(struct net_device *netdev)
4626 {
4627 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4628 	struct i40e_vsi *vsi = np->vsi;
4629 	struct i40e_pf *pf = vsi->back;
4630 	int i;
4631 
4632 	/* if interface is down do nothing */
4633 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4634 		return;
4635 
4636 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4637 		for (i = 0; i < vsi->num_q_vectors; i++)
4638 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4639 	} else {
4640 		i40e_intr(pf->pdev->irq, netdev);
4641 	}
4642 }
4643 #endif
4644 
4645 #define I40E_QTX_ENA_WAIT_COUNT 50
4646 
4647 /**
4648  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4649  * @pf: the PF being configured
4650  * @pf_q: the PF queue
4651  * @enable: enable or disable state of the queue
4652  *
4653  * This routine will wait for the given Tx queue of the PF to reach the
4654  * enabled or disabled state.
4655  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4656  * multiple retries; else will return 0 in case of success.
4657  **/
4658 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4659 {
4660 	int i;
4661 	u32 tx_reg;
4662 
4663 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4664 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4665 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4666 			break;
4667 
4668 		usleep_range(10, 20);
4669 	}
4670 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4671 		return -ETIMEDOUT;
4672 
4673 	return 0;
4674 }
4675 
4676 /**
4677  * i40e_control_tx_q - Start or stop a particular Tx queue
4678  * @pf: the PF structure
4679  * @pf_q: the PF queue to configure
4680  * @enable: start or stop the queue
4681  *
4682  * This function enables or disables a single queue. Note that any delay
4683  * required after the operation is expected to be handled by the caller of
4684  * this function.
4685  **/
4686 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4687 {
4688 	struct i40e_hw *hw = &pf->hw;
4689 	u32 tx_reg;
4690 	int i;
4691 
4692 	/* warn the TX unit of coming changes */
4693 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4694 	if (!enable)
4695 		usleep_range(10, 20);
4696 
4697 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4698 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4699 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4700 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4701 			break;
4702 		usleep_range(1000, 2000);
4703 	}
4704 
4705 	/* Skip if the queue is already in the requested state */
4706 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4707 		return;
4708 
4709 	/* turn on/off the queue */
4710 	if (enable) {
4711 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4712 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4713 	} else {
4714 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4715 	}
4716 
4717 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4718 }
4719 
4720 /**
4721  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4722  * @seid: VSI SEID
4723  * @pf: the PF structure
4724  * @pf_q: the PF queue to configure
4725  * @is_xdp: true if the queue is used for XDP
4726  * @enable: start or stop the queue
4727  **/
4728 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4729 			   bool is_xdp, bool enable)
4730 {
4731 	int ret;
4732 
4733 	i40e_control_tx_q(pf, pf_q, enable);
4734 
4735 	/* wait for the change to finish */
4736 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4737 	if (ret) {
4738 		dev_info(&pf->pdev->dev,
4739 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4740 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4741 			 (enable ? "en" : "dis"));
4742 	}
4743 
4744 	return ret;
4745 }
4746 
4747 /**
4748  * i40e_vsi_enable_tx - Start a VSI's rings
4749  * @vsi: the VSI being configured
4750  **/
4751 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4752 {
4753 	struct i40e_pf *pf = vsi->back;
4754 	int i, pf_q, ret = 0;
4755 
4756 	pf_q = vsi->base_queue;
4757 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4758 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4759 					     pf_q,
4760 					     false /*is xdp*/, true);
4761 		if (ret)
4762 			break;
4763 
4764 		if (!i40e_enabled_xdp_vsi(vsi))
4765 			continue;
4766 
4767 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4768 					     pf_q + vsi->alloc_queue_pairs,
4769 					     true /*is xdp*/, true);
4770 		if (ret)
4771 			break;
4772 	}
4773 	return ret;
4774 }
4775 
4776 /**
4777  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4778  * @pf: the PF being configured
4779  * @pf_q: the PF queue
4780  * @enable: enable or disable state of the queue
4781  *
4782  * This routine will wait for the given Rx queue of the PF to reach the
4783  * enabled or disabled state.
4784  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4785  * multiple retries; else will return 0 in case of success.
4786  **/
4787 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4788 {
4789 	int i;
4790 	u32 rx_reg;
4791 
4792 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4793 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4794 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4795 			break;
4796 
4797 		usleep_range(10, 20);
4798 	}
4799 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4800 		return -ETIMEDOUT;
4801 
4802 	return 0;
4803 }
4804 
4805 /**
4806  * i40e_control_rx_q - Start or stop a particular Rx queue
4807  * @pf: the PF structure
4808  * @pf_q: the PF queue to configure
4809  * @enable: start or stop the queue
4810  *
4811  * This function enables or disables a single queue. Note that
4812  * any delay required after the operation is expected to be
4813  * handled by the caller of this function.
4814  **/
4815 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4816 {
4817 	struct i40e_hw *hw = &pf->hw;
4818 	u32 rx_reg;
4819 	int i;
4820 
4821 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4822 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4823 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4824 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4825 			break;
4826 		usleep_range(1000, 2000);
4827 	}
4828 
4829 	/* Skip if the queue is already in the requested state */
4830 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4831 		return;
4832 
4833 	/* turn on/off the queue */
4834 	if (enable)
4835 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4836 	else
4837 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4838 
4839 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4840 }
4841 
4842 /**
4843  * i40e_control_wait_rx_q
4844  * @pf: the PF structure
4845  * @pf_q: queue being configured
4846  * @enable: start or stop the rings
4847  *
4848  * This function enables or disables a single queue along with waiting
4849  * for the change to finish. The caller of this function should handle
4850  * the delays needed in the case of disabling queues.
4851  **/
4852 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4853 {
4854 	int ret = 0;
4855 
4856 	i40e_control_rx_q(pf, pf_q, enable);
4857 
4858 	/* wait for the change to finish */
4859 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4860 	if (ret)
4861 		return ret;
4862 
4863 	return ret;
4864 }
4865 
4866 /**
4867  * i40e_vsi_enable_rx - Start a VSI's rings
4868  * @vsi: the VSI being configured
4869  **/
4870 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4871 {
4872 	struct i40e_pf *pf = vsi->back;
4873 	int i, pf_q, ret = 0;
4874 
4875 	pf_q = vsi->base_queue;
4876 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4877 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4878 		if (ret) {
4879 			dev_info(&pf->pdev->dev,
4880 				 "VSI seid %d Rx ring %d enable timeout\n",
4881 				 vsi->seid, pf_q);
4882 			break;
4883 		}
4884 	}
4885 
4886 	return ret;
4887 }
4888 
4889 /**
4890  * i40e_vsi_start_rings - Start a VSI's rings
4891  * @vsi: the VSI being configured
4892  **/
4893 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4894 {
4895 	int ret = 0;
4896 
4897 	/* do rx first for enable and last for disable */
4898 	ret = i40e_vsi_enable_rx(vsi);
4899 	if (ret)
4900 		return ret;
4901 	ret = i40e_vsi_enable_tx(vsi);
4902 
4903 	return ret;
4904 }
4905 
4906 #define I40E_DISABLE_TX_GAP_MSEC	50
4907 
4908 /**
4909  * i40e_vsi_stop_rings - Stop a VSI's rings
4910  * @vsi: the VSI being configured
4911  **/
4912 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4913 {
4914 	struct i40e_pf *pf = vsi->back;
4915 	int pf_q, err, q_end;
4916 
4917 	/* When port TX is suspended, don't wait */
4918 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4919 		return i40e_vsi_stop_rings_no_wait(vsi);
4920 
4921 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4922 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4923 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4924 
4925 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4926 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4927 		if (err)
4928 			dev_info(&pf->pdev->dev,
4929 				 "VSI seid %d Rx ring %d disable timeout\n",
4930 				 vsi->seid, pf_q);
4931 	}
4932 
4933 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4934 	pf_q = vsi->base_queue;
4935 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4936 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4937 
4938 	i40e_vsi_wait_queues_disabled(vsi);
4939 }
4940 
4941 /**
4942  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4943  * @vsi: the VSI being shutdown
4944  *
4945  * This function stops all the rings for a VSI but does not delay to verify
4946  * that rings have been disabled. It is expected that the caller is shutting
4947  * down multiple VSIs at once and will delay together for all the VSIs after
4948  * initiating the shutdown. This is particularly useful for shutting down lots
4949  * of VFs together. Otherwise, a large delay can be incurred while configuring
4950  * each VSI in serial.
4951  **/
4952 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4953 {
4954 	struct i40e_pf *pf = vsi->back;
4955 	int i, pf_q;
4956 
4957 	pf_q = vsi->base_queue;
4958 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4959 		i40e_control_tx_q(pf, pf_q, false);
4960 		i40e_control_rx_q(pf, pf_q, false);
4961 	}
4962 }
4963 
4964 /**
4965  * i40e_vsi_free_irq - Free the irq association with the OS
4966  * @vsi: the VSI being configured
4967  **/
4968 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4969 {
4970 	struct i40e_pf *pf = vsi->back;
4971 	struct i40e_hw *hw = &pf->hw;
4972 	int base = vsi->base_vector;
4973 	u32 val, qp;
4974 	int i;
4975 
4976 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4977 		if (!vsi->q_vectors)
4978 			return;
4979 
4980 		if (!vsi->irqs_ready)
4981 			return;
4982 
4983 		vsi->irqs_ready = false;
4984 		for (i = 0; i < vsi->num_q_vectors; i++) {
4985 			int irq_num;
4986 			u16 vector;
4987 
4988 			vector = i + base;
4989 			irq_num = pf->msix_entries[vector].vector;
4990 
4991 			/* free only the irqs that were actually requested */
4992 			if (!vsi->q_vectors[i] ||
4993 			    !vsi->q_vectors[i]->num_ringpairs)
4994 				continue;
4995 
4996 			/* clear the affinity notifier in the IRQ descriptor */
4997 			irq_set_affinity_notifier(irq_num, NULL);
4998 			/* remove our suggested affinity mask for this IRQ */
4999 			irq_update_affinity_hint(irq_num, NULL);
5000 			free_irq(irq_num, vsi->q_vectors[i]);
5001 
5002 			/* Tear down the interrupt queue link list
5003 			 *
5004 			 * We know that they come in pairs and always
5005 			 * the Rx first, then the Tx.  To clear the
5006 			 * link list, stick the EOL value into the
5007 			 * next_q field of the registers.
5008 			 */
5009 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5010 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5011 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5012 			val |= I40E_QUEUE_END_OF_LIST
5013 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5014 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5015 
5016 			while (qp != I40E_QUEUE_END_OF_LIST) {
5017 				u32 next;
5018 
5019 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5020 
5021 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5022 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5023 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5024 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5025 
5026 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5027 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5028 
5029 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5030 
5031 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5032 
5033 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5034 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5035 
5036 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5037 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5038 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5039 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5040 
5041 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5042 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5043 
5044 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5045 				qp = next;
5046 			}
5047 		}
5048 	} else {
5049 		free_irq(pf->pdev->irq, pf);
5050 
5051 		val = rd32(hw, I40E_PFINT_LNKLST0);
5052 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5053 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5054 		val |= I40E_QUEUE_END_OF_LIST
5055 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5056 		wr32(hw, I40E_PFINT_LNKLST0, val);
5057 
5058 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5059 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5060 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5061 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5062 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5063 
5064 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5065 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5066 
5067 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5068 
5069 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5070 
5071 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5072 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5073 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5074 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5075 
5076 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5077 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5078 
5079 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5080 	}
5081 }
5082 
5083 /**
5084  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5085  * @vsi: the VSI being configured
5086  * @v_idx: Index of vector to be freed
5087  *
5088  * This function frees the memory allocated to the q_vector.  In addition if
5089  * NAPI is enabled it will delete any references to the NAPI struct prior
5090  * to freeing the q_vector.
5091  **/
5092 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5093 {
5094 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5095 	struct i40e_ring *ring;
5096 
5097 	if (!q_vector)
5098 		return;
5099 
5100 	/* disassociate q_vector from rings */
5101 	i40e_for_each_ring(ring, q_vector->tx)
5102 		ring->q_vector = NULL;
5103 
5104 	i40e_for_each_ring(ring, q_vector->rx)
5105 		ring->q_vector = NULL;
5106 
5107 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5108 	if (vsi->netdev)
5109 		netif_napi_del(&q_vector->napi);
5110 
5111 	vsi->q_vectors[v_idx] = NULL;
5112 
5113 	kfree_rcu(q_vector, rcu);
5114 }
5115 
5116 /**
5117  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5118  * @vsi: the VSI being un-configured
5119  *
5120  * This frees the memory allocated to the q_vectors and
5121  * deletes references to the NAPI struct.
5122  **/
5123 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5124 {
5125 	int v_idx;
5126 
5127 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5128 		i40e_free_q_vector(vsi, v_idx);
5129 }
5130 
5131 /**
5132  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5133  * @pf: board private structure
5134  **/
5135 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5136 {
5137 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5138 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5139 		pci_disable_msix(pf->pdev);
5140 		kfree(pf->msix_entries);
5141 		pf->msix_entries = NULL;
5142 		kfree(pf->irq_pile);
5143 		pf->irq_pile = NULL;
5144 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5145 		pci_disable_msi(pf->pdev);
5146 	}
5147 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5148 }
5149 
5150 /**
5151  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5152  * @pf: board private structure
5153  *
5154  * We go through and clear interrupt specific resources and reset the structure
5155  * to pre-load conditions
5156  **/
5157 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5158 {
5159 	int i;
5160 
5161 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5162 		i40e_free_misc_vector(pf);
5163 
5164 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5165 		      I40E_IWARP_IRQ_PILE_ID);
5166 
5167 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5168 	for (i = 0; i < pf->num_alloc_vsi; i++)
5169 		if (pf->vsi[i])
5170 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5171 	i40e_reset_interrupt_capability(pf);
5172 }
5173 
5174 /**
5175  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5176  * @vsi: the VSI being configured
5177  **/
5178 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5179 {
5180 	int q_idx;
5181 
5182 	if (!vsi->netdev)
5183 		return;
5184 
5185 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5186 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5187 
5188 		if (q_vector->rx.ring || q_vector->tx.ring)
5189 			napi_enable(&q_vector->napi);
5190 	}
5191 }
5192 
5193 /**
5194  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5195  * @vsi: the VSI being configured
5196  **/
5197 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5198 {
5199 	int q_idx;
5200 
5201 	if (!vsi->netdev)
5202 		return;
5203 
5204 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5205 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5206 
5207 		if (q_vector->rx.ring || q_vector->tx.ring)
5208 			napi_disable(&q_vector->napi);
5209 	}
5210 }
5211 
5212 /**
5213  * i40e_vsi_close - Shut down a VSI
5214  * @vsi: the vsi to be quelled
5215  **/
5216 static void i40e_vsi_close(struct i40e_vsi *vsi)
5217 {
5218 	struct i40e_pf *pf = vsi->back;
5219 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5220 		i40e_down(vsi);
5221 	i40e_vsi_free_irq(vsi);
5222 	i40e_vsi_free_tx_resources(vsi);
5223 	i40e_vsi_free_rx_resources(vsi);
5224 	vsi->current_netdev_flags = 0;
5225 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5226 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5227 		set_bit(__I40E_CLIENT_RESET, pf->state);
5228 }
5229 
5230 /**
5231  * i40e_quiesce_vsi - Pause a given VSI
5232  * @vsi: the VSI being paused
5233  **/
5234 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5235 {
5236 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5237 		return;
5238 
5239 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5240 	if (vsi->netdev && netif_running(vsi->netdev))
5241 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5242 	else
5243 		i40e_vsi_close(vsi);
5244 }
5245 
5246 /**
5247  * i40e_unquiesce_vsi - Resume a given VSI
5248  * @vsi: the VSI being resumed
5249  **/
5250 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5251 {
5252 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5253 		return;
5254 
5255 	if (vsi->netdev && netif_running(vsi->netdev))
5256 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5257 	else
5258 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5259 }
5260 
5261 /**
5262  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5263  * @pf: the PF
5264  **/
5265 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5266 {
5267 	int v;
5268 
5269 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5270 		if (pf->vsi[v])
5271 			i40e_quiesce_vsi(pf->vsi[v]);
5272 	}
5273 }
5274 
5275 /**
5276  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5277  * @pf: the PF
5278  **/
5279 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5280 {
5281 	int v;
5282 
5283 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5284 		if (pf->vsi[v])
5285 			i40e_unquiesce_vsi(pf->vsi[v]);
5286 	}
5287 }
5288 
5289 /**
5290  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5291  * @vsi: the VSI being configured
5292  *
5293  * Wait until all queues on a given VSI have been disabled.
5294  **/
5295 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5296 {
5297 	struct i40e_pf *pf = vsi->back;
5298 	int i, pf_q, ret;
5299 
5300 	pf_q = vsi->base_queue;
5301 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5302 		/* Check and wait for the Tx queue */
5303 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5304 		if (ret) {
5305 			dev_info(&pf->pdev->dev,
5306 				 "VSI seid %d Tx ring %d disable timeout\n",
5307 				 vsi->seid, pf_q);
5308 			return ret;
5309 		}
5310 
5311 		if (!i40e_enabled_xdp_vsi(vsi))
5312 			goto wait_rx;
5313 
5314 		/* Check and wait for the XDP Tx queue */
5315 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5316 				       false);
5317 		if (ret) {
5318 			dev_info(&pf->pdev->dev,
5319 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5320 				 vsi->seid, pf_q);
5321 			return ret;
5322 		}
5323 wait_rx:
5324 		/* Check and wait for the Rx queue */
5325 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5326 		if (ret) {
5327 			dev_info(&pf->pdev->dev,
5328 				 "VSI seid %d Rx ring %d disable timeout\n",
5329 				 vsi->seid, pf_q);
5330 			return ret;
5331 		}
5332 	}
5333 
5334 	return 0;
5335 }
5336 
5337 #ifdef CONFIG_I40E_DCB
5338 /**
5339  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5340  * @pf: the PF
5341  *
5342  * This function waits for the queues to be in disabled state for all the
5343  * VSIs that are managed by this PF.
5344  **/
5345 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5346 {
5347 	int v, ret = 0;
5348 
5349 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5350 		if (pf->vsi[v]) {
5351 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5352 			if (ret)
5353 				break;
5354 		}
5355 	}
5356 
5357 	return ret;
5358 }
5359 
5360 #endif
5361 
5362 /**
5363  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5364  * @pf: pointer to PF
5365  *
5366  * Get TC map for ISCSI PF type that will include iSCSI TC
5367  * and LAN TC.
5368  **/
5369 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5370 {
5371 	struct i40e_dcb_app_priority_table app;
5372 	struct i40e_hw *hw = &pf->hw;
5373 	u8 enabled_tc = 1; /* TC0 is always enabled */
5374 	u8 tc, i;
5375 	/* Get the iSCSI APP TLV */
5376 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5377 
5378 	for (i = 0; i < dcbcfg->numapps; i++) {
5379 		app = dcbcfg->app[i];
5380 		if (app.selector == I40E_APP_SEL_TCPIP &&
5381 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5382 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5383 			enabled_tc |= BIT(tc);
5384 			break;
5385 		}
5386 	}
5387 
5388 	return enabled_tc;
5389 }
5390 
5391 /**
5392  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5393  * @dcbcfg: the corresponding DCBx configuration structure
5394  *
5395  * Return the number of TCs from given DCBx configuration
5396  **/
5397 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5398 {
5399 	int i, tc_unused = 0;
5400 	u8 num_tc = 0;
5401 	u8 ret = 0;
5402 
5403 	/* Scan the ETS Config Priority Table to find
5404 	 * traffic class enabled for a given priority
5405 	 * and create a bitmask of enabled TCs
5406 	 */
5407 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5408 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5409 
5410 	/* Now scan the bitmask to check for
5411 	 * contiguous TCs starting with TC0
5412 	 */
5413 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5414 		if (num_tc & BIT(i)) {
5415 			if (!tc_unused) {
5416 				ret++;
5417 			} else {
5418 				pr_err("Non-contiguous TC - Disabling DCB\n");
5419 				return 1;
5420 			}
5421 		} else {
5422 			tc_unused = 1;
5423 		}
5424 	}
5425 
5426 	/* There is always at least TC0 */
5427 	if (!ret)
5428 		ret = 1;
5429 
5430 	return ret;
5431 }
5432 
5433 /**
5434  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5435  * @dcbcfg: the corresponding DCBx configuration structure
5436  *
5437  * Query the current DCB configuration and return the number of
5438  * traffic classes enabled from the given DCBX config
5439  **/
5440 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5441 {
5442 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5443 	u8 enabled_tc = 1;
5444 	u8 i;
5445 
5446 	for (i = 0; i < num_tc; i++)
5447 		enabled_tc |= BIT(i);
5448 
5449 	return enabled_tc;
5450 }
5451 
5452 /**
5453  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5454  * @pf: PF being queried
5455  *
5456  * Query the current MQPRIO configuration and return the number of
5457  * traffic classes enabled.
5458  **/
5459 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5460 {
5461 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5462 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5463 	u8 enabled_tc = 1, i;
5464 
5465 	for (i = 1; i < num_tc; i++)
5466 		enabled_tc |= BIT(i);
5467 	return enabled_tc;
5468 }
5469 
5470 /**
5471  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5472  * @pf: PF being queried
5473  *
5474  * Return number of traffic classes enabled for the given PF
5475  **/
5476 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5477 {
5478 	struct i40e_hw *hw = &pf->hw;
5479 	u8 i, enabled_tc = 1;
5480 	u8 num_tc = 0;
5481 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5482 
5483 	if (i40e_is_tc_mqprio_enabled(pf))
5484 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5485 
5486 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5487 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5488 		return 1;
5489 
5490 	/* SFP mode will be enabled for all TCs on port */
5491 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5492 		return i40e_dcb_get_num_tc(dcbcfg);
5493 
5494 	/* MFP mode return count of enabled TCs for this PF */
5495 	if (pf->hw.func_caps.iscsi)
5496 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5497 	else
5498 		return 1; /* Only TC0 */
5499 
5500 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5501 		if (enabled_tc & BIT(i))
5502 			num_tc++;
5503 	}
5504 	return num_tc;
5505 }
5506 
5507 /**
5508  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5509  * @pf: PF being queried
5510  *
5511  * Return a bitmap for enabled traffic classes for this PF.
5512  **/
5513 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5514 {
5515 	if (i40e_is_tc_mqprio_enabled(pf))
5516 		return i40e_mqprio_get_enabled_tc(pf);
5517 
5518 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5519 	 * default TC
5520 	 */
5521 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5522 		return I40E_DEFAULT_TRAFFIC_CLASS;
5523 
5524 	/* SFP mode we want PF to be enabled for all TCs */
5525 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5526 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5527 
5528 	/* MFP enabled and iSCSI PF type */
5529 	if (pf->hw.func_caps.iscsi)
5530 		return i40e_get_iscsi_tc_map(pf);
5531 	else
5532 		return I40E_DEFAULT_TRAFFIC_CLASS;
5533 }
5534 
5535 /**
5536  * i40e_vsi_get_bw_info - Query VSI BW Information
5537  * @vsi: the VSI being queried
5538  *
5539  * Returns 0 on success, negative value on failure
5540  **/
5541 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5542 {
5543 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5544 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5545 	struct i40e_pf *pf = vsi->back;
5546 	struct i40e_hw *hw = &pf->hw;
5547 	u32 tc_bw_max;
5548 	int ret;
5549 	int i;
5550 
5551 	/* Get the VSI level BW configuration */
5552 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5553 	if (ret) {
5554 		dev_info(&pf->pdev->dev,
5555 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5556 			 ERR_PTR(ret),
5557 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5558 		return -EINVAL;
5559 	}
5560 
5561 	/* Get the VSI level BW configuration per TC */
5562 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5563 					       NULL);
5564 	if (ret) {
5565 		dev_info(&pf->pdev->dev,
5566 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5567 			 ERR_PTR(ret),
5568 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5569 		return -EINVAL;
5570 	}
5571 
5572 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5573 		dev_info(&pf->pdev->dev,
5574 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5575 			 bw_config.tc_valid_bits,
5576 			 bw_ets_config.tc_valid_bits);
5577 		/* Still continuing */
5578 	}
5579 
5580 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5581 	vsi->bw_max_quanta = bw_config.max_bw;
5582 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5583 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5584 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5585 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5586 		vsi->bw_ets_limit_credits[i] =
5587 					le16_to_cpu(bw_ets_config.credits[i]);
5588 		/* 3 bits out of 4 for each TC */
5589 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5590 	}
5591 
5592 	return 0;
5593 }
5594 
5595 /**
5596  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5597  * @vsi: the VSI being configured
5598  * @enabled_tc: TC bitmap
5599  * @bw_share: BW shared credits per TC
5600  *
5601  * Returns 0 on success, negative value on failure
5602  **/
5603 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5604 				       u8 *bw_share)
5605 {
5606 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5607 	struct i40e_pf *pf = vsi->back;
5608 	int ret;
5609 	int i;
5610 
5611 	/* There is no need to reset BW when mqprio mode is on.  */
5612 	if (i40e_is_tc_mqprio_enabled(pf))
5613 		return 0;
5614 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5615 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5616 		if (ret)
5617 			dev_info(&pf->pdev->dev,
5618 				 "Failed to reset tx rate for vsi->seid %u\n",
5619 				 vsi->seid);
5620 		return ret;
5621 	}
5622 	memset(&bw_data, 0, sizeof(bw_data));
5623 	bw_data.tc_valid_bits = enabled_tc;
5624 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5625 		bw_data.tc_bw_credits[i] = bw_share[i];
5626 
5627 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5628 	if (ret) {
5629 		dev_info(&pf->pdev->dev,
5630 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5631 			 pf->hw.aq.asq_last_status);
5632 		return -EINVAL;
5633 	}
5634 
5635 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5636 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5637 
5638 	return 0;
5639 }
5640 
5641 /**
5642  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5643  * @vsi: the VSI being configured
5644  * @enabled_tc: TC map to be enabled
5645  *
5646  **/
5647 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5648 {
5649 	struct net_device *netdev = vsi->netdev;
5650 	struct i40e_pf *pf = vsi->back;
5651 	struct i40e_hw *hw = &pf->hw;
5652 	u8 netdev_tc = 0;
5653 	int i;
5654 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5655 
5656 	if (!netdev)
5657 		return;
5658 
5659 	if (!enabled_tc) {
5660 		netdev_reset_tc(netdev);
5661 		return;
5662 	}
5663 
5664 	/* Set up actual enabled TCs on the VSI */
5665 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5666 		return;
5667 
5668 	/* set per TC queues for the VSI */
5669 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5670 		/* Only set TC queues for enabled tcs
5671 		 *
5672 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5673 		 * enabled_tc bitmap would be 0x00001001; the driver
5674 		 * will set the numtc for netdev as 2 that will be
5675 		 * referenced by the netdev layer as TC 0 and 1.
5676 		 */
5677 		if (vsi->tc_config.enabled_tc & BIT(i))
5678 			netdev_set_tc_queue(netdev,
5679 					vsi->tc_config.tc_info[i].netdev_tc,
5680 					vsi->tc_config.tc_info[i].qcount,
5681 					vsi->tc_config.tc_info[i].qoffset);
5682 	}
5683 
5684 	if (i40e_is_tc_mqprio_enabled(pf))
5685 		return;
5686 
5687 	/* Assign UP2TC map for the VSI */
5688 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5689 		/* Get the actual TC# for the UP */
5690 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5691 		/* Get the mapped netdev TC# for the UP */
5692 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5693 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5694 	}
5695 }
5696 
5697 /**
5698  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5699  * @vsi: the VSI being configured
5700  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5701  **/
5702 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5703 				      struct i40e_vsi_context *ctxt)
5704 {
5705 	/* copy just the sections touched not the entire info
5706 	 * since not all sections are valid as returned by
5707 	 * update vsi params
5708 	 */
5709 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5710 	memcpy(&vsi->info.queue_mapping,
5711 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5712 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5713 	       sizeof(vsi->info.tc_mapping));
5714 }
5715 
5716 /**
5717  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5718  * @vsi: the VSI being reconfigured
5719  * @vsi_offset: offset from main VF VSI
5720  */
5721 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5722 {
5723 	struct i40e_vsi_context ctxt = {};
5724 	struct i40e_pf *pf;
5725 	struct i40e_hw *hw;
5726 	int ret;
5727 
5728 	if (!vsi)
5729 		return -EINVAL;
5730 	pf = vsi->back;
5731 	hw = &pf->hw;
5732 
5733 	ctxt.seid = vsi->seid;
5734 	ctxt.pf_num = hw->pf_id;
5735 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5736 	ctxt.uplink_seid = vsi->uplink_seid;
5737 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5738 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5739 	ctxt.info = vsi->info;
5740 
5741 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5742 				 false);
5743 	if (vsi->reconfig_rss) {
5744 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5745 				      vsi->num_queue_pairs);
5746 		ret = i40e_vsi_config_rss(vsi);
5747 		if (ret) {
5748 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5749 			return ret;
5750 		}
5751 		vsi->reconfig_rss = false;
5752 	}
5753 
5754 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5755 	if (ret) {
5756 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5757 			 ERR_PTR(ret),
5758 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5759 		return ret;
5760 	}
5761 	/* update the local VSI info with updated queue map */
5762 	i40e_vsi_update_queue_map(vsi, &ctxt);
5763 	vsi->info.valid_sections = 0;
5764 
5765 	return ret;
5766 }
5767 
5768 /**
5769  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5770  * @vsi: VSI to be configured
5771  * @enabled_tc: TC bitmap
5772  *
5773  * This configures a particular VSI for TCs that are mapped to the
5774  * given TC bitmap. It uses default bandwidth share for TCs across
5775  * VSIs to configure TC for a particular VSI.
5776  *
5777  * NOTE:
5778  * It is expected that the VSI queues have been quisced before calling
5779  * this function.
5780  **/
5781 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5782 {
5783 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5784 	struct i40e_pf *pf = vsi->back;
5785 	struct i40e_hw *hw = &pf->hw;
5786 	struct i40e_vsi_context ctxt;
5787 	int ret = 0;
5788 	int i;
5789 
5790 	/* Check if enabled_tc is same as existing or new TCs */
5791 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5792 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5793 		return ret;
5794 
5795 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5796 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5797 		if (enabled_tc & BIT(i))
5798 			bw_share[i] = 1;
5799 	}
5800 
5801 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5802 	if (ret) {
5803 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5804 
5805 		dev_info(&pf->pdev->dev,
5806 			 "Failed configuring TC map %d for VSI %d\n",
5807 			 enabled_tc, vsi->seid);
5808 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5809 						  &bw_config, NULL);
5810 		if (ret) {
5811 			dev_info(&pf->pdev->dev,
5812 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5813 				 ERR_PTR(ret),
5814 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5815 			goto out;
5816 		}
5817 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5818 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5819 
5820 			if (!valid_tc)
5821 				valid_tc = bw_config.tc_valid_bits;
5822 			/* Always enable TC0, no matter what */
5823 			valid_tc |= 1;
5824 			dev_info(&pf->pdev->dev,
5825 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5826 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5827 			enabled_tc = valid_tc;
5828 		}
5829 
5830 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5831 		if (ret) {
5832 			dev_err(&pf->pdev->dev,
5833 				"Unable to  configure TC map %d for VSI %d\n",
5834 				enabled_tc, vsi->seid);
5835 			goto out;
5836 		}
5837 	}
5838 
5839 	/* Update Queue Pairs Mapping for currently enabled UPs */
5840 	ctxt.seid = vsi->seid;
5841 	ctxt.pf_num = vsi->back->hw.pf_id;
5842 	ctxt.vf_num = 0;
5843 	ctxt.uplink_seid = vsi->uplink_seid;
5844 	ctxt.info = vsi->info;
5845 	if (i40e_is_tc_mqprio_enabled(pf)) {
5846 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5847 		if (ret)
5848 			goto out;
5849 	} else {
5850 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5851 	}
5852 
5853 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5854 	 * queues changed.
5855 	 */
5856 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5857 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5858 				      vsi->num_queue_pairs);
5859 		ret = i40e_vsi_config_rss(vsi);
5860 		if (ret) {
5861 			dev_info(&vsi->back->pdev->dev,
5862 				 "Failed to reconfig rss for num_queues\n");
5863 			return ret;
5864 		}
5865 		vsi->reconfig_rss = false;
5866 	}
5867 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5868 		ctxt.info.valid_sections |=
5869 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5870 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5871 	}
5872 
5873 	/* Update the VSI after updating the VSI queue-mapping
5874 	 * information
5875 	 */
5876 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5877 	if (ret) {
5878 		dev_info(&pf->pdev->dev,
5879 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5880 			 ERR_PTR(ret),
5881 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5882 		goto out;
5883 	}
5884 	/* update the local VSI info with updated queue map */
5885 	i40e_vsi_update_queue_map(vsi, &ctxt);
5886 	vsi->info.valid_sections = 0;
5887 
5888 	/* Update current VSI BW information */
5889 	ret = i40e_vsi_get_bw_info(vsi);
5890 	if (ret) {
5891 		dev_info(&pf->pdev->dev,
5892 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5893 			 ERR_PTR(ret),
5894 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5895 		goto out;
5896 	}
5897 
5898 	/* Update the netdev TC setup */
5899 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5900 out:
5901 	return ret;
5902 }
5903 
5904 /**
5905  * i40e_get_link_speed - Returns link speed for the interface
5906  * @vsi: VSI to be configured
5907  *
5908  **/
5909 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5910 {
5911 	struct i40e_pf *pf = vsi->back;
5912 
5913 	switch (pf->hw.phy.link_info.link_speed) {
5914 	case I40E_LINK_SPEED_40GB:
5915 		return 40000;
5916 	case I40E_LINK_SPEED_25GB:
5917 		return 25000;
5918 	case I40E_LINK_SPEED_20GB:
5919 		return 20000;
5920 	case I40E_LINK_SPEED_10GB:
5921 		return 10000;
5922 	case I40E_LINK_SPEED_1GB:
5923 		return 1000;
5924 	default:
5925 		return -EINVAL;
5926 	}
5927 }
5928 
5929 /**
5930  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5931  * @vsi: Pointer to vsi structure
5932  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5933  *
5934  * Helper function to convert units before send to set BW limit
5935  **/
5936 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5937 {
5938 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5939 		dev_warn(&vsi->back->pdev->dev,
5940 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5941 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5942 	} else {
5943 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5944 	}
5945 
5946 	return max_tx_rate;
5947 }
5948 
5949 /**
5950  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5951  * @vsi: VSI to be configured
5952  * @seid: seid of the channel/VSI
5953  * @max_tx_rate: max TX rate to be configured as BW limit
5954  *
5955  * Helper function to set BW limit for a given VSI
5956  **/
5957 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5958 {
5959 	struct i40e_pf *pf = vsi->back;
5960 	u64 credits = 0;
5961 	int speed = 0;
5962 	int ret = 0;
5963 
5964 	speed = i40e_get_link_speed(vsi);
5965 	if (max_tx_rate > speed) {
5966 		dev_err(&pf->pdev->dev,
5967 			"Invalid max tx rate %llu specified for VSI seid %d.",
5968 			max_tx_rate, seid);
5969 		return -EINVAL;
5970 	}
5971 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5972 		dev_warn(&pf->pdev->dev,
5973 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5974 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5975 	}
5976 
5977 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5978 	credits = max_tx_rate;
5979 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5980 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5981 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5982 	if (ret)
5983 		dev_err(&pf->pdev->dev,
5984 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5985 			max_tx_rate, seid, ERR_PTR(ret),
5986 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5987 	return ret;
5988 }
5989 
5990 /**
5991  * i40e_remove_queue_channels - Remove queue channels for the TCs
5992  * @vsi: VSI to be configured
5993  *
5994  * Remove queue channels for the TCs
5995  **/
5996 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5997 {
5998 	enum i40e_admin_queue_err last_aq_status;
5999 	struct i40e_cloud_filter *cfilter;
6000 	struct i40e_channel *ch, *ch_tmp;
6001 	struct i40e_pf *pf = vsi->back;
6002 	struct hlist_node *node;
6003 	int ret, i;
6004 
6005 	/* Reset rss size that was stored when reconfiguring rss for
6006 	 * channel VSIs with non-power-of-2 queue count.
6007 	 */
6008 	vsi->current_rss_size = 0;
6009 
6010 	/* perform cleanup for channels if they exist */
6011 	if (list_empty(&vsi->ch_list))
6012 		return;
6013 
6014 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6015 		struct i40e_vsi *p_vsi;
6016 
6017 		list_del(&ch->list);
6018 		p_vsi = ch->parent_vsi;
6019 		if (!p_vsi || !ch->initialized) {
6020 			kfree(ch);
6021 			continue;
6022 		}
6023 		/* Reset queue contexts */
6024 		for (i = 0; i < ch->num_queue_pairs; i++) {
6025 			struct i40e_ring *tx_ring, *rx_ring;
6026 			u16 pf_q;
6027 
6028 			pf_q = ch->base_queue + i;
6029 			tx_ring = vsi->tx_rings[pf_q];
6030 			tx_ring->ch = NULL;
6031 
6032 			rx_ring = vsi->rx_rings[pf_q];
6033 			rx_ring->ch = NULL;
6034 		}
6035 
6036 		/* Reset BW configured for this VSI via mqprio */
6037 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6038 		if (ret)
6039 			dev_info(&vsi->back->pdev->dev,
6040 				 "Failed to reset tx rate for ch->seid %u\n",
6041 				 ch->seid);
6042 
6043 		/* delete cloud filters associated with this channel */
6044 		hlist_for_each_entry_safe(cfilter, node,
6045 					  &pf->cloud_filter_list, cloud_node) {
6046 			if (cfilter->seid != ch->seid)
6047 				continue;
6048 
6049 			hash_del(&cfilter->cloud_node);
6050 			if (cfilter->dst_port)
6051 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6052 									cfilter,
6053 									false);
6054 			else
6055 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6056 								false);
6057 			last_aq_status = pf->hw.aq.asq_last_status;
6058 			if (ret)
6059 				dev_info(&pf->pdev->dev,
6060 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6061 					 ERR_PTR(ret),
6062 					 i40e_aq_str(&pf->hw, last_aq_status));
6063 			kfree(cfilter);
6064 		}
6065 
6066 		/* delete VSI from FW */
6067 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6068 					     NULL);
6069 		if (ret)
6070 			dev_err(&vsi->back->pdev->dev,
6071 				"unable to remove channel (%d) for parent VSI(%d)\n",
6072 				ch->seid, p_vsi->seid);
6073 		kfree(ch);
6074 	}
6075 	INIT_LIST_HEAD(&vsi->ch_list);
6076 }
6077 
6078 /**
6079  * i40e_get_max_queues_for_channel
6080  * @vsi: ptr to VSI to which channels are associated with
6081  *
6082  * Helper function which returns max value among the queue counts set on the
6083  * channels/TCs created.
6084  **/
6085 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6086 {
6087 	struct i40e_channel *ch, *ch_tmp;
6088 	int max = 0;
6089 
6090 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6091 		if (!ch->initialized)
6092 			continue;
6093 		if (ch->num_queue_pairs > max)
6094 			max = ch->num_queue_pairs;
6095 	}
6096 
6097 	return max;
6098 }
6099 
6100 /**
6101  * i40e_validate_num_queues - validate num_queues w.r.t channel
6102  * @pf: ptr to PF device
6103  * @num_queues: number of queues
6104  * @vsi: the parent VSI
6105  * @reconfig_rss: indicates should the RSS be reconfigured or not
6106  *
6107  * This function validates number of queues in the context of new channel
6108  * which is being established and determines if RSS should be reconfigured
6109  * or not for parent VSI.
6110  **/
6111 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6112 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6113 {
6114 	int max_ch_queues;
6115 
6116 	if (!reconfig_rss)
6117 		return -EINVAL;
6118 
6119 	*reconfig_rss = false;
6120 	if (vsi->current_rss_size) {
6121 		if (num_queues > vsi->current_rss_size) {
6122 			dev_dbg(&pf->pdev->dev,
6123 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6124 				num_queues, vsi->current_rss_size);
6125 			return -EINVAL;
6126 		} else if ((num_queues < vsi->current_rss_size) &&
6127 			   (!is_power_of_2(num_queues))) {
6128 			dev_dbg(&pf->pdev->dev,
6129 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6130 				num_queues, vsi->current_rss_size);
6131 			return -EINVAL;
6132 		}
6133 	}
6134 
6135 	if (!is_power_of_2(num_queues)) {
6136 		/* Find the max num_queues configured for channel if channel
6137 		 * exist.
6138 		 * if channel exist, then enforce 'num_queues' to be more than
6139 		 * max ever queues configured for channel.
6140 		 */
6141 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6142 		if (num_queues < max_ch_queues) {
6143 			dev_dbg(&pf->pdev->dev,
6144 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6145 				num_queues, max_ch_queues);
6146 			return -EINVAL;
6147 		}
6148 		*reconfig_rss = true;
6149 	}
6150 
6151 	return 0;
6152 }
6153 
6154 /**
6155  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6156  * @vsi: the VSI being setup
6157  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6158  *
6159  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6160  **/
6161 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6162 {
6163 	struct i40e_pf *pf = vsi->back;
6164 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6165 	struct i40e_hw *hw = &pf->hw;
6166 	int local_rss_size;
6167 	u8 *lut;
6168 	int ret;
6169 
6170 	if (!vsi->rss_size)
6171 		return -EINVAL;
6172 
6173 	if (rss_size > vsi->rss_size)
6174 		return -EINVAL;
6175 
6176 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6177 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6178 	if (!lut)
6179 		return -ENOMEM;
6180 
6181 	/* Ignoring user configured lut if there is one */
6182 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6183 
6184 	/* Use user configured hash key if there is one, otherwise
6185 	 * use default.
6186 	 */
6187 	if (vsi->rss_hkey_user)
6188 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6189 	else
6190 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6191 
6192 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6193 	if (ret) {
6194 		dev_info(&pf->pdev->dev,
6195 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6196 			 ERR_PTR(ret),
6197 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6198 		kfree(lut);
6199 		return ret;
6200 	}
6201 	kfree(lut);
6202 
6203 	/* Do the update w.r.t. storing rss_size */
6204 	if (!vsi->orig_rss_size)
6205 		vsi->orig_rss_size = vsi->rss_size;
6206 	vsi->current_rss_size = local_rss_size;
6207 
6208 	return ret;
6209 }
6210 
6211 /**
6212  * i40e_channel_setup_queue_map - Setup a channel queue map
6213  * @pf: ptr to PF device
6214  * @ctxt: VSI context structure
6215  * @ch: ptr to channel structure
6216  *
6217  * Setup queue map for a specific channel
6218  **/
6219 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6220 					 struct i40e_vsi_context *ctxt,
6221 					 struct i40e_channel *ch)
6222 {
6223 	u16 qcount, qmap, sections = 0;
6224 	u8 offset = 0;
6225 	int pow;
6226 
6227 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6228 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6229 
6230 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6231 	ch->num_queue_pairs = qcount;
6232 
6233 	/* find the next higher power-of-2 of num queue pairs */
6234 	pow = ilog2(qcount);
6235 	if (!is_power_of_2(qcount))
6236 		pow++;
6237 
6238 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6239 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6240 
6241 	/* Setup queue TC[0].qmap for given VSI context */
6242 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6243 
6244 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6245 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6246 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6247 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6248 }
6249 
6250 /**
6251  * i40e_add_channel - add a channel by adding VSI
6252  * @pf: ptr to PF device
6253  * @uplink_seid: underlying HW switching element (VEB) ID
6254  * @ch: ptr to channel structure
6255  *
6256  * Add a channel (VSI) using add_vsi and queue_map
6257  **/
6258 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6259 			    struct i40e_channel *ch)
6260 {
6261 	struct i40e_hw *hw = &pf->hw;
6262 	struct i40e_vsi_context ctxt;
6263 	u8 enabled_tc = 0x1; /* TC0 enabled */
6264 	int ret;
6265 
6266 	if (ch->type != I40E_VSI_VMDQ2) {
6267 		dev_info(&pf->pdev->dev,
6268 			 "add new vsi failed, ch->type %d\n", ch->type);
6269 		return -EINVAL;
6270 	}
6271 
6272 	memset(&ctxt, 0, sizeof(ctxt));
6273 	ctxt.pf_num = hw->pf_id;
6274 	ctxt.vf_num = 0;
6275 	ctxt.uplink_seid = uplink_seid;
6276 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6277 	if (ch->type == I40E_VSI_VMDQ2)
6278 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6279 
6280 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6281 		ctxt.info.valid_sections |=
6282 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6283 		ctxt.info.switch_id =
6284 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6285 	}
6286 
6287 	/* Set queue map for a given VSI context */
6288 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6289 
6290 	/* Now time to create VSI */
6291 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6292 	if (ret) {
6293 		dev_info(&pf->pdev->dev,
6294 			 "add new vsi failed, err %pe aq_err %s\n",
6295 			 ERR_PTR(ret),
6296 			 i40e_aq_str(&pf->hw,
6297 				     pf->hw.aq.asq_last_status));
6298 		return -ENOENT;
6299 	}
6300 
6301 	/* Success, update channel, set enabled_tc only if the channel
6302 	 * is not a macvlan
6303 	 */
6304 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6305 	ch->seid = ctxt.seid;
6306 	ch->vsi_number = ctxt.vsi_number;
6307 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6308 
6309 	/* copy just the sections touched not the entire info
6310 	 * since not all sections are valid as returned by
6311 	 * update vsi params
6312 	 */
6313 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6314 	memcpy(&ch->info.queue_mapping,
6315 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6316 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6317 	       sizeof(ctxt.info.tc_mapping));
6318 
6319 	return 0;
6320 }
6321 
6322 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6323 				  u8 *bw_share)
6324 {
6325 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6326 	int ret;
6327 	int i;
6328 
6329 	memset(&bw_data, 0, sizeof(bw_data));
6330 	bw_data.tc_valid_bits = ch->enabled_tc;
6331 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6332 		bw_data.tc_bw_credits[i] = bw_share[i];
6333 
6334 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6335 				       &bw_data, NULL);
6336 	if (ret) {
6337 		dev_info(&vsi->back->pdev->dev,
6338 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6339 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6340 		return -EINVAL;
6341 	}
6342 
6343 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6344 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6345 
6346 	return 0;
6347 }
6348 
6349 /**
6350  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6351  * @pf: ptr to PF device
6352  * @vsi: the VSI being setup
6353  * @ch: ptr to channel structure
6354  *
6355  * Configure TX rings associated with channel (VSI) since queues are being
6356  * from parent VSI.
6357  **/
6358 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6359 				       struct i40e_vsi *vsi,
6360 				       struct i40e_channel *ch)
6361 {
6362 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6363 	int ret;
6364 	int i;
6365 
6366 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6367 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6368 		if (ch->enabled_tc & BIT(i))
6369 			bw_share[i] = 1;
6370 	}
6371 
6372 	/* configure BW for new VSI */
6373 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6374 	if (ret) {
6375 		dev_info(&vsi->back->pdev->dev,
6376 			 "Failed configuring TC map %d for channel (seid %u)\n",
6377 			 ch->enabled_tc, ch->seid);
6378 		return ret;
6379 	}
6380 
6381 	for (i = 0; i < ch->num_queue_pairs; i++) {
6382 		struct i40e_ring *tx_ring, *rx_ring;
6383 		u16 pf_q;
6384 
6385 		pf_q = ch->base_queue + i;
6386 
6387 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6388 		 * context
6389 		 */
6390 		tx_ring = vsi->tx_rings[pf_q];
6391 		tx_ring->ch = ch;
6392 
6393 		/* Get the RX ring ptr */
6394 		rx_ring = vsi->rx_rings[pf_q];
6395 		rx_ring->ch = ch;
6396 	}
6397 
6398 	return 0;
6399 }
6400 
6401 /**
6402  * i40e_setup_hw_channel - setup new channel
6403  * @pf: ptr to PF device
6404  * @vsi: the VSI being setup
6405  * @ch: ptr to channel structure
6406  * @uplink_seid: underlying HW switching element (VEB) ID
6407  * @type: type of channel to be created (VMDq2/VF)
6408  *
6409  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6410  * and configures TX rings accordingly
6411  **/
6412 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6413 					struct i40e_vsi *vsi,
6414 					struct i40e_channel *ch,
6415 					u16 uplink_seid, u8 type)
6416 {
6417 	int ret;
6418 
6419 	ch->initialized = false;
6420 	ch->base_queue = vsi->next_base_queue;
6421 	ch->type = type;
6422 
6423 	/* Proceed with creation of channel (VMDq2) VSI */
6424 	ret = i40e_add_channel(pf, uplink_seid, ch);
6425 	if (ret) {
6426 		dev_info(&pf->pdev->dev,
6427 			 "failed to add_channel using uplink_seid %u\n",
6428 			 uplink_seid);
6429 		return ret;
6430 	}
6431 
6432 	/* Mark the successful creation of channel */
6433 	ch->initialized = true;
6434 
6435 	/* Reconfigure TX queues using QTX_CTL register */
6436 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6437 	if (ret) {
6438 		dev_info(&pf->pdev->dev,
6439 			 "failed to configure TX rings for channel %u\n",
6440 			 ch->seid);
6441 		return ret;
6442 	}
6443 
6444 	/* update 'next_base_queue' */
6445 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6446 	dev_dbg(&pf->pdev->dev,
6447 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6448 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6449 		ch->num_queue_pairs,
6450 		vsi->next_base_queue);
6451 	return ret;
6452 }
6453 
6454 /**
6455  * i40e_setup_channel - setup new channel using uplink element
6456  * @pf: ptr to PF device
6457  * @vsi: pointer to the VSI to set up the channel within
6458  * @ch: ptr to channel structure
6459  *
6460  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6461  * and uplink switching element (uplink_seid)
6462  **/
6463 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6464 			       struct i40e_channel *ch)
6465 {
6466 	u8 vsi_type;
6467 	u16 seid;
6468 	int ret;
6469 
6470 	if (vsi->type == I40E_VSI_MAIN) {
6471 		vsi_type = I40E_VSI_VMDQ2;
6472 	} else {
6473 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6474 			vsi->type);
6475 		return false;
6476 	}
6477 
6478 	/* underlying switching element */
6479 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6480 
6481 	/* create channel (VSI), configure TX rings */
6482 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6483 	if (ret) {
6484 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6485 		return false;
6486 	}
6487 
6488 	return ch->initialized ? true : false;
6489 }
6490 
6491 /**
6492  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6493  * @vsi: ptr to VSI which has PF backing
6494  *
6495  * Sets up switch mode correctly if it needs to be changed and perform
6496  * what are allowed modes.
6497  **/
6498 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6499 {
6500 	u8 mode;
6501 	struct i40e_pf *pf = vsi->back;
6502 	struct i40e_hw *hw = &pf->hw;
6503 	int ret;
6504 
6505 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6506 	if (ret)
6507 		return -EINVAL;
6508 
6509 	if (hw->dev_caps.switch_mode) {
6510 		/* if switch mode is set, support mode2 (non-tunneled for
6511 		 * cloud filter) for now
6512 		 */
6513 		u32 switch_mode = hw->dev_caps.switch_mode &
6514 				  I40E_SWITCH_MODE_MASK;
6515 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6516 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6517 				return 0;
6518 			dev_err(&pf->pdev->dev,
6519 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6520 				hw->dev_caps.switch_mode);
6521 			return -EINVAL;
6522 		}
6523 	}
6524 
6525 	/* Set Bit 7 to be valid */
6526 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6527 
6528 	/* Set L4type for TCP support */
6529 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6530 
6531 	/* Set cloud filter mode */
6532 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6533 
6534 	/* Prep mode field for set_switch_config */
6535 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6536 					pf->last_sw_conf_valid_flags,
6537 					mode, NULL);
6538 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6539 		dev_err(&pf->pdev->dev,
6540 			"couldn't set switch config bits, err %pe aq_err %s\n",
6541 			ERR_PTR(ret),
6542 			i40e_aq_str(hw,
6543 				    hw->aq.asq_last_status));
6544 
6545 	return ret;
6546 }
6547 
6548 /**
6549  * i40e_create_queue_channel - function to create channel
6550  * @vsi: VSI to be configured
6551  * @ch: ptr to channel (it contains channel specific params)
6552  *
6553  * This function creates channel (VSI) using num_queues specified by user,
6554  * reconfigs RSS if needed.
6555  **/
6556 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6557 			      struct i40e_channel *ch)
6558 {
6559 	struct i40e_pf *pf = vsi->back;
6560 	bool reconfig_rss;
6561 	int err;
6562 
6563 	if (!ch)
6564 		return -EINVAL;
6565 
6566 	if (!ch->num_queue_pairs) {
6567 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6568 			ch->num_queue_pairs);
6569 		return -EINVAL;
6570 	}
6571 
6572 	/* validate user requested num_queues for channel */
6573 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6574 				       &reconfig_rss);
6575 	if (err) {
6576 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6577 			 ch->num_queue_pairs);
6578 		return -EINVAL;
6579 	}
6580 
6581 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6582 	 * VSI to be added switch to VEB mode.
6583 	 */
6584 
6585 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6586 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6587 
6588 		if (vsi->type == I40E_VSI_MAIN) {
6589 			if (i40e_is_tc_mqprio_enabled(pf))
6590 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6591 			else
6592 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6593 		}
6594 		/* now onwards for main VSI, number of queues will be value
6595 		 * of TC0's queue count
6596 		 */
6597 	}
6598 
6599 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6600 	 * it should be more than num_queues
6601 	 */
6602 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6603 		dev_dbg(&pf->pdev->dev,
6604 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6605 			vsi->cnt_q_avail, ch->num_queue_pairs);
6606 		return -EINVAL;
6607 	}
6608 
6609 	/* reconfig_rss only if vsi type is MAIN_VSI */
6610 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6611 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6612 		if (err) {
6613 			dev_info(&pf->pdev->dev,
6614 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6615 				 ch->num_queue_pairs);
6616 			return -EINVAL;
6617 		}
6618 	}
6619 
6620 	if (!i40e_setup_channel(pf, vsi, ch)) {
6621 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6622 		return -EINVAL;
6623 	}
6624 
6625 	dev_info(&pf->pdev->dev,
6626 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6627 		 ch->seid, ch->num_queue_pairs);
6628 
6629 	/* configure VSI for BW limit */
6630 	if (ch->max_tx_rate) {
6631 		u64 credits = ch->max_tx_rate;
6632 
6633 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6634 			return -EINVAL;
6635 
6636 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6637 		dev_dbg(&pf->pdev->dev,
6638 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6639 			ch->max_tx_rate,
6640 			credits,
6641 			ch->seid);
6642 	}
6643 
6644 	/* in case of VF, this will be main SRIOV VSI */
6645 	ch->parent_vsi = vsi;
6646 
6647 	/* and update main_vsi's count for queue_available to use */
6648 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6649 
6650 	return 0;
6651 }
6652 
6653 /**
6654  * i40e_configure_queue_channels - Add queue channel for the given TCs
6655  * @vsi: VSI to be configured
6656  *
6657  * Configures queue channel mapping to the given TCs
6658  **/
6659 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6660 {
6661 	struct i40e_channel *ch;
6662 	u64 max_rate = 0;
6663 	int ret = 0, i;
6664 
6665 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6666 	vsi->tc_seid_map[0] = vsi->seid;
6667 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6668 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6669 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6670 			if (!ch) {
6671 				ret = -ENOMEM;
6672 				goto err_free;
6673 			}
6674 
6675 			INIT_LIST_HEAD(&ch->list);
6676 			ch->num_queue_pairs =
6677 				vsi->tc_config.tc_info[i].qcount;
6678 			ch->base_queue =
6679 				vsi->tc_config.tc_info[i].qoffset;
6680 
6681 			/* Bandwidth limit through tc interface is in bytes/s,
6682 			 * change to Mbit/s
6683 			 */
6684 			max_rate = vsi->mqprio_qopt.max_rate[i];
6685 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6686 			ch->max_tx_rate = max_rate;
6687 
6688 			list_add_tail(&ch->list, &vsi->ch_list);
6689 
6690 			ret = i40e_create_queue_channel(vsi, ch);
6691 			if (ret) {
6692 				dev_err(&vsi->back->pdev->dev,
6693 					"Failed creating queue channel with TC%d: queues %d\n",
6694 					i, ch->num_queue_pairs);
6695 				goto err_free;
6696 			}
6697 			vsi->tc_seid_map[i] = ch->seid;
6698 		}
6699 	}
6700 
6701 	/* reset to reconfigure TX queue contexts */
6702 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6703 	return ret;
6704 
6705 err_free:
6706 	i40e_remove_queue_channels(vsi);
6707 	return ret;
6708 }
6709 
6710 /**
6711  * i40e_veb_config_tc - Configure TCs for given VEB
6712  * @veb: given VEB
6713  * @enabled_tc: TC bitmap
6714  *
6715  * Configures given TC bitmap for VEB (switching) element
6716  **/
6717 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6718 {
6719 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6720 	struct i40e_pf *pf = veb->pf;
6721 	int ret = 0;
6722 	int i;
6723 
6724 	/* No TCs or already enabled TCs just return */
6725 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6726 		return ret;
6727 
6728 	bw_data.tc_valid_bits = enabled_tc;
6729 	/* bw_data.absolute_credits is not set (relative) */
6730 
6731 	/* Enable ETS TCs with equal BW Share for now */
6732 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6733 		if (enabled_tc & BIT(i))
6734 			bw_data.tc_bw_share_credits[i] = 1;
6735 	}
6736 
6737 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6738 						   &bw_data, NULL);
6739 	if (ret) {
6740 		dev_info(&pf->pdev->dev,
6741 			 "VEB bw config failed, err %pe aq_err %s\n",
6742 			 ERR_PTR(ret),
6743 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6744 		goto out;
6745 	}
6746 
6747 	/* Update the BW information */
6748 	ret = i40e_veb_get_bw_info(veb);
6749 	if (ret) {
6750 		dev_info(&pf->pdev->dev,
6751 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6752 			 ERR_PTR(ret),
6753 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6754 	}
6755 
6756 out:
6757 	return ret;
6758 }
6759 
6760 #ifdef CONFIG_I40E_DCB
6761 /**
6762  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6763  * @pf: PF struct
6764  *
6765  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6766  * the caller would've quiesce all the VSIs before calling
6767  * this function
6768  **/
6769 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6770 {
6771 	u8 tc_map = 0;
6772 	int ret;
6773 	u8 v;
6774 
6775 	/* Enable the TCs available on PF to all VEBs */
6776 	tc_map = i40e_pf_get_tc_map(pf);
6777 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6778 		return;
6779 
6780 	for (v = 0; v < I40E_MAX_VEB; v++) {
6781 		if (!pf->veb[v])
6782 			continue;
6783 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6784 		if (ret) {
6785 			dev_info(&pf->pdev->dev,
6786 				 "Failed configuring TC for VEB seid=%d\n",
6787 				 pf->veb[v]->seid);
6788 			/* Will try to configure as many components */
6789 		}
6790 	}
6791 
6792 	/* Update each VSI */
6793 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6794 		if (!pf->vsi[v])
6795 			continue;
6796 
6797 		/* - Enable all TCs for the LAN VSI
6798 		 * - For all others keep them at TC0 for now
6799 		 */
6800 		if (v == pf->lan_vsi)
6801 			tc_map = i40e_pf_get_tc_map(pf);
6802 		else
6803 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6804 
6805 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6806 		if (ret) {
6807 			dev_info(&pf->pdev->dev,
6808 				 "Failed configuring TC for VSI seid=%d\n",
6809 				 pf->vsi[v]->seid);
6810 			/* Will try to configure as many components */
6811 		} else {
6812 			/* Re-configure VSI vectors based on updated TC map */
6813 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6814 			if (pf->vsi[v]->netdev)
6815 				i40e_dcbnl_set_all(pf->vsi[v]);
6816 		}
6817 	}
6818 }
6819 
6820 /**
6821  * i40e_resume_port_tx - Resume port Tx
6822  * @pf: PF struct
6823  *
6824  * Resume a port's Tx and issue a PF reset in case of failure to
6825  * resume.
6826  **/
6827 static int i40e_resume_port_tx(struct i40e_pf *pf)
6828 {
6829 	struct i40e_hw *hw = &pf->hw;
6830 	int ret;
6831 
6832 	ret = i40e_aq_resume_port_tx(hw, NULL);
6833 	if (ret) {
6834 		dev_info(&pf->pdev->dev,
6835 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6836 			  ERR_PTR(ret),
6837 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6838 		/* Schedule PF reset to recover */
6839 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6840 		i40e_service_event_schedule(pf);
6841 	}
6842 
6843 	return ret;
6844 }
6845 
6846 /**
6847  * i40e_suspend_port_tx - Suspend port Tx
6848  * @pf: PF struct
6849  *
6850  * Suspend a port's Tx and issue a PF reset in case of failure.
6851  **/
6852 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6853 {
6854 	struct i40e_hw *hw = &pf->hw;
6855 	int ret;
6856 
6857 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6858 	if (ret) {
6859 		dev_info(&pf->pdev->dev,
6860 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6861 			 ERR_PTR(ret),
6862 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6863 		/* Schedule PF reset to recover */
6864 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6865 		i40e_service_event_schedule(pf);
6866 	}
6867 
6868 	return ret;
6869 }
6870 
6871 /**
6872  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6873  * @pf: PF being configured
6874  * @new_cfg: New DCBX configuration
6875  *
6876  * Program DCB settings into HW and reconfigure VEB/VSIs on
6877  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6878  **/
6879 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6880 				  struct i40e_dcbx_config *new_cfg)
6881 {
6882 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6883 	int ret;
6884 
6885 	/* Check if need reconfiguration */
6886 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6887 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6888 		return 0;
6889 	}
6890 
6891 	/* Config change disable all VSIs */
6892 	i40e_pf_quiesce_all_vsi(pf);
6893 
6894 	/* Copy the new config to the current config */
6895 	*old_cfg = *new_cfg;
6896 	old_cfg->etsrec = old_cfg->etscfg;
6897 	ret = i40e_set_dcb_config(&pf->hw);
6898 	if (ret) {
6899 		dev_info(&pf->pdev->dev,
6900 			 "Set DCB Config failed, err %pe aq_err %s\n",
6901 			 ERR_PTR(ret),
6902 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6903 		goto out;
6904 	}
6905 
6906 	/* Changes in configuration update VEB/VSI */
6907 	i40e_dcb_reconfigure(pf);
6908 out:
6909 	/* In case of reset do not try to resume anything */
6910 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6911 		/* Re-start the VSIs if disabled */
6912 		ret = i40e_resume_port_tx(pf);
6913 		/* In case of error no point in resuming VSIs */
6914 		if (ret)
6915 			goto err;
6916 		i40e_pf_unquiesce_all_vsi(pf);
6917 	}
6918 err:
6919 	return ret;
6920 }
6921 
6922 /**
6923  * i40e_hw_dcb_config - Program new DCBX settings into HW
6924  * @pf: PF being configured
6925  * @new_cfg: New DCBX configuration
6926  *
6927  * Program DCB settings into HW and reconfigure VEB/VSIs on
6928  * given PF
6929  **/
6930 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6931 {
6932 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6933 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6934 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6935 	struct i40e_dcbx_config *old_cfg;
6936 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6937 	struct i40e_rx_pb_config pb_cfg;
6938 	struct i40e_hw *hw = &pf->hw;
6939 	u8 num_ports = hw->num_ports;
6940 	bool need_reconfig;
6941 	int ret = -EINVAL;
6942 	u8 lltc_map = 0;
6943 	u8 tc_map = 0;
6944 	u8 new_numtc;
6945 	u8 i;
6946 
6947 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6948 	/* Un-pack information to Program ETS HW via shared API
6949 	 * numtc, tcmap
6950 	 * LLTC map
6951 	 * ETS/NON-ETS arbiter mode
6952 	 * max exponent (credit refills)
6953 	 * Total number of ports
6954 	 * PFC priority bit-map
6955 	 * Priority Table
6956 	 * BW % per TC
6957 	 * Arbiter mode between UPs sharing same TC
6958 	 * TSA table (ETS or non-ETS)
6959 	 * EEE enabled or not
6960 	 * MFS TC table
6961 	 */
6962 
6963 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6964 
6965 	memset(&ets_data, 0, sizeof(ets_data));
6966 	for (i = 0; i < new_numtc; i++) {
6967 		tc_map |= BIT(i);
6968 		switch (new_cfg->etscfg.tsatable[i]) {
6969 		case I40E_IEEE_TSA_ETS:
6970 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6971 			ets_data.tc_bw_share_credits[i] =
6972 					new_cfg->etscfg.tcbwtable[i];
6973 			break;
6974 		case I40E_IEEE_TSA_STRICT:
6975 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6976 			lltc_map |= BIT(i);
6977 			ets_data.tc_bw_share_credits[i] =
6978 					I40E_DCB_STRICT_PRIO_CREDITS;
6979 			break;
6980 		default:
6981 			/* Invalid TSA type */
6982 			need_reconfig = false;
6983 			goto out;
6984 		}
6985 	}
6986 
6987 	old_cfg = &hw->local_dcbx_config;
6988 	/* Check if need reconfiguration */
6989 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6990 
6991 	/* If needed, enable/disable frame tagging, disable all VSIs
6992 	 * and suspend port tx
6993 	 */
6994 	if (need_reconfig) {
6995 		/* Enable DCB tagging only when more than one TC */
6996 		if (new_numtc > 1)
6997 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6998 		else
6999 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7000 
7001 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7002 		/* Reconfiguration needed quiesce all VSIs */
7003 		i40e_pf_quiesce_all_vsi(pf);
7004 		ret = i40e_suspend_port_tx(pf);
7005 		if (ret)
7006 			goto err;
7007 	}
7008 
7009 	/* Configure Port ETS Tx Scheduler */
7010 	ets_data.tc_valid_bits = tc_map;
7011 	ets_data.tc_strict_priority_flags = lltc_map;
7012 	ret = i40e_aq_config_switch_comp_ets
7013 		(hw, pf->mac_seid, &ets_data,
7014 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7015 	if (ret) {
7016 		dev_info(&pf->pdev->dev,
7017 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7018 			 ERR_PTR(ret),
7019 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7020 		goto out;
7021 	}
7022 
7023 	/* Configure Rx ETS HW */
7024 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7025 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7026 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7027 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7028 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7029 				   lltc_map);
7030 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7031 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7032 				     prio_type);
7033 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7034 			       new_cfg->etscfg.prioritytable);
7035 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7036 
7037 	/* Configure Rx Packet Buffers in HW */
7038 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7039 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7040 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7041 	}
7042 
7043 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7044 					 false, new_cfg->pfc.pfcenable,
7045 					 mfs_tc, &pb_cfg);
7046 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7047 
7048 	/* Update the local Rx Packet buffer config */
7049 	pf->pb_cfg = pb_cfg;
7050 
7051 	/* Inform the FW about changes to DCB configuration */
7052 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7053 	if (ret) {
7054 		dev_info(&pf->pdev->dev,
7055 			 "DCB Updated failed, err %pe aq_err %s\n",
7056 			 ERR_PTR(ret),
7057 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7058 		goto out;
7059 	}
7060 
7061 	/* Update the port DCBx configuration */
7062 	*old_cfg = *new_cfg;
7063 
7064 	/* Changes in configuration update VEB/VSI */
7065 	i40e_dcb_reconfigure(pf);
7066 out:
7067 	/* Re-start the VSIs if disabled */
7068 	if (need_reconfig) {
7069 		ret = i40e_resume_port_tx(pf);
7070 
7071 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7072 		/* In case of error no point in resuming VSIs */
7073 		if (ret)
7074 			goto err;
7075 
7076 		/* Wait for the PF's queues to be disabled */
7077 		ret = i40e_pf_wait_queues_disabled(pf);
7078 		if (ret) {
7079 			/* Schedule PF reset to recover */
7080 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7081 			i40e_service_event_schedule(pf);
7082 			goto err;
7083 		} else {
7084 			i40e_pf_unquiesce_all_vsi(pf);
7085 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7086 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7087 		}
7088 		/* registers are set, lets apply */
7089 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7090 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7091 	}
7092 
7093 err:
7094 	return ret;
7095 }
7096 
7097 /**
7098  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7099  * @pf: PF being queried
7100  *
7101  * Set default DCB configuration in case DCB is to be done in SW.
7102  **/
7103 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7104 {
7105 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7106 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7107 	struct i40e_hw *hw = &pf->hw;
7108 	int err;
7109 
7110 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7111 		/* Update the local cached instance with TC0 ETS */
7112 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7113 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7114 		pf->tmp_cfg.etscfg.maxtcs = 0;
7115 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7116 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7117 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7118 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7119 		/* FW needs one App to configure HW */
7120 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7121 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7122 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7123 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7124 
7125 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7126 	}
7127 
7128 	memset(&ets_data, 0, sizeof(ets_data));
7129 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7130 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7131 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7132 
7133 	/* Enable ETS on the Physical port */
7134 	err = i40e_aq_config_switch_comp_ets
7135 		(hw, pf->mac_seid, &ets_data,
7136 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7137 	if (err) {
7138 		dev_info(&pf->pdev->dev,
7139 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7140 			 ERR_PTR(err),
7141 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7142 		err = -ENOENT;
7143 		goto out;
7144 	}
7145 
7146 	/* Update the local cached instance with TC0 ETS */
7147 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7148 	dcb_cfg->etscfg.cbs = 0;
7149 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7150 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7151 
7152 out:
7153 	return err;
7154 }
7155 
7156 /**
7157  * i40e_init_pf_dcb - Initialize DCB configuration
7158  * @pf: PF being configured
7159  *
7160  * Query the current DCB configuration and cache it
7161  * in the hardware structure
7162  **/
7163 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7164 {
7165 	struct i40e_hw *hw = &pf->hw;
7166 	int err;
7167 
7168 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7169 	 * Also do not enable DCBx if FW LLDP agent is disabled
7170 	 */
7171 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7172 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7173 		err = -EOPNOTSUPP;
7174 		goto out;
7175 	}
7176 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7177 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7178 		err = i40e_dcb_sw_default_config(pf);
7179 		if (err) {
7180 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7181 			goto out;
7182 		}
7183 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7184 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7185 			       DCB_CAP_DCBX_VER_IEEE;
7186 		/* at init capable but disabled */
7187 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7188 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7189 		goto out;
7190 	}
7191 	err = i40e_init_dcb(hw, true);
7192 	if (!err) {
7193 		/* Device/Function is not DCBX capable */
7194 		if ((!hw->func_caps.dcb) ||
7195 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7196 			dev_info(&pf->pdev->dev,
7197 				 "DCBX offload is not supported or is disabled for this PF.\n");
7198 		} else {
7199 			/* When status is not DISABLED then DCBX in FW */
7200 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7201 				       DCB_CAP_DCBX_VER_IEEE;
7202 
7203 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7204 			/* Enable DCB tagging only when more than one TC
7205 			 * or explicitly disable if only one TC
7206 			 */
7207 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7208 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7209 			else
7210 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7211 			dev_dbg(&pf->pdev->dev,
7212 				"DCBX offload is supported for this PF.\n");
7213 		}
7214 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7215 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7216 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7217 	} else {
7218 		dev_info(&pf->pdev->dev,
7219 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7220 			 ERR_PTR(err),
7221 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7222 	}
7223 
7224 out:
7225 	return err;
7226 }
7227 #endif /* CONFIG_I40E_DCB */
7228 
7229 /**
7230  * i40e_print_link_message - print link up or down
7231  * @vsi: the VSI for which link needs a message
7232  * @isup: true of link is up, false otherwise
7233  */
7234 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7235 {
7236 	enum i40e_aq_link_speed new_speed;
7237 	struct i40e_pf *pf = vsi->back;
7238 	char *speed = "Unknown";
7239 	char *fc = "Unknown";
7240 	char *fec = "";
7241 	char *req_fec = "";
7242 	char *an = "";
7243 
7244 	if (isup)
7245 		new_speed = pf->hw.phy.link_info.link_speed;
7246 	else
7247 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7248 
7249 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7250 		return;
7251 	vsi->current_isup = isup;
7252 	vsi->current_speed = new_speed;
7253 	if (!isup) {
7254 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7255 		return;
7256 	}
7257 
7258 	/* Warn user if link speed on NPAR enabled partition is not at
7259 	 * least 10GB
7260 	 */
7261 	if (pf->hw.func_caps.npar_enable &&
7262 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7263 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7264 		netdev_warn(vsi->netdev,
7265 			    "The partition detected link speed that is less than 10Gbps\n");
7266 
7267 	switch (pf->hw.phy.link_info.link_speed) {
7268 	case I40E_LINK_SPEED_40GB:
7269 		speed = "40 G";
7270 		break;
7271 	case I40E_LINK_SPEED_20GB:
7272 		speed = "20 G";
7273 		break;
7274 	case I40E_LINK_SPEED_25GB:
7275 		speed = "25 G";
7276 		break;
7277 	case I40E_LINK_SPEED_10GB:
7278 		speed = "10 G";
7279 		break;
7280 	case I40E_LINK_SPEED_5GB:
7281 		speed = "5 G";
7282 		break;
7283 	case I40E_LINK_SPEED_2_5GB:
7284 		speed = "2.5 G";
7285 		break;
7286 	case I40E_LINK_SPEED_1GB:
7287 		speed = "1000 M";
7288 		break;
7289 	case I40E_LINK_SPEED_100MB:
7290 		speed = "100 M";
7291 		break;
7292 	default:
7293 		break;
7294 	}
7295 
7296 	switch (pf->hw.fc.current_mode) {
7297 	case I40E_FC_FULL:
7298 		fc = "RX/TX";
7299 		break;
7300 	case I40E_FC_TX_PAUSE:
7301 		fc = "TX";
7302 		break;
7303 	case I40E_FC_RX_PAUSE:
7304 		fc = "RX";
7305 		break;
7306 	default:
7307 		fc = "None";
7308 		break;
7309 	}
7310 
7311 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7312 		req_fec = "None";
7313 		fec = "None";
7314 		an = "False";
7315 
7316 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7317 			an = "True";
7318 
7319 		if (pf->hw.phy.link_info.fec_info &
7320 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7321 			fec = "CL74 FC-FEC/BASE-R";
7322 		else if (pf->hw.phy.link_info.fec_info &
7323 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7324 			fec = "CL108 RS-FEC";
7325 
7326 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7327 		 * both RS and FC are requested
7328 		 */
7329 		if (vsi->back->hw.phy.link_info.req_fec_info &
7330 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7331 			if (vsi->back->hw.phy.link_info.req_fec_info &
7332 			    I40E_AQ_REQUEST_FEC_RS)
7333 				req_fec = "CL108 RS-FEC";
7334 			else
7335 				req_fec = "CL74 FC-FEC/BASE-R";
7336 		}
7337 		netdev_info(vsi->netdev,
7338 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7339 			    speed, req_fec, fec, an, fc);
7340 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7341 		req_fec = "None";
7342 		fec = "None";
7343 		an = "False";
7344 
7345 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7346 			an = "True";
7347 
7348 		if (pf->hw.phy.link_info.fec_info &
7349 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7350 			fec = "CL74 FC-FEC/BASE-R";
7351 
7352 		if (pf->hw.phy.link_info.req_fec_info &
7353 		    I40E_AQ_REQUEST_FEC_KR)
7354 			req_fec = "CL74 FC-FEC/BASE-R";
7355 
7356 		netdev_info(vsi->netdev,
7357 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7358 			    speed, req_fec, fec, an, fc);
7359 	} else {
7360 		netdev_info(vsi->netdev,
7361 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7362 			    speed, fc);
7363 	}
7364 
7365 }
7366 
7367 /**
7368  * i40e_up_complete - Finish the last steps of bringing up a connection
7369  * @vsi: the VSI being configured
7370  **/
7371 static int i40e_up_complete(struct i40e_vsi *vsi)
7372 {
7373 	struct i40e_pf *pf = vsi->back;
7374 	int err;
7375 
7376 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7377 		i40e_vsi_configure_msix(vsi);
7378 	else
7379 		i40e_configure_msi_and_legacy(vsi);
7380 
7381 	/* start rings */
7382 	err = i40e_vsi_start_rings(vsi);
7383 	if (err)
7384 		return err;
7385 
7386 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7387 	i40e_napi_enable_all(vsi);
7388 	i40e_vsi_enable_irq(vsi);
7389 
7390 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7391 	    (vsi->netdev)) {
7392 		i40e_print_link_message(vsi, true);
7393 		netif_tx_start_all_queues(vsi->netdev);
7394 		netif_carrier_on(vsi->netdev);
7395 	}
7396 
7397 	/* replay FDIR SB filters */
7398 	if (vsi->type == I40E_VSI_FDIR) {
7399 		/* reset fd counters */
7400 		pf->fd_add_err = 0;
7401 		pf->fd_atr_cnt = 0;
7402 		i40e_fdir_filter_restore(vsi);
7403 	}
7404 
7405 	/* On the next run of the service_task, notify any clients of the new
7406 	 * opened netdev
7407 	 */
7408 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7409 	i40e_service_event_schedule(pf);
7410 
7411 	return 0;
7412 }
7413 
7414 /**
7415  * i40e_vsi_reinit_locked - Reset the VSI
7416  * @vsi: the VSI being configured
7417  *
7418  * Rebuild the ring structs after some configuration
7419  * has changed, e.g. MTU size.
7420  **/
7421 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7422 {
7423 	struct i40e_pf *pf = vsi->back;
7424 
7425 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7426 		usleep_range(1000, 2000);
7427 	i40e_down(vsi);
7428 
7429 	i40e_up(vsi);
7430 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7431 }
7432 
7433 /**
7434  * i40e_force_link_state - Force the link status
7435  * @pf: board private structure
7436  * @is_up: whether the link state should be forced up or down
7437  **/
7438 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7439 {
7440 	struct i40e_aq_get_phy_abilities_resp abilities;
7441 	struct i40e_aq_set_phy_config config = {0};
7442 	bool non_zero_phy_type = is_up;
7443 	struct i40e_hw *hw = &pf->hw;
7444 	u64 mask;
7445 	u8 speed;
7446 	int err;
7447 
7448 	/* Card might've been put in an unstable state by other drivers
7449 	 * and applications, which causes incorrect speed values being
7450 	 * set on startup. In order to clear speed registers, we call
7451 	 * get_phy_capabilities twice, once to get initial state of
7452 	 * available speeds, and once to get current PHY config.
7453 	 */
7454 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7455 					   NULL);
7456 	if (err) {
7457 		dev_err(&pf->pdev->dev,
7458 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7459 			ERR_PTR(err),
7460 			i40e_aq_str(hw, hw->aq.asq_last_status));
7461 		return err;
7462 	}
7463 	speed = abilities.link_speed;
7464 
7465 	/* Get the current phy config */
7466 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7467 					   NULL);
7468 	if (err) {
7469 		dev_err(&pf->pdev->dev,
7470 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7471 			ERR_PTR(err),
7472 			i40e_aq_str(hw, hw->aq.asq_last_status));
7473 		return err;
7474 	}
7475 
7476 	/* If link needs to go up, but was not forced to go down,
7477 	 * and its speed values are OK, no need for a flap
7478 	 * if non_zero_phy_type was set, still need to force up
7479 	 */
7480 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7481 		non_zero_phy_type = true;
7482 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7483 		return 0;
7484 
7485 	/* To force link we need to set bits for all supported PHY types,
7486 	 * but there are now more than 32, so we need to split the bitmap
7487 	 * across two fields.
7488 	 */
7489 	mask = I40E_PHY_TYPES_BITMASK;
7490 	config.phy_type =
7491 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7492 	config.phy_type_ext =
7493 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7494 	/* Copy the old settings, except of phy_type */
7495 	config.abilities = abilities.abilities;
7496 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7497 		if (is_up)
7498 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7499 		else
7500 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7501 	}
7502 	if (abilities.link_speed != 0)
7503 		config.link_speed = abilities.link_speed;
7504 	else
7505 		config.link_speed = speed;
7506 	config.eee_capability = abilities.eee_capability;
7507 	config.eeer = abilities.eeer_val;
7508 	config.low_power_ctrl = abilities.d3_lpan;
7509 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7510 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7511 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7512 
7513 	if (err) {
7514 		dev_err(&pf->pdev->dev,
7515 			"set phy config ret =  %pe last_status =  %s\n",
7516 			ERR_PTR(err),
7517 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7518 		return err;
7519 	}
7520 
7521 	/* Update the link info */
7522 	err = i40e_update_link_info(hw);
7523 	if (err) {
7524 		/* Wait a little bit (on 40G cards it sometimes takes a really
7525 		 * long time for link to come back from the atomic reset)
7526 		 * and try once more
7527 		 */
7528 		msleep(1000);
7529 		i40e_update_link_info(hw);
7530 	}
7531 
7532 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7533 
7534 	return 0;
7535 }
7536 
7537 /**
7538  * i40e_up - Bring the connection back up after being down
7539  * @vsi: the VSI being configured
7540  **/
7541 int i40e_up(struct i40e_vsi *vsi)
7542 {
7543 	int err;
7544 
7545 	if (vsi->type == I40E_VSI_MAIN &&
7546 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7547 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7548 		i40e_force_link_state(vsi->back, true);
7549 
7550 	err = i40e_vsi_configure(vsi);
7551 	if (!err)
7552 		err = i40e_up_complete(vsi);
7553 
7554 	return err;
7555 }
7556 
7557 /**
7558  * i40e_down - Shutdown the connection processing
7559  * @vsi: the VSI being stopped
7560  **/
7561 void i40e_down(struct i40e_vsi *vsi)
7562 {
7563 	int i;
7564 
7565 	/* It is assumed that the caller of this function
7566 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7567 	 */
7568 	if (vsi->netdev) {
7569 		netif_carrier_off(vsi->netdev);
7570 		netif_tx_disable(vsi->netdev);
7571 	}
7572 	i40e_vsi_disable_irq(vsi);
7573 	i40e_vsi_stop_rings(vsi);
7574 	if (vsi->type == I40E_VSI_MAIN &&
7575 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7576 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7577 		i40e_force_link_state(vsi->back, false);
7578 	i40e_napi_disable_all(vsi);
7579 
7580 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7581 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7582 		if (i40e_enabled_xdp_vsi(vsi)) {
7583 			/* Make sure that in-progress ndo_xdp_xmit and
7584 			 * ndo_xsk_wakeup calls are completed.
7585 			 */
7586 			synchronize_rcu();
7587 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7588 		}
7589 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7590 	}
7591 
7592 }
7593 
7594 /**
7595  * i40e_validate_mqprio_qopt- validate queue mapping info
7596  * @vsi: the VSI being configured
7597  * @mqprio_qopt: queue parametrs
7598  **/
7599 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7600 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7601 {
7602 	u64 sum_max_rate = 0;
7603 	u64 max_rate = 0;
7604 	int i;
7605 
7606 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7607 	    mqprio_qopt->qopt.num_tc < 1 ||
7608 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7609 		return -EINVAL;
7610 	for (i = 0; ; i++) {
7611 		if (!mqprio_qopt->qopt.count[i])
7612 			return -EINVAL;
7613 		if (mqprio_qopt->min_rate[i]) {
7614 			dev_err(&vsi->back->pdev->dev,
7615 				"Invalid min tx rate (greater than 0) specified\n");
7616 			return -EINVAL;
7617 		}
7618 		max_rate = mqprio_qopt->max_rate[i];
7619 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7620 		sum_max_rate += max_rate;
7621 
7622 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7623 			break;
7624 		if (mqprio_qopt->qopt.offset[i + 1] !=
7625 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7626 			return -EINVAL;
7627 	}
7628 	if (vsi->num_queue_pairs <
7629 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7630 		dev_err(&vsi->back->pdev->dev,
7631 			"Failed to create traffic channel, insufficient number of queues.\n");
7632 		return -EINVAL;
7633 	}
7634 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7635 		dev_err(&vsi->back->pdev->dev,
7636 			"Invalid max tx rate specified\n");
7637 		return -EINVAL;
7638 	}
7639 	return 0;
7640 }
7641 
7642 /**
7643  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7644  * @vsi: the VSI being configured
7645  **/
7646 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7647 {
7648 	u16 qcount;
7649 	int i;
7650 
7651 	/* Only TC0 is enabled */
7652 	vsi->tc_config.numtc = 1;
7653 	vsi->tc_config.enabled_tc = 1;
7654 	qcount = min_t(int, vsi->alloc_queue_pairs,
7655 		       i40e_pf_get_max_q_per_tc(vsi->back));
7656 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7657 		/* For the TC that is not enabled set the offset to default
7658 		 * queue and allocate one queue for the given TC.
7659 		 */
7660 		vsi->tc_config.tc_info[i].qoffset = 0;
7661 		if (i == 0)
7662 			vsi->tc_config.tc_info[i].qcount = qcount;
7663 		else
7664 			vsi->tc_config.tc_info[i].qcount = 1;
7665 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7666 	}
7667 }
7668 
7669 /**
7670  * i40e_del_macvlan_filter
7671  * @hw: pointer to the HW structure
7672  * @seid: seid of the channel VSI
7673  * @macaddr: the mac address to apply as a filter
7674  * @aq_err: store the admin Q error
7675  *
7676  * This function deletes a mac filter on the channel VSI which serves as the
7677  * macvlan. Returns 0 on success.
7678  **/
7679 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7680 				   const u8 *macaddr, int *aq_err)
7681 {
7682 	struct i40e_aqc_remove_macvlan_element_data element;
7683 	int status;
7684 
7685 	memset(&element, 0, sizeof(element));
7686 	ether_addr_copy(element.mac_addr, macaddr);
7687 	element.vlan_tag = 0;
7688 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7689 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7690 	*aq_err = hw->aq.asq_last_status;
7691 
7692 	return status;
7693 }
7694 
7695 /**
7696  * i40e_add_macvlan_filter
7697  * @hw: pointer to the HW structure
7698  * @seid: seid of the channel VSI
7699  * @macaddr: the mac address to apply as a filter
7700  * @aq_err: store the admin Q error
7701  *
7702  * This function adds a mac filter on the channel VSI which serves as the
7703  * macvlan. Returns 0 on success.
7704  **/
7705 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7706 				   const u8 *macaddr, int *aq_err)
7707 {
7708 	struct i40e_aqc_add_macvlan_element_data element;
7709 	u16 cmd_flags = 0;
7710 	int status;
7711 
7712 	ether_addr_copy(element.mac_addr, macaddr);
7713 	element.vlan_tag = 0;
7714 	element.queue_number = 0;
7715 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7716 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7717 	element.flags = cpu_to_le16(cmd_flags);
7718 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7719 	*aq_err = hw->aq.asq_last_status;
7720 
7721 	return status;
7722 }
7723 
7724 /**
7725  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7726  * @vsi: the VSI we want to access
7727  * @ch: the channel we want to access
7728  */
7729 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7730 {
7731 	struct i40e_ring *tx_ring, *rx_ring;
7732 	u16 pf_q;
7733 	int i;
7734 
7735 	for (i = 0; i < ch->num_queue_pairs; i++) {
7736 		pf_q = ch->base_queue + i;
7737 		tx_ring = vsi->tx_rings[pf_q];
7738 		tx_ring->ch = NULL;
7739 		rx_ring = vsi->rx_rings[pf_q];
7740 		rx_ring->ch = NULL;
7741 	}
7742 }
7743 
7744 /**
7745  * i40e_free_macvlan_channels
7746  * @vsi: the VSI we want to access
7747  *
7748  * This function frees the Qs of the channel VSI from
7749  * the stack and also deletes the channel VSIs which
7750  * serve as macvlans.
7751  */
7752 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7753 {
7754 	struct i40e_channel *ch, *ch_tmp;
7755 	int ret;
7756 
7757 	if (list_empty(&vsi->macvlan_list))
7758 		return;
7759 
7760 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7761 		struct i40e_vsi *parent_vsi;
7762 
7763 		if (i40e_is_channel_macvlan(ch)) {
7764 			i40e_reset_ch_rings(vsi, ch);
7765 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7766 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7767 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7768 			kfree(ch->fwd);
7769 			ch->fwd = NULL;
7770 		}
7771 
7772 		list_del(&ch->list);
7773 		parent_vsi = ch->parent_vsi;
7774 		if (!parent_vsi || !ch->initialized) {
7775 			kfree(ch);
7776 			continue;
7777 		}
7778 
7779 		/* remove the VSI */
7780 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7781 					     NULL);
7782 		if (ret)
7783 			dev_err(&vsi->back->pdev->dev,
7784 				"unable to remove channel (%d) for parent VSI(%d)\n",
7785 				ch->seid, parent_vsi->seid);
7786 		kfree(ch);
7787 	}
7788 	vsi->macvlan_cnt = 0;
7789 }
7790 
7791 /**
7792  * i40e_fwd_ring_up - bring the macvlan device up
7793  * @vsi: the VSI we want to access
7794  * @vdev: macvlan netdevice
7795  * @fwd: the private fwd structure
7796  */
7797 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7798 			    struct i40e_fwd_adapter *fwd)
7799 {
7800 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7801 	int ret = 0, num_tc = 1,  i, aq_err;
7802 	struct i40e_pf *pf = vsi->back;
7803 	struct i40e_hw *hw = &pf->hw;
7804 
7805 	/* Go through the list and find an available channel */
7806 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7807 		if (!i40e_is_channel_macvlan(iter)) {
7808 			iter->fwd = fwd;
7809 			/* record configuration for macvlan interface in vdev */
7810 			for (i = 0; i < num_tc; i++)
7811 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7812 							     i,
7813 							     iter->num_queue_pairs,
7814 							     iter->base_queue);
7815 			for (i = 0; i < iter->num_queue_pairs; i++) {
7816 				struct i40e_ring *tx_ring, *rx_ring;
7817 				u16 pf_q;
7818 
7819 				pf_q = iter->base_queue + i;
7820 
7821 				/* Get to TX ring ptr */
7822 				tx_ring = vsi->tx_rings[pf_q];
7823 				tx_ring->ch = iter;
7824 
7825 				/* Get the RX ring ptr */
7826 				rx_ring = vsi->rx_rings[pf_q];
7827 				rx_ring->ch = iter;
7828 			}
7829 			ch = iter;
7830 			break;
7831 		}
7832 	}
7833 
7834 	if (!ch)
7835 		return -EINVAL;
7836 
7837 	/* Guarantee all rings are updated before we update the
7838 	 * MAC address filter.
7839 	 */
7840 	wmb();
7841 
7842 	/* Add a mac filter */
7843 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7844 	if (ret) {
7845 		/* if we cannot add the MAC rule then disable the offload */
7846 		macvlan_release_l2fw_offload(vdev);
7847 		for (i = 0; i < ch->num_queue_pairs; i++) {
7848 			struct i40e_ring *rx_ring;
7849 			u16 pf_q;
7850 
7851 			pf_q = ch->base_queue + i;
7852 			rx_ring = vsi->rx_rings[pf_q];
7853 			rx_ring->netdev = NULL;
7854 		}
7855 		dev_info(&pf->pdev->dev,
7856 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7857 			  ERR_PTR(ret),
7858 			  i40e_aq_str(hw, aq_err));
7859 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7860 	}
7861 
7862 	return ret;
7863 }
7864 
7865 /**
7866  * i40e_setup_macvlans - create the channels which will be macvlans
7867  * @vsi: the VSI we want to access
7868  * @macvlan_cnt: no. of macvlans to be setup
7869  * @qcnt: no. of Qs per macvlan
7870  * @vdev: macvlan netdevice
7871  */
7872 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7873 			       struct net_device *vdev)
7874 {
7875 	struct i40e_pf *pf = vsi->back;
7876 	struct i40e_hw *hw = &pf->hw;
7877 	struct i40e_vsi_context ctxt;
7878 	u16 sections, qmap, num_qps;
7879 	struct i40e_channel *ch;
7880 	int i, pow, ret = 0;
7881 	u8 offset = 0;
7882 
7883 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7884 		return -EINVAL;
7885 
7886 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7887 
7888 	/* find the next higher power-of-2 of num queue pairs */
7889 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7890 
7891 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7892 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7893 
7894 	/* Setup context bits for the main VSI */
7895 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7896 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7897 	memset(&ctxt, 0, sizeof(ctxt));
7898 	ctxt.seid = vsi->seid;
7899 	ctxt.pf_num = vsi->back->hw.pf_id;
7900 	ctxt.vf_num = 0;
7901 	ctxt.uplink_seid = vsi->uplink_seid;
7902 	ctxt.info = vsi->info;
7903 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7904 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7905 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7906 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7907 
7908 	/* Reconfigure RSS for main VSI with new max queue count */
7909 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7910 	ret = i40e_vsi_config_rss(vsi);
7911 	if (ret) {
7912 		dev_info(&pf->pdev->dev,
7913 			 "Failed to reconfig RSS for num_queues (%u)\n",
7914 			 vsi->rss_size);
7915 		return ret;
7916 	}
7917 	vsi->reconfig_rss = true;
7918 	dev_dbg(&vsi->back->pdev->dev,
7919 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7920 	vsi->next_base_queue = num_qps;
7921 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7922 
7923 	/* Update the VSI after updating the VSI queue-mapping
7924 	 * information
7925 	 */
7926 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7927 	if (ret) {
7928 		dev_info(&pf->pdev->dev,
7929 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7930 			 ERR_PTR(ret),
7931 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7932 		return ret;
7933 	}
7934 	/* update the local VSI info with updated queue map */
7935 	i40e_vsi_update_queue_map(vsi, &ctxt);
7936 	vsi->info.valid_sections = 0;
7937 
7938 	/* Create channels for macvlans */
7939 	INIT_LIST_HEAD(&vsi->macvlan_list);
7940 	for (i = 0; i < macvlan_cnt; i++) {
7941 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7942 		if (!ch) {
7943 			ret = -ENOMEM;
7944 			goto err_free;
7945 		}
7946 		INIT_LIST_HEAD(&ch->list);
7947 		ch->num_queue_pairs = qcnt;
7948 		if (!i40e_setup_channel(pf, vsi, ch)) {
7949 			ret = -EINVAL;
7950 			kfree(ch);
7951 			goto err_free;
7952 		}
7953 		ch->parent_vsi = vsi;
7954 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7955 		vsi->macvlan_cnt++;
7956 		list_add_tail(&ch->list, &vsi->macvlan_list);
7957 	}
7958 
7959 	return ret;
7960 
7961 err_free:
7962 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7963 	i40e_free_macvlan_channels(vsi);
7964 
7965 	return ret;
7966 }
7967 
7968 /**
7969  * i40e_fwd_add - configure macvlans
7970  * @netdev: net device to configure
7971  * @vdev: macvlan netdevice
7972  **/
7973 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7974 {
7975 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7976 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7977 	struct i40e_vsi *vsi = np->vsi;
7978 	struct i40e_pf *pf = vsi->back;
7979 	struct i40e_fwd_adapter *fwd;
7980 	int avail_macvlan, ret;
7981 
7982 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7983 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7984 		return ERR_PTR(-EINVAL);
7985 	}
7986 	if (i40e_is_tc_mqprio_enabled(pf)) {
7987 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7988 		return ERR_PTR(-EINVAL);
7989 	}
7990 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7991 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7992 		return ERR_PTR(-EINVAL);
7993 	}
7994 
7995 	/* The macvlan device has to be a single Q device so that the
7996 	 * tc_to_txq field can be reused to pick the tx queue.
7997 	 */
7998 	if (netif_is_multiqueue(vdev))
7999 		return ERR_PTR(-ERANGE);
8000 
8001 	if (!vsi->macvlan_cnt) {
8002 		/* reserve bit 0 for the pf device */
8003 		set_bit(0, vsi->fwd_bitmask);
8004 
8005 		/* Try to reserve as many queues as possible for macvlans. First
8006 		 * reserve 3/4th of max vectors, then half, then quarter and
8007 		 * calculate Qs per macvlan as you go
8008 		 */
8009 		vectors = pf->num_lan_msix;
8010 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8011 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8012 			q_per_macvlan = 4;
8013 			macvlan_cnt = (vectors - 32) / 4;
8014 		} else if (vectors <= 64 && vectors > 32) {
8015 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8016 			q_per_macvlan = 2;
8017 			macvlan_cnt = (vectors - 16) / 2;
8018 		} else if (vectors <= 32 && vectors > 16) {
8019 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8020 			q_per_macvlan = 1;
8021 			macvlan_cnt = vectors - 16;
8022 		} else if (vectors <= 16 && vectors > 8) {
8023 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8024 			q_per_macvlan = 1;
8025 			macvlan_cnt = vectors - 8;
8026 		} else {
8027 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8028 			q_per_macvlan = 1;
8029 			macvlan_cnt = vectors - 1;
8030 		}
8031 
8032 		if (macvlan_cnt == 0)
8033 			return ERR_PTR(-EBUSY);
8034 
8035 		/* Quiesce VSI queues */
8036 		i40e_quiesce_vsi(vsi);
8037 
8038 		/* sets up the macvlans but does not "enable" them */
8039 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8040 					  vdev);
8041 		if (ret)
8042 			return ERR_PTR(ret);
8043 
8044 		/* Unquiesce VSI */
8045 		i40e_unquiesce_vsi(vsi);
8046 	}
8047 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8048 					    vsi->macvlan_cnt);
8049 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8050 		return ERR_PTR(-EBUSY);
8051 
8052 	/* create the fwd struct */
8053 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8054 	if (!fwd)
8055 		return ERR_PTR(-ENOMEM);
8056 
8057 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8058 	fwd->bit_no = avail_macvlan;
8059 	netdev_set_sb_channel(vdev, avail_macvlan);
8060 	fwd->netdev = vdev;
8061 
8062 	if (!netif_running(netdev))
8063 		return fwd;
8064 
8065 	/* Set fwd ring up */
8066 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8067 	if (ret) {
8068 		/* unbind the queues and drop the subordinate channel config */
8069 		netdev_unbind_sb_channel(netdev, vdev);
8070 		netdev_set_sb_channel(vdev, 0);
8071 
8072 		kfree(fwd);
8073 		return ERR_PTR(-EINVAL);
8074 	}
8075 
8076 	return fwd;
8077 }
8078 
8079 /**
8080  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8081  * @vsi: the VSI we want to access
8082  */
8083 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8084 {
8085 	struct i40e_channel *ch, *ch_tmp;
8086 	struct i40e_pf *pf = vsi->back;
8087 	struct i40e_hw *hw = &pf->hw;
8088 	int aq_err, ret = 0;
8089 
8090 	if (list_empty(&vsi->macvlan_list))
8091 		return;
8092 
8093 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8094 		if (i40e_is_channel_macvlan(ch)) {
8095 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8096 						      i40e_channel_mac(ch),
8097 						      &aq_err);
8098 			if (!ret) {
8099 				/* Reset queue contexts */
8100 				i40e_reset_ch_rings(vsi, ch);
8101 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8102 				netdev_unbind_sb_channel(vsi->netdev,
8103 							 ch->fwd->netdev);
8104 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8105 				kfree(ch->fwd);
8106 				ch->fwd = NULL;
8107 			}
8108 		}
8109 	}
8110 }
8111 
8112 /**
8113  * i40e_fwd_del - delete macvlan interfaces
8114  * @netdev: net device to configure
8115  * @vdev: macvlan netdevice
8116  */
8117 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8118 {
8119 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8120 	struct i40e_fwd_adapter *fwd = vdev;
8121 	struct i40e_channel *ch, *ch_tmp;
8122 	struct i40e_vsi *vsi = np->vsi;
8123 	struct i40e_pf *pf = vsi->back;
8124 	struct i40e_hw *hw = &pf->hw;
8125 	int aq_err, ret = 0;
8126 
8127 	/* Find the channel associated with the macvlan and del mac filter */
8128 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8129 		if (i40e_is_channel_macvlan(ch) &&
8130 		    ether_addr_equal(i40e_channel_mac(ch),
8131 				     fwd->netdev->dev_addr)) {
8132 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8133 						      i40e_channel_mac(ch),
8134 						      &aq_err);
8135 			if (!ret) {
8136 				/* Reset queue contexts */
8137 				i40e_reset_ch_rings(vsi, ch);
8138 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8139 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8140 				netdev_set_sb_channel(fwd->netdev, 0);
8141 				kfree(ch->fwd);
8142 				ch->fwd = NULL;
8143 			} else {
8144 				dev_info(&pf->pdev->dev,
8145 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8146 					  ERR_PTR(ret),
8147 					  i40e_aq_str(hw, aq_err));
8148 			}
8149 			break;
8150 		}
8151 	}
8152 }
8153 
8154 /**
8155  * i40e_setup_tc - configure multiple traffic classes
8156  * @netdev: net device to configure
8157  * @type_data: tc offload data
8158  **/
8159 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8160 {
8161 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8162 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8163 	struct i40e_vsi *vsi = np->vsi;
8164 	struct i40e_pf *pf = vsi->back;
8165 	u8 enabled_tc = 0, num_tc, hw;
8166 	bool need_reset = false;
8167 	int old_queue_pairs;
8168 	int ret = -EINVAL;
8169 	u16 mode;
8170 	int i;
8171 
8172 	old_queue_pairs = vsi->num_queue_pairs;
8173 	num_tc = mqprio_qopt->qopt.num_tc;
8174 	hw = mqprio_qopt->qopt.hw;
8175 	mode = mqprio_qopt->mode;
8176 	if (!hw) {
8177 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8178 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8179 		goto config_tc;
8180 	}
8181 
8182 	/* Check if MFP enabled */
8183 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8184 		netdev_info(netdev,
8185 			    "Configuring TC not supported in MFP mode\n");
8186 		return ret;
8187 	}
8188 	switch (mode) {
8189 	case TC_MQPRIO_MODE_DCB:
8190 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8191 
8192 		/* Check if DCB enabled to continue */
8193 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8194 			netdev_info(netdev,
8195 				    "DCB is not enabled for adapter\n");
8196 			return ret;
8197 		}
8198 
8199 		/* Check whether tc count is within enabled limit */
8200 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8201 			netdev_info(netdev,
8202 				    "TC count greater than enabled on link for adapter\n");
8203 			return ret;
8204 		}
8205 		break;
8206 	case TC_MQPRIO_MODE_CHANNEL:
8207 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8208 			netdev_info(netdev,
8209 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8210 			return ret;
8211 		}
8212 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8213 			return ret;
8214 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8215 		if (ret)
8216 			return ret;
8217 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8218 		       sizeof(*mqprio_qopt));
8219 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8220 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8221 		break;
8222 	default:
8223 		return -EINVAL;
8224 	}
8225 
8226 config_tc:
8227 	/* Generate TC map for number of tc requested */
8228 	for (i = 0; i < num_tc; i++)
8229 		enabled_tc |= BIT(i);
8230 
8231 	/* Requesting same TC configuration as already enabled */
8232 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8233 	    mode != TC_MQPRIO_MODE_CHANNEL)
8234 		return 0;
8235 
8236 	/* Quiesce VSI queues */
8237 	i40e_quiesce_vsi(vsi);
8238 
8239 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8240 		i40e_remove_queue_channels(vsi);
8241 
8242 	/* Configure VSI for enabled TCs */
8243 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8244 	if (ret) {
8245 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8246 			    vsi->seid);
8247 		need_reset = true;
8248 		goto exit;
8249 	} else if (enabled_tc &&
8250 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8251 		netdev_info(netdev,
8252 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8253 			    vsi->tc_config.tc_info[0].qcount);
8254 		ret = -EINVAL;
8255 		need_reset = true;
8256 		goto exit;
8257 	}
8258 
8259 	dev_info(&vsi->back->pdev->dev,
8260 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8261 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8262 
8263 	if (i40e_is_tc_mqprio_enabled(pf)) {
8264 		if (vsi->mqprio_qopt.max_rate[0]) {
8265 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8266 						  vsi->mqprio_qopt.max_rate[0]);
8267 
8268 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8269 			if (!ret) {
8270 				u64 credits = max_tx_rate;
8271 
8272 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8273 				dev_dbg(&vsi->back->pdev->dev,
8274 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8275 					max_tx_rate,
8276 					credits,
8277 					vsi->seid);
8278 			} else {
8279 				need_reset = true;
8280 				goto exit;
8281 			}
8282 		}
8283 		ret = i40e_configure_queue_channels(vsi);
8284 		if (ret) {
8285 			vsi->num_queue_pairs = old_queue_pairs;
8286 			netdev_info(netdev,
8287 				    "Failed configuring queue channels\n");
8288 			need_reset = true;
8289 			goto exit;
8290 		}
8291 	}
8292 
8293 exit:
8294 	/* Reset the configuration data to defaults, only TC0 is enabled */
8295 	if (need_reset) {
8296 		i40e_vsi_set_default_tc_config(vsi);
8297 		need_reset = false;
8298 	}
8299 
8300 	/* Unquiesce VSI */
8301 	i40e_unquiesce_vsi(vsi);
8302 	return ret;
8303 }
8304 
8305 /**
8306  * i40e_set_cld_element - sets cloud filter element data
8307  * @filter: cloud filter rule
8308  * @cld: ptr to cloud filter element data
8309  *
8310  * This is helper function to copy data into cloud filter element
8311  **/
8312 static inline void
8313 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8314 		     struct i40e_aqc_cloud_filters_element_data *cld)
8315 {
8316 	u32 ipa;
8317 	int i;
8318 
8319 	memset(cld, 0, sizeof(*cld));
8320 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8321 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8322 
8323 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8324 		return;
8325 
8326 	if (filter->n_proto == ETH_P_IPV6) {
8327 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8328 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8329 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8330 
8331 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8332 		}
8333 	} else {
8334 		ipa = be32_to_cpu(filter->dst_ipv4);
8335 
8336 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8337 	}
8338 
8339 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8340 
8341 	/* tenant_id is not supported by FW now, once the support is enabled
8342 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8343 	 */
8344 	if (filter->tenant_id)
8345 		return;
8346 }
8347 
8348 /**
8349  * i40e_add_del_cloud_filter - Add/del cloud filter
8350  * @vsi: pointer to VSI
8351  * @filter: cloud filter rule
8352  * @add: if true, add, if false, delete
8353  *
8354  * Add or delete a cloud filter for a specific flow spec.
8355  * Returns 0 if the filter were successfully added.
8356  **/
8357 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8358 			      struct i40e_cloud_filter *filter, bool add)
8359 {
8360 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8361 	struct i40e_pf *pf = vsi->back;
8362 	int ret;
8363 	static const u16 flag_table[128] = {
8364 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8365 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8366 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8367 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8368 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8369 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8370 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8371 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8372 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8373 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8374 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8375 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8376 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8377 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8378 	};
8379 
8380 	if (filter->flags >= ARRAY_SIZE(flag_table))
8381 		return -EIO;
8382 
8383 	memset(&cld_filter, 0, sizeof(cld_filter));
8384 
8385 	/* copy element needed to add cloud filter from filter */
8386 	i40e_set_cld_element(filter, &cld_filter);
8387 
8388 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8389 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8390 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8391 
8392 	if (filter->n_proto == ETH_P_IPV6)
8393 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8394 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8395 	else
8396 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8397 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8398 
8399 	if (add)
8400 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8401 						&cld_filter, 1);
8402 	else
8403 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8404 						&cld_filter, 1);
8405 	if (ret)
8406 		dev_dbg(&pf->pdev->dev,
8407 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8408 			add ? "add" : "delete", filter->dst_port, ret,
8409 			pf->hw.aq.asq_last_status);
8410 	else
8411 		dev_info(&pf->pdev->dev,
8412 			 "%s cloud filter for VSI: %d\n",
8413 			 add ? "Added" : "Deleted", filter->seid);
8414 	return ret;
8415 }
8416 
8417 /**
8418  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8419  * @vsi: pointer to VSI
8420  * @filter: cloud filter rule
8421  * @add: if true, add, if false, delete
8422  *
8423  * Add or delete a cloud filter for a specific flow spec using big buffer.
8424  * Returns 0 if the filter were successfully added.
8425  **/
8426 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8427 				      struct i40e_cloud_filter *filter,
8428 				      bool add)
8429 {
8430 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8431 	struct i40e_pf *pf = vsi->back;
8432 	int ret;
8433 
8434 	/* Both (src/dst) valid mac_addr are not supported */
8435 	if ((is_valid_ether_addr(filter->dst_mac) &&
8436 	     is_valid_ether_addr(filter->src_mac)) ||
8437 	    (is_multicast_ether_addr(filter->dst_mac) &&
8438 	     is_multicast_ether_addr(filter->src_mac)))
8439 		return -EOPNOTSUPP;
8440 
8441 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8442 	 * ports are not supported via big buffer now.
8443 	 */
8444 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8445 		return -EOPNOTSUPP;
8446 
8447 	/* adding filter using src_port/src_ip is not supported at this stage */
8448 	if (filter->src_port ||
8449 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8450 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8451 		return -EOPNOTSUPP;
8452 
8453 	memset(&cld_filter, 0, sizeof(cld_filter));
8454 
8455 	/* copy element needed to add cloud filter from filter */
8456 	i40e_set_cld_element(filter, &cld_filter.element);
8457 
8458 	if (is_valid_ether_addr(filter->dst_mac) ||
8459 	    is_valid_ether_addr(filter->src_mac) ||
8460 	    is_multicast_ether_addr(filter->dst_mac) ||
8461 	    is_multicast_ether_addr(filter->src_mac)) {
8462 		/* MAC + IP : unsupported mode */
8463 		if (filter->dst_ipv4)
8464 			return -EOPNOTSUPP;
8465 
8466 		/* since we validated that L4 port must be valid before
8467 		 * we get here, start with respective "flags" value
8468 		 * and update if vlan is present or not
8469 		 */
8470 		cld_filter.element.flags =
8471 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8472 
8473 		if (filter->vlan_id) {
8474 			cld_filter.element.flags =
8475 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8476 		}
8477 
8478 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8479 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8480 		cld_filter.element.flags =
8481 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8482 		if (filter->n_proto == ETH_P_IPV6)
8483 			cld_filter.element.flags |=
8484 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8485 		else
8486 			cld_filter.element.flags |=
8487 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8488 	} else {
8489 		dev_err(&pf->pdev->dev,
8490 			"either mac or ip has to be valid for cloud filter\n");
8491 		return -EINVAL;
8492 	}
8493 
8494 	/* Now copy L4 port in Byte 6..7 in general fields */
8495 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8496 						be16_to_cpu(filter->dst_port);
8497 
8498 	if (add) {
8499 		/* Validate current device switch mode, change if necessary */
8500 		ret = i40e_validate_and_set_switch_mode(vsi);
8501 		if (ret) {
8502 			dev_err(&pf->pdev->dev,
8503 				"failed to set switch mode, ret %d\n",
8504 				ret);
8505 			return ret;
8506 		}
8507 
8508 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8509 						   &cld_filter, 1);
8510 	} else {
8511 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8512 						   &cld_filter, 1);
8513 	}
8514 
8515 	if (ret)
8516 		dev_dbg(&pf->pdev->dev,
8517 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8518 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8519 	else
8520 		dev_info(&pf->pdev->dev,
8521 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8522 			 add ? "add" : "delete", filter->seid,
8523 			 ntohs(filter->dst_port));
8524 	return ret;
8525 }
8526 
8527 /**
8528  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8529  * @vsi: Pointer to VSI
8530  * @f: Pointer to struct flow_cls_offload
8531  * @filter: Pointer to cloud filter structure
8532  *
8533  **/
8534 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8535 				 struct flow_cls_offload *f,
8536 				 struct i40e_cloud_filter *filter)
8537 {
8538 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8539 	struct flow_dissector *dissector = rule->match.dissector;
8540 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8541 	struct i40e_pf *pf = vsi->back;
8542 	u8 field_flags = 0;
8543 
8544 	if (dissector->used_keys &
8545 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8546 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8547 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8548 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8549 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8550 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8551 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8552 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8553 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8554 			dissector->used_keys);
8555 		return -EOPNOTSUPP;
8556 	}
8557 
8558 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8559 		struct flow_match_enc_keyid match;
8560 
8561 		flow_rule_match_enc_keyid(rule, &match);
8562 		if (match.mask->keyid != 0)
8563 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8564 
8565 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8566 	}
8567 
8568 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8569 		struct flow_match_basic match;
8570 
8571 		flow_rule_match_basic(rule, &match);
8572 		n_proto_key = ntohs(match.key->n_proto);
8573 		n_proto_mask = ntohs(match.mask->n_proto);
8574 
8575 		if (n_proto_key == ETH_P_ALL) {
8576 			n_proto_key = 0;
8577 			n_proto_mask = 0;
8578 		}
8579 		filter->n_proto = n_proto_key & n_proto_mask;
8580 		filter->ip_proto = match.key->ip_proto;
8581 	}
8582 
8583 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8584 		struct flow_match_eth_addrs match;
8585 
8586 		flow_rule_match_eth_addrs(rule, &match);
8587 
8588 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8589 		if (!is_zero_ether_addr(match.mask->dst)) {
8590 			if (is_broadcast_ether_addr(match.mask->dst)) {
8591 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8592 			} else {
8593 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8594 					match.mask->dst);
8595 				return -EIO;
8596 			}
8597 		}
8598 
8599 		if (!is_zero_ether_addr(match.mask->src)) {
8600 			if (is_broadcast_ether_addr(match.mask->src)) {
8601 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8602 			} else {
8603 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8604 					match.mask->src);
8605 				return -EIO;
8606 			}
8607 		}
8608 		ether_addr_copy(filter->dst_mac, match.key->dst);
8609 		ether_addr_copy(filter->src_mac, match.key->src);
8610 	}
8611 
8612 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8613 		struct flow_match_vlan match;
8614 
8615 		flow_rule_match_vlan(rule, &match);
8616 		if (match.mask->vlan_id) {
8617 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8618 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8619 
8620 			} else {
8621 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8622 					match.mask->vlan_id);
8623 				return -EIO;
8624 			}
8625 		}
8626 
8627 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8628 	}
8629 
8630 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8631 		struct flow_match_control match;
8632 
8633 		flow_rule_match_control(rule, &match);
8634 		addr_type = match.key->addr_type;
8635 	}
8636 
8637 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8638 		struct flow_match_ipv4_addrs match;
8639 
8640 		flow_rule_match_ipv4_addrs(rule, &match);
8641 		if (match.mask->dst) {
8642 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8643 				field_flags |= I40E_CLOUD_FIELD_IIP;
8644 			} else {
8645 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8646 					&match.mask->dst);
8647 				return -EIO;
8648 			}
8649 		}
8650 
8651 		if (match.mask->src) {
8652 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8653 				field_flags |= I40E_CLOUD_FIELD_IIP;
8654 			} else {
8655 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8656 					&match.mask->src);
8657 				return -EIO;
8658 			}
8659 		}
8660 
8661 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8662 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8663 			return -EIO;
8664 		}
8665 		filter->dst_ipv4 = match.key->dst;
8666 		filter->src_ipv4 = match.key->src;
8667 	}
8668 
8669 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8670 		struct flow_match_ipv6_addrs match;
8671 
8672 		flow_rule_match_ipv6_addrs(rule, &match);
8673 
8674 		/* src and dest IPV6 address should not be LOOPBACK
8675 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8676 		 */
8677 		if (ipv6_addr_loopback(&match.key->dst) ||
8678 		    ipv6_addr_loopback(&match.key->src)) {
8679 			dev_err(&pf->pdev->dev,
8680 				"Bad ipv6, addr is LOOPBACK\n");
8681 			return -EIO;
8682 		}
8683 		if (!ipv6_addr_any(&match.mask->dst) ||
8684 		    !ipv6_addr_any(&match.mask->src))
8685 			field_flags |= I40E_CLOUD_FIELD_IIP;
8686 
8687 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8688 		       sizeof(filter->src_ipv6));
8689 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8690 		       sizeof(filter->dst_ipv6));
8691 	}
8692 
8693 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8694 		struct flow_match_ports match;
8695 
8696 		flow_rule_match_ports(rule, &match);
8697 		if (match.mask->src) {
8698 			if (match.mask->src == cpu_to_be16(0xffff)) {
8699 				field_flags |= I40E_CLOUD_FIELD_IIP;
8700 			} else {
8701 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8702 					be16_to_cpu(match.mask->src));
8703 				return -EIO;
8704 			}
8705 		}
8706 
8707 		if (match.mask->dst) {
8708 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8709 				field_flags |= I40E_CLOUD_FIELD_IIP;
8710 			} else {
8711 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8712 					be16_to_cpu(match.mask->dst));
8713 				return -EIO;
8714 			}
8715 		}
8716 
8717 		filter->dst_port = match.key->dst;
8718 		filter->src_port = match.key->src;
8719 
8720 		switch (filter->ip_proto) {
8721 		case IPPROTO_TCP:
8722 		case IPPROTO_UDP:
8723 			break;
8724 		default:
8725 			dev_err(&pf->pdev->dev,
8726 				"Only UDP and TCP transport are supported\n");
8727 			return -EINVAL;
8728 		}
8729 	}
8730 	filter->flags = field_flags;
8731 	return 0;
8732 }
8733 
8734 /**
8735  * i40e_handle_tclass: Forward to a traffic class on the device
8736  * @vsi: Pointer to VSI
8737  * @tc: traffic class index on the device
8738  * @filter: Pointer to cloud filter structure
8739  *
8740  **/
8741 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8742 			      struct i40e_cloud_filter *filter)
8743 {
8744 	struct i40e_channel *ch, *ch_tmp;
8745 
8746 	/* direct to a traffic class on the same device */
8747 	if (tc == 0) {
8748 		filter->seid = vsi->seid;
8749 		return 0;
8750 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8751 		if (!filter->dst_port) {
8752 			dev_err(&vsi->back->pdev->dev,
8753 				"Specify destination port to direct to traffic class that is not default\n");
8754 			return -EINVAL;
8755 		}
8756 		if (list_empty(&vsi->ch_list))
8757 			return -EINVAL;
8758 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8759 					 list) {
8760 			if (ch->seid == vsi->tc_seid_map[tc])
8761 				filter->seid = ch->seid;
8762 		}
8763 		return 0;
8764 	}
8765 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8766 	return -EINVAL;
8767 }
8768 
8769 /**
8770  * i40e_configure_clsflower - Configure tc flower filters
8771  * @vsi: Pointer to VSI
8772  * @cls_flower: Pointer to struct flow_cls_offload
8773  *
8774  **/
8775 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8776 				    struct flow_cls_offload *cls_flower)
8777 {
8778 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8779 	struct i40e_cloud_filter *filter = NULL;
8780 	struct i40e_pf *pf = vsi->back;
8781 	int err = 0;
8782 
8783 	if (tc < 0) {
8784 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8785 		return -EOPNOTSUPP;
8786 	}
8787 
8788 	if (!tc) {
8789 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8790 		return -EINVAL;
8791 	}
8792 
8793 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8794 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8795 		return -EBUSY;
8796 
8797 	if (pf->fdir_pf_active_filters ||
8798 	    (!hlist_empty(&pf->fdir_filter_list))) {
8799 		dev_err(&vsi->back->pdev->dev,
8800 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8801 		return -EINVAL;
8802 	}
8803 
8804 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8805 		dev_err(&vsi->back->pdev->dev,
8806 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8807 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8808 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8809 	}
8810 
8811 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8812 	if (!filter)
8813 		return -ENOMEM;
8814 
8815 	filter->cookie = cls_flower->cookie;
8816 
8817 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8818 	if (err < 0)
8819 		goto err;
8820 
8821 	err = i40e_handle_tclass(vsi, tc, filter);
8822 	if (err < 0)
8823 		goto err;
8824 
8825 	/* Add cloud filter */
8826 	if (filter->dst_port)
8827 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8828 	else
8829 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8830 
8831 	if (err) {
8832 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8833 			err);
8834 		goto err;
8835 	}
8836 
8837 	/* add filter to the ordered list */
8838 	INIT_HLIST_NODE(&filter->cloud_node);
8839 
8840 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8841 
8842 	pf->num_cloud_filters++;
8843 
8844 	return err;
8845 err:
8846 	kfree(filter);
8847 	return err;
8848 }
8849 
8850 /**
8851  * i40e_find_cloud_filter - Find the could filter in the list
8852  * @vsi: Pointer to VSI
8853  * @cookie: filter specific cookie
8854  *
8855  **/
8856 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8857 							unsigned long *cookie)
8858 {
8859 	struct i40e_cloud_filter *filter = NULL;
8860 	struct hlist_node *node2;
8861 
8862 	hlist_for_each_entry_safe(filter, node2,
8863 				  &vsi->back->cloud_filter_list, cloud_node)
8864 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8865 			return filter;
8866 	return NULL;
8867 }
8868 
8869 /**
8870  * i40e_delete_clsflower - Remove tc flower filters
8871  * @vsi: Pointer to VSI
8872  * @cls_flower: Pointer to struct flow_cls_offload
8873  *
8874  **/
8875 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8876 				 struct flow_cls_offload *cls_flower)
8877 {
8878 	struct i40e_cloud_filter *filter = NULL;
8879 	struct i40e_pf *pf = vsi->back;
8880 	int err = 0;
8881 
8882 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8883 
8884 	if (!filter)
8885 		return -EINVAL;
8886 
8887 	hash_del(&filter->cloud_node);
8888 
8889 	if (filter->dst_port)
8890 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8891 	else
8892 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8893 
8894 	kfree(filter);
8895 	if (err) {
8896 		dev_err(&pf->pdev->dev,
8897 			"Failed to delete cloud filter, err %pe\n",
8898 			ERR_PTR(err));
8899 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8900 	}
8901 
8902 	pf->num_cloud_filters--;
8903 	if (!pf->num_cloud_filters)
8904 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8905 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8906 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8907 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8908 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8909 		}
8910 	return 0;
8911 }
8912 
8913 /**
8914  * i40e_setup_tc_cls_flower - flower classifier offloads
8915  * @np: net device to configure
8916  * @cls_flower: offload data
8917  **/
8918 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8919 				    struct flow_cls_offload *cls_flower)
8920 {
8921 	struct i40e_vsi *vsi = np->vsi;
8922 
8923 	switch (cls_flower->command) {
8924 	case FLOW_CLS_REPLACE:
8925 		return i40e_configure_clsflower(vsi, cls_flower);
8926 	case FLOW_CLS_DESTROY:
8927 		return i40e_delete_clsflower(vsi, cls_flower);
8928 	case FLOW_CLS_STATS:
8929 		return -EOPNOTSUPP;
8930 	default:
8931 		return -EOPNOTSUPP;
8932 	}
8933 }
8934 
8935 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8936 				  void *cb_priv)
8937 {
8938 	struct i40e_netdev_priv *np = cb_priv;
8939 
8940 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8941 		return -EOPNOTSUPP;
8942 
8943 	switch (type) {
8944 	case TC_SETUP_CLSFLOWER:
8945 		return i40e_setup_tc_cls_flower(np, type_data);
8946 
8947 	default:
8948 		return -EOPNOTSUPP;
8949 	}
8950 }
8951 
8952 static LIST_HEAD(i40e_block_cb_list);
8953 
8954 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8955 			   void *type_data)
8956 {
8957 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8958 
8959 	switch (type) {
8960 	case TC_SETUP_QDISC_MQPRIO:
8961 		return i40e_setup_tc(netdev, type_data);
8962 	case TC_SETUP_BLOCK:
8963 		return flow_block_cb_setup_simple(type_data,
8964 						  &i40e_block_cb_list,
8965 						  i40e_setup_tc_block_cb,
8966 						  np, np, true);
8967 	default:
8968 		return -EOPNOTSUPP;
8969 	}
8970 }
8971 
8972 /**
8973  * i40e_open - Called when a network interface is made active
8974  * @netdev: network interface device structure
8975  *
8976  * The open entry point is called when a network interface is made
8977  * active by the system (IFF_UP).  At this point all resources needed
8978  * for transmit and receive operations are allocated, the interrupt
8979  * handler is registered with the OS, the netdev watchdog subtask is
8980  * enabled, and the stack is notified that the interface is ready.
8981  *
8982  * Returns 0 on success, negative value on failure
8983  **/
8984 int i40e_open(struct net_device *netdev)
8985 {
8986 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8987 	struct i40e_vsi *vsi = np->vsi;
8988 	struct i40e_pf *pf = vsi->back;
8989 	int err;
8990 
8991 	/* disallow open during test or if eeprom is broken */
8992 	if (test_bit(__I40E_TESTING, pf->state) ||
8993 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8994 		return -EBUSY;
8995 
8996 	netif_carrier_off(netdev);
8997 
8998 	if (i40e_force_link_state(pf, true))
8999 		return -EAGAIN;
9000 
9001 	err = i40e_vsi_open(vsi);
9002 	if (err)
9003 		return err;
9004 
9005 	/* configure global TSO hardware offload settings */
9006 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9007 						       TCP_FLAG_FIN) >> 16);
9008 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9009 						       TCP_FLAG_FIN |
9010 						       TCP_FLAG_CWR) >> 16);
9011 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9012 	udp_tunnel_get_rx_info(netdev);
9013 
9014 	return 0;
9015 }
9016 
9017 /**
9018  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9019  * @vsi: vsi structure
9020  *
9021  * This updates netdev's number of tx/rx queues
9022  *
9023  * Returns status of setting tx/rx queues
9024  **/
9025 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9026 {
9027 	int ret;
9028 
9029 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9030 					   vsi->num_queue_pairs);
9031 	if (ret)
9032 		return ret;
9033 
9034 	return netif_set_real_num_tx_queues(vsi->netdev,
9035 					    vsi->num_queue_pairs);
9036 }
9037 
9038 /**
9039  * i40e_vsi_open -
9040  * @vsi: the VSI to open
9041  *
9042  * Finish initialization of the VSI.
9043  *
9044  * Returns 0 on success, negative value on failure
9045  *
9046  * Note: expects to be called while under rtnl_lock()
9047  **/
9048 int i40e_vsi_open(struct i40e_vsi *vsi)
9049 {
9050 	struct i40e_pf *pf = vsi->back;
9051 	char int_name[I40E_INT_NAME_STR_LEN];
9052 	int err;
9053 
9054 	/* allocate descriptors */
9055 	err = i40e_vsi_setup_tx_resources(vsi);
9056 	if (err)
9057 		goto err_setup_tx;
9058 	err = i40e_vsi_setup_rx_resources(vsi);
9059 	if (err)
9060 		goto err_setup_rx;
9061 
9062 	err = i40e_vsi_configure(vsi);
9063 	if (err)
9064 		goto err_setup_rx;
9065 
9066 	if (vsi->netdev) {
9067 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9068 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9069 		err = i40e_vsi_request_irq(vsi, int_name);
9070 		if (err)
9071 			goto err_setup_rx;
9072 
9073 		/* Notify the stack of the actual queue counts. */
9074 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9075 		if (err)
9076 			goto err_set_queues;
9077 
9078 	} else if (vsi->type == I40E_VSI_FDIR) {
9079 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9080 			 dev_driver_string(&pf->pdev->dev),
9081 			 dev_name(&pf->pdev->dev));
9082 		err = i40e_vsi_request_irq(vsi, int_name);
9083 		if (err)
9084 			goto err_setup_rx;
9085 
9086 	} else {
9087 		err = -EINVAL;
9088 		goto err_setup_rx;
9089 	}
9090 
9091 	err = i40e_up_complete(vsi);
9092 	if (err)
9093 		goto err_up_complete;
9094 
9095 	return 0;
9096 
9097 err_up_complete:
9098 	i40e_down(vsi);
9099 err_set_queues:
9100 	i40e_vsi_free_irq(vsi);
9101 err_setup_rx:
9102 	i40e_vsi_free_rx_resources(vsi);
9103 err_setup_tx:
9104 	i40e_vsi_free_tx_resources(vsi);
9105 	if (vsi == pf->vsi[pf->lan_vsi])
9106 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9107 
9108 	return err;
9109 }
9110 
9111 /**
9112  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9113  * @pf: Pointer to PF
9114  *
9115  * This function destroys the hlist where all the Flow Director
9116  * filters were saved.
9117  **/
9118 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9119 {
9120 	struct i40e_fdir_filter *filter;
9121 	struct i40e_flex_pit *pit_entry, *tmp;
9122 	struct hlist_node *node2;
9123 
9124 	hlist_for_each_entry_safe(filter, node2,
9125 				  &pf->fdir_filter_list, fdir_node) {
9126 		hlist_del(&filter->fdir_node);
9127 		kfree(filter);
9128 	}
9129 
9130 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9131 		list_del(&pit_entry->list);
9132 		kfree(pit_entry);
9133 	}
9134 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9135 
9136 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9137 		list_del(&pit_entry->list);
9138 		kfree(pit_entry);
9139 	}
9140 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9141 
9142 	pf->fdir_pf_active_filters = 0;
9143 	i40e_reset_fdir_filter_cnt(pf);
9144 
9145 	/* Reprogram the default input set for TCP/IPv4 */
9146 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9147 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9148 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9149 
9150 	/* Reprogram the default input set for TCP/IPv6 */
9151 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9152 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9153 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9154 
9155 	/* Reprogram the default input set for UDP/IPv4 */
9156 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9157 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9158 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9159 
9160 	/* Reprogram the default input set for UDP/IPv6 */
9161 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9162 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9163 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9164 
9165 	/* Reprogram the default input set for SCTP/IPv4 */
9166 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9167 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9168 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9169 
9170 	/* Reprogram the default input set for SCTP/IPv6 */
9171 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9172 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9173 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9174 
9175 	/* Reprogram the default input set for Other/IPv4 */
9176 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9177 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9178 
9179 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9180 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9181 
9182 	/* Reprogram the default input set for Other/IPv6 */
9183 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9184 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9185 
9186 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9187 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9188 }
9189 
9190 /**
9191  * i40e_cloud_filter_exit - Cleans up the cloud filters
9192  * @pf: Pointer to PF
9193  *
9194  * This function destroys the hlist where all the cloud filters
9195  * were saved.
9196  **/
9197 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9198 {
9199 	struct i40e_cloud_filter *cfilter;
9200 	struct hlist_node *node;
9201 
9202 	hlist_for_each_entry_safe(cfilter, node,
9203 				  &pf->cloud_filter_list, cloud_node) {
9204 		hlist_del(&cfilter->cloud_node);
9205 		kfree(cfilter);
9206 	}
9207 	pf->num_cloud_filters = 0;
9208 
9209 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9210 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9211 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9212 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9213 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9214 	}
9215 }
9216 
9217 /**
9218  * i40e_close - Disables a network interface
9219  * @netdev: network interface device structure
9220  *
9221  * The close entry point is called when an interface is de-activated
9222  * by the OS.  The hardware is still under the driver's control, but
9223  * this netdev interface is disabled.
9224  *
9225  * Returns 0, this is not allowed to fail
9226  **/
9227 int i40e_close(struct net_device *netdev)
9228 {
9229 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9230 	struct i40e_vsi *vsi = np->vsi;
9231 
9232 	i40e_vsi_close(vsi);
9233 
9234 	return 0;
9235 }
9236 
9237 /**
9238  * i40e_do_reset - Start a PF or Core Reset sequence
9239  * @pf: board private structure
9240  * @reset_flags: which reset is requested
9241  * @lock_acquired: indicates whether or not the lock has been acquired
9242  * before this function was called.
9243  *
9244  * The essential difference in resets is that the PF Reset
9245  * doesn't clear the packet buffers, doesn't reset the PE
9246  * firmware, and doesn't bother the other PFs on the chip.
9247  **/
9248 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9249 {
9250 	u32 val;
9251 
9252 	/* do the biggest reset indicated */
9253 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9254 
9255 		/* Request a Global Reset
9256 		 *
9257 		 * This will start the chip's countdown to the actual full
9258 		 * chip reset event, and a warning interrupt to be sent
9259 		 * to all PFs, including the requestor.  Our handler
9260 		 * for the warning interrupt will deal with the shutdown
9261 		 * and recovery of the switch setup.
9262 		 */
9263 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9264 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9265 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9266 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9267 
9268 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9269 
9270 		/* Request a Core Reset
9271 		 *
9272 		 * Same as Global Reset, except does *not* include the MAC/PHY
9273 		 */
9274 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9275 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9276 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9277 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9278 		i40e_flush(&pf->hw);
9279 
9280 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9281 
9282 		/* Request a PF Reset
9283 		 *
9284 		 * Resets only the PF-specific registers
9285 		 *
9286 		 * This goes directly to the tear-down and rebuild of
9287 		 * the switch, since we need to do all the recovery as
9288 		 * for the Core Reset.
9289 		 */
9290 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9291 		i40e_handle_reset_warning(pf, lock_acquired);
9292 
9293 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9294 		/* Request a PF Reset
9295 		 *
9296 		 * Resets PF and reinitializes PFs VSI.
9297 		 */
9298 		i40e_prep_for_reset(pf);
9299 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9300 		dev_info(&pf->pdev->dev,
9301 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9302 			 "FW LLDP is disabled\n" :
9303 			 "FW LLDP is enabled\n");
9304 
9305 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9306 		int v;
9307 
9308 		/* Find the VSI(s) that requested a re-init */
9309 		dev_info(&pf->pdev->dev,
9310 			 "VSI reinit requested\n");
9311 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9312 			struct i40e_vsi *vsi = pf->vsi[v];
9313 
9314 			if (vsi != NULL &&
9315 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9316 					       vsi->state))
9317 				i40e_vsi_reinit_locked(pf->vsi[v]);
9318 		}
9319 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9320 		int v;
9321 
9322 		/* Find the VSI(s) that needs to be brought down */
9323 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9324 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9325 			struct i40e_vsi *vsi = pf->vsi[v];
9326 
9327 			if (vsi != NULL &&
9328 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9329 					       vsi->state)) {
9330 				set_bit(__I40E_VSI_DOWN, vsi->state);
9331 				i40e_down(vsi);
9332 			}
9333 		}
9334 	} else {
9335 		dev_info(&pf->pdev->dev,
9336 			 "bad reset request 0x%08x\n", reset_flags);
9337 	}
9338 }
9339 
9340 #ifdef CONFIG_I40E_DCB
9341 /**
9342  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9343  * @pf: board private structure
9344  * @old_cfg: current DCB config
9345  * @new_cfg: new DCB config
9346  **/
9347 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9348 			    struct i40e_dcbx_config *old_cfg,
9349 			    struct i40e_dcbx_config *new_cfg)
9350 {
9351 	bool need_reconfig = false;
9352 
9353 	/* Check if ETS configuration has changed */
9354 	if (memcmp(&new_cfg->etscfg,
9355 		   &old_cfg->etscfg,
9356 		   sizeof(new_cfg->etscfg))) {
9357 		/* If Priority Table has changed reconfig is needed */
9358 		if (memcmp(&new_cfg->etscfg.prioritytable,
9359 			   &old_cfg->etscfg.prioritytable,
9360 			   sizeof(new_cfg->etscfg.prioritytable))) {
9361 			need_reconfig = true;
9362 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9363 		}
9364 
9365 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9366 			   &old_cfg->etscfg.tcbwtable,
9367 			   sizeof(new_cfg->etscfg.tcbwtable)))
9368 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9369 
9370 		if (memcmp(&new_cfg->etscfg.tsatable,
9371 			   &old_cfg->etscfg.tsatable,
9372 			   sizeof(new_cfg->etscfg.tsatable)))
9373 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9374 	}
9375 
9376 	/* Check if PFC configuration has changed */
9377 	if (memcmp(&new_cfg->pfc,
9378 		   &old_cfg->pfc,
9379 		   sizeof(new_cfg->pfc))) {
9380 		need_reconfig = true;
9381 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9382 	}
9383 
9384 	/* Check if APP Table has changed */
9385 	if (memcmp(&new_cfg->app,
9386 		   &old_cfg->app,
9387 		   sizeof(new_cfg->app))) {
9388 		need_reconfig = true;
9389 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9390 	}
9391 
9392 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9393 	return need_reconfig;
9394 }
9395 
9396 /**
9397  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9398  * @pf: board private structure
9399  * @e: event info posted on ARQ
9400  **/
9401 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9402 				  struct i40e_arq_event_info *e)
9403 {
9404 	struct i40e_aqc_lldp_get_mib *mib =
9405 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9406 	struct i40e_hw *hw = &pf->hw;
9407 	struct i40e_dcbx_config tmp_dcbx_cfg;
9408 	bool need_reconfig = false;
9409 	int ret = 0;
9410 	u8 type;
9411 
9412 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9413 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9414 	    (hw->phy.link_info.link_speed &
9415 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9416 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9417 		/* let firmware decide if the DCB should be disabled */
9418 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9419 
9420 	/* Not DCB capable or capability disabled */
9421 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9422 		return ret;
9423 
9424 	/* Ignore if event is not for Nearest Bridge */
9425 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9426 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9427 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9428 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9429 		return ret;
9430 
9431 	/* Check MIB Type and return if event for Remote MIB update */
9432 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9433 	dev_dbg(&pf->pdev->dev,
9434 		"LLDP event mib type %s\n", type ? "remote" : "local");
9435 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9436 		/* Update the remote cached instance and return */
9437 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9438 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9439 				&hw->remote_dcbx_config);
9440 		goto exit;
9441 	}
9442 
9443 	/* Store the old configuration */
9444 	tmp_dcbx_cfg = hw->local_dcbx_config;
9445 
9446 	/* Reset the old DCBx configuration data */
9447 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9448 	/* Get updated DCBX data from firmware */
9449 	ret = i40e_get_dcb_config(&pf->hw);
9450 	if (ret) {
9451 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9452 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9453 		    (hw->phy.link_info.link_speed &
9454 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9455 			dev_warn(&pf->pdev->dev,
9456 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9457 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9458 		} else {
9459 			dev_info(&pf->pdev->dev,
9460 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9461 				 ERR_PTR(ret),
9462 				 i40e_aq_str(&pf->hw,
9463 					     pf->hw.aq.asq_last_status));
9464 		}
9465 		goto exit;
9466 	}
9467 
9468 	/* No change detected in DCBX configs */
9469 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9470 		    sizeof(tmp_dcbx_cfg))) {
9471 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9472 		goto exit;
9473 	}
9474 
9475 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9476 					       &hw->local_dcbx_config);
9477 
9478 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9479 
9480 	if (!need_reconfig)
9481 		goto exit;
9482 
9483 	/* Enable DCB tagging only when more than one TC */
9484 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9485 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9486 	else
9487 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9488 
9489 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9490 	/* Reconfiguration needed quiesce all VSIs */
9491 	i40e_pf_quiesce_all_vsi(pf);
9492 
9493 	/* Changes in configuration update VEB/VSI */
9494 	i40e_dcb_reconfigure(pf);
9495 
9496 	ret = i40e_resume_port_tx(pf);
9497 
9498 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9499 	/* In case of error no point in resuming VSIs */
9500 	if (ret)
9501 		goto exit;
9502 
9503 	/* Wait for the PF's queues to be disabled */
9504 	ret = i40e_pf_wait_queues_disabled(pf);
9505 	if (ret) {
9506 		/* Schedule PF reset to recover */
9507 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9508 		i40e_service_event_schedule(pf);
9509 	} else {
9510 		i40e_pf_unquiesce_all_vsi(pf);
9511 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9512 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9513 	}
9514 
9515 exit:
9516 	return ret;
9517 }
9518 #endif /* CONFIG_I40E_DCB */
9519 
9520 /**
9521  * i40e_do_reset_safe - Protected reset path for userland calls.
9522  * @pf: board private structure
9523  * @reset_flags: which reset is requested
9524  *
9525  **/
9526 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9527 {
9528 	rtnl_lock();
9529 	i40e_do_reset(pf, reset_flags, true);
9530 	rtnl_unlock();
9531 }
9532 
9533 /**
9534  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9535  * @pf: board private structure
9536  * @e: event info posted on ARQ
9537  *
9538  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9539  * and VF queues
9540  **/
9541 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9542 					   struct i40e_arq_event_info *e)
9543 {
9544 	struct i40e_aqc_lan_overflow *data =
9545 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9546 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9547 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9548 	struct i40e_hw *hw = &pf->hw;
9549 	struct i40e_vf *vf;
9550 	u16 vf_id;
9551 
9552 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9553 		queue, qtx_ctl);
9554 
9555 	/* Queue belongs to VF, find the VF and issue VF reset */
9556 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9557 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9558 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9559 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9560 		vf_id -= hw->func_caps.vf_base_id;
9561 		vf = &pf->vf[vf_id];
9562 		i40e_vc_notify_vf_reset(vf);
9563 		/* Allow VF to process pending reset notification */
9564 		msleep(20);
9565 		i40e_reset_vf(vf, false);
9566 	}
9567 }
9568 
9569 /**
9570  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9571  * @pf: board private structure
9572  **/
9573 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9574 {
9575 	u32 val, fcnt_prog;
9576 
9577 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9578 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9579 	return fcnt_prog;
9580 }
9581 
9582 /**
9583  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9584  * @pf: board private structure
9585  **/
9586 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9587 {
9588 	u32 val, fcnt_prog;
9589 
9590 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9591 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9592 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9593 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9594 	return fcnt_prog;
9595 }
9596 
9597 /**
9598  * i40e_get_global_fd_count - Get total FD filters programmed on device
9599  * @pf: board private structure
9600  **/
9601 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9602 {
9603 	u32 val, fcnt_prog;
9604 
9605 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9606 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9607 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9608 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9609 	return fcnt_prog;
9610 }
9611 
9612 /**
9613  * i40e_reenable_fdir_sb - Restore FDir SB capability
9614  * @pf: board private structure
9615  **/
9616 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9617 {
9618 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9619 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9620 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9621 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9622 }
9623 
9624 /**
9625  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9626  * @pf: board private structure
9627  **/
9628 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9629 {
9630 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9631 		/* ATR uses the same filtering logic as SB rules. It only
9632 		 * functions properly if the input set mask is at the default
9633 		 * settings. It is safe to restore the default input set
9634 		 * because there are no active TCPv4 filter rules.
9635 		 */
9636 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9637 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9638 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9639 
9640 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9641 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9642 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9643 	}
9644 }
9645 
9646 /**
9647  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9648  * @pf: board private structure
9649  * @filter: FDir filter to remove
9650  */
9651 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9652 				       struct i40e_fdir_filter *filter)
9653 {
9654 	/* Update counters */
9655 	pf->fdir_pf_active_filters--;
9656 	pf->fd_inv = 0;
9657 
9658 	switch (filter->flow_type) {
9659 	case TCP_V4_FLOW:
9660 		pf->fd_tcp4_filter_cnt--;
9661 		break;
9662 	case UDP_V4_FLOW:
9663 		pf->fd_udp4_filter_cnt--;
9664 		break;
9665 	case SCTP_V4_FLOW:
9666 		pf->fd_sctp4_filter_cnt--;
9667 		break;
9668 	case TCP_V6_FLOW:
9669 		pf->fd_tcp6_filter_cnt--;
9670 		break;
9671 	case UDP_V6_FLOW:
9672 		pf->fd_udp6_filter_cnt--;
9673 		break;
9674 	case SCTP_V6_FLOW:
9675 		pf->fd_udp6_filter_cnt--;
9676 		break;
9677 	case IP_USER_FLOW:
9678 		switch (filter->ipl4_proto) {
9679 		case IPPROTO_TCP:
9680 			pf->fd_tcp4_filter_cnt--;
9681 			break;
9682 		case IPPROTO_UDP:
9683 			pf->fd_udp4_filter_cnt--;
9684 			break;
9685 		case IPPROTO_SCTP:
9686 			pf->fd_sctp4_filter_cnt--;
9687 			break;
9688 		case IPPROTO_IP:
9689 			pf->fd_ip4_filter_cnt--;
9690 			break;
9691 		}
9692 		break;
9693 	case IPV6_USER_FLOW:
9694 		switch (filter->ipl4_proto) {
9695 		case IPPROTO_TCP:
9696 			pf->fd_tcp6_filter_cnt--;
9697 			break;
9698 		case IPPROTO_UDP:
9699 			pf->fd_udp6_filter_cnt--;
9700 			break;
9701 		case IPPROTO_SCTP:
9702 			pf->fd_sctp6_filter_cnt--;
9703 			break;
9704 		case IPPROTO_IP:
9705 			pf->fd_ip6_filter_cnt--;
9706 			break;
9707 		}
9708 		break;
9709 	}
9710 
9711 	/* Remove the filter from the list and free memory */
9712 	hlist_del(&filter->fdir_node);
9713 	kfree(filter);
9714 }
9715 
9716 /**
9717  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9718  * @pf: board private structure
9719  **/
9720 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9721 {
9722 	struct i40e_fdir_filter *filter;
9723 	u32 fcnt_prog, fcnt_avail;
9724 	struct hlist_node *node;
9725 
9726 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9727 		return;
9728 
9729 	/* Check if we have enough room to re-enable FDir SB capability. */
9730 	fcnt_prog = i40e_get_global_fd_count(pf);
9731 	fcnt_avail = pf->fdir_pf_filter_count;
9732 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9733 	    (pf->fd_add_err == 0) ||
9734 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9735 		i40e_reenable_fdir_sb(pf);
9736 
9737 	/* We should wait for even more space before re-enabling ATR.
9738 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9739 	 * rules active.
9740 	 */
9741 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9742 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9743 		i40e_reenable_fdir_atr(pf);
9744 
9745 	/* if hw had a problem adding a filter, delete it */
9746 	if (pf->fd_inv > 0) {
9747 		hlist_for_each_entry_safe(filter, node,
9748 					  &pf->fdir_filter_list, fdir_node)
9749 			if (filter->fd_id == pf->fd_inv)
9750 				i40e_delete_invalid_filter(pf, filter);
9751 	}
9752 }
9753 
9754 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9755 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9756 /**
9757  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9758  * @pf: board private structure
9759  **/
9760 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9761 {
9762 	unsigned long min_flush_time;
9763 	int flush_wait_retry = 50;
9764 	bool disable_atr = false;
9765 	int fd_room;
9766 	int reg;
9767 
9768 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9769 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9770 		return;
9771 
9772 	/* If the flush is happening too quick and we have mostly SB rules we
9773 	 * should not re-enable ATR for some time.
9774 	 */
9775 	min_flush_time = pf->fd_flush_timestamp +
9776 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9777 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9778 
9779 	if (!(time_after(jiffies, min_flush_time)) &&
9780 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9781 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9782 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9783 		disable_atr = true;
9784 	}
9785 
9786 	pf->fd_flush_timestamp = jiffies;
9787 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9788 	/* flush all filters */
9789 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9790 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9791 	i40e_flush(&pf->hw);
9792 	pf->fd_flush_cnt++;
9793 	pf->fd_add_err = 0;
9794 	do {
9795 		/* Check FD flush status every 5-6msec */
9796 		usleep_range(5000, 6000);
9797 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9798 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9799 			break;
9800 	} while (flush_wait_retry--);
9801 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9802 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9803 	} else {
9804 		/* replay sideband filters */
9805 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9806 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9807 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9808 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9809 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9810 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9811 	}
9812 }
9813 
9814 /**
9815  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9816  * @pf: board private structure
9817  **/
9818 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9819 {
9820 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9821 }
9822 
9823 /**
9824  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9825  * @pf: board private structure
9826  **/
9827 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9828 {
9829 
9830 	/* if interface is down do nothing */
9831 	if (test_bit(__I40E_DOWN, pf->state))
9832 		return;
9833 
9834 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9835 		i40e_fdir_flush_and_replay(pf);
9836 
9837 	i40e_fdir_check_and_reenable(pf);
9838 
9839 }
9840 
9841 /**
9842  * i40e_vsi_link_event - notify VSI of a link event
9843  * @vsi: vsi to be notified
9844  * @link_up: link up or down
9845  **/
9846 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9847 {
9848 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9849 		return;
9850 
9851 	switch (vsi->type) {
9852 	case I40E_VSI_MAIN:
9853 		if (!vsi->netdev || !vsi->netdev_registered)
9854 			break;
9855 
9856 		if (link_up) {
9857 			netif_carrier_on(vsi->netdev);
9858 			netif_tx_wake_all_queues(vsi->netdev);
9859 		} else {
9860 			netif_carrier_off(vsi->netdev);
9861 			netif_tx_stop_all_queues(vsi->netdev);
9862 		}
9863 		break;
9864 
9865 	case I40E_VSI_SRIOV:
9866 	case I40E_VSI_VMDQ2:
9867 	case I40E_VSI_CTRL:
9868 	case I40E_VSI_IWARP:
9869 	case I40E_VSI_MIRROR:
9870 	default:
9871 		/* there is no notification for other VSIs */
9872 		break;
9873 	}
9874 }
9875 
9876 /**
9877  * i40e_veb_link_event - notify elements on the veb of a link event
9878  * @veb: veb to be notified
9879  * @link_up: link up or down
9880  **/
9881 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9882 {
9883 	struct i40e_pf *pf;
9884 	int i;
9885 
9886 	if (!veb || !veb->pf)
9887 		return;
9888 	pf = veb->pf;
9889 
9890 	/* depth first... */
9891 	for (i = 0; i < I40E_MAX_VEB; i++)
9892 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9893 			i40e_veb_link_event(pf->veb[i], link_up);
9894 
9895 	/* ... now the local VSIs */
9896 	for (i = 0; i < pf->num_alloc_vsi; i++)
9897 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9898 			i40e_vsi_link_event(pf->vsi[i], link_up);
9899 }
9900 
9901 /**
9902  * i40e_link_event - Update netif_carrier status
9903  * @pf: board private structure
9904  **/
9905 static void i40e_link_event(struct i40e_pf *pf)
9906 {
9907 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9908 	u8 new_link_speed, old_link_speed;
9909 	bool new_link, old_link;
9910 	int status;
9911 #ifdef CONFIG_I40E_DCB
9912 	int err;
9913 #endif /* CONFIG_I40E_DCB */
9914 
9915 	/* set this to force the get_link_status call to refresh state */
9916 	pf->hw.phy.get_link_info = true;
9917 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9918 	status = i40e_get_link_status(&pf->hw, &new_link);
9919 
9920 	/* On success, disable temp link polling */
9921 	if (status == 0) {
9922 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9923 	} else {
9924 		/* Enable link polling temporarily until i40e_get_link_status
9925 		 * returns 0
9926 		 */
9927 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9928 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9929 			status);
9930 		return;
9931 	}
9932 
9933 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9934 	new_link_speed = pf->hw.phy.link_info.link_speed;
9935 
9936 	if (new_link == old_link &&
9937 	    new_link_speed == old_link_speed &&
9938 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9939 	     new_link == netif_carrier_ok(vsi->netdev)))
9940 		return;
9941 
9942 	i40e_print_link_message(vsi, new_link);
9943 
9944 	/* Notify the base of the switch tree connected to
9945 	 * the link.  Floating VEBs are not notified.
9946 	 */
9947 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9948 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9949 	else
9950 		i40e_vsi_link_event(vsi, new_link);
9951 
9952 	if (pf->vf)
9953 		i40e_vc_notify_link_state(pf);
9954 
9955 	if (pf->flags & I40E_FLAG_PTP)
9956 		i40e_ptp_set_increment(pf);
9957 #ifdef CONFIG_I40E_DCB
9958 	if (new_link == old_link)
9959 		return;
9960 	/* Not SW DCB so firmware will take care of default settings */
9961 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9962 		return;
9963 
9964 	/* We cover here only link down, as after link up in case of SW DCB
9965 	 * SW LLDP agent will take care of setting it up
9966 	 */
9967 	if (!new_link) {
9968 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9969 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9970 		err = i40e_dcb_sw_default_config(pf);
9971 		if (err) {
9972 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9973 				       I40E_FLAG_DCB_ENABLED);
9974 		} else {
9975 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9976 				       DCB_CAP_DCBX_VER_IEEE;
9977 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9978 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9979 		}
9980 	}
9981 #endif /* CONFIG_I40E_DCB */
9982 }
9983 
9984 /**
9985  * i40e_watchdog_subtask - periodic checks not using event driven response
9986  * @pf: board private structure
9987  **/
9988 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9989 {
9990 	int i;
9991 
9992 	/* if interface is down do nothing */
9993 	if (test_bit(__I40E_DOWN, pf->state) ||
9994 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9995 		return;
9996 
9997 	/* make sure we don't do these things too often */
9998 	if (time_before(jiffies, (pf->service_timer_previous +
9999 				  pf->service_timer_period)))
10000 		return;
10001 	pf->service_timer_previous = jiffies;
10002 
10003 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
10004 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10005 		i40e_link_event(pf);
10006 
10007 	/* Update the stats for active netdevs so the network stack
10008 	 * can look at updated numbers whenever it cares to
10009 	 */
10010 	for (i = 0; i < pf->num_alloc_vsi; i++)
10011 		if (pf->vsi[i] && pf->vsi[i]->netdev)
10012 			i40e_update_stats(pf->vsi[i]);
10013 
10014 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
10015 		/* Update the stats for the active switching components */
10016 		for (i = 0; i < I40E_MAX_VEB; i++)
10017 			if (pf->veb[i])
10018 				i40e_update_veb_stats(pf->veb[i]);
10019 	}
10020 
10021 	i40e_ptp_rx_hang(pf);
10022 	i40e_ptp_tx_hang(pf);
10023 }
10024 
10025 /**
10026  * i40e_reset_subtask - Set up for resetting the device and driver
10027  * @pf: board private structure
10028  **/
10029 static void i40e_reset_subtask(struct i40e_pf *pf)
10030 {
10031 	u32 reset_flags = 0;
10032 
10033 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10034 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10035 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10036 	}
10037 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10038 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10039 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10040 	}
10041 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10042 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10043 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10044 	}
10045 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10046 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10047 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10048 	}
10049 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10050 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10051 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10052 	}
10053 
10054 	/* If there's a recovery already waiting, it takes
10055 	 * precedence before starting a new reset sequence.
10056 	 */
10057 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10058 		i40e_prep_for_reset(pf);
10059 		i40e_reset(pf);
10060 		i40e_rebuild(pf, false, false);
10061 	}
10062 
10063 	/* If we're already down or resetting, just bail */
10064 	if (reset_flags &&
10065 	    !test_bit(__I40E_DOWN, pf->state) &&
10066 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10067 		i40e_do_reset(pf, reset_flags, false);
10068 	}
10069 }
10070 
10071 /**
10072  * i40e_handle_link_event - Handle link event
10073  * @pf: board private structure
10074  * @e: event info posted on ARQ
10075  **/
10076 static void i40e_handle_link_event(struct i40e_pf *pf,
10077 				   struct i40e_arq_event_info *e)
10078 {
10079 	struct i40e_aqc_get_link_status *status =
10080 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10081 
10082 	/* Do a new status request to re-enable LSE reporting
10083 	 * and load new status information into the hw struct
10084 	 * This completely ignores any state information
10085 	 * in the ARQ event info, instead choosing to always
10086 	 * issue the AQ update link status command.
10087 	 */
10088 	i40e_link_event(pf);
10089 
10090 	/* Check if module meets thermal requirements */
10091 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10092 		dev_err(&pf->pdev->dev,
10093 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10094 		dev_err(&pf->pdev->dev,
10095 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10096 	} else {
10097 		/* check for unqualified module, if link is down, suppress
10098 		 * the message if link was forced to be down.
10099 		 */
10100 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10101 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10102 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10103 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10104 			dev_err(&pf->pdev->dev,
10105 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10106 			dev_err(&pf->pdev->dev,
10107 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10108 		}
10109 	}
10110 }
10111 
10112 /**
10113  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10114  * @pf: board private structure
10115  **/
10116 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10117 {
10118 	struct i40e_arq_event_info event;
10119 	struct i40e_hw *hw = &pf->hw;
10120 	u16 pending, i = 0;
10121 	u16 opcode;
10122 	u32 oldval;
10123 	int ret;
10124 	u32 val;
10125 
10126 	/* Do not run clean AQ when PF reset fails */
10127 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10128 		return;
10129 
10130 	/* check for error indications */
10131 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10132 	oldval = val;
10133 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10134 		if (hw->debug_mask & I40E_DEBUG_AQ)
10135 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10136 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10137 	}
10138 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10139 		if (hw->debug_mask & I40E_DEBUG_AQ)
10140 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10141 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10142 		pf->arq_overflows++;
10143 	}
10144 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10145 		if (hw->debug_mask & I40E_DEBUG_AQ)
10146 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10147 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10148 	}
10149 	if (oldval != val)
10150 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10151 
10152 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10153 	oldval = val;
10154 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10155 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10156 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10157 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10158 	}
10159 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10160 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10161 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10162 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10163 	}
10164 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10165 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10166 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10167 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10168 	}
10169 	if (oldval != val)
10170 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10171 
10172 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10173 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10174 	if (!event.msg_buf)
10175 		return;
10176 
10177 	do {
10178 		ret = i40e_clean_arq_element(hw, &event, &pending);
10179 		if (ret == -EALREADY)
10180 			break;
10181 		else if (ret) {
10182 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10183 			break;
10184 		}
10185 
10186 		opcode = le16_to_cpu(event.desc.opcode);
10187 		switch (opcode) {
10188 
10189 		case i40e_aqc_opc_get_link_status:
10190 			rtnl_lock();
10191 			i40e_handle_link_event(pf, &event);
10192 			rtnl_unlock();
10193 			break;
10194 		case i40e_aqc_opc_send_msg_to_pf:
10195 			ret = i40e_vc_process_vf_msg(pf,
10196 					le16_to_cpu(event.desc.retval),
10197 					le32_to_cpu(event.desc.cookie_high),
10198 					le32_to_cpu(event.desc.cookie_low),
10199 					event.msg_buf,
10200 					event.msg_len);
10201 			break;
10202 		case i40e_aqc_opc_lldp_update_mib:
10203 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10204 #ifdef CONFIG_I40E_DCB
10205 			rtnl_lock();
10206 			i40e_handle_lldp_event(pf, &event);
10207 			rtnl_unlock();
10208 #endif /* CONFIG_I40E_DCB */
10209 			break;
10210 		case i40e_aqc_opc_event_lan_overflow:
10211 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10212 			i40e_handle_lan_overflow_event(pf, &event);
10213 			break;
10214 		case i40e_aqc_opc_send_msg_to_peer:
10215 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10216 			break;
10217 		case i40e_aqc_opc_nvm_erase:
10218 		case i40e_aqc_opc_nvm_update:
10219 		case i40e_aqc_opc_oem_post_update:
10220 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10221 				   "ARQ NVM operation 0x%04x completed\n",
10222 				   opcode);
10223 			break;
10224 		default:
10225 			dev_info(&pf->pdev->dev,
10226 				 "ARQ: Unknown event 0x%04x ignored\n",
10227 				 opcode);
10228 			break;
10229 		}
10230 	} while (i++ < pf->adminq_work_limit);
10231 
10232 	if (i < pf->adminq_work_limit)
10233 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10234 
10235 	/* re-enable Admin queue interrupt cause */
10236 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10237 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10238 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10239 	i40e_flush(hw);
10240 
10241 	kfree(event.msg_buf);
10242 }
10243 
10244 /**
10245  * i40e_verify_eeprom - make sure eeprom is good to use
10246  * @pf: board private structure
10247  **/
10248 static void i40e_verify_eeprom(struct i40e_pf *pf)
10249 {
10250 	int err;
10251 
10252 	err = i40e_diag_eeprom_test(&pf->hw);
10253 	if (err) {
10254 		/* retry in case of garbage read */
10255 		err = i40e_diag_eeprom_test(&pf->hw);
10256 		if (err) {
10257 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10258 				 err);
10259 			set_bit(__I40E_BAD_EEPROM, pf->state);
10260 		}
10261 	}
10262 
10263 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10264 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10265 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10266 	}
10267 }
10268 
10269 /**
10270  * i40e_enable_pf_switch_lb
10271  * @pf: pointer to the PF structure
10272  *
10273  * enable switch loop back or die - no point in a return value
10274  **/
10275 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10276 {
10277 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10278 	struct i40e_vsi_context ctxt;
10279 	int ret;
10280 
10281 	ctxt.seid = pf->main_vsi_seid;
10282 	ctxt.pf_num = pf->hw.pf_id;
10283 	ctxt.vf_num = 0;
10284 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10285 	if (ret) {
10286 		dev_info(&pf->pdev->dev,
10287 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10288 			 ERR_PTR(ret),
10289 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10290 		return;
10291 	}
10292 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10293 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10294 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10295 
10296 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10297 	if (ret) {
10298 		dev_info(&pf->pdev->dev,
10299 			 "update vsi switch failed, err %pe aq_err %s\n",
10300 			 ERR_PTR(ret),
10301 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10302 	}
10303 }
10304 
10305 /**
10306  * i40e_disable_pf_switch_lb
10307  * @pf: pointer to the PF structure
10308  *
10309  * disable switch loop back or die - no point in a return value
10310  **/
10311 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10312 {
10313 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10314 	struct i40e_vsi_context ctxt;
10315 	int ret;
10316 
10317 	ctxt.seid = pf->main_vsi_seid;
10318 	ctxt.pf_num = pf->hw.pf_id;
10319 	ctxt.vf_num = 0;
10320 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10321 	if (ret) {
10322 		dev_info(&pf->pdev->dev,
10323 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10324 			 ERR_PTR(ret),
10325 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10326 		return;
10327 	}
10328 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10329 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10330 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10331 
10332 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10333 	if (ret) {
10334 		dev_info(&pf->pdev->dev,
10335 			 "update vsi switch failed, err %pe aq_err %s\n",
10336 			 ERR_PTR(ret),
10337 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10338 	}
10339 }
10340 
10341 /**
10342  * i40e_config_bridge_mode - Configure the HW bridge mode
10343  * @veb: pointer to the bridge instance
10344  *
10345  * Configure the loop back mode for the LAN VSI that is downlink to the
10346  * specified HW bridge instance. It is expected this function is called
10347  * when a new HW bridge is instantiated.
10348  **/
10349 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10350 {
10351 	struct i40e_pf *pf = veb->pf;
10352 
10353 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10354 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10355 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10356 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10357 		i40e_disable_pf_switch_lb(pf);
10358 	else
10359 		i40e_enable_pf_switch_lb(pf);
10360 }
10361 
10362 /**
10363  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10364  * @veb: pointer to the VEB instance
10365  *
10366  * This is a recursive function that first builds the attached VSIs then
10367  * recurses in to build the next layer of VEB.  We track the connections
10368  * through our own index numbers because the seid's from the HW could
10369  * change across the reset.
10370  **/
10371 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10372 {
10373 	struct i40e_vsi *ctl_vsi = NULL;
10374 	struct i40e_pf *pf = veb->pf;
10375 	int v, veb_idx;
10376 	int ret;
10377 
10378 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10379 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10380 		if (pf->vsi[v] &&
10381 		    pf->vsi[v]->veb_idx == veb->idx &&
10382 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10383 			ctl_vsi = pf->vsi[v];
10384 			break;
10385 		}
10386 	}
10387 	if (!ctl_vsi) {
10388 		dev_info(&pf->pdev->dev,
10389 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10390 		ret = -ENOENT;
10391 		goto end_reconstitute;
10392 	}
10393 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10394 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10395 	ret = i40e_add_vsi(ctl_vsi);
10396 	if (ret) {
10397 		dev_info(&pf->pdev->dev,
10398 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10399 			 veb->idx, ret);
10400 		goto end_reconstitute;
10401 	}
10402 	i40e_vsi_reset_stats(ctl_vsi);
10403 
10404 	/* create the VEB in the switch and move the VSI onto the VEB */
10405 	ret = i40e_add_veb(veb, ctl_vsi);
10406 	if (ret)
10407 		goto end_reconstitute;
10408 
10409 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10410 		veb->bridge_mode = BRIDGE_MODE_VEB;
10411 	else
10412 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10413 	i40e_config_bridge_mode(veb);
10414 
10415 	/* create the remaining VSIs attached to this VEB */
10416 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10417 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10418 			continue;
10419 
10420 		if (pf->vsi[v]->veb_idx == veb->idx) {
10421 			struct i40e_vsi *vsi = pf->vsi[v];
10422 
10423 			vsi->uplink_seid = veb->seid;
10424 			ret = i40e_add_vsi(vsi);
10425 			if (ret) {
10426 				dev_info(&pf->pdev->dev,
10427 					 "rebuild of vsi_idx %d failed: %d\n",
10428 					 v, ret);
10429 				goto end_reconstitute;
10430 			}
10431 			i40e_vsi_reset_stats(vsi);
10432 		}
10433 	}
10434 
10435 	/* create any VEBs attached to this VEB - RECURSION */
10436 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10437 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10438 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10439 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10440 			if (ret)
10441 				break;
10442 		}
10443 	}
10444 
10445 end_reconstitute:
10446 	return ret;
10447 }
10448 
10449 /**
10450  * i40e_get_capabilities - get info about the HW
10451  * @pf: the PF struct
10452  * @list_type: AQ capability to be queried
10453  **/
10454 static int i40e_get_capabilities(struct i40e_pf *pf,
10455 				 enum i40e_admin_queue_opc list_type)
10456 {
10457 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10458 	u16 data_size;
10459 	int buf_len;
10460 	int err;
10461 
10462 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10463 	do {
10464 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10465 		if (!cap_buf)
10466 			return -ENOMEM;
10467 
10468 		/* this loads the data into the hw struct for us */
10469 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10470 						    &data_size, list_type,
10471 						    NULL);
10472 		/* data loaded, buffer no longer needed */
10473 		kfree(cap_buf);
10474 
10475 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10476 			/* retry with a larger buffer */
10477 			buf_len = data_size;
10478 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10479 			dev_info(&pf->pdev->dev,
10480 				 "capability discovery failed, err %pe aq_err %s\n",
10481 				 ERR_PTR(err),
10482 				 i40e_aq_str(&pf->hw,
10483 					     pf->hw.aq.asq_last_status));
10484 			return -ENODEV;
10485 		}
10486 	} while (err);
10487 
10488 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10489 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10490 			dev_info(&pf->pdev->dev,
10491 				 "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",
10492 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10493 				 pf->hw.func_caps.num_msix_vectors,
10494 				 pf->hw.func_caps.num_msix_vectors_vf,
10495 				 pf->hw.func_caps.fd_filters_guaranteed,
10496 				 pf->hw.func_caps.fd_filters_best_effort,
10497 				 pf->hw.func_caps.num_tx_qp,
10498 				 pf->hw.func_caps.num_vsis);
10499 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10500 			dev_info(&pf->pdev->dev,
10501 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10502 				 pf->hw.dev_caps.switch_mode,
10503 				 pf->hw.dev_caps.valid_functions);
10504 			dev_info(&pf->pdev->dev,
10505 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10506 				 pf->hw.dev_caps.sr_iov_1_1,
10507 				 pf->hw.dev_caps.num_vfs);
10508 			dev_info(&pf->pdev->dev,
10509 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10510 				 pf->hw.dev_caps.num_vsis,
10511 				 pf->hw.dev_caps.num_rx_qp,
10512 				 pf->hw.dev_caps.num_tx_qp);
10513 		}
10514 	}
10515 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10516 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10517 		       + pf->hw.func_caps.num_vfs)
10518 		if (pf->hw.revision_id == 0 &&
10519 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10520 			dev_info(&pf->pdev->dev,
10521 				 "got num_vsis %d, setting num_vsis to %d\n",
10522 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10523 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10524 		}
10525 	}
10526 	return 0;
10527 }
10528 
10529 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10530 
10531 /**
10532  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10533  * @pf: board private structure
10534  **/
10535 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10536 {
10537 	struct i40e_vsi *vsi;
10538 
10539 	/* quick workaround for an NVM issue that leaves a critical register
10540 	 * uninitialized
10541 	 */
10542 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10543 		static const u32 hkey[] = {
10544 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10545 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10546 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10547 			0x95b3a76d};
10548 		int i;
10549 
10550 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10551 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10552 	}
10553 
10554 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10555 		return;
10556 
10557 	/* find existing VSI and see if it needs configuring */
10558 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10559 
10560 	/* create a new VSI if none exists */
10561 	if (!vsi) {
10562 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10563 				     pf->vsi[pf->lan_vsi]->seid, 0);
10564 		if (!vsi) {
10565 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10566 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10567 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10568 			return;
10569 		}
10570 	}
10571 
10572 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10573 }
10574 
10575 /**
10576  * i40e_fdir_teardown - release the Flow Director resources
10577  * @pf: board private structure
10578  **/
10579 static void i40e_fdir_teardown(struct i40e_pf *pf)
10580 {
10581 	struct i40e_vsi *vsi;
10582 
10583 	i40e_fdir_filter_exit(pf);
10584 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10585 	if (vsi)
10586 		i40e_vsi_release(vsi);
10587 }
10588 
10589 /**
10590  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10591  * @vsi: PF main vsi
10592  * @seid: seid of main or channel VSIs
10593  *
10594  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10595  * existed before reset
10596  **/
10597 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10598 {
10599 	struct i40e_cloud_filter *cfilter;
10600 	struct i40e_pf *pf = vsi->back;
10601 	struct hlist_node *node;
10602 	int ret;
10603 
10604 	/* Add cloud filters back if they exist */
10605 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10606 				  cloud_node) {
10607 		if (cfilter->seid != seid)
10608 			continue;
10609 
10610 		if (cfilter->dst_port)
10611 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10612 								true);
10613 		else
10614 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10615 
10616 		if (ret) {
10617 			dev_dbg(&pf->pdev->dev,
10618 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10619 				ERR_PTR(ret),
10620 				i40e_aq_str(&pf->hw,
10621 					    pf->hw.aq.asq_last_status));
10622 			return ret;
10623 		}
10624 	}
10625 	return 0;
10626 }
10627 
10628 /**
10629  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10630  * @vsi: PF main vsi
10631  *
10632  * Rebuilds channel VSIs if they existed before reset
10633  **/
10634 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10635 {
10636 	struct i40e_channel *ch, *ch_tmp;
10637 	int ret;
10638 
10639 	if (list_empty(&vsi->ch_list))
10640 		return 0;
10641 
10642 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10643 		if (!ch->initialized)
10644 			break;
10645 		/* Proceed with creation of channel (VMDq2) VSI */
10646 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10647 		if (ret) {
10648 			dev_info(&vsi->back->pdev->dev,
10649 				 "failed to rebuild channels using uplink_seid %u\n",
10650 				 vsi->uplink_seid);
10651 			return ret;
10652 		}
10653 		/* Reconfigure TX queues using QTX_CTL register */
10654 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10655 		if (ret) {
10656 			dev_info(&vsi->back->pdev->dev,
10657 				 "failed to configure TX rings for channel %u\n",
10658 				 ch->seid);
10659 			return ret;
10660 		}
10661 		/* update 'next_base_queue' */
10662 		vsi->next_base_queue = vsi->next_base_queue +
10663 							ch->num_queue_pairs;
10664 		if (ch->max_tx_rate) {
10665 			u64 credits = ch->max_tx_rate;
10666 
10667 			if (i40e_set_bw_limit(vsi, ch->seid,
10668 					      ch->max_tx_rate))
10669 				return -EINVAL;
10670 
10671 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10672 			dev_dbg(&vsi->back->pdev->dev,
10673 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10674 				ch->max_tx_rate,
10675 				credits,
10676 				ch->seid);
10677 		}
10678 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10679 		if (ret) {
10680 			dev_dbg(&vsi->back->pdev->dev,
10681 				"Failed to rebuild cloud filters for channel VSI %u\n",
10682 				ch->seid);
10683 			return ret;
10684 		}
10685 	}
10686 	return 0;
10687 }
10688 
10689 /**
10690  * i40e_clean_xps_state - clean xps state for every tx_ring
10691  * @vsi: ptr to the VSI
10692  **/
10693 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10694 {
10695 	int i;
10696 
10697 	if (vsi->tx_rings)
10698 		for (i = 0; i < vsi->num_queue_pairs; i++)
10699 			if (vsi->tx_rings[i])
10700 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10701 					  vsi->tx_rings[i]->state);
10702 }
10703 
10704 /**
10705  * i40e_prep_for_reset - prep for the core to reset
10706  * @pf: board private structure
10707  *
10708  * Close up the VFs and other things in prep for PF Reset.
10709   **/
10710 static void i40e_prep_for_reset(struct i40e_pf *pf)
10711 {
10712 	struct i40e_hw *hw = &pf->hw;
10713 	int ret = 0;
10714 	u32 v;
10715 
10716 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10717 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10718 		return;
10719 	if (i40e_check_asq_alive(&pf->hw))
10720 		i40e_vc_notify_reset(pf);
10721 
10722 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10723 
10724 	/* quiesce the VSIs and their queues that are not already DOWN */
10725 	i40e_pf_quiesce_all_vsi(pf);
10726 
10727 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10728 		if (pf->vsi[v]) {
10729 			i40e_clean_xps_state(pf->vsi[v]);
10730 			pf->vsi[v]->seid = 0;
10731 		}
10732 	}
10733 
10734 	i40e_shutdown_adminq(&pf->hw);
10735 
10736 	/* call shutdown HMC */
10737 	if (hw->hmc.hmc_obj) {
10738 		ret = i40e_shutdown_lan_hmc(hw);
10739 		if (ret)
10740 			dev_warn(&pf->pdev->dev,
10741 				 "shutdown_lan_hmc failed: %d\n", ret);
10742 	}
10743 
10744 	/* Save the current PTP time so that we can restore the time after the
10745 	 * reset completes.
10746 	 */
10747 	i40e_ptp_save_hw_time(pf);
10748 }
10749 
10750 /**
10751  * i40e_send_version - update firmware with driver version
10752  * @pf: PF struct
10753  */
10754 static void i40e_send_version(struct i40e_pf *pf)
10755 {
10756 	struct i40e_driver_version dv;
10757 
10758 	dv.major_version = 0xff;
10759 	dv.minor_version = 0xff;
10760 	dv.build_version = 0xff;
10761 	dv.subbuild_version = 0;
10762 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10763 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10764 }
10765 
10766 /**
10767  * i40e_get_oem_version - get OEM specific version information
10768  * @hw: pointer to the hardware structure
10769  **/
10770 static void i40e_get_oem_version(struct i40e_hw *hw)
10771 {
10772 	u16 block_offset = 0xffff;
10773 	u16 block_length = 0;
10774 	u16 capabilities = 0;
10775 	u16 gen_snap = 0;
10776 	u16 release = 0;
10777 
10778 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10779 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10780 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10781 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10782 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10783 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10784 #define I40E_NVM_OEM_LENGTH			3
10785 
10786 	/* Check if pointer to OEM version block is valid. */
10787 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10788 	if (block_offset == 0xffff)
10789 		return;
10790 
10791 	/* Check if OEM version block has correct length. */
10792 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10793 			   &block_length);
10794 	if (block_length < I40E_NVM_OEM_LENGTH)
10795 		return;
10796 
10797 	/* Check if OEM version format is as expected. */
10798 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10799 			   &capabilities);
10800 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10801 		return;
10802 
10803 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10804 			   &gen_snap);
10805 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10806 			   &release);
10807 	hw->nvm.oem_ver =
10808 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10809 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10810 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10811 }
10812 
10813 /**
10814  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10815  * @pf: board private structure
10816  **/
10817 static int i40e_reset(struct i40e_pf *pf)
10818 {
10819 	struct i40e_hw *hw = &pf->hw;
10820 	int ret;
10821 
10822 	ret = i40e_pf_reset(hw);
10823 	if (ret) {
10824 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10825 		set_bit(__I40E_RESET_FAILED, pf->state);
10826 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10827 	} else {
10828 		pf->pfr_count++;
10829 	}
10830 	return ret;
10831 }
10832 
10833 /**
10834  * i40e_rebuild - rebuild using a saved config
10835  * @pf: board private structure
10836  * @reinit: if the Main VSI needs to re-initialized.
10837  * @lock_acquired: indicates whether or not the lock has been acquired
10838  * before this function was called.
10839  **/
10840 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10841 {
10842 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10843 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10844 	struct i40e_hw *hw = &pf->hw;
10845 	int ret;
10846 	u32 val;
10847 	int v;
10848 
10849 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10850 	    is_recovery_mode_reported)
10851 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10852 
10853 	if (test_bit(__I40E_DOWN, pf->state) &&
10854 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10855 		goto clear_recovery;
10856 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10857 
10858 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10859 	ret = i40e_init_adminq(&pf->hw);
10860 	if (ret) {
10861 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10862 			 ERR_PTR(ret),
10863 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10864 		goto clear_recovery;
10865 	}
10866 	i40e_get_oem_version(&pf->hw);
10867 
10868 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10869 		/* The following delay is necessary for firmware update. */
10870 		mdelay(1000);
10871 	}
10872 
10873 	/* re-verify the eeprom if we just had an EMP reset */
10874 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10875 		i40e_verify_eeprom(pf);
10876 
10877 	/* if we are going out of or into recovery mode we have to act
10878 	 * accordingly with regard to resources initialization
10879 	 * and deinitialization
10880 	 */
10881 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10882 		if (i40e_get_capabilities(pf,
10883 					  i40e_aqc_opc_list_func_capabilities))
10884 			goto end_unlock;
10885 
10886 		if (is_recovery_mode_reported) {
10887 			/* we're staying in recovery mode so we'll reinitialize
10888 			 * misc vector here
10889 			 */
10890 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10891 				goto end_unlock;
10892 		} else {
10893 			if (!lock_acquired)
10894 				rtnl_lock();
10895 			/* we're going out of recovery mode so we'll free
10896 			 * the IRQ allocated specifically for recovery mode
10897 			 * and restore the interrupt scheme
10898 			 */
10899 			free_irq(pf->pdev->irq, pf);
10900 			i40e_clear_interrupt_scheme(pf);
10901 			if (i40e_restore_interrupt_scheme(pf))
10902 				goto end_unlock;
10903 		}
10904 
10905 		/* tell the firmware that we're starting */
10906 		i40e_send_version(pf);
10907 
10908 		/* bail out in case recovery mode was detected, as there is
10909 		 * no need for further configuration.
10910 		 */
10911 		goto end_unlock;
10912 	}
10913 
10914 	i40e_clear_pxe_mode(hw);
10915 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10916 	if (ret)
10917 		goto end_core_reset;
10918 
10919 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10920 				hw->func_caps.num_rx_qp, 0, 0);
10921 	if (ret) {
10922 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10923 		goto end_core_reset;
10924 	}
10925 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10926 	if (ret) {
10927 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10928 		goto end_core_reset;
10929 	}
10930 
10931 #ifdef CONFIG_I40E_DCB
10932 	/* Enable FW to write a default DCB config on link-up
10933 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10934 	 * is not supported with new link speed
10935 	 */
10936 	if (i40e_is_tc_mqprio_enabled(pf)) {
10937 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10938 	} else {
10939 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10940 		    (hw->phy.link_info.link_speed &
10941 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10942 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10943 			dev_warn(&pf->pdev->dev,
10944 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10945 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10946 		} else {
10947 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10948 			ret = i40e_init_pf_dcb(pf);
10949 			if (ret) {
10950 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10951 					 ret);
10952 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10953 				/* Continue without DCB enabled */
10954 			}
10955 		}
10956 	}
10957 
10958 #endif /* CONFIG_I40E_DCB */
10959 	if (!lock_acquired)
10960 		rtnl_lock();
10961 	ret = i40e_setup_pf_switch(pf, reinit, true);
10962 	if (ret)
10963 		goto end_unlock;
10964 
10965 	/* The driver only wants link up/down and module qualification
10966 	 * reports from firmware.  Note the negative logic.
10967 	 */
10968 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10969 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10970 					 I40E_AQ_EVENT_MEDIA_NA |
10971 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10972 	if (ret)
10973 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10974 			 ERR_PTR(ret),
10975 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10976 
10977 	/* Rebuild the VSIs and VEBs that existed before reset.
10978 	 * They are still in our local switch element arrays, so only
10979 	 * need to rebuild the switch model in the HW.
10980 	 *
10981 	 * If there were VEBs but the reconstitution failed, we'll try
10982 	 * to recover minimal use by getting the basic PF VSI working.
10983 	 */
10984 	if (vsi->uplink_seid != pf->mac_seid) {
10985 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10986 		/* find the one VEB connected to the MAC, and find orphans */
10987 		for (v = 0; v < I40E_MAX_VEB; v++) {
10988 			if (!pf->veb[v])
10989 				continue;
10990 
10991 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10992 			    pf->veb[v]->uplink_seid == 0) {
10993 				ret = i40e_reconstitute_veb(pf->veb[v]);
10994 
10995 				if (!ret)
10996 					continue;
10997 
10998 				/* If Main VEB failed, we're in deep doodoo,
10999 				 * so give up rebuilding the switch and set up
11000 				 * for minimal rebuild of PF VSI.
11001 				 * If orphan failed, we'll report the error
11002 				 * but try to keep going.
11003 				 */
11004 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
11005 					dev_info(&pf->pdev->dev,
11006 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
11007 						 ret);
11008 					vsi->uplink_seid = pf->mac_seid;
11009 					break;
11010 				} else if (pf->veb[v]->uplink_seid == 0) {
11011 					dev_info(&pf->pdev->dev,
11012 						 "rebuild of orphan VEB failed: %d\n",
11013 						 ret);
11014 				}
11015 			}
11016 		}
11017 	}
11018 
11019 	if (vsi->uplink_seid == pf->mac_seid) {
11020 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11021 		/* no VEB, so rebuild only the Main VSI */
11022 		ret = i40e_add_vsi(vsi);
11023 		if (ret) {
11024 			dev_info(&pf->pdev->dev,
11025 				 "rebuild of Main VSI failed: %d\n", ret);
11026 			goto end_unlock;
11027 		}
11028 	}
11029 
11030 	if (vsi->mqprio_qopt.max_rate[0]) {
11031 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11032 						  vsi->mqprio_qopt.max_rate[0]);
11033 		u64 credits = 0;
11034 
11035 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11036 		if (ret)
11037 			goto end_unlock;
11038 
11039 		credits = max_tx_rate;
11040 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11041 		dev_dbg(&vsi->back->pdev->dev,
11042 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11043 			max_tx_rate,
11044 			credits,
11045 			vsi->seid);
11046 	}
11047 
11048 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11049 	if (ret)
11050 		goto end_unlock;
11051 
11052 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11053 	 * for this main VSI if they exist
11054 	 */
11055 	ret = i40e_rebuild_channels(vsi);
11056 	if (ret)
11057 		goto end_unlock;
11058 
11059 	/* Reconfigure hardware for allowing smaller MSS in the case
11060 	 * of TSO, so that we avoid the MDD being fired and causing
11061 	 * a reset in the case of small MSS+TSO.
11062 	 */
11063 #define I40E_REG_MSS          0x000E64DC
11064 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11065 #define I40E_64BYTE_MSS       0x400000
11066 	val = rd32(hw, I40E_REG_MSS);
11067 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11068 		val &= ~I40E_REG_MSS_MIN_MASK;
11069 		val |= I40E_64BYTE_MSS;
11070 		wr32(hw, I40E_REG_MSS, val);
11071 	}
11072 
11073 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11074 		msleep(75);
11075 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11076 		if (ret)
11077 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11078 				 ERR_PTR(ret),
11079 				 i40e_aq_str(&pf->hw,
11080 					     pf->hw.aq.asq_last_status));
11081 	}
11082 	/* reinit the misc interrupt */
11083 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11084 		ret = i40e_setup_misc_vector(pf);
11085 		if (ret)
11086 			goto end_unlock;
11087 	}
11088 
11089 	/* Add a filter to drop all Flow control frames from any VSI from being
11090 	 * transmitted. By doing so we stop a malicious VF from sending out
11091 	 * PAUSE or PFC frames and potentially controlling traffic for other
11092 	 * PF/VF VSIs.
11093 	 * The FW can still send Flow control frames if enabled.
11094 	 */
11095 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11096 						       pf->main_vsi_seid);
11097 
11098 	/* restart the VSIs that were rebuilt and running before the reset */
11099 	i40e_pf_unquiesce_all_vsi(pf);
11100 
11101 	/* Release the RTNL lock before we start resetting VFs */
11102 	if (!lock_acquired)
11103 		rtnl_unlock();
11104 
11105 	/* Restore promiscuous settings */
11106 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11107 	if (ret)
11108 		dev_warn(&pf->pdev->dev,
11109 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11110 			 pf->cur_promisc ? "on" : "off",
11111 			 ERR_PTR(ret),
11112 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11113 
11114 	i40e_reset_all_vfs(pf, true);
11115 
11116 	/* tell the firmware that we're starting */
11117 	i40e_send_version(pf);
11118 
11119 	/* We've already released the lock, so don't do it again */
11120 	goto end_core_reset;
11121 
11122 end_unlock:
11123 	if (!lock_acquired)
11124 		rtnl_unlock();
11125 end_core_reset:
11126 	clear_bit(__I40E_RESET_FAILED, pf->state);
11127 clear_recovery:
11128 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11129 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11130 }
11131 
11132 /**
11133  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11134  * @pf: board private structure
11135  * @reinit: if the Main VSI needs to re-initialized.
11136  * @lock_acquired: indicates whether or not the lock has been acquired
11137  * before this function was called.
11138  **/
11139 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11140 				   bool lock_acquired)
11141 {
11142 	int ret;
11143 
11144 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11145 		return;
11146 	/* Now we wait for GRST to settle out.
11147 	 * We don't have to delete the VEBs or VSIs from the hw switch
11148 	 * because the reset will make them disappear.
11149 	 */
11150 	ret = i40e_reset(pf);
11151 	if (!ret)
11152 		i40e_rebuild(pf, reinit, lock_acquired);
11153 }
11154 
11155 /**
11156  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11157  * @pf: board private structure
11158  *
11159  * Close up the VFs and other things in prep for a Core Reset,
11160  * then get ready to rebuild the world.
11161  * @lock_acquired: indicates whether or not the lock has been acquired
11162  * before this function was called.
11163  **/
11164 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11165 {
11166 	i40e_prep_for_reset(pf);
11167 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11168 }
11169 
11170 /**
11171  * i40e_handle_mdd_event
11172  * @pf: pointer to the PF structure
11173  *
11174  * Called from the MDD irq handler to identify possibly malicious vfs
11175  **/
11176 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11177 {
11178 	struct i40e_hw *hw = &pf->hw;
11179 	bool mdd_detected = false;
11180 	struct i40e_vf *vf;
11181 	u32 reg;
11182 	int i;
11183 
11184 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11185 		return;
11186 
11187 	/* find what triggered the MDD event */
11188 	reg = rd32(hw, I40E_GL_MDET_TX);
11189 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11190 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11191 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11192 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11193 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11194 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11195 				I40E_GL_MDET_TX_EVENT_SHIFT;
11196 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11197 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11198 				pf->hw.func_caps.base_queue;
11199 		if (netif_msg_tx_err(pf))
11200 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11201 				 event, queue, pf_num, vf_num);
11202 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11203 		mdd_detected = true;
11204 	}
11205 	reg = rd32(hw, I40E_GL_MDET_RX);
11206 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11207 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11208 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11209 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11210 				I40E_GL_MDET_RX_EVENT_SHIFT;
11211 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11212 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11213 				pf->hw.func_caps.base_queue;
11214 		if (netif_msg_rx_err(pf))
11215 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11216 				 event, queue, func);
11217 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11218 		mdd_detected = true;
11219 	}
11220 
11221 	if (mdd_detected) {
11222 		reg = rd32(hw, I40E_PF_MDET_TX);
11223 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11224 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11225 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11226 		}
11227 		reg = rd32(hw, I40E_PF_MDET_RX);
11228 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11229 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11230 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11231 		}
11232 	}
11233 
11234 	/* see if one of the VFs needs its hand slapped */
11235 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11236 		vf = &(pf->vf[i]);
11237 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11238 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11239 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11240 			vf->num_mdd_events++;
11241 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11242 				 i);
11243 			dev_info(&pf->pdev->dev,
11244 				 "Use PF Control I/F to re-enable the VF\n");
11245 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11246 		}
11247 
11248 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11249 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11250 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11251 			vf->num_mdd_events++;
11252 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11253 				 i);
11254 			dev_info(&pf->pdev->dev,
11255 				 "Use PF Control I/F to re-enable the VF\n");
11256 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11257 		}
11258 	}
11259 
11260 	/* re-enable mdd interrupt cause */
11261 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11262 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11263 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11264 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11265 	i40e_flush(hw);
11266 }
11267 
11268 /**
11269  * i40e_service_task - Run the driver's async subtasks
11270  * @work: pointer to work_struct containing our data
11271  **/
11272 static void i40e_service_task(struct work_struct *work)
11273 {
11274 	struct i40e_pf *pf = container_of(work,
11275 					  struct i40e_pf,
11276 					  service_task);
11277 	unsigned long start_time = jiffies;
11278 
11279 	/* don't bother with service tasks if a reset is in progress */
11280 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11281 	    test_bit(__I40E_SUSPENDED, pf->state))
11282 		return;
11283 
11284 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11285 		return;
11286 
11287 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11288 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11289 		i40e_sync_filters_subtask(pf);
11290 		i40e_reset_subtask(pf);
11291 		i40e_handle_mdd_event(pf);
11292 		i40e_vc_process_vflr_event(pf);
11293 		i40e_watchdog_subtask(pf);
11294 		i40e_fdir_reinit_subtask(pf);
11295 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11296 			/* Client subtask will reopen next time through. */
11297 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11298 							   true);
11299 		} else {
11300 			i40e_client_subtask(pf);
11301 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11302 					       pf->state))
11303 				i40e_notify_client_of_l2_param_changes(
11304 								pf->vsi[pf->lan_vsi]);
11305 		}
11306 		i40e_sync_filters_subtask(pf);
11307 	} else {
11308 		i40e_reset_subtask(pf);
11309 	}
11310 
11311 	i40e_clean_adminq_subtask(pf);
11312 
11313 	/* flush memory to make sure state is correct before next watchdog */
11314 	smp_mb__before_atomic();
11315 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11316 
11317 	/* If the tasks have taken longer than one timer cycle or there
11318 	 * is more work to be done, reschedule the service task now
11319 	 * rather than wait for the timer to tick again.
11320 	 */
11321 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11322 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11323 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11324 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11325 		i40e_service_event_schedule(pf);
11326 }
11327 
11328 /**
11329  * i40e_service_timer - timer callback
11330  * @t: timer list pointer
11331  **/
11332 static void i40e_service_timer(struct timer_list *t)
11333 {
11334 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11335 
11336 	mod_timer(&pf->service_timer,
11337 		  round_jiffies(jiffies + pf->service_timer_period));
11338 	i40e_service_event_schedule(pf);
11339 }
11340 
11341 /**
11342  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11343  * @vsi: the VSI being configured
11344  **/
11345 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11346 {
11347 	struct i40e_pf *pf = vsi->back;
11348 
11349 	switch (vsi->type) {
11350 	case I40E_VSI_MAIN:
11351 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11352 		if (!vsi->num_tx_desc)
11353 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11354 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11355 		if (!vsi->num_rx_desc)
11356 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11357 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11358 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11359 			vsi->num_q_vectors = pf->num_lan_msix;
11360 		else
11361 			vsi->num_q_vectors = 1;
11362 
11363 		break;
11364 
11365 	case I40E_VSI_FDIR:
11366 		vsi->alloc_queue_pairs = 1;
11367 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11368 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11369 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11370 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11371 		vsi->num_q_vectors = pf->num_fdsb_msix;
11372 		break;
11373 
11374 	case I40E_VSI_VMDQ2:
11375 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11376 		if (!vsi->num_tx_desc)
11377 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11378 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11379 		if (!vsi->num_rx_desc)
11380 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11381 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11382 		vsi->num_q_vectors = pf->num_vmdq_msix;
11383 		break;
11384 
11385 	case I40E_VSI_SRIOV:
11386 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11387 		if (!vsi->num_tx_desc)
11388 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11389 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11390 		if (!vsi->num_rx_desc)
11391 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11392 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11393 		break;
11394 
11395 	default:
11396 		WARN_ON(1);
11397 		return -ENODATA;
11398 	}
11399 
11400 	if (is_kdump_kernel()) {
11401 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11402 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11403 	}
11404 
11405 	return 0;
11406 }
11407 
11408 /**
11409  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11410  * @vsi: VSI pointer
11411  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11412  *
11413  * On error: returns error code (negative)
11414  * On success: returns 0
11415  **/
11416 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11417 {
11418 	struct i40e_ring **next_rings;
11419 	int size;
11420 	int ret = 0;
11421 
11422 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11423 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11424 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11425 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11426 	if (!vsi->tx_rings)
11427 		return -ENOMEM;
11428 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11429 	if (i40e_enabled_xdp_vsi(vsi)) {
11430 		vsi->xdp_rings = next_rings;
11431 		next_rings += vsi->alloc_queue_pairs;
11432 	}
11433 	vsi->rx_rings = next_rings;
11434 
11435 	if (alloc_qvectors) {
11436 		/* allocate memory for q_vector pointers */
11437 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11438 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11439 		if (!vsi->q_vectors) {
11440 			ret = -ENOMEM;
11441 			goto err_vectors;
11442 		}
11443 	}
11444 	return ret;
11445 
11446 err_vectors:
11447 	kfree(vsi->tx_rings);
11448 	return ret;
11449 }
11450 
11451 /**
11452  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11453  * @pf: board private structure
11454  * @type: type of VSI
11455  *
11456  * On error: returns error code (negative)
11457  * On success: returns vsi index in PF (positive)
11458  **/
11459 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11460 {
11461 	int ret = -ENODEV;
11462 	struct i40e_vsi *vsi;
11463 	int vsi_idx;
11464 	int i;
11465 
11466 	/* Need to protect the allocation of the VSIs at the PF level */
11467 	mutex_lock(&pf->switch_mutex);
11468 
11469 	/* VSI list may be fragmented if VSI creation/destruction has
11470 	 * been happening.  We can afford to do a quick scan to look
11471 	 * for any free VSIs in the list.
11472 	 *
11473 	 * find next empty vsi slot, looping back around if necessary
11474 	 */
11475 	i = pf->next_vsi;
11476 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11477 		i++;
11478 	if (i >= pf->num_alloc_vsi) {
11479 		i = 0;
11480 		while (i < pf->next_vsi && pf->vsi[i])
11481 			i++;
11482 	}
11483 
11484 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11485 		vsi_idx = i;             /* Found one! */
11486 	} else {
11487 		ret = -ENODEV;
11488 		goto unlock_pf;  /* out of VSI slots! */
11489 	}
11490 	pf->next_vsi = ++i;
11491 
11492 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11493 	if (!vsi) {
11494 		ret = -ENOMEM;
11495 		goto unlock_pf;
11496 	}
11497 	vsi->type = type;
11498 	vsi->back = pf;
11499 	set_bit(__I40E_VSI_DOWN, vsi->state);
11500 	vsi->flags = 0;
11501 	vsi->idx = vsi_idx;
11502 	vsi->int_rate_limit = 0;
11503 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11504 				pf->rss_table_size : 64;
11505 	vsi->netdev_registered = false;
11506 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11507 	hash_init(vsi->mac_filter_hash);
11508 	vsi->irqs_ready = false;
11509 
11510 	if (type == I40E_VSI_MAIN) {
11511 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11512 		if (!vsi->af_xdp_zc_qps)
11513 			goto err_rings;
11514 	}
11515 
11516 	ret = i40e_set_num_rings_in_vsi(vsi);
11517 	if (ret)
11518 		goto err_rings;
11519 
11520 	ret = i40e_vsi_alloc_arrays(vsi, true);
11521 	if (ret)
11522 		goto err_rings;
11523 
11524 	/* Setup default MSIX irq handler for VSI */
11525 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11526 
11527 	/* Initialize VSI lock */
11528 	spin_lock_init(&vsi->mac_filter_hash_lock);
11529 	pf->vsi[vsi_idx] = vsi;
11530 	ret = vsi_idx;
11531 	goto unlock_pf;
11532 
11533 err_rings:
11534 	bitmap_free(vsi->af_xdp_zc_qps);
11535 	pf->next_vsi = i - 1;
11536 	kfree(vsi);
11537 unlock_pf:
11538 	mutex_unlock(&pf->switch_mutex);
11539 	return ret;
11540 }
11541 
11542 /**
11543  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11544  * @vsi: VSI pointer
11545  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11546  *
11547  * On error: returns error code (negative)
11548  * On success: returns 0
11549  **/
11550 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11551 {
11552 	/* free the ring and vector containers */
11553 	if (free_qvectors) {
11554 		kfree(vsi->q_vectors);
11555 		vsi->q_vectors = NULL;
11556 	}
11557 	kfree(vsi->tx_rings);
11558 	vsi->tx_rings = NULL;
11559 	vsi->rx_rings = NULL;
11560 	vsi->xdp_rings = NULL;
11561 }
11562 
11563 /**
11564  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11565  * and lookup table
11566  * @vsi: Pointer to VSI structure
11567  */
11568 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11569 {
11570 	if (!vsi)
11571 		return;
11572 
11573 	kfree(vsi->rss_hkey_user);
11574 	vsi->rss_hkey_user = NULL;
11575 
11576 	kfree(vsi->rss_lut_user);
11577 	vsi->rss_lut_user = NULL;
11578 }
11579 
11580 /**
11581  * i40e_vsi_clear - Deallocate the VSI provided
11582  * @vsi: the VSI being un-configured
11583  **/
11584 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11585 {
11586 	struct i40e_pf *pf;
11587 
11588 	if (!vsi)
11589 		return 0;
11590 
11591 	if (!vsi->back)
11592 		goto free_vsi;
11593 	pf = vsi->back;
11594 
11595 	mutex_lock(&pf->switch_mutex);
11596 	if (!pf->vsi[vsi->idx]) {
11597 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11598 			vsi->idx, vsi->idx, vsi->type);
11599 		goto unlock_vsi;
11600 	}
11601 
11602 	if (pf->vsi[vsi->idx] != vsi) {
11603 		dev_err(&pf->pdev->dev,
11604 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11605 			pf->vsi[vsi->idx]->idx,
11606 			pf->vsi[vsi->idx]->type,
11607 			vsi->idx, vsi->type);
11608 		goto unlock_vsi;
11609 	}
11610 
11611 	/* updates the PF for this cleared vsi */
11612 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11613 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11614 
11615 	bitmap_free(vsi->af_xdp_zc_qps);
11616 	i40e_vsi_free_arrays(vsi, true);
11617 	i40e_clear_rss_config_user(vsi);
11618 
11619 	pf->vsi[vsi->idx] = NULL;
11620 	if (vsi->idx < pf->next_vsi)
11621 		pf->next_vsi = vsi->idx;
11622 
11623 unlock_vsi:
11624 	mutex_unlock(&pf->switch_mutex);
11625 free_vsi:
11626 	kfree(vsi);
11627 
11628 	return 0;
11629 }
11630 
11631 /**
11632  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11633  * @vsi: the VSI being cleaned
11634  **/
11635 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11636 {
11637 	int i;
11638 
11639 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11640 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11641 			kfree_rcu(vsi->tx_rings[i], rcu);
11642 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11643 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11644 			if (vsi->xdp_rings)
11645 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11646 		}
11647 	}
11648 }
11649 
11650 /**
11651  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11652  * @vsi: the VSI being configured
11653  **/
11654 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11655 {
11656 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11657 	struct i40e_pf *pf = vsi->back;
11658 	struct i40e_ring *ring;
11659 
11660 	/* Set basic values in the rings to be used later during open() */
11661 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11662 		/* allocate space for both Tx and Rx in one shot */
11663 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11664 		if (!ring)
11665 			goto err_out;
11666 
11667 		ring->queue_index = i;
11668 		ring->reg_idx = vsi->base_queue + i;
11669 		ring->ring_active = false;
11670 		ring->vsi = vsi;
11671 		ring->netdev = vsi->netdev;
11672 		ring->dev = &pf->pdev->dev;
11673 		ring->count = vsi->num_tx_desc;
11674 		ring->size = 0;
11675 		ring->dcb_tc = 0;
11676 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11677 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11678 		ring->itr_setting = pf->tx_itr_default;
11679 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11680 
11681 		if (!i40e_enabled_xdp_vsi(vsi))
11682 			goto setup_rx;
11683 
11684 		ring->queue_index = vsi->alloc_queue_pairs + i;
11685 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11686 		ring->ring_active = false;
11687 		ring->vsi = vsi;
11688 		ring->netdev = NULL;
11689 		ring->dev = &pf->pdev->dev;
11690 		ring->count = vsi->num_tx_desc;
11691 		ring->size = 0;
11692 		ring->dcb_tc = 0;
11693 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11694 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11695 		set_ring_xdp(ring);
11696 		ring->itr_setting = pf->tx_itr_default;
11697 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11698 
11699 setup_rx:
11700 		ring->queue_index = i;
11701 		ring->reg_idx = vsi->base_queue + i;
11702 		ring->ring_active = false;
11703 		ring->vsi = vsi;
11704 		ring->netdev = vsi->netdev;
11705 		ring->dev = &pf->pdev->dev;
11706 		ring->count = vsi->num_rx_desc;
11707 		ring->size = 0;
11708 		ring->dcb_tc = 0;
11709 		ring->itr_setting = pf->rx_itr_default;
11710 		WRITE_ONCE(vsi->rx_rings[i], ring);
11711 	}
11712 
11713 	return 0;
11714 
11715 err_out:
11716 	i40e_vsi_clear_rings(vsi);
11717 	return -ENOMEM;
11718 }
11719 
11720 /**
11721  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11722  * @pf: board private structure
11723  * @vectors: the number of MSI-X vectors to request
11724  *
11725  * Returns the number of vectors reserved, or error
11726  **/
11727 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11728 {
11729 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11730 					I40E_MIN_MSIX, vectors);
11731 	if (vectors < 0) {
11732 		dev_info(&pf->pdev->dev,
11733 			 "MSI-X vector reservation failed: %d\n", vectors);
11734 		vectors = 0;
11735 	}
11736 
11737 	return vectors;
11738 }
11739 
11740 /**
11741  * i40e_init_msix - Setup the MSIX capability
11742  * @pf: board private structure
11743  *
11744  * Work with the OS to set up the MSIX vectors needed.
11745  *
11746  * Returns the number of vectors reserved or negative on failure
11747  **/
11748 static int i40e_init_msix(struct i40e_pf *pf)
11749 {
11750 	struct i40e_hw *hw = &pf->hw;
11751 	int cpus, extra_vectors;
11752 	int vectors_left;
11753 	int v_budget, i;
11754 	int v_actual;
11755 	int iwarp_requested = 0;
11756 
11757 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11758 		return -ENODEV;
11759 
11760 	/* The number of vectors we'll request will be comprised of:
11761 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11762 	 *   - The number of LAN queue pairs
11763 	 *	- Queues being used for RSS.
11764 	 *		We don't need as many as max_rss_size vectors.
11765 	 *		use rss_size instead in the calculation since that
11766 	 *		is governed by number of cpus in the system.
11767 	 *	- assumes symmetric Tx/Rx pairing
11768 	 *   - The number of VMDq pairs
11769 	 *   - The CPU count within the NUMA node if iWARP is enabled
11770 	 * Once we count this up, try the request.
11771 	 *
11772 	 * If we can't get what we want, we'll simplify to nearly nothing
11773 	 * and try again.  If that still fails, we punt.
11774 	 */
11775 	vectors_left = hw->func_caps.num_msix_vectors;
11776 	v_budget = 0;
11777 
11778 	/* reserve one vector for miscellaneous handler */
11779 	if (vectors_left) {
11780 		v_budget++;
11781 		vectors_left--;
11782 	}
11783 
11784 	/* reserve some vectors for the main PF traffic queues. Initially we
11785 	 * only reserve at most 50% of the available vectors, in the case that
11786 	 * the number of online CPUs is large. This ensures that we can enable
11787 	 * extra features as well. Once we've enabled the other features, we
11788 	 * will use any remaining vectors to reach as close as we can to the
11789 	 * number of online CPUs.
11790 	 */
11791 	cpus = num_online_cpus();
11792 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11793 	vectors_left -= pf->num_lan_msix;
11794 
11795 	/* reserve one vector for sideband flow director */
11796 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11797 		if (vectors_left) {
11798 			pf->num_fdsb_msix = 1;
11799 			v_budget++;
11800 			vectors_left--;
11801 		} else {
11802 			pf->num_fdsb_msix = 0;
11803 		}
11804 	}
11805 
11806 	/* can we reserve enough for iWARP? */
11807 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11808 		iwarp_requested = pf->num_iwarp_msix;
11809 
11810 		if (!vectors_left)
11811 			pf->num_iwarp_msix = 0;
11812 		else if (vectors_left < pf->num_iwarp_msix)
11813 			pf->num_iwarp_msix = 1;
11814 		v_budget += pf->num_iwarp_msix;
11815 		vectors_left -= pf->num_iwarp_msix;
11816 	}
11817 
11818 	/* any vectors left over go for VMDq support */
11819 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11820 		if (!vectors_left) {
11821 			pf->num_vmdq_msix = 0;
11822 			pf->num_vmdq_qps = 0;
11823 		} else {
11824 			int vmdq_vecs_wanted =
11825 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11826 			int vmdq_vecs =
11827 				min_t(int, vectors_left, vmdq_vecs_wanted);
11828 
11829 			/* if we're short on vectors for what's desired, we limit
11830 			 * the queues per vmdq.  If this is still more than are
11831 			 * available, the user will need to change the number of
11832 			 * queues/vectors used by the PF later with the ethtool
11833 			 * channels command
11834 			 */
11835 			if (vectors_left < vmdq_vecs_wanted) {
11836 				pf->num_vmdq_qps = 1;
11837 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11838 				vmdq_vecs = min_t(int,
11839 						  vectors_left,
11840 						  vmdq_vecs_wanted);
11841 			}
11842 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11843 
11844 			v_budget += vmdq_vecs;
11845 			vectors_left -= vmdq_vecs;
11846 		}
11847 	}
11848 
11849 	/* On systems with a large number of SMP cores, we previously limited
11850 	 * the number of vectors for num_lan_msix to be at most 50% of the
11851 	 * available vectors, to allow for other features. Now, we add back
11852 	 * the remaining vectors. However, we ensure that the total
11853 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11854 	 * calculate the number of vectors we can add without going over the
11855 	 * cap of CPUs. For systems with a small number of CPUs this will be
11856 	 * zero.
11857 	 */
11858 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11859 	pf->num_lan_msix += extra_vectors;
11860 	vectors_left -= extra_vectors;
11861 
11862 	WARN(vectors_left < 0,
11863 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11864 
11865 	v_budget += pf->num_lan_msix;
11866 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11867 				   GFP_KERNEL);
11868 	if (!pf->msix_entries)
11869 		return -ENOMEM;
11870 
11871 	for (i = 0; i < v_budget; i++)
11872 		pf->msix_entries[i].entry = i;
11873 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11874 
11875 	if (v_actual < I40E_MIN_MSIX) {
11876 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11877 		kfree(pf->msix_entries);
11878 		pf->msix_entries = NULL;
11879 		pci_disable_msix(pf->pdev);
11880 		return -ENODEV;
11881 
11882 	} else if (v_actual == I40E_MIN_MSIX) {
11883 		/* Adjust for minimal MSIX use */
11884 		pf->num_vmdq_vsis = 0;
11885 		pf->num_vmdq_qps = 0;
11886 		pf->num_lan_qps = 1;
11887 		pf->num_lan_msix = 1;
11888 
11889 	} else if (v_actual != v_budget) {
11890 		/* If we have limited resources, we will start with no vectors
11891 		 * for the special features and then allocate vectors to some
11892 		 * of these features based on the policy and at the end disable
11893 		 * the features that did not get any vectors.
11894 		 */
11895 		int vec;
11896 
11897 		dev_info(&pf->pdev->dev,
11898 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11899 			 v_actual, v_budget);
11900 		/* reserve the misc vector */
11901 		vec = v_actual - 1;
11902 
11903 		/* Scale vector usage down */
11904 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11905 		pf->num_vmdq_vsis = 1;
11906 		pf->num_vmdq_qps = 1;
11907 
11908 		/* partition out the remaining vectors */
11909 		switch (vec) {
11910 		case 2:
11911 			pf->num_lan_msix = 1;
11912 			break;
11913 		case 3:
11914 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11915 				pf->num_lan_msix = 1;
11916 				pf->num_iwarp_msix = 1;
11917 			} else {
11918 				pf->num_lan_msix = 2;
11919 			}
11920 			break;
11921 		default:
11922 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11923 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11924 						 iwarp_requested);
11925 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11926 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11927 			} else {
11928 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11929 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11930 			}
11931 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11932 				pf->num_fdsb_msix = 1;
11933 				vec--;
11934 			}
11935 			pf->num_lan_msix = min_t(int,
11936 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11937 							      pf->num_lan_msix);
11938 			pf->num_lan_qps = pf->num_lan_msix;
11939 			break;
11940 		}
11941 	}
11942 
11943 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11944 	    (pf->num_fdsb_msix == 0)) {
11945 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11946 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11947 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11948 	}
11949 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11950 	    (pf->num_vmdq_msix == 0)) {
11951 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11952 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11953 	}
11954 
11955 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11956 	    (pf->num_iwarp_msix == 0)) {
11957 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11958 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11959 	}
11960 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11961 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11962 		   pf->num_lan_msix,
11963 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11964 		   pf->num_fdsb_msix,
11965 		   pf->num_iwarp_msix);
11966 
11967 	return v_actual;
11968 }
11969 
11970 /**
11971  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11972  * @vsi: the VSI being configured
11973  * @v_idx: index of the vector in the vsi struct
11974  *
11975  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11976  **/
11977 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11978 {
11979 	struct i40e_q_vector *q_vector;
11980 
11981 	/* allocate q_vector */
11982 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11983 	if (!q_vector)
11984 		return -ENOMEM;
11985 
11986 	q_vector->vsi = vsi;
11987 	q_vector->v_idx = v_idx;
11988 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11989 
11990 	if (vsi->netdev)
11991 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11992 
11993 	/* tie q_vector and vsi together */
11994 	vsi->q_vectors[v_idx] = q_vector;
11995 
11996 	return 0;
11997 }
11998 
11999 /**
12000  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12001  * @vsi: the VSI being configured
12002  *
12003  * We allocate one q_vector per queue interrupt.  If allocation fails we
12004  * return -ENOMEM.
12005  **/
12006 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12007 {
12008 	struct i40e_pf *pf = vsi->back;
12009 	int err, v_idx, num_q_vectors;
12010 
12011 	/* if not MSIX, give the one vector only to the LAN VSI */
12012 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12013 		num_q_vectors = vsi->num_q_vectors;
12014 	else if (vsi == pf->vsi[pf->lan_vsi])
12015 		num_q_vectors = 1;
12016 	else
12017 		return -EINVAL;
12018 
12019 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12020 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12021 		if (err)
12022 			goto err_out;
12023 	}
12024 
12025 	return 0;
12026 
12027 err_out:
12028 	while (v_idx--)
12029 		i40e_free_q_vector(vsi, v_idx);
12030 
12031 	return err;
12032 }
12033 
12034 /**
12035  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12036  * @pf: board private structure to initialize
12037  **/
12038 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12039 {
12040 	int vectors = 0;
12041 	ssize_t size;
12042 
12043 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12044 		vectors = i40e_init_msix(pf);
12045 		if (vectors < 0) {
12046 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12047 				       I40E_FLAG_IWARP_ENABLED	|
12048 				       I40E_FLAG_RSS_ENABLED	|
12049 				       I40E_FLAG_DCB_CAPABLE	|
12050 				       I40E_FLAG_DCB_ENABLED	|
12051 				       I40E_FLAG_SRIOV_ENABLED	|
12052 				       I40E_FLAG_FD_SB_ENABLED	|
12053 				       I40E_FLAG_FD_ATR_ENABLED	|
12054 				       I40E_FLAG_VMDQ_ENABLED);
12055 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12056 
12057 			/* rework the queue expectations without MSIX */
12058 			i40e_determine_queue_usage(pf);
12059 		}
12060 	}
12061 
12062 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12063 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12064 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12065 		vectors = pci_enable_msi(pf->pdev);
12066 		if (vectors < 0) {
12067 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12068 				 vectors);
12069 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12070 		}
12071 		vectors = 1;  /* one MSI or Legacy vector */
12072 	}
12073 
12074 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12075 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12076 
12077 	/* set up vector assignment tracking */
12078 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12079 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12080 	if (!pf->irq_pile)
12081 		return -ENOMEM;
12082 
12083 	pf->irq_pile->num_entries = vectors;
12084 
12085 	/* track first vector for misc interrupts, ignore return */
12086 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12087 
12088 	return 0;
12089 }
12090 
12091 /**
12092  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12093  * @pf: private board data structure
12094  *
12095  * Restore the interrupt scheme that was cleared when we suspended the
12096  * device. This should be called during resume to re-allocate the q_vectors
12097  * and reacquire IRQs.
12098  */
12099 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12100 {
12101 	int err, i;
12102 
12103 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12104 	 * scheme. We need to re-enabled them here in order to attempt to
12105 	 * re-acquire the MSI or MSI-X vectors
12106 	 */
12107 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12108 
12109 	err = i40e_init_interrupt_scheme(pf);
12110 	if (err)
12111 		return err;
12112 
12113 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12114 	 * rings together again.
12115 	 */
12116 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12117 		if (pf->vsi[i]) {
12118 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12119 			if (err)
12120 				goto err_unwind;
12121 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12122 		}
12123 	}
12124 
12125 	err = i40e_setup_misc_vector(pf);
12126 	if (err)
12127 		goto err_unwind;
12128 
12129 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12130 		i40e_client_update_msix_info(pf);
12131 
12132 	return 0;
12133 
12134 err_unwind:
12135 	while (i--) {
12136 		if (pf->vsi[i])
12137 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12138 	}
12139 
12140 	return err;
12141 }
12142 
12143 /**
12144  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12145  * non queue events in recovery mode
12146  * @pf: board private structure
12147  *
12148  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12149  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12150  * This is handled differently than in recovery mode since no Tx/Rx resources
12151  * are being allocated.
12152  **/
12153 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12154 {
12155 	int err;
12156 
12157 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12158 		err = i40e_setup_misc_vector(pf);
12159 
12160 		if (err) {
12161 			dev_info(&pf->pdev->dev,
12162 				 "MSI-X misc vector request failed, error %d\n",
12163 				 err);
12164 			return err;
12165 		}
12166 	} else {
12167 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12168 
12169 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12170 				  pf->int_name, pf);
12171 
12172 		if (err) {
12173 			dev_info(&pf->pdev->dev,
12174 				 "MSI/legacy misc vector request failed, error %d\n",
12175 				 err);
12176 			return err;
12177 		}
12178 		i40e_enable_misc_int_causes(pf);
12179 		i40e_irq_dynamic_enable_icr0(pf);
12180 	}
12181 
12182 	return 0;
12183 }
12184 
12185 /**
12186  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12187  * @pf: board private structure
12188  *
12189  * This sets up the handler for MSIX 0, which is used to manage the
12190  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12191  * when in MSI or Legacy interrupt mode.
12192  **/
12193 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12194 {
12195 	struct i40e_hw *hw = &pf->hw;
12196 	int err = 0;
12197 
12198 	/* Only request the IRQ once, the first time through. */
12199 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12200 		err = request_irq(pf->msix_entries[0].vector,
12201 				  i40e_intr, 0, pf->int_name, pf);
12202 		if (err) {
12203 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12204 			dev_info(&pf->pdev->dev,
12205 				 "request_irq for %s failed: %d\n",
12206 				 pf->int_name, err);
12207 			return -EFAULT;
12208 		}
12209 	}
12210 
12211 	i40e_enable_misc_int_causes(pf);
12212 
12213 	/* associate no queues to the misc vector */
12214 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12215 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12216 
12217 	i40e_flush(hw);
12218 
12219 	i40e_irq_dynamic_enable_icr0(pf);
12220 
12221 	return err;
12222 }
12223 
12224 /**
12225  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12226  * @vsi: Pointer to vsi structure
12227  * @seed: Buffter to store the hash keys
12228  * @lut: Buffer to store the lookup table entries
12229  * @lut_size: Size of buffer to store the lookup table entries
12230  *
12231  * Return 0 on success, negative on failure
12232  */
12233 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12234 			   u8 *lut, u16 lut_size)
12235 {
12236 	struct i40e_pf *pf = vsi->back;
12237 	struct i40e_hw *hw = &pf->hw;
12238 	int ret = 0;
12239 
12240 	if (seed) {
12241 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12242 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12243 		if (ret) {
12244 			dev_info(&pf->pdev->dev,
12245 				 "Cannot get RSS key, err %pe aq_err %s\n",
12246 				 ERR_PTR(ret),
12247 				 i40e_aq_str(&pf->hw,
12248 					     pf->hw.aq.asq_last_status));
12249 			return ret;
12250 		}
12251 	}
12252 
12253 	if (lut) {
12254 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12255 
12256 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12257 		if (ret) {
12258 			dev_info(&pf->pdev->dev,
12259 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12260 				 ERR_PTR(ret),
12261 				 i40e_aq_str(&pf->hw,
12262 					     pf->hw.aq.asq_last_status));
12263 			return ret;
12264 		}
12265 	}
12266 
12267 	return ret;
12268 }
12269 
12270 /**
12271  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12272  * @vsi: Pointer to vsi structure
12273  * @seed: RSS hash seed
12274  * @lut: Lookup table
12275  * @lut_size: Lookup table size
12276  *
12277  * Returns 0 on success, negative on failure
12278  **/
12279 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12280 			       const u8 *lut, u16 lut_size)
12281 {
12282 	struct i40e_pf *pf = vsi->back;
12283 	struct i40e_hw *hw = &pf->hw;
12284 	u16 vf_id = vsi->vf_id;
12285 	u8 i;
12286 
12287 	/* Fill out hash function seed */
12288 	if (seed) {
12289 		u32 *seed_dw = (u32 *)seed;
12290 
12291 		if (vsi->type == I40E_VSI_MAIN) {
12292 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12293 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12294 		} else if (vsi->type == I40E_VSI_SRIOV) {
12295 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12296 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12297 		} else {
12298 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12299 		}
12300 	}
12301 
12302 	if (lut) {
12303 		u32 *lut_dw = (u32 *)lut;
12304 
12305 		if (vsi->type == I40E_VSI_MAIN) {
12306 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12307 				return -EINVAL;
12308 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12309 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12310 		} else if (vsi->type == I40E_VSI_SRIOV) {
12311 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12312 				return -EINVAL;
12313 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12314 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12315 		} else {
12316 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12317 		}
12318 	}
12319 	i40e_flush(hw);
12320 
12321 	return 0;
12322 }
12323 
12324 /**
12325  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12326  * @vsi: Pointer to VSI structure
12327  * @seed: Buffer to store the keys
12328  * @lut: Buffer to store the lookup table entries
12329  * @lut_size: Size of buffer to store the lookup table entries
12330  *
12331  * Returns 0 on success, negative on failure
12332  */
12333 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12334 			    u8 *lut, u16 lut_size)
12335 {
12336 	struct i40e_pf *pf = vsi->back;
12337 	struct i40e_hw *hw = &pf->hw;
12338 	u16 i;
12339 
12340 	if (seed) {
12341 		u32 *seed_dw = (u32 *)seed;
12342 
12343 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12344 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12345 	}
12346 	if (lut) {
12347 		u32 *lut_dw = (u32 *)lut;
12348 
12349 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12350 			return -EINVAL;
12351 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12352 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12353 	}
12354 
12355 	return 0;
12356 }
12357 
12358 /**
12359  * i40e_config_rss - Configure RSS keys and lut
12360  * @vsi: Pointer to VSI structure
12361  * @seed: RSS hash seed
12362  * @lut: Lookup table
12363  * @lut_size: Lookup table size
12364  *
12365  * Returns 0 on success, negative on failure
12366  */
12367 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12368 {
12369 	struct i40e_pf *pf = vsi->back;
12370 
12371 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12372 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12373 	else
12374 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12375 }
12376 
12377 /**
12378  * i40e_get_rss - Get RSS keys and lut
12379  * @vsi: Pointer to VSI structure
12380  * @seed: Buffer to store the keys
12381  * @lut: Buffer to store the lookup table entries
12382  * @lut_size: Size of buffer to store the lookup table entries
12383  *
12384  * Returns 0 on success, negative on failure
12385  */
12386 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12387 {
12388 	struct i40e_pf *pf = vsi->back;
12389 
12390 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12391 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12392 	else
12393 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12394 }
12395 
12396 /**
12397  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12398  * @pf: Pointer to board private structure
12399  * @lut: Lookup table
12400  * @rss_table_size: Lookup table size
12401  * @rss_size: Range of queue number for hashing
12402  */
12403 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12404 		       u16 rss_table_size, u16 rss_size)
12405 {
12406 	u16 i;
12407 
12408 	for (i = 0; i < rss_table_size; i++)
12409 		lut[i] = i % rss_size;
12410 }
12411 
12412 /**
12413  * i40e_pf_config_rss - Prepare for RSS if used
12414  * @pf: board private structure
12415  **/
12416 static int i40e_pf_config_rss(struct i40e_pf *pf)
12417 {
12418 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12419 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12420 	u8 *lut;
12421 	struct i40e_hw *hw = &pf->hw;
12422 	u32 reg_val;
12423 	u64 hena;
12424 	int ret;
12425 
12426 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12427 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12428 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12429 	hena |= i40e_pf_get_default_rss_hena(pf);
12430 
12431 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12432 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12433 
12434 	/* Determine the RSS table size based on the hardware capabilities */
12435 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12436 	reg_val = (pf->rss_table_size == 512) ?
12437 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12438 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12439 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12440 
12441 	/* Determine the RSS size of the VSI */
12442 	if (!vsi->rss_size) {
12443 		u16 qcount;
12444 		/* If the firmware does something weird during VSI init, we
12445 		 * could end up with zero TCs. Check for that to avoid
12446 		 * divide-by-zero. It probably won't pass traffic, but it also
12447 		 * won't panic.
12448 		 */
12449 		qcount = vsi->num_queue_pairs /
12450 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12451 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12452 	}
12453 	if (!vsi->rss_size)
12454 		return -EINVAL;
12455 
12456 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12457 	if (!lut)
12458 		return -ENOMEM;
12459 
12460 	/* Use user configured lut if there is one, otherwise use default */
12461 	if (vsi->rss_lut_user)
12462 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12463 	else
12464 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12465 
12466 	/* Use user configured hash key if there is one, otherwise
12467 	 * use default.
12468 	 */
12469 	if (vsi->rss_hkey_user)
12470 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12471 	else
12472 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12473 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12474 	kfree(lut);
12475 
12476 	return ret;
12477 }
12478 
12479 /**
12480  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12481  * @pf: board private structure
12482  * @queue_count: the requested queue count for rss.
12483  *
12484  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12485  * count which may be different from the requested queue count.
12486  * Note: expects to be called while under rtnl_lock()
12487  **/
12488 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12489 {
12490 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12491 	int new_rss_size;
12492 
12493 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12494 		return 0;
12495 
12496 	queue_count = min_t(int, queue_count, num_online_cpus());
12497 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12498 
12499 	if (queue_count != vsi->num_queue_pairs) {
12500 		u16 qcount;
12501 
12502 		vsi->req_queue_pairs = queue_count;
12503 		i40e_prep_for_reset(pf);
12504 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12505 			return pf->alloc_rss_size;
12506 
12507 		pf->alloc_rss_size = new_rss_size;
12508 
12509 		i40e_reset_and_rebuild(pf, true, true);
12510 
12511 		/* Discard the user configured hash keys and lut, if less
12512 		 * queues are enabled.
12513 		 */
12514 		if (queue_count < vsi->rss_size) {
12515 			i40e_clear_rss_config_user(vsi);
12516 			dev_dbg(&pf->pdev->dev,
12517 				"discard user configured hash keys and lut\n");
12518 		}
12519 
12520 		/* Reset vsi->rss_size, as number of enabled queues changed */
12521 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12522 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12523 
12524 		i40e_pf_config_rss(pf);
12525 	}
12526 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12527 		 vsi->req_queue_pairs, pf->rss_size_max);
12528 	return pf->alloc_rss_size;
12529 }
12530 
12531 /**
12532  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12533  * @pf: board private structure
12534  **/
12535 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12536 {
12537 	bool min_valid, max_valid;
12538 	u32 max_bw, min_bw;
12539 	int status;
12540 
12541 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12542 					   &min_valid, &max_valid);
12543 
12544 	if (!status) {
12545 		if (min_valid)
12546 			pf->min_bw = min_bw;
12547 		if (max_valid)
12548 			pf->max_bw = max_bw;
12549 	}
12550 
12551 	return status;
12552 }
12553 
12554 /**
12555  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12556  * @pf: board private structure
12557  **/
12558 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12559 {
12560 	struct i40e_aqc_configure_partition_bw_data bw_data;
12561 	int status;
12562 
12563 	memset(&bw_data, 0, sizeof(bw_data));
12564 
12565 	/* Set the valid bit for this PF */
12566 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12567 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12568 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12569 
12570 	/* Set the new bandwidths */
12571 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12572 
12573 	return status;
12574 }
12575 
12576 /**
12577  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12578  * @pf: board private structure
12579  **/
12580 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12581 {
12582 	/* Commit temporary BW setting to permanent NVM image */
12583 	enum i40e_admin_queue_err last_aq_status;
12584 	u16 nvm_word;
12585 	int ret;
12586 
12587 	if (pf->hw.partition_id != 1) {
12588 		dev_info(&pf->pdev->dev,
12589 			 "Commit BW only works on partition 1! This is partition %d",
12590 			 pf->hw.partition_id);
12591 		ret = -EOPNOTSUPP;
12592 		goto bw_commit_out;
12593 	}
12594 
12595 	/* Acquire NVM for read access */
12596 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12597 	last_aq_status = pf->hw.aq.asq_last_status;
12598 	if (ret) {
12599 		dev_info(&pf->pdev->dev,
12600 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12601 			 ERR_PTR(ret),
12602 			 i40e_aq_str(&pf->hw, last_aq_status));
12603 		goto bw_commit_out;
12604 	}
12605 
12606 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12607 	ret = i40e_aq_read_nvm(&pf->hw,
12608 			       I40E_SR_NVM_CONTROL_WORD,
12609 			       0x10, sizeof(nvm_word), &nvm_word,
12610 			       false, NULL);
12611 	/* Save off last admin queue command status before releasing
12612 	 * the NVM
12613 	 */
12614 	last_aq_status = pf->hw.aq.asq_last_status;
12615 	i40e_release_nvm(&pf->hw);
12616 	if (ret) {
12617 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12618 			 ERR_PTR(ret),
12619 			 i40e_aq_str(&pf->hw, last_aq_status));
12620 		goto bw_commit_out;
12621 	}
12622 
12623 	/* Wait a bit for NVM release to complete */
12624 	msleep(50);
12625 
12626 	/* Acquire NVM for write access */
12627 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12628 	last_aq_status = pf->hw.aq.asq_last_status;
12629 	if (ret) {
12630 		dev_info(&pf->pdev->dev,
12631 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12632 			 ERR_PTR(ret),
12633 			 i40e_aq_str(&pf->hw, last_aq_status));
12634 		goto bw_commit_out;
12635 	}
12636 	/* Write it back out unchanged to initiate update NVM,
12637 	 * which will force a write of the shadow (alt) RAM to
12638 	 * the NVM - thus storing the bandwidth values permanently.
12639 	 */
12640 	ret = i40e_aq_update_nvm(&pf->hw,
12641 				 I40E_SR_NVM_CONTROL_WORD,
12642 				 0x10, sizeof(nvm_word),
12643 				 &nvm_word, true, 0, NULL);
12644 	/* Save off last admin queue command status before releasing
12645 	 * the NVM
12646 	 */
12647 	last_aq_status = pf->hw.aq.asq_last_status;
12648 	i40e_release_nvm(&pf->hw);
12649 	if (ret)
12650 		dev_info(&pf->pdev->dev,
12651 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12652 			 ERR_PTR(ret),
12653 			 i40e_aq_str(&pf->hw, last_aq_status));
12654 bw_commit_out:
12655 
12656 	return ret;
12657 }
12658 
12659 /**
12660  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12661  * if total port shutdown feature is enabled for this PF
12662  * @pf: board private structure
12663  **/
12664 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12665 {
12666 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12667 #define I40E_FEATURES_ENABLE_PTR		0x2A
12668 #define I40E_CURRENT_SETTING_PTR		0x2B
12669 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12670 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12671 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12672 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12673 	u16 sr_emp_sr_settings_ptr = 0;
12674 	u16 features_enable = 0;
12675 	u16 link_behavior = 0;
12676 	int read_status = 0;
12677 	bool ret = false;
12678 
12679 	read_status = i40e_read_nvm_word(&pf->hw,
12680 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12681 					 &sr_emp_sr_settings_ptr);
12682 	if (read_status)
12683 		goto err_nvm;
12684 	read_status = i40e_read_nvm_word(&pf->hw,
12685 					 sr_emp_sr_settings_ptr +
12686 					 I40E_FEATURES_ENABLE_PTR,
12687 					 &features_enable);
12688 	if (read_status)
12689 		goto err_nvm;
12690 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12691 		read_status = i40e_read_nvm_module_data(&pf->hw,
12692 							I40E_SR_EMP_SR_SETTINGS_PTR,
12693 							I40E_CURRENT_SETTING_PTR,
12694 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12695 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12696 							&link_behavior);
12697 		if (read_status)
12698 			goto err_nvm;
12699 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12700 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12701 	}
12702 	return ret;
12703 
12704 err_nvm:
12705 	dev_warn(&pf->pdev->dev,
12706 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12707 		 ERR_PTR(read_status));
12708 	return ret;
12709 }
12710 
12711 /**
12712  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12713  * @pf: board private structure to initialize
12714  *
12715  * i40e_sw_init initializes the Adapter private data structure.
12716  * Fields are initialized based on PCI device information and
12717  * OS network device settings (MTU size).
12718  **/
12719 static int i40e_sw_init(struct i40e_pf *pf)
12720 {
12721 	int err = 0;
12722 	int size;
12723 	u16 pow;
12724 
12725 	/* Set default capability flags */
12726 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12727 		    I40E_FLAG_MSI_ENABLED     |
12728 		    I40E_FLAG_MSIX_ENABLED;
12729 
12730 	/* Set default ITR */
12731 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12732 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12733 
12734 	/* Depending on PF configurations, it is possible that the RSS
12735 	 * maximum might end up larger than the available queues
12736 	 */
12737 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12738 	pf->alloc_rss_size = 1;
12739 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12740 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12741 				 pf->hw.func_caps.num_tx_qp);
12742 
12743 	/* find the next higher power-of-2 of num cpus */
12744 	pow = roundup_pow_of_two(num_online_cpus());
12745 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12746 
12747 	if (pf->hw.func_caps.rss) {
12748 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12749 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12750 					   num_online_cpus());
12751 	}
12752 
12753 	/* MFP mode enabled */
12754 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12755 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12756 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12757 		if (i40e_get_partition_bw_setting(pf)) {
12758 			dev_warn(&pf->pdev->dev,
12759 				 "Could not get partition bw settings\n");
12760 		} else {
12761 			dev_info(&pf->pdev->dev,
12762 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12763 				 pf->min_bw, pf->max_bw);
12764 
12765 			/* nudge the Tx scheduler */
12766 			i40e_set_partition_bw_setting(pf);
12767 		}
12768 	}
12769 
12770 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12771 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12772 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12773 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12774 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12775 		    pf->hw.num_partitions > 1)
12776 			dev_info(&pf->pdev->dev,
12777 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12778 		else
12779 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12780 		pf->fdir_pf_filter_count =
12781 				 pf->hw.func_caps.fd_filters_guaranteed;
12782 		pf->hw.fdir_shared_filter_count =
12783 				 pf->hw.func_caps.fd_filters_best_effort;
12784 	}
12785 
12786 	if (pf->hw.mac.type == I40E_MAC_X722) {
12787 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12788 				    I40E_HW_128_QP_RSS_CAPABLE |
12789 				    I40E_HW_ATR_EVICT_CAPABLE |
12790 				    I40E_HW_WB_ON_ITR_CAPABLE |
12791 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12792 				    I40E_HW_NO_PCI_LINK_CHECK |
12793 				    I40E_HW_USE_SET_LLDP_MIB |
12794 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12795 				    I40E_HW_PTP_L4_CAPABLE |
12796 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12797 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12798 
12799 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12800 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12801 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12802 			dev_warn(&pf->pdev->dev,
12803 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12804 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12805 		}
12806 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12807 		   ((pf->hw.aq.api_maj_ver == 1) &&
12808 		    (pf->hw.aq.api_min_ver > 4))) {
12809 		/* Supported in FW API version higher than 1.4 */
12810 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12811 	}
12812 
12813 	/* Enable HW ATR eviction if possible */
12814 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12815 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12816 
12817 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12818 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12819 	    (pf->hw.aq.fw_maj_ver < 4))) {
12820 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12821 		/* No DCB support  for FW < v4.33 */
12822 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12823 	}
12824 
12825 	/* Disable FW LLDP if FW < v4.3 */
12826 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12827 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12828 	    (pf->hw.aq.fw_maj_ver < 4)))
12829 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12830 
12831 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12832 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12833 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12834 	    (pf->hw.aq.fw_maj_ver >= 5)))
12835 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12836 
12837 	/* Enable PTP L4 if FW > v6.0 */
12838 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12839 	    pf->hw.aq.fw_maj_ver >= 6)
12840 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12841 
12842 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12843 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12844 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12845 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12846 	}
12847 
12848 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12849 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12850 		/* IWARP needs one extra vector for CQP just like MISC.*/
12851 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12852 	}
12853 	/* Stopping FW LLDP engine is supported on XL710 and X722
12854 	 * starting from FW versions determined in i40e_init_adminq.
12855 	 * Stopping the FW LLDP engine is not supported on XL710
12856 	 * if NPAR is functioning so unset this hw flag in this case.
12857 	 */
12858 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12859 	    pf->hw.func_caps.npar_enable &&
12860 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12861 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12862 
12863 #ifdef CONFIG_PCI_IOV
12864 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12865 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12866 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12867 		pf->num_req_vfs = min_t(int,
12868 					pf->hw.func_caps.num_vfs,
12869 					I40E_MAX_VF_COUNT);
12870 	}
12871 #endif /* CONFIG_PCI_IOV */
12872 	pf->eeprom_version = 0xDEAD;
12873 	pf->lan_veb = I40E_NO_VEB;
12874 	pf->lan_vsi = I40E_NO_VSI;
12875 
12876 	/* By default FW has this off for performance reasons */
12877 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12878 
12879 	/* set up queue assignment tracking */
12880 	size = sizeof(struct i40e_lump_tracking)
12881 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12882 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12883 	if (!pf->qp_pile) {
12884 		err = -ENOMEM;
12885 		goto sw_init_done;
12886 	}
12887 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12888 
12889 	pf->tx_timeout_recovery_level = 1;
12890 
12891 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12892 	    i40e_is_total_port_shutdown_enabled(pf)) {
12893 		/* Link down on close must be on when total port shutdown
12894 		 * is enabled for a given port
12895 		 */
12896 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12897 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12898 		dev_info(&pf->pdev->dev,
12899 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12900 	}
12901 	mutex_init(&pf->switch_mutex);
12902 
12903 sw_init_done:
12904 	return err;
12905 }
12906 
12907 /**
12908  * i40e_set_ntuple - set the ntuple feature flag and take action
12909  * @pf: board private structure to initialize
12910  * @features: the feature set that the stack is suggesting
12911  *
12912  * returns a bool to indicate if reset needs to happen
12913  **/
12914 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12915 {
12916 	bool need_reset = false;
12917 
12918 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12919 	 * the state changed, we need to reset.
12920 	 */
12921 	if (features & NETIF_F_NTUPLE) {
12922 		/* Enable filters and mark for reset */
12923 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12924 			need_reset = true;
12925 		/* enable FD_SB only if there is MSI-X vector and no cloud
12926 		 * filters exist
12927 		 */
12928 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12929 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12930 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12931 		}
12932 	} else {
12933 		/* turn off filters, mark for reset and clear SW filter list */
12934 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12935 			need_reset = true;
12936 			i40e_fdir_filter_exit(pf);
12937 		}
12938 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12939 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12940 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12941 
12942 		/* reset fd counters */
12943 		pf->fd_add_err = 0;
12944 		pf->fd_atr_cnt = 0;
12945 		/* if ATR was auto disabled it can be re-enabled. */
12946 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12947 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12948 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12949 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12950 	}
12951 	return need_reset;
12952 }
12953 
12954 /**
12955  * i40e_clear_rss_lut - clear the rx hash lookup table
12956  * @vsi: the VSI being configured
12957  **/
12958 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12959 {
12960 	struct i40e_pf *pf = vsi->back;
12961 	struct i40e_hw *hw = &pf->hw;
12962 	u16 vf_id = vsi->vf_id;
12963 	u8 i;
12964 
12965 	if (vsi->type == I40E_VSI_MAIN) {
12966 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12967 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12968 	} else if (vsi->type == I40E_VSI_SRIOV) {
12969 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12970 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12971 	} else {
12972 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12973 	}
12974 }
12975 
12976 /**
12977  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12978  * @vsi: ptr to VSI
12979  * @ena: flag to indicate the on/off setting
12980  */
12981 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12982 {
12983 	bool if_running = netif_running(vsi->netdev) &&
12984 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12985 	int ret;
12986 
12987 	if (if_running)
12988 		i40e_down(vsi);
12989 
12990 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12991 	if (ret)
12992 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12993 	if (if_running)
12994 		i40e_up(vsi);
12995 
12996 	return ret;
12997 }
12998 
12999 /**
13000  * i40e_set_features - set the netdev feature flags
13001  * @netdev: ptr to the netdev being adjusted
13002  * @features: the feature set that the stack is suggesting
13003  * Note: expects to be called while under rtnl_lock()
13004  **/
13005 static int i40e_set_features(struct net_device *netdev,
13006 			     netdev_features_t features)
13007 {
13008 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13009 	struct i40e_vsi *vsi = np->vsi;
13010 	struct i40e_pf *pf = vsi->back;
13011 	bool need_reset;
13012 
13013 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
13014 		i40e_pf_config_rss(pf);
13015 	else if (!(features & NETIF_F_RXHASH) &&
13016 		 netdev->features & NETIF_F_RXHASH)
13017 		i40e_clear_rss_lut(vsi);
13018 
13019 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
13020 		i40e_vlan_stripping_enable(vsi);
13021 	else
13022 		i40e_vlan_stripping_disable(vsi);
13023 
13024 	if (!(features & NETIF_F_HW_TC) &&
13025 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13026 		dev_err(&pf->pdev->dev,
13027 			"Offloaded tc filters active, can't turn hw_tc_offload off");
13028 		return -EINVAL;
13029 	}
13030 
13031 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13032 		i40e_del_all_macvlans(vsi);
13033 
13034 	need_reset = i40e_set_ntuple(pf, features);
13035 
13036 	if (need_reset)
13037 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13038 
13039 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13040 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13041 
13042 	return 0;
13043 }
13044 
13045 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13046 				    unsigned int table, unsigned int idx,
13047 				    struct udp_tunnel_info *ti)
13048 {
13049 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13050 	struct i40e_hw *hw = &np->vsi->back->hw;
13051 	u8 type, filter_index;
13052 	int ret;
13053 
13054 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13055 						   I40E_AQC_TUNNEL_TYPE_NGE;
13056 
13057 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13058 				     NULL);
13059 	if (ret) {
13060 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13061 			    ERR_PTR(ret),
13062 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13063 		return -EIO;
13064 	}
13065 
13066 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13067 	return 0;
13068 }
13069 
13070 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13071 				      unsigned int table, unsigned int idx,
13072 				      struct udp_tunnel_info *ti)
13073 {
13074 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13075 	struct i40e_hw *hw = &np->vsi->back->hw;
13076 	int ret;
13077 
13078 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13079 	if (ret) {
13080 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13081 			    ERR_PTR(ret),
13082 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13083 		return -EIO;
13084 	}
13085 
13086 	return 0;
13087 }
13088 
13089 static int i40e_get_phys_port_id(struct net_device *netdev,
13090 				 struct netdev_phys_item_id *ppid)
13091 {
13092 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13093 	struct i40e_pf *pf = np->vsi->back;
13094 	struct i40e_hw *hw = &pf->hw;
13095 
13096 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13097 		return -EOPNOTSUPP;
13098 
13099 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13100 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13101 
13102 	return 0;
13103 }
13104 
13105 /**
13106  * i40e_ndo_fdb_add - add an entry to the hardware database
13107  * @ndm: the input from the stack
13108  * @tb: pointer to array of nladdr (unused)
13109  * @dev: the net device pointer
13110  * @addr: the MAC address entry being added
13111  * @vid: VLAN ID
13112  * @flags: instructions from stack about fdb operation
13113  * @extack: netlink extended ack, unused currently
13114  */
13115 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13116 			    struct net_device *dev,
13117 			    const unsigned char *addr, u16 vid,
13118 			    u16 flags,
13119 			    struct netlink_ext_ack *extack)
13120 {
13121 	struct i40e_netdev_priv *np = netdev_priv(dev);
13122 	struct i40e_pf *pf = np->vsi->back;
13123 	int err = 0;
13124 
13125 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13126 		return -EOPNOTSUPP;
13127 
13128 	if (vid) {
13129 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13130 		return -EINVAL;
13131 	}
13132 
13133 	/* Hardware does not support aging addresses so if a
13134 	 * ndm_state is given only allow permanent addresses
13135 	 */
13136 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13137 		netdev_info(dev, "FDB only supports static addresses\n");
13138 		return -EINVAL;
13139 	}
13140 
13141 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13142 		err = dev_uc_add_excl(dev, addr);
13143 	else if (is_multicast_ether_addr(addr))
13144 		err = dev_mc_add_excl(dev, addr);
13145 	else
13146 		err = -EINVAL;
13147 
13148 	/* Only return duplicate errors if NLM_F_EXCL is set */
13149 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13150 		err = 0;
13151 
13152 	return err;
13153 }
13154 
13155 /**
13156  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13157  * @dev: the netdev being configured
13158  * @nlh: RTNL message
13159  * @flags: bridge flags
13160  * @extack: netlink extended ack
13161  *
13162  * Inserts a new hardware bridge if not already created and
13163  * enables the bridging mode requested (VEB or VEPA). If the
13164  * hardware bridge has already been inserted and the request
13165  * is to change the mode then that requires a PF reset to
13166  * allow rebuild of the components with required hardware
13167  * bridge mode enabled.
13168  *
13169  * Note: expects to be called while under rtnl_lock()
13170  **/
13171 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13172 				   struct nlmsghdr *nlh,
13173 				   u16 flags,
13174 				   struct netlink_ext_ack *extack)
13175 {
13176 	struct i40e_netdev_priv *np = netdev_priv(dev);
13177 	struct i40e_vsi *vsi = np->vsi;
13178 	struct i40e_pf *pf = vsi->back;
13179 	struct i40e_veb *veb = NULL;
13180 	struct nlattr *attr, *br_spec;
13181 	int i, rem;
13182 
13183 	/* Only for PF VSI for now */
13184 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13185 		return -EOPNOTSUPP;
13186 
13187 	/* Find the HW bridge for PF VSI */
13188 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13189 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13190 			veb = pf->veb[i];
13191 	}
13192 
13193 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13194 	if (!br_spec)
13195 		return -EINVAL;
13196 
13197 	nla_for_each_nested(attr, br_spec, rem) {
13198 		__u16 mode;
13199 
13200 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13201 			continue;
13202 
13203 		mode = nla_get_u16(attr);
13204 		if ((mode != BRIDGE_MODE_VEPA) &&
13205 		    (mode != BRIDGE_MODE_VEB))
13206 			return -EINVAL;
13207 
13208 		/* Insert a new HW bridge */
13209 		if (!veb) {
13210 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13211 					     vsi->tc_config.enabled_tc);
13212 			if (veb) {
13213 				veb->bridge_mode = mode;
13214 				i40e_config_bridge_mode(veb);
13215 			} else {
13216 				/* No Bridge HW offload available */
13217 				return -ENOENT;
13218 			}
13219 			break;
13220 		} else if (mode != veb->bridge_mode) {
13221 			/* Existing HW bridge but different mode needs reset */
13222 			veb->bridge_mode = mode;
13223 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13224 			if (mode == BRIDGE_MODE_VEB)
13225 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13226 			else
13227 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13228 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13229 			break;
13230 		}
13231 	}
13232 
13233 	return 0;
13234 }
13235 
13236 /**
13237  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13238  * @skb: skb buff
13239  * @pid: process id
13240  * @seq: RTNL message seq #
13241  * @dev: the netdev being configured
13242  * @filter_mask: unused
13243  * @nlflags: netlink flags passed in
13244  *
13245  * Return the mode in which the hardware bridge is operating in
13246  * i.e VEB or VEPA.
13247  **/
13248 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13249 				   struct net_device *dev,
13250 				   u32 __always_unused filter_mask,
13251 				   int nlflags)
13252 {
13253 	struct i40e_netdev_priv *np = netdev_priv(dev);
13254 	struct i40e_vsi *vsi = np->vsi;
13255 	struct i40e_pf *pf = vsi->back;
13256 	struct i40e_veb *veb = NULL;
13257 	int i;
13258 
13259 	/* Only for PF VSI for now */
13260 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13261 		return -EOPNOTSUPP;
13262 
13263 	/* Find the HW bridge for the PF VSI */
13264 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13265 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13266 			veb = pf->veb[i];
13267 	}
13268 
13269 	if (!veb)
13270 		return 0;
13271 
13272 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13273 				       0, 0, nlflags, filter_mask, NULL);
13274 }
13275 
13276 /**
13277  * i40e_features_check - Validate encapsulated packet conforms to limits
13278  * @skb: skb buff
13279  * @dev: This physical port's netdev
13280  * @features: Offload features that the stack believes apply
13281  **/
13282 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13283 					     struct net_device *dev,
13284 					     netdev_features_t features)
13285 {
13286 	size_t len;
13287 
13288 	/* No point in doing any of this if neither checksum nor GSO are
13289 	 * being requested for this frame.  We can rule out both by just
13290 	 * checking for CHECKSUM_PARTIAL
13291 	 */
13292 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13293 		return features;
13294 
13295 	/* We cannot support GSO if the MSS is going to be less than
13296 	 * 64 bytes.  If it is then we need to drop support for GSO.
13297 	 */
13298 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13299 		features &= ~NETIF_F_GSO_MASK;
13300 
13301 	/* MACLEN can support at most 63 words */
13302 	len = skb_network_header(skb) - skb->data;
13303 	if (len & ~(63 * 2))
13304 		goto out_err;
13305 
13306 	/* IPLEN and EIPLEN can support at most 127 dwords */
13307 	len = skb_transport_header(skb) - skb_network_header(skb);
13308 	if (len & ~(127 * 4))
13309 		goto out_err;
13310 
13311 	if (skb->encapsulation) {
13312 		/* L4TUNLEN can support 127 words */
13313 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13314 		if (len & ~(127 * 2))
13315 			goto out_err;
13316 
13317 		/* IPLEN can support at most 127 dwords */
13318 		len = skb_inner_transport_header(skb) -
13319 		      skb_inner_network_header(skb);
13320 		if (len & ~(127 * 4))
13321 			goto out_err;
13322 	}
13323 
13324 	/* No need to validate L4LEN as TCP is the only protocol with a
13325 	 * flexible value and we support all possible values supported
13326 	 * by TCP, which is at most 15 dwords
13327 	 */
13328 
13329 	return features;
13330 out_err:
13331 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13332 }
13333 
13334 /**
13335  * i40e_xdp_setup - add/remove an XDP program
13336  * @vsi: VSI to changed
13337  * @prog: XDP program
13338  * @extack: netlink extended ack
13339  **/
13340 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13341 			  struct netlink_ext_ack *extack)
13342 {
13343 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13344 	struct i40e_pf *pf = vsi->back;
13345 	struct bpf_prog *old_prog;
13346 	bool need_reset;
13347 	int i;
13348 
13349 	/* Don't allow frames that span over multiple buffers */
13350 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13351 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13352 		return -EINVAL;
13353 	}
13354 
13355 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13356 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13357 
13358 	if (need_reset)
13359 		i40e_prep_for_reset(pf);
13360 
13361 	/* VSI shall be deleted in a moment, just return EINVAL */
13362 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13363 		return -EINVAL;
13364 
13365 	old_prog = xchg(&vsi->xdp_prog, prog);
13366 
13367 	if (need_reset) {
13368 		if (!prog) {
13369 			xdp_features_clear_redirect_target(vsi->netdev);
13370 			/* Wait until ndo_xsk_wakeup completes. */
13371 			synchronize_rcu();
13372 		}
13373 		i40e_reset_and_rebuild(pf, true, true);
13374 	}
13375 
13376 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13377 		if (i40e_realloc_rx_bi_zc(vsi, true))
13378 			return -ENOMEM;
13379 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13380 		if (i40e_realloc_rx_bi_zc(vsi, false))
13381 			return -ENOMEM;
13382 	}
13383 
13384 	for (i = 0; i < vsi->num_queue_pairs; i++)
13385 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13386 
13387 	if (old_prog)
13388 		bpf_prog_put(old_prog);
13389 
13390 	/* Kick start the NAPI context if there is an AF_XDP socket open
13391 	 * on that queue id. This so that receiving will start.
13392 	 */
13393 	if (need_reset && prog) {
13394 		for (i = 0; i < vsi->num_queue_pairs; i++)
13395 			if (vsi->xdp_rings[i]->xsk_pool)
13396 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13397 						      XDP_WAKEUP_RX);
13398 		xdp_features_set_redirect_target(vsi->netdev, true);
13399 	}
13400 
13401 	return 0;
13402 }
13403 
13404 /**
13405  * i40e_enter_busy_conf - Enters busy config state
13406  * @vsi: vsi
13407  *
13408  * Returns 0 on success, <0 for failure.
13409  **/
13410 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13411 {
13412 	struct i40e_pf *pf = vsi->back;
13413 	int timeout = 50;
13414 
13415 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13416 		timeout--;
13417 		if (!timeout)
13418 			return -EBUSY;
13419 		usleep_range(1000, 2000);
13420 	}
13421 
13422 	return 0;
13423 }
13424 
13425 /**
13426  * i40e_exit_busy_conf - Exits busy config state
13427  * @vsi: vsi
13428  **/
13429 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13430 {
13431 	struct i40e_pf *pf = vsi->back;
13432 
13433 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13434 }
13435 
13436 /**
13437  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13438  * @vsi: vsi
13439  * @queue_pair: queue pair
13440  **/
13441 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13442 {
13443 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13444 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13445 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13446 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13447 	if (i40e_enabled_xdp_vsi(vsi)) {
13448 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13449 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13450 	}
13451 }
13452 
13453 /**
13454  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13455  * @vsi: vsi
13456  * @queue_pair: queue pair
13457  **/
13458 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13459 {
13460 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13461 	if (i40e_enabled_xdp_vsi(vsi)) {
13462 		/* Make sure that in-progress ndo_xdp_xmit calls are
13463 		 * completed.
13464 		 */
13465 		synchronize_rcu();
13466 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13467 	}
13468 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13469 }
13470 
13471 /**
13472  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13473  * @vsi: vsi
13474  * @queue_pair: queue pair
13475  * @enable: true for enable, false for disable
13476  **/
13477 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13478 					bool enable)
13479 {
13480 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13481 	struct i40e_q_vector *q_vector = rxr->q_vector;
13482 
13483 	if (!vsi->netdev)
13484 		return;
13485 
13486 	/* All rings in a qp belong to the same qvector. */
13487 	if (q_vector->rx.ring || q_vector->tx.ring) {
13488 		if (enable)
13489 			napi_enable(&q_vector->napi);
13490 		else
13491 			napi_disable(&q_vector->napi);
13492 	}
13493 }
13494 
13495 /**
13496  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13497  * @vsi: vsi
13498  * @queue_pair: queue pair
13499  * @enable: true for enable, false for disable
13500  *
13501  * Returns 0 on success, <0 on failure.
13502  **/
13503 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13504 					bool enable)
13505 {
13506 	struct i40e_pf *pf = vsi->back;
13507 	int pf_q, ret = 0;
13508 
13509 	pf_q = vsi->base_queue + queue_pair;
13510 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13511 				     false /*is xdp*/, enable);
13512 	if (ret) {
13513 		dev_info(&pf->pdev->dev,
13514 			 "VSI seid %d Tx ring %d %sable timeout\n",
13515 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13516 		return ret;
13517 	}
13518 
13519 	i40e_control_rx_q(pf, pf_q, enable);
13520 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13521 	if (ret) {
13522 		dev_info(&pf->pdev->dev,
13523 			 "VSI seid %d Rx ring %d %sable timeout\n",
13524 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13525 		return ret;
13526 	}
13527 
13528 	/* Due to HW errata, on Rx disable only, the register can
13529 	 * indicate done before it really is. Needs 50ms to be sure
13530 	 */
13531 	if (!enable)
13532 		mdelay(50);
13533 
13534 	if (!i40e_enabled_xdp_vsi(vsi))
13535 		return ret;
13536 
13537 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13538 				     pf_q + vsi->alloc_queue_pairs,
13539 				     true /*is xdp*/, enable);
13540 	if (ret) {
13541 		dev_info(&pf->pdev->dev,
13542 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13543 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13544 	}
13545 
13546 	return ret;
13547 }
13548 
13549 /**
13550  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13551  * @vsi: vsi
13552  * @queue_pair: queue_pair
13553  **/
13554 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13555 {
13556 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13557 	struct i40e_pf *pf = vsi->back;
13558 	struct i40e_hw *hw = &pf->hw;
13559 
13560 	/* All rings in a qp belong to the same qvector. */
13561 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13562 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13563 	else
13564 		i40e_irq_dynamic_enable_icr0(pf);
13565 
13566 	i40e_flush(hw);
13567 }
13568 
13569 /**
13570  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13571  * @vsi: vsi
13572  * @queue_pair: queue_pair
13573  **/
13574 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13575 {
13576 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13577 	struct i40e_pf *pf = vsi->back;
13578 	struct i40e_hw *hw = &pf->hw;
13579 
13580 	/* For simplicity, instead of removing the qp interrupt causes
13581 	 * from the interrupt linked list, we simply disable the interrupt, and
13582 	 * leave the list intact.
13583 	 *
13584 	 * All rings in a qp belong to the same qvector.
13585 	 */
13586 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13587 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13588 
13589 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13590 		i40e_flush(hw);
13591 		synchronize_irq(pf->msix_entries[intpf].vector);
13592 	} else {
13593 		/* Legacy and MSI mode - this stops all interrupt handling */
13594 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13595 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13596 		i40e_flush(hw);
13597 		synchronize_irq(pf->pdev->irq);
13598 	}
13599 }
13600 
13601 /**
13602  * i40e_queue_pair_disable - Disables a queue pair
13603  * @vsi: vsi
13604  * @queue_pair: queue pair
13605  *
13606  * Returns 0 on success, <0 on failure.
13607  **/
13608 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13609 {
13610 	int err;
13611 
13612 	err = i40e_enter_busy_conf(vsi);
13613 	if (err)
13614 		return err;
13615 
13616 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13617 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13618 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13619 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13620 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13621 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13622 
13623 	return err;
13624 }
13625 
13626 /**
13627  * i40e_queue_pair_enable - Enables a queue pair
13628  * @vsi: vsi
13629  * @queue_pair: queue pair
13630  *
13631  * Returns 0 on success, <0 on failure.
13632  **/
13633 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13634 {
13635 	int err;
13636 
13637 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13638 	if (err)
13639 		return err;
13640 
13641 	if (i40e_enabled_xdp_vsi(vsi)) {
13642 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13643 		if (err)
13644 			return err;
13645 	}
13646 
13647 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13648 	if (err)
13649 		return err;
13650 
13651 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13652 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13653 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13654 
13655 	i40e_exit_busy_conf(vsi);
13656 
13657 	return err;
13658 }
13659 
13660 /**
13661  * i40e_xdp - implements ndo_bpf for i40e
13662  * @dev: netdevice
13663  * @xdp: XDP command
13664  **/
13665 static int i40e_xdp(struct net_device *dev,
13666 		    struct netdev_bpf *xdp)
13667 {
13668 	struct i40e_netdev_priv *np = netdev_priv(dev);
13669 	struct i40e_vsi *vsi = np->vsi;
13670 
13671 	if (vsi->type != I40E_VSI_MAIN)
13672 		return -EINVAL;
13673 
13674 	switch (xdp->command) {
13675 	case XDP_SETUP_PROG:
13676 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13677 	case XDP_SETUP_XSK_POOL:
13678 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13679 					   xdp->xsk.queue_id);
13680 	default:
13681 		return -EINVAL;
13682 	}
13683 }
13684 
13685 static const struct net_device_ops i40e_netdev_ops = {
13686 	.ndo_open		= i40e_open,
13687 	.ndo_stop		= i40e_close,
13688 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13689 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13690 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13691 	.ndo_validate_addr	= eth_validate_addr,
13692 	.ndo_set_mac_address	= i40e_set_mac,
13693 	.ndo_change_mtu		= i40e_change_mtu,
13694 	.ndo_eth_ioctl		= i40e_ioctl,
13695 	.ndo_tx_timeout		= i40e_tx_timeout,
13696 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13697 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13698 #ifdef CONFIG_NET_POLL_CONTROLLER
13699 	.ndo_poll_controller	= i40e_netpoll,
13700 #endif
13701 	.ndo_setup_tc		= __i40e_setup_tc,
13702 	.ndo_select_queue	= i40e_lan_select_queue,
13703 	.ndo_set_features	= i40e_set_features,
13704 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13705 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13706 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13707 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13708 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13709 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13710 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13711 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13712 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13713 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13714 	.ndo_features_check	= i40e_features_check,
13715 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13716 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13717 	.ndo_bpf		= i40e_xdp,
13718 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13719 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13720 	.ndo_dfwd_add_station	= i40e_fwd_add,
13721 	.ndo_dfwd_del_station	= i40e_fwd_del,
13722 };
13723 
13724 /**
13725  * i40e_config_netdev - Setup the netdev flags
13726  * @vsi: the VSI being configured
13727  *
13728  * Returns 0 on success, negative value on failure
13729  **/
13730 static int i40e_config_netdev(struct i40e_vsi *vsi)
13731 {
13732 	struct i40e_pf *pf = vsi->back;
13733 	struct i40e_hw *hw = &pf->hw;
13734 	struct i40e_netdev_priv *np;
13735 	struct net_device *netdev;
13736 	u8 broadcast[ETH_ALEN];
13737 	u8 mac_addr[ETH_ALEN];
13738 	int etherdev_size;
13739 	netdev_features_t hw_enc_features;
13740 	netdev_features_t hw_features;
13741 
13742 	etherdev_size = sizeof(struct i40e_netdev_priv);
13743 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13744 	if (!netdev)
13745 		return -ENOMEM;
13746 
13747 	vsi->netdev = netdev;
13748 	np = netdev_priv(netdev);
13749 	np->vsi = vsi;
13750 
13751 	hw_enc_features = NETIF_F_SG			|
13752 			  NETIF_F_HW_CSUM		|
13753 			  NETIF_F_HIGHDMA		|
13754 			  NETIF_F_SOFT_FEATURES		|
13755 			  NETIF_F_TSO			|
13756 			  NETIF_F_TSO_ECN		|
13757 			  NETIF_F_TSO6			|
13758 			  NETIF_F_GSO_GRE		|
13759 			  NETIF_F_GSO_GRE_CSUM		|
13760 			  NETIF_F_GSO_PARTIAL		|
13761 			  NETIF_F_GSO_IPXIP4		|
13762 			  NETIF_F_GSO_IPXIP6		|
13763 			  NETIF_F_GSO_UDP_TUNNEL	|
13764 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13765 			  NETIF_F_GSO_UDP_L4		|
13766 			  NETIF_F_SCTP_CRC		|
13767 			  NETIF_F_RXHASH		|
13768 			  NETIF_F_RXCSUM		|
13769 			  0;
13770 
13771 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13772 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13773 
13774 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13775 
13776 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13777 
13778 	netdev->hw_enc_features |= hw_enc_features;
13779 
13780 	/* record features VLANs can make use of */
13781 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13782 
13783 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13784 				   NETIF_F_GSO_GRE_CSUM |	\
13785 				   NETIF_F_GSO_IPXIP4 |		\
13786 				   NETIF_F_GSO_IPXIP6 |		\
13787 				   NETIF_F_GSO_UDP_TUNNEL |	\
13788 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13789 
13790 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13791 	netdev->features |= NETIF_F_GSO_PARTIAL |
13792 			    I40E_GSO_PARTIAL_FEATURES;
13793 
13794 	netdev->mpls_features |= NETIF_F_SG;
13795 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13796 	netdev->mpls_features |= NETIF_F_TSO;
13797 	netdev->mpls_features |= NETIF_F_TSO6;
13798 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13799 
13800 	/* enable macvlan offloads */
13801 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13802 
13803 	hw_features = hw_enc_features		|
13804 		      NETIF_F_HW_VLAN_CTAG_TX	|
13805 		      NETIF_F_HW_VLAN_CTAG_RX;
13806 
13807 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13808 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13809 
13810 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13811 
13812 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13813 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13814 
13815 	netdev->features &= ~NETIF_F_HW_TC;
13816 
13817 	if (vsi->type == I40E_VSI_MAIN) {
13818 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13819 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13820 		/* The following steps are necessary for two reasons. First,
13821 		 * some older NVM configurations load a default MAC-VLAN
13822 		 * filter that will accept any tagged packet, and we want to
13823 		 * replace this with a normal filter. Additionally, it is
13824 		 * possible our MAC address was provided by the platform using
13825 		 * Open Firmware or similar.
13826 		 *
13827 		 * Thus, we need to remove the default filter and install one
13828 		 * specific to the MAC address.
13829 		 */
13830 		i40e_rm_default_mac_filter(vsi, mac_addr);
13831 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13832 		i40e_add_mac_filter(vsi, mac_addr);
13833 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13834 
13835 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13836 				       NETDEV_XDP_ACT_REDIRECT |
13837 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13838 				       NETDEV_XDP_ACT_RX_SG;
13839 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13840 	} else {
13841 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13842 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13843 		 * the end, which is 4 bytes long, so force truncation of the
13844 		 * original name by IFNAMSIZ - 4
13845 		 */
13846 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13847 			 IFNAMSIZ - 4,
13848 			 pf->vsi[pf->lan_vsi]->netdev->name);
13849 		eth_random_addr(mac_addr);
13850 
13851 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13852 		i40e_add_mac_filter(vsi, mac_addr);
13853 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13854 	}
13855 
13856 	/* Add the broadcast filter so that we initially will receive
13857 	 * broadcast packets. Note that when a new VLAN is first added the
13858 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13859 	 * specific filters as part of transitioning into "vlan" operation.
13860 	 * When more VLANs are added, the driver will copy each existing MAC
13861 	 * filter and add it for the new VLAN.
13862 	 *
13863 	 * Broadcast filters are handled specially by
13864 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13865 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13866 	 * filter. The subtask will update the correct broadcast promiscuous
13867 	 * bits as VLANs become active or inactive.
13868 	 */
13869 	eth_broadcast_addr(broadcast);
13870 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13871 	i40e_add_mac_filter(vsi, broadcast);
13872 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13873 
13874 	eth_hw_addr_set(netdev, mac_addr);
13875 	ether_addr_copy(netdev->perm_addr, mac_addr);
13876 
13877 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13878 	netdev->neigh_priv_len = sizeof(u32) * 4;
13879 
13880 	netdev->priv_flags |= IFF_UNICAST_FLT;
13881 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13882 	/* Setup netdev TC information */
13883 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13884 
13885 	netdev->netdev_ops = &i40e_netdev_ops;
13886 	netdev->watchdog_timeo = 5 * HZ;
13887 	i40e_set_ethtool_ops(netdev);
13888 
13889 	/* MTU range: 68 - 9706 */
13890 	netdev->min_mtu = ETH_MIN_MTU;
13891 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13892 
13893 	return 0;
13894 }
13895 
13896 /**
13897  * i40e_vsi_delete - Delete a VSI from the switch
13898  * @vsi: the VSI being removed
13899  *
13900  * Returns 0 on success, negative value on failure
13901  **/
13902 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13903 {
13904 	/* remove default VSI is not allowed */
13905 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13906 		return;
13907 
13908 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13909 }
13910 
13911 /**
13912  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13913  * @vsi: the VSI being queried
13914  *
13915  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13916  **/
13917 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13918 {
13919 	struct i40e_veb *veb;
13920 	struct i40e_pf *pf = vsi->back;
13921 
13922 	/* Uplink is not a bridge so default to VEB */
13923 	if (vsi->veb_idx >= I40E_MAX_VEB)
13924 		return 1;
13925 
13926 	veb = pf->veb[vsi->veb_idx];
13927 	if (!veb) {
13928 		dev_info(&pf->pdev->dev,
13929 			 "There is no veb associated with the bridge\n");
13930 		return -ENOENT;
13931 	}
13932 
13933 	/* Uplink is a bridge in VEPA mode */
13934 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13935 		return 0;
13936 	} else {
13937 		/* Uplink is a bridge in VEB mode */
13938 		return 1;
13939 	}
13940 
13941 	/* VEPA is now default bridge, so return 0 */
13942 	return 0;
13943 }
13944 
13945 /**
13946  * i40e_add_vsi - Add a VSI to the switch
13947  * @vsi: the VSI being configured
13948  *
13949  * This initializes a VSI context depending on the VSI type to be added and
13950  * passes it down to the add_vsi aq command.
13951  **/
13952 static int i40e_add_vsi(struct i40e_vsi *vsi)
13953 {
13954 	int ret = -ENODEV;
13955 	struct i40e_pf *pf = vsi->back;
13956 	struct i40e_hw *hw = &pf->hw;
13957 	struct i40e_vsi_context ctxt;
13958 	struct i40e_mac_filter *f;
13959 	struct hlist_node *h;
13960 	int bkt;
13961 
13962 	u8 enabled_tc = 0x1; /* TC0 enabled */
13963 	int f_count = 0;
13964 
13965 	memset(&ctxt, 0, sizeof(ctxt));
13966 	switch (vsi->type) {
13967 	case I40E_VSI_MAIN:
13968 		/* The PF's main VSI is already setup as part of the
13969 		 * device initialization, so we'll not bother with
13970 		 * the add_vsi call, but we will retrieve the current
13971 		 * VSI context.
13972 		 */
13973 		ctxt.seid = pf->main_vsi_seid;
13974 		ctxt.pf_num = pf->hw.pf_id;
13975 		ctxt.vf_num = 0;
13976 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13977 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13978 		if (ret) {
13979 			dev_info(&pf->pdev->dev,
13980 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13981 				 ERR_PTR(ret),
13982 				 i40e_aq_str(&pf->hw,
13983 					     pf->hw.aq.asq_last_status));
13984 			return -ENOENT;
13985 		}
13986 		vsi->info = ctxt.info;
13987 		vsi->info.valid_sections = 0;
13988 
13989 		vsi->seid = ctxt.seid;
13990 		vsi->id = ctxt.vsi_number;
13991 
13992 		enabled_tc = i40e_pf_get_tc_map(pf);
13993 
13994 		/* Source pruning is enabled by default, so the flag is
13995 		 * negative logic - if it's set, we need to fiddle with
13996 		 * the VSI to disable source pruning.
13997 		 */
13998 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13999 			memset(&ctxt, 0, sizeof(ctxt));
14000 			ctxt.seid = pf->main_vsi_seid;
14001 			ctxt.pf_num = pf->hw.pf_id;
14002 			ctxt.vf_num = 0;
14003 			ctxt.info.valid_sections |=
14004 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14005 			ctxt.info.switch_id =
14006 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
14007 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14008 			if (ret) {
14009 				dev_info(&pf->pdev->dev,
14010 					 "update vsi failed, err %d aq_err %s\n",
14011 					 ret,
14012 					 i40e_aq_str(&pf->hw,
14013 						     pf->hw.aq.asq_last_status));
14014 				ret = -ENOENT;
14015 				goto err;
14016 			}
14017 		}
14018 
14019 		/* MFP mode setup queue map and update VSI */
14020 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
14021 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
14022 			memset(&ctxt, 0, sizeof(ctxt));
14023 			ctxt.seid = pf->main_vsi_seid;
14024 			ctxt.pf_num = pf->hw.pf_id;
14025 			ctxt.vf_num = 0;
14026 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
14027 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14028 			if (ret) {
14029 				dev_info(&pf->pdev->dev,
14030 					 "update vsi failed, err %pe aq_err %s\n",
14031 					 ERR_PTR(ret),
14032 					 i40e_aq_str(&pf->hw,
14033 						    pf->hw.aq.asq_last_status));
14034 				ret = -ENOENT;
14035 				goto err;
14036 			}
14037 			/* update the local VSI info queue map */
14038 			i40e_vsi_update_queue_map(vsi, &ctxt);
14039 			vsi->info.valid_sections = 0;
14040 		} else {
14041 			/* Default/Main VSI is only enabled for TC0
14042 			 * reconfigure it to enable all TCs that are
14043 			 * available on the port in SFP mode.
14044 			 * For MFP case the iSCSI PF would use this
14045 			 * flow to enable LAN+iSCSI TC.
14046 			 */
14047 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
14048 			if (ret) {
14049 				/* Single TC condition is not fatal,
14050 				 * message and continue
14051 				 */
14052 				dev_info(&pf->pdev->dev,
14053 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14054 					 enabled_tc,
14055 					 ERR_PTR(ret),
14056 					 i40e_aq_str(&pf->hw,
14057 						    pf->hw.aq.asq_last_status));
14058 			}
14059 		}
14060 		break;
14061 
14062 	case I40E_VSI_FDIR:
14063 		ctxt.pf_num = hw->pf_id;
14064 		ctxt.vf_num = 0;
14065 		ctxt.uplink_seid = vsi->uplink_seid;
14066 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14067 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14068 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14069 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14070 			ctxt.info.valid_sections |=
14071 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14072 			ctxt.info.switch_id =
14073 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14074 		}
14075 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14076 		break;
14077 
14078 	case I40E_VSI_VMDQ2:
14079 		ctxt.pf_num = hw->pf_id;
14080 		ctxt.vf_num = 0;
14081 		ctxt.uplink_seid = vsi->uplink_seid;
14082 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14083 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14084 
14085 		/* This VSI is connected to VEB so the switch_id
14086 		 * should be set to zero by default.
14087 		 */
14088 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14089 			ctxt.info.valid_sections |=
14090 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14091 			ctxt.info.switch_id =
14092 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14093 		}
14094 
14095 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14096 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14097 		break;
14098 
14099 	case I40E_VSI_SRIOV:
14100 		ctxt.pf_num = hw->pf_id;
14101 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14102 		ctxt.uplink_seid = vsi->uplink_seid;
14103 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14104 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14105 
14106 		/* This VSI is connected to VEB so the switch_id
14107 		 * should be set to zero by default.
14108 		 */
14109 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14110 			ctxt.info.valid_sections |=
14111 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14112 			ctxt.info.switch_id =
14113 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14114 		}
14115 
14116 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14117 			ctxt.info.valid_sections |=
14118 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14119 			ctxt.info.queueing_opt_flags |=
14120 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14121 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14122 		}
14123 
14124 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14125 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14126 		if (pf->vf[vsi->vf_id].spoofchk) {
14127 			ctxt.info.valid_sections |=
14128 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14129 			ctxt.info.sec_flags |=
14130 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14131 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14132 		}
14133 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14134 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14135 		break;
14136 
14137 	case I40E_VSI_IWARP:
14138 		/* send down message to iWARP */
14139 		break;
14140 
14141 	default:
14142 		return -ENODEV;
14143 	}
14144 
14145 	if (vsi->type != I40E_VSI_MAIN) {
14146 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14147 		if (ret) {
14148 			dev_info(&vsi->back->pdev->dev,
14149 				 "add vsi failed, err %pe aq_err %s\n",
14150 				 ERR_PTR(ret),
14151 				 i40e_aq_str(&pf->hw,
14152 					     pf->hw.aq.asq_last_status));
14153 			ret = -ENOENT;
14154 			goto err;
14155 		}
14156 		vsi->info = ctxt.info;
14157 		vsi->info.valid_sections = 0;
14158 		vsi->seid = ctxt.seid;
14159 		vsi->id = ctxt.vsi_number;
14160 	}
14161 
14162 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14163 	vsi->active_filters = 0;
14164 	/* If macvlan filters already exist, force them to get loaded */
14165 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14166 		f->state = I40E_FILTER_NEW;
14167 		f_count++;
14168 	}
14169 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14170 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14171 
14172 	if (f_count) {
14173 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14174 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14175 	}
14176 
14177 	/* Update VSI BW information */
14178 	ret = i40e_vsi_get_bw_info(vsi);
14179 	if (ret) {
14180 		dev_info(&pf->pdev->dev,
14181 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14182 			 ERR_PTR(ret),
14183 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14184 		/* VSI is already added so not tearing that up */
14185 		ret = 0;
14186 	}
14187 
14188 err:
14189 	return ret;
14190 }
14191 
14192 /**
14193  * i40e_vsi_release - Delete a VSI and free its resources
14194  * @vsi: the VSI being removed
14195  *
14196  * Returns 0 on success or < 0 on error
14197  **/
14198 int i40e_vsi_release(struct i40e_vsi *vsi)
14199 {
14200 	struct i40e_mac_filter *f;
14201 	struct hlist_node *h;
14202 	struct i40e_veb *veb = NULL;
14203 	struct i40e_pf *pf;
14204 	u16 uplink_seid;
14205 	int i, n, bkt;
14206 
14207 	pf = vsi->back;
14208 
14209 	/* release of a VEB-owner or last VSI is not allowed */
14210 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14211 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14212 			 vsi->seid, vsi->uplink_seid);
14213 		return -ENODEV;
14214 	}
14215 	if (vsi == pf->vsi[pf->lan_vsi] &&
14216 	    !test_bit(__I40E_DOWN, pf->state)) {
14217 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14218 		return -ENODEV;
14219 	}
14220 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14221 	uplink_seid = vsi->uplink_seid;
14222 
14223 	if (vsi->type != I40E_VSI_SRIOV) {
14224 		if (vsi->netdev_registered) {
14225 			vsi->netdev_registered = false;
14226 			if (vsi->netdev) {
14227 				/* results in a call to i40e_close() */
14228 				unregister_netdev(vsi->netdev);
14229 			}
14230 		} else {
14231 			i40e_vsi_close(vsi);
14232 		}
14233 		i40e_vsi_disable_irq(vsi);
14234 	}
14235 
14236 	if (vsi->type == I40E_VSI_MAIN)
14237 		i40e_devlink_destroy_port(pf);
14238 
14239 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14240 
14241 	/* clear the sync flag on all filters */
14242 	if (vsi->netdev) {
14243 		__dev_uc_unsync(vsi->netdev, NULL);
14244 		__dev_mc_unsync(vsi->netdev, NULL);
14245 	}
14246 
14247 	/* make sure any remaining filters are marked for deletion */
14248 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14249 		__i40e_del_filter(vsi, f);
14250 
14251 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14252 
14253 	i40e_sync_vsi_filters(vsi);
14254 
14255 	i40e_vsi_delete(vsi);
14256 	i40e_vsi_free_q_vectors(vsi);
14257 	if (vsi->netdev) {
14258 		free_netdev(vsi->netdev);
14259 		vsi->netdev = NULL;
14260 	}
14261 	i40e_vsi_clear_rings(vsi);
14262 	i40e_vsi_clear(vsi);
14263 
14264 	/* If this was the last thing on the VEB, except for the
14265 	 * controlling VSI, remove the VEB, which puts the controlling
14266 	 * VSI onto the next level down in the switch.
14267 	 *
14268 	 * Well, okay, there's one more exception here: don't remove
14269 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14270 	 * from up the network stack.
14271 	 */
14272 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14273 		if (pf->vsi[i] &&
14274 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14275 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14276 			n++;      /* count the VSIs */
14277 		}
14278 	}
14279 	for (i = 0; i < I40E_MAX_VEB; i++) {
14280 		if (!pf->veb[i])
14281 			continue;
14282 		if (pf->veb[i]->uplink_seid == uplink_seid)
14283 			n++;     /* count the VEBs */
14284 		if (pf->veb[i]->seid == uplink_seid)
14285 			veb = pf->veb[i];
14286 	}
14287 	if (n == 0 && veb && veb->uplink_seid != 0)
14288 		i40e_veb_release(veb);
14289 
14290 	return 0;
14291 }
14292 
14293 /**
14294  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14295  * @vsi: ptr to the VSI
14296  *
14297  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14298  * corresponding SW VSI structure and initializes num_queue_pairs for the
14299  * newly allocated VSI.
14300  *
14301  * Returns 0 on success or negative on failure
14302  **/
14303 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14304 {
14305 	int ret = -ENOENT;
14306 	struct i40e_pf *pf = vsi->back;
14307 
14308 	if (vsi->q_vectors[0]) {
14309 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14310 			 vsi->seid);
14311 		return -EEXIST;
14312 	}
14313 
14314 	if (vsi->base_vector) {
14315 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14316 			 vsi->seid, vsi->base_vector);
14317 		return -EEXIST;
14318 	}
14319 
14320 	ret = i40e_vsi_alloc_q_vectors(vsi);
14321 	if (ret) {
14322 		dev_info(&pf->pdev->dev,
14323 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14324 			 vsi->num_q_vectors, vsi->seid, ret);
14325 		vsi->num_q_vectors = 0;
14326 		goto vector_setup_out;
14327 	}
14328 
14329 	/* In Legacy mode, we do not have to get any other vector since we
14330 	 * piggyback on the misc/ICR0 for queue interrupts.
14331 	*/
14332 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14333 		return ret;
14334 	if (vsi->num_q_vectors)
14335 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14336 						 vsi->num_q_vectors, vsi->idx);
14337 	if (vsi->base_vector < 0) {
14338 		dev_info(&pf->pdev->dev,
14339 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14340 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14341 		i40e_vsi_free_q_vectors(vsi);
14342 		ret = -ENOENT;
14343 		goto vector_setup_out;
14344 	}
14345 
14346 vector_setup_out:
14347 	return ret;
14348 }
14349 
14350 /**
14351  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14352  * @vsi: pointer to the vsi.
14353  *
14354  * This re-allocates a vsi's queue resources.
14355  *
14356  * Returns pointer to the successfully allocated and configured VSI sw struct
14357  * on success, otherwise returns NULL on failure.
14358  **/
14359 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14360 {
14361 	u16 alloc_queue_pairs;
14362 	struct i40e_pf *pf;
14363 	u8 enabled_tc;
14364 	int ret;
14365 
14366 	if (!vsi)
14367 		return NULL;
14368 
14369 	pf = vsi->back;
14370 
14371 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14372 	i40e_vsi_clear_rings(vsi);
14373 
14374 	i40e_vsi_free_arrays(vsi, false);
14375 	i40e_set_num_rings_in_vsi(vsi);
14376 	ret = i40e_vsi_alloc_arrays(vsi, false);
14377 	if (ret)
14378 		goto err_vsi;
14379 
14380 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14381 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14382 
14383 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14384 	if (ret < 0) {
14385 		dev_info(&pf->pdev->dev,
14386 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14387 			 alloc_queue_pairs, vsi->seid, ret);
14388 		goto err_vsi;
14389 	}
14390 	vsi->base_queue = ret;
14391 
14392 	/* Update the FW view of the VSI. Force a reset of TC and queue
14393 	 * layout configurations.
14394 	 */
14395 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14396 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14397 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14398 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14399 	if (vsi->type == I40E_VSI_MAIN)
14400 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14401 
14402 	/* assign it some queues */
14403 	ret = i40e_alloc_rings(vsi);
14404 	if (ret)
14405 		goto err_rings;
14406 
14407 	/* map all of the rings to the q_vectors */
14408 	i40e_vsi_map_rings_to_vectors(vsi);
14409 	return vsi;
14410 
14411 err_rings:
14412 	i40e_vsi_free_q_vectors(vsi);
14413 	if (vsi->netdev_registered) {
14414 		vsi->netdev_registered = false;
14415 		unregister_netdev(vsi->netdev);
14416 		free_netdev(vsi->netdev);
14417 		vsi->netdev = NULL;
14418 	}
14419 	if (vsi->type == I40E_VSI_MAIN)
14420 		i40e_devlink_destroy_port(pf);
14421 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14422 err_vsi:
14423 	i40e_vsi_clear(vsi);
14424 	return NULL;
14425 }
14426 
14427 /**
14428  * i40e_vsi_setup - Set up a VSI by a given type
14429  * @pf: board private structure
14430  * @type: VSI type
14431  * @uplink_seid: the switch element to link to
14432  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14433  *
14434  * This allocates the sw VSI structure and its queue resources, then add a VSI
14435  * to the identified VEB.
14436  *
14437  * Returns pointer to the successfully allocated and configure VSI sw struct on
14438  * success, otherwise returns NULL on failure.
14439  **/
14440 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14441 				u16 uplink_seid, u32 param1)
14442 {
14443 	struct i40e_vsi *vsi = NULL;
14444 	struct i40e_veb *veb = NULL;
14445 	u16 alloc_queue_pairs;
14446 	int ret, i;
14447 	int v_idx;
14448 
14449 	/* The requested uplink_seid must be either
14450 	 *     - the PF's port seid
14451 	 *              no VEB is needed because this is the PF
14452 	 *              or this is a Flow Director special case VSI
14453 	 *     - seid of an existing VEB
14454 	 *     - seid of a VSI that owns an existing VEB
14455 	 *     - seid of a VSI that doesn't own a VEB
14456 	 *              a new VEB is created and the VSI becomes the owner
14457 	 *     - seid of the PF VSI, which is what creates the first VEB
14458 	 *              this is a special case of the previous
14459 	 *
14460 	 * Find which uplink_seid we were given and create a new VEB if needed
14461 	 */
14462 	for (i = 0; i < I40E_MAX_VEB; i++) {
14463 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14464 			veb = pf->veb[i];
14465 			break;
14466 		}
14467 	}
14468 
14469 	if (!veb && uplink_seid != pf->mac_seid) {
14470 
14471 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14472 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14473 				vsi = pf->vsi[i];
14474 				break;
14475 			}
14476 		}
14477 		if (!vsi) {
14478 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14479 				 uplink_seid);
14480 			return NULL;
14481 		}
14482 
14483 		if (vsi->uplink_seid == pf->mac_seid)
14484 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14485 					     vsi->tc_config.enabled_tc);
14486 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14487 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14488 					     vsi->tc_config.enabled_tc);
14489 		if (veb) {
14490 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14491 				dev_info(&vsi->back->pdev->dev,
14492 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14493 				return NULL;
14494 			}
14495 			/* We come up by default in VEPA mode if SRIOV is not
14496 			 * already enabled, in which case we can't force VEPA
14497 			 * mode.
14498 			 */
14499 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14500 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14501 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14502 			}
14503 			i40e_config_bridge_mode(veb);
14504 		}
14505 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14506 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14507 				veb = pf->veb[i];
14508 		}
14509 		if (!veb) {
14510 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14511 			return NULL;
14512 		}
14513 
14514 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14515 		uplink_seid = veb->seid;
14516 	}
14517 
14518 	/* get vsi sw struct */
14519 	v_idx = i40e_vsi_mem_alloc(pf, type);
14520 	if (v_idx < 0)
14521 		goto err_alloc;
14522 	vsi = pf->vsi[v_idx];
14523 	if (!vsi)
14524 		goto err_alloc;
14525 	vsi->type = type;
14526 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14527 
14528 	if (type == I40E_VSI_MAIN)
14529 		pf->lan_vsi = v_idx;
14530 	else if (type == I40E_VSI_SRIOV)
14531 		vsi->vf_id = param1;
14532 	/* assign it some queues */
14533 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14534 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14535 
14536 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14537 	if (ret < 0) {
14538 		dev_info(&pf->pdev->dev,
14539 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14540 			 alloc_queue_pairs, vsi->seid, ret);
14541 		goto err_vsi;
14542 	}
14543 	vsi->base_queue = ret;
14544 
14545 	/* get a VSI from the hardware */
14546 	vsi->uplink_seid = uplink_seid;
14547 	ret = i40e_add_vsi(vsi);
14548 	if (ret)
14549 		goto err_vsi;
14550 
14551 	switch (vsi->type) {
14552 	/* setup the netdev if needed */
14553 	case I40E_VSI_MAIN:
14554 	case I40E_VSI_VMDQ2:
14555 		ret = i40e_config_netdev(vsi);
14556 		if (ret)
14557 			goto err_netdev;
14558 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14559 		if (ret)
14560 			goto err_netdev;
14561 		if (vsi->type == I40E_VSI_MAIN) {
14562 			ret = i40e_devlink_create_port(pf);
14563 			if (ret)
14564 				goto err_netdev;
14565 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14566 		}
14567 		ret = register_netdev(vsi->netdev);
14568 		if (ret)
14569 			goto err_dl_port;
14570 		vsi->netdev_registered = true;
14571 		netif_carrier_off(vsi->netdev);
14572 #ifdef CONFIG_I40E_DCB
14573 		/* Setup DCB netlink interface */
14574 		i40e_dcbnl_setup(vsi);
14575 #endif /* CONFIG_I40E_DCB */
14576 		fallthrough;
14577 	case I40E_VSI_FDIR:
14578 		/* set up vectors and rings if needed */
14579 		ret = i40e_vsi_setup_vectors(vsi);
14580 		if (ret)
14581 			goto err_msix;
14582 
14583 		ret = i40e_alloc_rings(vsi);
14584 		if (ret)
14585 			goto err_rings;
14586 
14587 		/* map all of the rings to the q_vectors */
14588 		i40e_vsi_map_rings_to_vectors(vsi);
14589 
14590 		i40e_vsi_reset_stats(vsi);
14591 		break;
14592 	default:
14593 		/* no netdev or rings for the other VSI types */
14594 		break;
14595 	}
14596 
14597 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14598 	    (vsi->type == I40E_VSI_VMDQ2)) {
14599 		ret = i40e_vsi_config_rss(vsi);
14600 	}
14601 	return vsi;
14602 
14603 err_rings:
14604 	i40e_vsi_free_q_vectors(vsi);
14605 err_msix:
14606 	if (vsi->netdev_registered) {
14607 		vsi->netdev_registered = false;
14608 		unregister_netdev(vsi->netdev);
14609 		free_netdev(vsi->netdev);
14610 		vsi->netdev = NULL;
14611 	}
14612 err_dl_port:
14613 	if (vsi->type == I40E_VSI_MAIN)
14614 		i40e_devlink_destroy_port(pf);
14615 err_netdev:
14616 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14617 err_vsi:
14618 	i40e_vsi_clear(vsi);
14619 err_alloc:
14620 	return NULL;
14621 }
14622 
14623 /**
14624  * i40e_veb_get_bw_info - Query VEB BW information
14625  * @veb: the veb to query
14626  *
14627  * Query the Tx scheduler BW configuration data for given VEB
14628  **/
14629 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14630 {
14631 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14632 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14633 	struct i40e_pf *pf = veb->pf;
14634 	struct i40e_hw *hw = &pf->hw;
14635 	u32 tc_bw_max;
14636 	int ret = 0;
14637 	int i;
14638 
14639 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14640 						  &bw_data, NULL);
14641 	if (ret) {
14642 		dev_info(&pf->pdev->dev,
14643 			 "query veb bw config failed, err %pe aq_err %s\n",
14644 			 ERR_PTR(ret),
14645 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14646 		goto out;
14647 	}
14648 
14649 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14650 						   &ets_data, NULL);
14651 	if (ret) {
14652 		dev_info(&pf->pdev->dev,
14653 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14654 			 ERR_PTR(ret),
14655 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14656 		goto out;
14657 	}
14658 
14659 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14660 	veb->bw_max_quanta = ets_data.tc_bw_max;
14661 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14662 	veb->enabled_tc = ets_data.tc_valid_bits;
14663 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14664 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14665 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14666 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14667 		veb->bw_tc_limit_credits[i] =
14668 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14669 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14670 	}
14671 
14672 out:
14673 	return ret;
14674 }
14675 
14676 /**
14677  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14678  * @pf: board private structure
14679  *
14680  * On error: returns error code (negative)
14681  * On success: returns vsi index in PF (positive)
14682  **/
14683 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14684 {
14685 	int ret = -ENOENT;
14686 	struct i40e_veb *veb;
14687 	int i;
14688 
14689 	/* Need to protect the allocation of switch elements at the PF level */
14690 	mutex_lock(&pf->switch_mutex);
14691 
14692 	/* VEB list may be fragmented if VEB creation/destruction has
14693 	 * been happening.  We can afford to do a quick scan to look
14694 	 * for any free slots in the list.
14695 	 *
14696 	 * find next empty veb slot, looping back around if necessary
14697 	 */
14698 	i = 0;
14699 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14700 		i++;
14701 	if (i >= I40E_MAX_VEB) {
14702 		ret = -ENOMEM;
14703 		goto err_alloc_veb;  /* out of VEB slots! */
14704 	}
14705 
14706 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14707 	if (!veb) {
14708 		ret = -ENOMEM;
14709 		goto err_alloc_veb;
14710 	}
14711 	veb->pf = pf;
14712 	veb->idx = i;
14713 	veb->enabled_tc = 1;
14714 
14715 	pf->veb[i] = veb;
14716 	ret = i;
14717 err_alloc_veb:
14718 	mutex_unlock(&pf->switch_mutex);
14719 	return ret;
14720 }
14721 
14722 /**
14723  * i40e_switch_branch_release - Delete a branch of the switch tree
14724  * @branch: where to start deleting
14725  *
14726  * This uses recursion to find the tips of the branch to be
14727  * removed, deleting until we get back to and can delete this VEB.
14728  **/
14729 static void i40e_switch_branch_release(struct i40e_veb *branch)
14730 {
14731 	struct i40e_pf *pf = branch->pf;
14732 	u16 branch_seid = branch->seid;
14733 	u16 veb_idx = branch->idx;
14734 	int i;
14735 
14736 	/* release any VEBs on this VEB - RECURSION */
14737 	for (i = 0; i < I40E_MAX_VEB; i++) {
14738 		if (!pf->veb[i])
14739 			continue;
14740 		if (pf->veb[i]->uplink_seid == branch->seid)
14741 			i40e_switch_branch_release(pf->veb[i]);
14742 	}
14743 
14744 	/* Release the VSIs on this VEB, but not the owner VSI.
14745 	 *
14746 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14747 	 *       the VEB itself, so don't use (*branch) after this loop.
14748 	 */
14749 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14750 		if (!pf->vsi[i])
14751 			continue;
14752 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14753 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14754 			i40e_vsi_release(pf->vsi[i]);
14755 		}
14756 	}
14757 
14758 	/* There's one corner case where the VEB might not have been
14759 	 * removed, so double check it here and remove it if needed.
14760 	 * This case happens if the veb was created from the debugfs
14761 	 * commands and no VSIs were added to it.
14762 	 */
14763 	if (pf->veb[veb_idx])
14764 		i40e_veb_release(pf->veb[veb_idx]);
14765 }
14766 
14767 /**
14768  * i40e_veb_clear - remove veb struct
14769  * @veb: the veb to remove
14770  **/
14771 static void i40e_veb_clear(struct i40e_veb *veb)
14772 {
14773 	if (!veb)
14774 		return;
14775 
14776 	if (veb->pf) {
14777 		struct i40e_pf *pf = veb->pf;
14778 
14779 		mutex_lock(&pf->switch_mutex);
14780 		if (pf->veb[veb->idx] == veb)
14781 			pf->veb[veb->idx] = NULL;
14782 		mutex_unlock(&pf->switch_mutex);
14783 	}
14784 
14785 	kfree(veb);
14786 }
14787 
14788 /**
14789  * i40e_veb_release - Delete a VEB and free its resources
14790  * @veb: the VEB being removed
14791  **/
14792 void i40e_veb_release(struct i40e_veb *veb)
14793 {
14794 	struct i40e_vsi *vsi = NULL;
14795 	struct i40e_pf *pf;
14796 	int i, n = 0;
14797 
14798 	pf = veb->pf;
14799 
14800 	/* find the remaining VSI and check for extras */
14801 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14802 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14803 			n++;
14804 			vsi = pf->vsi[i];
14805 		}
14806 	}
14807 	if (n != 1) {
14808 		dev_info(&pf->pdev->dev,
14809 			 "can't remove VEB %d with %d VSIs left\n",
14810 			 veb->seid, n);
14811 		return;
14812 	}
14813 
14814 	/* move the remaining VSI to uplink veb */
14815 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14816 	if (veb->uplink_seid) {
14817 		vsi->uplink_seid = veb->uplink_seid;
14818 		if (veb->uplink_seid == pf->mac_seid)
14819 			vsi->veb_idx = I40E_NO_VEB;
14820 		else
14821 			vsi->veb_idx = veb->veb_idx;
14822 	} else {
14823 		/* floating VEB */
14824 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14825 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14826 	}
14827 
14828 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14829 	i40e_veb_clear(veb);
14830 }
14831 
14832 /**
14833  * i40e_add_veb - create the VEB in the switch
14834  * @veb: the VEB to be instantiated
14835  * @vsi: the controlling VSI
14836  **/
14837 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14838 {
14839 	struct i40e_pf *pf = veb->pf;
14840 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14841 	int ret;
14842 
14843 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14844 			      veb->enabled_tc, false,
14845 			      &veb->seid, enable_stats, NULL);
14846 
14847 	/* get a VEB from the hardware */
14848 	if (ret) {
14849 		dev_info(&pf->pdev->dev,
14850 			 "couldn't add VEB, err %pe aq_err %s\n",
14851 			 ERR_PTR(ret),
14852 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14853 		return -EPERM;
14854 	}
14855 
14856 	/* get statistics counter */
14857 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14858 					 &veb->stats_idx, NULL, NULL, NULL);
14859 	if (ret) {
14860 		dev_info(&pf->pdev->dev,
14861 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14862 			 ERR_PTR(ret),
14863 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14864 		return -EPERM;
14865 	}
14866 	ret = i40e_veb_get_bw_info(veb);
14867 	if (ret) {
14868 		dev_info(&pf->pdev->dev,
14869 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14870 			 ERR_PTR(ret),
14871 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14872 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14873 		return -ENOENT;
14874 	}
14875 
14876 	vsi->uplink_seid = veb->seid;
14877 	vsi->veb_idx = veb->idx;
14878 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14879 
14880 	return 0;
14881 }
14882 
14883 /**
14884  * i40e_veb_setup - Set up a VEB
14885  * @pf: board private structure
14886  * @flags: VEB setup flags
14887  * @uplink_seid: the switch element to link to
14888  * @vsi_seid: the initial VSI seid
14889  * @enabled_tc: Enabled TC bit-map
14890  *
14891  * This allocates the sw VEB structure and links it into the switch
14892  * It is possible and legal for this to be a duplicate of an already
14893  * existing VEB.  It is also possible for both uplink and vsi seids
14894  * to be zero, in order to create a floating VEB.
14895  *
14896  * Returns pointer to the successfully allocated VEB sw struct on
14897  * success, otherwise returns NULL on failure.
14898  **/
14899 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14900 				u16 uplink_seid, u16 vsi_seid,
14901 				u8 enabled_tc)
14902 {
14903 	struct i40e_veb *veb, *uplink_veb = NULL;
14904 	int vsi_idx, veb_idx;
14905 	int ret;
14906 
14907 	/* if one seid is 0, the other must be 0 to create a floating relay */
14908 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14909 	    (uplink_seid + vsi_seid != 0)) {
14910 		dev_info(&pf->pdev->dev,
14911 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14912 			 uplink_seid, vsi_seid);
14913 		return NULL;
14914 	}
14915 
14916 	/* make sure there is such a vsi and uplink */
14917 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14918 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14919 			break;
14920 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14921 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14922 			 vsi_seid);
14923 		return NULL;
14924 	}
14925 
14926 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14927 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14928 			if (pf->veb[veb_idx] &&
14929 			    pf->veb[veb_idx]->seid == uplink_seid) {
14930 				uplink_veb = pf->veb[veb_idx];
14931 				break;
14932 			}
14933 		}
14934 		if (!uplink_veb) {
14935 			dev_info(&pf->pdev->dev,
14936 				 "uplink seid %d not found\n", uplink_seid);
14937 			return NULL;
14938 		}
14939 	}
14940 
14941 	/* get veb sw struct */
14942 	veb_idx = i40e_veb_mem_alloc(pf);
14943 	if (veb_idx < 0)
14944 		goto err_alloc;
14945 	veb = pf->veb[veb_idx];
14946 	veb->flags = flags;
14947 	veb->uplink_seid = uplink_seid;
14948 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14949 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14950 
14951 	/* create the VEB in the switch */
14952 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14953 	if (ret)
14954 		goto err_veb;
14955 	if (vsi_idx == pf->lan_vsi)
14956 		pf->lan_veb = veb->idx;
14957 
14958 	return veb;
14959 
14960 err_veb:
14961 	i40e_veb_clear(veb);
14962 err_alloc:
14963 	return NULL;
14964 }
14965 
14966 /**
14967  * i40e_setup_pf_switch_element - set PF vars based on switch type
14968  * @pf: board private structure
14969  * @ele: element we are building info from
14970  * @num_reported: total number of elements
14971  * @printconfig: should we print the contents
14972  *
14973  * helper function to assist in extracting a few useful SEID values.
14974  **/
14975 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14976 				struct i40e_aqc_switch_config_element_resp *ele,
14977 				u16 num_reported, bool printconfig)
14978 {
14979 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14980 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14981 	u8 element_type = ele->element_type;
14982 	u16 seid = le16_to_cpu(ele->seid);
14983 
14984 	if (printconfig)
14985 		dev_info(&pf->pdev->dev,
14986 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14987 			 element_type, seid, uplink_seid, downlink_seid);
14988 
14989 	switch (element_type) {
14990 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14991 		pf->mac_seid = seid;
14992 		break;
14993 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14994 		/* Main VEB? */
14995 		if (uplink_seid != pf->mac_seid)
14996 			break;
14997 		if (pf->lan_veb >= I40E_MAX_VEB) {
14998 			int v;
14999 
15000 			/* find existing or else empty VEB */
15001 			for (v = 0; v < I40E_MAX_VEB; v++) {
15002 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
15003 					pf->lan_veb = v;
15004 					break;
15005 				}
15006 			}
15007 			if (pf->lan_veb >= I40E_MAX_VEB) {
15008 				v = i40e_veb_mem_alloc(pf);
15009 				if (v < 0)
15010 					break;
15011 				pf->lan_veb = v;
15012 			}
15013 		}
15014 		if (pf->lan_veb >= I40E_MAX_VEB)
15015 			break;
15016 
15017 		pf->veb[pf->lan_veb]->seid = seid;
15018 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
15019 		pf->veb[pf->lan_veb]->pf = pf;
15020 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
15021 		break;
15022 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
15023 		if (num_reported != 1)
15024 			break;
15025 		/* This is immediately after a reset so we can assume this is
15026 		 * the PF's VSI
15027 		 */
15028 		pf->mac_seid = uplink_seid;
15029 		pf->pf_seid = downlink_seid;
15030 		pf->main_vsi_seid = seid;
15031 		if (printconfig)
15032 			dev_info(&pf->pdev->dev,
15033 				 "pf_seid=%d main_vsi_seid=%d\n",
15034 				 pf->pf_seid, pf->main_vsi_seid);
15035 		break;
15036 	case I40E_SWITCH_ELEMENT_TYPE_PF:
15037 	case I40E_SWITCH_ELEMENT_TYPE_VF:
15038 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
15039 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
15040 	case I40E_SWITCH_ELEMENT_TYPE_PE:
15041 	case I40E_SWITCH_ELEMENT_TYPE_PA:
15042 		/* ignore these for now */
15043 		break;
15044 	default:
15045 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
15046 			 element_type, seid);
15047 		break;
15048 	}
15049 }
15050 
15051 /**
15052  * i40e_fetch_switch_configuration - Get switch config from firmware
15053  * @pf: board private structure
15054  * @printconfig: should we print the contents
15055  *
15056  * Get the current switch configuration from the device and
15057  * extract a few useful SEID values.
15058  **/
15059 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15060 {
15061 	struct i40e_aqc_get_switch_config_resp *sw_config;
15062 	u16 next_seid = 0;
15063 	int ret = 0;
15064 	u8 *aq_buf;
15065 	int i;
15066 
15067 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15068 	if (!aq_buf)
15069 		return -ENOMEM;
15070 
15071 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15072 	do {
15073 		u16 num_reported, num_total;
15074 
15075 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15076 						I40E_AQ_LARGE_BUF,
15077 						&next_seid, NULL);
15078 		if (ret) {
15079 			dev_info(&pf->pdev->dev,
15080 				 "get switch config failed err %d aq_err %s\n",
15081 				 ret,
15082 				 i40e_aq_str(&pf->hw,
15083 					     pf->hw.aq.asq_last_status));
15084 			kfree(aq_buf);
15085 			return -ENOENT;
15086 		}
15087 
15088 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15089 		num_total = le16_to_cpu(sw_config->header.num_total);
15090 
15091 		if (printconfig)
15092 			dev_info(&pf->pdev->dev,
15093 				 "header: %d reported %d total\n",
15094 				 num_reported, num_total);
15095 
15096 		for (i = 0; i < num_reported; i++) {
15097 			struct i40e_aqc_switch_config_element_resp *ele =
15098 				&sw_config->element[i];
15099 
15100 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15101 						     printconfig);
15102 		}
15103 	} while (next_seid != 0);
15104 
15105 	kfree(aq_buf);
15106 	return ret;
15107 }
15108 
15109 /**
15110  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15111  * @pf: board private structure
15112  * @reinit: if the Main VSI needs to re-initialized.
15113  * @lock_acquired: indicates whether or not the lock has been acquired
15114  *
15115  * Returns 0 on success, negative value on failure
15116  **/
15117 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15118 {
15119 	u16 flags = 0;
15120 	int ret;
15121 
15122 	/* find out what's out there already */
15123 	ret = i40e_fetch_switch_configuration(pf, false);
15124 	if (ret) {
15125 		dev_info(&pf->pdev->dev,
15126 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15127 			 ERR_PTR(ret),
15128 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15129 		return ret;
15130 	}
15131 	i40e_pf_reset_stats(pf);
15132 
15133 	/* set the switch config bit for the whole device to
15134 	 * support limited promisc or true promisc
15135 	 * when user requests promisc. The default is limited
15136 	 * promisc.
15137 	*/
15138 
15139 	if ((pf->hw.pf_id == 0) &&
15140 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15141 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15142 		pf->last_sw_conf_flags = flags;
15143 	}
15144 
15145 	if (pf->hw.pf_id == 0) {
15146 		u16 valid_flags;
15147 
15148 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15149 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15150 						NULL);
15151 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15152 			dev_info(&pf->pdev->dev,
15153 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15154 				 ERR_PTR(ret),
15155 				 i40e_aq_str(&pf->hw,
15156 					     pf->hw.aq.asq_last_status));
15157 			/* not a fatal problem, just keep going */
15158 		}
15159 		pf->last_sw_conf_valid_flags = valid_flags;
15160 	}
15161 
15162 	/* first time setup */
15163 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15164 		struct i40e_vsi *vsi = NULL;
15165 		u16 uplink_seid;
15166 
15167 		/* Set up the PF VSI associated with the PF's main VSI
15168 		 * that is already in the HW switch
15169 		 */
15170 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15171 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15172 		else
15173 			uplink_seid = pf->mac_seid;
15174 		if (pf->lan_vsi == I40E_NO_VSI)
15175 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15176 		else if (reinit)
15177 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15178 		if (!vsi) {
15179 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15180 			i40e_cloud_filter_exit(pf);
15181 			i40e_fdir_teardown(pf);
15182 			return -EAGAIN;
15183 		}
15184 	} else {
15185 		/* force a reset of TC and queue layout configurations */
15186 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15187 
15188 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15189 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15190 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15191 	}
15192 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15193 
15194 	i40e_fdir_sb_setup(pf);
15195 
15196 	/* Setup static PF queue filter control settings */
15197 	ret = i40e_setup_pf_filter_control(pf);
15198 	if (ret) {
15199 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15200 			 ret);
15201 		/* Failure here should not stop continuing other steps */
15202 	}
15203 
15204 	/* enable RSS in the HW, even for only one queue, as the stack can use
15205 	 * the hash
15206 	 */
15207 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15208 		i40e_pf_config_rss(pf);
15209 
15210 	/* fill in link information and enable LSE reporting */
15211 	i40e_link_event(pf);
15212 
15213 	/* Initialize user-specific link properties */
15214 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15215 				  I40E_AQ_AN_COMPLETED) ? true : false);
15216 
15217 	i40e_ptp_init(pf);
15218 
15219 	if (!lock_acquired)
15220 		rtnl_lock();
15221 
15222 	/* repopulate tunnel port filters */
15223 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15224 
15225 	if (!lock_acquired)
15226 		rtnl_unlock();
15227 
15228 	return ret;
15229 }
15230 
15231 /**
15232  * i40e_determine_queue_usage - Work out queue distribution
15233  * @pf: board private structure
15234  **/
15235 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15236 {
15237 	int queues_left;
15238 	int q_max;
15239 
15240 	pf->num_lan_qps = 0;
15241 
15242 	/* Find the max queues to be put into basic use.  We'll always be
15243 	 * using TC0, whether or not DCB is running, and TC0 will get the
15244 	 * big RSS set.
15245 	 */
15246 	queues_left = pf->hw.func_caps.num_tx_qp;
15247 
15248 	if ((queues_left == 1) ||
15249 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15250 		/* one qp for PF, no queues for anything else */
15251 		queues_left = 0;
15252 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15253 
15254 		/* make sure all the fancies are disabled */
15255 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15256 			       I40E_FLAG_IWARP_ENABLED	|
15257 			       I40E_FLAG_FD_SB_ENABLED	|
15258 			       I40E_FLAG_FD_ATR_ENABLED	|
15259 			       I40E_FLAG_DCB_CAPABLE	|
15260 			       I40E_FLAG_DCB_ENABLED	|
15261 			       I40E_FLAG_SRIOV_ENABLED	|
15262 			       I40E_FLAG_VMDQ_ENABLED);
15263 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15264 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15265 				  I40E_FLAG_FD_SB_ENABLED |
15266 				  I40E_FLAG_FD_ATR_ENABLED |
15267 				  I40E_FLAG_DCB_CAPABLE))) {
15268 		/* one qp for PF */
15269 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15270 		queues_left -= pf->num_lan_qps;
15271 
15272 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15273 			       I40E_FLAG_IWARP_ENABLED	|
15274 			       I40E_FLAG_FD_SB_ENABLED	|
15275 			       I40E_FLAG_FD_ATR_ENABLED	|
15276 			       I40E_FLAG_DCB_ENABLED	|
15277 			       I40E_FLAG_VMDQ_ENABLED);
15278 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15279 	} else {
15280 		/* Not enough queues for all TCs */
15281 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15282 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15283 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15284 					I40E_FLAG_DCB_ENABLED);
15285 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15286 		}
15287 
15288 		/* limit lan qps to the smaller of qps, cpus or msix */
15289 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15290 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15291 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15292 		pf->num_lan_qps = q_max;
15293 
15294 		queues_left -= pf->num_lan_qps;
15295 	}
15296 
15297 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15298 		if (queues_left > 1) {
15299 			queues_left -= 1; /* save 1 queue for FD */
15300 		} else {
15301 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15302 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15303 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15304 		}
15305 	}
15306 
15307 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15308 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15309 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15310 					(queues_left / pf->num_vf_qps));
15311 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15312 	}
15313 
15314 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15315 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15316 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15317 					  (queues_left / pf->num_vmdq_qps));
15318 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15319 	}
15320 
15321 	pf->queues_left = queues_left;
15322 	dev_dbg(&pf->pdev->dev,
15323 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15324 		pf->hw.func_caps.num_tx_qp,
15325 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15326 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15327 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15328 		queues_left);
15329 }
15330 
15331 /**
15332  * i40e_setup_pf_filter_control - Setup PF static filter control
15333  * @pf: PF to be setup
15334  *
15335  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15336  * settings. If PE/FCoE are enabled then it will also set the per PF
15337  * based filter sizes required for them. It also enables Flow director,
15338  * ethertype and macvlan type filter settings for the pf.
15339  *
15340  * Returns 0 on success, negative on failure
15341  **/
15342 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15343 {
15344 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15345 
15346 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15347 
15348 	/* Flow Director is enabled */
15349 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15350 		settings->enable_fdir = true;
15351 
15352 	/* Ethtype and MACVLAN filters enabled for PF */
15353 	settings->enable_ethtype = true;
15354 	settings->enable_macvlan = true;
15355 
15356 	if (i40e_set_filter_control(&pf->hw, settings))
15357 		return -ENOENT;
15358 
15359 	return 0;
15360 }
15361 
15362 #define INFO_STRING_LEN 255
15363 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15364 static void i40e_print_features(struct i40e_pf *pf)
15365 {
15366 	struct i40e_hw *hw = &pf->hw;
15367 	char *buf;
15368 	int i;
15369 
15370 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15371 	if (!buf)
15372 		return;
15373 
15374 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15375 #ifdef CONFIG_PCI_IOV
15376 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15377 #endif
15378 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15379 		      pf->hw.func_caps.num_vsis,
15380 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15381 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15382 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15383 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15384 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15385 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15386 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15387 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15388 	}
15389 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15390 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15391 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15392 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15393 	if (pf->flags & I40E_FLAG_PTP)
15394 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15395 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15396 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15397 	else
15398 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15399 
15400 	dev_info(&pf->pdev->dev, "%s\n", buf);
15401 	kfree(buf);
15402 	WARN_ON(i > INFO_STRING_LEN);
15403 }
15404 
15405 /**
15406  * i40e_get_platform_mac_addr - get platform-specific MAC address
15407  * @pdev: PCI device information struct
15408  * @pf: board private structure
15409  *
15410  * Look up the MAC address for the device. First we'll try
15411  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15412  * specific fallback. Otherwise, we'll default to the stored value in
15413  * firmware.
15414  **/
15415 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15416 {
15417 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15418 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15419 }
15420 
15421 /**
15422  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15423  * @fec_cfg: FEC option to set in flags
15424  * @flags: ptr to flags in which we set FEC option
15425  **/
15426 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15427 {
15428 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15429 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15430 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15431 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15432 		*flags |= I40E_FLAG_RS_FEC;
15433 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15434 	}
15435 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15436 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15437 		*flags |= I40E_FLAG_BASE_R_FEC;
15438 		*flags &= ~I40E_FLAG_RS_FEC;
15439 	}
15440 	if (fec_cfg == 0)
15441 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15442 }
15443 
15444 /**
15445  * i40e_check_recovery_mode - check if we are running transition firmware
15446  * @pf: board private structure
15447  *
15448  * Check registers indicating the firmware runs in recovery mode. Sets the
15449  * appropriate driver state.
15450  *
15451  * Returns true if the recovery mode was detected, false otherwise
15452  **/
15453 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15454 {
15455 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15456 
15457 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15458 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15459 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15460 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15461 
15462 		return true;
15463 	}
15464 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15465 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15466 
15467 	return false;
15468 }
15469 
15470 /**
15471  * i40e_pf_loop_reset - perform reset in a loop.
15472  * @pf: board private structure
15473  *
15474  * This function is useful when a NIC is about to enter recovery mode.
15475  * When a NIC's internal data structures are corrupted the NIC's
15476  * firmware is going to enter recovery mode.
15477  * Right after a POR it takes about 7 minutes for firmware to enter
15478  * recovery mode. Until that time a NIC is in some kind of intermediate
15479  * state. After that time period the NIC almost surely enters
15480  * recovery mode. The only way for a driver to detect intermediate
15481  * state is to issue a series of pf-resets and check a return value.
15482  * If a PF reset returns success then the firmware could be in recovery
15483  * mode so the caller of this code needs to check for recovery mode
15484  * if this function returns success. There is a little chance that
15485  * firmware will hang in intermediate state forever.
15486  * Since waiting 7 minutes is quite a lot of time this function waits
15487  * 10 seconds and then gives up by returning an error.
15488  *
15489  * Return 0 on success, negative on failure.
15490  **/
15491 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15492 {
15493 	/* wait max 10 seconds for PF reset to succeed */
15494 	const unsigned long time_end = jiffies + 10 * HZ;
15495 	struct i40e_hw *hw = &pf->hw;
15496 	int ret;
15497 
15498 	ret = i40e_pf_reset(hw);
15499 	while (ret != 0 && time_before(jiffies, time_end)) {
15500 		usleep_range(10000, 20000);
15501 		ret = i40e_pf_reset(hw);
15502 	}
15503 
15504 	if (ret == 0)
15505 		pf->pfr_count++;
15506 	else
15507 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15508 
15509 	return ret;
15510 }
15511 
15512 /**
15513  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15514  * @pf: board private structure
15515  *
15516  * Check FW registers to determine if FW issued unexpected EMP Reset.
15517  * Every time when unexpected EMP Reset occurs the FW increments
15518  * a counter of unexpected EMP Resets. When the counter reaches 10
15519  * the FW should enter the Recovery mode
15520  *
15521  * Returns true if FW issued unexpected EMP Reset
15522  **/
15523 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15524 {
15525 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15526 			   I40E_GL_FWSTS_FWS1B_MASK;
15527 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15528 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15529 }
15530 
15531 /**
15532  * i40e_handle_resets - handle EMP resets and PF resets
15533  * @pf: board private structure
15534  *
15535  * Handle both EMP resets and PF resets and conclude whether there are
15536  * any issues regarding these resets. If there are any issues then
15537  * generate log entry.
15538  *
15539  * Return 0 if NIC is healthy or negative value when there are issues
15540  * with resets
15541  **/
15542 static int i40e_handle_resets(struct i40e_pf *pf)
15543 {
15544 	const int pfr = i40e_pf_loop_reset(pf);
15545 	const bool is_empr = i40e_check_fw_empr(pf);
15546 
15547 	if (is_empr || pfr != 0)
15548 		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");
15549 
15550 	return is_empr ? -EIO : pfr;
15551 }
15552 
15553 /**
15554  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15555  * @pf: board private structure
15556  * @hw: ptr to the hardware info
15557  *
15558  * This function does a minimal setup of all subsystems needed for running
15559  * recovery mode.
15560  *
15561  * Returns 0 on success, negative on failure
15562  **/
15563 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15564 {
15565 	struct i40e_vsi *vsi;
15566 	int err;
15567 	int v_idx;
15568 
15569 	pci_set_drvdata(pf->pdev, pf);
15570 	pci_save_state(pf->pdev);
15571 
15572 	/* set up periodic task facility */
15573 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15574 	pf->service_timer_period = HZ;
15575 
15576 	INIT_WORK(&pf->service_task, i40e_service_task);
15577 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15578 
15579 	err = i40e_init_interrupt_scheme(pf);
15580 	if (err)
15581 		goto err_switch_setup;
15582 
15583 	/* The number of VSIs reported by the FW is the minimum guaranteed
15584 	 * to us; HW supports far more and we share the remaining pool with
15585 	 * the other PFs. We allocate space for more than the guarantee with
15586 	 * the understanding that we might not get them all later.
15587 	 */
15588 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15589 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15590 	else
15591 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15592 
15593 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15594 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15595 			  GFP_KERNEL);
15596 	if (!pf->vsi) {
15597 		err = -ENOMEM;
15598 		goto err_switch_setup;
15599 	}
15600 
15601 	/* We allocate one VSI which is needed as absolute minimum
15602 	 * in order to register the netdev
15603 	 */
15604 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15605 	if (v_idx < 0) {
15606 		err = v_idx;
15607 		goto err_switch_setup;
15608 	}
15609 	pf->lan_vsi = v_idx;
15610 	vsi = pf->vsi[v_idx];
15611 	if (!vsi) {
15612 		err = -EFAULT;
15613 		goto err_switch_setup;
15614 	}
15615 	vsi->alloc_queue_pairs = 1;
15616 	err = i40e_config_netdev(vsi);
15617 	if (err)
15618 		goto err_switch_setup;
15619 	err = register_netdev(vsi->netdev);
15620 	if (err)
15621 		goto err_switch_setup;
15622 	vsi->netdev_registered = true;
15623 	i40e_dbg_pf_init(pf);
15624 
15625 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15626 	if (err)
15627 		goto err_switch_setup;
15628 
15629 	/* tell the firmware that we're starting */
15630 	i40e_send_version(pf);
15631 
15632 	/* since everything's happy, start the service_task timer */
15633 	mod_timer(&pf->service_timer,
15634 		  round_jiffies(jiffies + pf->service_timer_period));
15635 
15636 	return 0;
15637 
15638 err_switch_setup:
15639 	i40e_reset_interrupt_capability(pf);
15640 	timer_shutdown_sync(&pf->service_timer);
15641 	i40e_shutdown_adminq(hw);
15642 	iounmap(hw->hw_addr);
15643 	pci_release_mem_regions(pf->pdev);
15644 	pci_disable_device(pf->pdev);
15645 	i40e_free_pf(pf);
15646 
15647 	return err;
15648 }
15649 
15650 /**
15651  * i40e_set_subsystem_device_id - set subsystem device id
15652  * @hw: pointer to the hardware info
15653  *
15654  * Set PCI subsystem device id either from a pci_dev structure or
15655  * a specific FW register.
15656  **/
15657 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15658 {
15659 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15660 
15661 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15662 		pf->pdev->subsystem_device :
15663 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15664 }
15665 
15666 /**
15667  * i40e_probe - Device initialization routine
15668  * @pdev: PCI device information struct
15669  * @ent: entry in i40e_pci_tbl
15670  *
15671  * i40e_probe initializes a PF identified by a pci_dev structure.
15672  * The OS initialization, configuring of the PF private structure,
15673  * and a hardware reset occur.
15674  *
15675  * Returns 0 on success, negative on failure
15676  **/
15677 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15678 {
15679 	struct i40e_aq_get_phy_abilities_resp abilities;
15680 #ifdef CONFIG_I40E_DCB
15681 	enum i40e_get_fw_lldp_status_resp lldp_status;
15682 #endif /* CONFIG_I40E_DCB */
15683 	struct i40e_pf *pf;
15684 	struct i40e_hw *hw;
15685 	static u16 pfs_found;
15686 	u16 wol_nvm_bits;
15687 	char nvm_ver[32];
15688 	u16 link_status;
15689 #ifdef CONFIG_I40E_DCB
15690 	int status;
15691 #endif /* CONFIG_I40E_DCB */
15692 	int err;
15693 	u32 val;
15694 	u32 i;
15695 
15696 	err = pci_enable_device_mem(pdev);
15697 	if (err)
15698 		return err;
15699 
15700 	/* set up for high or low dma */
15701 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15702 	if (err) {
15703 		dev_err(&pdev->dev,
15704 			"DMA configuration failed: 0x%x\n", err);
15705 		goto err_dma;
15706 	}
15707 
15708 	/* set up pci connections */
15709 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15710 	if (err) {
15711 		dev_info(&pdev->dev,
15712 			 "pci_request_selected_regions failed %d\n", err);
15713 		goto err_pci_reg;
15714 	}
15715 
15716 	pci_set_master(pdev);
15717 
15718 	/* Now that we have a PCI connection, we need to do the
15719 	 * low level device setup.  This is primarily setting up
15720 	 * the Admin Queue structures and then querying for the
15721 	 * device's current profile information.
15722 	 */
15723 	pf = i40e_alloc_pf(&pdev->dev);
15724 	if (!pf) {
15725 		err = -ENOMEM;
15726 		goto err_pf_alloc;
15727 	}
15728 	pf->next_vsi = 0;
15729 	pf->pdev = pdev;
15730 	set_bit(__I40E_DOWN, pf->state);
15731 
15732 	hw = &pf->hw;
15733 
15734 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15735 				I40E_MAX_CSR_SPACE);
15736 	/* We believe that the highest register to read is
15737 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15738 	 * is not less than that before mapping to prevent a
15739 	 * kernel panic.
15740 	 */
15741 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15742 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15743 			pf->ioremap_len);
15744 		err = -ENOMEM;
15745 		goto err_ioremap;
15746 	}
15747 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15748 	if (!hw->hw_addr) {
15749 		err = -EIO;
15750 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15751 			 (unsigned int)pci_resource_start(pdev, 0),
15752 			 pf->ioremap_len, err);
15753 		goto err_ioremap;
15754 	}
15755 	hw->vendor_id = pdev->vendor;
15756 	hw->device_id = pdev->device;
15757 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15758 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15759 	i40e_set_subsystem_device_id(hw);
15760 	hw->bus.device = PCI_SLOT(pdev->devfn);
15761 	hw->bus.func = PCI_FUNC(pdev->devfn);
15762 	hw->bus.bus_id = pdev->bus->number;
15763 	pf->instance = pfs_found;
15764 
15765 	/* Select something other than the 802.1ad ethertype for the
15766 	 * switch to use internally and drop on ingress.
15767 	 */
15768 	hw->switch_tag = 0xffff;
15769 	hw->first_tag = ETH_P_8021AD;
15770 	hw->second_tag = ETH_P_8021Q;
15771 
15772 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15773 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15774 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15775 
15776 	/* set up the locks for the AQ, do this only once in probe
15777 	 * and destroy them only once in remove
15778 	 */
15779 	mutex_init(&hw->aq.asq_mutex);
15780 	mutex_init(&hw->aq.arq_mutex);
15781 
15782 	pf->msg_enable = netif_msg_init(debug,
15783 					NETIF_MSG_DRV |
15784 					NETIF_MSG_PROBE |
15785 					NETIF_MSG_LINK);
15786 	if (debug < -1)
15787 		pf->hw.debug_mask = debug;
15788 
15789 	/* do a special CORER for clearing PXE mode once at init */
15790 	if (hw->revision_id == 0 &&
15791 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15792 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15793 		i40e_flush(hw);
15794 		msleep(200);
15795 		pf->corer_count++;
15796 
15797 		i40e_clear_pxe_mode(hw);
15798 	}
15799 
15800 	/* Reset here to make sure all is clean and to define PF 'n' */
15801 	i40e_clear_hw(hw);
15802 
15803 	err = i40e_set_mac_type(hw);
15804 	if (err) {
15805 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15806 			 err);
15807 		goto err_pf_reset;
15808 	}
15809 
15810 	err = i40e_handle_resets(pf);
15811 	if (err)
15812 		goto err_pf_reset;
15813 
15814 	i40e_check_recovery_mode(pf);
15815 
15816 	if (is_kdump_kernel()) {
15817 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15818 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15819 	} else {
15820 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15821 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15822 	}
15823 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15824 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15825 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15826 
15827 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15828 		 "%s-%s:misc",
15829 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15830 
15831 	err = i40e_init_shared_code(hw);
15832 	if (err) {
15833 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15834 			 err);
15835 		goto err_pf_reset;
15836 	}
15837 
15838 	/* set up a default setting for link flow control */
15839 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15840 
15841 	err = i40e_init_adminq(hw);
15842 	if (err) {
15843 		if (err == -EIO)
15844 			dev_info(&pdev->dev,
15845 				 "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",
15846 				 hw->aq.api_maj_ver,
15847 				 hw->aq.api_min_ver,
15848 				 I40E_FW_API_VERSION_MAJOR,
15849 				 I40E_FW_MINOR_VERSION(hw));
15850 		else
15851 			dev_info(&pdev->dev,
15852 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15853 
15854 		goto err_pf_reset;
15855 	}
15856 	i40e_get_oem_version(hw);
15857 	i40e_get_pba_string(hw);
15858 
15859 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15860 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15861 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15862 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15863 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15864 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15865 		 hw->subsystem_device_id);
15866 
15867 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15868 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15869 		dev_dbg(&pdev->dev,
15870 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15871 			 hw->aq.api_maj_ver,
15872 			 hw->aq.api_min_ver,
15873 			 I40E_FW_API_VERSION_MAJOR,
15874 			 I40E_FW_MINOR_VERSION(hw));
15875 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15876 		dev_info(&pdev->dev,
15877 			 "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",
15878 			 hw->aq.api_maj_ver,
15879 			 hw->aq.api_min_ver,
15880 			 I40E_FW_API_VERSION_MAJOR,
15881 			 I40E_FW_MINOR_VERSION(hw));
15882 
15883 	i40e_verify_eeprom(pf);
15884 
15885 	/* Rev 0 hardware was never productized */
15886 	if (hw->revision_id < 1)
15887 		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");
15888 
15889 	i40e_clear_pxe_mode(hw);
15890 
15891 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15892 	if (err)
15893 		goto err_adminq_setup;
15894 
15895 	err = i40e_sw_init(pf);
15896 	if (err) {
15897 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15898 		goto err_sw_init;
15899 	}
15900 
15901 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15902 		return i40e_init_recovery_mode(pf, hw);
15903 
15904 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15905 				hw->func_caps.num_rx_qp, 0, 0);
15906 	if (err) {
15907 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15908 		goto err_init_lan_hmc;
15909 	}
15910 
15911 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15912 	if (err) {
15913 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15914 		err = -ENOENT;
15915 		goto err_configure_lan_hmc;
15916 	}
15917 
15918 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15919 	 * Ignore error return codes because if it was already disabled via
15920 	 * hardware settings this will fail
15921 	 */
15922 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15923 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15924 		i40e_aq_stop_lldp(hw, true, false, NULL);
15925 	}
15926 
15927 	/* allow a platform config to override the HW addr */
15928 	i40e_get_platform_mac_addr(pdev, pf);
15929 
15930 	if (!is_valid_ether_addr(hw->mac.addr)) {
15931 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15932 		err = -EIO;
15933 		goto err_mac_addr;
15934 	}
15935 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15936 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15937 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15938 	if (is_valid_ether_addr(hw->mac.port_addr))
15939 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15940 
15941 	i40e_ptp_alloc_pins(pf);
15942 	pci_set_drvdata(pdev, pf);
15943 	pci_save_state(pdev);
15944 
15945 #ifdef CONFIG_I40E_DCB
15946 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15947 	(!status &&
15948 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15949 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15950 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15951 	dev_info(&pdev->dev,
15952 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15953 			"FW LLDP is disabled\n" :
15954 			"FW LLDP is enabled\n");
15955 
15956 	/* Enable FW to write default DCB config on link-up */
15957 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15958 
15959 	err = i40e_init_pf_dcb(pf);
15960 	if (err) {
15961 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15962 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15963 		/* Continue without DCB enabled */
15964 	}
15965 #endif /* CONFIG_I40E_DCB */
15966 
15967 	/* set up periodic task facility */
15968 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15969 	pf->service_timer_period = HZ;
15970 
15971 	INIT_WORK(&pf->service_task, i40e_service_task);
15972 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15973 
15974 	/* NVM bit on means WoL disabled for the port */
15975 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15976 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15977 		pf->wol_en = false;
15978 	else
15979 		pf->wol_en = true;
15980 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15981 
15982 	/* set up the main switch operations */
15983 	i40e_determine_queue_usage(pf);
15984 	err = i40e_init_interrupt_scheme(pf);
15985 	if (err)
15986 		goto err_switch_setup;
15987 
15988 	/* Reduce Tx and Rx pairs for kdump
15989 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15990 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15991 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15992 	 */
15993 	if (is_kdump_kernel())
15994 		pf->num_lan_msix = 1;
15995 
15996 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15997 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15998 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15999 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
16000 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
16001 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
16002 						    UDP_TUNNEL_TYPE_GENEVE;
16003 
16004 	/* The number of VSIs reported by the FW is the minimum guaranteed
16005 	 * to us; HW supports far more and we share the remaining pool with
16006 	 * the other PFs. We allocate space for more than the guarantee with
16007 	 * the understanding that we might not get them all later.
16008 	 */
16009 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
16010 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
16011 	else
16012 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
16013 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
16014 		dev_warn(&pf->pdev->dev,
16015 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
16016 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
16017 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
16018 	}
16019 
16020 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
16021 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
16022 			  GFP_KERNEL);
16023 	if (!pf->vsi) {
16024 		err = -ENOMEM;
16025 		goto err_switch_setup;
16026 	}
16027 
16028 #ifdef CONFIG_PCI_IOV
16029 	/* prep for VF support */
16030 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16031 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16032 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16033 		if (pci_num_vf(pdev))
16034 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
16035 	}
16036 #endif
16037 	err = i40e_setup_pf_switch(pf, false, false);
16038 	if (err) {
16039 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
16040 		goto err_vsis;
16041 	}
16042 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
16043 
16044 	/* if FDIR VSI was set up, start it now */
16045 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16046 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
16047 			i40e_vsi_open(pf->vsi[i]);
16048 			break;
16049 		}
16050 	}
16051 
16052 	/* The driver only wants link up/down and module qualification
16053 	 * reports from firmware.  Note the negative logic.
16054 	 */
16055 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16056 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16057 					 I40E_AQ_EVENT_MEDIA_NA |
16058 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16059 	if (err)
16060 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16061 			 ERR_PTR(err),
16062 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16063 
16064 	/* Reconfigure hardware for allowing smaller MSS in the case
16065 	 * of TSO, so that we avoid the MDD being fired and causing
16066 	 * a reset in the case of small MSS+TSO.
16067 	 */
16068 	val = rd32(hw, I40E_REG_MSS);
16069 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16070 		val &= ~I40E_REG_MSS_MIN_MASK;
16071 		val |= I40E_64BYTE_MSS;
16072 		wr32(hw, I40E_REG_MSS, val);
16073 	}
16074 
16075 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
16076 		msleep(75);
16077 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16078 		if (err)
16079 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16080 				 ERR_PTR(err),
16081 				 i40e_aq_str(&pf->hw,
16082 					     pf->hw.aq.asq_last_status));
16083 	}
16084 	/* The main driver is (mostly) up and happy. We need to set this state
16085 	 * before setting up the misc vector or we get a race and the vector
16086 	 * ends up disabled forever.
16087 	 */
16088 	clear_bit(__I40E_DOWN, pf->state);
16089 
16090 	/* In case of MSIX we are going to setup the misc vector right here
16091 	 * to handle admin queue events etc. In case of legacy and MSI
16092 	 * the misc functionality and queue processing is combined in
16093 	 * the same vector and that gets setup at open.
16094 	 */
16095 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16096 		err = i40e_setup_misc_vector(pf);
16097 		if (err) {
16098 			dev_info(&pdev->dev,
16099 				 "setup of misc vector failed: %d\n", err);
16100 			i40e_cloud_filter_exit(pf);
16101 			i40e_fdir_teardown(pf);
16102 			goto err_vsis;
16103 		}
16104 	}
16105 
16106 #ifdef CONFIG_PCI_IOV
16107 	/* prep for VF support */
16108 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16109 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16110 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16111 		/* disable link interrupts for VFs */
16112 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16113 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16114 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16115 		i40e_flush(hw);
16116 
16117 		if (pci_num_vf(pdev)) {
16118 			dev_info(&pdev->dev,
16119 				 "Active VFs found, allocating resources.\n");
16120 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16121 			if (err)
16122 				dev_info(&pdev->dev,
16123 					 "Error %d allocating resources for existing VFs\n",
16124 					 err);
16125 		}
16126 	}
16127 #endif /* CONFIG_PCI_IOV */
16128 
16129 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16130 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16131 						      pf->num_iwarp_msix,
16132 						      I40E_IWARP_IRQ_PILE_ID);
16133 		if (pf->iwarp_base_vector < 0) {
16134 			dev_info(&pdev->dev,
16135 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16136 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16137 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16138 		}
16139 	}
16140 
16141 	i40e_dbg_pf_init(pf);
16142 
16143 	/* tell the firmware that we're starting */
16144 	i40e_send_version(pf);
16145 
16146 	/* since everything's happy, start the service_task timer */
16147 	mod_timer(&pf->service_timer,
16148 		  round_jiffies(jiffies + pf->service_timer_period));
16149 
16150 	/* add this PF to client device list and launch a client service task */
16151 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16152 		err = i40e_lan_add_device(pf);
16153 		if (err)
16154 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16155 				 err);
16156 	}
16157 
16158 #define PCI_SPEED_SIZE 8
16159 #define PCI_WIDTH_SIZE 8
16160 	/* Devices on the IOSF bus do not have this information
16161 	 * and will report PCI Gen 1 x 1 by default so don't bother
16162 	 * checking them.
16163 	 */
16164 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16165 		char speed[PCI_SPEED_SIZE] = "Unknown";
16166 		char width[PCI_WIDTH_SIZE] = "Unknown";
16167 
16168 		/* Get the negotiated link width and speed from PCI config
16169 		 * space
16170 		 */
16171 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16172 					  &link_status);
16173 
16174 		i40e_set_pci_config_data(hw, link_status);
16175 
16176 		switch (hw->bus.speed) {
16177 		case i40e_bus_speed_8000:
16178 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16179 		case i40e_bus_speed_5000:
16180 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16181 		case i40e_bus_speed_2500:
16182 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16183 		default:
16184 			break;
16185 		}
16186 		switch (hw->bus.width) {
16187 		case i40e_bus_width_pcie_x8:
16188 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16189 		case i40e_bus_width_pcie_x4:
16190 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16191 		case i40e_bus_width_pcie_x2:
16192 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16193 		case i40e_bus_width_pcie_x1:
16194 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16195 		default:
16196 			break;
16197 		}
16198 
16199 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16200 			 speed, width);
16201 
16202 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16203 		    hw->bus.speed < i40e_bus_speed_8000) {
16204 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16205 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16206 		}
16207 	}
16208 
16209 	/* get the requested speeds from the fw */
16210 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16211 	if (err)
16212 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16213 			ERR_PTR(err),
16214 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16215 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16216 
16217 	/* set the FEC config due to the board capabilities */
16218 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16219 
16220 	/* get the supported phy types from the fw */
16221 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16222 	if (err)
16223 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16224 			ERR_PTR(err),
16225 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16226 
16227 	/* make sure the MFS hasn't been set lower than the default */
16228 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16229 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16230 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16231 	if (val < MAX_FRAME_SIZE_DEFAULT)
16232 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16233 			 pf->hw.port, val);
16234 
16235 	/* Add a filter to drop all Flow control frames from any VSI from being
16236 	 * transmitted. By doing so we stop a malicious VF from sending out
16237 	 * PAUSE or PFC frames and potentially controlling traffic for other
16238 	 * PF/VF VSIs.
16239 	 * The FW can still send Flow control frames if enabled.
16240 	 */
16241 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16242 						       pf->main_vsi_seid);
16243 
16244 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16245 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16246 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16247 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16248 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16249 	/* print a string summarizing features */
16250 	i40e_print_features(pf);
16251 
16252 	i40e_devlink_register(pf);
16253 
16254 	return 0;
16255 
16256 	/* Unwind what we've done if something failed in the setup */
16257 err_vsis:
16258 	set_bit(__I40E_DOWN, pf->state);
16259 	i40e_clear_interrupt_scheme(pf);
16260 	kfree(pf->vsi);
16261 err_switch_setup:
16262 	i40e_reset_interrupt_capability(pf);
16263 	timer_shutdown_sync(&pf->service_timer);
16264 err_mac_addr:
16265 err_configure_lan_hmc:
16266 	(void)i40e_shutdown_lan_hmc(hw);
16267 err_init_lan_hmc:
16268 	kfree(pf->qp_pile);
16269 err_sw_init:
16270 err_adminq_setup:
16271 err_pf_reset:
16272 	iounmap(hw->hw_addr);
16273 err_ioremap:
16274 	i40e_free_pf(pf);
16275 err_pf_alloc:
16276 	pci_release_mem_regions(pdev);
16277 err_pci_reg:
16278 err_dma:
16279 	pci_disable_device(pdev);
16280 	return err;
16281 }
16282 
16283 /**
16284  * i40e_remove - Device removal routine
16285  * @pdev: PCI device information struct
16286  *
16287  * i40e_remove is called by the PCI subsystem to alert the driver
16288  * that is should release a PCI device.  This could be caused by a
16289  * Hot-Plug event, or because the driver is going to be removed from
16290  * memory.
16291  **/
16292 static void i40e_remove(struct pci_dev *pdev)
16293 {
16294 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16295 	struct i40e_hw *hw = &pf->hw;
16296 	int ret_code;
16297 	int i;
16298 
16299 	i40e_devlink_unregister(pf);
16300 
16301 	i40e_dbg_pf_exit(pf);
16302 
16303 	i40e_ptp_stop(pf);
16304 
16305 	/* Disable RSS in hw */
16306 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16307 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16308 
16309 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16310 	 * flags, once they are set, i40e_rebuild should not be called as
16311 	 * i40e_prep_for_reset always returns early.
16312 	 */
16313 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16314 		usleep_range(1000, 2000);
16315 	set_bit(__I40E_IN_REMOVE, pf->state);
16316 
16317 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16318 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16319 		i40e_free_vfs(pf);
16320 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16321 	}
16322 	/* no more scheduling of any task */
16323 	set_bit(__I40E_SUSPENDED, pf->state);
16324 	set_bit(__I40E_DOWN, pf->state);
16325 	if (pf->service_timer.function)
16326 		timer_shutdown_sync(&pf->service_timer);
16327 	if (pf->service_task.func)
16328 		cancel_work_sync(&pf->service_task);
16329 
16330 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16331 		struct i40e_vsi *vsi = pf->vsi[0];
16332 
16333 		/* We know that we have allocated only one vsi for this PF,
16334 		 * it was just for registering netdevice, so the interface
16335 		 * could be visible in the 'ifconfig' output
16336 		 */
16337 		unregister_netdev(vsi->netdev);
16338 		free_netdev(vsi->netdev);
16339 
16340 		goto unmap;
16341 	}
16342 
16343 	/* Client close must be called explicitly here because the timer
16344 	 * has been stopped.
16345 	 */
16346 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16347 
16348 	i40e_fdir_teardown(pf);
16349 
16350 	/* If there is a switch structure or any orphans, remove them.
16351 	 * This will leave only the PF's VSI remaining.
16352 	 */
16353 	for (i = 0; i < I40E_MAX_VEB; i++) {
16354 		if (!pf->veb[i])
16355 			continue;
16356 
16357 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16358 		    pf->veb[i]->uplink_seid == 0)
16359 			i40e_switch_branch_release(pf->veb[i]);
16360 	}
16361 
16362 	/* Now we can shutdown the PF's VSIs, just before we kill
16363 	 * adminq and hmc.
16364 	 */
16365 	for (i = pf->num_alloc_vsi; i--;)
16366 		if (pf->vsi[i]) {
16367 			i40e_vsi_close(pf->vsi[i]);
16368 			i40e_vsi_release(pf->vsi[i]);
16369 			pf->vsi[i] = NULL;
16370 		}
16371 
16372 	i40e_cloud_filter_exit(pf);
16373 
16374 	/* remove attached clients */
16375 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16376 		ret_code = i40e_lan_del_device(pf);
16377 		if (ret_code)
16378 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16379 				 ret_code);
16380 	}
16381 
16382 	/* shutdown and destroy the HMC */
16383 	if (hw->hmc.hmc_obj) {
16384 		ret_code = i40e_shutdown_lan_hmc(hw);
16385 		if (ret_code)
16386 			dev_warn(&pdev->dev,
16387 				 "Failed to destroy the HMC resources: %d\n",
16388 				 ret_code);
16389 	}
16390 
16391 unmap:
16392 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16393 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16394 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16395 		free_irq(pf->pdev->irq, pf);
16396 
16397 	/* shutdown the adminq */
16398 	i40e_shutdown_adminq(hw);
16399 
16400 	/* destroy the locks only once, here */
16401 	mutex_destroy(&hw->aq.arq_mutex);
16402 	mutex_destroy(&hw->aq.asq_mutex);
16403 
16404 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16405 	rtnl_lock();
16406 	i40e_clear_interrupt_scheme(pf);
16407 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16408 		if (pf->vsi[i]) {
16409 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16410 				i40e_vsi_clear_rings(pf->vsi[i]);
16411 			i40e_vsi_clear(pf->vsi[i]);
16412 			pf->vsi[i] = NULL;
16413 		}
16414 	}
16415 	rtnl_unlock();
16416 
16417 	for (i = 0; i < I40E_MAX_VEB; i++) {
16418 		kfree(pf->veb[i]);
16419 		pf->veb[i] = NULL;
16420 	}
16421 
16422 	kfree(pf->qp_pile);
16423 	kfree(pf->vsi);
16424 
16425 	iounmap(hw->hw_addr);
16426 	i40e_free_pf(pf);
16427 	pci_release_mem_regions(pdev);
16428 
16429 	pci_disable_device(pdev);
16430 }
16431 
16432 /**
16433  * i40e_pci_error_detected - warning that something funky happened in PCI land
16434  * @pdev: PCI device information struct
16435  * @error: the type of PCI error
16436  *
16437  * Called to warn that something happened and the error handling steps
16438  * are in progress.  Allows the driver to quiesce things, be ready for
16439  * remediation.
16440  **/
16441 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16442 						pci_channel_state_t error)
16443 {
16444 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16445 
16446 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16447 
16448 	if (!pf) {
16449 		dev_info(&pdev->dev,
16450 			 "Cannot recover - error happened during device probe\n");
16451 		return PCI_ERS_RESULT_DISCONNECT;
16452 	}
16453 
16454 	/* shutdown all operations */
16455 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16456 		i40e_prep_for_reset(pf);
16457 
16458 	/* Request a slot reset */
16459 	return PCI_ERS_RESULT_NEED_RESET;
16460 }
16461 
16462 /**
16463  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16464  * @pdev: PCI device information struct
16465  *
16466  * Called to find if the driver can work with the device now that
16467  * the pci slot has been reset.  If a basic connection seems good
16468  * (registers are readable and have sane content) then return a
16469  * happy little PCI_ERS_RESULT_xxx.
16470  **/
16471 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16472 {
16473 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16474 	pci_ers_result_t result;
16475 	u32 reg;
16476 
16477 	dev_dbg(&pdev->dev, "%s\n", __func__);
16478 	if (pci_enable_device_mem(pdev)) {
16479 		dev_info(&pdev->dev,
16480 			 "Cannot re-enable PCI device after reset.\n");
16481 		result = PCI_ERS_RESULT_DISCONNECT;
16482 	} else {
16483 		pci_set_master(pdev);
16484 		pci_restore_state(pdev);
16485 		pci_save_state(pdev);
16486 		pci_wake_from_d3(pdev, false);
16487 
16488 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16489 		if (reg == 0)
16490 			result = PCI_ERS_RESULT_RECOVERED;
16491 		else
16492 			result = PCI_ERS_RESULT_DISCONNECT;
16493 	}
16494 
16495 	return result;
16496 }
16497 
16498 /**
16499  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16500  * @pdev: PCI device information struct
16501  */
16502 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16503 {
16504 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16505 
16506 	i40e_prep_for_reset(pf);
16507 }
16508 
16509 /**
16510  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16511  * @pdev: PCI device information struct
16512  */
16513 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16514 {
16515 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16516 
16517 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16518 		return;
16519 
16520 	i40e_reset_and_rebuild(pf, false, false);
16521 #ifdef CONFIG_PCI_IOV
16522 	i40e_restore_all_vfs_msi_state(pdev);
16523 #endif /* CONFIG_PCI_IOV */
16524 }
16525 
16526 /**
16527  * i40e_pci_error_resume - restart operations after PCI error recovery
16528  * @pdev: PCI device information struct
16529  *
16530  * Called to allow the driver to bring things back up after PCI error
16531  * and/or reset recovery has finished.
16532  **/
16533 static void i40e_pci_error_resume(struct pci_dev *pdev)
16534 {
16535 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16536 
16537 	dev_dbg(&pdev->dev, "%s\n", __func__);
16538 	if (test_bit(__I40E_SUSPENDED, pf->state))
16539 		return;
16540 
16541 	i40e_handle_reset_warning(pf, false);
16542 }
16543 
16544 /**
16545  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16546  * using the mac_address_write admin q function
16547  * @pf: pointer to i40e_pf struct
16548  **/
16549 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16550 {
16551 	struct i40e_hw *hw = &pf->hw;
16552 	u8 mac_addr[6];
16553 	u16 flags = 0;
16554 	int ret;
16555 
16556 	/* Get current MAC address in case it's an LAA */
16557 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16558 		ether_addr_copy(mac_addr,
16559 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16560 	} else {
16561 		dev_err(&pf->pdev->dev,
16562 			"Failed to retrieve MAC address; using default\n");
16563 		ether_addr_copy(mac_addr, hw->mac.addr);
16564 	}
16565 
16566 	/* The FW expects the mac address write cmd to first be called with
16567 	 * one of these flags before calling it again with the multicast
16568 	 * enable flags.
16569 	 */
16570 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16571 
16572 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16573 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16574 
16575 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16576 	if (ret) {
16577 		dev_err(&pf->pdev->dev,
16578 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16579 		return;
16580 	}
16581 
16582 	flags = I40E_AQC_MC_MAG_EN
16583 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16584 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16585 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16586 	if (ret)
16587 		dev_err(&pf->pdev->dev,
16588 			"Failed to enable Multicast Magic Packet wake up\n");
16589 }
16590 
16591 /**
16592  * i40e_shutdown - PCI callback for shutting down
16593  * @pdev: PCI device information struct
16594  **/
16595 static void i40e_shutdown(struct pci_dev *pdev)
16596 {
16597 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16598 	struct i40e_hw *hw = &pf->hw;
16599 
16600 	set_bit(__I40E_SUSPENDED, pf->state);
16601 	set_bit(__I40E_DOWN, pf->state);
16602 
16603 	del_timer_sync(&pf->service_timer);
16604 	cancel_work_sync(&pf->service_task);
16605 	i40e_cloud_filter_exit(pf);
16606 	i40e_fdir_teardown(pf);
16607 
16608 	/* Client close must be called explicitly here because the timer
16609 	 * has been stopped.
16610 	 */
16611 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16612 
16613 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16614 		i40e_enable_mc_magic_wake(pf);
16615 
16616 	i40e_prep_for_reset(pf);
16617 
16618 	wr32(hw, I40E_PFPM_APM,
16619 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16620 	wr32(hw, I40E_PFPM_WUFC,
16621 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16622 
16623 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16624 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16625 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16626 		free_irq(pf->pdev->irq, pf);
16627 
16628 	/* Since we're going to destroy queues during the
16629 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16630 	 * whole section
16631 	 */
16632 	rtnl_lock();
16633 	i40e_clear_interrupt_scheme(pf);
16634 	rtnl_unlock();
16635 
16636 	if (system_state == SYSTEM_POWER_OFF) {
16637 		pci_wake_from_d3(pdev, pf->wol_en);
16638 		pci_set_power_state(pdev, PCI_D3hot);
16639 	}
16640 }
16641 
16642 /**
16643  * i40e_suspend - PM callback for moving to D3
16644  * @dev: generic device information structure
16645  **/
16646 static int __maybe_unused i40e_suspend(struct device *dev)
16647 {
16648 	struct i40e_pf *pf = dev_get_drvdata(dev);
16649 	struct i40e_hw *hw = &pf->hw;
16650 
16651 	/* If we're already suspended, then there is nothing to do */
16652 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16653 		return 0;
16654 
16655 	set_bit(__I40E_DOWN, pf->state);
16656 
16657 	/* Ensure service task will not be running */
16658 	del_timer_sync(&pf->service_timer);
16659 	cancel_work_sync(&pf->service_task);
16660 
16661 	/* Client close must be called explicitly here because the timer
16662 	 * has been stopped.
16663 	 */
16664 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16665 
16666 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16667 		i40e_enable_mc_magic_wake(pf);
16668 
16669 	/* Since we're going to destroy queues during the
16670 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16671 	 * whole section
16672 	 */
16673 	rtnl_lock();
16674 
16675 	i40e_prep_for_reset(pf);
16676 
16677 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16678 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16679 
16680 	/* Clear the interrupt scheme and release our IRQs so that the system
16681 	 * can safely hibernate even when there are a large number of CPUs.
16682 	 * Otherwise hibernation might fail when mapping all the vectors back
16683 	 * to CPU0.
16684 	 */
16685 	i40e_clear_interrupt_scheme(pf);
16686 
16687 	rtnl_unlock();
16688 
16689 	return 0;
16690 }
16691 
16692 /**
16693  * i40e_resume - PM callback for waking up from D3
16694  * @dev: generic device information structure
16695  **/
16696 static int __maybe_unused i40e_resume(struct device *dev)
16697 {
16698 	struct i40e_pf *pf = dev_get_drvdata(dev);
16699 	int err;
16700 
16701 	/* If we're not suspended, then there is nothing to do */
16702 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16703 		return 0;
16704 
16705 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16706 	 * since we're going to be restoring queues
16707 	 */
16708 	rtnl_lock();
16709 
16710 	/* We cleared the interrupt scheme when we suspended, so we need to
16711 	 * restore it now to resume device functionality.
16712 	 */
16713 	err = i40e_restore_interrupt_scheme(pf);
16714 	if (err) {
16715 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16716 			err);
16717 	}
16718 
16719 	clear_bit(__I40E_DOWN, pf->state);
16720 	i40e_reset_and_rebuild(pf, false, true);
16721 
16722 	rtnl_unlock();
16723 
16724 	/* Clear suspended state last after everything is recovered */
16725 	clear_bit(__I40E_SUSPENDED, pf->state);
16726 
16727 	/* Restart the service task */
16728 	mod_timer(&pf->service_timer,
16729 		  round_jiffies(jiffies + pf->service_timer_period));
16730 
16731 	return 0;
16732 }
16733 
16734 static const struct pci_error_handlers i40e_err_handler = {
16735 	.error_detected = i40e_pci_error_detected,
16736 	.slot_reset = i40e_pci_error_slot_reset,
16737 	.reset_prepare = i40e_pci_error_reset_prepare,
16738 	.reset_done = i40e_pci_error_reset_done,
16739 	.resume = i40e_pci_error_resume,
16740 };
16741 
16742 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16743 
16744 static struct pci_driver i40e_driver = {
16745 	.name     = i40e_driver_name,
16746 	.id_table = i40e_pci_tbl,
16747 	.probe    = i40e_probe,
16748 	.remove   = i40e_remove,
16749 	.driver   = {
16750 		.pm = &i40e_pm_ops,
16751 	},
16752 	.shutdown = i40e_shutdown,
16753 	.err_handler = &i40e_err_handler,
16754 	.sriov_configure = i40e_pci_sriov_configure,
16755 };
16756 
16757 /**
16758  * i40e_init_module - Driver registration routine
16759  *
16760  * i40e_init_module is the first routine called when the driver is
16761  * loaded. All it does is register with the PCI subsystem.
16762  **/
16763 static int __init i40e_init_module(void)
16764 {
16765 	int err;
16766 
16767 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16768 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16769 
16770 	/* There is no need to throttle the number of active tasks because
16771 	 * each device limits its own task using a state bit for scheduling
16772 	 * the service task, and the device tasks do not interfere with each
16773 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16774 	 * since we need to be able to guarantee forward progress even under
16775 	 * memory pressure.
16776 	 */
16777 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16778 	if (!i40e_wq) {
16779 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16780 		return -ENOMEM;
16781 	}
16782 
16783 	i40e_dbg_init();
16784 	err = pci_register_driver(&i40e_driver);
16785 	if (err) {
16786 		destroy_workqueue(i40e_wq);
16787 		i40e_dbg_exit();
16788 		return err;
16789 	}
16790 
16791 	return 0;
16792 }
16793 module_init(i40e_init_module);
16794 
16795 /**
16796  * i40e_exit_module - Driver exit cleanup routine
16797  *
16798  * i40e_exit_module is called just before the driver is removed
16799  * from memory.
16800  **/
16801 static void __exit i40e_exit_module(void)
16802 {
16803 	pci_unregister_driver(&i40e_driver);
16804 	destroy_workqueue(i40e_wq);
16805 	ida_destroy(&i40e_client_ida);
16806 	i40e_dbg_exit();
16807 }
16808 module_exit(i40e_exit_module);
16809