xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 8f3f06dfd6873135068ccf1a0b386308e8c4da38)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
88 	/* required last entry */
89 	{0, }
90 };
91 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
92 
93 #define I40E_MAX_VF_COUNT 128
94 static int debug = -1;
95 module_param(debug, uint, 0);
96 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
97 
98 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
99 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
100 MODULE_LICENSE("GPL v2");
101 
102 static struct workqueue_struct *i40e_wq;
103 
104 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
105 				  struct net_device *netdev, int delta)
106 {
107 	struct netdev_hw_addr *ha;
108 
109 	if (!f || !netdev)
110 		return;
111 
112 	netdev_for_each_mc_addr(ha, netdev) {
113 		if (ether_addr_equal(ha->addr, f->macaddr)) {
114 			ha->refcount += delta;
115 			if (ha->refcount <= 0)
116 				ha->refcount = 1;
117 			break;
118 		}
119 	}
120 }
121 
122 /**
123  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
124  * @hw:   pointer to the HW structure
125  * @mem:  ptr to mem struct to fill out
126  * @size: size of memory requested
127  * @alignment: what to align the allocation to
128  **/
129 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
130 			    u64 size, u32 alignment)
131 {
132 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
133 
134 	mem->size = ALIGN(size, alignment);
135 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
136 				     GFP_KERNEL);
137 	if (!mem->va)
138 		return -ENOMEM;
139 
140 	return 0;
141 }
142 
143 /**
144  * i40e_free_dma_mem_d - OS specific memory free for shared code
145  * @hw:   pointer to the HW structure
146  * @mem:  ptr to mem struct to free
147  **/
148 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
149 {
150 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
151 
152 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
153 	mem->va = NULL;
154 	mem->pa = 0;
155 	mem->size = 0;
156 
157 	return 0;
158 }
159 
160 /**
161  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
162  * @hw:   pointer to the HW structure
163  * @mem:  ptr to mem struct to fill out
164  * @size: size of memory requested
165  **/
166 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
167 			     u32 size)
168 {
169 	mem->size = size;
170 	mem->va = kzalloc(size, GFP_KERNEL);
171 
172 	if (!mem->va)
173 		return -ENOMEM;
174 
175 	return 0;
176 }
177 
178 /**
179  * i40e_free_virt_mem_d - OS specific memory free for shared code
180  * @hw:   pointer to the HW structure
181  * @mem:  ptr to mem struct to free
182  **/
183 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
184 {
185 	/* it's ok to kfree a NULL pointer */
186 	kfree(mem->va);
187 	mem->va = NULL;
188 	mem->size = 0;
189 
190 	return 0;
191 }
192 
193 /**
194  * i40e_get_lump - find a lump of free generic resource
195  * @pf: board private structure
196  * @pile: the pile of resource to search
197  * @needed: the number of items needed
198  * @id: an owner id to stick on the items assigned
199  *
200  * Returns the base item index of the lump, or negative for error
201  **/
202 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
203 			 u16 needed, u16 id)
204 {
205 	int ret = -ENOMEM;
206 	int i, j;
207 
208 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
209 		dev_info(&pf->pdev->dev,
210 			 "param err: pile=%s needed=%d id=0x%04x\n",
211 			 pile ? "<valid>" : "<null>", needed, id);
212 		return -EINVAL;
213 	}
214 
215 	/* Allocate last queue in the pile for FDIR VSI queue
216 	 * so it doesn't fragment the qp_pile
217 	 */
218 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
219 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
220 			dev_err(&pf->pdev->dev,
221 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
222 				pile->num_entries - 1);
223 			return -ENOMEM;
224 		}
225 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
226 		return pile->num_entries - 1;
227 	}
228 
229 	i = 0;
230 	while (i < pile->num_entries) {
231 		/* skip already allocated entries */
232 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
233 			i++;
234 			continue;
235 		}
236 
237 		/* do we have enough in this lump? */
238 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
239 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
240 				break;
241 		}
242 
243 		if (j == needed) {
244 			/* there was enough, so assign it to the requestor */
245 			for (j = 0; j < needed; j++)
246 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
247 			ret = i;
248 			break;
249 		}
250 
251 		/* not enough, so skip over it and continue looking */
252 		i += j;
253 	}
254 
255 	return ret;
256 }
257 
258 /**
259  * i40e_put_lump - return a lump of generic resource
260  * @pile: the pile of resource to search
261  * @index: the base item index
262  * @id: the owner id of the items assigned
263  *
264  * Returns the count of items in the lump
265  **/
266 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
267 {
268 	int valid_id = (id | I40E_PILE_VALID_BIT);
269 	int count = 0;
270 	u16 i;
271 
272 	if (!pile || index >= pile->num_entries)
273 		return -EINVAL;
274 
275 	for (i = index;
276 	     i < pile->num_entries && pile->list[i] == valid_id;
277 	     i++) {
278 		pile->list[i] = 0;
279 		count++;
280 	}
281 
282 
283 	return count;
284 }
285 
286 /**
287  * i40e_find_vsi_from_id - searches for the vsi with the given id
288  * @pf: the pf structure to search for the vsi
289  * @id: id of the vsi it is searching for
290  **/
291 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
292 {
293 	int i;
294 
295 	for (i = 0; i < pf->num_alloc_vsi; i++)
296 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
297 			return pf->vsi[i];
298 
299 	return NULL;
300 }
301 
302 /**
303  * i40e_service_event_schedule - Schedule the service task to wake up
304  * @pf: board private structure
305  *
306  * If not already scheduled, this puts the task into the work queue
307  **/
308 void i40e_service_event_schedule(struct i40e_pf *pf)
309 {
310 	if ((!test_bit(__I40E_DOWN, pf->state) &&
311 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
312 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
313 		queue_work(i40e_wq, &pf->service_task);
314 }
315 
316 /**
317  * i40e_tx_timeout - Respond to a Tx Hang
318  * @netdev: network interface device structure
319  * @txqueue: queue number timing out
320  *
321  * If any port has noticed a Tx timeout, it is likely that the whole
322  * device is munged, not just the one netdev port, so go for the full
323  * reset.
324  **/
325 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
326 {
327 	struct i40e_netdev_priv *np = netdev_priv(netdev);
328 	struct i40e_vsi *vsi = np->vsi;
329 	struct i40e_pf *pf = vsi->back;
330 	struct i40e_ring *tx_ring = NULL;
331 	unsigned int i;
332 	u32 head, val;
333 
334 	pf->tx_timeout_count++;
335 
336 	/* with txqueue index, find the tx_ring struct */
337 	for (i = 0; i < vsi->num_queue_pairs; i++) {
338 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 			if (txqueue ==
340 			    vsi->tx_rings[i]->queue_index) {
341 				tx_ring = vsi->tx_rings[i];
342 				break;
343 			}
344 		}
345 	}
346 
347 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
348 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
349 	else if (time_before(jiffies,
350 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 		return;   /* don't do any new action before the next timeout */
352 
353 	/* don't kick off another recovery if one is already pending */
354 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
355 		return;
356 
357 	if (tx_ring) {
358 		head = i40e_get_head(tx_ring);
359 		/* Read interrupt register */
360 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
361 			val = rd32(&pf->hw,
362 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
363 						tx_ring->vsi->base_vector - 1));
364 		else
365 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
366 
367 		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",
368 			    vsi->seid, txqueue, tx_ring->next_to_clean,
369 			    head, tx_ring->next_to_use,
370 			    readl(tx_ring->tail), val);
371 	}
372 
373 	pf->tx_timeout_last_recovery = jiffies;
374 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
375 		    pf->tx_timeout_recovery_level, txqueue);
376 
377 	switch (pf->tx_timeout_recovery_level) {
378 	case 1:
379 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
380 		break;
381 	case 2:
382 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
383 		break;
384 	case 3:
385 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
386 		break;
387 	default:
388 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
389 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
390 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
391 		break;
392 	}
393 
394 	i40e_service_event_schedule(pf);
395 	pf->tx_timeout_recovery_level++;
396 }
397 
398 /**
399  * i40e_get_vsi_stats_struct - Get System Network Statistics
400  * @vsi: the VSI we care about
401  *
402  * Returns the address of the device statistics structure.
403  * The statistics are actually updated from the service task.
404  **/
405 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
406 {
407 	return &vsi->net_stats;
408 }
409 
410 /**
411  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412  * @ring: Tx ring to get statistics from
413  * @stats: statistics entry to be updated
414  **/
415 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 					    struct rtnl_link_stats64 *stats)
417 {
418 	u64 bytes, packets;
419 	unsigned int start;
420 
421 	do {
422 		start = u64_stats_fetch_begin(&ring->syncp);
423 		packets = ring->stats.packets;
424 		bytes   = ring->stats.bytes;
425 	} while (u64_stats_fetch_retry(&ring->syncp, start));
426 
427 	stats->tx_packets += packets;
428 	stats->tx_bytes   += bytes;
429 }
430 
431 /**
432  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433  * @netdev: network interface device structure
434  * @stats: data structure to store statistics
435  *
436  * Returns the address of the device statistics structure.
437  * The statistics are actually updated from the service task.
438  **/
439 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
440 				  struct rtnl_link_stats64 *stats)
441 {
442 	struct i40e_netdev_priv *np = netdev_priv(netdev);
443 	struct i40e_vsi *vsi = np->vsi;
444 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 	struct i40e_ring *ring;
446 	int i;
447 
448 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
449 		return;
450 
451 	if (!vsi->tx_rings)
452 		return;
453 
454 	rcu_read_lock();
455 	for (i = 0; i < vsi->num_queue_pairs; i++) {
456 		u64 bytes, packets;
457 		unsigned int start;
458 
459 		ring = READ_ONCE(vsi->tx_rings[i]);
460 		if (!ring)
461 			continue;
462 		i40e_get_netdev_stats_struct_tx(ring, stats);
463 
464 		if (i40e_enabled_xdp_vsi(vsi)) {
465 			ring = READ_ONCE(vsi->xdp_rings[i]);
466 			if (!ring)
467 				continue;
468 			i40e_get_netdev_stats_struct_tx(ring, stats);
469 		}
470 
471 		ring = READ_ONCE(vsi->rx_rings[i]);
472 		if (!ring)
473 			continue;
474 		do {
475 			start   = u64_stats_fetch_begin(&ring->syncp);
476 			packets = ring->stats.packets;
477 			bytes   = ring->stats.bytes;
478 		} while (u64_stats_fetch_retry(&ring->syncp, start));
479 
480 		stats->rx_packets += packets;
481 		stats->rx_bytes   += bytes;
482 
483 	}
484 	rcu_read_unlock();
485 
486 	/* following stats updated by i40e_watchdog_subtask() */
487 	stats->multicast	= vsi_stats->multicast;
488 	stats->tx_errors	= vsi_stats->tx_errors;
489 	stats->tx_dropped	= vsi_stats->tx_dropped;
490 	stats->rx_errors	= vsi_stats->rx_errors;
491 	stats->rx_dropped	= vsi_stats->rx_dropped;
492 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
493 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
494 }
495 
496 /**
497  * i40e_vsi_reset_stats - Resets all stats of the given vsi
498  * @vsi: the VSI to have its stats reset
499  **/
500 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
501 {
502 	struct rtnl_link_stats64 *ns;
503 	int i;
504 
505 	if (!vsi)
506 		return;
507 
508 	ns = i40e_get_vsi_stats_struct(vsi);
509 	memset(ns, 0, sizeof(*ns));
510 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
511 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
512 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
513 	if (vsi->rx_rings && vsi->rx_rings[0]) {
514 		for (i = 0; i < vsi->num_queue_pairs; i++) {
515 			memset(&vsi->rx_rings[i]->stats, 0,
516 			       sizeof(vsi->rx_rings[i]->stats));
517 			memset(&vsi->rx_rings[i]->rx_stats, 0,
518 			       sizeof(vsi->rx_rings[i]->rx_stats));
519 			memset(&vsi->tx_rings[i]->stats, 0,
520 			       sizeof(vsi->tx_rings[i]->stats));
521 			memset(&vsi->tx_rings[i]->tx_stats, 0,
522 			       sizeof(vsi->tx_rings[i]->tx_stats));
523 		}
524 	}
525 	vsi->stat_offsets_loaded = false;
526 }
527 
528 /**
529  * i40e_pf_reset_stats - Reset all of the stats for the given PF
530  * @pf: the PF to be reset
531  **/
532 void i40e_pf_reset_stats(struct i40e_pf *pf)
533 {
534 	int i;
535 
536 	memset(&pf->stats, 0, sizeof(pf->stats));
537 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
538 	pf->stat_offsets_loaded = false;
539 
540 	for (i = 0; i < I40E_MAX_VEB; i++) {
541 		if (pf->veb[i]) {
542 			memset(&pf->veb[i]->stats, 0,
543 			       sizeof(pf->veb[i]->stats));
544 			memset(&pf->veb[i]->stats_offsets, 0,
545 			       sizeof(pf->veb[i]->stats_offsets));
546 			memset(&pf->veb[i]->tc_stats, 0,
547 			       sizeof(pf->veb[i]->tc_stats));
548 			memset(&pf->veb[i]->tc_stats_offsets, 0,
549 			       sizeof(pf->veb[i]->tc_stats_offsets));
550 			pf->veb[i]->stat_offsets_loaded = false;
551 		}
552 	}
553 	pf->hw_csum_rx_error = 0;
554 }
555 
556 /**
557  * i40e_compute_pci_to_hw_id - compute index form PCI function.
558  * @vsi: ptr to the VSI to read from.
559  * @hw: ptr to the hardware info.
560  **/
561 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
562 {
563 	int pf_count = i40e_get_pf_count(hw);
564 
565 	if (vsi->type == I40E_VSI_SRIOV)
566 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
567 
568 	return hw->port + BIT(7);
569 }
570 
571 /**
572  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
573  * @hw: ptr to the hardware info.
574  * @hireg: the high 32 bit reg to read.
575  * @loreg: the low 32 bit reg to read.
576  * @offset_loaded: has the initial offset been loaded yet.
577  * @offset: ptr to current offset value.
578  * @stat: ptr to the stat.
579  *
580  * Since the device stats are not reset at PFReset, they will not
581  * be zeroed when the driver starts.  We'll save the first values read
582  * and use them as offsets to be subtracted from the raw values in order
583  * to report stats that count from zero.
584  **/
585 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
586 			       bool offset_loaded, u64 *offset, u64 *stat)
587 {
588 	u64 new_data;
589 
590 	new_data = rd64(hw, loreg);
591 
592 	if (!offset_loaded || new_data < *offset)
593 		*offset = new_data;
594 	*stat = new_data - *offset;
595 }
596 
597 /**
598  * i40e_stat_update48 - read and update a 48 bit stat from the chip
599  * @hw: ptr to the hardware info
600  * @hireg: the high 32 bit reg to read
601  * @loreg: the low 32 bit reg to read
602  * @offset_loaded: has the initial offset been loaded yet
603  * @offset: ptr to current offset value
604  * @stat: ptr to the stat
605  *
606  * Since the device stats are not reset at PFReset, they likely will not
607  * be zeroed when the driver starts.  We'll save the first values read
608  * and use them as offsets to be subtracted from the raw values in order
609  * to report stats that count from zero.  In the process, we also manage
610  * the potential roll-over.
611  **/
612 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
613 			       bool offset_loaded, u64 *offset, u64 *stat)
614 {
615 	u64 new_data;
616 
617 	if (hw->device_id == I40E_DEV_ID_QEMU) {
618 		new_data = rd32(hw, loreg);
619 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
620 	} else {
621 		new_data = rd64(hw, loreg);
622 	}
623 	if (!offset_loaded)
624 		*offset = new_data;
625 	if (likely(new_data >= *offset))
626 		*stat = new_data - *offset;
627 	else
628 		*stat = (new_data + BIT_ULL(48)) - *offset;
629 	*stat &= 0xFFFFFFFFFFFFULL;
630 }
631 
632 /**
633  * i40e_stat_update32 - read and update a 32 bit stat from the chip
634  * @hw: ptr to the hardware info
635  * @reg: the hw reg to read
636  * @offset_loaded: has the initial offset been loaded yet
637  * @offset: ptr to current offset value
638  * @stat: ptr to the stat
639  **/
640 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
641 			       bool offset_loaded, u64 *offset, u64 *stat)
642 {
643 	u32 new_data;
644 
645 	new_data = rd32(hw, reg);
646 	if (!offset_loaded)
647 		*offset = new_data;
648 	if (likely(new_data >= *offset))
649 		*stat = (u32)(new_data - *offset);
650 	else
651 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
652 }
653 
654 /**
655  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
656  * @hw: ptr to the hardware info
657  * @reg: the hw reg to read and clear
658  * @stat: ptr to the stat
659  **/
660 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
661 {
662 	u32 new_data = rd32(hw, reg);
663 
664 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
665 	*stat += new_data;
666 }
667 
668 /**
669  * i40e_stats_update_rx_discards - update rx_discards.
670  * @vsi: ptr to the VSI to be updated.
671  * @hw: ptr to the hardware info.
672  * @stat_idx: VSI's stat_counter_idx.
673  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
674  * @stat_offset: ptr to stat_offset to store first read of specific register.
675  * @stat: ptr to VSI's stat to be updated.
676  **/
677 static void
678 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
679 			      int stat_idx, bool offset_loaded,
680 			      struct i40e_eth_stats *stat_offset,
681 			      struct i40e_eth_stats *stat)
682 {
683 	u64 rx_rdpc, rx_rxerr;
684 
685 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
686 			   &stat_offset->rx_discards, &rx_rdpc);
687 	i40e_stat_update64(hw,
688 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
689 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
690 			   offset_loaded, &stat_offset->rx_discards_other,
691 			   &rx_rxerr);
692 
693 	stat->rx_discards = rx_rdpc + rx_rxerr;
694 }
695 
696 /**
697  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
698  * @vsi: the VSI to be updated
699  **/
700 void i40e_update_eth_stats(struct i40e_vsi *vsi)
701 {
702 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
703 	struct i40e_pf *pf = vsi->back;
704 	struct i40e_hw *hw = &pf->hw;
705 	struct i40e_eth_stats *oes;
706 	struct i40e_eth_stats *es;     /* device's eth stats */
707 
708 	es = &vsi->eth_stats;
709 	oes = &vsi->eth_stats_offsets;
710 
711 	/* Gather up the stats that the hw collects */
712 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
713 			   vsi->stat_offsets_loaded,
714 			   &oes->tx_errors, &es->tx_errors);
715 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
716 			   vsi->stat_offsets_loaded,
717 			   &oes->rx_discards, &es->rx_discards);
718 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
719 			   vsi->stat_offsets_loaded,
720 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
721 
722 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
723 			   I40E_GLV_GORCL(stat_idx),
724 			   vsi->stat_offsets_loaded,
725 			   &oes->rx_bytes, &es->rx_bytes);
726 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
727 			   I40E_GLV_UPRCL(stat_idx),
728 			   vsi->stat_offsets_loaded,
729 			   &oes->rx_unicast, &es->rx_unicast);
730 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
731 			   I40E_GLV_MPRCL(stat_idx),
732 			   vsi->stat_offsets_loaded,
733 			   &oes->rx_multicast, &es->rx_multicast);
734 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
735 			   I40E_GLV_BPRCL(stat_idx),
736 			   vsi->stat_offsets_loaded,
737 			   &oes->rx_broadcast, &es->rx_broadcast);
738 
739 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
740 			   I40E_GLV_GOTCL(stat_idx),
741 			   vsi->stat_offsets_loaded,
742 			   &oes->tx_bytes, &es->tx_bytes);
743 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
744 			   I40E_GLV_UPTCL(stat_idx),
745 			   vsi->stat_offsets_loaded,
746 			   &oes->tx_unicast, &es->tx_unicast);
747 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
748 			   I40E_GLV_MPTCL(stat_idx),
749 			   vsi->stat_offsets_loaded,
750 			   &oes->tx_multicast, &es->tx_multicast);
751 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
752 			   I40E_GLV_BPTCL(stat_idx),
753 			   vsi->stat_offsets_loaded,
754 			   &oes->tx_broadcast, &es->tx_broadcast);
755 
756 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
757 				      vsi->stat_offsets_loaded, oes, es);
758 
759 	vsi->stat_offsets_loaded = true;
760 }
761 
762 /**
763  * i40e_update_veb_stats - Update Switch component statistics
764  * @veb: the VEB being updated
765  **/
766 void i40e_update_veb_stats(struct i40e_veb *veb)
767 {
768 	struct i40e_pf *pf = veb->pf;
769 	struct i40e_hw *hw = &pf->hw;
770 	struct i40e_eth_stats *oes;
771 	struct i40e_eth_stats *es;     /* device's eth stats */
772 	struct i40e_veb_tc_stats *veb_oes;
773 	struct i40e_veb_tc_stats *veb_es;
774 	int i, idx = 0;
775 
776 	idx = veb->stats_idx;
777 	es = &veb->stats;
778 	oes = &veb->stats_offsets;
779 	veb_es = &veb->tc_stats;
780 	veb_oes = &veb->tc_stats_offsets;
781 
782 	/* Gather up the stats that the hw collects */
783 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
784 			   veb->stat_offsets_loaded,
785 			   &oes->tx_discards, &es->tx_discards);
786 	if (hw->revision_id > 0)
787 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
788 				   veb->stat_offsets_loaded,
789 				   &oes->rx_unknown_protocol,
790 				   &es->rx_unknown_protocol);
791 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
792 			   veb->stat_offsets_loaded,
793 			   &oes->rx_bytes, &es->rx_bytes);
794 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
795 			   veb->stat_offsets_loaded,
796 			   &oes->rx_unicast, &es->rx_unicast);
797 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->rx_multicast, &es->rx_multicast);
800 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
801 			   veb->stat_offsets_loaded,
802 			   &oes->rx_broadcast, &es->rx_broadcast);
803 
804 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
805 			   veb->stat_offsets_loaded,
806 			   &oes->tx_bytes, &es->tx_bytes);
807 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
808 			   veb->stat_offsets_loaded,
809 			   &oes->tx_unicast, &es->tx_unicast);
810 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->tx_multicast, &es->tx_multicast);
813 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
814 			   veb->stat_offsets_loaded,
815 			   &oes->tx_broadcast, &es->tx_broadcast);
816 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
817 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
818 				   I40E_GLVEBTC_RPCL(i, idx),
819 				   veb->stat_offsets_loaded,
820 				   &veb_oes->tc_rx_packets[i],
821 				   &veb_es->tc_rx_packets[i]);
822 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
823 				   I40E_GLVEBTC_RBCL(i, idx),
824 				   veb->stat_offsets_loaded,
825 				   &veb_oes->tc_rx_bytes[i],
826 				   &veb_es->tc_rx_bytes[i]);
827 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
828 				   I40E_GLVEBTC_TPCL(i, idx),
829 				   veb->stat_offsets_loaded,
830 				   &veb_oes->tc_tx_packets[i],
831 				   &veb_es->tc_tx_packets[i]);
832 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
833 				   I40E_GLVEBTC_TBCL(i, idx),
834 				   veb->stat_offsets_loaded,
835 				   &veb_oes->tc_tx_bytes[i],
836 				   &veb_es->tc_tx_bytes[i]);
837 	}
838 	veb->stat_offsets_loaded = true;
839 }
840 
841 /**
842  * i40e_update_vsi_stats - Update the vsi statistics counters.
843  * @vsi: the VSI to be updated
844  *
845  * There are a few instances where we store the same stat in a
846  * couple of different structs.  This is partly because we have
847  * the netdev stats that need to be filled out, which is slightly
848  * different from the "eth_stats" defined by the chip and used in
849  * VF communications.  We sort it out here.
850  **/
851 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
852 {
853 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
854 	struct i40e_pf *pf = vsi->back;
855 	struct rtnl_link_stats64 *ons;
856 	struct rtnl_link_stats64 *ns;   /* netdev stats */
857 	struct i40e_eth_stats *oes;
858 	struct i40e_eth_stats *es;     /* device's eth stats */
859 	u64 tx_restart, tx_busy;
860 	struct i40e_ring *p;
861 	u64 bytes, packets;
862 	unsigned int start;
863 	u64 tx_linearize;
864 	u64 tx_force_wb;
865 	u64 tx_stopped;
866 	u64 rx_p, rx_b;
867 	u64 tx_p, tx_b;
868 	u16 q;
869 
870 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
871 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
872 		return;
873 
874 	ns = i40e_get_vsi_stats_struct(vsi);
875 	ons = &vsi->net_stats_offsets;
876 	es = &vsi->eth_stats;
877 	oes = &vsi->eth_stats_offsets;
878 
879 	/* Gather up the netdev and vsi stats that the driver collects
880 	 * on the fly during packet processing
881 	 */
882 	rx_b = rx_p = 0;
883 	tx_b = tx_p = 0;
884 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
885 	tx_stopped = 0;
886 	rx_page = 0;
887 	rx_buf = 0;
888 	rx_reuse = 0;
889 	rx_alloc = 0;
890 	rx_waive = 0;
891 	rx_busy = 0;
892 	rcu_read_lock();
893 	for (q = 0; q < vsi->num_queue_pairs; q++) {
894 		/* locate Tx ring */
895 		p = READ_ONCE(vsi->tx_rings[q]);
896 		if (!p)
897 			continue;
898 
899 		do {
900 			start = u64_stats_fetch_begin(&p->syncp);
901 			packets = p->stats.packets;
902 			bytes = p->stats.bytes;
903 		} while (u64_stats_fetch_retry(&p->syncp, start));
904 		tx_b += bytes;
905 		tx_p += packets;
906 		tx_restart += p->tx_stats.restart_queue;
907 		tx_busy += p->tx_stats.tx_busy;
908 		tx_linearize += p->tx_stats.tx_linearize;
909 		tx_force_wb += p->tx_stats.tx_force_wb;
910 		tx_stopped += p->tx_stats.tx_stopped;
911 
912 		/* locate Rx ring */
913 		p = READ_ONCE(vsi->rx_rings[q]);
914 		if (!p)
915 			continue;
916 
917 		do {
918 			start = u64_stats_fetch_begin(&p->syncp);
919 			packets = p->stats.packets;
920 			bytes = p->stats.bytes;
921 		} while (u64_stats_fetch_retry(&p->syncp, start));
922 		rx_b += bytes;
923 		rx_p += packets;
924 		rx_buf += p->rx_stats.alloc_buff_failed;
925 		rx_page += p->rx_stats.alloc_page_failed;
926 		rx_reuse += p->rx_stats.page_reuse_count;
927 		rx_alloc += p->rx_stats.page_alloc_count;
928 		rx_waive += p->rx_stats.page_waive_count;
929 		rx_busy += p->rx_stats.page_busy_count;
930 
931 		if (i40e_enabled_xdp_vsi(vsi)) {
932 			/* locate XDP ring */
933 			p = READ_ONCE(vsi->xdp_rings[q]);
934 			if (!p)
935 				continue;
936 
937 			do {
938 				start = u64_stats_fetch_begin(&p->syncp);
939 				packets = p->stats.packets;
940 				bytes = p->stats.bytes;
941 			} while (u64_stats_fetch_retry(&p->syncp, start));
942 			tx_b += bytes;
943 			tx_p += packets;
944 			tx_restart += p->tx_stats.restart_queue;
945 			tx_busy += p->tx_stats.tx_busy;
946 			tx_linearize += p->tx_stats.tx_linearize;
947 			tx_force_wb += p->tx_stats.tx_force_wb;
948 		}
949 	}
950 	rcu_read_unlock();
951 	vsi->tx_restart = tx_restart;
952 	vsi->tx_busy = tx_busy;
953 	vsi->tx_linearize = tx_linearize;
954 	vsi->tx_force_wb = tx_force_wb;
955 	vsi->tx_stopped = tx_stopped;
956 	vsi->rx_page_failed = rx_page;
957 	vsi->rx_buf_failed = rx_buf;
958 	vsi->rx_page_reuse = rx_reuse;
959 	vsi->rx_page_alloc = rx_alloc;
960 	vsi->rx_page_waive = rx_waive;
961 	vsi->rx_page_busy = rx_busy;
962 
963 	ns->rx_packets = rx_p;
964 	ns->rx_bytes = rx_b;
965 	ns->tx_packets = tx_p;
966 	ns->tx_bytes = tx_b;
967 
968 	/* update netdev stats from eth stats */
969 	i40e_update_eth_stats(vsi);
970 	ons->tx_errors = oes->tx_errors;
971 	ns->tx_errors = es->tx_errors;
972 	ons->multicast = oes->rx_multicast;
973 	ns->multicast = es->rx_multicast;
974 	ons->rx_dropped = oes->rx_discards;
975 	ns->rx_dropped = es->rx_discards;
976 	ons->tx_dropped = oes->tx_discards;
977 	ns->tx_dropped = es->tx_discards;
978 
979 	/* pull in a couple PF stats if this is the main vsi */
980 	if (vsi == pf->vsi[pf->lan_vsi]) {
981 		ns->rx_crc_errors = pf->stats.crc_errors;
982 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
983 		ns->rx_length_errors = pf->stats.rx_length_errors;
984 	}
985 }
986 
987 /**
988  * i40e_update_pf_stats - Update the PF statistics counters.
989  * @pf: the PF to be updated
990  **/
991 static void i40e_update_pf_stats(struct i40e_pf *pf)
992 {
993 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
994 	struct i40e_hw_port_stats *nsd = &pf->stats;
995 	struct i40e_hw *hw = &pf->hw;
996 	u32 val;
997 	int i;
998 
999 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1000 			   I40E_GLPRT_GORCL(hw->port),
1001 			   pf->stat_offsets_loaded,
1002 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1003 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1004 			   I40E_GLPRT_GOTCL(hw->port),
1005 			   pf->stat_offsets_loaded,
1006 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1007 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1008 			   pf->stat_offsets_loaded,
1009 			   &osd->eth.rx_discards,
1010 			   &nsd->eth.rx_discards);
1011 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1012 			   I40E_GLPRT_UPRCL(hw->port),
1013 			   pf->stat_offsets_loaded,
1014 			   &osd->eth.rx_unicast,
1015 			   &nsd->eth.rx_unicast);
1016 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1017 			   I40E_GLPRT_MPRCL(hw->port),
1018 			   pf->stat_offsets_loaded,
1019 			   &osd->eth.rx_multicast,
1020 			   &nsd->eth.rx_multicast);
1021 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1022 			   I40E_GLPRT_BPRCL(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->eth.rx_broadcast,
1025 			   &nsd->eth.rx_broadcast);
1026 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1027 			   I40E_GLPRT_UPTCL(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->eth.tx_unicast,
1030 			   &nsd->eth.tx_unicast);
1031 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1032 			   I40E_GLPRT_MPTCL(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->eth.tx_multicast,
1035 			   &nsd->eth.tx_multicast);
1036 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1037 			   I40E_GLPRT_BPTCL(hw->port),
1038 			   pf->stat_offsets_loaded,
1039 			   &osd->eth.tx_broadcast,
1040 			   &nsd->eth.tx_broadcast);
1041 
1042 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->tx_dropped_link_down,
1045 			   &nsd->tx_dropped_link_down);
1046 
1047 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->crc_errors, &nsd->crc_errors);
1050 
1051 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1054 
1055 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->mac_local_faults,
1058 			   &nsd->mac_local_faults);
1059 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->mac_remote_faults,
1062 			   &nsd->mac_remote_faults);
1063 
1064 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->rx_length_errors,
1067 			   &nsd->rx_length_errors);
1068 
1069 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1072 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1075 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1078 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1081 
1082 	for (i = 0; i < 8; i++) {
1083 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1084 				   pf->stat_offsets_loaded,
1085 				   &osd->priority_xoff_rx[i],
1086 				   &nsd->priority_xoff_rx[i]);
1087 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1088 				   pf->stat_offsets_loaded,
1089 				   &osd->priority_xon_rx[i],
1090 				   &nsd->priority_xon_rx[i]);
1091 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1092 				   pf->stat_offsets_loaded,
1093 				   &osd->priority_xon_tx[i],
1094 				   &nsd->priority_xon_tx[i]);
1095 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1096 				   pf->stat_offsets_loaded,
1097 				   &osd->priority_xoff_tx[i],
1098 				   &nsd->priority_xoff_tx[i]);
1099 		i40e_stat_update32(hw,
1100 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1101 				   pf->stat_offsets_loaded,
1102 				   &osd->priority_xon_2_xoff[i],
1103 				   &nsd->priority_xon_2_xoff[i]);
1104 	}
1105 
1106 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1107 			   I40E_GLPRT_PRC64L(hw->port),
1108 			   pf->stat_offsets_loaded,
1109 			   &osd->rx_size_64, &nsd->rx_size_64);
1110 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1111 			   I40E_GLPRT_PRC127L(hw->port),
1112 			   pf->stat_offsets_loaded,
1113 			   &osd->rx_size_127, &nsd->rx_size_127);
1114 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1115 			   I40E_GLPRT_PRC255L(hw->port),
1116 			   pf->stat_offsets_loaded,
1117 			   &osd->rx_size_255, &nsd->rx_size_255);
1118 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1119 			   I40E_GLPRT_PRC511L(hw->port),
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->rx_size_511, &nsd->rx_size_511);
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1123 			   I40E_GLPRT_PRC1023L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1127 			   I40E_GLPRT_PRC1522L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1131 			   I40E_GLPRT_PRC9522L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_big, &nsd->rx_size_big);
1134 
1135 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1136 			   I40E_GLPRT_PTC64L(hw->port),
1137 			   pf->stat_offsets_loaded,
1138 			   &osd->tx_size_64, &nsd->tx_size_64);
1139 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1140 			   I40E_GLPRT_PTC127L(hw->port),
1141 			   pf->stat_offsets_loaded,
1142 			   &osd->tx_size_127, &nsd->tx_size_127);
1143 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1144 			   I40E_GLPRT_PTC255L(hw->port),
1145 			   pf->stat_offsets_loaded,
1146 			   &osd->tx_size_255, &nsd->tx_size_255);
1147 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1148 			   I40E_GLPRT_PTC511L(hw->port),
1149 			   pf->stat_offsets_loaded,
1150 			   &osd->tx_size_511, &nsd->tx_size_511);
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1152 			   I40E_GLPRT_PTC1023L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1156 			   I40E_GLPRT_PTC1522L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1160 			   I40E_GLPRT_PTC9522L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_big, &nsd->tx_size_big);
1163 
1164 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->rx_undersize, &nsd->rx_undersize);
1167 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1168 			   pf->stat_offsets_loaded,
1169 			   &osd->rx_fragments, &nsd->rx_fragments);
1170 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->rx_oversize, &nsd->rx_oversize);
1173 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1174 			   pf->stat_offsets_loaded,
1175 			   &osd->rx_jabber, &nsd->rx_jabber);
1176 
1177 	/* FDIR stats */
1178 	i40e_stat_update_and_clear32(hw,
1179 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1180 			&nsd->fd_atr_match);
1181 	i40e_stat_update_and_clear32(hw,
1182 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1183 			&nsd->fd_sb_match);
1184 	i40e_stat_update_and_clear32(hw,
1185 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1186 			&nsd->fd_atr_tunnel_match);
1187 
1188 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1189 	nsd->tx_lpi_status =
1190 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1191 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1192 	nsd->rx_lpi_status =
1193 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1194 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1195 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1196 			   pf->stat_offsets_loaded,
1197 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1198 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1199 			   pf->stat_offsets_loaded,
1200 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1201 
1202 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1203 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1204 		nsd->fd_sb_status = true;
1205 	else
1206 		nsd->fd_sb_status = false;
1207 
1208 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1209 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1210 		nsd->fd_atr_status = true;
1211 	else
1212 		nsd->fd_atr_status = false;
1213 
1214 	pf->stat_offsets_loaded = true;
1215 }
1216 
1217 /**
1218  * i40e_update_stats - Update the various statistics counters.
1219  * @vsi: the VSI to be updated
1220  *
1221  * Update the various stats for this VSI and its related entities.
1222  **/
1223 void i40e_update_stats(struct i40e_vsi *vsi)
1224 {
1225 	struct i40e_pf *pf = vsi->back;
1226 
1227 	if (vsi == pf->vsi[pf->lan_vsi])
1228 		i40e_update_pf_stats(pf);
1229 
1230 	i40e_update_vsi_stats(vsi);
1231 }
1232 
1233 /**
1234  * i40e_count_filters - counts VSI mac filters
1235  * @vsi: the VSI to be searched
1236  *
1237  * Returns count of mac filters
1238  **/
1239 int i40e_count_filters(struct i40e_vsi *vsi)
1240 {
1241 	struct i40e_mac_filter *f;
1242 	struct hlist_node *h;
1243 	int bkt;
1244 	int cnt = 0;
1245 
1246 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1247 		++cnt;
1248 
1249 	return cnt;
1250 }
1251 
1252 /**
1253  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1254  * @vsi: the VSI to be searched
1255  * @macaddr: the MAC address
1256  * @vlan: the vlan
1257  *
1258  * Returns ptr to the filter object or NULL
1259  **/
1260 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1261 						const u8 *macaddr, s16 vlan)
1262 {
1263 	struct i40e_mac_filter *f;
1264 	u64 key;
1265 
1266 	if (!vsi || !macaddr)
1267 		return NULL;
1268 
1269 	key = i40e_addr_to_hkey(macaddr);
1270 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1271 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1272 		    (vlan == f->vlan))
1273 			return f;
1274 	}
1275 	return NULL;
1276 }
1277 
1278 /**
1279  * i40e_find_mac - Find a mac addr in the macvlan filters list
1280  * @vsi: the VSI to be searched
1281  * @macaddr: the MAC address we are searching for
1282  *
1283  * Returns the first filter with the provided MAC address or NULL if
1284  * MAC address was not found
1285  **/
1286 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1287 {
1288 	struct i40e_mac_filter *f;
1289 	u64 key;
1290 
1291 	if (!vsi || !macaddr)
1292 		return NULL;
1293 
1294 	key = i40e_addr_to_hkey(macaddr);
1295 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1296 		if ((ether_addr_equal(macaddr, f->macaddr)))
1297 			return f;
1298 	}
1299 	return NULL;
1300 }
1301 
1302 /**
1303  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1304  * @vsi: the VSI to be searched
1305  *
1306  * Returns true if VSI is in vlan mode or false otherwise
1307  **/
1308 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1309 {
1310 	/* If we have a PVID, always operate in VLAN mode */
1311 	if (vsi->info.pvid)
1312 		return true;
1313 
1314 	/* We need to operate in VLAN mode whenever we have any filters with
1315 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1316 	 * time, incurring search cost repeatedly. However, we can notice two
1317 	 * things:
1318 	 *
1319 	 * 1) the only place where we can gain a VLAN filter is in
1320 	 *    i40e_add_filter.
1321 	 *
1322 	 * 2) the only place where filters are actually removed is in
1323 	 *    i40e_sync_filters_subtask.
1324 	 *
1325 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1326 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1327 	 * we have to perform the full search after deleting filters in
1328 	 * i40e_sync_filters_subtask, but we already have to search
1329 	 * filters here and can perform the check at the same time. This
1330 	 * results in avoiding embedding a loop for VLAN mode inside another
1331 	 * loop over all the filters, and should maintain correctness as noted
1332 	 * above.
1333 	 */
1334 	return vsi->has_vlan_filter;
1335 }
1336 
1337 /**
1338  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1339  * @vsi: the VSI to configure
1340  * @tmp_add_list: list of filters ready to be added
1341  * @tmp_del_list: list of filters ready to be deleted
1342  * @vlan_filters: the number of active VLAN filters
1343  *
1344  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1345  * behave as expected. If we have any active VLAN filters remaining or about
1346  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1347  * so that they only match against untagged traffic. If we no longer have any
1348  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1349  * so that they match against both tagged and untagged traffic. In this way,
1350  * we ensure that we correctly receive the desired traffic. This ensures that
1351  * when we have an active VLAN we will receive only untagged traffic and
1352  * traffic matching active VLANs. If we have no active VLANs then we will
1353  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1354  *
1355  * Finally, in a similar fashion, this function also corrects filters when
1356  * there is an active PVID assigned to this VSI.
1357  *
1358  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1359  *
1360  * This function is only expected to be called from within
1361  * i40e_sync_vsi_filters.
1362  *
1363  * NOTE: This function expects to be called while under the
1364  * mac_filter_hash_lock
1365  */
1366 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1367 					 struct hlist_head *tmp_add_list,
1368 					 struct hlist_head *tmp_del_list,
1369 					 int vlan_filters)
1370 {
1371 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1372 	struct i40e_mac_filter *f, *add_head;
1373 	struct i40e_new_mac_filter *new;
1374 	struct hlist_node *h;
1375 	int bkt, new_vlan;
1376 
1377 	/* To determine if a particular filter needs to be replaced we
1378 	 * have the three following conditions:
1379 	 *
1380 	 * a) if we have a PVID assigned, then all filters which are
1381 	 *    not marked as VLAN=PVID must be replaced with filters that
1382 	 *    are.
1383 	 * b) otherwise, if we have any active VLANS, all filters
1384 	 *    which are marked as VLAN=-1 must be replaced with
1385 	 *    filters marked as VLAN=0
1386 	 * c) finally, if we do not have any active VLANS, all filters
1387 	 *    which are marked as VLAN=0 must be replaced with filters
1388 	 *    marked as VLAN=-1
1389 	 */
1390 
1391 	/* Update the filters about to be added in place */
1392 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1393 		if (pvid && new->f->vlan != pvid)
1394 			new->f->vlan = pvid;
1395 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1396 			new->f->vlan = 0;
1397 		else if (!vlan_filters && new->f->vlan == 0)
1398 			new->f->vlan = I40E_VLAN_ANY;
1399 	}
1400 
1401 	/* Update the remaining active filters */
1402 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1403 		/* Combine the checks for whether a filter needs to be changed
1404 		 * and then determine the new VLAN inside the if block, in
1405 		 * order to avoid duplicating code for adding the new filter
1406 		 * then deleting the old filter.
1407 		 */
1408 		if ((pvid && f->vlan != pvid) ||
1409 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1410 		    (!vlan_filters && f->vlan == 0)) {
1411 			/* Determine the new vlan we will be adding */
1412 			if (pvid)
1413 				new_vlan = pvid;
1414 			else if (vlan_filters)
1415 				new_vlan = 0;
1416 			else
1417 				new_vlan = I40E_VLAN_ANY;
1418 
1419 			/* Create the new filter */
1420 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1421 			if (!add_head)
1422 				return -ENOMEM;
1423 
1424 			/* Create a temporary i40e_new_mac_filter */
1425 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1426 			if (!new)
1427 				return -ENOMEM;
1428 
1429 			new->f = add_head;
1430 			new->state = add_head->state;
1431 
1432 			/* Add the new filter to the tmp list */
1433 			hlist_add_head(&new->hlist, tmp_add_list);
1434 
1435 			/* Put the original filter into the delete list */
1436 			f->state = I40E_FILTER_REMOVE;
1437 			hash_del(&f->hlist);
1438 			hlist_add_head(&f->hlist, tmp_del_list);
1439 		}
1440 	}
1441 
1442 	vsi->has_vlan_filter = !!vlan_filters;
1443 
1444 	return 0;
1445 }
1446 
1447 /**
1448  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1449  * @vsi: the vsi to configure
1450  * @new_mac: new mac filter to be added
1451  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1452  * @vlan_filters: the number of active VLAN filters
1453  * @trusted: flag if the VF is trusted
1454  *
1455  * Get new VLAN id based on current VLAN filters, trust, PVID
1456  * and vf-vlan-prune-disable flag.
1457  *
1458  * Returns the value of the new vlan filter or
1459  * the old value if no new filter is needed.
1460  */
1461 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1462 				struct i40e_new_mac_filter *new_mac,
1463 				struct i40e_mac_filter *f,
1464 				int vlan_filters,
1465 				bool trusted)
1466 {
1467 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1468 	struct i40e_pf *pf = vsi->back;
1469 	bool is_any;
1470 
1471 	if (new_mac)
1472 		f = new_mac->f;
1473 
1474 	if (pvid && f->vlan != pvid)
1475 		return pvid;
1476 
1477 	is_any = (trusted ||
1478 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1479 
1480 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1481 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1482 	    (is_any && !vlan_filters && f->vlan == 0)) {
1483 		if (is_any)
1484 			return I40E_VLAN_ANY;
1485 		else
1486 			return 0;
1487 	}
1488 
1489 	return f->vlan;
1490 }
1491 
1492 /**
1493  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1494  * @vsi: the vsi to configure
1495  * @tmp_add_list: list of filters ready to be added
1496  * @tmp_del_list: list of filters ready to be deleted
1497  * @vlan_filters: the number of active VLAN filters
1498  * @trusted: flag if the VF is trusted
1499  *
1500  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1501  * and vf-vlan-prune-disable flag.
1502  *
1503  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1504  *
1505  * This function is only expected to be called from within
1506  * i40e_sync_vsi_filters.
1507  *
1508  * NOTE: This function expects to be called while under the
1509  * mac_filter_hash_lock
1510  */
1511 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1512 					    struct hlist_head *tmp_add_list,
1513 					    struct hlist_head *tmp_del_list,
1514 					    int vlan_filters,
1515 					    bool trusted)
1516 {
1517 	struct i40e_mac_filter *f, *add_head;
1518 	struct i40e_new_mac_filter *new_mac;
1519 	struct hlist_node *h;
1520 	int bkt, new_vlan;
1521 
1522 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1523 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1524 							vlan_filters, trusted);
1525 	}
1526 
1527 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1528 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1529 						trusted);
1530 		if (new_vlan != f->vlan) {
1531 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1532 			if (!add_head)
1533 				return -ENOMEM;
1534 			/* Create a temporary i40e_new_mac_filter */
1535 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1536 			if (!new_mac)
1537 				return -ENOMEM;
1538 			new_mac->f = add_head;
1539 			new_mac->state = add_head->state;
1540 
1541 			/* Add the new filter to the tmp list */
1542 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1543 
1544 			/* Put the original filter into the delete list */
1545 			f->state = I40E_FILTER_REMOVE;
1546 			hash_del(&f->hlist);
1547 			hlist_add_head(&f->hlist, tmp_del_list);
1548 		}
1549 	}
1550 
1551 	vsi->has_vlan_filter = !!vlan_filters;
1552 	return 0;
1553 }
1554 
1555 /**
1556  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1557  * @vsi: the PF Main VSI - inappropriate for any other VSI
1558  * @macaddr: the MAC address
1559  *
1560  * Remove whatever filter the firmware set up so the driver can manage
1561  * its own filtering intelligently.
1562  **/
1563 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1564 {
1565 	struct i40e_aqc_remove_macvlan_element_data element;
1566 	struct i40e_pf *pf = vsi->back;
1567 
1568 	/* Only appropriate for the PF main VSI */
1569 	if (vsi->type != I40E_VSI_MAIN)
1570 		return;
1571 
1572 	memset(&element, 0, sizeof(element));
1573 	ether_addr_copy(element.mac_addr, macaddr);
1574 	element.vlan_tag = 0;
1575 	/* Ignore error returns, some firmware does it this way... */
1576 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1577 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1578 
1579 	memset(&element, 0, sizeof(element));
1580 	ether_addr_copy(element.mac_addr, macaddr);
1581 	element.vlan_tag = 0;
1582 	/* ...and some firmware does it this way. */
1583 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1584 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1585 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1586 }
1587 
1588 /**
1589  * i40e_add_filter - Add a mac/vlan filter to the VSI
1590  * @vsi: the VSI to be searched
1591  * @macaddr: the MAC address
1592  * @vlan: the vlan
1593  *
1594  * Returns ptr to the filter object or NULL when no memory available.
1595  *
1596  * NOTE: This function is expected to be called with mac_filter_hash_lock
1597  * being held.
1598  **/
1599 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1600 					const u8 *macaddr, s16 vlan)
1601 {
1602 	struct i40e_mac_filter *f;
1603 	u64 key;
1604 
1605 	if (!vsi || !macaddr)
1606 		return NULL;
1607 
1608 	f = i40e_find_filter(vsi, macaddr, vlan);
1609 	if (!f) {
1610 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1611 		if (!f)
1612 			return NULL;
1613 
1614 		/* Update the boolean indicating if we need to function in
1615 		 * VLAN mode.
1616 		 */
1617 		if (vlan >= 0)
1618 			vsi->has_vlan_filter = true;
1619 
1620 		ether_addr_copy(f->macaddr, macaddr);
1621 		f->vlan = vlan;
1622 		f->state = I40E_FILTER_NEW;
1623 		INIT_HLIST_NODE(&f->hlist);
1624 
1625 		key = i40e_addr_to_hkey(macaddr);
1626 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1627 
1628 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1629 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1630 	}
1631 
1632 	/* If we're asked to add a filter that has been marked for removal, it
1633 	 * is safe to simply restore it to active state. __i40e_del_filter
1634 	 * will have simply deleted any filters which were previously marked
1635 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1636 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1637 	 * task, just restore this filter to the ACTIVE state so that the
1638 	 * sync task leaves it in place
1639 	 */
1640 	if (f->state == I40E_FILTER_REMOVE)
1641 		f->state = I40E_FILTER_ACTIVE;
1642 
1643 	return f;
1644 }
1645 
1646 /**
1647  * __i40e_del_filter - Remove a specific filter from the VSI
1648  * @vsi: VSI to remove from
1649  * @f: the filter to remove from the list
1650  *
1651  * This function should be called instead of i40e_del_filter only if you know
1652  * the exact filter you will remove already, such as via i40e_find_filter or
1653  * i40e_find_mac.
1654  *
1655  * NOTE: This function is expected to be called with mac_filter_hash_lock
1656  * being held.
1657  * ANOTHER NOTE: This function MUST be called from within the context of
1658  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1659  * instead of list_for_each_entry().
1660  **/
1661 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1662 {
1663 	if (!f)
1664 		return;
1665 
1666 	/* If the filter was never added to firmware then we can just delete it
1667 	 * directly and we don't want to set the status to remove or else an
1668 	 * admin queue command will unnecessarily fire.
1669 	 */
1670 	if ((f->state == I40E_FILTER_FAILED) ||
1671 	    (f->state == I40E_FILTER_NEW)) {
1672 		hash_del(&f->hlist);
1673 		kfree(f);
1674 	} else {
1675 		f->state = I40E_FILTER_REMOVE;
1676 	}
1677 
1678 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1679 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1680 }
1681 
1682 /**
1683  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1684  * @vsi: the VSI to be searched
1685  * @macaddr: the MAC address
1686  * @vlan: the VLAN
1687  *
1688  * NOTE: This function is expected to be called with mac_filter_hash_lock
1689  * being held.
1690  * ANOTHER NOTE: This function MUST be called from within the context of
1691  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1692  * instead of list_for_each_entry().
1693  **/
1694 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1695 {
1696 	struct i40e_mac_filter *f;
1697 
1698 	if (!vsi || !macaddr)
1699 		return;
1700 
1701 	f = i40e_find_filter(vsi, macaddr, vlan);
1702 	__i40e_del_filter(vsi, f);
1703 }
1704 
1705 /**
1706  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1707  * @vsi: the VSI to be searched
1708  * @macaddr: the mac address to be filtered
1709  *
1710  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1711  * go through all the macvlan filters and add a macvlan filter for each
1712  * unique vlan that already exists. If a PVID has been assigned, instead only
1713  * add the macaddr to that VLAN.
1714  *
1715  * Returns last filter added on success, else NULL
1716  **/
1717 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1718 					    const u8 *macaddr)
1719 {
1720 	struct i40e_mac_filter *f, *add = NULL;
1721 	struct hlist_node *h;
1722 	int bkt;
1723 
1724 	if (vsi->info.pvid)
1725 		return i40e_add_filter(vsi, macaddr,
1726 				       le16_to_cpu(vsi->info.pvid));
1727 
1728 	if (!i40e_is_vsi_in_vlan(vsi))
1729 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1730 
1731 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1732 		if (f->state == I40E_FILTER_REMOVE)
1733 			continue;
1734 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1735 		if (!add)
1736 			return NULL;
1737 	}
1738 
1739 	return add;
1740 }
1741 
1742 /**
1743  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1744  * @vsi: the VSI to be searched
1745  * @macaddr: the mac address to be removed
1746  *
1747  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1748  * associated with.
1749  *
1750  * Returns 0 for success, or error
1751  **/
1752 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1753 {
1754 	struct i40e_mac_filter *f;
1755 	struct hlist_node *h;
1756 	bool found = false;
1757 	int bkt;
1758 
1759 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1760 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1761 		if (ether_addr_equal(macaddr, f->macaddr)) {
1762 			__i40e_del_filter(vsi, f);
1763 			found = true;
1764 		}
1765 	}
1766 
1767 	if (found)
1768 		return 0;
1769 	else
1770 		return -ENOENT;
1771 }
1772 
1773 /**
1774  * i40e_set_mac - NDO callback to set mac address
1775  * @netdev: network interface device structure
1776  * @p: pointer to an address structure
1777  *
1778  * Returns 0 on success, negative on failure
1779  **/
1780 static int i40e_set_mac(struct net_device *netdev, void *p)
1781 {
1782 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1783 	struct i40e_vsi *vsi = np->vsi;
1784 	struct i40e_pf *pf = vsi->back;
1785 	struct i40e_hw *hw = &pf->hw;
1786 	struct sockaddr *addr = p;
1787 
1788 	if (!is_valid_ether_addr(addr->sa_data))
1789 		return -EADDRNOTAVAIL;
1790 
1791 	if (test_bit(__I40E_DOWN, pf->state) ||
1792 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1793 		return -EADDRNOTAVAIL;
1794 
1795 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1796 		netdev_info(netdev, "returning to hw mac address %pM\n",
1797 			    hw->mac.addr);
1798 	else
1799 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1800 
1801 	/* Copy the address first, so that we avoid a possible race with
1802 	 * .set_rx_mode().
1803 	 * - Remove old address from MAC filter
1804 	 * - Copy new address
1805 	 * - Add new address to MAC filter
1806 	 */
1807 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1808 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1809 	eth_hw_addr_set(netdev, addr->sa_data);
1810 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1811 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1812 
1813 	if (vsi->type == I40E_VSI_MAIN) {
1814 		int ret;
1815 
1816 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1817 						addr->sa_data, NULL);
1818 		if (ret)
1819 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1820 				    ERR_PTR(ret),
1821 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1822 	}
1823 
1824 	/* schedule our worker thread which will take care of
1825 	 * applying the new filter changes
1826 	 */
1827 	i40e_service_event_schedule(pf);
1828 	return 0;
1829 }
1830 
1831 /**
1832  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1833  * @vsi: vsi structure
1834  * @seed: RSS hash seed
1835  * @lut: pointer to lookup table of lut_size
1836  * @lut_size: size of the lookup table
1837  **/
1838 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1839 			      u8 *lut, u16 lut_size)
1840 {
1841 	struct i40e_pf *pf = vsi->back;
1842 	struct i40e_hw *hw = &pf->hw;
1843 	int ret = 0;
1844 
1845 	if (seed) {
1846 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1847 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1848 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1849 		if (ret) {
1850 			dev_info(&pf->pdev->dev,
1851 				 "Cannot set RSS key, err %pe aq_err %s\n",
1852 				 ERR_PTR(ret),
1853 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1854 			return ret;
1855 		}
1856 	}
1857 	if (lut) {
1858 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1859 
1860 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1861 		if (ret) {
1862 			dev_info(&pf->pdev->dev,
1863 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1864 				 ERR_PTR(ret),
1865 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1866 			return ret;
1867 		}
1868 	}
1869 	return ret;
1870 }
1871 
1872 /**
1873  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1874  * @vsi: VSI structure
1875  **/
1876 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1877 {
1878 	struct i40e_pf *pf = vsi->back;
1879 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1880 	u8 *lut;
1881 	int ret;
1882 
1883 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1884 		return 0;
1885 	if (!vsi->rss_size)
1886 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1887 				      vsi->num_queue_pairs);
1888 	if (!vsi->rss_size)
1889 		return -EINVAL;
1890 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1891 	if (!lut)
1892 		return -ENOMEM;
1893 
1894 	/* Use the user configured hash keys and lookup table if there is one,
1895 	 * otherwise use default
1896 	 */
1897 	if (vsi->rss_lut_user)
1898 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1899 	else
1900 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1901 	if (vsi->rss_hkey_user)
1902 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1903 	else
1904 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1905 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1906 	kfree(lut);
1907 	return ret;
1908 }
1909 
1910 /**
1911  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1912  * @vsi: the VSI being configured,
1913  * @ctxt: VSI context structure
1914  * @enabled_tc: number of traffic classes to enable
1915  *
1916  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1917  **/
1918 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1919 					   struct i40e_vsi_context *ctxt,
1920 					   u8 enabled_tc)
1921 {
1922 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1923 	int i, override_q, pow, num_qps, ret;
1924 	u8 netdev_tc = 0, offset = 0;
1925 
1926 	if (vsi->type != I40E_VSI_MAIN)
1927 		return -EINVAL;
1928 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1929 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1930 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1931 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1932 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1933 
1934 	/* find the next higher power-of-2 of num queue pairs */
1935 	pow = ilog2(num_qps);
1936 	if (!is_power_of_2(num_qps))
1937 		pow++;
1938 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1939 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1940 
1941 	/* Setup queue offset/count for all TCs for given VSI */
1942 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1943 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1944 		/* See if the given TC is enabled for the given VSI */
1945 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1946 			offset = vsi->mqprio_qopt.qopt.offset[i];
1947 			qcount = vsi->mqprio_qopt.qopt.count[i];
1948 			if (qcount > max_qcount)
1949 				max_qcount = qcount;
1950 			vsi->tc_config.tc_info[i].qoffset = offset;
1951 			vsi->tc_config.tc_info[i].qcount = qcount;
1952 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1953 		} else {
1954 			/* TC is not enabled so set the offset to
1955 			 * default queue and allocate one queue
1956 			 * for the given TC.
1957 			 */
1958 			vsi->tc_config.tc_info[i].qoffset = 0;
1959 			vsi->tc_config.tc_info[i].qcount = 1;
1960 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1961 		}
1962 	}
1963 
1964 	/* Set actual Tx/Rx queue pairs */
1965 	vsi->num_queue_pairs = offset + qcount;
1966 
1967 	/* Setup queue TC[0].qmap for given VSI context */
1968 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1969 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1970 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1971 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1972 
1973 	/* Reconfigure RSS for main VSI with max queue count */
1974 	vsi->rss_size = max_qcount;
1975 	ret = i40e_vsi_config_rss(vsi);
1976 	if (ret) {
1977 		dev_info(&vsi->back->pdev->dev,
1978 			 "Failed to reconfig rss for num_queues (%u)\n",
1979 			 max_qcount);
1980 		return ret;
1981 	}
1982 	vsi->reconfig_rss = true;
1983 	dev_dbg(&vsi->back->pdev->dev,
1984 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1985 
1986 	/* Find queue count available for channel VSIs and starting offset
1987 	 * for channel VSIs
1988 	 */
1989 	override_q = vsi->mqprio_qopt.qopt.count[0];
1990 	if (override_q && override_q < vsi->num_queue_pairs) {
1991 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1992 		vsi->next_base_queue = override_q;
1993 	}
1994 	return 0;
1995 }
1996 
1997 /**
1998  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1999  * @vsi: the VSI being setup
2000  * @ctxt: VSI context structure
2001  * @enabled_tc: Enabled TCs bitmap
2002  * @is_add: True if called before Add VSI
2003  *
2004  * Setup VSI queue mapping for enabled traffic classes.
2005  **/
2006 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2007 				     struct i40e_vsi_context *ctxt,
2008 				     u8 enabled_tc,
2009 				     bool is_add)
2010 {
2011 	struct i40e_pf *pf = vsi->back;
2012 	u16 num_tc_qps = 0;
2013 	u16 sections = 0;
2014 	u8 netdev_tc = 0;
2015 	u16 numtc = 1;
2016 	u16 qcount;
2017 	u8 offset;
2018 	u16 qmap;
2019 	int i;
2020 
2021 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2022 	offset = 0;
2023 	/* zero out queue mapping, it will get updated on the end of the function */
2024 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2025 
2026 	if (vsi->type == I40E_VSI_MAIN) {
2027 		/* This code helps add more queue to the VSI if we have
2028 		 * more cores than RSS can support, the higher cores will
2029 		 * be served by ATR or other filters. Furthermore, the
2030 		 * non-zero req_queue_pairs says that user requested a new
2031 		 * queue count via ethtool's set_channels, so use this
2032 		 * value for queues distribution across traffic classes
2033 		 * We need at least one queue pair for the interface
2034 		 * to be usable as we see in else statement.
2035 		 */
2036 		if (vsi->req_queue_pairs > 0)
2037 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2038 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2039 			vsi->num_queue_pairs = pf->num_lan_msix;
2040 		else
2041 			vsi->num_queue_pairs = 1;
2042 	}
2043 
2044 	/* Number of queues per enabled TC */
2045 	if (vsi->type == I40E_VSI_MAIN ||
2046 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2047 		num_tc_qps = vsi->num_queue_pairs;
2048 	else
2049 		num_tc_qps = vsi->alloc_queue_pairs;
2050 
2051 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2052 		/* Find numtc from enabled TC bitmap */
2053 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2054 			if (enabled_tc & BIT(i)) /* TC is enabled */
2055 				numtc++;
2056 		}
2057 		if (!numtc) {
2058 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2059 			numtc = 1;
2060 		}
2061 		num_tc_qps = num_tc_qps / numtc;
2062 		num_tc_qps = min_t(int, num_tc_qps,
2063 				   i40e_pf_get_max_q_per_tc(pf));
2064 	}
2065 
2066 	vsi->tc_config.numtc = numtc;
2067 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2068 
2069 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2070 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2071 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2072 
2073 	/* Setup queue offset/count for all TCs for given VSI */
2074 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2075 		/* See if the given TC is enabled for the given VSI */
2076 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2077 			/* TC is enabled */
2078 			int pow, num_qps;
2079 
2080 			switch (vsi->type) {
2081 			case I40E_VSI_MAIN:
2082 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2083 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2084 				    vsi->tc_config.enabled_tc != 1) {
2085 					qcount = min_t(int, pf->alloc_rss_size,
2086 						       num_tc_qps);
2087 					break;
2088 				}
2089 				fallthrough;
2090 			case I40E_VSI_FDIR:
2091 			case I40E_VSI_SRIOV:
2092 			case I40E_VSI_VMDQ2:
2093 			default:
2094 				qcount = num_tc_qps;
2095 				WARN_ON(i != 0);
2096 				break;
2097 			}
2098 			vsi->tc_config.tc_info[i].qoffset = offset;
2099 			vsi->tc_config.tc_info[i].qcount = qcount;
2100 
2101 			/* find the next higher power-of-2 of num queue pairs */
2102 			num_qps = qcount;
2103 			pow = 0;
2104 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2105 				pow++;
2106 				num_qps >>= 1;
2107 			}
2108 
2109 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2110 			qmap =
2111 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2112 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2113 
2114 			offset += qcount;
2115 		} else {
2116 			/* TC is not enabled so set the offset to
2117 			 * default queue and allocate one queue
2118 			 * for the given TC.
2119 			 */
2120 			vsi->tc_config.tc_info[i].qoffset = 0;
2121 			vsi->tc_config.tc_info[i].qcount = 1;
2122 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2123 
2124 			qmap = 0;
2125 		}
2126 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2127 	}
2128 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2129 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2130 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2131 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2132 		vsi->num_queue_pairs = offset;
2133 
2134 	/* Scheduler section valid can only be set for ADD VSI */
2135 	if (is_add) {
2136 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2137 
2138 		ctxt->info.up_enable_bits = enabled_tc;
2139 	}
2140 	if (vsi->type == I40E_VSI_SRIOV) {
2141 		ctxt->info.mapping_flags |=
2142 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2143 		for (i = 0; i < vsi->num_queue_pairs; i++)
2144 			ctxt->info.queue_mapping[i] =
2145 					       cpu_to_le16(vsi->base_queue + i);
2146 	} else {
2147 		ctxt->info.mapping_flags |=
2148 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2149 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2150 	}
2151 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2152 }
2153 
2154 /**
2155  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2156  * @netdev: the netdevice
2157  * @addr: address to add
2158  *
2159  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2160  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2161  */
2162 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2163 {
2164 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2165 	struct i40e_vsi *vsi = np->vsi;
2166 
2167 	if (i40e_add_mac_filter(vsi, addr))
2168 		return 0;
2169 	else
2170 		return -ENOMEM;
2171 }
2172 
2173 /**
2174  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2175  * @netdev: the netdevice
2176  * @addr: address to add
2177  *
2178  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2179  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2180  */
2181 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2182 {
2183 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2184 	struct i40e_vsi *vsi = np->vsi;
2185 
2186 	/* Under some circumstances, we might receive a request to delete
2187 	 * our own device address from our uc list. Because we store the
2188 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2189 	 * such requests and not delete our device address from this list.
2190 	 */
2191 	if (ether_addr_equal(addr, netdev->dev_addr))
2192 		return 0;
2193 
2194 	i40e_del_mac_filter(vsi, addr);
2195 
2196 	return 0;
2197 }
2198 
2199 /**
2200  * i40e_set_rx_mode - NDO callback to set the netdev filters
2201  * @netdev: network interface device structure
2202  **/
2203 static void i40e_set_rx_mode(struct net_device *netdev)
2204 {
2205 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2206 	struct i40e_vsi *vsi = np->vsi;
2207 
2208 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2209 
2210 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2211 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2212 
2213 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2214 
2215 	/* check for other flag changes */
2216 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2217 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2218 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2219 	}
2220 }
2221 
2222 /**
2223  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2224  * @vsi: Pointer to VSI struct
2225  * @from: Pointer to list which contains MAC filter entries - changes to
2226  *        those entries needs to be undone.
2227  *
2228  * MAC filter entries from this list were slated for deletion.
2229  **/
2230 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2231 					 struct hlist_head *from)
2232 {
2233 	struct i40e_mac_filter *f;
2234 	struct hlist_node *h;
2235 
2236 	hlist_for_each_entry_safe(f, h, from, hlist) {
2237 		u64 key = i40e_addr_to_hkey(f->macaddr);
2238 
2239 		/* Move the element back into MAC filter list*/
2240 		hlist_del(&f->hlist);
2241 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2242 	}
2243 }
2244 
2245 /**
2246  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2247  * @vsi: Pointer to vsi struct
2248  * @from: Pointer to list which contains MAC filter entries - changes to
2249  *        those entries needs to be undone.
2250  *
2251  * MAC filter entries from this list were slated for addition.
2252  **/
2253 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2254 					 struct hlist_head *from)
2255 {
2256 	struct i40e_new_mac_filter *new;
2257 	struct hlist_node *h;
2258 
2259 	hlist_for_each_entry_safe(new, h, from, hlist) {
2260 		/* We can simply free the wrapper structure */
2261 		hlist_del(&new->hlist);
2262 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2263 		kfree(new);
2264 	}
2265 }
2266 
2267 /**
2268  * i40e_next_filter - Get the next non-broadcast filter from a list
2269  * @next: pointer to filter in list
2270  *
2271  * Returns the next non-broadcast filter in the list. Required so that we
2272  * ignore broadcast filters within the list, since these are not handled via
2273  * the normal firmware update path.
2274  */
2275 static
2276 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2277 {
2278 	hlist_for_each_entry_continue(next, hlist) {
2279 		if (!is_broadcast_ether_addr(next->f->macaddr))
2280 			return next;
2281 	}
2282 
2283 	return NULL;
2284 }
2285 
2286 /**
2287  * i40e_update_filter_state - Update filter state based on return data
2288  * from firmware
2289  * @count: Number of filters added
2290  * @add_list: return data from fw
2291  * @add_head: pointer to first filter in current batch
2292  *
2293  * MAC filter entries from list were slated to be added to device. Returns
2294  * number of successful filters. Note that 0 does NOT mean success!
2295  **/
2296 static int
2297 i40e_update_filter_state(int count,
2298 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2299 			 struct i40e_new_mac_filter *add_head)
2300 {
2301 	int retval = 0;
2302 	int i;
2303 
2304 	for (i = 0; i < count; i++) {
2305 		/* Always check status of each filter. We don't need to check
2306 		 * the firmware return status because we pre-set the filter
2307 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2308 		 * request to the adminq. Thus, if it no longer matches then
2309 		 * we know the filter is active.
2310 		 */
2311 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2312 			add_head->state = I40E_FILTER_FAILED;
2313 		} else {
2314 			add_head->state = I40E_FILTER_ACTIVE;
2315 			retval++;
2316 		}
2317 
2318 		add_head = i40e_next_filter(add_head);
2319 		if (!add_head)
2320 			break;
2321 	}
2322 
2323 	return retval;
2324 }
2325 
2326 /**
2327  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2328  * @vsi: ptr to the VSI
2329  * @vsi_name: name to display in messages
2330  * @list: the list of filters to send to firmware
2331  * @num_del: the number of filters to delete
2332  * @retval: Set to -EIO on failure to delete
2333  *
2334  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2335  * *retval instead of a return value so that success does not force ret_val to
2336  * be set to 0. This ensures that a sequence of calls to this function
2337  * preserve the previous value of *retval on successful delete.
2338  */
2339 static
2340 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2341 			  struct i40e_aqc_remove_macvlan_element_data *list,
2342 			  int num_del, int *retval)
2343 {
2344 	struct i40e_hw *hw = &vsi->back->hw;
2345 	enum i40e_admin_queue_err aq_status;
2346 	int aq_ret;
2347 
2348 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2349 					   &aq_status);
2350 
2351 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2352 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2353 		*retval = -EIO;
2354 		dev_info(&vsi->back->pdev->dev,
2355 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2356 			 vsi_name, ERR_PTR(aq_ret),
2357 			 i40e_aq_str(hw, aq_status));
2358 	}
2359 }
2360 
2361 /**
2362  * i40e_aqc_add_filters - Request firmware to add a set of filters
2363  * @vsi: ptr to the VSI
2364  * @vsi_name: name to display in messages
2365  * @list: the list of filters to send to firmware
2366  * @add_head: Position in the add hlist
2367  * @num_add: the number of filters to add
2368  *
2369  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2370  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2371  * space for more filters.
2372  */
2373 static
2374 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2375 			  struct i40e_aqc_add_macvlan_element_data *list,
2376 			  struct i40e_new_mac_filter *add_head,
2377 			  int num_add)
2378 {
2379 	struct i40e_hw *hw = &vsi->back->hw;
2380 	enum i40e_admin_queue_err aq_status;
2381 	int fcnt;
2382 
2383 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2384 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2385 
2386 	if (fcnt != num_add) {
2387 		if (vsi->type == I40E_VSI_MAIN) {
2388 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2389 			dev_warn(&vsi->back->pdev->dev,
2390 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2391 				 i40e_aq_str(hw, aq_status), vsi_name);
2392 		} else if (vsi->type == I40E_VSI_SRIOV ||
2393 			   vsi->type == I40E_VSI_VMDQ1 ||
2394 			   vsi->type == I40E_VSI_VMDQ2) {
2395 			dev_warn(&vsi->back->pdev->dev,
2396 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2397 				 i40e_aq_str(hw, aq_status), vsi_name,
2398 					     vsi_name);
2399 		} else {
2400 			dev_warn(&vsi->back->pdev->dev,
2401 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2402 				 i40e_aq_str(hw, aq_status), vsi_name,
2403 					     vsi->type);
2404 		}
2405 	}
2406 }
2407 
2408 /**
2409  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2410  * @vsi: pointer to the VSI
2411  * @vsi_name: the VSI name
2412  * @f: filter data
2413  *
2414  * This function sets or clears the promiscuous broadcast flags for VLAN
2415  * filters in order to properly receive broadcast frames. Assumes that only
2416  * broadcast filters are passed.
2417  *
2418  * Returns status indicating success or failure;
2419  **/
2420 static int
2421 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2422 			  struct i40e_mac_filter *f)
2423 {
2424 	bool enable = f->state == I40E_FILTER_NEW;
2425 	struct i40e_hw *hw = &vsi->back->hw;
2426 	int aq_ret;
2427 
2428 	if (f->vlan == I40E_VLAN_ANY) {
2429 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2430 						   vsi->seid,
2431 						   enable,
2432 						   NULL);
2433 	} else {
2434 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2435 							    vsi->seid,
2436 							    enable,
2437 							    f->vlan,
2438 							    NULL);
2439 	}
2440 
2441 	if (aq_ret) {
2442 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2443 		dev_warn(&vsi->back->pdev->dev,
2444 			 "Error %s, forcing overflow promiscuous on %s\n",
2445 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2446 			 vsi_name);
2447 	}
2448 
2449 	return aq_ret;
2450 }
2451 
2452 /**
2453  * i40e_set_promiscuous - set promiscuous mode
2454  * @pf: board private structure
2455  * @promisc: promisc on or off
2456  *
2457  * There are different ways of setting promiscuous mode on a PF depending on
2458  * what state/environment we're in.  This identifies and sets it appropriately.
2459  * Returns 0 on success.
2460  **/
2461 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2462 {
2463 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2464 	struct i40e_hw *hw = &pf->hw;
2465 	int aq_ret;
2466 
2467 	if (vsi->type == I40E_VSI_MAIN &&
2468 	    pf->lan_veb != I40E_NO_VEB &&
2469 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2470 		/* set defport ON for Main VSI instead of true promisc
2471 		 * this way we will get all unicast/multicast and VLAN
2472 		 * promisc behavior but will not get VF or VMDq traffic
2473 		 * replicated on the Main VSI.
2474 		 */
2475 		if (promisc)
2476 			aq_ret = i40e_aq_set_default_vsi(hw,
2477 							 vsi->seid,
2478 							 NULL);
2479 		else
2480 			aq_ret = i40e_aq_clear_default_vsi(hw,
2481 							   vsi->seid,
2482 							   NULL);
2483 		if (aq_ret) {
2484 			dev_info(&pf->pdev->dev,
2485 				 "Set default VSI failed, err %pe, aq_err %s\n",
2486 				 ERR_PTR(aq_ret),
2487 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2488 		}
2489 	} else {
2490 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2491 						  hw,
2492 						  vsi->seid,
2493 						  promisc, NULL,
2494 						  true);
2495 		if (aq_ret) {
2496 			dev_info(&pf->pdev->dev,
2497 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2498 				 ERR_PTR(aq_ret),
2499 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2500 		}
2501 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2502 						  hw,
2503 						  vsi->seid,
2504 						  promisc, NULL);
2505 		if (aq_ret) {
2506 			dev_info(&pf->pdev->dev,
2507 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2508 				 ERR_PTR(aq_ret),
2509 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2510 		}
2511 	}
2512 
2513 	if (!aq_ret)
2514 		pf->cur_promisc = promisc;
2515 
2516 	return aq_ret;
2517 }
2518 
2519 /**
2520  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2521  * @vsi: ptr to the VSI
2522  *
2523  * Push any outstanding VSI filter changes through the AdminQ.
2524  *
2525  * Returns 0 or error value
2526  **/
2527 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2528 {
2529 	struct hlist_head tmp_add_list, tmp_del_list;
2530 	struct i40e_mac_filter *f;
2531 	struct i40e_new_mac_filter *new, *add_head = NULL;
2532 	struct i40e_hw *hw = &vsi->back->hw;
2533 	bool old_overflow, new_overflow;
2534 	unsigned int failed_filters = 0;
2535 	unsigned int vlan_filters = 0;
2536 	char vsi_name[16] = "PF";
2537 	int filter_list_len = 0;
2538 	u32 changed_flags = 0;
2539 	struct hlist_node *h;
2540 	struct i40e_pf *pf;
2541 	int num_add = 0;
2542 	int num_del = 0;
2543 	int aq_ret = 0;
2544 	int retval = 0;
2545 	u16 cmd_flags;
2546 	int list_size;
2547 	int bkt;
2548 
2549 	/* empty array typed pointers, kcalloc later */
2550 	struct i40e_aqc_add_macvlan_element_data *add_list;
2551 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2552 
2553 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2554 		usleep_range(1000, 2000);
2555 	pf = vsi->back;
2556 
2557 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2558 
2559 	if (vsi->netdev) {
2560 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2561 		vsi->current_netdev_flags = vsi->netdev->flags;
2562 	}
2563 
2564 	INIT_HLIST_HEAD(&tmp_add_list);
2565 	INIT_HLIST_HEAD(&tmp_del_list);
2566 
2567 	if (vsi->type == I40E_VSI_SRIOV)
2568 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2569 	else if (vsi->type != I40E_VSI_MAIN)
2570 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2571 
2572 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2573 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2574 
2575 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2576 		/* Create a list of filters to delete. */
2577 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2578 			if (f->state == I40E_FILTER_REMOVE) {
2579 				/* Move the element into temporary del_list */
2580 				hash_del(&f->hlist);
2581 				hlist_add_head(&f->hlist, &tmp_del_list);
2582 
2583 				/* Avoid counting removed filters */
2584 				continue;
2585 			}
2586 			if (f->state == I40E_FILTER_NEW) {
2587 				/* Create a temporary i40e_new_mac_filter */
2588 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2589 				if (!new)
2590 					goto err_no_memory_locked;
2591 
2592 				/* Store pointer to the real filter */
2593 				new->f = f;
2594 				new->state = f->state;
2595 
2596 				/* Add it to the hash list */
2597 				hlist_add_head(&new->hlist, &tmp_add_list);
2598 			}
2599 
2600 			/* Count the number of active (current and new) VLAN
2601 			 * filters we have now. Does not count filters which
2602 			 * are marked for deletion.
2603 			 */
2604 			if (f->vlan > 0)
2605 				vlan_filters++;
2606 		}
2607 
2608 		if (vsi->type != I40E_VSI_SRIOV)
2609 			retval = i40e_correct_mac_vlan_filters
2610 				(vsi, &tmp_add_list, &tmp_del_list,
2611 				 vlan_filters);
2612 		else if (pf->vf)
2613 			retval = i40e_correct_vf_mac_vlan_filters
2614 				(vsi, &tmp_add_list, &tmp_del_list,
2615 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2616 
2617 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2618 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2619 
2620 		if (retval)
2621 			goto err_no_memory_locked;
2622 
2623 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2624 	}
2625 
2626 	/* Now process 'del_list' outside the lock */
2627 	if (!hlist_empty(&tmp_del_list)) {
2628 		filter_list_len = hw->aq.asq_buf_size /
2629 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2630 		list_size = filter_list_len *
2631 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2632 		del_list = kzalloc(list_size, GFP_ATOMIC);
2633 		if (!del_list)
2634 			goto err_no_memory;
2635 
2636 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2637 			cmd_flags = 0;
2638 
2639 			/* handle broadcast filters by updating the broadcast
2640 			 * promiscuous flag and release filter list.
2641 			 */
2642 			if (is_broadcast_ether_addr(f->macaddr)) {
2643 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2644 
2645 				hlist_del(&f->hlist);
2646 				kfree(f);
2647 				continue;
2648 			}
2649 
2650 			/* add to delete list */
2651 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2652 			if (f->vlan == I40E_VLAN_ANY) {
2653 				del_list[num_del].vlan_tag = 0;
2654 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2655 			} else {
2656 				del_list[num_del].vlan_tag =
2657 					cpu_to_le16((u16)(f->vlan));
2658 			}
2659 
2660 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2661 			del_list[num_del].flags = cmd_flags;
2662 			num_del++;
2663 
2664 			/* flush a full buffer */
2665 			if (num_del == filter_list_len) {
2666 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2667 						     num_del, &retval);
2668 				memset(del_list, 0, list_size);
2669 				num_del = 0;
2670 			}
2671 			/* Release memory for MAC filter entries which were
2672 			 * synced up with HW.
2673 			 */
2674 			hlist_del(&f->hlist);
2675 			kfree(f);
2676 		}
2677 
2678 		if (num_del) {
2679 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2680 					     num_del, &retval);
2681 		}
2682 
2683 		kfree(del_list);
2684 		del_list = NULL;
2685 	}
2686 
2687 	if (!hlist_empty(&tmp_add_list)) {
2688 		/* Do all the adds now. */
2689 		filter_list_len = hw->aq.asq_buf_size /
2690 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2691 		list_size = filter_list_len *
2692 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2693 		add_list = kzalloc(list_size, GFP_ATOMIC);
2694 		if (!add_list)
2695 			goto err_no_memory;
2696 
2697 		num_add = 0;
2698 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2699 			/* handle broadcast filters by updating the broadcast
2700 			 * promiscuous flag instead of adding a MAC filter.
2701 			 */
2702 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2703 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2704 							      new->f))
2705 					new->state = I40E_FILTER_FAILED;
2706 				else
2707 					new->state = I40E_FILTER_ACTIVE;
2708 				continue;
2709 			}
2710 
2711 			/* add to add array */
2712 			if (num_add == 0)
2713 				add_head = new;
2714 			cmd_flags = 0;
2715 			ether_addr_copy(add_list[num_add].mac_addr,
2716 					new->f->macaddr);
2717 			if (new->f->vlan == I40E_VLAN_ANY) {
2718 				add_list[num_add].vlan_tag = 0;
2719 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2720 			} else {
2721 				add_list[num_add].vlan_tag =
2722 					cpu_to_le16((u16)(new->f->vlan));
2723 			}
2724 			add_list[num_add].queue_number = 0;
2725 			/* set invalid match method for later detection */
2726 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2727 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2728 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2729 			num_add++;
2730 
2731 			/* flush a full buffer */
2732 			if (num_add == filter_list_len) {
2733 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2734 						     add_head, num_add);
2735 				memset(add_list, 0, list_size);
2736 				num_add = 0;
2737 			}
2738 		}
2739 		if (num_add) {
2740 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2741 					     num_add);
2742 		}
2743 		/* Now move all of the filters from the temp add list back to
2744 		 * the VSI's list.
2745 		 */
2746 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2747 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2748 			/* Only update the state if we're still NEW */
2749 			if (new->f->state == I40E_FILTER_NEW)
2750 				new->f->state = new->state;
2751 			hlist_del(&new->hlist);
2752 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2753 			kfree(new);
2754 		}
2755 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2756 		kfree(add_list);
2757 		add_list = NULL;
2758 	}
2759 
2760 	/* Determine the number of active and failed filters. */
2761 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2762 	vsi->active_filters = 0;
2763 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2764 		if (f->state == I40E_FILTER_ACTIVE)
2765 			vsi->active_filters++;
2766 		else if (f->state == I40E_FILTER_FAILED)
2767 			failed_filters++;
2768 	}
2769 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2770 
2771 	/* Check if we are able to exit overflow promiscuous mode. We can
2772 	 * safely exit if we didn't just enter, we no longer have any failed
2773 	 * filters, and we have reduced filters below the threshold value.
2774 	 */
2775 	if (old_overflow && !failed_filters &&
2776 	    vsi->active_filters < vsi->promisc_threshold) {
2777 		dev_info(&pf->pdev->dev,
2778 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2779 			 vsi_name);
2780 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2781 		vsi->promisc_threshold = 0;
2782 	}
2783 
2784 	/* if the VF is not trusted do not do promisc */
2785 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2786 	    !pf->vf[vsi->vf_id].trusted) {
2787 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2788 		goto out;
2789 	}
2790 
2791 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2792 
2793 	/* If we are entering overflow promiscuous, we need to calculate a new
2794 	 * threshold for when we are safe to exit
2795 	 */
2796 	if (!old_overflow && new_overflow)
2797 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2798 
2799 	/* check for changes in promiscuous modes */
2800 	if (changed_flags & IFF_ALLMULTI) {
2801 		bool cur_multipromisc;
2802 
2803 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2804 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2805 							       vsi->seid,
2806 							       cur_multipromisc,
2807 							       NULL);
2808 		if (aq_ret) {
2809 			retval = i40e_aq_rc_to_posix(aq_ret,
2810 						     hw->aq.asq_last_status);
2811 			dev_info(&pf->pdev->dev,
2812 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2813 				 vsi_name,
2814 				 ERR_PTR(aq_ret),
2815 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2816 		} else {
2817 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2818 				 cur_multipromisc ? "entering" : "leaving");
2819 		}
2820 	}
2821 
2822 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2823 		bool cur_promisc;
2824 
2825 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2826 			       new_overflow);
2827 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2828 		if (aq_ret) {
2829 			retval = i40e_aq_rc_to_posix(aq_ret,
2830 						     hw->aq.asq_last_status);
2831 			dev_info(&pf->pdev->dev,
2832 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2833 				 cur_promisc ? "on" : "off",
2834 				 vsi_name,
2835 				 ERR_PTR(aq_ret),
2836 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2837 		}
2838 	}
2839 out:
2840 	/* if something went wrong then set the changed flag so we try again */
2841 	if (retval)
2842 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2843 
2844 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2845 	return retval;
2846 
2847 err_no_memory:
2848 	/* Restore elements on the temporary add and delete lists */
2849 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2850 err_no_memory_locked:
2851 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2852 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2853 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2854 
2855 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2856 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2857 	return -ENOMEM;
2858 }
2859 
2860 /**
2861  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2862  * @pf: board private structure
2863  **/
2864 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2865 {
2866 	int v;
2867 
2868 	if (!pf)
2869 		return;
2870 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2871 		return;
2872 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2873 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2874 		return;
2875 	}
2876 
2877 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2878 		if (pf->vsi[v] &&
2879 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2880 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2881 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2882 
2883 			if (ret) {
2884 				/* come back and try again later */
2885 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2886 					pf->state);
2887 				break;
2888 			}
2889 		}
2890 	}
2891 }
2892 
2893 /**
2894  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2895  *
2896  * @vsi: VSI to calculate rx_buf_len from
2897  */
2898 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2899 {
2900 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2901 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2902 
2903 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2904 }
2905 
2906 /**
2907  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2908  * @vsi: the vsi
2909  * @xdp_prog: XDP program
2910  **/
2911 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2912 				   struct bpf_prog *xdp_prog)
2913 {
2914 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2915 	u16 chain_len;
2916 
2917 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2918 		chain_len = 1;
2919 	else
2920 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2921 
2922 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2923 }
2924 
2925 /**
2926  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2927  * @netdev: network interface device structure
2928  * @new_mtu: new value for maximum frame size
2929  *
2930  * Returns 0 on success, negative on failure
2931  **/
2932 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2933 {
2934 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2935 	struct i40e_vsi *vsi = np->vsi;
2936 	struct i40e_pf *pf = vsi->back;
2937 	int frame_size;
2938 
2939 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2940 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2941 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2942 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2943 		return -EINVAL;
2944 	}
2945 
2946 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2947 		   netdev->mtu, new_mtu);
2948 	netdev->mtu = new_mtu;
2949 	if (netif_running(netdev))
2950 		i40e_vsi_reinit_locked(vsi);
2951 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2952 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2953 	return 0;
2954 }
2955 
2956 /**
2957  * i40e_ioctl - Access the hwtstamp interface
2958  * @netdev: network interface device structure
2959  * @ifr: interface request data
2960  * @cmd: ioctl command
2961  **/
2962 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2963 {
2964 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2965 	struct i40e_pf *pf = np->vsi->back;
2966 
2967 	switch (cmd) {
2968 	case SIOCGHWTSTAMP:
2969 		return i40e_ptp_get_ts_config(pf, ifr);
2970 	case SIOCSHWTSTAMP:
2971 		return i40e_ptp_set_ts_config(pf, ifr);
2972 	default:
2973 		return -EOPNOTSUPP;
2974 	}
2975 }
2976 
2977 /**
2978  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2979  * @vsi: the vsi being adjusted
2980  **/
2981 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2982 {
2983 	struct i40e_vsi_context ctxt;
2984 	int ret;
2985 
2986 	/* Don't modify stripping options if a port VLAN is active */
2987 	if (vsi->info.pvid)
2988 		return;
2989 
2990 	if ((vsi->info.valid_sections &
2991 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2992 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2993 		return;  /* already enabled */
2994 
2995 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2996 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2997 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2998 
2999 	ctxt.seid = vsi->seid;
3000 	ctxt.info = vsi->info;
3001 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3002 	if (ret) {
3003 		dev_info(&vsi->back->pdev->dev,
3004 			 "update vlan stripping failed, err %pe aq_err %s\n",
3005 			 ERR_PTR(ret),
3006 			 i40e_aq_str(&vsi->back->hw,
3007 				     vsi->back->hw.aq.asq_last_status));
3008 	}
3009 }
3010 
3011 /**
3012  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3013  * @vsi: the vsi being adjusted
3014  **/
3015 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3016 {
3017 	struct i40e_vsi_context ctxt;
3018 	int ret;
3019 
3020 	/* Don't modify stripping options if a port VLAN is active */
3021 	if (vsi->info.pvid)
3022 		return;
3023 
3024 	if ((vsi->info.valid_sections &
3025 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3026 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3027 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3028 		return;  /* already disabled */
3029 
3030 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3031 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3032 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3033 
3034 	ctxt.seid = vsi->seid;
3035 	ctxt.info = vsi->info;
3036 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3037 	if (ret) {
3038 		dev_info(&vsi->back->pdev->dev,
3039 			 "update vlan stripping failed, err %pe aq_err %s\n",
3040 			 ERR_PTR(ret),
3041 			 i40e_aq_str(&vsi->back->hw,
3042 				     vsi->back->hw.aq.asq_last_status));
3043 	}
3044 }
3045 
3046 /**
3047  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3048  * @vsi: the vsi being configured
3049  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3050  *
3051  * This is a helper function for adding a new MAC/VLAN filter with the
3052  * specified VLAN for each existing MAC address already in the hash table.
3053  * This function does *not* perform any accounting to update filters based on
3054  * VLAN mode.
3055  *
3056  * NOTE: this function expects to be called while under the
3057  * mac_filter_hash_lock
3058  **/
3059 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3060 {
3061 	struct i40e_mac_filter *f, *add_f;
3062 	struct hlist_node *h;
3063 	int bkt;
3064 
3065 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3066 		/* If we're asked to add a filter that has been marked for
3067 		 * removal, it is safe to simply restore it to active state.
3068 		 * __i40e_del_filter will have simply deleted any filters which
3069 		 * were previously marked NEW or FAILED, so if it is currently
3070 		 * marked REMOVE it must have previously been ACTIVE. Since we
3071 		 * haven't yet run the sync filters task, just restore this
3072 		 * filter to the ACTIVE state so that the sync task leaves it
3073 		 * in place.
3074 		 */
3075 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3076 			f->state = I40E_FILTER_ACTIVE;
3077 			continue;
3078 		} else if (f->state == I40E_FILTER_REMOVE) {
3079 			continue;
3080 		}
3081 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3082 		if (!add_f) {
3083 			dev_info(&vsi->back->pdev->dev,
3084 				 "Could not add vlan filter %d for %pM\n",
3085 				 vid, f->macaddr);
3086 			return -ENOMEM;
3087 		}
3088 	}
3089 
3090 	return 0;
3091 }
3092 
3093 /**
3094  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3095  * @vsi: the VSI being configured
3096  * @vid: VLAN id to be added
3097  **/
3098 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3099 {
3100 	int err;
3101 
3102 	if (vsi->info.pvid)
3103 		return -EINVAL;
3104 
3105 	/* The network stack will attempt to add VID=0, with the intention to
3106 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3107 	 * these packets by default when configured to receive untagged
3108 	 * packets, so we don't need to add a filter for this case.
3109 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3110 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3111 	 * Thus, we do not want to actually add a filter for VID=0
3112 	 */
3113 	if (!vid)
3114 		return 0;
3115 
3116 	/* Locked once because all functions invoked below iterates list*/
3117 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3118 	err = i40e_add_vlan_all_mac(vsi, vid);
3119 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3120 	if (err)
3121 		return err;
3122 
3123 	/* schedule our worker thread which will take care of
3124 	 * applying the new filter changes
3125 	 */
3126 	i40e_service_event_schedule(vsi->back);
3127 	return 0;
3128 }
3129 
3130 /**
3131  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3132  * @vsi: the vsi being configured
3133  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3134  *
3135  * This function should be used to remove all VLAN filters which match the
3136  * given VID. It does not schedule the service event and does not take the
3137  * mac_filter_hash_lock so it may be combined with other operations under
3138  * a single invocation of the mac_filter_hash_lock.
3139  *
3140  * NOTE: this function expects to be called while under the
3141  * mac_filter_hash_lock
3142  */
3143 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3144 {
3145 	struct i40e_mac_filter *f;
3146 	struct hlist_node *h;
3147 	int bkt;
3148 
3149 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3150 		if (f->vlan == vid)
3151 			__i40e_del_filter(vsi, f);
3152 	}
3153 }
3154 
3155 /**
3156  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3157  * @vsi: the VSI being configured
3158  * @vid: VLAN id to be removed
3159  **/
3160 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3161 {
3162 	if (!vid || vsi->info.pvid)
3163 		return;
3164 
3165 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3166 	i40e_rm_vlan_all_mac(vsi, vid);
3167 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3168 
3169 	/* schedule our worker thread which will take care of
3170 	 * applying the new filter changes
3171 	 */
3172 	i40e_service_event_schedule(vsi->back);
3173 }
3174 
3175 /**
3176  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3177  * @netdev: network interface to be adjusted
3178  * @proto: unused protocol value
3179  * @vid: vlan id to be added
3180  *
3181  * net_device_ops implementation for adding vlan ids
3182  **/
3183 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3184 				__always_unused __be16 proto, u16 vid)
3185 {
3186 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3187 	struct i40e_vsi *vsi = np->vsi;
3188 	int ret = 0;
3189 
3190 	if (vid >= VLAN_N_VID)
3191 		return -EINVAL;
3192 
3193 	ret = i40e_vsi_add_vlan(vsi, vid);
3194 	if (!ret)
3195 		set_bit(vid, vsi->active_vlans);
3196 
3197 	return ret;
3198 }
3199 
3200 /**
3201  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3202  * @netdev: network interface to be adjusted
3203  * @proto: unused protocol value
3204  * @vid: vlan id to be added
3205  **/
3206 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3207 				    __always_unused __be16 proto, u16 vid)
3208 {
3209 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3210 	struct i40e_vsi *vsi = np->vsi;
3211 
3212 	if (vid >= VLAN_N_VID)
3213 		return;
3214 	set_bit(vid, vsi->active_vlans);
3215 }
3216 
3217 /**
3218  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3219  * @netdev: network interface to be adjusted
3220  * @proto: unused protocol value
3221  * @vid: vlan id to be removed
3222  *
3223  * net_device_ops implementation for removing vlan ids
3224  **/
3225 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3226 				 __always_unused __be16 proto, u16 vid)
3227 {
3228 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3229 	struct i40e_vsi *vsi = np->vsi;
3230 
3231 	/* return code is ignored as there is nothing a user
3232 	 * can do about failure to remove and a log message was
3233 	 * already printed from the other function
3234 	 */
3235 	i40e_vsi_kill_vlan(vsi, vid);
3236 
3237 	clear_bit(vid, vsi->active_vlans);
3238 
3239 	return 0;
3240 }
3241 
3242 /**
3243  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3244  * @vsi: the vsi being brought back up
3245  **/
3246 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3247 {
3248 	u16 vid;
3249 
3250 	if (!vsi->netdev)
3251 		return;
3252 
3253 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3254 		i40e_vlan_stripping_enable(vsi);
3255 	else
3256 		i40e_vlan_stripping_disable(vsi);
3257 
3258 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3259 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3260 					vid);
3261 }
3262 
3263 /**
3264  * i40e_vsi_add_pvid - Add pvid for the VSI
3265  * @vsi: the vsi being adjusted
3266  * @vid: the vlan id to set as a PVID
3267  **/
3268 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3269 {
3270 	struct i40e_vsi_context ctxt;
3271 	int ret;
3272 
3273 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3274 	vsi->info.pvid = cpu_to_le16(vid);
3275 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3276 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3277 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3278 
3279 	ctxt.seid = vsi->seid;
3280 	ctxt.info = vsi->info;
3281 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3282 	if (ret) {
3283 		dev_info(&vsi->back->pdev->dev,
3284 			 "add pvid failed, err %pe aq_err %s\n",
3285 			 ERR_PTR(ret),
3286 			 i40e_aq_str(&vsi->back->hw,
3287 				     vsi->back->hw.aq.asq_last_status));
3288 		return -ENOENT;
3289 	}
3290 
3291 	return 0;
3292 }
3293 
3294 /**
3295  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3296  * @vsi: the vsi being adjusted
3297  *
3298  * Just use the vlan_rx_register() service to put it back to normal
3299  **/
3300 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3301 {
3302 	vsi->info.pvid = 0;
3303 
3304 	i40e_vlan_stripping_disable(vsi);
3305 }
3306 
3307 /**
3308  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3309  * @vsi: ptr to the VSI
3310  *
3311  * If this function returns with an error, then it's possible one or
3312  * more of the rings is populated (while the rest are not).  It is the
3313  * callers duty to clean those orphaned rings.
3314  *
3315  * Return 0 on success, negative on failure
3316  **/
3317 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3318 {
3319 	int i, err = 0;
3320 
3321 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3322 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3323 
3324 	if (!i40e_enabled_xdp_vsi(vsi))
3325 		return err;
3326 
3327 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3328 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3329 
3330 	return err;
3331 }
3332 
3333 /**
3334  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3335  * @vsi: ptr to the VSI
3336  *
3337  * Free VSI's transmit software resources
3338  **/
3339 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3340 {
3341 	int i;
3342 
3343 	if (vsi->tx_rings) {
3344 		for (i = 0; i < vsi->num_queue_pairs; i++)
3345 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3346 				i40e_free_tx_resources(vsi->tx_rings[i]);
3347 	}
3348 
3349 	if (vsi->xdp_rings) {
3350 		for (i = 0; i < vsi->num_queue_pairs; i++)
3351 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3352 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3353 	}
3354 }
3355 
3356 /**
3357  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3358  * @vsi: ptr to the VSI
3359  *
3360  * If this function returns with an error, then it's possible one or
3361  * more of the rings is populated (while the rest are not).  It is the
3362  * callers duty to clean those orphaned rings.
3363  *
3364  * Return 0 on success, negative on failure
3365  **/
3366 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3367 {
3368 	int i, err = 0;
3369 
3370 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3371 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3372 	return err;
3373 }
3374 
3375 /**
3376  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3377  * @vsi: ptr to the VSI
3378  *
3379  * Free all receive software resources
3380  **/
3381 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3382 {
3383 	int i;
3384 
3385 	if (!vsi->rx_rings)
3386 		return;
3387 
3388 	for (i = 0; i < vsi->num_queue_pairs; i++)
3389 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3390 			i40e_free_rx_resources(vsi->rx_rings[i]);
3391 }
3392 
3393 /**
3394  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3395  * @ring: The Tx ring to configure
3396  *
3397  * This enables/disables XPS for a given Tx descriptor ring
3398  * based on the TCs enabled for the VSI that ring belongs to.
3399  **/
3400 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3401 {
3402 	int cpu;
3403 
3404 	if (!ring->q_vector || !ring->netdev || ring->ch)
3405 		return;
3406 
3407 	/* We only initialize XPS once, so as not to overwrite user settings */
3408 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3409 		return;
3410 
3411 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3412 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3413 			    ring->queue_index);
3414 }
3415 
3416 /**
3417  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3418  * @ring: The Tx or Rx ring
3419  *
3420  * Returns the AF_XDP buffer pool or NULL.
3421  **/
3422 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3423 {
3424 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3425 	int qid = ring->queue_index;
3426 
3427 	if (ring_is_xdp(ring))
3428 		qid -= ring->vsi->alloc_queue_pairs;
3429 
3430 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3431 		return NULL;
3432 
3433 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3434 }
3435 
3436 /**
3437  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3438  * @ring: The Tx ring to configure
3439  *
3440  * Configure the Tx descriptor ring in the HMC context.
3441  **/
3442 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3443 {
3444 	struct i40e_vsi *vsi = ring->vsi;
3445 	u16 pf_q = vsi->base_queue + ring->queue_index;
3446 	struct i40e_hw *hw = &vsi->back->hw;
3447 	struct i40e_hmc_obj_txq tx_ctx;
3448 	u32 qtx_ctl = 0;
3449 	int err = 0;
3450 
3451 	if (ring_is_xdp(ring))
3452 		ring->xsk_pool = i40e_xsk_pool(ring);
3453 
3454 	/* some ATR related tx ring init */
3455 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3456 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3457 		ring->atr_count = 0;
3458 	} else {
3459 		ring->atr_sample_rate = 0;
3460 	}
3461 
3462 	/* configure XPS */
3463 	i40e_config_xps_tx_ring(ring);
3464 
3465 	/* clear the context structure first */
3466 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3467 
3468 	tx_ctx.new_context = 1;
3469 	tx_ctx.base = (ring->dma / 128);
3470 	tx_ctx.qlen = ring->count;
3471 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3472 					       I40E_FLAG_FD_ATR_ENABLED));
3473 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3474 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3475 	if (vsi->type != I40E_VSI_FDIR)
3476 		tx_ctx.head_wb_ena = 1;
3477 	tx_ctx.head_wb_addr = ring->dma +
3478 			      (ring->count * sizeof(struct i40e_tx_desc));
3479 
3480 	/* As part of VSI creation/update, FW allocates certain
3481 	 * Tx arbitration queue sets for each TC enabled for
3482 	 * the VSI. The FW returns the handles to these queue
3483 	 * sets as part of the response buffer to Add VSI,
3484 	 * Update VSI, etc. AQ commands. It is expected that
3485 	 * these queue set handles be associated with the Tx
3486 	 * queues by the driver as part of the TX queue context
3487 	 * initialization. This has to be done regardless of
3488 	 * DCB as by default everything is mapped to TC0.
3489 	 */
3490 
3491 	if (ring->ch)
3492 		tx_ctx.rdylist =
3493 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3494 
3495 	else
3496 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3497 
3498 	tx_ctx.rdylist_act = 0;
3499 
3500 	/* clear the context in the HMC */
3501 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3502 	if (err) {
3503 		dev_info(&vsi->back->pdev->dev,
3504 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3505 			 ring->queue_index, pf_q, err);
3506 		return -ENOMEM;
3507 	}
3508 
3509 	/* set the context in the HMC */
3510 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3511 	if (err) {
3512 		dev_info(&vsi->back->pdev->dev,
3513 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3514 			 ring->queue_index, pf_q, err);
3515 		return -ENOMEM;
3516 	}
3517 
3518 	/* Now associate this queue with this PCI function */
3519 	if (ring->ch) {
3520 		if (ring->ch->type == I40E_VSI_VMDQ2)
3521 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3522 		else
3523 			return -EINVAL;
3524 
3525 		qtx_ctl |= (ring->ch->vsi_number <<
3526 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3527 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3528 	} else {
3529 		if (vsi->type == I40E_VSI_VMDQ2) {
3530 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3531 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3532 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3533 		} else {
3534 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3535 		}
3536 	}
3537 
3538 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3539 		    I40E_QTX_CTL_PF_INDX_MASK);
3540 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3541 	i40e_flush(hw);
3542 
3543 	/* cache tail off for easier writes later */
3544 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3545 
3546 	return 0;
3547 }
3548 
3549 /**
3550  * i40e_rx_offset - Return expected offset into page to access data
3551  * @rx_ring: Ring we are requesting offset of
3552  *
3553  * Returns the offset value for ring into the data buffer.
3554  */
3555 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3556 {
3557 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3558 }
3559 
3560 /**
3561  * i40e_configure_rx_ring - Configure a receive ring context
3562  * @ring: The Rx ring to configure
3563  *
3564  * Configure the Rx descriptor ring in the HMC context.
3565  **/
3566 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3567 {
3568 	struct i40e_vsi *vsi = ring->vsi;
3569 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3570 	u16 pf_q = vsi->base_queue + ring->queue_index;
3571 	struct i40e_hw *hw = &vsi->back->hw;
3572 	struct i40e_hmc_obj_rxq rx_ctx;
3573 	int err = 0;
3574 	bool ok;
3575 	int ret;
3576 
3577 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3578 
3579 	/* clear the context structure first */
3580 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3581 
3582 	if (ring->vsi->type == I40E_VSI_MAIN)
3583 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3584 
3585 	ring->xsk_pool = i40e_xsk_pool(ring);
3586 	if (ring->xsk_pool) {
3587 		ring->rx_buf_len =
3588 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3589 		/* For AF_XDP ZC, we disallow packets to span on
3590 		 * multiple buffers, thus letting us skip that
3591 		 * handling in the fast-path.
3592 		 */
3593 		chain_len = 1;
3594 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3595 						 MEM_TYPE_XSK_BUFF_POOL,
3596 						 NULL);
3597 		if (ret)
3598 			return ret;
3599 		dev_info(&vsi->back->pdev->dev,
3600 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3601 			 ring->queue_index);
3602 
3603 	} else {
3604 		ring->rx_buf_len = vsi->rx_buf_len;
3605 		if (ring->vsi->type == I40E_VSI_MAIN) {
3606 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3607 							 MEM_TYPE_PAGE_SHARED,
3608 							 NULL);
3609 			if (ret)
3610 				return ret;
3611 		}
3612 	}
3613 
3614 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3615 
3616 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3617 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3618 
3619 	rx_ctx.base = (ring->dma / 128);
3620 	rx_ctx.qlen = ring->count;
3621 
3622 	/* use 16 byte descriptors */
3623 	rx_ctx.dsize = 0;
3624 
3625 	/* descriptor type is always zero
3626 	 * rx_ctx.dtype = 0;
3627 	 */
3628 	rx_ctx.hsplit_0 = 0;
3629 
3630 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3631 	if (hw->revision_id == 0)
3632 		rx_ctx.lrxqthresh = 0;
3633 	else
3634 		rx_ctx.lrxqthresh = 1;
3635 	rx_ctx.crcstrip = 1;
3636 	rx_ctx.l2tsel = 1;
3637 	/* this controls whether VLAN is stripped from inner headers */
3638 	rx_ctx.showiv = 0;
3639 	/* set the prefena field to 1 because the manual says to */
3640 	rx_ctx.prefena = 1;
3641 
3642 	/* clear the context in the HMC */
3643 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3644 	if (err) {
3645 		dev_info(&vsi->back->pdev->dev,
3646 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3647 			 ring->queue_index, pf_q, err);
3648 		return -ENOMEM;
3649 	}
3650 
3651 	/* set the context in the HMC */
3652 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3653 	if (err) {
3654 		dev_info(&vsi->back->pdev->dev,
3655 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3656 			 ring->queue_index, pf_q, err);
3657 		return -ENOMEM;
3658 	}
3659 
3660 	/* configure Rx buffer alignment */
3661 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3662 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3663 			dev_info(&vsi->back->pdev->dev,
3664 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3665 			return -EOPNOTSUPP;
3666 		}
3667 		clear_ring_build_skb_enabled(ring);
3668 	} else {
3669 		set_ring_build_skb_enabled(ring);
3670 	}
3671 
3672 	ring->rx_offset = i40e_rx_offset(ring);
3673 
3674 	/* cache tail for quicker writes, and clear the reg before use */
3675 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3676 	writel(0, ring->tail);
3677 
3678 	if (ring->xsk_pool) {
3679 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3680 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3681 	} else {
3682 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3683 	}
3684 	if (!ok) {
3685 		/* Log this in case the user has forgotten to give the kernel
3686 		 * any buffers, even later in the application.
3687 		 */
3688 		dev_info(&vsi->back->pdev->dev,
3689 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3690 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3691 			 ring->queue_index, pf_q);
3692 	}
3693 
3694 	return 0;
3695 }
3696 
3697 /**
3698  * i40e_vsi_configure_tx - Configure the VSI for Tx
3699  * @vsi: VSI structure describing this set of rings and resources
3700  *
3701  * Configure the Tx VSI for operation.
3702  **/
3703 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3704 {
3705 	int err = 0;
3706 	u16 i;
3707 
3708 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3709 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3710 
3711 	if (err || !i40e_enabled_xdp_vsi(vsi))
3712 		return err;
3713 
3714 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3715 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3716 
3717 	return err;
3718 }
3719 
3720 /**
3721  * i40e_vsi_configure_rx - Configure the VSI for Rx
3722  * @vsi: the VSI being configured
3723  *
3724  * Configure the Rx VSI for operation.
3725  **/
3726 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3727 {
3728 	int err = 0;
3729 	u16 i;
3730 
3731 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3732 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3733 
3734 #if (PAGE_SIZE < 8192)
3735 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3736 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3737 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3738 		vsi->max_frame = vsi->rx_buf_len;
3739 	}
3740 #endif
3741 
3742 	/* set up individual rings */
3743 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3744 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3745 
3746 	return err;
3747 }
3748 
3749 /**
3750  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3751  * @vsi: ptr to the VSI
3752  **/
3753 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3754 {
3755 	struct i40e_ring *tx_ring, *rx_ring;
3756 	u16 qoffset, qcount;
3757 	int i, n;
3758 
3759 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3760 		/* Reset the TC information */
3761 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3762 			rx_ring = vsi->rx_rings[i];
3763 			tx_ring = vsi->tx_rings[i];
3764 			rx_ring->dcb_tc = 0;
3765 			tx_ring->dcb_tc = 0;
3766 		}
3767 		return;
3768 	}
3769 
3770 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3771 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3772 			continue;
3773 
3774 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3775 		qcount = vsi->tc_config.tc_info[n].qcount;
3776 		for (i = qoffset; i < (qoffset + qcount); i++) {
3777 			rx_ring = vsi->rx_rings[i];
3778 			tx_ring = vsi->tx_rings[i];
3779 			rx_ring->dcb_tc = n;
3780 			tx_ring->dcb_tc = n;
3781 		}
3782 	}
3783 }
3784 
3785 /**
3786  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3787  * @vsi: ptr to the VSI
3788  **/
3789 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3790 {
3791 	if (vsi->netdev)
3792 		i40e_set_rx_mode(vsi->netdev);
3793 }
3794 
3795 /**
3796  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3797  * @pf: Pointer to the targeted PF
3798  *
3799  * Set all flow director counters to 0.
3800  */
3801 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3802 {
3803 	pf->fd_tcp4_filter_cnt = 0;
3804 	pf->fd_udp4_filter_cnt = 0;
3805 	pf->fd_sctp4_filter_cnt = 0;
3806 	pf->fd_ip4_filter_cnt = 0;
3807 	pf->fd_tcp6_filter_cnt = 0;
3808 	pf->fd_udp6_filter_cnt = 0;
3809 	pf->fd_sctp6_filter_cnt = 0;
3810 	pf->fd_ip6_filter_cnt = 0;
3811 }
3812 
3813 /**
3814  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3815  * @vsi: Pointer to the targeted VSI
3816  *
3817  * This function replays the hlist on the hw where all the SB Flow Director
3818  * filters were saved.
3819  **/
3820 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3821 {
3822 	struct i40e_fdir_filter *filter;
3823 	struct i40e_pf *pf = vsi->back;
3824 	struct hlist_node *node;
3825 
3826 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3827 		return;
3828 
3829 	/* Reset FDir counters as we're replaying all existing filters */
3830 	i40e_reset_fdir_filter_cnt(pf);
3831 
3832 	hlist_for_each_entry_safe(filter, node,
3833 				  &pf->fdir_filter_list, fdir_node) {
3834 		i40e_add_del_fdir(vsi, filter, true);
3835 	}
3836 }
3837 
3838 /**
3839  * i40e_vsi_configure - Set up the VSI for action
3840  * @vsi: the VSI being configured
3841  **/
3842 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3843 {
3844 	int err;
3845 
3846 	i40e_set_vsi_rx_mode(vsi);
3847 	i40e_restore_vlan(vsi);
3848 	i40e_vsi_config_dcb_rings(vsi);
3849 	err = i40e_vsi_configure_tx(vsi);
3850 	if (!err)
3851 		err = i40e_vsi_configure_rx(vsi);
3852 
3853 	return err;
3854 }
3855 
3856 /**
3857  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3858  * @vsi: the VSI being configured
3859  **/
3860 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3861 {
3862 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3863 	struct i40e_pf *pf = vsi->back;
3864 	struct i40e_hw *hw = &pf->hw;
3865 	u16 vector;
3866 	int i, q;
3867 	u32 qp;
3868 
3869 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3870 	 * and PFINT_LNKLSTn registers, e.g.:
3871 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3872 	 */
3873 	qp = vsi->base_queue;
3874 	vector = vsi->base_vector;
3875 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3876 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3877 
3878 		q_vector->rx.next_update = jiffies + 1;
3879 		q_vector->rx.target_itr =
3880 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3881 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3882 		     q_vector->rx.target_itr >> 1);
3883 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3884 
3885 		q_vector->tx.next_update = jiffies + 1;
3886 		q_vector->tx.target_itr =
3887 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3888 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3889 		     q_vector->tx.target_itr >> 1);
3890 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3891 
3892 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3893 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3894 
3895 		/* begin of linked list for RX queue assigned to this vector */
3896 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3897 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3898 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3899 			u32 val;
3900 
3901 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3902 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3903 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3904 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3905 			      (I40E_QUEUE_TYPE_TX <<
3906 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3907 
3908 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3909 
3910 			if (has_xdp) {
3911 				/* TX queue with next queue set to TX */
3912 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3913 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3914 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3915 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3916 				      (I40E_QUEUE_TYPE_TX <<
3917 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3918 
3919 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3920 			}
3921 			/* TX queue with next RX or end of linked list */
3922 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3923 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3924 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3925 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3926 			      (I40E_QUEUE_TYPE_RX <<
3927 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3928 
3929 			/* Terminate the linked list */
3930 			if (q == (q_vector->num_ringpairs - 1))
3931 				val |= (I40E_QUEUE_END_OF_LIST <<
3932 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3933 
3934 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3935 			qp++;
3936 		}
3937 	}
3938 
3939 	i40e_flush(hw);
3940 }
3941 
3942 /**
3943  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3944  * @pf: pointer to private device data structure
3945  **/
3946 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3947 {
3948 	struct i40e_hw *hw = &pf->hw;
3949 	u32 val;
3950 
3951 	/* clear things first */
3952 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3953 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3954 
3955 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3956 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3957 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3958 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3959 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3960 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3961 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3962 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3963 
3964 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3965 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3966 
3967 	if (pf->flags & I40E_FLAG_PTP)
3968 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3969 
3970 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3971 
3972 	/* SW_ITR_IDX = 0, but don't change INTENA */
3973 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3974 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3975 
3976 	/* OTHER_ITR_IDX = 0 */
3977 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3978 }
3979 
3980 /**
3981  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3982  * @vsi: the VSI being configured
3983  **/
3984 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3985 {
3986 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3987 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3988 	struct i40e_pf *pf = vsi->back;
3989 	struct i40e_hw *hw = &pf->hw;
3990 
3991 	/* set the ITR configuration */
3992 	q_vector->rx.next_update = jiffies + 1;
3993 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3994 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3995 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3996 	q_vector->tx.next_update = jiffies + 1;
3997 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3998 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3999 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4000 
4001 	i40e_enable_misc_int_causes(pf);
4002 
4003 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4004 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4005 
4006 	/* Associate the queue pair to the vector and enable the queue
4007 	 * interrupt RX queue in linked list with next queue set to TX
4008 	 */
4009 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4010 
4011 	if (i40e_enabled_xdp_vsi(vsi)) {
4012 		/* TX queue in linked list with next queue set to TX */
4013 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4014 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4015 	}
4016 
4017 	/* last TX queue so the next RX queue doesn't matter */
4018 	wr32(hw, I40E_QINT_TQCTL(0),
4019 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4020 	i40e_flush(hw);
4021 }
4022 
4023 /**
4024  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4025  * @pf: board private structure
4026  **/
4027 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4028 {
4029 	struct i40e_hw *hw = &pf->hw;
4030 
4031 	wr32(hw, I40E_PFINT_DYN_CTL0,
4032 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4033 	i40e_flush(hw);
4034 }
4035 
4036 /**
4037  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4038  * @pf: board private structure
4039  **/
4040 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4041 {
4042 	struct i40e_hw *hw = &pf->hw;
4043 	u32 val;
4044 
4045 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4046 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4047 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4048 
4049 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4050 	i40e_flush(hw);
4051 }
4052 
4053 /**
4054  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4055  * @irq: interrupt number
4056  * @data: pointer to a q_vector
4057  **/
4058 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4059 {
4060 	struct i40e_q_vector *q_vector = data;
4061 
4062 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4063 		return IRQ_HANDLED;
4064 
4065 	napi_schedule_irqoff(&q_vector->napi);
4066 
4067 	return IRQ_HANDLED;
4068 }
4069 
4070 /**
4071  * i40e_irq_affinity_notify - Callback for affinity changes
4072  * @notify: context as to what irq was changed
4073  * @mask: the new affinity mask
4074  *
4075  * This is a callback function used by the irq_set_affinity_notifier function
4076  * so that we may register to receive changes to the irq affinity masks.
4077  **/
4078 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4079 				     const cpumask_t *mask)
4080 {
4081 	struct i40e_q_vector *q_vector =
4082 		container_of(notify, struct i40e_q_vector, affinity_notify);
4083 
4084 	cpumask_copy(&q_vector->affinity_mask, mask);
4085 }
4086 
4087 /**
4088  * i40e_irq_affinity_release - Callback for affinity notifier release
4089  * @ref: internal core kernel usage
4090  *
4091  * This is a callback function used by the irq_set_affinity_notifier function
4092  * to inform the current notification subscriber that they will no longer
4093  * receive notifications.
4094  **/
4095 static void i40e_irq_affinity_release(struct kref *ref) {}
4096 
4097 /**
4098  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4099  * @vsi: the VSI being configured
4100  * @basename: name for the vector
4101  *
4102  * Allocates MSI-X vectors and requests interrupts from the kernel.
4103  **/
4104 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4105 {
4106 	int q_vectors = vsi->num_q_vectors;
4107 	struct i40e_pf *pf = vsi->back;
4108 	int base = vsi->base_vector;
4109 	int rx_int_idx = 0;
4110 	int tx_int_idx = 0;
4111 	int vector, err;
4112 	int irq_num;
4113 	int cpu;
4114 
4115 	for (vector = 0; vector < q_vectors; vector++) {
4116 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4117 
4118 		irq_num = pf->msix_entries[base + vector].vector;
4119 
4120 		if (q_vector->tx.ring && q_vector->rx.ring) {
4121 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4122 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4123 			tx_int_idx++;
4124 		} else if (q_vector->rx.ring) {
4125 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4126 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4127 		} else if (q_vector->tx.ring) {
4128 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4129 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4130 		} else {
4131 			/* skip this unused q_vector */
4132 			continue;
4133 		}
4134 		err = request_irq(irq_num,
4135 				  vsi->irq_handler,
4136 				  0,
4137 				  q_vector->name,
4138 				  q_vector);
4139 		if (err) {
4140 			dev_info(&pf->pdev->dev,
4141 				 "MSIX request_irq failed, error: %d\n", err);
4142 			goto free_queue_irqs;
4143 		}
4144 
4145 		/* register for affinity change notifications */
4146 		q_vector->irq_num = irq_num;
4147 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4148 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4149 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4150 		/* Spread affinity hints out across online CPUs.
4151 		 *
4152 		 * get_cpu_mask returns a static constant mask with
4153 		 * a permanent lifetime so it's ok to pass to
4154 		 * irq_update_affinity_hint without making a copy.
4155 		 */
4156 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4157 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4158 	}
4159 
4160 	vsi->irqs_ready = true;
4161 	return 0;
4162 
4163 free_queue_irqs:
4164 	while (vector) {
4165 		vector--;
4166 		irq_num = pf->msix_entries[base + vector].vector;
4167 		irq_set_affinity_notifier(irq_num, NULL);
4168 		irq_update_affinity_hint(irq_num, NULL);
4169 		free_irq(irq_num, &vsi->q_vectors[vector]);
4170 	}
4171 	return err;
4172 }
4173 
4174 /**
4175  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4176  * @vsi: the VSI being un-configured
4177  **/
4178 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4179 {
4180 	struct i40e_pf *pf = vsi->back;
4181 	struct i40e_hw *hw = &pf->hw;
4182 	int base = vsi->base_vector;
4183 	int i;
4184 
4185 	/* disable interrupt causation from each queue */
4186 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4187 		u32 val;
4188 
4189 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4190 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4191 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4192 
4193 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4194 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4195 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4196 
4197 		if (!i40e_enabled_xdp_vsi(vsi))
4198 			continue;
4199 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4200 	}
4201 
4202 	/* disable each interrupt */
4203 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4204 		for (i = vsi->base_vector;
4205 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4206 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4207 
4208 		i40e_flush(hw);
4209 		for (i = 0; i < vsi->num_q_vectors; i++)
4210 			synchronize_irq(pf->msix_entries[i + base].vector);
4211 	} else {
4212 		/* Legacy and MSI mode - this stops all interrupt handling */
4213 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4214 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4215 		i40e_flush(hw);
4216 		synchronize_irq(pf->pdev->irq);
4217 	}
4218 }
4219 
4220 /**
4221  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4222  * @vsi: the VSI being configured
4223  **/
4224 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4225 {
4226 	struct i40e_pf *pf = vsi->back;
4227 	int i;
4228 
4229 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4230 		for (i = 0; i < vsi->num_q_vectors; i++)
4231 			i40e_irq_dynamic_enable(vsi, i);
4232 	} else {
4233 		i40e_irq_dynamic_enable_icr0(pf);
4234 	}
4235 
4236 	i40e_flush(&pf->hw);
4237 	return 0;
4238 }
4239 
4240 /**
4241  * i40e_free_misc_vector - Free the vector that handles non-queue events
4242  * @pf: board private structure
4243  **/
4244 static void i40e_free_misc_vector(struct i40e_pf *pf)
4245 {
4246 	/* Disable ICR 0 */
4247 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4248 	i40e_flush(&pf->hw);
4249 
4250 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4251 		free_irq(pf->msix_entries[0].vector, pf);
4252 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4253 	}
4254 }
4255 
4256 /**
4257  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4258  * @irq: interrupt number
4259  * @data: pointer to a q_vector
4260  *
4261  * This is the handler used for all MSI/Legacy interrupts, and deals
4262  * with both queue and non-queue interrupts.  This is also used in
4263  * MSIX mode to handle the non-queue interrupts.
4264  **/
4265 static irqreturn_t i40e_intr(int irq, void *data)
4266 {
4267 	struct i40e_pf *pf = (struct i40e_pf *)data;
4268 	struct i40e_hw *hw = &pf->hw;
4269 	irqreturn_t ret = IRQ_NONE;
4270 	u32 icr0, icr0_remaining;
4271 	u32 val, ena_mask;
4272 
4273 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4274 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4275 
4276 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4277 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4278 		goto enable_intr;
4279 
4280 	/* if interrupt but no bits showing, must be SWINT */
4281 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4282 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4283 		pf->sw_int_count++;
4284 
4285 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4286 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4287 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4288 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4289 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4290 	}
4291 
4292 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4293 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4294 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4295 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4296 
4297 		/* We do not have a way to disarm Queue causes while leaving
4298 		 * interrupt enabled for all other causes, ideally
4299 		 * interrupt should be disabled while we are in NAPI but
4300 		 * this is not a performance path and napi_schedule()
4301 		 * can deal with rescheduling.
4302 		 */
4303 		if (!test_bit(__I40E_DOWN, pf->state))
4304 			napi_schedule_irqoff(&q_vector->napi);
4305 	}
4306 
4307 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4308 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4309 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4310 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4311 	}
4312 
4313 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4314 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4315 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4316 	}
4317 
4318 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4319 		/* disable any further VFLR event notifications */
4320 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4321 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4322 
4323 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4324 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4325 		} else {
4326 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4327 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4328 		}
4329 	}
4330 
4331 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4332 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4333 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4334 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4335 		val = rd32(hw, I40E_GLGEN_RSTAT);
4336 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4337 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4338 		if (val == I40E_RESET_CORER) {
4339 			pf->corer_count++;
4340 		} else if (val == I40E_RESET_GLOBR) {
4341 			pf->globr_count++;
4342 		} else if (val == I40E_RESET_EMPR) {
4343 			pf->empr_count++;
4344 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4345 		}
4346 	}
4347 
4348 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4349 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4350 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4351 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4352 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4353 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4354 	}
4355 
4356 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4357 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4358 
4359 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4360 			schedule_work(&pf->ptp_extts0_work);
4361 
4362 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4363 			i40e_ptp_tx_hwtstamp(pf);
4364 
4365 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4366 	}
4367 
4368 	/* If a critical error is pending we have no choice but to reset the
4369 	 * device.
4370 	 * Report and mask out any remaining unexpected interrupts.
4371 	 */
4372 	icr0_remaining = icr0 & ena_mask;
4373 	if (icr0_remaining) {
4374 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4375 			 icr0_remaining);
4376 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4377 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4378 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4379 			dev_info(&pf->pdev->dev, "device will be reset\n");
4380 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4381 			i40e_service_event_schedule(pf);
4382 		}
4383 		ena_mask &= ~icr0_remaining;
4384 	}
4385 	ret = IRQ_HANDLED;
4386 
4387 enable_intr:
4388 	/* re-enable interrupt causes */
4389 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4390 	if (!test_bit(__I40E_DOWN, pf->state) ||
4391 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4392 		i40e_service_event_schedule(pf);
4393 		i40e_irq_dynamic_enable_icr0(pf);
4394 	}
4395 
4396 	return ret;
4397 }
4398 
4399 /**
4400  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4401  * @tx_ring:  tx ring to clean
4402  * @budget:   how many cleans we're allowed
4403  *
4404  * Returns true if there's any budget left (e.g. the clean is finished)
4405  **/
4406 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4407 {
4408 	struct i40e_vsi *vsi = tx_ring->vsi;
4409 	u16 i = tx_ring->next_to_clean;
4410 	struct i40e_tx_buffer *tx_buf;
4411 	struct i40e_tx_desc *tx_desc;
4412 
4413 	tx_buf = &tx_ring->tx_bi[i];
4414 	tx_desc = I40E_TX_DESC(tx_ring, i);
4415 	i -= tx_ring->count;
4416 
4417 	do {
4418 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4419 
4420 		/* if next_to_watch is not set then there is no work pending */
4421 		if (!eop_desc)
4422 			break;
4423 
4424 		/* prevent any other reads prior to eop_desc */
4425 		smp_rmb();
4426 
4427 		/* if the descriptor isn't done, no work yet to do */
4428 		if (!(eop_desc->cmd_type_offset_bsz &
4429 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4430 			break;
4431 
4432 		/* clear next_to_watch to prevent false hangs */
4433 		tx_buf->next_to_watch = NULL;
4434 
4435 		tx_desc->buffer_addr = 0;
4436 		tx_desc->cmd_type_offset_bsz = 0;
4437 		/* move past filter desc */
4438 		tx_buf++;
4439 		tx_desc++;
4440 		i++;
4441 		if (unlikely(!i)) {
4442 			i -= tx_ring->count;
4443 			tx_buf = tx_ring->tx_bi;
4444 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4445 		}
4446 		/* unmap skb header data */
4447 		dma_unmap_single(tx_ring->dev,
4448 				 dma_unmap_addr(tx_buf, dma),
4449 				 dma_unmap_len(tx_buf, len),
4450 				 DMA_TO_DEVICE);
4451 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4452 			kfree(tx_buf->raw_buf);
4453 
4454 		tx_buf->raw_buf = NULL;
4455 		tx_buf->tx_flags = 0;
4456 		tx_buf->next_to_watch = NULL;
4457 		dma_unmap_len_set(tx_buf, len, 0);
4458 		tx_desc->buffer_addr = 0;
4459 		tx_desc->cmd_type_offset_bsz = 0;
4460 
4461 		/* move us past the eop_desc for start of next FD desc */
4462 		tx_buf++;
4463 		tx_desc++;
4464 		i++;
4465 		if (unlikely(!i)) {
4466 			i -= tx_ring->count;
4467 			tx_buf = tx_ring->tx_bi;
4468 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4469 		}
4470 
4471 		/* update budget accounting */
4472 		budget--;
4473 	} while (likely(budget));
4474 
4475 	i += tx_ring->count;
4476 	tx_ring->next_to_clean = i;
4477 
4478 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4479 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4480 
4481 	return budget > 0;
4482 }
4483 
4484 /**
4485  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4486  * @irq: interrupt number
4487  * @data: pointer to a q_vector
4488  **/
4489 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4490 {
4491 	struct i40e_q_vector *q_vector = data;
4492 	struct i40e_vsi *vsi;
4493 
4494 	if (!q_vector->tx.ring)
4495 		return IRQ_HANDLED;
4496 
4497 	vsi = q_vector->tx.ring->vsi;
4498 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4499 
4500 	return IRQ_HANDLED;
4501 }
4502 
4503 /**
4504  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4505  * @vsi: the VSI being configured
4506  * @v_idx: vector index
4507  * @qp_idx: queue pair index
4508  **/
4509 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4510 {
4511 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4512 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4513 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4514 
4515 	tx_ring->q_vector = q_vector;
4516 	tx_ring->next = q_vector->tx.ring;
4517 	q_vector->tx.ring = tx_ring;
4518 	q_vector->tx.count++;
4519 
4520 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4521 	if (i40e_enabled_xdp_vsi(vsi)) {
4522 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4523 
4524 		xdp_ring->q_vector = q_vector;
4525 		xdp_ring->next = q_vector->tx.ring;
4526 		q_vector->tx.ring = xdp_ring;
4527 		q_vector->tx.count++;
4528 	}
4529 
4530 	rx_ring->q_vector = q_vector;
4531 	rx_ring->next = q_vector->rx.ring;
4532 	q_vector->rx.ring = rx_ring;
4533 	q_vector->rx.count++;
4534 }
4535 
4536 /**
4537  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4538  * @vsi: the VSI being configured
4539  *
4540  * This function maps descriptor rings to the queue-specific vectors
4541  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4542  * one vector per queue pair, but on a constrained vector budget, we
4543  * group the queue pairs as "efficiently" as possible.
4544  **/
4545 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4546 {
4547 	int qp_remaining = vsi->num_queue_pairs;
4548 	int q_vectors = vsi->num_q_vectors;
4549 	int num_ringpairs;
4550 	int v_start = 0;
4551 	int qp_idx = 0;
4552 
4553 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4554 	 * group them so there are multiple queues per vector.
4555 	 * It is also important to go through all the vectors available to be
4556 	 * sure that if we don't use all the vectors, that the remaining vectors
4557 	 * are cleared. This is especially important when decreasing the
4558 	 * number of queues in use.
4559 	 */
4560 	for (; v_start < q_vectors; v_start++) {
4561 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4562 
4563 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4564 
4565 		q_vector->num_ringpairs = num_ringpairs;
4566 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4567 
4568 		q_vector->rx.count = 0;
4569 		q_vector->tx.count = 0;
4570 		q_vector->rx.ring = NULL;
4571 		q_vector->tx.ring = NULL;
4572 
4573 		while (num_ringpairs--) {
4574 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4575 			qp_idx++;
4576 			qp_remaining--;
4577 		}
4578 	}
4579 }
4580 
4581 /**
4582  * i40e_vsi_request_irq - Request IRQ from the OS
4583  * @vsi: the VSI being configured
4584  * @basename: name for the vector
4585  **/
4586 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4587 {
4588 	struct i40e_pf *pf = vsi->back;
4589 	int err;
4590 
4591 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4592 		err = i40e_vsi_request_irq_msix(vsi, basename);
4593 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4594 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4595 				  pf->int_name, pf);
4596 	else
4597 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4598 				  pf->int_name, pf);
4599 
4600 	if (err)
4601 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4602 
4603 	return err;
4604 }
4605 
4606 #ifdef CONFIG_NET_POLL_CONTROLLER
4607 /**
4608  * i40e_netpoll - A Polling 'interrupt' handler
4609  * @netdev: network interface device structure
4610  *
4611  * This is used by netconsole to send skbs without having to re-enable
4612  * interrupts.  It's not called while the normal interrupt routine is executing.
4613  **/
4614 static void i40e_netpoll(struct net_device *netdev)
4615 {
4616 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4617 	struct i40e_vsi *vsi = np->vsi;
4618 	struct i40e_pf *pf = vsi->back;
4619 	int i;
4620 
4621 	/* if interface is down do nothing */
4622 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4623 		return;
4624 
4625 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4626 		for (i = 0; i < vsi->num_q_vectors; i++)
4627 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4628 	} else {
4629 		i40e_intr(pf->pdev->irq, netdev);
4630 	}
4631 }
4632 #endif
4633 
4634 #define I40E_QTX_ENA_WAIT_COUNT 50
4635 
4636 /**
4637  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4638  * @pf: the PF being configured
4639  * @pf_q: the PF queue
4640  * @enable: enable or disable state of the queue
4641  *
4642  * This routine will wait for the given Tx queue of the PF to reach the
4643  * enabled or disabled state.
4644  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4645  * multiple retries; else will return 0 in case of success.
4646  **/
4647 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4648 {
4649 	int i;
4650 	u32 tx_reg;
4651 
4652 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4653 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4654 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4655 			break;
4656 
4657 		usleep_range(10, 20);
4658 	}
4659 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4660 		return -ETIMEDOUT;
4661 
4662 	return 0;
4663 }
4664 
4665 /**
4666  * i40e_control_tx_q - Start or stop a particular Tx queue
4667  * @pf: the PF structure
4668  * @pf_q: the PF queue to configure
4669  * @enable: start or stop the queue
4670  *
4671  * This function enables or disables a single queue. Note that any delay
4672  * required after the operation is expected to be handled by the caller of
4673  * this function.
4674  **/
4675 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4676 {
4677 	struct i40e_hw *hw = &pf->hw;
4678 	u32 tx_reg;
4679 	int i;
4680 
4681 	/* warn the TX unit of coming changes */
4682 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4683 	if (!enable)
4684 		usleep_range(10, 20);
4685 
4686 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4687 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4688 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4689 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4690 			break;
4691 		usleep_range(1000, 2000);
4692 	}
4693 
4694 	/* Skip if the queue is already in the requested state */
4695 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4696 		return;
4697 
4698 	/* turn on/off the queue */
4699 	if (enable) {
4700 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4701 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4702 	} else {
4703 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4704 	}
4705 
4706 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4707 }
4708 
4709 /**
4710  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4711  * @seid: VSI SEID
4712  * @pf: the PF structure
4713  * @pf_q: the PF queue to configure
4714  * @is_xdp: true if the queue is used for XDP
4715  * @enable: start or stop the queue
4716  **/
4717 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4718 			   bool is_xdp, bool enable)
4719 {
4720 	int ret;
4721 
4722 	i40e_control_tx_q(pf, pf_q, enable);
4723 
4724 	/* wait for the change to finish */
4725 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4726 	if (ret) {
4727 		dev_info(&pf->pdev->dev,
4728 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4729 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4730 			 (enable ? "en" : "dis"));
4731 	}
4732 
4733 	return ret;
4734 }
4735 
4736 /**
4737  * i40e_vsi_enable_tx - Start a VSI's rings
4738  * @vsi: the VSI being configured
4739  **/
4740 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4741 {
4742 	struct i40e_pf *pf = vsi->back;
4743 	int i, pf_q, ret = 0;
4744 
4745 	pf_q = vsi->base_queue;
4746 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4747 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4748 					     pf_q,
4749 					     false /*is xdp*/, true);
4750 		if (ret)
4751 			break;
4752 
4753 		if (!i40e_enabled_xdp_vsi(vsi))
4754 			continue;
4755 
4756 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4757 					     pf_q + vsi->alloc_queue_pairs,
4758 					     true /*is xdp*/, true);
4759 		if (ret)
4760 			break;
4761 	}
4762 	return ret;
4763 }
4764 
4765 /**
4766  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4767  * @pf: the PF being configured
4768  * @pf_q: the PF queue
4769  * @enable: enable or disable state of the queue
4770  *
4771  * This routine will wait for the given Rx queue of the PF to reach the
4772  * enabled or disabled state.
4773  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4774  * multiple retries; else will return 0 in case of success.
4775  **/
4776 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4777 {
4778 	int i;
4779 	u32 rx_reg;
4780 
4781 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4782 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4783 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4784 			break;
4785 
4786 		usleep_range(10, 20);
4787 	}
4788 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4789 		return -ETIMEDOUT;
4790 
4791 	return 0;
4792 }
4793 
4794 /**
4795  * i40e_control_rx_q - Start or stop a particular Rx queue
4796  * @pf: the PF structure
4797  * @pf_q: the PF queue to configure
4798  * @enable: start or stop the queue
4799  *
4800  * This function enables or disables a single queue. Note that
4801  * any delay required after the operation is expected to be
4802  * handled by the caller of this function.
4803  **/
4804 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4805 {
4806 	struct i40e_hw *hw = &pf->hw;
4807 	u32 rx_reg;
4808 	int i;
4809 
4810 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4811 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4812 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4813 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4814 			break;
4815 		usleep_range(1000, 2000);
4816 	}
4817 
4818 	/* Skip if the queue is already in the requested state */
4819 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4820 		return;
4821 
4822 	/* turn on/off the queue */
4823 	if (enable)
4824 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4825 	else
4826 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4827 
4828 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4829 }
4830 
4831 /**
4832  * i40e_control_wait_rx_q
4833  * @pf: the PF structure
4834  * @pf_q: queue being configured
4835  * @enable: start or stop the rings
4836  *
4837  * This function enables or disables a single queue along with waiting
4838  * for the change to finish. The caller of this function should handle
4839  * the delays needed in the case of disabling queues.
4840  **/
4841 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4842 {
4843 	int ret = 0;
4844 
4845 	i40e_control_rx_q(pf, pf_q, enable);
4846 
4847 	/* wait for the change to finish */
4848 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4849 	if (ret)
4850 		return ret;
4851 
4852 	return ret;
4853 }
4854 
4855 /**
4856  * i40e_vsi_enable_rx - Start a VSI's rings
4857  * @vsi: the VSI being configured
4858  **/
4859 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4860 {
4861 	struct i40e_pf *pf = vsi->back;
4862 	int i, pf_q, ret = 0;
4863 
4864 	pf_q = vsi->base_queue;
4865 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4866 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4867 		if (ret) {
4868 			dev_info(&pf->pdev->dev,
4869 				 "VSI seid %d Rx ring %d enable timeout\n",
4870 				 vsi->seid, pf_q);
4871 			break;
4872 		}
4873 	}
4874 
4875 	return ret;
4876 }
4877 
4878 /**
4879  * i40e_vsi_start_rings - Start a VSI's rings
4880  * @vsi: the VSI being configured
4881  **/
4882 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4883 {
4884 	int ret = 0;
4885 
4886 	/* do rx first for enable and last for disable */
4887 	ret = i40e_vsi_enable_rx(vsi);
4888 	if (ret)
4889 		return ret;
4890 	ret = i40e_vsi_enable_tx(vsi);
4891 
4892 	return ret;
4893 }
4894 
4895 #define I40E_DISABLE_TX_GAP_MSEC	50
4896 
4897 /**
4898  * i40e_vsi_stop_rings - Stop a VSI's rings
4899  * @vsi: the VSI being configured
4900  **/
4901 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4902 {
4903 	struct i40e_pf *pf = vsi->back;
4904 	int pf_q, err, q_end;
4905 
4906 	/* When port TX is suspended, don't wait */
4907 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4908 		return i40e_vsi_stop_rings_no_wait(vsi);
4909 
4910 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4911 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4912 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4913 
4914 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4915 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4916 		if (err)
4917 			dev_info(&pf->pdev->dev,
4918 				 "VSI seid %d Rx ring %d disable timeout\n",
4919 				 vsi->seid, pf_q);
4920 	}
4921 
4922 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4923 	pf_q = vsi->base_queue;
4924 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4925 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4926 
4927 	i40e_vsi_wait_queues_disabled(vsi);
4928 }
4929 
4930 /**
4931  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4932  * @vsi: the VSI being shutdown
4933  *
4934  * This function stops all the rings for a VSI but does not delay to verify
4935  * that rings have been disabled. It is expected that the caller is shutting
4936  * down multiple VSIs at once and will delay together for all the VSIs after
4937  * initiating the shutdown. This is particularly useful for shutting down lots
4938  * of VFs together. Otherwise, a large delay can be incurred while configuring
4939  * each VSI in serial.
4940  **/
4941 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4942 {
4943 	struct i40e_pf *pf = vsi->back;
4944 	int i, pf_q;
4945 
4946 	pf_q = vsi->base_queue;
4947 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4948 		i40e_control_tx_q(pf, pf_q, false);
4949 		i40e_control_rx_q(pf, pf_q, false);
4950 	}
4951 }
4952 
4953 /**
4954  * i40e_vsi_free_irq - Free the irq association with the OS
4955  * @vsi: the VSI being configured
4956  **/
4957 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4958 {
4959 	struct i40e_pf *pf = vsi->back;
4960 	struct i40e_hw *hw = &pf->hw;
4961 	int base = vsi->base_vector;
4962 	u32 val, qp;
4963 	int i;
4964 
4965 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4966 		if (!vsi->q_vectors)
4967 			return;
4968 
4969 		if (!vsi->irqs_ready)
4970 			return;
4971 
4972 		vsi->irqs_ready = false;
4973 		for (i = 0; i < vsi->num_q_vectors; i++) {
4974 			int irq_num;
4975 			u16 vector;
4976 
4977 			vector = i + base;
4978 			irq_num = pf->msix_entries[vector].vector;
4979 
4980 			/* free only the irqs that were actually requested */
4981 			if (!vsi->q_vectors[i] ||
4982 			    !vsi->q_vectors[i]->num_ringpairs)
4983 				continue;
4984 
4985 			/* clear the affinity notifier in the IRQ descriptor */
4986 			irq_set_affinity_notifier(irq_num, NULL);
4987 			/* remove our suggested affinity mask for this IRQ */
4988 			irq_update_affinity_hint(irq_num, NULL);
4989 			free_irq(irq_num, vsi->q_vectors[i]);
4990 
4991 			/* Tear down the interrupt queue link list
4992 			 *
4993 			 * We know that they come in pairs and always
4994 			 * the Rx first, then the Tx.  To clear the
4995 			 * link list, stick the EOL value into the
4996 			 * next_q field of the registers.
4997 			 */
4998 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4999 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5000 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5001 			val |= I40E_QUEUE_END_OF_LIST
5002 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5003 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5004 
5005 			while (qp != I40E_QUEUE_END_OF_LIST) {
5006 				u32 next;
5007 
5008 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5009 
5010 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5011 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5012 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5013 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5014 
5015 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5016 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5017 
5018 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5019 
5020 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5021 
5022 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5023 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5024 
5025 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5026 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5027 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5028 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5029 
5030 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5031 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5032 
5033 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5034 				qp = next;
5035 			}
5036 		}
5037 	} else {
5038 		free_irq(pf->pdev->irq, pf);
5039 
5040 		val = rd32(hw, I40E_PFINT_LNKLST0);
5041 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5042 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5043 		val |= I40E_QUEUE_END_OF_LIST
5044 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5045 		wr32(hw, I40E_PFINT_LNKLST0, val);
5046 
5047 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5048 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5049 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5050 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5051 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5052 
5053 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5054 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5055 
5056 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5057 
5058 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5059 
5060 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5061 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5062 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5063 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5064 
5065 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5066 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5067 
5068 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5069 	}
5070 }
5071 
5072 /**
5073  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5074  * @vsi: the VSI being configured
5075  * @v_idx: Index of vector to be freed
5076  *
5077  * This function frees the memory allocated to the q_vector.  In addition if
5078  * NAPI is enabled it will delete any references to the NAPI struct prior
5079  * to freeing the q_vector.
5080  **/
5081 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5082 {
5083 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5084 	struct i40e_ring *ring;
5085 
5086 	if (!q_vector)
5087 		return;
5088 
5089 	/* disassociate q_vector from rings */
5090 	i40e_for_each_ring(ring, q_vector->tx)
5091 		ring->q_vector = NULL;
5092 
5093 	i40e_for_each_ring(ring, q_vector->rx)
5094 		ring->q_vector = NULL;
5095 
5096 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5097 	if (vsi->netdev)
5098 		netif_napi_del(&q_vector->napi);
5099 
5100 	vsi->q_vectors[v_idx] = NULL;
5101 
5102 	kfree_rcu(q_vector, rcu);
5103 }
5104 
5105 /**
5106  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5107  * @vsi: the VSI being un-configured
5108  *
5109  * This frees the memory allocated to the q_vectors and
5110  * deletes references to the NAPI struct.
5111  **/
5112 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5113 {
5114 	int v_idx;
5115 
5116 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5117 		i40e_free_q_vector(vsi, v_idx);
5118 }
5119 
5120 /**
5121  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5122  * @pf: board private structure
5123  **/
5124 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5125 {
5126 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5127 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5128 		pci_disable_msix(pf->pdev);
5129 		kfree(pf->msix_entries);
5130 		pf->msix_entries = NULL;
5131 		kfree(pf->irq_pile);
5132 		pf->irq_pile = NULL;
5133 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5134 		pci_disable_msi(pf->pdev);
5135 	}
5136 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5137 }
5138 
5139 /**
5140  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5141  * @pf: board private structure
5142  *
5143  * We go through and clear interrupt specific resources and reset the structure
5144  * to pre-load conditions
5145  **/
5146 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5147 {
5148 	int i;
5149 
5150 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5151 		i40e_free_misc_vector(pf);
5152 
5153 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5154 		      I40E_IWARP_IRQ_PILE_ID);
5155 
5156 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5157 	for (i = 0; i < pf->num_alloc_vsi; i++)
5158 		if (pf->vsi[i])
5159 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5160 	i40e_reset_interrupt_capability(pf);
5161 }
5162 
5163 /**
5164  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5165  * @vsi: the VSI being configured
5166  **/
5167 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5168 {
5169 	int q_idx;
5170 
5171 	if (!vsi->netdev)
5172 		return;
5173 
5174 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5175 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5176 
5177 		if (q_vector->rx.ring || q_vector->tx.ring)
5178 			napi_enable(&q_vector->napi);
5179 	}
5180 }
5181 
5182 /**
5183  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5184  * @vsi: the VSI being configured
5185  **/
5186 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5187 {
5188 	int q_idx;
5189 
5190 	if (!vsi->netdev)
5191 		return;
5192 
5193 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5194 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5195 
5196 		if (q_vector->rx.ring || q_vector->tx.ring)
5197 			napi_disable(&q_vector->napi);
5198 	}
5199 }
5200 
5201 /**
5202  * i40e_vsi_close - Shut down a VSI
5203  * @vsi: the vsi to be quelled
5204  **/
5205 static void i40e_vsi_close(struct i40e_vsi *vsi)
5206 {
5207 	struct i40e_pf *pf = vsi->back;
5208 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5209 		i40e_down(vsi);
5210 	i40e_vsi_free_irq(vsi);
5211 	i40e_vsi_free_tx_resources(vsi);
5212 	i40e_vsi_free_rx_resources(vsi);
5213 	vsi->current_netdev_flags = 0;
5214 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5215 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5216 		set_bit(__I40E_CLIENT_RESET, pf->state);
5217 }
5218 
5219 /**
5220  * i40e_quiesce_vsi - Pause a given VSI
5221  * @vsi: the VSI being paused
5222  **/
5223 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5224 {
5225 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5226 		return;
5227 
5228 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5229 	if (vsi->netdev && netif_running(vsi->netdev))
5230 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5231 	else
5232 		i40e_vsi_close(vsi);
5233 }
5234 
5235 /**
5236  * i40e_unquiesce_vsi - Resume a given VSI
5237  * @vsi: the VSI being resumed
5238  **/
5239 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5240 {
5241 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5242 		return;
5243 
5244 	if (vsi->netdev && netif_running(vsi->netdev))
5245 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5246 	else
5247 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5248 }
5249 
5250 /**
5251  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5252  * @pf: the PF
5253  **/
5254 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5255 {
5256 	int v;
5257 
5258 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5259 		if (pf->vsi[v])
5260 			i40e_quiesce_vsi(pf->vsi[v]);
5261 	}
5262 }
5263 
5264 /**
5265  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5266  * @pf: the PF
5267  **/
5268 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5269 {
5270 	int v;
5271 
5272 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5273 		if (pf->vsi[v])
5274 			i40e_unquiesce_vsi(pf->vsi[v]);
5275 	}
5276 }
5277 
5278 /**
5279  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5280  * @vsi: the VSI being configured
5281  *
5282  * Wait until all queues on a given VSI have been disabled.
5283  **/
5284 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5285 {
5286 	struct i40e_pf *pf = vsi->back;
5287 	int i, pf_q, ret;
5288 
5289 	pf_q = vsi->base_queue;
5290 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5291 		/* Check and wait for the Tx queue */
5292 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5293 		if (ret) {
5294 			dev_info(&pf->pdev->dev,
5295 				 "VSI seid %d Tx ring %d disable timeout\n",
5296 				 vsi->seid, pf_q);
5297 			return ret;
5298 		}
5299 
5300 		if (!i40e_enabled_xdp_vsi(vsi))
5301 			goto wait_rx;
5302 
5303 		/* Check and wait for the XDP Tx queue */
5304 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5305 				       false);
5306 		if (ret) {
5307 			dev_info(&pf->pdev->dev,
5308 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5309 				 vsi->seid, pf_q);
5310 			return ret;
5311 		}
5312 wait_rx:
5313 		/* Check and wait for the Rx queue */
5314 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5315 		if (ret) {
5316 			dev_info(&pf->pdev->dev,
5317 				 "VSI seid %d Rx ring %d disable timeout\n",
5318 				 vsi->seid, pf_q);
5319 			return ret;
5320 		}
5321 	}
5322 
5323 	return 0;
5324 }
5325 
5326 #ifdef CONFIG_I40E_DCB
5327 /**
5328  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5329  * @pf: the PF
5330  *
5331  * This function waits for the queues to be in disabled state for all the
5332  * VSIs that are managed by this PF.
5333  **/
5334 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5335 {
5336 	int v, ret = 0;
5337 
5338 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5339 		if (pf->vsi[v]) {
5340 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5341 			if (ret)
5342 				break;
5343 		}
5344 	}
5345 
5346 	return ret;
5347 }
5348 
5349 #endif
5350 
5351 /**
5352  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5353  * @pf: pointer to PF
5354  *
5355  * Get TC map for ISCSI PF type that will include iSCSI TC
5356  * and LAN TC.
5357  **/
5358 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5359 {
5360 	struct i40e_dcb_app_priority_table app;
5361 	struct i40e_hw *hw = &pf->hw;
5362 	u8 enabled_tc = 1; /* TC0 is always enabled */
5363 	u8 tc, i;
5364 	/* Get the iSCSI APP TLV */
5365 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5366 
5367 	for (i = 0; i < dcbcfg->numapps; i++) {
5368 		app = dcbcfg->app[i];
5369 		if (app.selector == I40E_APP_SEL_TCPIP &&
5370 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5371 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5372 			enabled_tc |= BIT(tc);
5373 			break;
5374 		}
5375 	}
5376 
5377 	return enabled_tc;
5378 }
5379 
5380 /**
5381  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5382  * @dcbcfg: the corresponding DCBx configuration structure
5383  *
5384  * Return the number of TCs from given DCBx configuration
5385  **/
5386 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5387 {
5388 	int i, tc_unused = 0;
5389 	u8 num_tc = 0;
5390 	u8 ret = 0;
5391 
5392 	/* Scan the ETS Config Priority Table to find
5393 	 * traffic class enabled for a given priority
5394 	 * and create a bitmask of enabled TCs
5395 	 */
5396 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5397 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5398 
5399 	/* Now scan the bitmask to check for
5400 	 * contiguous TCs starting with TC0
5401 	 */
5402 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5403 		if (num_tc & BIT(i)) {
5404 			if (!tc_unused) {
5405 				ret++;
5406 			} else {
5407 				pr_err("Non-contiguous TC - Disabling DCB\n");
5408 				return 1;
5409 			}
5410 		} else {
5411 			tc_unused = 1;
5412 		}
5413 	}
5414 
5415 	/* There is always at least TC0 */
5416 	if (!ret)
5417 		ret = 1;
5418 
5419 	return ret;
5420 }
5421 
5422 /**
5423  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5424  * @dcbcfg: the corresponding DCBx configuration structure
5425  *
5426  * Query the current DCB configuration and return the number of
5427  * traffic classes enabled from the given DCBX config
5428  **/
5429 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5430 {
5431 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5432 	u8 enabled_tc = 1;
5433 	u8 i;
5434 
5435 	for (i = 0; i < num_tc; i++)
5436 		enabled_tc |= BIT(i);
5437 
5438 	return enabled_tc;
5439 }
5440 
5441 /**
5442  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5443  * @pf: PF being queried
5444  *
5445  * Query the current MQPRIO configuration and return the number of
5446  * traffic classes enabled.
5447  **/
5448 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5449 {
5450 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5451 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5452 	u8 enabled_tc = 1, i;
5453 
5454 	for (i = 1; i < num_tc; i++)
5455 		enabled_tc |= BIT(i);
5456 	return enabled_tc;
5457 }
5458 
5459 /**
5460  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5461  * @pf: PF being queried
5462  *
5463  * Return number of traffic classes enabled for the given PF
5464  **/
5465 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5466 {
5467 	struct i40e_hw *hw = &pf->hw;
5468 	u8 i, enabled_tc = 1;
5469 	u8 num_tc = 0;
5470 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5471 
5472 	if (i40e_is_tc_mqprio_enabled(pf))
5473 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5474 
5475 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5476 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5477 		return 1;
5478 
5479 	/* SFP mode will be enabled for all TCs on port */
5480 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5481 		return i40e_dcb_get_num_tc(dcbcfg);
5482 
5483 	/* MFP mode return count of enabled TCs for this PF */
5484 	if (pf->hw.func_caps.iscsi)
5485 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5486 	else
5487 		return 1; /* Only TC0 */
5488 
5489 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5490 		if (enabled_tc & BIT(i))
5491 			num_tc++;
5492 	}
5493 	return num_tc;
5494 }
5495 
5496 /**
5497  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5498  * @pf: PF being queried
5499  *
5500  * Return a bitmap for enabled traffic classes for this PF.
5501  **/
5502 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5503 {
5504 	if (i40e_is_tc_mqprio_enabled(pf))
5505 		return i40e_mqprio_get_enabled_tc(pf);
5506 
5507 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5508 	 * default TC
5509 	 */
5510 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5511 		return I40E_DEFAULT_TRAFFIC_CLASS;
5512 
5513 	/* SFP mode we want PF to be enabled for all TCs */
5514 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5515 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5516 
5517 	/* MFP enabled and iSCSI PF type */
5518 	if (pf->hw.func_caps.iscsi)
5519 		return i40e_get_iscsi_tc_map(pf);
5520 	else
5521 		return I40E_DEFAULT_TRAFFIC_CLASS;
5522 }
5523 
5524 /**
5525  * i40e_vsi_get_bw_info - Query VSI BW Information
5526  * @vsi: the VSI being queried
5527  *
5528  * Returns 0 on success, negative value on failure
5529  **/
5530 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5531 {
5532 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5533 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5534 	struct i40e_pf *pf = vsi->back;
5535 	struct i40e_hw *hw = &pf->hw;
5536 	u32 tc_bw_max;
5537 	int ret;
5538 	int i;
5539 
5540 	/* Get the VSI level BW configuration */
5541 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5542 	if (ret) {
5543 		dev_info(&pf->pdev->dev,
5544 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5545 			 ERR_PTR(ret),
5546 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5547 		return -EINVAL;
5548 	}
5549 
5550 	/* Get the VSI level BW configuration per TC */
5551 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5552 					       NULL);
5553 	if (ret) {
5554 		dev_info(&pf->pdev->dev,
5555 			 "couldn't get PF vsi ets 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 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5562 		dev_info(&pf->pdev->dev,
5563 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5564 			 bw_config.tc_valid_bits,
5565 			 bw_ets_config.tc_valid_bits);
5566 		/* Still continuing */
5567 	}
5568 
5569 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5570 	vsi->bw_max_quanta = bw_config.max_bw;
5571 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5572 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5573 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5574 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5575 		vsi->bw_ets_limit_credits[i] =
5576 					le16_to_cpu(bw_ets_config.credits[i]);
5577 		/* 3 bits out of 4 for each TC */
5578 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5579 	}
5580 
5581 	return 0;
5582 }
5583 
5584 /**
5585  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5586  * @vsi: the VSI being configured
5587  * @enabled_tc: TC bitmap
5588  * @bw_share: BW shared credits per TC
5589  *
5590  * Returns 0 on success, negative value on failure
5591  **/
5592 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5593 				       u8 *bw_share)
5594 {
5595 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5596 	struct i40e_pf *pf = vsi->back;
5597 	int ret;
5598 	int i;
5599 
5600 	/* There is no need to reset BW when mqprio mode is on.  */
5601 	if (i40e_is_tc_mqprio_enabled(pf))
5602 		return 0;
5603 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5604 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5605 		if (ret)
5606 			dev_info(&pf->pdev->dev,
5607 				 "Failed to reset tx rate for vsi->seid %u\n",
5608 				 vsi->seid);
5609 		return ret;
5610 	}
5611 	memset(&bw_data, 0, sizeof(bw_data));
5612 	bw_data.tc_valid_bits = enabled_tc;
5613 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5614 		bw_data.tc_bw_credits[i] = bw_share[i];
5615 
5616 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5617 	if (ret) {
5618 		dev_info(&pf->pdev->dev,
5619 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5620 			 pf->hw.aq.asq_last_status);
5621 		return -EINVAL;
5622 	}
5623 
5624 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5625 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5626 
5627 	return 0;
5628 }
5629 
5630 /**
5631  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5632  * @vsi: the VSI being configured
5633  * @enabled_tc: TC map to be enabled
5634  *
5635  **/
5636 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5637 {
5638 	struct net_device *netdev = vsi->netdev;
5639 	struct i40e_pf *pf = vsi->back;
5640 	struct i40e_hw *hw = &pf->hw;
5641 	u8 netdev_tc = 0;
5642 	int i;
5643 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5644 
5645 	if (!netdev)
5646 		return;
5647 
5648 	if (!enabled_tc) {
5649 		netdev_reset_tc(netdev);
5650 		return;
5651 	}
5652 
5653 	/* Set up actual enabled TCs on the VSI */
5654 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5655 		return;
5656 
5657 	/* set per TC queues for the VSI */
5658 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5659 		/* Only set TC queues for enabled tcs
5660 		 *
5661 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5662 		 * enabled_tc bitmap would be 0x00001001; the driver
5663 		 * will set the numtc for netdev as 2 that will be
5664 		 * referenced by the netdev layer as TC 0 and 1.
5665 		 */
5666 		if (vsi->tc_config.enabled_tc & BIT(i))
5667 			netdev_set_tc_queue(netdev,
5668 					vsi->tc_config.tc_info[i].netdev_tc,
5669 					vsi->tc_config.tc_info[i].qcount,
5670 					vsi->tc_config.tc_info[i].qoffset);
5671 	}
5672 
5673 	if (i40e_is_tc_mqprio_enabled(pf))
5674 		return;
5675 
5676 	/* Assign UP2TC map for the VSI */
5677 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5678 		/* Get the actual TC# for the UP */
5679 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5680 		/* Get the mapped netdev TC# for the UP */
5681 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5682 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5683 	}
5684 }
5685 
5686 /**
5687  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5688  * @vsi: the VSI being configured
5689  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5690  **/
5691 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5692 				      struct i40e_vsi_context *ctxt)
5693 {
5694 	/* copy just the sections touched not the entire info
5695 	 * since not all sections are valid as returned by
5696 	 * update vsi params
5697 	 */
5698 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5699 	memcpy(&vsi->info.queue_mapping,
5700 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5701 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5702 	       sizeof(vsi->info.tc_mapping));
5703 }
5704 
5705 /**
5706  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5707  * @vsi: the VSI being reconfigured
5708  * @vsi_offset: offset from main VF VSI
5709  */
5710 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5711 {
5712 	struct i40e_vsi_context ctxt = {};
5713 	struct i40e_pf *pf;
5714 	struct i40e_hw *hw;
5715 	int ret;
5716 
5717 	if (!vsi)
5718 		return I40E_ERR_PARAM;
5719 	pf = vsi->back;
5720 	hw = &pf->hw;
5721 
5722 	ctxt.seid = vsi->seid;
5723 	ctxt.pf_num = hw->pf_id;
5724 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5725 	ctxt.uplink_seid = vsi->uplink_seid;
5726 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5727 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5728 	ctxt.info = vsi->info;
5729 
5730 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5731 				 false);
5732 	if (vsi->reconfig_rss) {
5733 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5734 				      vsi->num_queue_pairs);
5735 		ret = i40e_vsi_config_rss(vsi);
5736 		if (ret) {
5737 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5738 			return ret;
5739 		}
5740 		vsi->reconfig_rss = false;
5741 	}
5742 
5743 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5744 	if (ret) {
5745 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5746 			 ERR_PTR(ret),
5747 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5748 		return ret;
5749 	}
5750 	/* update the local VSI info with updated queue map */
5751 	i40e_vsi_update_queue_map(vsi, &ctxt);
5752 	vsi->info.valid_sections = 0;
5753 
5754 	return ret;
5755 }
5756 
5757 /**
5758  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5759  * @vsi: VSI to be configured
5760  * @enabled_tc: TC bitmap
5761  *
5762  * This configures a particular VSI for TCs that are mapped to the
5763  * given TC bitmap. It uses default bandwidth share for TCs across
5764  * VSIs to configure TC for a particular VSI.
5765  *
5766  * NOTE:
5767  * It is expected that the VSI queues have been quisced before calling
5768  * this function.
5769  **/
5770 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5771 {
5772 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5773 	struct i40e_pf *pf = vsi->back;
5774 	struct i40e_hw *hw = &pf->hw;
5775 	struct i40e_vsi_context ctxt;
5776 	int ret = 0;
5777 	int i;
5778 
5779 	/* Check if enabled_tc is same as existing or new TCs */
5780 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5781 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5782 		return ret;
5783 
5784 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5785 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5786 		if (enabled_tc & BIT(i))
5787 			bw_share[i] = 1;
5788 	}
5789 
5790 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5791 	if (ret) {
5792 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5793 
5794 		dev_info(&pf->pdev->dev,
5795 			 "Failed configuring TC map %d for VSI %d\n",
5796 			 enabled_tc, vsi->seid);
5797 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5798 						  &bw_config, NULL);
5799 		if (ret) {
5800 			dev_info(&pf->pdev->dev,
5801 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5802 				 ERR_PTR(ret),
5803 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5804 			goto out;
5805 		}
5806 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5807 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5808 
5809 			if (!valid_tc)
5810 				valid_tc = bw_config.tc_valid_bits;
5811 			/* Always enable TC0, no matter what */
5812 			valid_tc |= 1;
5813 			dev_info(&pf->pdev->dev,
5814 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5815 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5816 			enabled_tc = valid_tc;
5817 		}
5818 
5819 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5820 		if (ret) {
5821 			dev_err(&pf->pdev->dev,
5822 				"Unable to  configure TC map %d for VSI %d\n",
5823 				enabled_tc, vsi->seid);
5824 			goto out;
5825 		}
5826 	}
5827 
5828 	/* Update Queue Pairs Mapping for currently enabled UPs */
5829 	ctxt.seid = vsi->seid;
5830 	ctxt.pf_num = vsi->back->hw.pf_id;
5831 	ctxt.vf_num = 0;
5832 	ctxt.uplink_seid = vsi->uplink_seid;
5833 	ctxt.info = vsi->info;
5834 	if (i40e_is_tc_mqprio_enabled(pf)) {
5835 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5836 		if (ret)
5837 			goto out;
5838 	} else {
5839 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5840 	}
5841 
5842 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5843 	 * queues changed.
5844 	 */
5845 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5846 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5847 				      vsi->num_queue_pairs);
5848 		ret = i40e_vsi_config_rss(vsi);
5849 		if (ret) {
5850 			dev_info(&vsi->back->pdev->dev,
5851 				 "Failed to reconfig rss for num_queues\n");
5852 			return ret;
5853 		}
5854 		vsi->reconfig_rss = false;
5855 	}
5856 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5857 		ctxt.info.valid_sections |=
5858 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5859 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5860 	}
5861 
5862 	/* Update the VSI after updating the VSI queue-mapping
5863 	 * information
5864 	 */
5865 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5866 	if (ret) {
5867 		dev_info(&pf->pdev->dev,
5868 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5869 			 ERR_PTR(ret),
5870 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5871 		goto out;
5872 	}
5873 	/* update the local VSI info with updated queue map */
5874 	i40e_vsi_update_queue_map(vsi, &ctxt);
5875 	vsi->info.valid_sections = 0;
5876 
5877 	/* Update current VSI BW information */
5878 	ret = i40e_vsi_get_bw_info(vsi);
5879 	if (ret) {
5880 		dev_info(&pf->pdev->dev,
5881 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5882 			 ERR_PTR(ret),
5883 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5884 		goto out;
5885 	}
5886 
5887 	/* Update the netdev TC setup */
5888 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5889 out:
5890 	return ret;
5891 }
5892 
5893 /**
5894  * i40e_get_link_speed - Returns link speed for the interface
5895  * @vsi: VSI to be configured
5896  *
5897  **/
5898 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5899 {
5900 	struct i40e_pf *pf = vsi->back;
5901 
5902 	switch (pf->hw.phy.link_info.link_speed) {
5903 	case I40E_LINK_SPEED_40GB:
5904 		return 40000;
5905 	case I40E_LINK_SPEED_25GB:
5906 		return 25000;
5907 	case I40E_LINK_SPEED_20GB:
5908 		return 20000;
5909 	case I40E_LINK_SPEED_10GB:
5910 		return 10000;
5911 	case I40E_LINK_SPEED_1GB:
5912 		return 1000;
5913 	default:
5914 		return -EINVAL;
5915 	}
5916 }
5917 
5918 /**
5919  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5920  * @vsi: Pointer to vsi structure
5921  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5922  *
5923  * Helper function to convert units before send to set BW limit
5924  **/
5925 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5926 {
5927 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5928 		dev_warn(&vsi->back->pdev->dev,
5929 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5930 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5931 	} else {
5932 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5933 	}
5934 
5935 	return max_tx_rate;
5936 }
5937 
5938 /**
5939  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5940  * @vsi: VSI to be configured
5941  * @seid: seid of the channel/VSI
5942  * @max_tx_rate: max TX rate to be configured as BW limit
5943  *
5944  * Helper function to set BW limit for a given VSI
5945  **/
5946 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5947 {
5948 	struct i40e_pf *pf = vsi->back;
5949 	u64 credits = 0;
5950 	int speed = 0;
5951 	int ret = 0;
5952 
5953 	speed = i40e_get_link_speed(vsi);
5954 	if (max_tx_rate > speed) {
5955 		dev_err(&pf->pdev->dev,
5956 			"Invalid max tx rate %llu specified for VSI seid %d.",
5957 			max_tx_rate, seid);
5958 		return -EINVAL;
5959 	}
5960 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5961 		dev_warn(&pf->pdev->dev,
5962 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5963 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5964 	}
5965 
5966 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5967 	credits = max_tx_rate;
5968 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5969 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5970 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5971 	if (ret)
5972 		dev_err(&pf->pdev->dev,
5973 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5974 			max_tx_rate, seid, ERR_PTR(ret),
5975 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5976 	return ret;
5977 }
5978 
5979 /**
5980  * i40e_remove_queue_channels - Remove queue channels for the TCs
5981  * @vsi: VSI to be configured
5982  *
5983  * Remove queue channels for the TCs
5984  **/
5985 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5986 {
5987 	enum i40e_admin_queue_err last_aq_status;
5988 	struct i40e_cloud_filter *cfilter;
5989 	struct i40e_channel *ch, *ch_tmp;
5990 	struct i40e_pf *pf = vsi->back;
5991 	struct hlist_node *node;
5992 	int ret, i;
5993 
5994 	/* Reset rss size that was stored when reconfiguring rss for
5995 	 * channel VSIs with non-power-of-2 queue count.
5996 	 */
5997 	vsi->current_rss_size = 0;
5998 
5999 	/* perform cleanup for channels if they exist */
6000 	if (list_empty(&vsi->ch_list))
6001 		return;
6002 
6003 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6004 		struct i40e_vsi *p_vsi;
6005 
6006 		list_del(&ch->list);
6007 		p_vsi = ch->parent_vsi;
6008 		if (!p_vsi || !ch->initialized) {
6009 			kfree(ch);
6010 			continue;
6011 		}
6012 		/* Reset queue contexts */
6013 		for (i = 0; i < ch->num_queue_pairs; i++) {
6014 			struct i40e_ring *tx_ring, *rx_ring;
6015 			u16 pf_q;
6016 
6017 			pf_q = ch->base_queue + i;
6018 			tx_ring = vsi->tx_rings[pf_q];
6019 			tx_ring->ch = NULL;
6020 
6021 			rx_ring = vsi->rx_rings[pf_q];
6022 			rx_ring->ch = NULL;
6023 		}
6024 
6025 		/* Reset BW configured for this VSI via mqprio */
6026 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6027 		if (ret)
6028 			dev_info(&vsi->back->pdev->dev,
6029 				 "Failed to reset tx rate for ch->seid %u\n",
6030 				 ch->seid);
6031 
6032 		/* delete cloud filters associated with this channel */
6033 		hlist_for_each_entry_safe(cfilter, node,
6034 					  &pf->cloud_filter_list, cloud_node) {
6035 			if (cfilter->seid != ch->seid)
6036 				continue;
6037 
6038 			hash_del(&cfilter->cloud_node);
6039 			if (cfilter->dst_port)
6040 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6041 									cfilter,
6042 									false);
6043 			else
6044 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6045 								false);
6046 			last_aq_status = pf->hw.aq.asq_last_status;
6047 			if (ret)
6048 				dev_info(&pf->pdev->dev,
6049 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6050 					 ERR_PTR(ret),
6051 					 i40e_aq_str(&pf->hw, last_aq_status));
6052 			kfree(cfilter);
6053 		}
6054 
6055 		/* delete VSI from FW */
6056 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6057 					     NULL);
6058 		if (ret)
6059 			dev_err(&vsi->back->pdev->dev,
6060 				"unable to remove channel (%d) for parent VSI(%d)\n",
6061 				ch->seid, p_vsi->seid);
6062 		kfree(ch);
6063 	}
6064 	INIT_LIST_HEAD(&vsi->ch_list);
6065 }
6066 
6067 /**
6068  * i40e_get_max_queues_for_channel
6069  * @vsi: ptr to VSI to which channels are associated with
6070  *
6071  * Helper function which returns max value among the queue counts set on the
6072  * channels/TCs created.
6073  **/
6074 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6075 {
6076 	struct i40e_channel *ch, *ch_tmp;
6077 	int max = 0;
6078 
6079 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6080 		if (!ch->initialized)
6081 			continue;
6082 		if (ch->num_queue_pairs > max)
6083 			max = ch->num_queue_pairs;
6084 	}
6085 
6086 	return max;
6087 }
6088 
6089 /**
6090  * i40e_validate_num_queues - validate num_queues w.r.t channel
6091  * @pf: ptr to PF device
6092  * @num_queues: number of queues
6093  * @vsi: the parent VSI
6094  * @reconfig_rss: indicates should the RSS be reconfigured or not
6095  *
6096  * This function validates number of queues in the context of new channel
6097  * which is being established and determines if RSS should be reconfigured
6098  * or not for parent VSI.
6099  **/
6100 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6101 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6102 {
6103 	int max_ch_queues;
6104 
6105 	if (!reconfig_rss)
6106 		return -EINVAL;
6107 
6108 	*reconfig_rss = false;
6109 	if (vsi->current_rss_size) {
6110 		if (num_queues > vsi->current_rss_size) {
6111 			dev_dbg(&pf->pdev->dev,
6112 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6113 				num_queues, vsi->current_rss_size);
6114 			return -EINVAL;
6115 		} else if ((num_queues < vsi->current_rss_size) &&
6116 			   (!is_power_of_2(num_queues))) {
6117 			dev_dbg(&pf->pdev->dev,
6118 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6119 				num_queues, vsi->current_rss_size);
6120 			return -EINVAL;
6121 		}
6122 	}
6123 
6124 	if (!is_power_of_2(num_queues)) {
6125 		/* Find the max num_queues configured for channel if channel
6126 		 * exist.
6127 		 * if channel exist, then enforce 'num_queues' to be more than
6128 		 * max ever queues configured for channel.
6129 		 */
6130 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6131 		if (num_queues < max_ch_queues) {
6132 			dev_dbg(&pf->pdev->dev,
6133 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6134 				num_queues, max_ch_queues);
6135 			return -EINVAL;
6136 		}
6137 		*reconfig_rss = true;
6138 	}
6139 
6140 	return 0;
6141 }
6142 
6143 /**
6144  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6145  * @vsi: the VSI being setup
6146  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6147  *
6148  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6149  **/
6150 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6151 {
6152 	struct i40e_pf *pf = vsi->back;
6153 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6154 	struct i40e_hw *hw = &pf->hw;
6155 	int local_rss_size;
6156 	u8 *lut;
6157 	int ret;
6158 
6159 	if (!vsi->rss_size)
6160 		return -EINVAL;
6161 
6162 	if (rss_size > vsi->rss_size)
6163 		return -EINVAL;
6164 
6165 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6166 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6167 	if (!lut)
6168 		return -ENOMEM;
6169 
6170 	/* Ignoring user configured lut if there is one */
6171 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6172 
6173 	/* Use user configured hash key if there is one, otherwise
6174 	 * use default.
6175 	 */
6176 	if (vsi->rss_hkey_user)
6177 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6178 	else
6179 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6180 
6181 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6182 	if (ret) {
6183 		dev_info(&pf->pdev->dev,
6184 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6185 			 ERR_PTR(ret),
6186 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6187 		kfree(lut);
6188 		return ret;
6189 	}
6190 	kfree(lut);
6191 
6192 	/* Do the update w.r.t. storing rss_size */
6193 	if (!vsi->orig_rss_size)
6194 		vsi->orig_rss_size = vsi->rss_size;
6195 	vsi->current_rss_size = local_rss_size;
6196 
6197 	return ret;
6198 }
6199 
6200 /**
6201  * i40e_channel_setup_queue_map - Setup a channel queue map
6202  * @pf: ptr to PF device
6203  * @ctxt: VSI context structure
6204  * @ch: ptr to channel structure
6205  *
6206  * Setup queue map for a specific channel
6207  **/
6208 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6209 					 struct i40e_vsi_context *ctxt,
6210 					 struct i40e_channel *ch)
6211 {
6212 	u16 qcount, qmap, sections = 0;
6213 	u8 offset = 0;
6214 	int pow;
6215 
6216 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6217 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6218 
6219 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6220 	ch->num_queue_pairs = qcount;
6221 
6222 	/* find the next higher power-of-2 of num queue pairs */
6223 	pow = ilog2(qcount);
6224 	if (!is_power_of_2(qcount))
6225 		pow++;
6226 
6227 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6228 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6229 
6230 	/* Setup queue TC[0].qmap for given VSI context */
6231 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6232 
6233 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6234 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6235 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6236 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6237 }
6238 
6239 /**
6240  * i40e_add_channel - add a channel by adding VSI
6241  * @pf: ptr to PF device
6242  * @uplink_seid: underlying HW switching element (VEB) ID
6243  * @ch: ptr to channel structure
6244  *
6245  * Add a channel (VSI) using add_vsi and queue_map
6246  **/
6247 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6248 			    struct i40e_channel *ch)
6249 {
6250 	struct i40e_hw *hw = &pf->hw;
6251 	struct i40e_vsi_context ctxt;
6252 	u8 enabled_tc = 0x1; /* TC0 enabled */
6253 	int ret;
6254 
6255 	if (ch->type != I40E_VSI_VMDQ2) {
6256 		dev_info(&pf->pdev->dev,
6257 			 "add new vsi failed, ch->type %d\n", ch->type);
6258 		return -EINVAL;
6259 	}
6260 
6261 	memset(&ctxt, 0, sizeof(ctxt));
6262 	ctxt.pf_num = hw->pf_id;
6263 	ctxt.vf_num = 0;
6264 	ctxt.uplink_seid = uplink_seid;
6265 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6266 	if (ch->type == I40E_VSI_VMDQ2)
6267 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6268 
6269 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6270 		ctxt.info.valid_sections |=
6271 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6272 		ctxt.info.switch_id =
6273 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6274 	}
6275 
6276 	/* Set queue map for a given VSI context */
6277 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6278 
6279 	/* Now time to create VSI */
6280 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6281 	if (ret) {
6282 		dev_info(&pf->pdev->dev,
6283 			 "add new vsi failed, err %pe aq_err %s\n",
6284 			 ERR_PTR(ret),
6285 			 i40e_aq_str(&pf->hw,
6286 				     pf->hw.aq.asq_last_status));
6287 		return -ENOENT;
6288 	}
6289 
6290 	/* Success, update channel, set enabled_tc only if the channel
6291 	 * is not a macvlan
6292 	 */
6293 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6294 	ch->seid = ctxt.seid;
6295 	ch->vsi_number = ctxt.vsi_number;
6296 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6297 
6298 	/* copy just the sections touched not the entire info
6299 	 * since not all sections are valid as returned by
6300 	 * update vsi params
6301 	 */
6302 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6303 	memcpy(&ch->info.queue_mapping,
6304 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6305 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6306 	       sizeof(ctxt.info.tc_mapping));
6307 
6308 	return 0;
6309 }
6310 
6311 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6312 				  u8 *bw_share)
6313 {
6314 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6315 	int ret;
6316 	int i;
6317 
6318 	memset(&bw_data, 0, sizeof(bw_data));
6319 	bw_data.tc_valid_bits = ch->enabled_tc;
6320 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6321 		bw_data.tc_bw_credits[i] = bw_share[i];
6322 
6323 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6324 				       &bw_data, NULL);
6325 	if (ret) {
6326 		dev_info(&vsi->back->pdev->dev,
6327 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6328 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6329 		return -EINVAL;
6330 	}
6331 
6332 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6333 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6334 
6335 	return 0;
6336 }
6337 
6338 /**
6339  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6340  * @pf: ptr to PF device
6341  * @vsi: the VSI being setup
6342  * @ch: ptr to channel structure
6343  *
6344  * Configure TX rings associated with channel (VSI) since queues are being
6345  * from parent VSI.
6346  **/
6347 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6348 				       struct i40e_vsi *vsi,
6349 				       struct i40e_channel *ch)
6350 {
6351 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6352 	int ret;
6353 	int i;
6354 
6355 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6356 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6357 		if (ch->enabled_tc & BIT(i))
6358 			bw_share[i] = 1;
6359 	}
6360 
6361 	/* configure BW for new VSI */
6362 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6363 	if (ret) {
6364 		dev_info(&vsi->back->pdev->dev,
6365 			 "Failed configuring TC map %d for channel (seid %u)\n",
6366 			 ch->enabled_tc, ch->seid);
6367 		return ret;
6368 	}
6369 
6370 	for (i = 0; i < ch->num_queue_pairs; i++) {
6371 		struct i40e_ring *tx_ring, *rx_ring;
6372 		u16 pf_q;
6373 
6374 		pf_q = ch->base_queue + i;
6375 
6376 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6377 		 * context
6378 		 */
6379 		tx_ring = vsi->tx_rings[pf_q];
6380 		tx_ring->ch = ch;
6381 
6382 		/* Get the RX ring ptr */
6383 		rx_ring = vsi->rx_rings[pf_q];
6384 		rx_ring->ch = ch;
6385 	}
6386 
6387 	return 0;
6388 }
6389 
6390 /**
6391  * i40e_setup_hw_channel - setup new channel
6392  * @pf: ptr to PF device
6393  * @vsi: the VSI being setup
6394  * @ch: ptr to channel structure
6395  * @uplink_seid: underlying HW switching element (VEB) ID
6396  * @type: type of channel to be created (VMDq2/VF)
6397  *
6398  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6399  * and configures TX rings accordingly
6400  **/
6401 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6402 					struct i40e_vsi *vsi,
6403 					struct i40e_channel *ch,
6404 					u16 uplink_seid, u8 type)
6405 {
6406 	int ret;
6407 
6408 	ch->initialized = false;
6409 	ch->base_queue = vsi->next_base_queue;
6410 	ch->type = type;
6411 
6412 	/* Proceed with creation of channel (VMDq2) VSI */
6413 	ret = i40e_add_channel(pf, uplink_seid, ch);
6414 	if (ret) {
6415 		dev_info(&pf->pdev->dev,
6416 			 "failed to add_channel using uplink_seid %u\n",
6417 			 uplink_seid);
6418 		return ret;
6419 	}
6420 
6421 	/* Mark the successful creation of channel */
6422 	ch->initialized = true;
6423 
6424 	/* Reconfigure TX queues using QTX_CTL register */
6425 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6426 	if (ret) {
6427 		dev_info(&pf->pdev->dev,
6428 			 "failed to configure TX rings for channel %u\n",
6429 			 ch->seid);
6430 		return ret;
6431 	}
6432 
6433 	/* update 'next_base_queue' */
6434 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6435 	dev_dbg(&pf->pdev->dev,
6436 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6437 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6438 		ch->num_queue_pairs,
6439 		vsi->next_base_queue);
6440 	return ret;
6441 }
6442 
6443 /**
6444  * i40e_setup_channel - setup new channel using uplink element
6445  * @pf: ptr to PF device
6446  * @vsi: pointer to the VSI to set up the channel within
6447  * @ch: ptr to channel structure
6448  *
6449  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6450  * and uplink switching element (uplink_seid)
6451  **/
6452 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6453 			       struct i40e_channel *ch)
6454 {
6455 	u8 vsi_type;
6456 	u16 seid;
6457 	int ret;
6458 
6459 	if (vsi->type == I40E_VSI_MAIN) {
6460 		vsi_type = I40E_VSI_VMDQ2;
6461 	} else {
6462 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6463 			vsi->type);
6464 		return false;
6465 	}
6466 
6467 	/* underlying switching element */
6468 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6469 
6470 	/* create channel (VSI), configure TX rings */
6471 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6472 	if (ret) {
6473 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6474 		return false;
6475 	}
6476 
6477 	return ch->initialized ? true : false;
6478 }
6479 
6480 /**
6481  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6482  * @vsi: ptr to VSI which has PF backing
6483  *
6484  * Sets up switch mode correctly if it needs to be changed and perform
6485  * what are allowed modes.
6486  **/
6487 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6488 {
6489 	u8 mode;
6490 	struct i40e_pf *pf = vsi->back;
6491 	struct i40e_hw *hw = &pf->hw;
6492 	int ret;
6493 
6494 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6495 	if (ret)
6496 		return -EINVAL;
6497 
6498 	if (hw->dev_caps.switch_mode) {
6499 		/* if switch mode is set, support mode2 (non-tunneled for
6500 		 * cloud filter) for now
6501 		 */
6502 		u32 switch_mode = hw->dev_caps.switch_mode &
6503 				  I40E_SWITCH_MODE_MASK;
6504 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6505 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6506 				return 0;
6507 			dev_err(&pf->pdev->dev,
6508 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6509 				hw->dev_caps.switch_mode);
6510 			return -EINVAL;
6511 		}
6512 	}
6513 
6514 	/* Set Bit 7 to be valid */
6515 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6516 
6517 	/* Set L4type for TCP support */
6518 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6519 
6520 	/* Set cloud filter mode */
6521 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6522 
6523 	/* Prep mode field for set_switch_config */
6524 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6525 					pf->last_sw_conf_valid_flags,
6526 					mode, NULL);
6527 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6528 		dev_err(&pf->pdev->dev,
6529 			"couldn't set switch config bits, err %pe aq_err %s\n",
6530 			ERR_PTR(ret),
6531 			i40e_aq_str(hw,
6532 				    hw->aq.asq_last_status));
6533 
6534 	return ret;
6535 }
6536 
6537 /**
6538  * i40e_create_queue_channel - function to create channel
6539  * @vsi: VSI to be configured
6540  * @ch: ptr to channel (it contains channel specific params)
6541  *
6542  * This function creates channel (VSI) using num_queues specified by user,
6543  * reconfigs RSS if needed.
6544  **/
6545 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6546 			      struct i40e_channel *ch)
6547 {
6548 	struct i40e_pf *pf = vsi->back;
6549 	bool reconfig_rss;
6550 	int err;
6551 
6552 	if (!ch)
6553 		return -EINVAL;
6554 
6555 	if (!ch->num_queue_pairs) {
6556 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6557 			ch->num_queue_pairs);
6558 		return -EINVAL;
6559 	}
6560 
6561 	/* validate user requested num_queues for channel */
6562 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6563 				       &reconfig_rss);
6564 	if (err) {
6565 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6566 			 ch->num_queue_pairs);
6567 		return -EINVAL;
6568 	}
6569 
6570 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6571 	 * VSI to be added switch to VEB mode.
6572 	 */
6573 
6574 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6575 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6576 
6577 		if (vsi->type == I40E_VSI_MAIN) {
6578 			if (i40e_is_tc_mqprio_enabled(pf))
6579 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6580 			else
6581 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6582 		}
6583 		/* now onwards for main VSI, number of queues will be value
6584 		 * of TC0's queue count
6585 		 */
6586 	}
6587 
6588 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6589 	 * it should be more than num_queues
6590 	 */
6591 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6592 		dev_dbg(&pf->pdev->dev,
6593 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6594 			vsi->cnt_q_avail, ch->num_queue_pairs);
6595 		return -EINVAL;
6596 	}
6597 
6598 	/* reconfig_rss only if vsi type is MAIN_VSI */
6599 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6600 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6601 		if (err) {
6602 			dev_info(&pf->pdev->dev,
6603 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6604 				 ch->num_queue_pairs);
6605 			return -EINVAL;
6606 		}
6607 	}
6608 
6609 	if (!i40e_setup_channel(pf, vsi, ch)) {
6610 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6611 		return -EINVAL;
6612 	}
6613 
6614 	dev_info(&pf->pdev->dev,
6615 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6616 		 ch->seid, ch->num_queue_pairs);
6617 
6618 	/* configure VSI for BW limit */
6619 	if (ch->max_tx_rate) {
6620 		u64 credits = ch->max_tx_rate;
6621 
6622 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6623 			return -EINVAL;
6624 
6625 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6626 		dev_dbg(&pf->pdev->dev,
6627 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6628 			ch->max_tx_rate,
6629 			credits,
6630 			ch->seid);
6631 	}
6632 
6633 	/* in case of VF, this will be main SRIOV VSI */
6634 	ch->parent_vsi = vsi;
6635 
6636 	/* and update main_vsi's count for queue_available to use */
6637 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6638 
6639 	return 0;
6640 }
6641 
6642 /**
6643  * i40e_configure_queue_channels - Add queue channel for the given TCs
6644  * @vsi: VSI to be configured
6645  *
6646  * Configures queue channel mapping to the given TCs
6647  **/
6648 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6649 {
6650 	struct i40e_channel *ch;
6651 	u64 max_rate = 0;
6652 	int ret = 0, i;
6653 
6654 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6655 	vsi->tc_seid_map[0] = vsi->seid;
6656 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6657 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6658 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6659 			if (!ch) {
6660 				ret = -ENOMEM;
6661 				goto err_free;
6662 			}
6663 
6664 			INIT_LIST_HEAD(&ch->list);
6665 			ch->num_queue_pairs =
6666 				vsi->tc_config.tc_info[i].qcount;
6667 			ch->base_queue =
6668 				vsi->tc_config.tc_info[i].qoffset;
6669 
6670 			/* Bandwidth limit through tc interface is in bytes/s,
6671 			 * change to Mbit/s
6672 			 */
6673 			max_rate = vsi->mqprio_qopt.max_rate[i];
6674 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6675 			ch->max_tx_rate = max_rate;
6676 
6677 			list_add_tail(&ch->list, &vsi->ch_list);
6678 
6679 			ret = i40e_create_queue_channel(vsi, ch);
6680 			if (ret) {
6681 				dev_err(&vsi->back->pdev->dev,
6682 					"Failed creating queue channel with TC%d: queues %d\n",
6683 					i, ch->num_queue_pairs);
6684 				goto err_free;
6685 			}
6686 			vsi->tc_seid_map[i] = ch->seid;
6687 		}
6688 	}
6689 
6690 	/* reset to reconfigure TX queue contexts */
6691 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6692 	return ret;
6693 
6694 err_free:
6695 	i40e_remove_queue_channels(vsi);
6696 	return ret;
6697 }
6698 
6699 /**
6700  * i40e_veb_config_tc - Configure TCs for given VEB
6701  * @veb: given VEB
6702  * @enabled_tc: TC bitmap
6703  *
6704  * Configures given TC bitmap for VEB (switching) element
6705  **/
6706 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6707 {
6708 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6709 	struct i40e_pf *pf = veb->pf;
6710 	int ret = 0;
6711 	int i;
6712 
6713 	/* No TCs or already enabled TCs just return */
6714 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6715 		return ret;
6716 
6717 	bw_data.tc_valid_bits = enabled_tc;
6718 	/* bw_data.absolute_credits is not set (relative) */
6719 
6720 	/* Enable ETS TCs with equal BW Share for now */
6721 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6722 		if (enabled_tc & BIT(i))
6723 			bw_data.tc_bw_share_credits[i] = 1;
6724 	}
6725 
6726 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6727 						   &bw_data, NULL);
6728 	if (ret) {
6729 		dev_info(&pf->pdev->dev,
6730 			 "VEB bw config failed, err %pe aq_err %s\n",
6731 			 ERR_PTR(ret),
6732 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6733 		goto out;
6734 	}
6735 
6736 	/* Update the BW information */
6737 	ret = i40e_veb_get_bw_info(veb);
6738 	if (ret) {
6739 		dev_info(&pf->pdev->dev,
6740 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6741 			 ERR_PTR(ret),
6742 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6743 	}
6744 
6745 out:
6746 	return ret;
6747 }
6748 
6749 #ifdef CONFIG_I40E_DCB
6750 /**
6751  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6752  * @pf: PF struct
6753  *
6754  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6755  * the caller would've quiesce all the VSIs before calling
6756  * this function
6757  **/
6758 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6759 {
6760 	u8 tc_map = 0;
6761 	int ret;
6762 	u8 v;
6763 
6764 	/* Enable the TCs available on PF to all VEBs */
6765 	tc_map = i40e_pf_get_tc_map(pf);
6766 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6767 		return;
6768 
6769 	for (v = 0; v < I40E_MAX_VEB; v++) {
6770 		if (!pf->veb[v])
6771 			continue;
6772 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6773 		if (ret) {
6774 			dev_info(&pf->pdev->dev,
6775 				 "Failed configuring TC for VEB seid=%d\n",
6776 				 pf->veb[v]->seid);
6777 			/* Will try to configure as many components */
6778 		}
6779 	}
6780 
6781 	/* Update each VSI */
6782 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6783 		if (!pf->vsi[v])
6784 			continue;
6785 
6786 		/* - Enable all TCs for the LAN VSI
6787 		 * - For all others keep them at TC0 for now
6788 		 */
6789 		if (v == pf->lan_vsi)
6790 			tc_map = i40e_pf_get_tc_map(pf);
6791 		else
6792 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6793 
6794 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6795 		if (ret) {
6796 			dev_info(&pf->pdev->dev,
6797 				 "Failed configuring TC for VSI seid=%d\n",
6798 				 pf->vsi[v]->seid);
6799 			/* Will try to configure as many components */
6800 		} else {
6801 			/* Re-configure VSI vectors based on updated TC map */
6802 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6803 			if (pf->vsi[v]->netdev)
6804 				i40e_dcbnl_set_all(pf->vsi[v]);
6805 		}
6806 	}
6807 }
6808 
6809 /**
6810  * i40e_resume_port_tx - Resume port Tx
6811  * @pf: PF struct
6812  *
6813  * Resume a port's Tx and issue a PF reset in case of failure to
6814  * resume.
6815  **/
6816 static int i40e_resume_port_tx(struct i40e_pf *pf)
6817 {
6818 	struct i40e_hw *hw = &pf->hw;
6819 	int ret;
6820 
6821 	ret = i40e_aq_resume_port_tx(hw, NULL);
6822 	if (ret) {
6823 		dev_info(&pf->pdev->dev,
6824 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6825 			  ERR_PTR(ret),
6826 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6827 		/* Schedule PF reset to recover */
6828 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6829 		i40e_service_event_schedule(pf);
6830 	}
6831 
6832 	return ret;
6833 }
6834 
6835 /**
6836  * i40e_suspend_port_tx - Suspend port Tx
6837  * @pf: PF struct
6838  *
6839  * Suspend a port's Tx and issue a PF reset in case of failure.
6840  **/
6841 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6842 {
6843 	struct i40e_hw *hw = &pf->hw;
6844 	int ret;
6845 
6846 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6847 	if (ret) {
6848 		dev_info(&pf->pdev->dev,
6849 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6850 			 ERR_PTR(ret),
6851 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6852 		/* Schedule PF reset to recover */
6853 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6854 		i40e_service_event_schedule(pf);
6855 	}
6856 
6857 	return ret;
6858 }
6859 
6860 /**
6861  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6862  * @pf: PF being configured
6863  * @new_cfg: New DCBX configuration
6864  *
6865  * Program DCB settings into HW and reconfigure VEB/VSIs on
6866  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6867  **/
6868 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6869 				  struct i40e_dcbx_config *new_cfg)
6870 {
6871 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6872 	int ret;
6873 
6874 	/* Check if need reconfiguration */
6875 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6876 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6877 		return 0;
6878 	}
6879 
6880 	/* Config change disable all VSIs */
6881 	i40e_pf_quiesce_all_vsi(pf);
6882 
6883 	/* Copy the new config to the current config */
6884 	*old_cfg = *new_cfg;
6885 	old_cfg->etsrec = old_cfg->etscfg;
6886 	ret = i40e_set_dcb_config(&pf->hw);
6887 	if (ret) {
6888 		dev_info(&pf->pdev->dev,
6889 			 "Set DCB Config failed, err %pe aq_err %s\n",
6890 			 ERR_PTR(ret),
6891 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6892 		goto out;
6893 	}
6894 
6895 	/* Changes in configuration update VEB/VSI */
6896 	i40e_dcb_reconfigure(pf);
6897 out:
6898 	/* In case of reset do not try to resume anything */
6899 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6900 		/* Re-start the VSIs if disabled */
6901 		ret = i40e_resume_port_tx(pf);
6902 		/* In case of error no point in resuming VSIs */
6903 		if (ret)
6904 			goto err;
6905 		i40e_pf_unquiesce_all_vsi(pf);
6906 	}
6907 err:
6908 	return ret;
6909 }
6910 
6911 /**
6912  * i40e_hw_dcb_config - Program new DCBX settings into HW
6913  * @pf: PF being configured
6914  * @new_cfg: New DCBX configuration
6915  *
6916  * Program DCB settings into HW and reconfigure VEB/VSIs on
6917  * given PF
6918  **/
6919 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6920 {
6921 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6922 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6923 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6924 	struct i40e_dcbx_config *old_cfg;
6925 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6926 	struct i40e_rx_pb_config pb_cfg;
6927 	struct i40e_hw *hw = &pf->hw;
6928 	u8 num_ports = hw->num_ports;
6929 	bool need_reconfig;
6930 	int ret = -EINVAL;
6931 	u8 lltc_map = 0;
6932 	u8 tc_map = 0;
6933 	u8 new_numtc;
6934 	u8 i;
6935 
6936 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6937 	/* Un-pack information to Program ETS HW via shared API
6938 	 * numtc, tcmap
6939 	 * LLTC map
6940 	 * ETS/NON-ETS arbiter mode
6941 	 * max exponent (credit refills)
6942 	 * Total number of ports
6943 	 * PFC priority bit-map
6944 	 * Priority Table
6945 	 * BW % per TC
6946 	 * Arbiter mode between UPs sharing same TC
6947 	 * TSA table (ETS or non-ETS)
6948 	 * EEE enabled or not
6949 	 * MFS TC table
6950 	 */
6951 
6952 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6953 
6954 	memset(&ets_data, 0, sizeof(ets_data));
6955 	for (i = 0; i < new_numtc; i++) {
6956 		tc_map |= BIT(i);
6957 		switch (new_cfg->etscfg.tsatable[i]) {
6958 		case I40E_IEEE_TSA_ETS:
6959 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6960 			ets_data.tc_bw_share_credits[i] =
6961 					new_cfg->etscfg.tcbwtable[i];
6962 			break;
6963 		case I40E_IEEE_TSA_STRICT:
6964 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6965 			lltc_map |= BIT(i);
6966 			ets_data.tc_bw_share_credits[i] =
6967 					I40E_DCB_STRICT_PRIO_CREDITS;
6968 			break;
6969 		default:
6970 			/* Invalid TSA type */
6971 			need_reconfig = false;
6972 			goto out;
6973 		}
6974 	}
6975 
6976 	old_cfg = &hw->local_dcbx_config;
6977 	/* Check if need reconfiguration */
6978 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6979 
6980 	/* If needed, enable/disable frame tagging, disable all VSIs
6981 	 * and suspend port tx
6982 	 */
6983 	if (need_reconfig) {
6984 		/* Enable DCB tagging only when more than one TC */
6985 		if (new_numtc > 1)
6986 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6987 		else
6988 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6989 
6990 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6991 		/* Reconfiguration needed quiesce all VSIs */
6992 		i40e_pf_quiesce_all_vsi(pf);
6993 		ret = i40e_suspend_port_tx(pf);
6994 		if (ret)
6995 			goto err;
6996 	}
6997 
6998 	/* Configure Port ETS Tx Scheduler */
6999 	ets_data.tc_valid_bits = tc_map;
7000 	ets_data.tc_strict_priority_flags = lltc_map;
7001 	ret = i40e_aq_config_switch_comp_ets
7002 		(hw, pf->mac_seid, &ets_data,
7003 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7004 	if (ret) {
7005 		dev_info(&pf->pdev->dev,
7006 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7007 			 ERR_PTR(ret),
7008 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7009 		goto out;
7010 	}
7011 
7012 	/* Configure Rx ETS HW */
7013 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7014 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7015 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7016 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7017 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7018 				   lltc_map);
7019 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7020 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7021 				     prio_type);
7022 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7023 			       new_cfg->etscfg.prioritytable);
7024 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7025 
7026 	/* Configure Rx Packet Buffers in HW */
7027 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7028 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7029 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7030 	}
7031 
7032 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7033 					 false, new_cfg->pfc.pfcenable,
7034 					 mfs_tc, &pb_cfg);
7035 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7036 
7037 	/* Update the local Rx Packet buffer config */
7038 	pf->pb_cfg = pb_cfg;
7039 
7040 	/* Inform the FW about changes to DCB configuration */
7041 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7042 	if (ret) {
7043 		dev_info(&pf->pdev->dev,
7044 			 "DCB Updated failed, err %pe aq_err %s\n",
7045 			 ERR_PTR(ret),
7046 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7047 		goto out;
7048 	}
7049 
7050 	/* Update the port DCBx configuration */
7051 	*old_cfg = *new_cfg;
7052 
7053 	/* Changes in configuration update VEB/VSI */
7054 	i40e_dcb_reconfigure(pf);
7055 out:
7056 	/* Re-start the VSIs if disabled */
7057 	if (need_reconfig) {
7058 		ret = i40e_resume_port_tx(pf);
7059 
7060 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7061 		/* In case of error no point in resuming VSIs */
7062 		if (ret)
7063 			goto err;
7064 
7065 		/* Wait for the PF's queues to be disabled */
7066 		ret = i40e_pf_wait_queues_disabled(pf);
7067 		if (ret) {
7068 			/* Schedule PF reset to recover */
7069 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7070 			i40e_service_event_schedule(pf);
7071 			goto err;
7072 		} else {
7073 			i40e_pf_unquiesce_all_vsi(pf);
7074 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7075 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7076 		}
7077 		/* registers are set, lets apply */
7078 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7079 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7080 	}
7081 
7082 err:
7083 	return ret;
7084 }
7085 
7086 /**
7087  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7088  * @pf: PF being queried
7089  *
7090  * Set default DCB configuration in case DCB is to be done in SW.
7091  **/
7092 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7093 {
7094 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7095 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7096 	struct i40e_hw *hw = &pf->hw;
7097 	int err;
7098 
7099 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7100 		/* Update the local cached instance with TC0 ETS */
7101 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7102 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7103 		pf->tmp_cfg.etscfg.maxtcs = 0;
7104 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7105 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7106 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7107 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7108 		/* FW needs one App to configure HW */
7109 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7110 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7111 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7112 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7113 
7114 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7115 	}
7116 
7117 	memset(&ets_data, 0, sizeof(ets_data));
7118 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7119 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7120 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7121 
7122 	/* Enable ETS on the Physical port */
7123 	err = i40e_aq_config_switch_comp_ets
7124 		(hw, pf->mac_seid, &ets_data,
7125 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7126 	if (err) {
7127 		dev_info(&pf->pdev->dev,
7128 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7129 			 ERR_PTR(err),
7130 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7131 		err = -ENOENT;
7132 		goto out;
7133 	}
7134 
7135 	/* Update the local cached instance with TC0 ETS */
7136 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7137 	dcb_cfg->etscfg.cbs = 0;
7138 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7139 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7140 
7141 out:
7142 	return err;
7143 }
7144 
7145 /**
7146  * i40e_init_pf_dcb - Initialize DCB configuration
7147  * @pf: PF being configured
7148  *
7149  * Query the current DCB configuration and cache it
7150  * in the hardware structure
7151  **/
7152 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7153 {
7154 	struct i40e_hw *hw = &pf->hw;
7155 	int err;
7156 
7157 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7158 	 * Also do not enable DCBx if FW LLDP agent is disabled
7159 	 */
7160 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7161 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7162 		err = I40E_NOT_SUPPORTED;
7163 		goto out;
7164 	}
7165 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7166 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7167 		err = i40e_dcb_sw_default_config(pf);
7168 		if (err) {
7169 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7170 			goto out;
7171 		}
7172 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7173 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7174 			       DCB_CAP_DCBX_VER_IEEE;
7175 		/* at init capable but disabled */
7176 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7177 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7178 		goto out;
7179 	}
7180 	err = i40e_init_dcb(hw, true);
7181 	if (!err) {
7182 		/* Device/Function is not DCBX capable */
7183 		if ((!hw->func_caps.dcb) ||
7184 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7185 			dev_info(&pf->pdev->dev,
7186 				 "DCBX offload is not supported or is disabled for this PF.\n");
7187 		} else {
7188 			/* When status is not DISABLED then DCBX in FW */
7189 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7190 				       DCB_CAP_DCBX_VER_IEEE;
7191 
7192 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7193 			/* Enable DCB tagging only when more than one TC
7194 			 * or explicitly disable if only one TC
7195 			 */
7196 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7197 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7198 			else
7199 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7200 			dev_dbg(&pf->pdev->dev,
7201 				"DCBX offload is supported for this PF.\n");
7202 		}
7203 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7204 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7205 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7206 	} else {
7207 		dev_info(&pf->pdev->dev,
7208 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7209 			 ERR_PTR(err),
7210 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7211 	}
7212 
7213 out:
7214 	return err;
7215 }
7216 #endif /* CONFIG_I40E_DCB */
7217 
7218 /**
7219  * i40e_print_link_message - print link up or down
7220  * @vsi: the VSI for which link needs a message
7221  * @isup: true of link is up, false otherwise
7222  */
7223 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7224 {
7225 	enum i40e_aq_link_speed new_speed;
7226 	struct i40e_pf *pf = vsi->back;
7227 	char *speed = "Unknown";
7228 	char *fc = "Unknown";
7229 	char *fec = "";
7230 	char *req_fec = "";
7231 	char *an = "";
7232 
7233 	if (isup)
7234 		new_speed = pf->hw.phy.link_info.link_speed;
7235 	else
7236 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7237 
7238 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7239 		return;
7240 	vsi->current_isup = isup;
7241 	vsi->current_speed = new_speed;
7242 	if (!isup) {
7243 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7244 		return;
7245 	}
7246 
7247 	/* Warn user if link speed on NPAR enabled partition is not at
7248 	 * least 10GB
7249 	 */
7250 	if (pf->hw.func_caps.npar_enable &&
7251 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7252 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7253 		netdev_warn(vsi->netdev,
7254 			    "The partition detected link speed that is less than 10Gbps\n");
7255 
7256 	switch (pf->hw.phy.link_info.link_speed) {
7257 	case I40E_LINK_SPEED_40GB:
7258 		speed = "40 G";
7259 		break;
7260 	case I40E_LINK_SPEED_20GB:
7261 		speed = "20 G";
7262 		break;
7263 	case I40E_LINK_SPEED_25GB:
7264 		speed = "25 G";
7265 		break;
7266 	case I40E_LINK_SPEED_10GB:
7267 		speed = "10 G";
7268 		break;
7269 	case I40E_LINK_SPEED_5GB:
7270 		speed = "5 G";
7271 		break;
7272 	case I40E_LINK_SPEED_2_5GB:
7273 		speed = "2.5 G";
7274 		break;
7275 	case I40E_LINK_SPEED_1GB:
7276 		speed = "1000 M";
7277 		break;
7278 	case I40E_LINK_SPEED_100MB:
7279 		speed = "100 M";
7280 		break;
7281 	default:
7282 		break;
7283 	}
7284 
7285 	switch (pf->hw.fc.current_mode) {
7286 	case I40E_FC_FULL:
7287 		fc = "RX/TX";
7288 		break;
7289 	case I40E_FC_TX_PAUSE:
7290 		fc = "TX";
7291 		break;
7292 	case I40E_FC_RX_PAUSE:
7293 		fc = "RX";
7294 		break;
7295 	default:
7296 		fc = "None";
7297 		break;
7298 	}
7299 
7300 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7301 		req_fec = "None";
7302 		fec = "None";
7303 		an = "False";
7304 
7305 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7306 			an = "True";
7307 
7308 		if (pf->hw.phy.link_info.fec_info &
7309 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7310 			fec = "CL74 FC-FEC/BASE-R";
7311 		else if (pf->hw.phy.link_info.fec_info &
7312 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7313 			fec = "CL108 RS-FEC";
7314 
7315 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7316 		 * both RS and FC are requested
7317 		 */
7318 		if (vsi->back->hw.phy.link_info.req_fec_info &
7319 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7320 			if (vsi->back->hw.phy.link_info.req_fec_info &
7321 			    I40E_AQ_REQUEST_FEC_RS)
7322 				req_fec = "CL108 RS-FEC";
7323 			else
7324 				req_fec = "CL74 FC-FEC/BASE-R";
7325 		}
7326 		netdev_info(vsi->netdev,
7327 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7328 			    speed, req_fec, fec, an, fc);
7329 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7330 		req_fec = "None";
7331 		fec = "None";
7332 		an = "False";
7333 
7334 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7335 			an = "True";
7336 
7337 		if (pf->hw.phy.link_info.fec_info &
7338 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7339 			fec = "CL74 FC-FEC/BASE-R";
7340 
7341 		if (pf->hw.phy.link_info.req_fec_info &
7342 		    I40E_AQ_REQUEST_FEC_KR)
7343 			req_fec = "CL74 FC-FEC/BASE-R";
7344 
7345 		netdev_info(vsi->netdev,
7346 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7347 			    speed, req_fec, fec, an, fc);
7348 	} else {
7349 		netdev_info(vsi->netdev,
7350 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7351 			    speed, fc);
7352 	}
7353 
7354 }
7355 
7356 /**
7357  * i40e_up_complete - Finish the last steps of bringing up a connection
7358  * @vsi: the VSI being configured
7359  **/
7360 static int i40e_up_complete(struct i40e_vsi *vsi)
7361 {
7362 	struct i40e_pf *pf = vsi->back;
7363 	int err;
7364 
7365 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7366 		i40e_vsi_configure_msix(vsi);
7367 	else
7368 		i40e_configure_msi_and_legacy(vsi);
7369 
7370 	/* start rings */
7371 	err = i40e_vsi_start_rings(vsi);
7372 	if (err)
7373 		return err;
7374 
7375 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7376 	i40e_napi_enable_all(vsi);
7377 	i40e_vsi_enable_irq(vsi);
7378 
7379 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7380 	    (vsi->netdev)) {
7381 		i40e_print_link_message(vsi, true);
7382 		netif_tx_start_all_queues(vsi->netdev);
7383 		netif_carrier_on(vsi->netdev);
7384 	}
7385 
7386 	/* replay FDIR SB filters */
7387 	if (vsi->type == I40E_VSI_FDIR) {
7388 		/* reset fd counters */
7389 		pf->fd_add_err = 0;
7390 		pf->fd_atr_cnt = 0;
7391 		i40e_fdir_filter_restore(vsi);
7392 	}
7393 
7394 	/* On the next run of the service_task, notify any clients of the new
7395 	 * opened netdev
7396 	 */
7397 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7398 	i40e_service_event_schedule(pf);
7399 
7400 	return 0;
7401 }
7402 
7403 /**
7404  * i40e_vsi_reinit_locked - Reset the VSI
7405  * @vsi: the VSI being configured
7406  *
7407  * Rebuild the ring structs after some configuration
7408  * has changed, e.g. MTU size.
7409  **/
7410 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7411 {
7412 	struct i40e_pf *pf = vsi->back;
7413 
7414 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7415 		usleep_range(1000, 2000);
7416 	i40e_down(vsi);
7417 
7418 	i40e_up(vsi);
7419 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7420 }
7421 
7422 /**
7423  * i40e_force_link_state - Force the link status
7424  * @pf: board private structure
7425  * @is_up: whether the link state should be forced up or down
7426  **/
7427 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7428 {
7429 	struct i40e_aq_get_phy_abilities_resp abilities;
7430 	struct i40e_aq_set_phy_config config = {0};
7431 	bool non_zero_phy_type = is_up;
7432 	struct i40e_hw *hw = &pf->hw;
7433 	u64 mask;
7434 	u8 speed;
7435 	int err;
7436 
7437 	/* Card might've been put in an unstable state by other drivers
7438 	 * and applications, which causes incorrect speed values being
7439 	 * set on startup. In order to clear speed registers, we call
7440 	 * get_phy_capabilities twice, once to get initial state of
7441 	 * available speeds, and once to get current PHY config.
7442 	 */
7443 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7444 					   NULL);
7445 	if (err) {
7446 		dev_err(&pf->pdev->dev,
7447 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7448 			ERR_PTR(err),
7449 			i40e_aq_str(hw, hw->aq.asq_last_status));
7450 		return err;
7451 	}
7452 	speed = abilities.link_speed;
7453 
7454 	/* Get the current phy config */
7455 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7456 					   NULL);
7457 	if (err) {
7458 		dev_err(&pf->pdev->dev,
7459 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7460 			ERR_PTR(err),
7461 			i40e_aq_str(hw, hw->aq.asq_last_status));
7462 		return err;
7463 	}
7464 
7465 	/* If link needs to go up, but was not forced to go down,
7466 	 * and its speed values are OK, no need for a flap
7467 	 * if non_zero_phy_type was set, still need to force up
7468 	 */
7469 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7470 		non_zero_phy_type = true;
7471 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7472 		return I40E_SUCCESS;
7473 
7474 	/* To force link we need to set bits for all supported PHY types,
7475 	 * but there are now more than 32, so we need to split the bitmap
7476 	 * across two fields.
7477 	 */
7478 	mask = I40E_PHY_TYPES_BITMASK;
7479 	config.phy_type =
7480 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7481 	config.phy_type_ext =
7482 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7483 	/* Copy the old settings, except of phy_type */
7484 	config.abilities = abilities.abilities;
7485 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7486 		if (is_up)
7487 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7488 		else
7489 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7490 	}
7491 	if (abilities.link_speed != 0)
7492 		config.link_speed = abilities.link_speed;
7493 	else
7494 		config.link_speed = speed;
7495 	config.eee_capability = abilities.eee_capability;
7496 	config.eeer = abilities.eeer_val;
7497 	config.low_power_ctrl = abilities.d3_lpan;
7498 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7499 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7500 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7501 
7502 	if (err) {
7503 		dev_err(&pf->pdev->dev,
7504 			"set phy config ret =  %pe last_status =  %s\n",
7505 			ERR_PTR(err),
7506 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7507 		return err;
7508 	}
7509 
7510 	/* Update the link info */
7511 	err = i40e_update_link_info(hw);
7512 	if (err) {
7513 		/* Wait a little bit (on 40G cards it sometimes takes a really
7514 		 * long time for link to come back from the atomic reset)
7515 		 * and try once more
7516 		 */
7517 		msleep(1000);
7518 		i40e_update_link_info(hw);
7519 	}
7520 
7521 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7522 
7523 	return I40E_SUCCESS;
7524 }
7525 
7526 /**
7527  * i40e_up - Bring the connection back up after being down
7528  * @vsi: the VSI being configured
7529  **/
7530 int i40e_up(struct i40e_vsi *vsi)
7531 {
7532 	int err;
7533 
7534 	if (vsi->type == I40E_VSI_MAIN &&
7535 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7536 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7537 		i40e_force_link_state(vsi->back, true);
7538 
7539 	err = i40e_vsi_configure(vsi);
7540 	if (!err)
7541 		err = i40e_up_complete(vsi);
7542 
7543 	return err;
7544 }
7545 
7546 /**
7547  * i40e_down - Shutdown the connection processing
7548  * @vsi: the VSI being stopped
7549  **/
7550 void i40e_down(struct i40e_vsi *vsi)
7551 {
7552 	int i;
7553 
7554 	/* It is assumed that the caller of this function
7555 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7556 	 */
7557 	if (vsi->netdev) {
7558 		netif_carrier_off(vsi->netdev);
7559 		netif_tx_disable(vsi->netdev);
7560 	}
7561 	i40e_vsi_disable_irq(vsi);
7562 	i40e_vsi_stop_rings(vsi);
7563 	if (vsi->type == I40E_VSI_MAIN &&
7564 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7565 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7566 		i40e_force_link_state(vsi->back, false);
7567 	i40e_napi_disable_all(vsi);
7568 
7569 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7570 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7571 		if (i40e_enabled_xdp_vsi(vsi)) {
7572 			/* Make sure that in-progress ndo_xdp_xmit and
7573 			 * ndo_xsk_wakeup calls are completed.
7574 			 */
7575 			synchronize_rcu();
7576 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7577 		}
7578 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7579 	}
7580 
7581 }
7582 
7583 /**
7584  * i40e_validate_mqprio_qopt- validate queue mapping info
7585  * @vsi: the VSI being configured
7586  * @mqprio_qopt: queue parametrs
7587  **/
7588 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7589 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7590 {
7591 	u64 sum_max_rate = 0;
7592 	u64 max_rate = 0;
7593 	int i;
7594 
7595 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7596 	    mqprio_qopt->qopt.num_tc < 1 ||
7597 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7598 		return -EINVAL;
7599 	for (i = 0; ; i++) {
7600 		if (!mqprio_qopt->qopt.count[i])
7601 			return -EINVAL;
7602 		if (mqprio_qopt->min_rate[i]) {
7603 			dev_err(&vsi->back->pdev->dev,
7604 				"Invalid min tx rate (greater than 0) specified\n");
7605 			return -EINVAL;
7606 		}
7607 		max_rate = mqprio_qopt->max_rate[i];
7608 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7609 		sum_max_rate += max_rate;
7610 
7611 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7612 			break;
7613 		if (mqprio_qopt->qopt.offset[i + 1] !=
7614 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7615 			return -EINVAL;
7616 	}
7617 	if (vsi->num_queue_pairs <
7618 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7619 		dev_err(&vsi->back->pdev->dev,
7620 			"Failed to create traffic channel, insufficient number of queues.\n");
7621 		return -EINVAL;
7622 	}
7623 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7624 		dev_err(&vsi->back->pdev->dev,
7625 			"Invalid max tx rate specified\n");
7626 		return -EINVAL;
7627 	}
7628 	return 0;
7629 }
7630 
7631 /**
7632  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7633  * @vsi: the VSI being configured
7634  **/
7635 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7636 {
7637 	u16 qcount;
7638 	int i;
7639 
7640 	/* Only TC0 is enabled */
7641 	vsi->tc_config.numtc = 1;
7642 	vsi->tc_config.enabled_tc = 1;
7643 	qcount = min_t(int, vsi->alloc_queue_pairs,
7644 		       i40e_pf_get_max_q_per_tc(vsi->back));
7645 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7646 		/* For the TC that is not enabled set the offset to default
7647 		 * queue and allocate one queue for the given TC.
7648 		 */
7649 		vsi->tc_config.tc_info[i].qoffset = 0;
7650 		if (i == 0)
7651 			vsi->tc_config.tc_info[i].qcount = qcount;
7652 		else
7653 			vsi->tc_config.tc_info[i].qcount = 1;
7654 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7655 	}
7656 }
7657 
7658 /**
7659  * i40e_del_macvlan_filter
7660  * @hw: pointer to the HW structure
7661  * @seid: seid of the channel VSI
7662  * @macaddr: the mac address to apply as a filter
7663  * @aq_err: store the admin Q error
7664  *
7665  * This function deletes a mac filter on the channel VSI which serves as the
7666  * macvlan. Returns 0 on success.
7667  **/
7668 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7669 				   const u8 *macaddr, int *aq_err)
7670 {
7671 	struct i40e_aqc_remove_macvlan_element_data element;
7672 	int status;
7673 
7674 	memset(&element, 0, sizeof(element));
7675 	ether_addr_copy(element.mac_addr, macaddr);
7676 	element.vlan_tag = 0;
7677 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7678 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7679 	*aq_err = hw->aq.asq_last_status;
7680 
7681 	return status;
7682 }
7683 
7684 /**
7685  * i40e_add_macvlan_filter
7686  * @hw: pointer to the HW structure
7687  * @seid: seid of the channel VSI
7688  * @macaddr: the mac address to apply as a filter
7689  * @aq_err: store the admin Q error
7690  *
7691  * This function adds a mac filter on the channel VSI which serves as the
7692  * macvlan. Returns 0 on success.
7693  **/
7694 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7695 				   const u8 *macaddr, int *aq_err)
7696 {
7697 	struct i40e_aqc_add_macvlan_element_data element;
7698 	u16 cmd_flags = 0;
7699 	int status;
7700 
7701 	ether_addr_copy(element.mac_addr, macaddr);
7702 	element.vlan_tag = 0;
7703 	element.queue_number = 0;
7704 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7705 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7706 	element.flags = cpu_to_le16(cmd_flags);
7707 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7708 	*aq_err = hw->aq.asq_last_status;
7709 
7710 	return status;
7711 }
7712 
7713 /**
7714  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7715  * @vsi: the VSI we want to access
7716  * @ch: the channel we want to access
7717  */
7718 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7719 {
7720 	struct i40e_ring *tx_ring, *rx_ring;
7721 	u16 pf_q;
7722 	int i;
7723 
7724 	for (i = 0; i < ch->num_queue_pairs; i++) {
7725 		pf_q = ch->base_queue + i;
7726 		tx_ring = vsi->tx_rings[pf_q];
7727 		tx_ring->ch = NULL;
7728 		rx_ring = vsi->rx_rings[pf_q];
7729 		rx_ring->ch = NULL;
7730 	}
7731 }
7732 
7733 /**
7734  * i40e_free_macvlan_channels
7735  * @vsi: the VSI we want to access
7736  *
7737  * This function frees the Qs of the channel VSI from
7738  * the stack and also deletes the channel VSIs which
7739  * serve as macvlans.
7740  */
7741 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7742 {
7743 	struct i40e_channel *ch, *ch_tmp;
7744 	int ret;
7745 
7746 	if (list_empty(&vsi->macvlan_list))
7747 		return;
7748 
7749 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7750 		struct i40e_vsi *parent_vsi;
7751 
7752 		if (i40e_is_channel_macvlan(ch)) {
7753 			i40e_reset_ch_rings(vsi, ch);
7754 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7755 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7756 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7757 			kfree(ch->fwd);
7758 			ch->fwd = NULL;
7759 		}
7760 
7761 		list_del(&ch->list);
7762 		parent_vsi = ch->parent_vsi;
7763 		if (!parent_vsi || !ch->initialized) {
7764 			kfree(ch);
7765 			continue;
7766 		}
7767 
7768 		/* remove the VSI */
7769 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7770 					     NULL);
7771 		if (ret)
7772 			dev_err(&vsi->back->pdev->dev,
7773 				"unable to remove channel (%d) for parent VSI(%d)\n",
7774 				ch->seid, parent_vsi->seid);
7775 		kfree(ch);
7776 	}
7777 	vsi->macvlan_cnt = 0;
7778 }
7779 
7780 /**
7781  * i40e_fwd_ring_up - bring the macvlan device up
7782  * @vsi: the VSI we want to access
7783  * @vdev: macvlan netdevice
7784  * @fwd: the private fwd structure
7785  */
7786 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7787 			    struct i40e_fwd_adapter *fwd)
7788 {
7789 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7790 	int ret = 0, num_tc = 1,  i, aq_err;
7791 	struct i40e_pf *pf = vsi->back;
7792 	struct i40e_hw *hw = &pf->hw;
7793 
7794 	/* Go through the list and find an available channel */
7795 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7796 		if (!i40e_is_channel_macvlan(iter)) {
7797 			iter->fwd = fwd;
7798 			/* record configuration for macvlan interface in vdev */
7799 			for (i = 0; i < num_tc; i++)
7800 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7801 							     i,
7802 							     iter->num_queue_pairs,
7803 							     iter->base_queue);
7804 			for (i = 0; i < iter->num_queue_pairs; i++) {
7805 				struct i40e_ring *tx_ring, *rx_ring;
7806 				u16 pf_q;
7807 
7808 				pf_q = iter->base_queue + i;
7809 
7810 				/* Get to TX ring ptr */
7811 				tx_ring = vsi->tx_rings[pf_q];
7812 				tx_ring->ch = iter;
7813 
7814 				/* Get the RX ring ptr */
7815 				rx_ring = vsi->rx_rings[pf_q];
7816 				rx_ring->ch = iter;
7817 			}
7818 			ch = iter;
7819 			break;
7820 		}
7821 	}
7822 
7823 	if (!ch)
7824 		return -EINVAL;
7825 
7826 	/* Guarantee all rings are updated before we update the
7827 	 * MAC address filter.
7828 	 */
7829 	wmb();
7830 
7831 	/* Add a mac filter */
7832 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7833 	if (ret) {
7834 		/* if we cannot add the MAC rule then disable the offload */
7835 		macvlan_release_l2fw_offload(vdev);
7836 		for (i = 0; i < ch->num_queue_pairs; i++) {
7837 			struct i40e_ring *rx_ring;
7838 			u16 pf_q;
7839 
7840 			pf_q = ch->base_queue + i;
7841 			rx_ring = vsi->rx_rings[pf_q];
7842 			rx_ring->netdev = NULL;
7843 		}
7844 		dev_info(&pf->pdev->dev,
7845 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7846 			  ERR_PTR(ret),
7847 			  i40e_aq_str(hw, aq_err));
7848 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7849 	}
7850 
7851 	return ret;
7852 }
7853 
7854 /**
7855  * i40e_setup_macvlans - create the channels which will be macvlans
7856  * @vsi: the VSI we want to access
7857  * @macvlan_cnt: no. of macvlans to be setup
7858  * @qcnt: no. of Qs per macvlan
7859  * @vdev: macvlan netdevice
7860  */
7861 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7862 			       struct net_device *vdev)
7863 {
7864 	struct i40e_pf *pf = vsi->back;
7865 	struct i40e_hw *hw = &pf->hw;
7866 	struct i40e_vsi_context ctxt;
7867 	u16 sections, qmap, num_qps;
7868 	struct i40e_channel *ch;
7869 	int i, pow, ret = 0;
7870 	u8 offset = 0;
7871 
7872 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7873 		return -EINVAL;
7874 
7875 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7876 
7877 	/* find the next higher power-of-2 of num queue pairs */
7878 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7879 
7880 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7881 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7882 
7883 	/* Setup context bits for the main VSI */
7884 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7885 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7886 	memset(&ctxt, 0, sizeof(ctxt));
7887 	ctxt.seid = vsi->seid;
7888 	ctxt.pf_num = vsi->back->hw.pf_id;
7889 	ctxt.vf_num = 0;
7890 	ctxt.uplink_seid = vsi->uplink_seid;
7891 	ctxt.info = vsi->info;
7892 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7893 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7894 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7895 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7896 
7897 	/* Reconfigure RSS for main VSI with new max queue count */
7898 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7899 	ret = i40e_vsi_config_rss(vsi);
7900 	if (ret) {
7901 		dev_info(&pf->pdev->dev,
7902 			 "Failed to reconfig RSS for num_queues (%u)\n",
7903 			 vsi->rss_size);
7904 		return ret;
7905 	}
7906 	vsi->reconfig_rss = true;
7907 	dev_dbg(&vsi->back->pdev->dev,
7908 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7909 	vsi->next_base_queue = num_qps;
7910 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7911 
7912 	/* Update the VSI after updating the VSI queue-mapping
7913 	 * information
7914 	 */
7915 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7916 	if (ret) {
7917 		dev_info(&pf->pdev->dev,
7918 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7919 			 ERR_PTR(ret),
7920 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7921 		return ret;
7922 	}
7923 	/* update the local VSI info with updated queue map */
7924 	i40e_vsi_update_queue_map(vsi, &ctxt);
7925 	vsi->info.valid_sections = 0;
7926 
7927 	/* Create channels for macvlans */
7928 	INIT_LIST_HEAD(&vsi->macvlan_list);
7929 	for (i = 0; i < macvlan_cnt; i++) {
7930 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7931 		if (!ch) {
7932 			ret = -ENOMEM;
7933 			goto err_free;
7934 		}
7935 		INIT_LIST_HEAD(&ch->list);
7936 		ch->num_queue_pairs = qcnt;
7937 		if (!i40e_setup_channel(pf, vsi, ch)) {
7938 			ret = -EINVAL;
7939 			kfree(ch);
7940 			goto err_free;
7941 		}
7942 		ch->parent_vsi = vsi;
7943 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7944 		vsi->macvlan_cnt++;
7945 		list_add_tail(&ch->list, &vsi->macvlan_list);
7946 	}
7947 
7948 	return ret;
7949 
7950 err_free:
7951 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7952 	i40e_free_macvlan_channels(vsi);
7953 
7954 	return ret;
7955 }
7956 
7957 /**
7958  * i40e_fwd_add - configure macvlans
7959  * @netdev: net device to configure
7960  * @vdev: macvlan netdevice
7961  **/
7962 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7963 {
7964 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7965 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7966 	struct i40e_vsi *vsi = np->vsi;
7967 	struct i40e_pf *pf = vsi->back;
7968 	struct i40e_fwd_adapter *fwd;
7969 	int avail_macvlan, ret;
7970 
7971 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7972 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7973 		return ERR_PTR(-EINVAL);
7974 	}
7975 	if (i40e_is_tc_mqprio_enabled(pf)) {
7976 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7977 		return ERR_PTR(-EINVAL);
7978 	}
7979 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7980 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7981 		return ERR_PTR(-EINVAL);
7982 	}
7983 
7984 	/* The macvlan device has to be a single Q device so that the
7985 	 * tc_to_txq field can be reused to pick the tx queue.
7986 	 */
7987 	if (netif_is_multiqueue(vdev))
7988 		return ERR_PTR(-ERANGE);
7989 
7990 	if (!vsi->macvlan_cnt) {
7991 		/* reserve bit 0 for the pf device */
7992 		set_bit(0, vsi->fwd_bitmask);
7993 
7994 		/* Try to reserve as many queues as possible for macvlans. First
7995 		 * reserve 3/4th of max vectors, then half, then quarter and
7996 		 * calculate Qs per macvlan as you go
7997 		 */
7998 		vectors = pf->num_lan_msix;
7999 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8000 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8001 			q_per_macvlan = 4;
8002 			macvlan_cnt = (vectors - 32) / 4;
8003 		} else if (vectors <= 64 && vectors > 32) {
8004 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8005 			q_per_macvlan = 2;
8006 			macvlan_cnt = (vectors - 16) / 2;
8007 		} else if (vectors <= 32 && vectors > 16) {
8008 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8009 			q_per_macvlan = 1;
8010 			macvlan_cnt = vectors - 16;
8011 		} else if (vectors <= 16 && vectors > 8) {
8012 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8013 			q_per_macvlan = 1;
8014 			macvlan_cnt = vectors - 8;
8015 		} else {
8016 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8017 			q_per_macvlan = 1;
8018 			macvlan_cnt = vectors - 1;
8019 		}
8020 
8021 		if (macvlan_cnt == 0)
8022 			return ERR_PTR(-EBUSY);
8023 
8024 		/* Quiesce VSI queues */
8025 		i40e_quiesce_vsi(vsi);
8026 
8027 		/* sets up the macvlans but does not "enable" them */
8028 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8029 					  vdev);
8030 		if (ret)
8031 			return ERR_PTR(ret);
8032 
8033 		/* Unquiesce VSI */
8034 		i40e_unquiesce_vsi(vsi);
8035 	}
8036 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8037 					    vsi->macvlan_cnt);
8038 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8039 		return ERR_PTR(-EBUSY);
8040 
8041 	/* create the fwd struct */
8042 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8043 	if (!fwd)
8044 		return ERR_PTR(-ENOMEM);
8045 
8046 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8047 	fwd->bit_no = avail_macvlan;
8048 	netdev_set_sb_channel(vdev, avail_macvlan);
8049 	fwd->netdev = vdev;
8050 
8051 	if (!netif_running(netdev))
8052 		return fwd;
8053 
8054 	/* Set fwd ring up */
8055 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8056 	if (ret) {
8057 		/* unbind the queues and drop the subordinate channel config */
8058 		netdev_unbind_sb_channel(netdev, vdev);
8059 		netdev_set_sb_channel(vdev, 0);
8060 
8061 		kfree(fwd);
8062 		return ERR_PTR(-EINVAL);
8063 	}
8064 
8065 	return fwd;
8066 }
8067 
8068 /**
8069  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8070  * @vsi: the VSI we want to access
8071  */
8072 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8073 {
8074 	struct i40e_channel *ch, *ch_tmp;
8075 	struct i40e_pf *pf = vsi->back;
8076 	struct i40e_hw *hw = &pf->hw;
8077 	int aq_err, ret = 0;
8078 
8079 	if (list_empty(&vsi->macvlan_list))
8080 		return;
8081 
8082 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8083 		if (i40e_is_channel_macvlan(ch)) {
8084 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8085 						      i40e_channel_mac(ch),
8086 						      &aq_err);
8087 			if (!ret) {
8088 				/* Reset queue contexts */
8089 				i40e_reset_ch_rings(vsi, ch);
8090 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8091 				netdev_unbind_sb_channel(vsi->netdev,
8092 							 ch->fwd->netdev);
8093 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8094 				kfree(ch->fwd);
8095 				ch->fwd = NULL;
8096 			}
8097 		}
8098 	}
8099 }
8100 
8101 /**
8102  * i40e_fwd_del - delete macvlan interfaces
8103  * @netdev: net device to configure
8104  * @vdev: macvlan netdevice
8105  */
8106 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8107 {
8108 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8109 	struct i40e_fwd_adapter *fwd = vdev;
8110 	struct i40e_channel *ch, *ch_tmp;
8111 	struct i40e_vsi *vsi = np->vsi;
8112 	struct i40e_pf *pf = vsi->back;
8113 	struct i40e_hw *hw = &pf->hw;
8114 	int aq_err, ret = 0;
8115 
8116 	/* Find the channel associated with the macvlan and del mac filter */
8117 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8118 		if (i40e_is_channel_macvlan(ch) &&
8119 		    ether_addr_equal(i40e_channel_mac(ch),
8120 				     fwd->netdev->dev_addr)) {
8121 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8122 						      i40e_channel_mac(ch),
8123 						      &aq_err);
8124 			if (!ret) {
8125 				/* Reset queue contexts */
8126 				i40e_reset_ch_rings(vsi, ch);
8127 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8128 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8129 				netdev_set_sb_channel(fwd->netdev, 0);
8130 				kfree(ch->fwd);
8131 				ch->fwd = NULL;
8132 			} else {
8133 				dev_info(&pf->pdev->dev,
8134 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8135 					  ERR_PTR(ret),
8136 					  i40e_aq_str(hw, aq_err));
8137 			}
8138 			break;
8139 		}
8140 	}
8141 }
8142 
8143 /**
8144  * i40e_setup_tc - configure multiple traffic classes
8145  * @netdev: net device to configure
8146  * @type_data: tc offload data
8147  **/
8148 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8149 {
8150 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8151 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8152 	struct i40e_vsi *vsi = np->vsi;
8153 	struct i40e_pf *pf = vsi->back;
8154 	u8 enabled_tc = 0, num_tc, hw;
8155 	bool need_reset = false;
8156 	int old_queue_pairs;
8157 	int ret = -EINVAL;
8158 	u16 mode;
8159 	int i;
8160 
8161 	old_queue_pairs = vsi->num_queue_pairs;
8162 	num_tc = mqprio_qopt->qopt.num_tc;
8163 	hw = mqprio_qopt->qopt.hw;
8164 	mode = mqprio_qopt->mode;
8165 	if (!hw) {
8166 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8167 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8168 		goto config_tc;
8169 	}
8170 
8171 	/* Check if MFP enabled */
8172 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8173 		netdev_info(netdev,
8174 			    "Configuring TC not supported in MFP mode\n");
8175 		return ret;
8176 	}
8177 	switch (mode) {
8178 	case TC_MQPRIO_MODE_DCB:
8179 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8180 
8181 		/* Check if DCB enabled to continue */
8182 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8183 			netdev_info(netdev,
8184 				    "DCB is not enabled for adapter\n");
8185 			return ret;
8186 		}
8187 
8188 		/* Check whether tc count is within enabled limit */
8189 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8190 			netdev_info(netdev,
8191 				    "TC count greater than enabled on link for adapter\n");
8192 			return ret;
8193 		}
8194 		break;
8195 	case TC_MQPRIO_MODE_CHANNEL:
8196 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8197 			netdev_info(netdev,
8198 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8199 			return ret;
8200 		}
8201 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8202 			return ret;
8203 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8204 		if (ret)
8205 			return ret;
8206 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8207 		       sizeof(*mqprio_qopt));
8208 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8209 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8210 		break;
8211 	default:
8212 		return -EINVAL;
8213 	}
8214 
8215 config_tc:
8216 	/* Generate TC map for number of tc requested */
8217 	for (i = 0; i < num_tc; i++)
8218 		enabled_tc |= BIT(i);
8219 
8220 	/* Requesting same TC configuration as already enabled */
8221 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8222 	    mode != TC_MQPRIO_MODE_CHANNEL)
8223 		return 0;
8224 
8225 	/* Quiesce VSI queues */
8226 	i40e_quiesce_vsi(vsi);
8227 
8228 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8229 		i40e_remove_queue_channels(vsi);
8230 
8231 	/* Configure VSI for enabled TCs */
8232 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8233 	if (ret) {
8234 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8235 			    vsi->seid);
8236 		need_reset = true;
8237 		goto exit;
8238 	} else if (enabled_tc &&
8239 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8240 		netdev_info(netdev,
8241 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8242 			    vsi->tc_config.tc_info[0].qcount);
8243 		ret = -EINVAL;
8244 		need_reset = true;
8245 		goto exit;
8246 	}
8247 
8248 	dev_info(&vsi->back->pdev->dev,
8249 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8250 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8251 
8252 	if (i40e_is_tc_mqprio_enabled(pf)) {
8253 		if (vsi->mqprio_qopt.max_rate[0]) {
8254 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8255 						  vsi->mqprio_qopt.max_rate[0]);
8256 
8257 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8258 			if (!ret) {
8259 				u64 credits = max_tx_rate;
8260 
8261 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8262 				dev_dbg(&vsi->back->pdev->dev,
8263 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8264 					max_tx_rate,
8265 					credits,
8266 					vsi->seid);
8267 			} else {
8268 				need_reset = true;
8269 				goto exit;
8270 			}
8271 		}
8272 		ret = i40e_configure_queue_channels(vsi);
8273 		if (ret) {
8274 			vsi->num_queue_pairs = old_queue_pairs;
8275 			netdev_info(netdev,
8276 				    "Failed configuring queue channels\n");
8277 			need_reset = true;
8278 			goto exit;
8279 		}
8280 	}
8281 
8282 exit:
8283 	/* Reset the configuration data to defaults, only TC0 is enabled */
8284 	if (need_reset) {
8285 		i40e_vsi_set_default_tc_config(vsi);
8286 		need_reset = false;
8287 	}
8288 
8289 	/* Unquiesce VSI */
8290 	i40e_unquiesce_vsi(vsi);
8291 	return ret;
8292 }
8293 
8294 /**
8295  * i40e_set_cld_element - sets cloud filter element data
8296  * @filter: cloud filter rule
8297  * @cld: ptr to cloud filter element data
8298  *
8299  * This is helper function to copy data into cloud filter element
8300  **/
8301 static inline void
8302 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8303 		     struct i40e_aqc_cloud_filters_element_data *cld)
8304 {
8305 	u32 ipa;
8306 	int i;
8307 
8308 	memset(cld, 0, sizeof(*cld));
8309 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8310 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8311 
8312 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8313 		return;
8314 
8315 	if (filter->n_proto == ETH_P_IPV6) {
8316 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8317 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8318 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8319 
8320 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8321 		}
8322 	} else {
8323 		ipa = be32_to_cpu(filter->dst_ipv4);
8324 
8325 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8326 	}
8327 
8328 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8329 
8330 	/* tenant_id is not supported by FW now, once the support is enabled
8331 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8332 	 */
8333 	if (filter->tenant_id)
8334 		return;
8335 }
8336 
8337 /**
8338  * i40e_add_del_cloud_filter - Add/del cloud filter
8339  * @vsi: pointer to VSI
8340  * @filter: cloud filter rule
8341  * @add: if true, add, if false, delete
8342  *
8343  * Add or delete a cloud filter for a specific flow spec.
8344  * Returns 0 if the filter were successfully added.
8345  **/
8346 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8347 			      struct i40e_cloud_filter *filter, bool add)
8348 {
8349 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8350 	struct i40e_pf *pf = vsi->back;
8351 	int ret;
8352 	static const u16 flag_table[128] = {
8353 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8354 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8355 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8356 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8357 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8358 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8359 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8360 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8361 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8362 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8363 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8364 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8365 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8366 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8367 	};
8368 
8369 	if (filter->flags >= ARRAY_SIZE(flag_table))
8370 		return I40E_ERR_CONFIG;
8371 
8372 	memset(&cld_filter, 0, sizeof(cld_filter));
8373 
8374 	/* copy element needed to add cloud filter from filter */
8375 	i40e_set_cld_element(filter, &cld_filter);
8376 
8377 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8378 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8379 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8380 
8381 	if (filter->n_proto == ETH_P_IPV6)
8382 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8383 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8384 	else
8385 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8386 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8387 
8388 	if (add)
8389 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8390 						&cld_filter, 1);
8391 	else
8392 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8393 						&cld_filter, 1);
8394 	if (ret)
8395 		dev_dbg(&pf->pdev->dev,
8396 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8397 			add ? "add" : "delete", filter->dst_port, ret,
8398 			pf->hw.aq.asq_last_status);
8399 	else
8400 		dev_info(&pf->pdev->dev,
8401 			 "%s cloud filter for VSI: %d\n",
8402 			 add ? "Added" : "Deleted", filter->seid);
8403 	return ret;
8404 }
8405 
8406 /**
8407  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8408  * @vsi: pointer to VSI
8409  * @filter: cloud filter rule
8410  * @add: if true, add, if false, delete
8411  *
8412  * Add or delete a cloud filter for a specific flow spec using big buffer.
8413  * Returns 0 if the filter were successfully added.
8414  **/
8415 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8416 				      struct i40e_cloud_filter *filter,
8417 				      bool add)
8418 {
8419 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8420 	struct i40e_pf *pf = vsi->back;
8421 	int ret;
8422 
8423 	/* Both (src/dst) valid mac_addr are not supported */
8424 	if ((is_valid_ether_addr(filter->dst_mac) &&
8425 	     is_valid_ether_addr(filter->src_mac)) ||
8426 	    (is_multicast_ether_addr(filter->dst_mac) &&
8427 	     is_multicast_ether_addr(filter->src_mac)))
8428 		return -EOPNOTSUPP;
8429 
8430 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8431 	 * ports are not supported via big buffer now.
8432 	 */
8433 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8434 		return -EOPNOTSUPP;
8435 
8436 	/* adding filter using src_port/src_ip is not supported at this stage */
8437 	if (filter->src_port ||
8438 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8439 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8440 		return -EOPNOTSUPP;
8441 
8442 	memset(&cld_filter, 0, sizeof(cld_filter));
8443 
8444 	/* copy element needed to add cloud filter from filter */
8445 	i40e_set_cld_element(filter, &cld_filter.element);
8446 
8447 	if (is_valid_ether_addr(filter->dst_mac) ||
8448 	    is_valid_ether_addr(filter->src_mac) ||
8449 	    is_multicast_ether_addr(filter->dst_mac) ||
8450 	    is_multicast_ether_addr(filter->src_mac)) {
8451 		/* MAC + IP : unsupported mode */
8452 		if (filter->dst_ipv4)
8453 			return -EOPNOTSUPP;
8454 
8455 		/* since we validated that L4 port must be valid before
8456 		 * we get here, start with respective "flags" value
8457 		 * and update if vlan is present or not
8458 		 */
8459 		cld_filter.element.flags =
8460 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8461 
8462 		if (filter->vlan_id) {
8463 			cld_filter.element.flags =
8464 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8465 		}
8466 
8467 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8468 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8469 		cld_filter.element.flags =
8470 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8471 		if (filter->n_proto == ETH_P_IPV6)
8472 			cld_filter.element.flags |=
8473 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8474 		else
8475 			cld_filter.element.flags |=
8476 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8477 	} else {
8478 		dev_err(&pf->pdev->dev,
8479 			"either mac or ip has to be valid for cloud filter\n");
8480 		return -EINVAL;
8481 	}
8482 
8483 	/* Now copy L4 port in Byte 6..7 in general fields */
8484 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8485 						be16_to_cpu(filter->dst_port);
8486 
8487 	if (add) {
8488 		/* Validate current device switch mode, change if necessary */
8489 		ret = i40e_validate_and_set_switch_mode(vsi);
8490 		if (ret) {
8491 			dev_err(&pf->pdev->dev,
8492 				"failed to set switch mode, ret %d\n",
8493 				ret);
8494 			return ret;
8495 		}
8496 
8497 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8498 						   &cld_filter, 1);
8499 	} else {
8500 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8501 						   &cld_filter, 1);
8502 	}
8503 
8504 	if (ret)
8505 		dev_dbg(&pf->pdev->dev,
8506 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8507 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8508 	else
8509 		dev_info(&pf->pdev->dev,
8510 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8511 			 add ? "add" : "delete", filter->seid,
8512 			 ntohs(filter->dst_port));
8513 	return ret;
8514 }
8515 
8516 /**
8517  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8518  * @vsi: Pointer to VSI
8519  * @f: Pointer to struct flow_cls_offload
8520  * @filter: Pointer to cloud filter structure
8521  *
8522  **/
8523 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8524 				 struct flow_cls_offload *f,
8525 				 struct i40e_cloud_filter *filter)
8526 {
8527 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8528 	struct flow_dissector *dissector = rule->match.dissector;
8529 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8530 	struct i40e_pf *pf = vsi->back;
8531 	u8 field_flags = 0;
8532 
8533 	if (dissector->used_keys &
8534 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8535 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8536 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8537 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8538 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8539 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8540 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8541 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8542 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8543 			dissector->used_keys);
8544 		return -EOPNOTSUPP;
8545 	}
8546 
8547 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8548 		struct flow_match_enc_keyid match;
8549 
8550 		flow_rule_match_enc_keyid(rule, &match);
8551 		if (match.mask->keyid != 0)
8552 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8553 
8554 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8555 	}
8556 
8557 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8558 		struct flow_match_basic match;
8559 
8560 		flow_rule_match_basic(rule, &match);
8561 		n_proto_key = ntohs(match.key->n_proto);
8562 		n_proto_mask = ntohs(match.mask->n_proto);
8563 
8564 		if (n_proto_key == ETH_P_ALL) {
8565 			n_proto_key = 0;
8566 			n_proto_mask = 0;
8567 		}
8568 		filter->n_proto = n_proto_key & n_proto_mask;
8569 		filter->ip_proto = match.key->ip_proto;
8570 	}
8571 
8572 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8573 		struct flow_match_eth_addrs match;
8574 
8575 		flow_rule_match_eth_addrs(rule, &match);
8576 
8577 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8578 		if (!is_zero_ether_addr(match.mask->dst)) {
8579 			if (is_broadcast_ether_addr(match.mask->dst)) {
8580 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8581 			} else {
8582 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8583 					match.mask->dst);
8584 				return I40E_ERR_CONFIG;
8585 			}
8586 		}
8587 
8588 		if (!is_zero_ether_addr(match.mask->src)) {
8589 			if (is_broadcast_ether_addr(match.mask->src)) {
8590 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8591 			} else {
8592 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8593 					match.mask->src);
8594 				return I40E_ERR_CONFIG;
8595 			}
8596 		}
8597 		ether_addr_copy(filter->dst_mac, match.key->dst);
8598 		ether_addr_copy(filter->src_mac, match.key->src);
8599 	}
8600 
8601 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8602 		struct flow_match_vlan match;
8603 
8604 		flow_rule_match_vlan(rule, &match);
8605 		if (match.mask->vlan_id) {
8606 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8607 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8608 
8609 			} else {
8610 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8611 					match.mask->vlan_id);
8612 				return I40E_ERR_CONFIG;
8613 			}
8614 		}
8615 
8616 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8617 	}
8618 
8619 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8620 		struct flow_match_control match;
8621 
8622 		flow_rule_match_control(rule, &match);
8623 		addr_type = match.key->addr_type;
8624 	}
8625 
8626 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8627 		struct flow_match_ipv4_addrs match;
8628 
8629 		flow_rule_match_ipv4_addrs(rule, &match);
8630 		if (match.mask->dst) {
8631 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8632 				field_flags |= I40E_CLOUD_FIELD_IIP;
8633 			} else {
8634 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8635 					&match.mask->dst);
8636 				return I40E_ERR_CONFIG;
8637 			}
8638 		}
8639 
8640 		if (match.mask->src) {
8641 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8642 				field_flags |= I40E_CLOUD_FIELD_IIP;
8643 			} else {
8644 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8645 					&match.mask->src);
8646 				return I40E_ERR_CONFIG;
8647 			}
8648 		}
8649 
8650 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8651 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8652 			return I40E_ERR_CONFIG;
8653 		}
8654 		filter->dst_ipv4 = match.key->dst;
8655 		filter->src_ipv4 = match.key->src;
8656 	}
8657 
8658 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8659 		struct flow_match_ipv6_addrs match;
8660 
8661 		flow_rule_match_ipv6_addrs(rule, &match);
8662 
8663 		/* src and dest IPV6 address should not be LOOPBACK
8664 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8665 		 */
8666 		if (ipv6_addr_loopback(&match.key->dst) ||
8667 		    ipv6_addr_loopback(&match.key->src)) {
8668 			dev_err(&pf->pdev->dev,
8669 				"Bad ipv6, addr is LOOPBACK\n");
8670 			return I40E_ERR_CONFIG;
8671 		}
8672 		if (!ipv6_addr_any(&match.mask->dst) ||
8673 		    !ipv6_addr_any(&match.mask->src))
8674 			field_flags |= I40E_CLOUD_FIELD_IIP;
8675 
8676 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8677 		       sizeof(filter->src_ipv6));
8678 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8679 		       sizeof(filter->dst_ipv6));
8680 	}
8681 
8682 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8683 		struct flow_match_ports match;
8684 
8685 		flow_rule_match_ports(rule, &match);
8686 		if (match.mask->src) {
8687 			if (match.mask->src == cpu_to_be16(0xffff)) {
8688 				field_flags |= I40E_CLOUD_FIELD_IIP;
8689 			} else {
8690 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8691 					be16_to_cpu(match.mask->src));
8692 				return I40E_ERR_CONFIG;
8693 			}
8694 		}
8695 
8696 		if (match.mask->dst) {
8697 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8698 				field_flags |= I40E_CLOUD_FIELD_IIP;
8699 			} else {
8700 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8701 					be16_to_cpu(match.mask->dst));
8702 				return I40E_ERR_CONFIG;
8703 			}
8704 		}
8705 
8706 		filter->dst_port = match.key->dst;
8707 		filter->src_port = match.key->src;
8708 
8709 		switch (filter->ip_proto) {
8710 		case IPPROTO_TCP:
8711 		case IPPROTO_UDP:
8712 			break;
8713 		default:
8714 			dev_err(&pf->pdev->dev,
8715 				"Only UDP and TCP transport are supported\n");
8716 			return -EINVAL;
8717 		}
8718 	}
8719 	filter->flags = field_flags;
8720 	return 0;
8721 }
8722 
8723 /**
8724  * i40e_handle_tclass: Forward to a traffic class on the device
8725  * @vsi: Pointer to VSI
8726  * @tc: traffic class index on the device
8727  * @filter: Pointer to cloud filter structure
8728  *
8729  **/
8730 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8731 			      struct i40e_cloud_filter *filter)
8732 {
8733 	struct i40e_channel *ch, *ch_tmp;
8734 
8735 	/* direct to a traffic class on the same device */
8736 	if (tc == 0) {
8737 		filter->seid = vsi->seid;
8738 		return 0;
8739 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8740 		if (!filter->dst_port) {
8741 			dev_err(&vsi->back->pdev->dev,
8742 				"Specify destination port to direct to traffic class that is not default\n");
8743 			return -EINVAL;
8744 		}
8745 		if (list_empty(&vsi->ch_list))
8746 			return -EINVAL;
8747 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8748 					 list) {
8749 			if (ch->seid == vsi->tc_seid_map[tc])
8750 				filter->seid = ch->seid;
8751 		}
8752 		return 0;
8753 	}
8754 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8755 	return -EINVAL;
8756 }
8757 
8758 /**
8759  * i40e_configure_clsflower - Configure tc flower filters
8760  * @vsi: Pointer to VSI
8761  * @cls_flower: Pointer to struct flow_cls_offload
8762  *
8763  **/
8764 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8765 				    struct flow_cls_offload *cls_flower)
8766 {
8767 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8768 	struct i40e_cloud_filter *filter = NULL;
8769 	struct i40e_pf *pf = vsi->back;
8770 	int err = 0;
8771 
8772 	if (tc < 0) {
8773 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8774 		return -EOPNOTSUPP;
8775 	}
8776 
8777 	if (!tc) {
8778 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8779 		return -EINVAL;
8780 	}
8781 
8782 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8783 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8784 		return -EBUSY;
8785 
8786 	if (pf->fdir_pf_active_filters ||
8787 	    (!hlist_empty(&pf->fdir_filter_list))) {
8788 		dev_err(&vsi->back->pdev->dev,
8789 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8790 		return -EINVAL;
8791 	}
8792 
8793 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8794 		dev_err(&vsi->back->pdev->dev,
8795 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8796 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8797 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8798 	}
8799 
8800 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8801 	if (!filter)
8802 		return -ENOMEM;
8803 
8804 	filter->cookie = cls_flower->cookie;
8805 
8806 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8807 	if (err < 0)
8808 		goto err;
8809 
8810 	err = i40e_handle_tclass(vsi, tc, filter);
8811 	if (err < 0)
8812 		goto err;
8813 
8814 	/* Add cloud filter */
8815 	if (filter->dst_port)
8816 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8817 	else
8818 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8819 
8820 	if (err) {
8821 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8822 			err);
8823 		goto err;
8824 	}
8825 
8826 	/* add filter to the ordered list */
8827 	INIT_HLIST_NODE(&filter->cloud_node);
8828 
8829 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8830 
8831 	pf->num_cloud_filters++;
8832 
8833 	return err;
8834 err:
8835 	kfree(filter);
8836 	return err;
8837 }
8838 
8839 /**
8840  * i40e_find_cloud_filter - Find the could filter in the list
8841  * @vsi: Pointer to VSI
8842  * @cookie: filter specific cookie
8843  *
8844  **/
8845 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8846 							unsigned long *cookie)
8847 {
8848 	struct i40e_cloud_filter *filter = NULL;
8849 	struct hlist_node *node2;
8850 
8851 	hlist_for_each_entry_safe(filter, node2,
8852 				  &vsi->back->cloud_filter_list, cloud_node)
8853 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8854 			return filter;
8855 	return NULL;
8856 }
8857 
8858 /**
8859  * i40e_delete_clsflower - Remove tc flower filters
8860  * @vsi: Pointer to VSI
8861  * @cls_flower: Pointer to struct flow_cls_offload
8862  *
8863  **/
8864 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8865 				 struct flow_cls_offload *cls_flower)
8866 {
8867 	struct i40e_cloud_filter *filter = NULL;
8868 	struct i40e_pf *pf = vsi->back;
8869 	int err = 0;
8870 
8871 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8872 
8873 	if (!filter)
8874 		return -EINVAL;
8875 
8876 	hash_del(&filter->cloud_node);
8877 
8878 	if (filter->dst_port)
8879 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8880 	else
8881 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8882 
8883 	kfree(filter);
8884 	if (err) {
8885 		dev_err(&pf->pdev->dev,
8886 			"Failed to delete cloud filter, err %pe\n",
8887 			ERR_PTR(err));
8888 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8889 	}
8890 
8891 	pf->num_cloud_filters--;
8892 	if (!pf->num_cloud_filters)
8893 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8894 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8895 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8896 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8897 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8898 		}
8899 	return 0;
8900 }
8901 
8902 /**
8903  * i40e_setup_tc_cls_flower - flower classifier offloads
8904  * @np: net device to configure
8905  * @cls_flower: offload data
8906  **/
8907 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8908 				    struct flow_cls_offload *cls_flower)
8909 {
8910 	struct i40e_vsi *vsi = np->vsi;
8911 
8912 	switch (cls_flower->command) {
8913 	case FLOW_CLS_REPLACE:
8914 		return i40e_configure_clsflower(vsi, cls_flower);
8915 	case FLOW_CLS_DESTROY:
8916 		return i40e_delete_clsflower(vsi, cls_flower);
8917 	case FLOW_CLS_STATS:
8918 		return -EOPNOTSUPP;
8919 	default:
8920 		return -EOPNOTSUPP;
8921 	}
8922 }
8923 
8924 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8925 				  void *cb_priv)
8926 {
8927 	struct i40e_netdev_priv *np = cb_priv;
8928 
8929 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8930 		return -EOPNOTSUPP;
8931 
8932 	switch (type) {
8933 	case TC_SETUP_CLSFLOWER:
8934 		return i40e_setup_tc_cls_flower(np, type_data);
8935 
8936 	default:
8937 		return -EOPNOTSUPP;
8938 	}
8939 }
8940 
8941 static LIST_HEAD(i40e_block_cb_list);
8942 
8943 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8944 			   void *type_data)
8945 {
8946 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8947 
8948 	switch (type) {
8949 	case TC_SETUP_QDISC_MQPRIO:
8950 		return i40e_setup_tc(netdev, type_data);
8951 	case TC_SETUP_BLOCK:
8952 		return flow_block_cb_setup_simple(type_data,
8953 						  &i40e_block_cb_list,
8954 						  i40e_setup_tc_block_cb,
8955 						  np, np, true);
8956 	default:
8957 		return -EOPNOTSUPP;
8958 	}
8959 }
8960 
8961 /**
8962  * i40e_open - Called when a network interface is made active
8963  * @netdev: network interface device structure
8964  *
8965  * The open entry point is called when a network interface is made
8966  * active by the system (IFF_UP).  At this point all resources needed
8967  * for transmit and receive operations are allocated, the interrupt
8968  * handler is registered with the OS, the netdev watchdog subtask is
8969  * enabled, and the stack is notified that the interface is ready.
8970  *
8971  * Returns 0 on success, negative value on failure
8972  **/
8973 int i40e_open(struct net_device *netdev)
8974 {
8975 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8976 	struct i40e_vsi *vsi = np->vsi;
8977 	struct i40e_pf *pf = vsi->back;
8978 	int err;
8979 
8980 	/* disallow open during test or if eeprom is broken */
8981 	if (test_bit(__I40E_TESTING, pf->state) ||
8982 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8983 		return -EBUSY;
8984 
8985 	netif_carrier_off(netdev);
8986 
8987 	if (i40e_force_link_state(pf, true))
8988 		return -EAGAIN;
8989 
8990 	err = i40e_vsi_open(vsi);
8991 	if (err)
8992 		return err;
8993 
8994 	/* configure global TSO hardware offload settings */
8995 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8996 						       TCP_FLAG_FIN) >> 16);
8997 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8998 						       TCP_FLAG_FIN |
8999 						       TCP_FLAG_CWR) >> 16);
9000 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9001 	udp_tunnel_get_rx_info(netdev);
9002 
9003 	return 0;
9004 }
9005 
9006 /**
9007  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9008  * @vsi: vsi structure
9009  *
9010  * This updates netdev's number of tx/rx queues
9011  *
9012  * Returns status of setting tx/rx queues
9013  **/
9014 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9015 {
9016 	int ret;
9017 
9018 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9019 					   vsi->num_queue_pairs);
9020 	if (ret)
9021 		return ret;
9022 
9023 	return netif_set_real_num_tx_queues(vsi->netdev,
9024 					    vsi->num_queue_pairs);
9025 }
9026 
9027 /**
9028  * i40e_vsi_open -
9029  * @vsi: the VSI to open
9030  *
9031  * Finish initialization of the VSI.
9032  *
9033  * Returns 0 on success, negative value on failure
9034  *
9035  * Note: expects to be called while under rtnl_lock()
9036  **/
9037 int i40e_vsi_open(struct i40e_vsi *vsi)
9038 {
9039 	struct i40e_pf *pf = vsi->back;
9040 	char int_name[I40E_INT_NAME_STR_LEN];
9041 	int err;
9042 
9043 	/* allocate descriptors */
9044 	err = i40e_vsi_setup_tx_resources(vsi);
9045 	if (err)
9046 		goto err_setup_tx;
9047 	err = i40e_vsi_setup_rx_resources(vsi);
9048 	if (err)
9049 		goto err_setup_rx;
9050 
9051 	err = i40e_vsi_configure(vsi);
9052 	if (err)
9053 		goto err_setup_rx;
9054 
9055 	if (vsi->netdev) {
9056 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9057 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9058 		err = i40e_vsi_request_irq(vsi, int_name);
9059 		if (err)
9060 			goto err_setup_rx;
9061 
9062 		/* Notify the stack of the actual queue counts. */
9063 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9064 		if (err)
9065 			goto err_set_queues;
9066 
9067 	} else if (vsi->type == I40E_VSI_FDIR) {
9068 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9069 			 dev_driver_string(&pf->pdev->dev),
9070 			 dev_name(&pf->pdev->dev));
9071 		err = i40e_vsi_request_irq(vsi, int_name);
9072 		if (err)
9073 			goto err_setup_rx;
9074 
9075 	} else {
9076 		err = -EINVAL;
9077 		goto err_setup_rx;
9078 	}
9079 
9080 	err = i40e_up_complete(vsi);
9081 	if (err)
9082 		goto err_up_complete;
9083 
9084 	return 0;
9085 
9086 err_up_complete:
9087 	i40e_down(vsi);
9088 err_set_queues:
9089 	i40e_vsi_free_irq(vsi);
9090 err_setup_rx:
9091 	i40e_vsi_free_rx_resources(vsi);
9092 err_setup_tx:
9093 	i40e_vsi_free_tx_resources(vsi);
9094 	if (vsi == pf->vsi[pf->lan_vsi])
9095 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9096 
9097 	return err;
9098 }
9099 
9100 /**
9101  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9102  * @pf: Pointer to PF
9103  *
9104  * This function destroys the hlist where all the Flow Director
9105  * filters were saved.
9106  **/
9107 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9108 {
9109 	struct i40e_fdir_filter *filter;
9110 	struct i40e_flex_pit *pit_entry, *tmp;
9111 	struct hlist_node *node2;
9112 
9113 	hlist_for_each_entry_safe(filter, node2,
9114 				  &pf->fdir_filter_list, fdir_node) {
9115 		hlist_del(&filter->fdir_node);
9116 		kfree(filter);
9117 	}
9118 
9119 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9120 		list_del(&pit_entry->list);
9121 		kfree(pit_entry);
9122 	}
9123 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9124 
9125 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9126 		list_del(&pit_entry->list);
9127 		kfree(pit_entry);
9128 	}
9129 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9130 
9131 	pf->fdir_pf_active_filters = 0;
9132 	i40e_reset_fdir_filter_cnt(pf);
9133 
9134 	/* Reprogram the default input set for TCP/IPv4 */
9135 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9136 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9137 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9138 
9139 	/* Reprogram the default input set for TCP/IPv6 */
9140 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9141 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9142 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9143 
9144 	/* Reprogram the default input set for UDP/IPv4 */
9145 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9146 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9147 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9148 
9149 	/* Reprogram the default input set for UDP/IPv6 */
9150 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9151 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9152 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9153 
9154 	/* Reprogram the default input set for SCTP/IPv4 */
9155 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9156 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9157 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9158 
9159 	/* Reprogram the default input set for SCTP/IPv6 */
9160 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9161 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9162 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9163 
9164 	/* Reprogram the default input set for Other/IPv4 */
9165 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9166 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9167 
9168 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9169 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9170 
9171 	/* Reprogram the default input set for Other/IPv6 */
9172 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9173 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9174 
9175 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9176 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9177 }
9178 
9179 /**
9180  * i40e_cloud_filter_exit - Cleans up the cloud filters
9181  * @pf: Pointer to PF
9182  *
9183  * This function destroys the hlist where all the cloud filters
9184  * were saved.
9185  **/
9186 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9187 {
9188 	struct i40e_cloud_filter *cfilter;
9189 	struct hlist_node *node;
9190 
9191 	hlist_for_each_entry_safe(cfilter, node,
9192 				  &pf->cloud_filter_list, cloud_node) {
9193 		hlist_del(&cfilter->cloud_node);
9194 		kfree(cfilter);
9195 	}
9196 	pf->num_cloud_filters = 0;
9197 
9198 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9199 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9200 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9201 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9202 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9203 	}
9204 }
9205 
9206 /**
9207  * i40e_close - Disables a network interface
9208  * @netdev: network interface device structure
9209  *
9210  * The close entry point is called when an interface is de-activated
9211  * by the OS.  The hardware is still under the driver's control, but
9212  * this netdev interface is disabled.
9213  *
9214  * Returns 0, this is not allowed to fail
9215  **/
9216 int i40e_close(struct net_device *netdev)
9217 {
9218 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9219 	struct i40e_vsi *vsi = np->vsi;
9220 
9221 	i40e_vsi_close(vsi);
9222 
9223 	return 0;
9224 }
9225 
9226 /**
9227  * i40e_do_reset - Start a PF or Core Reset sequence
9228  * @pf: board private structure
9229  * @reset_flags: which reset is requested
9230  * @lock_acquired: indicates whether or not the lock has been acquired
9231  * before this function was called.
9232  *
9233  * The essential difference in resets is that the PF Reset
9234  * doesn't clear the packet buffers, doesn't reset the PE
9235  * firmware, and doesn't bother the other PFs on the chip.
9236  **/
9237 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9238 {
9239 	u32 val;
9240 
9241 	/* do the biggest reset indicated */
9242 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9243 
9244 		/* Request a Global Reset
9245 		 *
9246 		 * This will start the chip's countdown to the actual full
9247 		 * chip reset event, and a warning interrupt to be sent
9248 		 * to all PFs, including the requestor.  Our handler
9249 		 * for the warning interrupt will deal with the shutdown
9250 		 * and recovery of the switch setup.
9251 		 */
9252 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9253 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9254 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9255 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9256 
9257 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9258 
9259 		/* Request a Core Reset
9260 		 *
9261 		 * Same as Global Reset, except does *not* include the MAC/PHY
9262 		 */
9263 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9264 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9265 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9266 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9267 		i40e_flush(&pf->hw);
9268 
9269 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9270 
9271 		/* Request a PF Reset
9272 		 *
9273 		 * Resets only the PF-specific registers
9274 		 *
9275 		 * This goes directly to the tear-down and rebuild of
9276 		 * the switch, since we need to do all the recovery as
9277 		 * for the Core Reset.
9278 		 */
9279 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9280 		i40e_handle_reset_warning(pf, lock_acquired);
9281 
9282 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9283 		/* Request a PF Reset
9284 		 *
9285 		 * Resets PF and reinitializes PFs VSI.
9286 		 */
9287 		i40e_prep_for_reset(pf);
9288 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9289 		dev_info(&pf->pdev->dev,
9290 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9291 			 "FW LLDP is disabled\n" :
9292 			 "FW LLDP is enabled\n");
9293 
9294 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9295 		int v;
9296 
9297 		/* Find the VSI(s) that requested a re-init */
9298 		dev_info(&pf->pdev->dev,
9299 			 "VSI reinit requested\n");
9300 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9301 			struct i40e_vsi *vsi = pf->vsi[v];
9302 
9303 			if (vsi != NULL &&
9304 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9305 					       vsi->state))
9306 				i40e_vsi_reinit_locked(pf->vsi[v]);
9307 		}
9308 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9309 		int v;
9310 
9311 		/* Find the VSI(s) that needs to be brought down */
9312 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9313 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9314 			struct i40e_vsi *vsi = pf->vsi[v];
9315 
9316 			if (vsi != NULL &&
9317 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9318 					       vsi->state)) {
9319 				set_bit(__I40E_VSI_DOWN, vsi->state);
9320 				i40e_down(vsi);
9321 			}
9322 		}
9323 	} else {
9324 		dev_info(&pf->pdev->dev,
9325 			 "bad reset request 0x%08x\n", reset_flags);
9326 	}
9327 }
9328 
9329 #ifdef CONFIG_I40E_DCB
9330 /**
9331  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9332  * @pf: board private structure
9333  * @old_cfg: current DCB config
9334  * @new_cfg: new DCB config
9335  **/
9336 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9337 			    struct i40e_dcbx_config *old_cfg,
9338 			    struct i40e_dcbx_config *new_cfg)
9339 {
9340 	bool need_reconfig = false;
9341 
9342 	/* Check if ETS configuration has changed */
9343 	if (memcmp(&new_cfg->etscfg,
9344 		   &old_cfg->etscfg,
9345 		   sizeof(new_cfg->etscfg))) {
9346 		/* If Priority Table has changed reconfig is needed */
9347 		if (memcmp(&new_cfg->etscfg.prioritytable,
9348 			   &old_cfg->etscfg.prioritytable,
9349 			   sizeof(new_cfg->etscfg.prioritytable))) {
9350 			need_reconfig = true;
9351 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9352 		}
9353 
9354 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9355 			   &old_cfg->etscfg.tcbwtable,
9356 			   sizeof(new_cfg->etscfg.tcbwtable)))
9357 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9358 
9359 		if (memcmp(&new_cfg->etscfg.tsatable,
9360 			   &old_cfg->etscfg.tsatable,
9361 			   sizeof(new_cfg->etscfg.tsatable)))
9362 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9363 	}
9364 
9365 	/* Check if PFC configuration has changed */
9366 	if (memcmp(&new_cfg->pfc,
9367 		   &old_cfg->pfc,
9368 		   sizeof(new_cfg->pfc))) {
9369 		need_reconfig = true;
9370 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9371 	}
9372 
9373 	/* Check if APP Table has changed */
9374 	if (memcmp(&new_cfg->app,
9375 		   &old_cfg->app,
9376 		   sizeof(new_cfg->app))) {
9377 		need_reconfig = true;
9378 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9379 	}
9380 
9381 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9382 	return need_reconfig;
9383 }
9384 
9385 /**
9386  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9387  * @pf: board private structure
9388  * @e: event info posted on ARQ
9389  **/
9390 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9391 				  struct i40e_arq_event_info *e)
9392 {
9393 	struct i40e_aqc_lldp_get_mib *mib =
9394 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9395 	struct i40e_hw *hw = &pf->hw;
9396 	struct i40e_dcbx_config tmp_dcbx_cfg;
9397 	bool need_reconfig = false;
9398 	int ret = 0;
9399 	u8 type;
9400 
9401 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9402 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9403 	    (hw->phy.link_info.link_speed &
9404 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9405 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9406 		/* let firmware decide if the DCB should be disabled */
9407 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9408 
9409 	/* Not DCB capable or capability disabled */
9410 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9411 		return ret;
9412 
9413 	/* Ignore if event is not for Nearest Bridge */
9414 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9415 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9416 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9417 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9418 		return ret;
9419 
9420 	/* Check MIB Type and return if event for Remote MIB update */
9421 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9422 	dev_dbg(&pf->pdev->dev,
9423 		"LLDP event mib type %s\n", type ? "remote" : "local");
9424 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9425 		/* Update the remote cached instance and return */
9426 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9427 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9428 				&hw->remote_dcbx_config);
9429 		goto exit;
9430 	}
9431 
9432 	/* Store the old configuration */
9433 	tmp_dcbx_cfg = hw->local_dcbx_config;
9434 
9435 	/* Reset the old DCBx configuration data */
9436 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9437 	/* Get updated DCBX data from firmware */
9438 	ret = i40e_get_dcb_config(&pf->hw);
9439 	if (ret) {
9440 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9441 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9442 		    (hw->phy.link_info.link_speed &
9443 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9444 			dev_warn(&pf->pdev->dev,
9445 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9446 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9447 		} else {
9448 			dev_info(&pf->pdev->dev,
9449 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9450 				 ERR_PTR(ret),
9451 				 i40e_aq_str(&pf->hw,
9452 					     pf->hw.aq.asq_last_status));
9453 		}
9454 		goto exit;
9455 	}
9456 
9457 	/* No change detected in DCBX configs */
9458 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9459 		    sizeof(tmp_dcbx_cfg))) {
9460 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9461 		goto exit;
9462 	}
9463 
9464 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9465 					       &hw->local_dcbx_config);
9466 
9467 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9468 
9469 	if (!need_reconfig)
9470 		goto exit;
9471 
9472 	/* Enable DCB tagging only when more than one TC */
9473 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9474 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9475 	else
9476 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9477 
9478 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9479 	/* Reconfiguration needed quiesce all VSIs */
9480 	i40e_pf_quiesce_all_vsi(pf);
9481 
9482 	/* Changes in configuration update VEB/VSI */
9483 	i40e_dcb_reconfigure(pf);
9484 
9485 	ret = i40e_resume_port_tx(pf);
9486 
9487 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9488 	/* In case of error no point in resuming VSIs */
9489 	if (ret)
9490 		goto exit;
9491 
9492 	/* Wait for the PF's queues to be disabled */
9493 	ret = i40e_pf_wait_queues_disabled(pf);
9494 	if (ret) {
9495 		/* Schedule PF reset to recover */
9496 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9497 		i40e_service_event_schedule(pf);
9498 	} else {
9499 		i40e_pf_unquiesce_all_vsi(pf);
9500 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9501 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9502 	}
9503 
9504 exit:
9505 	return ret;
9506 }
9507 #endif /* CONFIG_I40E_DCB */
9508 
9509 /**
9510  * i40e_do_reset_safe - Protected reset path for userland calls.
9511  * @pf: board private structure
9512  * @reset_flags: which reset is requested
9513  *
9514  **/
9515 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9516 {
9517 	rtnl_lock();
9518 	i40e_do_reset(pf, reset_flags, true);
9519 	rtnl_unlock();
9520 }
9521 
9522 /**
9523  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9524  * @pf: board private structure
9525  * @e: event info posted on ARQ
9526  *
9527  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9528  * and VF queues
9529  **/
9530 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9531 					   struct i40e_arq_event_info *e)
9532 {
9533 	struct i40e_aqc_lan_overflow *data =
9534 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9535 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9536 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9537 	struct i40e_hw *hw = &pf->hw;
9538 	struct i40e_vf *vf;
9539 	u16 vf_id;
9540 
9541 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9542 		queue, qtx_ctl);
9543 
9544 	/* Queue belongs to VF, find the VF and issue VF reset */
9545 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9546 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9547 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9548 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9549 		vf_id -= hw->func_caps.vf_base_id;
9550 		vf = &pf->vf[vf_id];
9551 		i40e_vc_notify_vf_reset(vf);
9552 		/* Allow VF to process pending reset notification */
9553 		msleep(20);
9554 		i40e_reset_vf(vf, false);
9555 	}
9556 }
9557 
9558 /**
9559  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9560  * @pf: board private structure
9561  **/
9562 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9563 {
9564 	u32 val, fcnt_prog;
9565 
9566 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9567 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9568 	return fcnt_prog;
9569 }
9570 
9571 /**
9572  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9573  * @pf: board private structure
9574  **/
9575 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9576 {
9577 	u32 val, fcnt_prog;
9578 
9579 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9580 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9581 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9582 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9583 	return fcnt_prog;
9584 }
9585 
9586 /**
9587  * i40e_get_global_fd_count - Get total FD filters programmed on device
9588  * @pf: board private structure
9589  **/
9590 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9591 {
9592 	u32 val, fcnt_prog;
9593 
9594 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9595 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9596 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9597 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9598 	return fcnt_prog;
9599 }
9600 
9601 /**
9602  * i40e_reenable_fdir_sb - Restore FDir SB capability
9603  * @pf: board private structure
9604  **/
9605 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9606 {
9607 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9608 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9609 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9610 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9611 }
9612 
9613 /**
9614  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9615  * @pf: board private structure
9616  **/
9617 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9618 {
9619 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9620 		/* ATR uses the same filtering logic as SB rules. It only
9621 		 * functions properly if the input set mask is at the default
9622 		 * settings. It is safe to restore the default input set
9623 		 * because there are no active TCPv4 filter rules.
9624 		 */
9625 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9626 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9627 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9628 
9629 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9630 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9631 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9632 	}
9633 }
9634 
9635 /**
9636  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9637  * @pf: board private structure
9638  * @filter: FDir filter to remove
9639  */
9640 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9641 				       struct i40e_fdir_filter *filter)
9642 {
9643 	/* Update counters */
9644 	pf->fdir_pf_active_filters--;
9645 	pf->fd_inv = 0;
9646 
9647 	switch (filter->flow_type) {
9648 	case TCP_V4_FLOW:
9649 		pf->fd_tcp4_filter_cnt--;
9650 		break;
9651 	case UDP_V4_FLOW:
9652 		pf->fd_udp4_filter_cnt--;
9653 		break;
9654 	case SCTP_V4_FLOW:
9655 		pf->fd_sctp4_filter_cnt--;
9656 		break;
9657 	case TCP_V6_FLOW:
9658 		pf->fd_tcp6_filter_cnt--;
9659 		break;
9660 	case UDP_V6_FLOW:
9661 		pf->fd_udp6_filter_cnt--;
9662 		break;
9663 	case SCTP_V6_FLOW:
9664 		pf->fd_udp6_filter_cnt--;
9665 		break;
9666 	case IP_USER_FLOW:
9667 		switch (filter->ipl4_proto) {
9668 		case IPPROTO_TCP:
9669 			pf->fd_tcp4_filter_cnt--;
9670 			break;
9671 		case IPPROTO_UDP:
9672 			pf->fd_udp4_filter_cnt--;
9673 			break;
9674 		case IPPROTO_SCTP:
9675 			pf->fd_sctp4_filter_cnt--;
9676 			break;
9677 		case IPPROTO_IP:
9678 			pf->fd_ip4_filter_cnt--;
9679 			break;
9680 		}
9681 		break;
9682 	case IPV6_USER_FLOW:
9683 		switch (filter->ipl4_proto) {
9684 		case IPPROTO_TCP:
9685 			pf->fd_tcp6_filter_cnt--;
9686 			break;
9687 		case IPPROTO_UDP:
9688 			pf->fd_udp6_filter_cnt--;
9689 			break;
9690 		case IPPROTO_SCTP:
9691 			pf->fd_sctp6_filter_cnt--;
9692 			break;
9693 		case IPPROTO_IP:
9694 			pf->fd_ip6_filter_cnt--;
9695 			break;
9696 		}
9697 		break;
9698 	}
9699 
9700 	/* Remove the filter from the list and free memory */
9701 	hlist_del(&filter->fdir_node);
9702 	kfree(filter);
9703 }
9704 
9705 /**
9706  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9707  * @pf: board private structure
9708  **/
9709 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9710 {
9711 	struct i40e_fdir_filter *filter;
9712 	u32 fcnt_prog, fcnt_avail;
9713 	struct hlist_node *node;
9714 
9715 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9716 		return;
9717 
9718 	/* Check if we have enough room to re-enable FDir SB capability. */
9719 	fcnt_prog = i40e_get_global_fd_count(pf);
9720 	fcnt_avail = pf->fdir_pf_filter_count;
9721 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9722 	    (pf->fd_add_err == 0) ||
9723 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9724 		i40e_reenable_fdir_sb(pf);
9725 
9726 	/* We should wait for even more space before re-enabling ATR.
9727 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9728 	 * rules active.
9729 	 */
9730 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9731 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9732 		i40e_reenable_fdir_atr(pf);
9733 
9734 	/* if hw had a problem adding a filter, delete it */
9735 	if (pf->fd_inv > 0) {
9736 		hlist_for_each_entry_safe(filter, node,
9737 					  &pf->fdir_filter_list, fdir_node)
9738 			if (filter->fd_id == pf->fd_inv)
9739 				i40e_delete_invalid_filter(pf, filter);
9740 	}
9741 }
9742 
9743 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9744 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9745 /**
9746  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9747  * @pf: board private structure
9748  **/
9749 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9750 {
9751 	unsigned long min_flush_time;
9752 	int flush_wait_retry = 50;
9753 	bool disable_atr = false;
9754 	int fd_room;
9755 	int reg;
9756 
9757 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9758 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9759 		return;
9760 
9761 	/* If the flush is happening too quick and we have mostly SB rules we
9762 	 * should not re-enable ATR for some time.
9763 	 */
9764 	min_flush_time = pf->fd_flush_timestamp +
9765 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9766 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9767 
9768 	if (!(time_after(jiffies, min_flush_time)) &&
9769 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9770 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9771 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9772 		disable_atr = true;
9773 	}
9774 
9775 	pf->fd_flush_timestamp = jiffies;
9776 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9777 	/* flush all filters */
9778 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9779 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9780 	i40e_flush(&pf->hw);
9781 	pf->fd_flush_cnt++;
9782 	pf->fd_add_err = 0;
9783 	do {
9784 		/* Check FD flush status every 5-6msec */
9785 		usleep_range(5000, 6000);
9786 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9787 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9788 			break;
9789 	} while (flush_wait_retry--);
9790 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9791 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9792 	} else {
9793 		/* replay sideband filters */
9794 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9795 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9796 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9797 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9798 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9799 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9800 	}
9801 }
9802 
9803 /**
9804  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9805  * @pf: board private structure
9806  **/
9807 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9808 {
9809 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9810 }
9811 
9812 /**
9813  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9814  * @pf: board private structure
9815  **/
9816 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9817 {
9818 
9819 	/* if interface is down do nothing */
9820 	if (test_bit(__I40E_DOWN, pf->state))
9821 		return;
9822 
9823 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9824 		i40e_fdir_flush_and_replay(pf);
9825 
9826 	i40e_fdir_check_and_reenable(pf);
9827 
9828 }
9829 
9830 /**
9831  * i40e_vsi_link_event - notify VSI of a link event
9832  * @vsi: vsi to be notified
9833  * @link_up: link up or down
9834  **/
9835 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9836 {
9837 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9838 		return;
9839 
9840 	switch (vsi->type) {
9841 	case I40E_VSI_MAIN:
9842 		if (!vsi->netdev || !vsi->netdev_registered)
9843 			break;
9844 
9845 		if (link_up) {
9846 			netif_carrier_on(vsi->netdev);
9847 			netif_tx_wake_all_queues(vsi->netdev);
9848 		} else {
9849 			netif_carrier_off(vsi->netdev);
9850 			netif_tx_stop_all_queues(vsi->netdev);
9851 		}
9852 		break;
9853 
9854 	case I40E_VSI_SRIOV:
9855 	case I40E_VSI_VMDQ2:
9856 	case I40E_VSI_CTRL:
9857 	case I40E_VSI_IWARP:
9858 	case I40E_VSI_MIRROR:
9859 	default:
9860 		/* there is no notification for other VSIs */
9861 		break;
9862 	}
9863 }
9864 
9865 /**
9866  * i40e_veb_link_event - notify elements on the veb of a link event
9867  * @veb: veb to be notified
9868  * @link_up: link up or down
9869  **/
9870 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9871 {
9872 	struct i40e_pf *pf;
9873 	int i;
9874 
9875 	if (!veb || !veb->pf)
9876 		return;
9877 	pf = veb->pf;
9878 
9879 	/* depth first... */
9880 	for (i = 0; i < I40E_MAX_VEB; i++)
9881 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9882 			i40e_veb_link_event(pf->veb[i], link_up);
9883 
9884 	/* ... now the local VSIs */
9885 	for (i = 0; i < pf->num_alloc_vsi; i++)
9886 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9887 			i40e_vsi_link_event(pf->vsi[i], link_up);
9888 }
9889 
9890 /**
9891  * i40e_link_event - Update netif_carrier status
9892  * @pf: board private structure
9893  **/
9894 static void i40e_link_event(struct i40e_pf *pf)
9895 {
9896 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9897 	u8 new_link_speed, old_link_speed;
9898 	bool new_link, old_link;
9899 	int status;
9900 #ifdef CONFIG_I40E_DCB
9901 	int err;
9902 #endif /* CONFIG_I40E_DCB */
9903 
9904 	/* set this to force the get_link_status call to refresh state */
9905 	pf->hw.phy.get_link_info = true;
9906 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9907 	status = i40e_get_link_status(&pf->hw, &new_link);
9908 
9909 	/* On success, disable temp link polling */
9910 	if (status == I40E_SUCCESS) {
9911 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9912 	} else {
9913 		/* Enable link polling temporarily until i40e_get_link_status
9914 		 * returns I40E_SUCCESS
9915 		 */
9916 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9917 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9918 			status);
9919 		return;
9920 	}
9921 
9922 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9923 	new_link_speed = pf->hw.phy.link_info.link_speed;
9924 
9925 	if (new_link == old_link &&
9926 	    new_link_speed == old_link_speed &&
9927 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9928 	     new_link == netif_carrier_ok(vsi->netdev)))
9929 		return;
9930 
9931 	i40e_print_link_message(vsi, new_link);
9932 
9933 	/* Notify the base of the switch tree connected to
9934 	 * the link.  Floating VEBs are not notified.
9935 	 */
9936 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9937 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9938 	else
9939 		i40e_vsi_link_event(vsi, new_link);
9940 
9941 	if (pf->vf)
9942 		i40e_vc_notify_link_state(pf);
9943 
9944 	if (pf->flags & I40E_FLAG_PTP)
9945 		i40e_ptp_set_increment(pf);
9946 #ifdef CONFIG_I40E_DCB
9947 	if (new_link == old_link)
9948 		return;
9949 	/* Not SW DCB so firmware will take care of default settings */
9950 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9951 		return;
9952 
9953 	/* We cover here only link down, as after link up in case of SW DCB
9954 	 * SW LLDP agent will take care of setting it up
9955 	 */
9956 	if (!new_link) {
9957 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9958 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9959 		err = i40e_dcb_sw_default_config(pf);
9960 		if (err) {
9961 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9962 				       I40E_FLAG_DCB_ENABLED);
9963 		} else {
9964 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9965 				       DCB_CAP_DCBX_VER_IEEE;
9966 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9967 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9968 		}
9969 	}
9970 #endif /* CONFIG_I40E_DCB */
9971 }
9972 
9973 /**
9974  * i40e_watchdog_subtask - periodic checks not using event driven response
9975  * @pf: board private structure
9976  **/
9977 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9978 {
9979 	int i;
9980 
9981 	/* if interface is down do nothing */
9982 	if (test_bit(__I40E_DOWN, pf->state) ||
9983 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9984 		return;
9985 
9986 	/* make sure we don't do these things too often */
9987 	if (time_before(jiffies, (pf->service_timer_previous +
9988 				  pf->service_timer_period)))
9989 		return;
9990 	pf->service_timer_previous = jiffies;
9991 
9992 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9993 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9994 		i40e_link_event(pf);
9995 
9996 	/* Update the stats for active netdevs so the network stack
9997 	 * can look at updated numbers whenever it cares to
9998 	 */
9999 	for (i = 0; i < pf->num_alloc_vsi; i++)
10000 		if (pf->vsi[i] && pf->vsi[i]->netdev)
10001 			i40e_update_stats(pf->vsi[i]);
10002 
10003 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
10004 		/* Update the stats for the active switching components */
10005 		for (i = 0; i < I40E_MAX_VEB; i++)
10006 			if (pf->veb[i])
10007 				i40e_update_veb_stats(pf->veb[i]);
10008 	}
10009 
10010 	i40e_ptp_rx_hang(pf);
10011 	i40e_ptp_tx_hang(pf);
10012 }
10013 
10014 /**
10015  * i40e_reset_subtask - Set up for resetting the device and driver
10016  * @pf: board private structure
10017  **/
10018 static void i40e_reset_subtask(struct i40e_pf *pf)
10019 {
10020 	u32 reset_flags = 0;
10021 
10022 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10023 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10024 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10025 	}
10026 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10027 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10028 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10029 	}
10030 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10031 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10032 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10033 	}
10034 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10035 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10036 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10037 	}
10038 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10039 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10040 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10041 	}
10042 
10043 	/* If there's a recovery already waiting, it takes
10044 	 * precedence before starting a new reset sequence.
10045 	 */
10046 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10047 		i40e_prep_for_reset(pf);
10048 		i40e_reset(pf);
10049 		i40e_rebuild(pf, false, false);
10050 	}
10051 
10052 	/* If we're already down or resetting, just bail */
10053 	if (reset_flags &&
10054 	    !test_bit(__I40E_DOWN, pf->state) &&
10055 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10056 		i40e_do_reset(pf, reset_flags, false);
10057 	}
10058 }
10059 
10060 /**
10061  * i40e_handle_link_event - Handle link event
10062  * @pf: board private structure
10063  * @e: event info posted on ARQ
10064  **/
10065 static void i40e_handle_link_event(struct i40e_pf *pf,
10066 				   struct i40e_arq_event_info *e)
10067 {
10068 	struct i40e_aqc_get_link_status *status =
10069 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10070 
10071 	/* Do a new status request to re-enable LSE reporting
10072 	 * and load new status information into the hw struct
10073 	 * This completely ignores any state information
10074 	 * in the ARQ event info, instead choosing to always
10075 	 * issue the AQ update link status command.
10076 	 */
10077 	i40e_link_event(pf);
10078 
10079 	/* Check if module meets thermal requirements */
10080 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10081 		dev_err(&pf->pdev->dev,
10082 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10083 		dev_err(&pf->pdev->dev,
10084 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10085 	} else {
10086 		/* check for unqualified module, if link is down, suppress
10087 		 * the message if link was forced to be down.
10088 		 */
10089 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10090 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10091 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10092 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10093 			dev_err(&pf->pdev->dev,
10094 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10095 			dev_err(&pf->pdev->dev,
10096 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10097 		}
10098 	}
10099 }
10100 
10101 /**
10102  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10103  * @pf: board private structure
10104  **/
10105 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10106 {
10107 	struct i40e_arq_event_info event;
10108 	struct i40e_hw *hw = &pf->hw;
10109 	u16 pending, i = 0;
10110 	u16 opcode;
10111 	u32 oldval;
10112 	int ret;
10113 	u32 val;
10114 
10115 	/* Do not run clean AQ when PF reset fails */
10116 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10117 		return;
10118 
10119 	/* check for error indications */
10120 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10121 	oldval = val;
10122 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10123 		if (hw->debug_mask & I40E_DEBUG_AQ)
10124 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10125 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10126 	}
10127 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10128 		if (hw->debug_mask & I40E_DEBUG_AQ)
10129 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10130 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10131 		pf->arq_overflows++;
10132 	}
10133 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10134 		if (hw->debug_mask & I40E_DEBUG_AQ)
10135 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10136 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10137 	}
10138 	if (oldval != val)
10139 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10140 
10141 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10142 	oldval = val;
10143 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10144 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10145 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10146 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10147 	}
10148 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10149 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10150 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10151 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10152 	}
10153 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10154 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10155 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10156 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10157 	}
10158 	if (oldval != val)
10159 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10160 
10161 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10162 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10163 	if (!event.msg_buf)
10164 		return;
10165 
10166 	do {
10167 		ret = i40e_clean_arq_element(hw, &event, &pending);
10168 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10169 			break;
10170 		else if (ret) {
10171 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10172 			break;
10173 		}
10174 
10175 		opcode = le16_to_cpu(event.desc.opcode);
10176 		switch (opcode) {
10177 
10178 		case i40e_aqc_opc_get_link_status:
10179 			rtnl_lock();
10180 			i40e_handle_link_event(pf, &event);
10181 			rtnl_unlock();
10182 			break;
10183 		case i40e_aqc_opc_send_msg_to_pf:
10184 			ret = i40e_vc_process_vf_msg(pf,
10185 					le16_to_cpu(event.desc.retval),
10186 					le32_to_cpu(event.desc.cookie_high),
10187 					le32_to_cpu(event.desc.cookie_low),
10188 					event.msg_buf,
10189 					event.msg_len);
10190 			break;
10191 		case i40e_aqc_opc_lldp_update_mib:
10192 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10193 #ifdef CONFIG_I40E_DCB
10194 			rtnl_lock();
10195 			i40e_handle_lldp_event(pf, &event);
10196 			rtnl_unlock();
10197 #endif /* CONFIG_I40E_DCB */
10198 			break;
10199 		case i40e_aqc_opc_event_lan_overflow:
10200 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10201 			i40e_handle_lan_overflow_event(pf, &event);
10202 			break;
10203 		case i40e_aqc_opc_send_msg_to_peer:
10204 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10205 			break;
10206 		case i40e_aqc_opc_nvm_erase:
10207 		case i40e_aqc_opc_nvm_update:
10208 		case i40e_aqc_opc_oem_post_update:
10209 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10210 				   "ARQ NVM operation 0x%04x completed\n",
10211 				   opcode);
10212 			break;
10213 		default:
10214 			dev_info(&pf->pdev->dev,
10215 				 "ARQ: Unknown event 0x%04x ignored\n",
10216 				 opcode);
10217 			break;
10218 		}
10219 	} while (i++ < pf->adminq_work_limit);
10220 
10221 	if (i < pf->adminq_work_limit)
10222 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10223 
10224 	/* re-enable Admin queue interrupt cause */
10225 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10226 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10227 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10228 	i40e_flush(hw);
10229 
10230 	kfree(event.msg_buf);
10231 }
10232 
10233 /**
10234  * i40e_verify_eeprom - make sure eeprom is good to use
10235  * @pf: board private structure
10236  **/
10237 static void i40e_verify_eeprom(struct i40e_pf *pf)
10238 {
10239 	int err;
10240 
10241 	err = i40e_diag_eeprom_test(&pf->hw);
10242 	if (err) {
10243 		/* retry in case of garbage read */
10244 		err = i40e_diag_eeprom_test(&pf->hw);
10245 		if (err) {
10246 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10247 				 err);
10248 			set_bit(__I40E_BAD_EEPROM, pf->state);
10249 		}
10250 	}
10251 
10252 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10253 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10254 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10255 	}
10256 }
10257 
10258 /**
10259  * i40e_enable_pf_switch_lb
10260  * @pf: pointer to the PF structure
10261  *
10262  * enable switch loop back or die - no point in a return value
10263  **/
10264 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10265 {
10266 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10267 	struct i40e_vsi_context ctxt;
10268 	int ret;
10269 
10270 	ctxt.seid = pf->main_vsi_seid;
10271 	ctxt.pf_num = pf->hw.pf_id;
10272 	ctxt.vf_num = 0;
10273 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10274 	if (ret) {
10275 		dev_info(&pf->pdev->dev,
10276 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10277 			 ERR_PTR(ret),
10278 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10279 		return;
10280 	}
10281 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10282 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10283 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10284 
10285 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10286 	if (ret) {
10287 		dev_info(&pf->pdev->dev,
10288 			 "update vsi switch failed, err %pe aq_err %s\n",
10289 			 ERR_PTR(ret),
10290 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10291 	}
10292 }
10293 
10294 /**
10295  * i40e_disable_pf_switch_lb
10296  * @pf: pointer to the PF structure
10297  *
10298  * disable switch loop back or die - no point in a return value
10299  **/
10300 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10301 {
10302 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10303 	struct i40e_vsi_context ctxt;
10304 	int ret;
10305 
10306 	ctxt.seid = pf->main_vsi_seid;
10307 	ctxt.pf_num = pf->hw.pf_id;
10308 	ctxt.vf_num = 0;
10309 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10310 	if (ret) {
10311 		dev_info(&pf->pdev->dev,
10312 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10313 			 ERR_PTR(ret),
10314 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10315 		return;
10316 	}
10317 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10318 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10319 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10320 
10321 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10322 	if (ret) {
10323 		dev_info(&pf->pdev->dev,
10324 			 "update vsi switch failed, err %pe aq_err %s\n",
10325 			 ERR_PTR(ret),
10326 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10327 	}
10328 }
10329 
10330 /**
10331  * i40e_config_bridge_mode - Configure the HW bridge mode
10332  * @veb: pointer to the bridge instance
10333  *
10334  * Configure the loop back mode for the LAN VSI that is downlink to the
10335  * specified HW bridge instance. It is expected this function is called
10336  * when a new HW bridge is instantiated.
10337  **/
10338 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10339 {
10340 	struct i40e_pf *pf = veb->pf;
10341 
10342 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10343 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10344 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10345 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10346 		i40e_disable_pf_switch_lb(pf);
10347 	else
10348 		i40e_enable_pf_switch_lb(pf);
10349 }
10350 
10351 /**
10352  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10353  * @veb: pointer to the VEB instance
10354  *
10355  * This is a recursive function that first builds the attached VSIs then
10356  * recurses in to build the next layer of VEB.  We track the connections
10357  * through our own index numbers because the seid's from the HW could
10358  * change across the reset.
10359  **/
10360 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10361 {
10362 	struct i40e_vsi *ctl_vsi = NULL;
10363 	struct i40e_pf *pf = veb->pf;
10364 	int v, veb_idx;
10365 	int ret;
10366 
10367 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10368 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10369 		if (pf->vsi[v] &&
10370 		    pf->vsi[v]->veb_idx == veb->idx &&
10371 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10372 			ctl_vsi = pf->vsi[v];
10373 			break;
10374 		}
10375 	}
10376 	if (!ctl_vsi) {
10377 		dev_info(&pf->pdev->dev,
10378 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10379 		ret = -ENOENT;
10380 		goto end_reconstitute;
10381 	}
10382 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10383 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10384 	ret = i40e_add_vsi(ctl_vsi);
10385 	if (ret) {
10386 		dev_info(&pf->pdev->dev,
10387 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10388 			 veb->idx, ret);
10389 		goto end_reconstitute;
10390 	}
10391 	i40e_vsi_reset_stats(ctl_vsi);
10392 
10393 	/* create the VEB in the switch and move the VSI onto the VEB */
10394 	ret = i40e_add_veb(veb, ctl_vsi);
10395 	if (ret)
10396 		goto end_reconstitute;
10397 
10398 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10399 		veb->bridge_mode = BRIDGE_MODE_VEB;
10400 	else
10401 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10402 	i40e_config_bridge_mode(veb);
10403 
10404 	/* create the remaining VSIs attached to this VEB */
10405 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10406 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10407 			continue;
10408 
10409 		if (pf->vsi[v]->veb_idx == veb->idx) {
10410 			struct i40e_vsi *vsi = pf->vsi[v];
10411 
10412 			vsi->uplink_seid = veb->seid;
10413 			ret = i40e_add_vsi(vsi);
10414 			if (ret) {
10415 				dev_info(&pf->pdev->dev,
10416 					 "rebuild of vsi_idx %d failed: %d\n",
10417 					 v, ret);
10418 				goto end_reconstitute;
10419 			}
10420 			i40e_vsi_reset_stats(vsi);
10421 		}
10422 	}
10423 
10424 	/* create any VEBs attached to this VEB - RECURSION */
10425 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10426 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10427 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10428 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10429 			if (ret)
10430 				break;
10431 		}
10432 	}
10433 
10434 end_reconstitute:
10435 	return ret;
10436 }
10437 
10438 /**
10439  * i40e_get_capabilities - get info about the HW
10440  * @pf: the PF struct
10441  * @list_type: AQ capability to be queried
10442  **/
10443 static int i40e_get_capabilities(struct i40e_pf *pf,
10444 				 enum i40e_admin_queue_opc list_type)
10445 {
10446 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10447 	u16 data_size;
10448 	int buf_len;
10449 	int err;
10450 
10451 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10452 	do {
10453 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10454 		if (!cap_buf)
10455 			return -ENOMEM;
10456 
10457 		/* this loads the data into the hw struct for us */
10458 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10459 						    &data_size, list_type,
10460 						    NULL);
10461 		/* data loaded, buffer no longer needed */
10462 		kfree(cap_buf);
10463 
10464 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10465 			/* retry with a larger buffer */
10466 			buf_len = data_size;
10467 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10468 			dev_info(&pf->pdev->dev,
10469 				 "capability discovery failed, err %pe aq_err %s\n",
10470 				 ERR_PTR(err),
10471 				 i40e_aq_str(&pf->hw,
10472 					     pf->hw.aq.asq_last_status));
10473 			return -ENODEV;
10474 		}
10475 	} while (err);
10476 
10477 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10478 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10479 			dev_info(&pf->pdev->dev,
10480 				 "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",
10481 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10482 				 pf->hw.func_caps.num_msix_vectors,
10483 				 pf->hw.func_caps.num_msix_vectors_vf,
10484 				 pf->hw.func_caps.fd_filters_guaranteed,
10485 				 pf->hw.func_caps.fd_filters_best_effort,
10486 				 pf->hw.func_caps.num_tx_qp,
10487 				 pf->hw.func_caps.num_vsis);
10488 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10489 			dev_info(&pf->pdev->dev,
10490 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10491 				 pf->hw.dev_caps.switch_mode,
10492 				 pf->hw.dev_caps.valid_functions);
10493 			dev_info(&pf->pdev->dev,
10494 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10495 				 pf->hw.dev_caps.sr_iov_1_1,
10496 				 pf->hw.dev_caps.num_vfs);
10497 			dev_info(&pf->pdev->dev,
10498 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10499 				 pf->hw.dev_caps.num_vsis,
10500 				 pf->hw.dev_caps.num_rx_qp,
10501 				 pf->hw.dev_caps.num_tx_qp);
10502 		}
10503 	}
10504 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10505 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10506 		       + pf->hw.func_caps.num_vfs)
10507 		if (pf->hw.revision_id == 0 &&
10508 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10509 			dev_info(&pf->pdev->dev,
10510 				 "got num_vsis %d, setting num_vsis to %d\n",
10511 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10512 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10513 		}
10514 	}
10515 	return 0;
10516 }
10517 
10518 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10519 
10520 /**
10521  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10522  * @pf: board private structure
10523  **/
10524 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10525 {
10526 	struct i40e_vsi *vsi;
10527 
10528 	/* quick workaround for an NVM issue that leaves a critical register
10529 	 * uninitialized
10530 	 */
10531 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10532 		static const u32 hkey[] = {
10533 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10534 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10535 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10536 			0x95b3a76d};
10537 		int i;
10538 
10539 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10540 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10541 	}
10542 
10543 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10544 		return;
10545 
10546 	/* find existing VSI and see if it needs configuring */
10547 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10548 
10549 	/* create a new VSI if none exists */
10550 	if (!vsi) {
10551 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10552 				     pf->vsi[pf->lan_vsi]->seid, 0);
10553 		if (!vsi) {
10554 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10555 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10556 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10557 			return;
10558 		}
10559 	}
10560 
10561 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10562 }
10563 
10564 /**
10565  * i40e_fdir_teardown - release the Flow Director resources
10566  * @pf: board private structure
10567  **/
10568 static void i40e_fdir_teardown(struct i40e_pf *pf)
10569 {
10570 	struct i40e_vsi *vsi;
10571 
10572 	i40e_fdir_filter_exit(pf);
10573 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10574 	if (vsi)
10575 		i40e_vsi_release(vsi);
10576 }
10577 
10578 /**
10579  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10580  * @vsi: PF main vsi
10581  * @seid: seid of main or channel VSIs
10582  *
10583  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10584  * existed before reset
10585  **/
10586 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10587 {
10588 	struct i40e_cloud_filter *cfilter;
10589 	struct i40e_pf *pf = vsi->back;
10590 	struct hlist_node *node;
10591 	int ret;
10592 
10593 	/* Add cloud filters back if they exist */
10594 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10595 				  cloud_node) {
10596 		if (cfilter->seid != seid)
10597 			continue;
10598 
10599 		if (cfilter->dst_port)
10600 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10601 								true);
10602 		else
10603 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10604 
10605 		if (ret) {
10606 			dev_dbg(&pf->pdev->dev,
10607 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10608 				ERR_PTR(ret),
10609 				i40e_aq_str(&pf->hw,
10610 					    pf->hw.aq.asq_last_status));
10611 			return ret;
10612 		}
10613 	}
10614 	return 0;
10615 }
10616 
10617 /**
10618  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10619  * @vsi: PF main vsi
10620  *
10621  * Rebuilds channel VSIs if they existed before reset
10622  **/
10623 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10624 {
10625 	struct i40e_channel *ch, *ch_tmp;
10626 	int ret;
10627 
10628 	if (list_empty(&vsi->ch_list))
10629 		return 0;
10630 
10631 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10632 		if (!ch->initialized)
10633 			break;
10634 		/* Proceed with creation of channel (VMDq2) VSI */
10635 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10636 		if (ret) {
10637 			dev_info(&vsi->back->pdev->dev,
10638 				 "failed to rebuild channels using uplink_seid %u\n",
10639 				 vsi->uplink_seid);
10640 			return ret;
10641 		}
10642 		/* Reconfigure TX queues using QTX_CTL register */
10643 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10644 		if (ret) {
10645 			dev_info(&vsi->back->pdev->dev,
10646 				 "failed to configure TX rings for channel %u\n",
10647 				 ch->seid);
10648 			return ret;
10649 		}
10650 		/* update 'next_base_queue' */
10651 		vsi->next_base_queue = vsi->next_base_queue +
10652 							ch->num_queue_pairs;
10653 		if (ch->max_tx_rate) {
10654 			u64 credits = ch->max_tx_rate;
10655 
10656 			if (i40e_set_bw_limit(vsi, ch->seid,
10657 					      ch->max_tx_rate))
10658 				return -EINVAL;
10659 
10660 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10661 			dev_dbg(&vsi->back->pdev->dev,
10662 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10663 				ch->max_tx_rate,
10664 				credits,
10665 				ch->seid);
10666 		}
10667 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10668 		if (ret) {
10669 			dev_dbg(&vsi->back->pdev->dev,
10670 				"Failed to rebuild cloud filters for channel VSI %u\n",
10671 				ch->seid);
10672 			return ret;
10673 		}
10674 	}
10675 	return 0;
10676 }
10677 
10678 /**
10679  * i40e_clean_xps_state - clean xps state for every tx_ring
10680  * @vsi: ptr to the VSI
10681  **/
10682 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10683 {
10684 	int i;
10685 
10686 	if (vsi->tx_rings)
10687 		for (i = 0; i < vsi->num_queue_pairs; i++)
10688 			if (vsi->tx_rings[i])
10689 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10690 					  vsi->tx_rings[i]->state);
10691 }
10692 
10693 /**
10694  * i40e_prep_for_reset - prep for the core to reset
10695  * @pf: board private structure
10696  *
10697  * Close up the VFs and other things in prep for PF Reset.
10698   **/
10699 static void i40e_prep_for_reset(struct i40e_pf *pf)
10700 {
10701 	struct i40e_hw *hw = &pf->hw;
10702 	int ret = 0;
10703 	u32 v;
10704 
10705 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10706 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10707 		return;
10708 	if (i40e_check_asq_alive(&pf->hw))
10709 		i40e_vc_notify_reset(pf);
10710 
10711 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10712 
10713 	/* quiesce the VSIs and their queues that are not already DOWN */
10714 	i40e_pf_quiesce_all_vsi(pf);
10715 
10716 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10717 		if (pf->vsi[v]) {
10718 			i40e_clean_xps_state(pf->vsi[v]);
10719 			pf->vsi[v]->seid = 0;
10720 		}
10721 	}
10722 
10723 	i40e_shutdown_adminq(&pf->hw);
10724 
10725 	/* call shutdown HMC */
10726 	if (hw->hmc.hmc_obj) {
10727 		ret = i40e_shutdown_lan_hmc(hw);
10728 		if (ret)
10729 			dev_warn(&pf->pdev->dev,
10730 				 "shutdown_lan_hmc failed: %d\n", ret);
10731 	}
10732 
10733 	/* Save the current PTP time so that we can restore the time after the
10734 	 * reset completes.
10735 	 */
10736 	i40e_ptp_save_hw_time(pf);
10737 }
10738 
10739 /**
10740  * i40e_send_version - update firmware with driver version
10741  * @pf: PF struct
10742  */
10743 static void i40e_send_version(struct i40e_pf *pf)
10744 {
10745 	struct i40e_driver_version dv;
10746 
10747 	dv.major_version = 0xff;
10748 	dv.minor_version = 0xff;
10749 	dv.build_version = 0xff;
10750 	dv.subbuild_version = 0;
10751 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10752 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10753 }
10754 
10755 /**
10756  * i40e_get_oem_version - get OEM specific version information
10757  * @hw: pointer to the hardware structure
10758  **/
10759 static void i40e_get_oem_version(struct i40e_hw *hw)
10760 {
10761 	u16 block_offset = 0xffff;
10762 	u16 block_length = 0;
10763 	u16 capabilities = 0;
10764 	u16 gen_snap = 0;
10765 	u16 release = 0;
10766 
10767 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10768 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10769 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10770 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10771 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10772 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10773 #define I40E_NVM_OEM_LENGTH			3
10774 
10775 	/* Check if pointer to OEM version block is valid. */
10776 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10777 	if (block_offset == 0xffff)
10778 		return;
10779 
10780 	/* Check if OEM version block has correct length. */
10781 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10782 			   &block_length);
10783 	if (block_length < I40E_NVM_OEM_LENGTH)
10784 		return;
10785 
10786 	/* Check if OEM version format is as expected. */
10787 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10788 			   &capabilities);
10789 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10790 		return;
10791 
10792 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10793 			   &gen_snap);
10794 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10795 			   &release);
10796 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10797 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10798 }
10799 
10800 /**
10801  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10802  * @pf: board private structure
10803  **/
10804 static int i40e_reset(struct i40e_pf *pf)
10805 {
10806 	struct i40e_hw *hw = &pf->hw;
10807 	int ret;
10808 
10809 	ret = i40e_pf_reset(hw);
10810 	if (ret) {
10811 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10812 		set_bit(__I40E_RESET_FAILED, pf->state);
10813 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10814 	} else {
10815 		pf->pfr_count++;
10816 	}
10817 	return ret;
10818 }
10819 
10820 /**
10821  * i40e_rebuild - rebuild using a saved config
10822  * @pf: board private structure
10823  * @reinit: if the Main VSI needs to re-initialized.
10824  * @lock_acquired: indicates whether or not the lock has been acquired
10825  * before this function was called.
10826  **/
10827 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10828 {
10829 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10830 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10831 	struct i40e_hw *hw = &pf->hw;
10832 	int ret;
10833 	u32 val;
10834 	int v;
10835 
10836 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10837 	    is_recovery_mode_reported)
10838 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10839 
10840 	if (test_bit(__I40E_DOWN, pf->state) &&
10841 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10842 		goto clear_recovery;
10843 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10844 
10845 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10846 	ret = i40e_init_adminq(&pf->hw);
10847 	if (ret) {
10848 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10849 			 ERR_PTR(ret),
10850 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10851 		goto clear_recovery;
10852 	}
10853 	i40e_get_oem_version(&pf->hw);
10854 
10855 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10856 		/* The following delay is necessary for firmware update. */
10857 		mdelay(1000);
10858 	}
10859 
10860 	/* re-verify the eeprom if we just had an EMP reset */
10861 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10862 		i40e_verify_eeprom(pf);
10863 
10864 	/* if we are going out of or into recovery mode we have to act
10865 	 * accordingly with regard to resources initialization
10866 	 * and deinitialization
10867 	 */
10868 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10869 		if (i40e_get_capabilities(pf,
10870 					  i40e_aqc_opc_list_func_capabilities))
10871 			goto end_unlock;
10872 
10873 		if (is_recovery_mode_reported) {
10874 			/* we're staying in recovery mode so we'll reinitialize
10875 			 * misc vector here
10876 			 */
10877 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10878 				goto end_unlock;
10879 		} else {
10880 			if (!lock_acquired)
10881 				rtnl_lock();
10882 			/* we're going out of recovery mode so we'll free
10883 			 * the IRQ allocated specifically for recovery mode
10884 			 * and restore the interrupt scheme
10885 			 */
10886 			free_irq(pf->pdev->irq, pf);
10887 			i40e_clear_interrupt_scheme(pf);
10888 			if (i40e_restore_interrupt_scheme(pf))
10889 				goto end_unlock;
10890 		}
10891 
10892 		/* tell the firmware that we're starting */
10893 		i40e_send_version(pf);
10894 
10895 		/* bail out in case recovery mode was detected, as there is
10896 		 * no need for further configuration.
10897 		 */
10898 		goto end_unlock;
10899 	}
10900 
10901 	i40e_clear_pxe_mode(hw);
10902 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10903 	if (ret)
10904 		goto end_core_reset;
10905 
10906 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10907 				hw->func_caps.num_rx_qp, 0, 0);
10908 	if (ret) {
10909 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10910 		goto end_core_reset;
10911 	}
10912 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10913 	if (ret) {
10914 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10915 		goto end_core_reset;
10916 	}
10917 
10918 #ifdef CONFIG_I40E_DCB
10919 	/* Enable FW to write a default DCB config on link-up
10920 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10921 	 * is not supported with new link speed
10922 	 */
10923 	if (i40e_is_tc_mqprio_enabled(pf)) {
10924 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10925 	} else {
10926 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10927 		    (hw->phy.link_info.link_speed &
10928 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10929 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10930 			dev_warn(&pf->pdev->dev,
10931 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10932 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10933 		} else {
10934 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10935 			ret = i40e_init_pf_dcb(pf);
10936 			if (ret) {
10937 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10938 					 ret);
10939 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10940 				/* Continue without DCB enabled */
10941 			}
10942 		}
10943 	}
10944 
10945 #endif /* CONFIG_I40E_DCB */
10946 	if (!lock_acquired)
10947 		rtnl_lock();
10948 	ret = i40e_setup_pf_switch(pf, reinit, true);
10949 	if (ret)
10950 		goto end_unlock;
10951 
10952 	/* The driver only wants link up/down and module qualification
10953 	 * reports from firmware.  Note the negative logic.
10954 	 */
10955 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10956 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10957 					 I40E_AQ_EVENT_MEDIA_NA |
10958 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10959 	if (ret)
10960 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10961 			 ERR_PTR(ret),
10962 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10963 
10964 	/* Rebuild the VSIs and VEBs that existed before reset.
10965 	 * They are still in our local switch element arrays, so only
10966 	 * need to rebuild the switch model in the HW.
10967 	 *
10968 	 * If there were VEBs but the reconstitution failed, we'll try
10969 	 * to recover minimal use by getting the basic PF VSI working.
10970 	 */
10971 	if (vsi->uplink_seid != pf->mac_seid) {
10972 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10973 		/* find the one VEB connected to the MAC, and find orphans */
10974 		for (v = 0; v < I40E_MAX_VEB; v++) {
10975 			if (!pf->veb[v])
10976 				continue;
10977 
10978 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10979 			    pf->veb[v]->uplink_seid == 0) {
10980 				ret = i40e_reconstitute_veb(pf->veb[v]);
10981 
10982 				if (!ret)
10983 					continue;
10984 
10985 				/* If Main VEB failed, we're in deep doodoo,
10986 				 * so give up rebuilding the switch and set up
10987 				 * for minimal rebuild of PF VSI.
10988 				 * If orphan failed, we'll report the error
10989 				 * but try to keep going.
10990 				 */
10991 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10992 					dev_info(&pf->pdev->dev,
10993 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10994 						 ret);
10995 					vsi->uplink_seid = pf->mac_seid;
10996 					break;
10997 				} else if (pf->veb[v]->uplink_seid == 0) {
10998 					dev_info(&pf->pdev->dev,
10999 						 "rebuild of orphan VEB failed: %d\n",
11000 						 ret);
11001 				}
11002 			}
11003 		}
11004 	}
11005 
11006 	if (vsi->uplink_seid == pf->mac_seid) {
11007 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11008 		/* no VEB, so rebuild only the Main VSI */
11009 		ret = i40e_add_vsi(vsi);
11010 		if (ret) {
11011 			dev_info(&pf->pdev->dev,
11012 				 "rebuild of Main VSI failed: %d\n", ret);
11013 			goto end_unlock;
11014 		}
11015 	}
11016 
11017 	if (vsi->mqprio_qopt.max_rate[0]) {
11018 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11019 						  vsi->mqprio_qopt.max_rate[0]);
11020 		u64 credits = 0;
11021 
11022 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11023 		if (ret)
11024 			goto end_unlock;
11025 
11026 		credits = max_tx_rate;
11027 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11028 		dev_dbg(&vsi->back->pdev->dev,
11029 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11030 			max_tx_rate,
11031 			credits,
11032 			vsi->seid);
11033 	}
11034 
11035 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11036 	if (ret)
11037 		goto end_unlock;
11038 
11039 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11040 	 * for this main VSI if they exist
11041 	 */
11042 	ret = i40e_rebuild_channels(vsi);
11043 	if (ret)
11044 		goto end_unlock;
11045 
11046 	/* Reconfigure hardware for allowing smaller MSS in the case
11047 	 * of TSO, so that we avoid the MDD being fired and causing
11048 	 * a reset in the case of small MSS+TSO.
11049 	 */
11050 #define I40E_REG_MSS          0x000E64DC
11051 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11052 #define I40E_64BYTE_MSS       0x400000
11053 	val = rd32(hw, I40E_REG_MSS);
11054 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11055 		val &= ~I40E_REG_MSS_MIN_MASK;
11056 		val |= I40E_64BYTE_MSS;
11057 		wr32(hw, I40E_REG_MSS, val);
11058 	}
11059 
11060 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11061 		msleep(75);
11062 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11063 		if (ret)
11064 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11065 				 ERR_PTR(ret),
11066 				 i40e_aq_str(&pf->hw,
11067 					     pf->hw.aq.asq_last_status));
11068 	}
11069 	/* reinit the misc interrupt */
11070 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11071 		ret = i40e_setup_misc_vector(pf);
11072 		if (ret)
11073 			goto end_unlock;
11074 	}
11075 
11076 	/* Add a filter to drop all Flow control frames from any VSI from being
11077 	 * transmitted. By doing so we stop a malicious VF from sending out
11078 	 * PAUSE or PFC frames and potentially controlling traffic for other
11079 	 * PF/VF VSIs.
11080 	 * The FW can still send Flow control frames if enabled.
11081 	 */
11082 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11083 						       pf->main_vsi_seid);
11084 
11085 	/* restart the VSIs that were rebuilt and running before the reset */
11086 	i40e_pf_unquiesce_all_vsi(pf);
11087 
11088 	/* Release the RTNL lock before we start resetting VFs */
11089 	if (!lock_acquired)
11090 		rtnl_unlock();
11091 
11092 	/* Restore promiscuous settings */
11093 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11094 	if (ret)
11095 		dev_warn(&pf->pdev->dev,
11096 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11097 			 pf->cur_promisc ? "on" : "off",
11098 			 ERR_PTR(ret),
11099 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11100 
11101 	i40e_reset_all_vfs(pf, true);
11102 
11103 	/* tell the firmware that we're starting */
11104 	i40e_send_version(pf);
11105 
11106 	/* We've already released the lock, so don't do it again */
11107 	goto end_core_reset;
11108 
11109 end_unlock:
11110 	if (!lock_acquired)
11111 		rtnl_unlock();
11112 end_core_reset:
11113 	clear_bit(__I40E_RESET_FAILED, pf->state);
11114 clear_recovery:
11115 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11116 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11117 }
11118 
11119 /**
11120  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11121  * @pf: board private structure
11122  * @reinit: if the Main VSI needs to re-initialized.
11123  * @lock_acquired: indicates whether or not the lock has been acquired
11124  * before this function was called.
11125  **/
11126 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11127 				   bool lock_acquired)
11128 {
11129 	int ret;
11130 
11131 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11132 		return;
11133 	/* Now we wait for GRST to settle out.
11134 	 * We don't have to delete the VEBs or VSIs from the hw switch
11135 	 * because the reset will make them disappear.
11136 	 */
11137 	ret = i40e_reset(pf);
11138 	if (!ret)
11139 		i40e_rebuild(pf, reinit, lock_acquired);
11140 }
11141 
11142 /**
11143  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11144  * @pf: board private structure
11145  *
11146  * Close up the VFs and other things in prep for a Core Reset,
11147  * then get ready to rebuild the world.
11148  * @lock_acquired: indicates whether or not the lock has been acquired
11149  * before this function was called.
11150  **/
11151 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11152 {
11153 	i40e_prep_for_reset(pf);
11154 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11155 }
11156 
11157 /**
11158  * i40e_handle_mdd_event
11159  * @pf: pointer to the PF structure
11160  *
11161  * Called from the MDD irq handler to identify possibly malicious vfs
11162  **/
11163 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11164 {
11165 	struct i40e_hw *hw = &pf->hw;
11166 	bool mdd_detected = false;
11167 	struct i40e_vf *vf;
11168 	u32 reg;
11169 	int i;
11170 
11171 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11172 		return;
11173 
11174 	/* find what triggered the MDD event */
11175 	reg = rd32(hw, I40E_GL_MDET_TX);
11176 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11177 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11178 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11179 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11180 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11181 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11182 				I40E_GL_MDET_TX_EVENT_SHIFT;
11183 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11184 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11185 				pf->hw.func_caps.base_queue;
11186 		if (netif_msg_tx_err(pf))
11187 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11188 				 event, queue, pf_num, vf_num);
11189 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11190 		mdd_detected = true;
11191 	}
11192 	reg = rd32(hw, I40E_GL_MDET_RX);
11193 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11194 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11195 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11196 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11197 				I40E_GL_MDET_RX_EVENT_SHIFT;
11198 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11199 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11200 				pf->hw.func_caps.base_queue;
11201 		if (netif_msg_rx_err(pf))
11202 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11203 				 event, queue, func);
11204 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11205 		mdd_detected = true;
11206 	}
11207 
11208 	if (mdd_detected) {
11209 		reg = rd32(hw, I40E_PF_MDET_TX);
11210 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11211 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11212 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11213 		}
11214 		reg = rd32(hw, I40E_PF_MDET_RX);
11215 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11216 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11217 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11218 		}
11219 	}
11220 
11221 	/* see if one of the VFs needs its hand slapped */
11222 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11223 		vf = &(pf->vf[i]);
11224 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11225 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11226 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11227 			vf->num_mdd_events++;
11228 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11229 				 i);
11230 			dev_info(&pf->pdev->dev,
11231 				 "Use PF Control I/F to re-enable the VF\n");
11232 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11233 		}
11234 
11235 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11236 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11237 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11238 			vf->num_mdd_events++;
11239 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11240 				 i);
11241 			dev_info(&pf->pdev->dev,
11242 				 "Use PF Control I/F to re-enable the VF\n");
11243 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11244 		}
11245 	}
11246 
11247 	/* re-enable mdd interrupt cause */
11248 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11249 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11250 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11251 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11252 	i40e_flush(hw);
11253 }
11254 
11255 /**
11256  * i40e_service_task - Run the driver's async subtasks
11257  * @work: pointer to work_struct containing our data
11258  **/
11259 static void i40e_service_task(struct work_struct *work)
11260 {
11261 	struct i40e_pf *pf = container_of(work,
11262 					  struct i40e_pf,
11263 					  service_task);
11264 	unsigned long start_time = jiffies;
11265 
11266 	/* don't bother with service tasks if a reset is in progress */
11267 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11268 	    test_bit(__I40E_SUSPENDED, pf->state))
11269 		return;
11270 
11271 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11272 		return;
11273 
11274 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11275 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11276 		i40e_sync_filters_subtask(pf);
11277 		i40e_reset_subtask(pf);
11278 		i40e_handle_mdd_event(pf);
11279 		i40e_vc_process_vflr_event(pf);
11280 		i40e_watchdog_subtask(pf);
11281 		i40e_fdir_reinit_subtask(pf);
11282 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11283 			/* Client subtask will reopen next time through. */
11284 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11285 							   true);
11286 		} else {
11287 			i40e_client_subtask(pf);
11288 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11289 					       pf->state))
11290 				i40e_notify_client_of_l2_param_changes(
11291 								pf->vsi[pf->lan_vsi]);
11292 		}
11293 		i40e_sync_filters_subtask(pf);
11294 	} else {
11295 		i40e_reset_subtask(pf);
11296 	}
11297 
11298 	i40e_clean_adminq_subtask(pf);
11299 
11300 	/* flush memory to make sure state is correct before next watchdog */
11301 	smp_mb__before_atomic();
11302 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11303 
11304 	/* If the tasks have taken longer than one timer cycle or there
11305 	 * is more work to be done, reschedule the service task now
11306 	 * rather than wait for the timer to tick again.
11307 	 */
11308 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11309 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11310 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11311 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11312 		i40e_service_event_schedule(pf);
11313 }
11314 
11315 /**
11316  * i40e_service_timer - timer callback
11317  * @t: timer list pointer
11318  **/
11319 static void i40e_service_timer(struct timer_list *t)
11320 {
11321 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11322 
11323 	mod_timer(&pf->service_timer,
11324 		  round_jiffies(jiffies + pf->service_timer_period));
11325 	i40e_service_event_schedule(pf);
11326 }
11327 
11328 /**
11329  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11330  * @vsi: the VSI being configured
11331  **/
11332 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11333 {
11334 	struct i40e_pf *pf = vsi->back;
11335 
11336 	switch (vsi->type) {
11337 	case I40E_VSI_MAIN:
11338 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11339 		if (!vsi->num_tx_desc)
11340 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11341 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11342 		if (!vsi->num_rx_desc)
11343 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11344 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11345 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11346 			vsi->num_q_vectors = pf->num_lan_msix;
11347 		else
11348 			vsi->num_q_vectors = 1;
11349 
11350 		break;
11351 
11352 	case I40E_VSI_FDIR:
11353 		vsi->alloc_queue_pairs = 1;
11354 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11355 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11356 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11357 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11358 		vsi->num_q_vectors = pf->num_fdsb_msix;
11359 		break;
11360 
11361 	case I40E_VSI_VMDQ2:
11362 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11363 		if (!vsi->num_tx_desc)
11364 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11365 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11366 		if (!vsi->num_rx_desc)
11367 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11368 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11369 		vsi->num_q_vectors = pf->num_vmdq_msix;
11370 		break;
11371 
11372 	case I40E_VSI_SRIOV:
11373 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11374 		if (!vsi->num_tx_desc)
11375 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11376 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11377 		if (!vsi->num_rx_desc)
11378 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11379 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11380 		break;
11381 
11382 	default:
11383 		WARN_ON(1);
11384 		return -ENODATA;
11385 	}
11386 
11387 	if (is_kdump_kernel()) {
11388 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11389 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11390 	}
11391 
11392 	return 0;
11393 }
11394 
11395 /**
11396  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11397  * @vsi: VSI pointer
11398  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11399  *
11400  * On error: returns error code (negative)
11401  * On success: returns 0
11402  **/
11403 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11404 {
11405 	struct i40e_ring **next_rings;
11406 	int size;
11407 	int ret = 0;
11408 
11409 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11410 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11411 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11412 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11413 	if (!vsi->tx_rings)
11414 		return -ENOMEM;
11415 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11416 	if (i40e_enabled_xdp_vsi(vsi)) {
11417 		vsi->xdp_rings = next_rings;
11418 		next_rings += vsi->alloc_queue_pairs;
11419 	}
11420 	vsi->rx_rings = next_rings;
11421 
11422 	if (alloc_qvectors) {
11423 		/* allocate memory for q_vector pointers */
11424 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11425 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11426 		if (!vsi->q_vectors) {
11427 			ret = -ENOMEM;
11428 			goto err_vectors;
11429 		}
11430 	}
11431 	return ret;
11432 
11433 err_vectors:
11434 	kfree(vsi->tx_rings);
11435 	return ret;
11436 }
11437 
11438 /**
11439  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11440  * @pf: board private structure
11441  * @type: type of VSI
11442  *
11443  * On error: returns error code (negative)
11444  * On success: returns vsi index in PF (positive)
11445  **/
11446 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11447 {
11448 	int ret = -ENODEV;
11449 	struct i40e_vsi *vsi;
11450 	int vsi_idx;
11451 	int i;
11452 
11453 	/* Need to protect the allocation of the VSIs at the PF level */
11454 	mutex_lock(&pf->switch_mutex);
11455 
11456 	/* VSI list may be fragmented if VSI creation/destruction has
11457 	 * been happening.  We can afford to do a quick scan to look
11458 	 * for any free VSIs in the list.
11459 	 *
11460 	 * find next empty vsi slot, looping back around if necessary
11461 	 */
11462 	i = pf->next_vsi;
11463 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11464 		i++;
11465 	if (i >= pf->num_alloc_vsi) {
11466 		i = 0;
11467 		while (i < pf->next_vsi && pf->vsi[i])
11468 			i++;
11469 	}
11470 
11471 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11472 		vsi_idx = i;             /* Found one! */
11473 	} else {
11474 		ret = -ENODEV;
11475 		goto unlock_pf;  /* out of VSI slots! */
11476 	}
11477 	pf->next_vsi = ++i;
11478 
11479 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11480 	if (!vsi) {
11481 		ret = -ENOMEM;
11482 		goto unlock_pf;
11483 	}
11484 	vsi->type = type;
11485 	vsi->back = pf;
11486 	set_bit(__I40E_VSI_DOWN, vsi->state);
11487 	vsi->flags = 0;
11488 	vsi->idx = vsi_idx;
11489 	vsi->int_rate_limit = 0;
11490 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11491 				pf->rss_table_size : 64;
11492 	vsi->netdev_registered = false;
11493 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11494 	hash_init(vsi->mac_filter_hash);
11495 	vsi->irqs_ready = false;
11496 
11497 	if (type == I40E_VSI_MAIN) {
11498 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11499 		if (!vsi->af_xdp_zc_qps)
11500 			goto err_rings;
11501 	}
11502 
11503 	ret = i40e_set_num_rings_in_vsi(vsi);
11504 	if (ret)
11505 		goto err_rings;
11506 
11507 	ret = i40e_vsi_alloc_arrays(vsi, true);
11508 	if (ret)
11509 		goto err_rings;
11510 
11511 	/* Setup default MSIX irq handler for VSI */
11512 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11513 
11514 	/* Initialize VSI lock */
11515 	spin_lock_init(&vsi->mac_filter_hash_lock);
11516 	pf->vsi[vsi_idx] = vsi;
11517 	ret = vsi_idx;
11518 	goto unlock_pf;
11519 
11520 err_rings:
11521 	bitmap_free(vsi->af_xdp_zc_qps);
11522 	pf->next_vsi = i - 1;
11523 	kfree(vsi);
11524 unlock_pf:
11525 	mutex_unlock(&pf->switch_mutex);
11526 	return ret;
11527 }
11528 
11529 /**
11530  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11531  * @vsi: VSI pointer
11532  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11533  *
11534  * On error: returns error code (negative)
11535  * On success: returns 0
11536  **/
11537 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11538 {
11539 	/* free the ring and vector containers */
11540 	if (free_qvectors) {
11541 		kfree(vsi->q_vectors);
11542 		vsi->q_vectors = NULL;
11543 	}
11544 	kfree(vsi->tx_rings);
11545 	vsi->tx_rings = NULL;
11546 	vsi->rx_rings = NULL;
11547 	vsi->xdp_rings = NULL;
11548 }
11549 
11550 /**
11551  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11552  * and lookup table
11553  * @vsi: Pointer to VSI structure
11554  */
11555 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11556 {
11557 	if (!vsi)
11558 		return;
11559 
11560 	kfree(vsi->rss_hkey_user);
11561 	vsi->rss_hkey_user = NULL;
11562 
11563 	kfree(vsi->rss_lut_user);
11564 	vsi->rss_lut_user = NULL;
11565 }
11566 
11567 /**
11568  * i40e_vsi_clear - Deallocate the VSI provided
11569  * @vsi: the VSI being un-configured
11570  **/
11571 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11572 {
11573 	struct i40e_pf *pf;
11574 
11575 	if (!vsi)
11576 		return 0;
11577 
11578 	if (!vsi->back)
11579 		goto free_vsi;
11580 	pf = vsi->back;
11581 
11582 	mutex_lock(&pf->switch_mutex);
11583 	if (!pf->vsi[vsi->idx]) {
11584 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11585 			vsi->idx, vsi->idx, vsi->type);
11586 		goto unlock_vsi;
11587 	}
11588 
11589 	if (pf->vsi[vsi->idx] != vsi) {
11590 		dev_err(&pf->pdev->dev,
11591 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11592 			pf->vsi[vsi->idx]->idx,
11593 			pf->vsi[vsi->idx]->type,
11594 			vsi->idx, vsi->type);
11595 		goto unlock_vsi;
11596 	}
11597 
11598 	/* updates the PF for this cleared vsi */
11599 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11600 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11601 
11602 	bitmap_free(vsi->af_xdp_zc_qps);
11603 	i40e_vsi_free_arrays(vsi, true);
11604 	i40e_clear_rss_config_user(vsi);
11605 
11606 	pf->vsi[vsi->idx] = NULL;
11607 	if (vsi->idx < pf->next_vsi)
11608 		pf->next_vsi = vsi->idx;
11609 
11610 unlock_vsi:
11611 	mutex_unlock(&pf->switch_mutex);
11612 free_vsi:
11613 	kfree(vsi);
11614 
11615 	return 0;
11616 }
11617 
11618 /**
11619  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11620  * @vsi: the VSI being cleaned
11621  **/
11622 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11623 {
11624 	int i;
11625 
11626 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11627 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11628 			kfree_rcu(vsi->tx_rings[i], rcu);
11629 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11630 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11631 			if (vsi->xdp_rings)
11632 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11633 		}
11634 	}
11635 }
11636 
11637 /**
11638  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11639  * @vsi: the VSI being configured
11640  **/
11641 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11642 {
11643 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11644 	struct i40e_pf *pf = vsi->back;
11645 	struct i40e_ring *ring;
11646 
11647 	/* Set basic values in the rings to be used later during open() */
11648 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11649 		/* allocate space for both Tx and Rx in one shot */
11650 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11651 		if (!ring)
11652 			goto err_out;
11653 
11654 		ring->queue_index = i;
11655 		ring->reg_idx = vsi->base_queue + i;
11656 		ring->ring_active = false;
11657 		ring->vsi = vsi;
11658 		ring->netdev = vsi->netdev;
11659 		ring->dev = &pf->pdev->dev;
11660 		ring->count = vsi->num_tx_desc;
11661 		ring->size = 0;
11662 		ring->dcb_tc = 0;
11663 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11664 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11665 		ring->itr_setting = pf->tx_itr_default;
11666 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11667 
11668 		if (!i40e_enabled_xdp_vsi(vsi))
11669 			goto setup_rx;
11670 
11671 		ring->queue_index = vsi->alloc_queue_pairs + i;
11672 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11673 		ring->ring_active = false;
11674 		ring->vsi = vsi;
11675 		ring->netdev = NULL;
11676 		ring->dev = &pf->pdev->dev;
11677 		ring->count = vsi->num_tx_desc;
11678 		ring->size = 0;
11679 		ring->dcb_tc = 0;
11680 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11681 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11682 		set_ring_xdp(ring);
11683 		ring->itr_setting = pf->tx_itr_default;
11684 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11685 
11686 setup_rx:
11687 		ring->queue_index = i;
11688 		ring->reg_idx = vsi->base_queue + i;
11689 		ring->ring_active = false;
11690 		ring->vsi = vsi;
11691 		ring->netdev = vsi->netdev;
11692 		ring->dev = &pf->pdev->dev;
11693 		ring->count = vsi->num_rx_desc;
11694 		ring->size = 0;
11695 		ring->dcb_tc = 0;
11696 		ring->itr_setting = pf->rx_itr_default;
11697 		WRITE_ONCE(vsi->rx_rings[i], ring);
11698 	}
11699 
11700 	return 0;
11701 
11702 err_out:
11703 	i40e_vsi_clear_rings(vsi);
11704 	return -ENOMEM;
11705 }
11706 
11707 /**
11708  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11709  * @pf: board private structure
11710  * @vectors: the number of MSI-X vectors to request
11711  *
11712  * Returns the number of vectors reserved, or error
11713  **/
11714 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11715 {
11716 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11717 					I40E_MIN_MSIX, vectors);
11718 	if (vectors < 0) {
11719 		dev_info(&pf->pdev->dev,
11720 			 "MSI-X vector reservation failed: %d\n", vectors);
11721 		vectors = 0;
11722 	}
11723 
11724 	return vectors;
11725 }
11726 
11727 /**
11728  * i40e_init_msix - Setup the MSIX capability
11729  * @pf: board private structure
11730  *
11731  * Work with the OS to set up the MSIX vectors needed.
11732  *
11733  * Returns the number of vectors reserved or negative on failure
11734  **/
11735 static int i40e_init_msix(struct i40e_pf *pf)
11736 {
11737 	struct i40e_hw *hw = &pf->hw;
11738 	int cpus, extra_vectors;
11739 	int vectors_left;
11740 	int v_budget, i;
11741 	int v_actual;
11742 	int iwarp_requested = 0;
11743 
11744 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11745 		return -ENODEV;
11746 
11747 	/* The number of vectors we'll request will be comprised of:
11748 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11749 	 *   - The number of LAN queue pairs
11750 	 *	- Queues being used for RSS.
11751 	 *		We don't need as many as max_rss_size vectors.
11752 	 *		use rss_size instead in the calculation since that
11753 	 *		is governed by number of cpus in the system.
11754 	 *	- assumes symmetric Tx/Rx pairing
11755 	 *   - The number of VMDq pairs
11756 	 *   - The CPU count within the NUMA node if iWARP is enabled
11757 	 * Once we count this up, try the request.
11758 	 *
11759 	 * If we can't get what we want, we'll simplify to nearly nothing
11760 	 * and try again.  If that still fails, we punt.
11761 	 */
11762 	vectors_left = hw->func_caps.num_msix_vectors;
11763 	v_budget = 0;
11764 
11765 	/* reserve one vector for miscellaneous handler */
11766 	if (vectors_left) {
11767 		v_budget++;
11768 		vectors_left--;
11769 	}
11770 
11771 	/* reserve some vectors for the main PF traffic queues. Initially we
11772 	 * only reserve at most 50% of the available vectors, in the case that
11773 	 * the number of online CPUs is large. This ensures that we can enable
11774 	 * extra features as well. Once we've enabled the other features, we
11775 	 * will use any remaining vectors to reach as close as we can to the
11776 	 * number of online CPUs.
11777 	 */
11778 	cpus = num_online_cpus();
11779 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11780 	vectors_left -= pf->num_lan_msix;
11781 
11782 	/* reserve one vector for sideband flow director */
11783 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11784 		if (vectors_left) {
11785 			pf->num_fdsb_msix = 1;
11786 			v_budget++;
11787 			vectors_left--;
11788 		} else {
11789 			pf->num_fdsb_msix = 0;
11790 		}
11791 	}
11792 
11793 	/* can we reserve enough for iWARP? */
11794 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11795 		iwarp_requested = pf->num_iwarp_msix;
11796 
11797 		if (!vectors_left)
11798 			pf->num_iwarp_msix = 0;
11799 		else if (vectors_left < pf->num_iwarp_msix)
11800 			pf->num_iwarp_msix = 1;
11801 		v_budget += pf->num_iwarp_msix;
11802 		vectors_left -= pf->num_iwarp_msix;
11803 	}
11804 
11805 	/* any vectors left over go for VMDq support */
11806 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11807 		if (!vectors_left) {
11808 			pf->num_vmdq_msix = 0;
11809 			pf->num_vmdq_qps = 0;
11810 		} else {
11811 			int vmdq_vecs_wanted =
11812 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11813 			int vmdq_vecs =
11814 				min_t(int, vectors_left, vmdq_vecs_wanted);
11815 
11816 			/* if we're short on vectors for what's desired, we limit
11817 			 * the queues per vmdq.  If this is still more than are
11818 			 * available, the user will need to change the number of
11819 			 * queues/vectors used by the PF later with the ethtool
11820 			 * channels command
11821 			 */
11822 			if (vectors_left < vmdq_vecs_wanted) {
11823 				pf->num_vmdq_qps = 1;
11824 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11825 				vmdq_vecs = min_t(int,
11826 						  vectors_left,
11827 						  vmdq_vecs_wanted);
11828 			}
11829 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11830 
11831 			v_budget += vmdq_vecs;
11832 			vectors_left -= vmdq_vecs;
11833 		}
11834 	}
11835 
11836 	/* On systems with a large number of SMP cores, we previously limited
11837 	 * the number of vectors for num_lan_msix to be at most 50% of the
11838 	 * available vectors, to allow for other features. Now, we add back
11839 	 * the remaining vectors. However, we ensure that the total
11840 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11841 	 * calculate the number of vectors we can add without going over the
11842 	 * cap of CPUs. For systems with a small number of CPUs this will be
11843 	 * zero.
11844 	 */
11845 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11846 	pf->num_lan_msix += extra_vectors;
11847 	vectors_left -= extra_vectors;
11848 
11849 	WARN(vectors_left < 0,
11850 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11851 
11852 	v_budget += pf->num_lan_msix;
11853 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11854 				   GFP_KERNEL);
11855 	if (!pf->msix_entries)
11856 		return -ENOMEM;
11857 
11858 	for (i = 0; i < v_budget; i++)
11859 		pf->msix_entries[i].entry = i;
11860 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11861 
11862 	if (v_actual < I40E_MIN_MSIX) {
11863 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11864 		kfree(pf->msix_entries);
11865 		pf->msix_entries = NULL;
11866 		pci_disable_msix(pf->pdev);
11867 		return -ENODEV;
11868 
11869 	} else if (v_actual == I40E_MIN_MSIX) {
11870 		/* Adjust for minimal MSIX use */
11871 		pf->num_vmdq_vsis = 0;
11872 		pf->num_vmdq_qps = 0;
11873 		pf->num_lan_qps = 1;
11874 		pf->num_lan_msix = 1;
11875 
11876 	} else if (v_actual != v_budget) {
11877 		/* If we have limited resources, we will start with no vectors
11878 		 * for the special features and then allocate vectors to some
11879 		 * of these features based on the policy and at the end disable
11880 		 * the features that did not get any vectors.
11881 		 */
11882 		int vec;
11883 
11884 		dev_info(&pf->pdev->dev,
11885 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11886 			 v_actual, v_budget);
11887 		/* reserve the misc vector */
11888 		vec = v_actual - 1;
11889 
11890 		/* Scale vector usage down */
11891 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11892 		pf->num_vmdq_vsis = 1;
11893 		pf->num_vmdq_qps = 1;
11894 
11895 		/* partition out the remaining vectors */
11896 		switch (vec) {
11897 		case 2:
11898 			pf->num_lan_msix = 1;
11899 			break;
11900 		case 3:
11901 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11902 				pf->num_lan_msix = 1;
11903 				pf->num_iwarp_msix = 1;
11904 			} else {
11905 				pf->num_lan_msix = 2;
11906 			}
11907 			break;
11908 		default:
11909 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11910 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11911 						 iwarp_requested);
11912 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11913 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11914 			} else {
11915 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11916 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11917 			}
11918 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11919 				pf->num_fdsb_msix = 1;
11920 				vec--;
11921 			}
11922 			pf->num_lan_msix = min_t(int,
11923 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11924 							      pf->num_lan_msix);
11925 			pf->num_lan_qps = pf->num_lan_msix;
11926 			break;
11927 		}
11928 	}
11929 
11930 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11931 	    (pf->num_fdsb_msix == 0)) {
11932 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11933 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11934 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11935 	}
11936 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11937 	    (pf->num_vmdq_msix == 0)) {
11938 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11939 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11940 	}
11941 
11942 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11943 	    (pf->num_iwarp_msix == 0)) {
11944 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11945 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11946 	}
11947 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11948 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11949 		   pf->num_lan_msix,
11950 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11951 		   pf->num_fdsb_msix,
11952 		   pf->num_iwarp_msix);
11953 
11954 	return v_actual;
11955 }
11956 
11957 /**
11958  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11959  * @vsi: the VSI being configured
11960  * @v_idx: index of the vector in the vsi struct
11961  *
11962  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11963  **/
11964 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11965 {
11966 	struct i40e_q_vector *q_vector;
11967 
11968 	/* allocate q_vector */
11969 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11970 	if (!q_vector)
11971 		return -ENOMEM;
11972 
11973 	q_vector->vsi = vsi;
11974 	q_vector->v_idx = v_idx;
11975 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11976 
11977 	if (vsi->netdev)
11978 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11979 
11980 	/* tie q_vector and vsi together */
11981 	vsi->q_vectors[v_idx] = q_vector;
11982 
11983 	return 0;
11984 }
11985 
11986 /**
11987  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11988  * @vsi: the VSI being configured
11989  *
11990  * We allocate one q_vector per queue interrupt.  If allocation fails we
11991  * return -ENOMEM.
11992  **/
11993 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11994 {
11995 	struct i40e_pf *pf = vsi->back;
11996 	int err, v_idx, num_q_vectors;
11997 
11998 	/* if not MSIX, give the one vector only to the LAN VSI */
11999 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
12000 		num_q_vectors = vsi->num_q_vectors;
12001 	else if (vsi == pf->vsi[pf->lan_vsi])
12002 		num_q_vectors = 1;
12003 	else
12004 		return -EINVAL;
12005 
12006 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12007 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12008 		if (err)
12009 			goto err_out;
12010 	}
12011 
12012 	return 0;
12013 
12014 err_out:
12015 	while (v_idx--)
12016 		i40e_free_q_vector(vsi, v_idx);
12017 
12018 	return err;
12019 }
12020 
12021 /**
12022  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12023  * @pf: board private structure to initialize
12024  **/
12025 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12026 {
12027 	int vectors = 0;
12028 	ssize_t size;
12029 
12030 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12031 		vectors = i40e_init_msix(pf);
12032 		if (vectors < 0) {
12033 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12034 				       I40E_FLAG_IWARP_ENABLED	|
12035 				       I40E_FLAG_RSS_ENABLED	|
12036 				       I40E_FLAG_DCB_CAPABLE	|
12037 				       I40E_FLAG_DCB_ENABLED	|
12038 				       I40E_FLAG_SRIOV_ENABLED	|
12039 				       I40E_FLAG_FD_SB_ENABLED	|
12040 				       I40E_FLAG_FD_ATR_ENABLED	|
12041 				       I40E_FLAG_VMDQ_ENABLED);
12042 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12043 
12044 			/* rework the queue expectations without MSIX */
12045 			i40e_determine_queue_usage(pf);
12046 		}
12047 	}
12048 
12049 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12050 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12051 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12052 		vectors = pci_enable_msi(pf->pdev);
12053 		if (vectors < 0) {
12054 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12055 				 vectors);
12056 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12057 		}
12058 		vectors = 1;  /* one MSI or Legacy vector */
12059 	}
12060 
12061 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12062 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12063 
12064 	/* set up vector assignment tracking */
12065 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12066 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12067 	if (!pf->irq_pile)
12068 		return -ENOMEM;
12069 
12070 	pf->irq_pile->num_entries = vectors;
12071 
12072 	/* track first vector for misc interrupts, ignore return */
12073 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12074 
12075 	return 0;
12076 }
12077 
12078 /**
12079  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12080  * @pf: private board data structure
12081  *
12082  * Restore the interrupt scheme that was cleared when we suspended the
12083  * device. This should be called during resume to re-allocate the q_vectors
12084  * and reacquire IRQs.
12085  */
12086 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12087 {
12088 	int err, i;
12089 
12090 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12091 	 * scheme. We need to re-enabled them here in order to attempt to
12092 	 * re-acquire the MSI or MSI-X vectors
12093 	 */
12094 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12095 
12096 	err = i40e_init_interrupt_scheme(pf);
12097 	if (err)
12098 		return err;
12099 
12100 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12101 	 * rings together again.
12102 	 */
12103 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12104 		if (pf->vsi[i]) {
12105 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12106 			if (err)
12107 				goto err_unwind;
12108 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12109 		}
12110 	}
12111 
12112 	err = i40e_setup_misc_vector(pf);
12113 	if (err)
12114 		goto err_unwind;
12115 
12116 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12117 		i40e_client_update_msix_info(pf);
12118 
12119 	return 0;
12120 
12121 err_unwind:
12122 	while (i--) {
12123 		if (pf->vsi[i])
12124 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12125 	}
12126 
12127 	return err;
12128 }
12129 
12130 /**
12131  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12132  * non queue events in recovery mode
12133  * @pf: board private structure
12134  *
12135  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12136  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12137  * This is handled differently than in recovery mode since no Tx/Rx resources
12138  * are being allocated.
12139  **/
12140 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12141 {
12142 	int err;
12143 
12144 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12145 		err = i40e_setup_misc_vector(pf);
12146 
12147 		if (err) {
12148 			dev_info(&pf->pdev->dev,
12149 				 "MSI-X misc vector request failed, error %d\n",
12150 				 err);
12151 			return err;
12152 		}
12153 	} else {
12154 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12155 
12156 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12157 				  pf->int_name, pf);
12158 
12159 		if (err) {
12160 			dev_info(&pf->pdev->dev,
12161 				 "MSI/legacy misc vector request failed, error %d\n",
12162 				 err);
12163 			return err;
12164 		}
12165 		i40e_enable_misc_int_causes(pf);
12166 		i40e_irq_dynamic_enable_icr0(pf);
12167 	}
12168 
12169 	return 0;
12170 }
12171 
12172 /**
12173  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12174  * @pf: board private structure
12175  *
12176  * This sets up the handler for MSIX 0, which is used to manage the
12177  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12178  * when in MSI or Legacy interrupt mode.
12179  **/
12180 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12181 {
12182 	struct i40e_hw *hw = &pf->hw;
12183 	int err = 0;
12184 
12185 	/* Only request the IRQ once, the first time through. */
12186 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12187 		err = request_irq(pf->msix_entries[0].vector,
12188 				  i40e_intr, 0, pf->int_name, pf);
12189 		if (err) {
12190 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12191 			dev_info(&pf->pdev->dev,
12192 				 "request_irq for %s failed: %d\n",
12193 				 pf->int_name, err);
12194 			return -EFAULT;
12195 		}
12196 	}
12197 
12198 	i40e_enable_misc_int_causes(pf);
12199 
12200 	/* associate no queues to the misc vector */
12201 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12202 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12203 
12204 	i40e_flush(hw);
12205 
12206 	i40e_irq_dynamic_enable_icr0(pf);
12207 
12208 	return err;
12209 }
12210 
12211 /**
12212  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12213  * @vsi: Pointer to vsi structure
12214  * @seed: Buffter to store the hash keys
12215  * @lut: Buffer to store the lookup table entries
12216  * @lut_size: Size of buffer to store the lookup table entries
12217  *
12218  * Return 0 on success, negative on failure
12219  */
12220 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12221 			   u8 *lut, u16 lut_size)
12222 {
12223 	struct i40e_pf *pf = vsi->back;
12224 	struct i40e_hw *hw = &pf->hw;
12225 	int ret = 0;
12226 
12227 	if (seed) {
12228 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12229 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12230 		if (ret) {
12231 			dev_info(&pf->pdev->dev,
12232 				 "Cannot get RSS key, err %pe aq_err %s\n",
12233 				 ERR_PTR(ret),
12234 				 i40e_aq_str(&pf->hw,
12235 					     pf->hw.aq.asq_last_status));
12236 			return ret;
12237 		}
12238 	}
12239 
12240 	if (lut) {
12241 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12242 
12243 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12244 		if (ret) {
12245 			dev_info(&pf->pdev->dev,
12246 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12247 				 ERR_PTR(ret),
12248 				 i40e_aq_str(&pf->hw,
12249 					     pf->hw.aq.asq_last_status));
12250 			return ret;
12251 		}
12252 	}
12253 
12254 	return ret;
12255 }
12256 
12257 /**
12258  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12259  * @vsi: Pointer to vsi structure
12260  * @seed: RSS hash seed
12261  * @lut: Lookup table
12262  * @lut_size: Lookup table size
12263  *
12264  * Returns 0 on success, negative on failure
12265  **/
12266 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12267 			       const u8 *lut, u16 lut_size)
12268 {
12269 	struct i40e_pf *pf = vsi->back;
12270 	struct i40e_hw *hw = &pf->hw;
12271 	u16 vf_id = vsi->vf_id;
12272 	u8 i;
12273 
12274 	/* Fill out hash function seed */
12275 	if (seed) {
12276 		u32 *seed_dw = (u32 *)seed;
12277 
12278 		if (vsi->type == I40E_VSI_MAIN) {
12279 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12280 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12281 		} else if (vsi->type == I40E_VSI_SRIOV) {
12282 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12283 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12284 		} else {
12285 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12286 		}
12287 	}
12288 
12289 	if (lut) {
12290 		u32 *lut_dw = (u32 *)lut;
12291 
12292 		if (vsi->type == I40E_VSI_MAIN) {
12293 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12294 				return -EINVAL;
12295 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12296 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12297 		} else if (vsi->type == I40E_VSI_SRIOV) {
12298 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12299 				return -EINVAL;
12300 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12301 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12302 		} else {
12303 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12304 		}
12305 	}
12306 	i40e_flush(hw);
12307 
12308 	return 0;
12309 }
12310 
12311 /**
12312  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12313  * @vsi: Pointer to VSI structure
12314  * @seed: Buffer to store the keys
12315  * @lut: Buffer to store the lookup table entries
12316  * @lut_size: Size of buffer to store the lookup table entries
12317  *
12318  * Returns 0 on success, negative on failure
12319  */
12320 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12321 			    u8 *lut, u16 lut_size)
12322 {
12323 	struct i40e_pf *pf = vsi->back;
12324 	struct i40e_hw *hw = &pf->hw;
12325 	u16 i;
12326 
12327 	if (seed) {
12328 		u32 *seed_dw = (u32 *)seed;
12329 
12330 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12331 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12332 	}
12333 	if (lut) {
12334 		u32 *lut_dw = (u32 *)lut;
12335 
12336 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12337 			return -EINVAL;
12338 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12339 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12340 	}
12341 
12342 	return 0;
12343 }
12344 
12345 /**
12346  * i40e_config_rss - Configure RSS keys and lut
12347  * @vsi: Pointer to VSI structure
12348  * @seed: RSS hash seed
12349  * @lut: Lookup table
12350  * @lut_size: Lookup table size
12351  *
12352  * Returns 0 on success, negative on failure
12353  */
12354 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12355 {
12356 	struct i40e_pf *pf = vsi->back;
12357 
12358 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12359 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12360 	else
12361 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12362 }
12363 
12364 /**
12365  * i40e_get_rss - Get RSS keys and lut
12366  * @vsi: Pointer to VSI structure
12367  * @seed: Buffer to store the keys
12368  * @lut: Buffer to store the lookup table entries
12369  * @lut_size: Size of buffer to store the lookup table entries
12370  *
12371  * Returns 0 on success, negative on failure
12372  */
12373 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12374 {
12375 	struct i40e_pf *pf = vsi->back;
12376 
12377 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12378 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12379 	else
12380 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12381 }
12382 
12383 /**
12384  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12385  * @pf: Pointer to board private structure
12386  * @lut: Lookup table
12387  * @rss_table_size: Lookup table size
12388  * @rss_size: Range of queue number for hashing
12389  */
12390 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12391 		       u16 rss_table_size, u16 rss_size)
12392 {
12393 	u16 i;
12394 
12395 	for (i = 0; i < rss_table_size; i++)
12396 		lut[i] = i % rss_size;
12397 }
12398 
12399 /**
12400  * i40e_pf_config_rss - Prepare for RSS if used
12401  * @pf: board private structure
12402  **/
12403 static int i40e_pf_config_rss(struct i40e_pf *pf)
12404 {
12405 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12406 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12407 	u8 *lut;
12408 	struct i40e_hw *hw = &pf->hw;
12409 	u32 reg_val;
12410 	u64 hena;
12411 	int ret;
12412 
12413 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12414 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12415 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12416 	hena |= i40e_pf_get_default_rss_hena(pf);
12417 
12418 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12419 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12420 
12421 	/* Determine the RSS table size based on the hardware capabilities */
12422 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12423 	reg_val = (pf->rss_table_size == 512) ?
12424 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12425 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12426 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12427 
12428 	/* Determine the RSS size of the VSI */
12429 	if (!vsi->rss_size) {
12430 		u16 qcount;
12431 		/* If the firmware does something weird during VSI init, we
12432 		 * could end up with zero TCs. Check for that to avoid
12433 		 * divide-by-zero. It probably won't pass traffic, but it also
12434 		 * won't panic.
12435 		 */
12436 		qcount = vsi->num_queue_pairs /
12437 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12438 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12439 	}
12440 	if (!vsi->rss_size)
12441 		return -EINVAL;
12442 
12443 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12444 	if (!lut)
12445 		return -ENOMEM;
12446 
12447 	/* Use user configured lut if there is one, otherwise use default */
12448 	if (vsi->rss_lut_user)
12449 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12450 	else
12451 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12452 
12453 	/* Use user configured hash key if there is one, otherwise
12454 	 * use default.
12455 	 */
12456 	if (vsi->rss_hkey_user)
12457 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12458 	else
12459 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12460 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12461 	kfree(lut);
12462 
12463 	return ret;
12464 }
12465 
12466 /**
12467  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12468  * @pf: board private structure
12469  * @queue_count: the requested queue count for rss.
12470  *
12471  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12472  * count which may be different from the requested queue count.
12473  * Note: expects to be called while under rtnl_lock()
12474  **/
12475 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12476 {
12477 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12478 	int new_rss_size;
12479 
12480 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12481 		return 0;
12482 
12483 	queue_count = min_t(int, queue_count, num_online_cpus());
12484 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12485 
12486 	if (queue_count != vsi->num_queue_pairs) {
12487 		u16 qcount;
12488 
12489 		vsi->req_queue_pairs = queue_count;
12490 		i40e_prep_for_reset(pf);
12491 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12492 			return pf->alloc_rss_size;
12493 
12494 		pf->alloc_rss_size = new_rss_size;
12495 
12496 		i40e_reset_and_rebuild(pf, true, true);
12497 
12498 		/* Discard the user configured hash keys and lut, if less
12499 		 * queues are enabled.
12500 		 */
12501 		if (queue_count < vsi->rss_size) {
12502 			i40e_clear_rss_config_user(vsi);
12503 			dev_dbg(&pf->pdev->dev,
12504 				"discard user configured hash keys and lut\n");
12505 		}
12506 
12507 		/* Reset vsi->rss_size, as number of enabled queues changed */
12508 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12509 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12510 
12511 		i40e_pf_config_rss(pf);
12512 	}
12513 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12514 		 vsi->req_queue_pairs, pf->rss_size_max);
12515 	return pf->alloc_rss_size;
12516 }
12517 
12518 /**
12519  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12520  * @pf: board private structure
12521  **/
12522 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12523 {
12524 	bool min_valid, max_valid;
12525 	u32 max_bw, min_bw;
12526 	int status;
12527 
12528 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12529 					   &min_valid, &max_valid);
12530 
12531 	if (!status) {
12532 		if (min_valid)
12533 			pf->min_bw = min_bw;
12534 		if (max_valid)
12535 			pf->max_bw = max_bw;
12536 	}
12537 
12538 	return status;
12539 }
12540 
12541 /**
12542  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12543  * @pf: board private structure
12544  **/
12545 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12546 {
12547 	struct i40e_aqc_configure_partition_bw_data bw_data;
12548 	int status;
12549 
12550 	memset(&bw_data, 0, sizeof(bw_data));
12551 
12552 	/* Set the valid bit for this PF */
12553 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12554 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12555 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12556 
12557 	/* Set the new bandwidths */
12558 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12559 
12560 	return status;
12561 }
12562 
12563 /**
12564  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12565  * @pf: board private structure
12566  **/
12567 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12568 {
12569 	/* Commit temporary BW setting to permanent NVM image */
12570 	enum i40e_admin_queue_err last_aq_status;
12571 	u16 nvm_word;
12572 	int ret;
12573 
12574 	if (pf->hw.partition_id != 1) {
12575 		dev_info(&pf->pdev->dev,
12576 			 "Commit BW only works on partition 1! This is partition %d",
12577 			 pf->hw.partition_id);
12578 		ret = I40E_NOT_SUPPORTED;
12579 		goto bw_commit_out;
12580 	}
12581 
12582 	/* Acquire NVM for read access */
12583 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12584 	last_aq_status = pf->hw.aq.asq_last_status;
12585 	if (ret) {
12586 		dev_info(&pf->pdev->dev,
12587 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12588 			 ERR_PTR(ret),
12589 			 i40e_aq_str(&pf->hw, last_aq_status));
12590 		goto bw_commit_out;
12591 	}
12592 
12593 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12594 	ret = i40e_aq_read_nvm(&pf->hw,
12595 			       I40E_SR_NVM_CONTROL_WORD,
12596 			       0x10, sizeof(nvm_word), &nvm_word,
12597 			       false, NULL);
12598 	/* Save off last admin queue command status before releasing
12599 	 * the NVM
12600 	 */
12601 	last_aq_status = pf->hw.aq.asq_last_status;
12602 	i40e_release_nvm(&pf->hw);
12603 	if (ret) {
12604 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12605 			 ERR_PTR(ret),
12606 			 i40e_aq_str(&pf->hw, last_aq_status));
12607 		goto bw_commit_out;
12608 	}
12609 
12610 	/* Wait a bit for NVM release to complete */
12611 	msleep(50);
12612 
12613 	/* Acquire NVM for write access */
12614 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12615 	last_aq_status = pf->hw.aq.asq_last_status;
12616 	if (ret) {
12617 		dev_info(&pf->pdev->dev,
12618 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12619 			 ERR_PTR(ret),
12620 			 i40e_aq_str(&pf->hw, last_aq_status));
12621 		goto bw_commit_out;
12622 	}
12623 	/* Write it back out unchanged to initiate update NVM,
12624 	 * which will force a write of the shadow (alt) RAM to
12625 	 * the NVM - thus storing the bandwidth values permanently.
12626 	 */
12627 	ret = i40e_aq_update_nvm(&pf->hw,
12628 				 I40E_SR_NVM_CONTROL_WORD,
12629 				 0x10, sizeof(nvm_word),
12630 				 &nvm_word, true, 0, NULL);
12631 	/* Save off last admin queue command status before releasing
12632 	 * the NVM
12633 	 */
12634 	last_aq_status = pf->hw.aq.asq_last_status;
12635 	i40e_release_nvm(&pf->hw);
12636 	if (ret)
12637 		dev_info(&pf->pdev->dev,
12638 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12639 			 ERR_PTR(ret),
12640 			 i40e_aq_str(&pf->hw, last_aq_status));
12641 bw_commit_out:
12642 
12643 	return ret;
12644 }
12645 
12646 /**
12647  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12648  * if total port shutdown feature is enabled for this PF
12649  * @pf: board private structure
12650  **/
12651 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12652 {
12653 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12654 #define I40E_FEATURES_ENABLE_PTR		0x2A
12655 #define I40E_CURRENT_SETTING_PTR		0x2B
12656 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12657 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12658 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12659 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12660 	int read_status = I40E_SUCCESS;
12661 	u16 sr_emp_sr_settings_ptr = 0;
12662 	u16 features_enable = 0;
12663 	u16 link_behavior = 0;
12664 	bool ret = false;
12665 
12666 	read_status = i40e_read_nvm_word(&pf->hw,
12667 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12668 					 &sr_emp_sr_settings_ptr);
12669 	if (read_status)
12670 		goto err_nvm;
12671 	read_status = i40e_read_nvm_word(&pf->hw,
12672 					 sr_emp_sr_settings_ptr +
12673 					 I40E_FEATURES_ENABLE_PTR,
12674 					 &features_enable);
12675 	if (read_status)
12676 		goto err_nvm;
12677 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12678 		read_status = i40e_read_nvm_module_data(&pf->hw,
12679 							I40E_SR_EMP_SR_SETTINGS_PTR,
12680 							I40E_CURRENT_SETTING_PTR,
12681 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12682 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12683 							&link_behavior);
12684 		if (read_status)
12685 			goto err_nvm;
12686 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12687 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12688 	}
12689 	return ret;
12690 
12691 err_nvm:
12692 	dev_warn(&pf->pdev->dev,
12693 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12694 		 ERR_PTR(read_status));
12695 	return ret;
12696 }
12697 
12698 /**
12699  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12700  * @pf: board private structure to initialize
12701  *
12702  * i40e_sw_init initializes the Adapter private data structure.
12703  * Fields are initialized based on PCI device information and
12704  * OS network device settings (MTU size).
12705  **/
12706 static int i40e_sw_init(struct i40e_pf *pf)
12707 {
12708 	int err = 0;
12709 	int size;
12710 	u16 pow;
12711 
12712 	/* Set default capability flags */
12713 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12714 		    I40E_FLAG_MSI_ENABLED     |
12715 		    I40E_FLAG_MSIX_ENABLED;
12716 
12717 	/* Set default ITR */
12718 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12719 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12720 
12721 	/* Depending on PF configurations, it is possible that the RSS
12722 	 * maximum might end up larger than the available queues
12723 	 */
12724 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12725 	pf->alloc_rss_size = 1;
12726 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12727 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12728 				 pf->hw.func_caps.num_tx_qp);
12729 
12730 	/* find the next higher power-of-2 of num cpus */
12731 	pow = roundup_pow_of_two(num_online_cpus());
12732 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12733 
12734 	if (pf->hw.func_caps.rss) {
12735 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12736 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12737 					   num_online_cpus());
12738 	}
12739 
12740 	/* MFP mode enabled */
12741 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12742 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12743 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12744 		if (i40e_get_partition_bw_setting(pf)) {
12745 			dev_warn(&pf->pdev->dev,
12746 				 "Could not get partition bw settings\n");
12747 		} else {
12748 			dev_info(&pf->pdev->dev,
12749 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12750 				 pf->min_bw, pf->max_bw);
12751 
12752 			/* nudge the Tx scheduler */
12753 			i40e_set_partition_bw_setting(pf);
12754 		}
12755 	}
12756 
12757 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12758 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12759 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12760 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12761 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12762 		    pf->hw.num_partitions > 1)
12763 			dev_info(&pf->pdev->dev,
12764 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12765 		else
12766 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12767 		pf->fdir_pf_filter_count =
12768 				 pf->hw.func_caps.fd_filters_guaranteed;
12769 		pf->hw.fdir_shared_filter_count =
12770 				 pf->hw.func_caps.fd_filters_best_effort;
12771 	}
12772 
12773 	if (pf->hw.mac.type == I40E_MAC_X722) {
12774 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12775 				    I40E_HW_128_QP_RSS_CAPABLE |
12776 				    I40E_HW_ATR_EVICT_CAPABLE |
12777 				    I40E_HW_WB_ON_ITR_CAPABLE |
12778 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12779 				    I40E_HW_NO_PCI_LINK_CHECK |
12780 				    I40E_HW_USE_SET_LLDP_MIB |
12781 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12782 				    I40E_HW_PTP_L4_CAPABLE |
12783 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12784 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12785 
12786 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12787 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12788 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12789 			dev_warn(&pf->pdev->dev,
12790 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12791 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12792 		}
12793 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12794 		   ((pf->hw.aq.api_maj_ver == 1) &&
12795 		    (pf->hw.aq.api_min_ver > 4))) {
12796 		/* Supported in FW API version higher than 1.4 */
12797 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12798 	}
12799 
12800 	/* Enable HW ATR eviction if possible */
12801 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12802 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12803 
12804 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12805 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12806 	    (pf->hw.aq.fw_maj_ver < 4))) {
12807 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12808 		/* No DCB support  for FW < v4.33 */
12809 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12810 	}
12811 
12812 	/* Disable FW LLDP if FW < v4.3 */
12813 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12814 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12815 	    (pf->hw.aq.fw_maj_ver < 4)))
12816 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12817 
12818 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12819 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12820 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12821 	    (pf->hw.aq.fw_maj_ver >= 5)))
12822 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12823 
12824 	/* Enable PTP L4 if FW > v6.0 */
12825 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12826 	    pf->hw.aq.fw_maj_ver >= 6)
12827 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12828 
12829 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12830 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12831 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12832 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12833 	}
12834 
12835 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12836 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12837 		/* IWARP needs one extra vector for CQP just like MISC.*/
12838 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12839 	}
12840 	/* Stopping FW LLDP engine is supported on XL710 and X722
12841 	 * starting from FW versions determined in i40e_init_adminq.
12842 	 * Stopping the FW LLDP engine is not supported on XL710
12843 	 * if NPAR is functioning so unset this hw flag in this case.
12844 	 */
12845 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12846 	    pf->hw.func_caps.npar_enable &&
12847 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12848 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12849 
12850 #ifdef CONFIG_PCI_IOV
12851 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12852 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12853 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12854 		pf->num_req_vfs = min_t(int,
12855 					pf->hw.func_caps.num_vfs,
12856 					I40E_MAX_VF_COUNT);
12857 	}
12858 #endif /* CONFIG_PCI_IOV */
12859 	pf->eeprom_version = 0xDEAD;
12860 	pf->lan_veb = I40E_NO_VEB;
12861 	pf->lan_vsi = I40E_NO_VSI;
12862 
12863 	/* By default FW has this off for performance reasons */
12864 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12865 
12866 	/* set up queue assignment tracking */
12867 	size = sizeof(struct i40e_lump_tracking)
12868 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12869 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12870 	if (!pf->qp_pile) {
12871 		err = -ENOMEM;
12872 		goto sw_init_done;
12873 	}
12874 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12875 
12876 	pf->tx_timeout_recovery_level = 1;
12877 
12878 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12879 	    i40e_is_total_port_shutdown_enabled(pf)) {
12880 		/* Link down on close must be on when total port shutdown
12881 		 * is enabled for a given port
12882 		 */
12883 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12884 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12885 		dev_info(&pf->pdev->dev,
12886 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12887 	}
12888 	mutex_init(&pf->switch_mutex);
12889 
12890 sw_init_done:
12891 	return err;
12892 }
12893 
12894 /**
12895  * i40e_set_ntuple - set the ntuple feature flag and take action
12896  * @pf: board private structure to initialize
12897  * @features: the feature set that the stack is suggesting
12898  *
12899  * returns a bool to indicate if reset needs to happen
12900  **/
12901 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12902 {
12903 	bool need_reset = false;
12904 
12905 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12906 	 * the state changed, we need to reset.
12907 	 */
12908 	if (features & NETIF_F_NTUPLE) {
12909 		/* Enable filters and mark for reset */
12910 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12911 			need_reset = true;
12912 		/* enable FD_SB only if there is MSI-X vector and no cloud
12913 		 * filters exist
12914 		 */
12915 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12916 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12917 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12918 		}
12919 	} else {
12920 		/* turn off filters, mark for reset and clear SW filter list */
12921 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12922 			need_reset = true;
12923 			i40e_fdir_filter_exit(pf);
12924 		}
12925 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12926 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12927 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12928 
12929 		/* reset fd counters */
12930 		pf->fd_add_err = 0;
12931 		pf->fd_atr_cnt = 0;
12932 		/* if ATR was auto disabled it can be re-enabled. */
12933 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12934 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12935 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12936 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12937 	}
12938 	return need_reset;
12939 }
12940 
12941 /**
12942  * i40e_clear_rss_lut - clear the rx hash lookup table
12943  * @vsi: the VSI being configured
12944  **/
12945 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12946 {
12947 	struct i40e_pf *pf = vsi->back;
12948 	struct i40e_hw *hw = &pf->hw;
12949 	u16 vf_id = vsi->vf_id;
12950 	u8 i;
12951 
12952 	if (vsi->type == I40E_VSI_MAIN) {
12953 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12954 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12955 	} else if (vsi->type == I40E_VSI_SRIOV) {
12956 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12957 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12958 	} else {
12959 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12960 	}
12961 }
12962 
12963 /**
12964  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12965  * @vsi: ptr to VSI
12966  * @ena: flag to indicate the on/off setting
12967  */
12968 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12969 {
12970 	bool if_running = netif_running(vsi->netdev) &&
12971 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12972 	int ret;
12973 
12974 	if (if_running)
12975 		i40e_down(vsi);
12976 
12977 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12978 	if (ret)
12979 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12980 	if (if_running)
12981 		i40e_up(vsi);
12982 
12983 	return ret;
12984 }
12985 
12986 /**
12987  * i40e_set_features - set the netdev feature flags
12988  * @netdev: ptr to the netdev being adjusted
12989  * @features: the feature set that the stack is suggesting
12990  * Note: expects to be called while under rtnl_lock()
12991  **/
12992 static int i40e_set_features(struct net_device *netdev,
12993 			     netdev_features_t features)
12994 {
12995 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12996 	struct i40e_vsi *vsi = np->vsi;
12997 	struct i40e_pf *pf = vsi->back;
12998 	bool need_reset;
12999 
13000 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
13001 		i40e_pf_config_rss(pf);
13002 	else if (!(features & NETIF_F_RXHASH) &&
13003 		 netdev->features & NETIF_F_RXHASH)
13004 		i40e_clear_rss_lut(vsi);
13005 
13006 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
13007 		i40e_vlan_stripping_enable(vsi);
13008 	else
13009 		i40e_vlan_stripping_disable(vsi);
13010 
13011 	if (!(features & NETIF_F_HW_TC) &&
13012 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13013 		dev_err(&pf->pdev->dev,
13014 			"Offloaded tc filters active, can't turn hw_tc_offload off");
13015 		return -EINVAL;
13016 	}
13017 
13018 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13019 		i40e_del_all_macvlans(vsi);
13020 
13021 	need_reset = i40e_set_ntuple(pf, features);
13022 
13023 	if (need_reset)
13024 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13025 
13026 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13027 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13028 
13029 	return 0;
13030 }
13031 
13032 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13033 				    unsigned int table, unsigned int idx,
13034 				    struct udp_tunnel_info *ti)
13035 {
13036 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13037 	struct i40e_hw *hw = &np->vsi->back->hw;
13038 	u8 type, filter_index;
13039 	int ret;
13040 
13041 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13042 						   I40E_AQC_TUNNEL_TYPE_NGE;
13043 
13044 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13045 				     NULL);
13046 	if (ret) {
13047 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13048 			    ERR_PTR(ret),
13049 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13050 		return -EIO;
13051 	}
13052 
13053 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13054 	return 0;
13055 }
13056 
13057 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13058 				      unsigned int table, unsigned int idx,
13059 				      struct udp_tunnel_info *ti)
13060 {
13061 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13062 	struct i40e_hw *hw = &np->vsi->back->hw;
13063 	int ret;
13064 
13065 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13066 	if (ret) {
13067 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13068 			    ERR_PTR(ret),
13069 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13070 		return -EIO;
13071 	}
13072 
13073 	return 0;
13074 }
13075 
13076 static int i40e_get_phys_port_id(struct net_device *netdev,
13077 				 struct netdev_phys_item_id *ppid)
13078 {
13079 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13080 	struct i40e_pf *pf = np->vsi->back;
13081 	struct i40e_hw *hw = &pf->hw;
13082 
13083 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13084 		return -EOPNOTSUPP;
13085 
13086 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13087 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13088 
13089 	return 0;
13090 }
13091 
13092 /**
13093  * i40e_ndo_fdb_add - add an entry to the hardware database
13094  * @ndm: the input from the stack
13095  * @tb: pointer to array of nladdr (unused)
13096  * @dev: the net device pointer
13097  * @addr: the MAC address entry being added
13098  * @vid: VLAN ID
13099  * @flags: instructions from stack about fdb operation
13100  * @extack: netlink extended ack, unused currently
13101  */
13102 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13103 			    struct net_device *dev,
13104 			    const unsigned char *addr, u16 vid,
13105 			    u16 flags,
13106 			    struct netlink_ext_ack *extack)
13107 {
13108 	struct i40e_netdev_priv *np = netdev_priv(dev);
13109 	struct i40e_pf *pf = np->vsi->back;
13110 	int err = 0;
13111 
13112 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13113 		return -EOPNOTSUPP;
13114 
13115 	if (vid) {
13116 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13117 		return -EINVAL;
13118 	}
13119 
13120 	/* Hardware does not support aging addresses so if a
13121 	 * ndm_state is given only allow permanent addresses
13122 	 */
13123 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13124 		netdev_info(dev, "FDB only supports static addresses\n");
13125 		return -EINVAL;
13126 	}
13127 
13128 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13129 		err = dev_uc_add_excl(dev, addr);
13130 	else if (is_multicast_ether_addr(addr))
13131 		err = dev_mc_add_excl(dev, addr);
13132 	else
13133 		err = -EINVAL;
13134 
13135 	/* Only return duplicate errors if NLM_F_EXCL is set */
13136 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13137 		err = 0;
13138 
13139 	return err;
13140 }
13141 
13142 /**
13143  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13144  * @dev: the netdev being configured
13145  * @nlh: RTNL message
13146  * @flags: bridge flags
13147  * @extack: netlink extended ack
13148  *
13149  * Inserts a new hardware bridge if not already created and
13150  * enables the bridging mode requested (VEB or VEPA). If the
13151  * hardware bridge has already been inserted and the request
13152  * is to change the mode then that requires a PF reset to
13153  * allow rebuild of the components with required hardware
13154  * bridge mode enabled.
13155  *
13156  * Note: expects to be called while under rtnl_lock()
13157  **/
13158 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13159 				   struct nlmsghdr *nlh,
13160 				   u16 flags,
13161 				   struct netlink_ext_ack *extack)
13162 {
13163 	struct i40e_netdev_priv *np = netdev_priv(dev);
13164 	struct i40e_vsi *vsi = np->vsi;
13165 	struct i40e_pf *pf = vsi->back;
13166 	struct i40e_veb *veb = NULL;
13167 	struct nlattr *attr, *br_spec;
13168 	int i, rem;
13169 
13170 	/* Only for PF VSI for now */
13171 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13172 		return -EOPNOTSUPP;
13173 
13174 	/* Find the HW bridge for PF VSI */
13175 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13176 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13177 			veb = pf->veb[i];
13178 	}
13179 
13180 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13181 	if (!br_spec)
13182 		return -EINVAL;
13183 
13184 	nla_for_each_nested(attr, br_spec, rem) {
13185 		__u16 mode;
13186 
13187 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13188 			continue;
13189 
13190 		mode = nla_get_u16(attr);
13191 		if ((mode != BRIDGE_MODE_VEPA) &&
13192 		    (mode != BRIDGE_MODE_VEB))
13193 			return -EINVAL;
13194 
13195 		/* Insert a new HW bridge */
13196 		if (!veb) {
13197 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13198 					     vsi->tc_config.enabled_tc);
13199 			if (veb) {
13200 				veb->bridge_mode = mode;
13201 				i40e_config_bridge_mode(veb);
13202 			} else {
13203 				/* No Bridge HW offload available */
13204 				return -ENOENT;
13205 			}
13206 			break;
13207 		} else if (mode != veb->bridge_mode) {
13208 			/* Existing HW bridge but different mode needs reset */
13209 			veb->bridge_mode = mode;
13210 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13211 			if (mode == BRIDGE_MODE_VEB)
13212 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13213 			else
13214 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13215 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13216 			break;
13217 		}
13218 	}
13219 
13220 	return 0;
13221 }
13222 
13223 /**
13224  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13225  * @skb: skb buff
13226  * @pid: process id
13227  * @seq: RTNL message seq #
13228  * @dev: the netdev being configured
13229  * @filter_mask: unused
13230  * @nlflags: netlink flags passed in
13231  *
13232  * Return the mode in which the hardware bridge is operating in
13233  * i.e VEB or VEPA.
13234  **/
13235 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13236 				   struct net_device *dev,
13237 				   u32 __always_unused filter_mask,
13238 				   int nlflags)
13239 {
13240 	struct i40e_netdev_priv *np = netdev_priv(dev);
13241 	struct i40e_vsi *vsi = np->vsi;
13242 	struct i40e_pf *pf = vsi->back;
13243 	struct i40e_veb *veb = NULL;
13244 	int i;
13245 
13246 	/* Only for PF VSI for now */
13247 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13248 		return -EOPNOTSUPP;
13249 
13250 	/* Find the HW bridge for the PF VSI */
13251 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13252 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13253 			veb = pf->veb[i];
13254 	}
13255 
13256 	if (!veb)
13257 		return 0;
13258 
13259 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13260 				       0, 0, nlflags, filter_mask, NULL);
13261 }
13262 
13263 /**
13264  * i40e_features_check - Validate encapsulated packet conforms to limits
13265  * @skb: skb buff
13266  * @dev: This physical port's netdev
13267  * @features: Offload features that the stack believes apply
13268  **/
13269 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13270 					     struct net_device *dev,
13271 					     netdev_features_t features)
13272 {
13273 	size_t len;
13274 
13275 	/* No point in doing any of this if neither checksum nor GSO are
13276 	 * being requested for this frame.  We can rule out both by just
13277 	 * checking for CHECKSUM_PARTIAL
13278 	 */
13279 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13280 		return features;
13281 
13282 	/* We cannot support GSO if the MSS is going to be less than
13283 	 * 64 bytes.  If it is then we need to drop support for GSO.
13284 	 */
13285 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13286 		features &= ~NETIF_F_GSO_MASK;
13287 
13288 	/* MACLEN can support at most 63 words */
13289 	len = skb_network_header(skb) - skb->data;
13290 	if (len & ~(63 * 2))
13291 		goto out_err;
13292 
13293 	/* IPLEN and EIPLEN can support at most 127 dwords */
13294 	len = skb_transport_header(skb) - skb_network_header(skb);
13295 	if (len & ~(127 * 4))
13296 		goto out_err;
13297 
13298 	if (skb->encapsulation) {
13299 		/* L4TUNLEN can support 127 words */
13300 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13301 		if (len & ~(127 * 2))
13302 			goto out_err;
13303 
13304 		/* IPLEN can support at most 127 dwords */
13305 		len = skb_inner_transport_header(skb) -
13306 		      skb_inner_network_header(skb);
13307 		if (len & ~(127 * 4))
13308 			goto out_err;
13309 	}
13310 
13311 	/* No need to validate L4LEN as TCP is the only protocol with a
13312 	 * flexible value and we support all possible values supported
13313 	 * by TCP, which is at most 15 dwords
13314 	 */
13315 
13316 	return features;
13317 out_err:
13318 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13319 }
13320 
13321 /**
13322  * i40e_xdp_setup - add/remove an XDP program
13323  * @vsi: VSI to changed
13324  * @prog: XDP program
13325  * @extack: netlink extended ack
13326  **/
13327 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13328 			  struct netlink_ext_ack *extack)
13329 {
13330 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13331 	struct i40e_pf *pf = vsi->back;
13332 	struct bpf_prog *old_prog;
13333 	bool need_reset;
13334 	int i;
13335 
13336 	/* Don't allow frames that span over multiple buffers */
13337 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13338 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13339 		return -EINVAL;
13340 	}
13341 
13342 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13343 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13344 
13345 	if (need_reset)
13346 		i40e_prep_for_reset(pf);
13347 
13348 	/* VSI shall be deleted in a moment, just return EINVAL */
13349 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13350 		return -EINVAL;
13351 
13352 	old_prog = xchg(&vsi->xdp_prog, prog);
13353 
13354 	if (need_reset) {
13355 		if (!prog) {
13356 			xdp_features_clear_redirect_target(vsi->netdev);
13357 			/* Wait until ndo_xsk_wakeup completes. */
13358 			synchronize_rcu();
13359 		}
13360 		i40e_reset_and_rebuild(pf, true, true);
13361 	}
13362 
13363 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13364 		if (i40e_realloc_rx_bi_zc(vsi, true))
13365 			return -ENOMEM;
13366 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13367 		if (i40e_realloc_rx_bi_zc(vsi, false))
13368 			return -ENOMEM;
13369 	}
13370 
13371 	for (i = 0; i < vsi->num_queue_pairs; i++)
13372 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13373 
13374 	if (old_prog)
13375 		bpf_prog_put(old_prog);
13376 
13377 	/* Kick start the NAPI context if there is an AF_XDP socket open
13378 	 * on that queue id. This so that receiving will start.
13379 	 */
13380 	if (need_reset && prog) {
13381 		for (i = 0; i < vsi->num_queue_pairs; i++)
13382 			if (vsi->xdp_rings[i]->xsk_pool)
13383 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13384 						      XDP_WAKEUP_RX);
13385 		xdp_features_set_redirect_target(vsi->netdev, true);
13386 	}
13387 
13388 	return 0;
13389 }
13390 
13391 /**
13392  * i40e_enter_busy_conf - Enters busy config state
13393  * @vsi: vsi
13394  *
13395  * Returns 0 on success, <0 for failure.
13396  **/
13397 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13398 {
13399 	struct i40e_pf *pf = vsi->back;
13400 	int timeout = 50;
13401 
13402 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13403 		timeout--;
13404 		if (!timeout)
13405 			return -EBUSY;
13406 		usleep_range(1000, 2000);
13407 	}
13408 
13409 	return 0;
13410 }
13411 
13412 /**
13413  * i40e_exit_busy_conf - Exits busy config state
13414  * @vsi: vsi
13415  **/
13416 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13417 {
13418 	struct i40e_pf *pf = vsi->back;
13419 
13420 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13421 }
13422 
13423 /**
13424  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13425  * @vsi: vsi
13426  * @queue_pair: queue pair
13427  **/
13428 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13429 {
13430 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13431 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13432 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13433 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13434 	if (i40e_enabled_xdp_vsi(vsi)) {
13435 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13436 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13437 	}
13438 }
13439 
13440 /**
13441  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13442  * @vsi: vsi
13443  * @queue_pair: queue pair
13444  **/
13445 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13446 {
13447 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13448 	if (i40e_enabled_xdp_vsi(vsi)) {
13449 		/* Make sure that in-progress ndo_xdp_xmit calls are
13450 		 * completed.
13451 		 */
13452 		synchronize_rcu();
13453 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13454 	}
13455 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13456 }
13457 
13458 /**
13459  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13460  * @vsi: vsi
13461  * @queue_pair: queue pair
13462  * @enable: true for enable, false for disable
13463  **/
13464 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13465 					bool enable)
13466 {
13467 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13468 	struct i40e_q_vector *q_vector = rxr->q_vector;
13469 
13470 	if (!vsi->netdev)
13471 		return;
13472 
13473 	/* All rings in a qp belong to the same qvector. */
13474 	if (q_vector->rx.ring || q_vector->tx.ring) {
13475 		if (enable)
13476 			napi_enable(&q_vector->napi);
13477 		else
13478 			napi_disable(&q_vector->napi);
13479 	}
13480 }
13481 
13482 /**
13483  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13484  * @vsi: vsi
13485  * @queue_pair: queue pair
13486  * @enable: true for enable, false for disable
13487  *
13488  * Returns 0 on success, <0 on failure.
13489  **/
13490 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13491 					bool enable)
13492 {
13493 	struct i40e_pf *pf = vsi->back;
13494 	int pf_q, ret = 0;
13495 
13496 	pf_q = vsi->base_queue + queue_pair;
13497 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13498 				     false /*is xdp*/, enable);
13499 	if (ret) {
13500 		dev_info(&pf->pdev->dev,
13501 			 "VSI seid %d Tx ring %d %sable timeout\n",
13502 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13503 		return ret;
13504 	}
13505 
13506 	i40e_control_rx_q(pf, pf_q, enable);
13507 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13508 	if (ret) {
13509 		dev_info(&pf->pdev->dev,
13510 			 "VSI seid %d Rx ring %d %sable timeout\n",
13511 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13512 		return ret;
13513 	}
13514 
13515 	/* Due to HW errata, on Rx disable only, the register can
13516 	 * indicate done before it really is. Needs 50ms to be sure
13517 	 */
13518 	if (!enable)
13519 		mdelay(50);
13520 
13521 	if (!i40e_enabled_xdp_vsi(vsi))
13522 		return ret;
13523 
13524 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13525 				     pf_q + vsi->alloc_queue_pairs,
13526 				     true /*is xdp*/, enable);
13527 	if (ret) {
13528 		dev_info(&pf->pdev->dev,
13529 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13530 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13531 	}
13532 
13533 	return ret;
13534 }
13535 
13536 /**
13537  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13538  * @vsi: vsi
13539  * @queue_pair: queue_pair
13540  **/
13541 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13542 {
13543 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13544 	struct i40e_pf *pf = vsi->back;
13545 	struct i40e_hw *hw = &pf->hw;
13546 
13547 	/* All rings in a qp belong to the same qvector. */
13548 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13549 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13550 	else
13551 		i40e_irq_dynamic_enable_icr0(pf);
13552 
13553 	i40e_flush(hw);
13554 }
13555 
13556 /**
13557  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13558  * @vsi: vsi
13559  * @queue_pair: queue_pair
13560  **/
13561 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13562 {
13563 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13564 	struct i40e_pf *pf = vsi->back;
13565 	struct i40e_hw *hw = &pf->hw;
13566 
13567 	/* For simplicity, instead of removing the qp interrupt causes
13568 	 * from the interrupt linked list, we simply disable the interrupt, and
13569 	 * leave the list intact.
13570 	 *
13571 	 * All rings in a qp belong to the same qvector.
13572 	 */
13573 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13574 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13575 
13576 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13577 		i40e_flush(hw);
13578 		synchronize_irq(pf->msix_entries[intpf].vector);
13579 	} else {
13580 		/* Legacy and MSI mode - this stops all interrupt handling */
13581 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13582 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13583 		i40e_flush(hw);
13584 		synchronize_irq(pf->pdev->irq);
13585 	}
13586 }
13587 
13588 /**
13589  * i40e_queue_pair_disable - Disables a queue pair
13590  * @vsi: vsi
13591  * @queue_pair: queue pair
13592  *
13593  * Returns 0 on success, <0 on failure.
13594  **/
13595 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13596 {
13597 	int err;
13598 
13599 	err = i40e_enter_busy_conf(vsi);
13600 	if (err)
13601 		return err;
13602 
13603 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13604 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13605 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13606 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13607 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13608 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13609 
13610 	return err;
13611 }
13612 
13613 /**
13614  * i40e_queue_pair_enable - Enables a queue pair
13615  * @vsi: vsi
13616  * @queue_pair: queue pair
13617  *
13618  * Returns 0 on success, <0 on failure.
13619  **/
13620 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13621 {
13622 	int err;
13623 
13624 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13625 	if (err)
13626 		return err;
13627 
13628 	if (i40e_enabled_xdp_vsi(vsi)) {
13629 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13630 		if (err)
13631 			return err;
13632 	}
13633 
13634 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13635 	if (err)
13636 		return err;
13637 
13638 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13639 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13640 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13641 
13642 	i40e_exit_busy_conf(vsi);
13643 
13644 	return err;
13645 }
13646 
13647 /**
13648  * i40e_xdp - implements ndo_bpf for i40e
13649  * @dev: netdevice
13650  * @xdp: XDP command
13651  **/
13652 static int i40e_xdp(struct net_device *dev,
13653 		    struct netdev_bpf *xdp)
13654 {
13655 	struct i40e_netdev_priv *np = netdev_priv(dev);
13656 	struct i40e_vsi *vsi = np->vsi;
13657 
13658 	if (vsi->type != I40E_VSI_MAIN)
13659 		return -EINVAL;
13660 
13661 	switch (xdp->command) {
13662 	case XDP_SETUP_PROG:
13663 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13664 	case XDP_SETUP_XSK_POOL:
13665 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13666 					   xdp->xsk.queue_id);
13667 	default:
13668 		return -EINVAL;
13669 	}
13670 }
13671 
13672 static const struct net_device_ops i40e_netdev_ops = {
13673 	.ndo_open		= i40e_open,
13674 	.ndo_stop		= i40e_close,
13675 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13676 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13677 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13678 	.ndo_validate_addr	= eth_validate_addr,
13679 	.ndo_set_mac_address	= i40e_set_mac,
13680 	.ndo_change_mtu		= i40e_change_mtu,
13681 	.ndo_eth_ioctl		= i40e_ioctl,
13682 	.ndo_tx_timeout		= i40e_tx_timeout,
13683 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13684 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13685 #ifdef CONFIG_NET_POLL_CONTROLLER
13686 	.ndo_poll_controller	= i40e_netpoll,
13687 #endif
13688 	.ndo_setup_tc		= __i40e_setup_tc,
13689 	.ndo_select_queue	= i40e_lan_select_queue,
13690 	.ndo_set_features	= i40e_set_features,
13691 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13692 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13693 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13694 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13695 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13696 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13697 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13698 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13699 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13700 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13701 	.ndo_features_check	= i40e_features_check,
13702 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13703 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13704 	.ndo_bpf		= i40e_xdp,
13705 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13706 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13707 	.ndo_dfwd_add_station	= i40e_fwd_add,
13708 	.ndo_dfwd_del_station	= i40e_fwd_del,
13709 };
13710 
13711 /**
13712  * i40e_config_netdev - Setup the netdev flags
13713  * @vsi: the VSI being configured
13714  *
13715  * Returns 0 on success, negative value on failure
13716  **/
13717 static int i40e_config_netdev(struct i40e_vsi *vsi)
13718 {
13719 	struct i40e_pf *pf = vsi->back;
13720 	struct i40e_hw *hw = &pf->hw;
13721 	struct i40e_netdev_priv *np;
13722 	struct net_device *netdev;
13723 	u8 broadcast[ETH_ALEN];
13724 	u8 mac_addr[ETH_ALEN];
13725 	int etherdev_size;
13726 	netdev_features_t hw_enc_features;
13727 	netdev_features_t hw_features;
13728 
13729 	etherdev_size = sizeof(struct i40e_netdev_priv);
13730 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13731 	if (!netdev)
13732 		return -ENOMEM;
13733 
13734 	vsi->netdev = netdev;
13735 	np = netdev_priv(netdev);
13736 	np->vsi = vsi;
13737 
13738 	hw_enc_features = NETIF_F_SG			|
13739 			  NETIF_F_HW_CSUM		|
13740 			  NETIF_F_HIGHDMA		|
13741 			  NETIF_F_SOFT_FEATURES		|
13742 			  NETIF_F_TSO			|
13743 			  NETIF_F_TSO_ECN		|
13744 			  NETIF_F_TSO6			|
13745 			  NETIF_F_GSO_GRE		|
13746 			  NETIF_F_GSO_GRE_CSUM		|
13747 			  NETIF_F_GSO_PARTIAL		|
13748 			  NETIF_F_GSO_IPXIP4		|
13749 			  NETIF_F_GSO_IPXIP6		|
13750 			  NETIF_F_GSO_UDP_TUNNEL	|
13751 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13752 			  NETIF_F_GSO_UDP_L4		|
13753 			  NETIF_F_SCTP_CRC		|
13754 			  NETIF_F_RXHASH		|
13755 			  NETIF_F_RXCSUM		|
13756 			  0;
13757 
13758 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13759 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13760 
13761 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13762 
13763 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13764 
13765 	netdev->hw_enc_features |= hw_enc_features;
13766 
13767 	/* record features VLANs can make use of */
13768 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13769 
13770 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13771 				   NETIF_F_GSO_GRE_CSUM |	\
13772 				   NETIF_F_GSO_IPXIP4 |		\
13773 				   NETIF_F_GSO_IPXIP6 |		\
13774 				   NETIF_F_GSO_UDP_TUNNEL |	\
13775 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13776 
13777 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13778 	netdev->features |= NETIF_F_GSO_PARTIAL |
13779 			    I40E_GSO_PARTIAL_FEATURES;
13780 
13781 	netdev->mpls_features |= NETIF_F_SG;
13782 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13783 	netdev->mpls_features |= NETIF_F_TSO;
13784 	netdev->mpls_features |= NETIF_F_TSO6;
13785 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13786 
13787 	/* enable macvlan offloads */
13788 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13789 
13790 	hw_features = hw_enc_features		|
13791 		      NETIF_F_HW_VLAN_CTAG_TX	|
13792 		      NETIF_F_HW_VLAN_CTAG_RX;
13793 
13794 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13795 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13796 
13797 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13798 
13799 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13800 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13801 
13802 	netdev->features &= ~NETIF_F_HW_TC;
13803 
13804 	if (vsi->type == I40E_VSI_MAIN) {
13805 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13806 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13807 		/* The following steps are necessary for two reasons. First,
13808 		 * some older NVM configurations load a default MAC-VLAN
13809 		 * filter that will accept any tagged packet, and we want to
13810 		 * replace this with a normal filter. Additionally, it is
13811 		 * possible our MAC address was provided by the platform using
13812 		 * Open Firmware or similar.
13813 		 *
13814 		 * Thus, we need to remove the default filter and install one
13815 		 * specific to the MAC address.
13816 		 */
13817 		i40e_rm_default_mac_filter(vsi, mac_addr);
13818 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13819 		i40e_add_mac_filter(vsi, mac_addr);
13820 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13821 
13822 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13823 				       NETDEV_XDP_ACT_REDIRECT |
13824 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13825 				       NETDEV_XDP_ACT_RX_SG;
13826 	} else {
13827 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13828 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13829 		 * the end, which is 4 bytes long, so force truncation of the
13830 		 * original name by IFNAMSIZ - 4
13831 		 */
13832 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13833 			 IFNAMSIZ - 4,
13834 			 pf->vsi[pf->lan_vsi]->netdev->name);
13835 		eth_random_addr(mac_addr);
13836 
13837 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13838 		i40e_add_mac_filter(vsi, mac_addr);
13839 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13840 	}
13841 
13842 	/* Add the broadcast filter so that we initially will receive
13843 	 * broadcast packets. Note that when a new VLAN is first added the
13844 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13845 	 * specific filters as part of transitioning into "vlan" operation.
13846 	 * When more VLANs are added, the driver will copy each existing MAC
13847 	 * filter and add it for the new VLAN.
13848 	 *
13849 	 * Broadcast filters are handled specially by
13850 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13851 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13852 	 * filter. The subtask will update the correct broadcast promiscuous
13853 	 * bits as VLANs become active or inactive.
13854 	 */
13855 	eth_broadcast_addr(broadcast);
13856 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13857 	i40e_add_mac_filter(vsi, broadcast);
13858 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13859 
13860 	eth_hw_addr_set(netdev, mac_addr);
13861 	ether_addr_copy(netdev->perm_addr, mac_addr);
13862 
13863 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13864 	netdev->neigh_priv_len = sizeof(u32) * 4;
13865 
13866 	netdev->priv_flags |= IFF_UNICAST_FLT;
13867 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13868 	/* Setup netdev TC information */
13869 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13870 
13871 	netdev->netdev_ops = &i40e_netdev_ops;
13872 	netdev->watchdog_timeo = 5 * HZ;
13873 	i40e_set_ethtool_ops(netdev);
13874 
13875 	/* MTU range: 68 - 9706 */
13876 	netdev->min_mtu = ETH_MIN_MTU;
13877 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13878 
13879 	return 0;
13880 }
13881 
13882 /**
13883  * i40e_vsi_delete - Delete a VSI from the switch
13884  * @vsi: the VSI being removed
13885  *
13886  * Returns 0 on success, negative value on failure
13887  **/
13888 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13889 {
13890 	/* remove default VSI is not allowed */
13891 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13892 		return;
13893 
13894 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13895 }
13896 
13897 /**
13898  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13899  * @vsi: the VSI being queried
13900  *
13901  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13902  **/
13903 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13904 {
13905 	struct i40e_veb *veb;
13906 	struct i40e_pf *pf = vsi->back;
13907 
13908 	/* Uplink is not a bridge so default to VEB */
13909 	if (vsi->veb_idx >= I40E_MAX_VEB)
13910 		return 1;
13911 
13912 	veb = pf->veb[vsi->veb_idx];
13913 	if (!veb) {
13914 		dev_info(&pf->pdev->dev,
13915 			 "There is no veb associated with the bridge\n");
13916 		return -ENOENT;
13917 	}
13918 
13919 	/* Uplink is a bridge in VEPA mode */
13920 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13921 		return 0;
13922 	} else {
13923 		/* Uplink is a bridge in VEB mode */
13924 		return 1;
13925 	}
13926 
13927 	/* VEPA is now default bridge, so return 0 */
13928 	return 0;
13929 }
13930 
13931 /**
13932  * i40e_add_vsi - Add a VSI to the switch
13933  * @vsi: the VSI being configured
13934  *
13935  * This initializes a VSI context depending on the VSI type to be added and
13936  * passes it down to the add_vsi aq command.
13937  **/
13938 static int i40e_add_vsi(struct i40e_vsi *vsi)
13939 {
13940 	int ret = -ENODEV;
13941 	struct i40e_pf *pf = vsi->back;
13942 	struct i40e_hw *hw = &pf->hw;
13943 	struct i40e_vsi_context ctxt;
13944 	struct i40e_mac_filter *f;
13945 	struct hlist_node *h;
13946 	int bkt;
13947 
13948 	u8 enabled_tc = 0x1; /* TC0 enabled */
13949 	int f_count = 0;
13950 
13951 	memset(&ctxt, 0, sizeof(ctxt));
13952 	switch (vsi->type) {
13953 	case I40E_VSI_MAIN:
13954 		/* The PF's main VSI is already setup as part of the
13955 		 * device initialization, so we'll not bother with
13956 		 * the add_vsi call, but we will retrieve the current
13957 		 * VSI context.
13958 		 */
13959 		ctxt.seid = pf->main_vsi_seid;
13960 		ctxt.pf_num = pf->hw.pf_id;
13961 		ctxt.vf_num = 0;
13962 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13963 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13964 		if (ret) {
13965 			dev_info(&pf->pdev->dev,
13966 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13967 				 ERR_PTR(ret),
13968 				 i40e_aq_str(&pf->hw,
13969 					     pf->hw.aq.asq_last_status));
13970 			return -ENOENT;
13971 		}
13972 		vsi->info = ctxt.info;
13973 		vsi->info.valid_sections = 0;
13974 
13975 		vsi->seid = ctxt.seid;
13976 		vsi->id = ctxt.vsi_number;
13977 
13978 		enabled_tc = i40e_pf_get_tc_map(pf);
13979 
13980 		/* Source pruning is enabled by default, so the flag is
13981 		 * negative logic - if it's set, we need to fiddle with
13982 		 * the VSI to disable source pruning.
13983 		 */
13984 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13985 			memset(&ctxt, 0, sizeof(ctxt));
13986 			ctxt.seid = pf->main_vsi_seid;
13987 			ctxt.pf_num = pf->hw.pf_id;
13988 			ctxt.vf_num = 0;
13989 			ctxt.info.valid_sections |=
13990 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13991 			ctxt.info.switch_id =
13992 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13993 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13994 			if (ret) {
13995 				dev_info(&pf->pdev->dev,
13996 					 "update vsi failed, err %d aq_err %s\n",
13997 					 ret,
13998 					 i40e_aq_str(&pf->hw,
13999 						     pf->hw.aq.asq_last_status));
14000 				ret = -ENOENT;
14001 				goto err;
14002 			}
14003 		}
14004 
14005 		/* MFP mode setup queue map and update VSI */
14006 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
14007 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
14008 			memset(&ctxt, 0, sizeof(ctxt));
14009 			ctxt.seid = pf->main_vsi_seid;
14010 			ctxt.pf_num = pf->hw.pf_id;
14011 			ctxt.vf_num = 0;
14012 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
14013 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14014 			if (ret) {
14015 				dev_info(&pf->pdev->dev,
14016 					 "update vsi failed, err %pe aq_err %s\n",
14017 					 ERR_PTR(ret),
14018 					 i40e_aq_str(&pf->hw,
14019 						    pf->hw.aq.asq_last_status));
14020 				ret = -ENOENT;
14021 				goto err;
14022 			}
14023 			/* update the local VSI info queue map */
14024 			i40e_vsi_update_queue_map(vsi, &ctxt);
14025 			vsi->info.valid_sections = 0;
14026 		} else {
14027 			/* Default/Main VSI is only enabled for TC0
14028 			 * reconfigure it to enable all TCs that are
14029 			 * available on the port in SFP mode.
14030 			 * For MFP case the iSCSI PF would use this
14031 			 * flow to enable LAN+iSCSI TC.
14032 			 */
14033 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
14034 			if (ret) {
14035 				/* Single TC condition is not fatal,
14036 				 * message and continue
14037 				 */
14038 				dev_info(&pf->pdev->dev,
14039 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14040 					 enabled_tc,
14041 					 ERR_PTR(ret),
14042 					 i40e_aq_str(&pf->hw,
14043 						    pf->hw.aq.asq_last_status));
14044 			}
14045 		}
14046 		break;
14047 
14048 	case I40E_VSI_FDIR:
14049 		ctxt.pf_num = hw->pf_id;
14050 		ctxt.vf_num = 0;
14051 		ctxt.uplink_seid = vsi->uplink_seid;
14052 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14053 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14054 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14055 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14056 			ctxt.info.valid_sections |=
14057 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14058 			ctxt.info.switch_id =
14059 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14060 		}
14061 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14062 		break;
14063 
14064 	case I40E_VSI_VMDQ2:
14065 		ctxt.pf_num = hw->pf_id;
14066 		ctxt.vf_num = 0;
14067 		ctxt.uplink_seid = vsi->uplink_seid;
14068 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14069 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14070 
14071 		/* This VSI is connected to VEB so the switch_id
14072 		 * should be set to zero by default.
14073 		 */
14074 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14075 			ctxt.info.valid_sections |=
14076 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14077 			ctxt.info.switch_id =
14078 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14079 		}
14080 
14081 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14082 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14083 		break;
14084 
14085 	case I40E_VSI_SRIOV:
14086 		ctxt.pf_num = hw->pf_id;
14087 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14088 		ctxt.uplink_seid = vsi->uplink_seid;
14089 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14090 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14091 
14092 		/* This VSI is connected to VEB so the switch_id
14093 		 * should be set to zero by default.
14094 		 */
14095 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14096 			ctxt.info.valid_sections |=
14097 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14098 			ctxt.info.switch_id =
14099 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14100 		}
14101 
14102 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14103 			ctxt.info.valid_sections |=
14104 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14105 			ctxt.info.queueing_opt_flags |=
14106 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14107 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14108 		}
14109 
14110 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14111 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14112 		if (pf->vf[vsi->vf_id].spoofchk) {
14113 			ctxt.info.valid_sections |=
14114 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14115 			ctxt.info.sec_flags |=
14116 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14117 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14118 		}
14119 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14120 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14121 		break;
14122 
14123 	case I40E_VSI_IWARP:
14124 		/* send down message to iWARP */
14125 		break;
14126 
14127 	default:
14128 		return -ENODEV;
14129 	}
14130 
14131 	if (vsi->type != I40E_VSI_MAIN) {
14132 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14133 		if (ret) {
14134 			dev_info(&vsi->back->pdev->dev,
14135 				 "add vsi failed, err %pe aq_err %s\n",
14136 				 ERR_PTR(ret),
14137 				 i40e_aq_str(&pf->hw,
14138 					     pf->hw.aq.asq_last_status));
14139 			ret = -ENOENT;
14140 			goto err;
14141 		}
14142 		vsi->info = ctxt.info;
14143 		vsi->info.valid_sections = 0;
14144 		vsi->seid = ctxt.seid;
14145 		vsi->id = ctxt.vsi_number;
14146 	}
14147 
14148 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14149 	vsi->active_filters = 0;
14150 	/* If macvlan filters already exist, force them to get loaded */
14151 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14152 		f->state = I40E_FILTER_NEW;
14153 		f_count++;
14154 	}
14155 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14156 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14157 
14158 	if (f_count) {
14159 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14160 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14161 	}
14162 
14163 	/* Update VSI BW information */
14164 	ret = i40e_vsi_get_bw_info(vsi);
14165 	if (ret) {
14166 		dev_info(&pf->pdev->dev,
14167 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14168 			 ERR_PTR(ret),
14169 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14170 		/* VSI is already added so not tearing that up */
14171 		ret = 0;
14172 	}
14173 
14174 err:
14175 	return ret;
14176 }
14177 
14178 /**
14179  * i40e_vsi_release - Delete a VSI and free its resources
14180  * @vsi: the VSI being removed
14181  *
14182  * Returns 0 on success or < 0 on error
14183  **/
14184 int i40e_vsi_release(struct i40e_vsi *vsi)
14185 {
14186 	struct i40e_mac_filter *f;
14187 	struct hlist_node *h;
14188 	struct i40e_veb *veb = NULL;
14189 	struct i40e_pf *pf;
14190 	u16 uplink_seid;
14191 	int i, n, bkt;
14192 
14193 	pf = vsi->back;
14194 
14195 	/* release of a VEB-owner or last VSI is not allowed */
14196 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14197 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14198 			 vsi->seid, vsi->uplink_seid);
14199 		return -ENODEV;
14200 	}
14201 	if (vsi == pf->vsi[pf->lan_vsi] &&
14202 	    !test_bit(__I40E_DOWN, pf->state)) {
14203 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14204 		return -ENODEV;
14205 	}
14206 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14207 	uplink_seid = vsi->uplink_seid;
14208 	if (vsi->type != I40E_VSI_SRIOV) {
14209 		if (vsi->netdev_registered) {
14210 			vsi->netdev_registered = false;
14211 			if (vsi->netdev) {
14212 				/* results in a call to i40e_close() */
14213 				unregister_netdev(vsi->netdev);
14214 			}
14215 		} else {
14216 			i40e_vsi_close(vsi);
14217 		}
14218 		i40e_vsi_disable_irq(vsi);
14219 	}
14220 
14221 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14222 
14223 	/* clear the sync flag on all filters */
14224 	if (vsi->netdev) {
14225 		__dev_uc_unsync(vsi->netdev, NULL);
14226 		__dev_mc_unsync(vsi->netdev, NULL);
14227 	}
14228 
14229 	/* make sure any remaining filters are marked for deletion */
14230 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14231 		__i40e_del_filter(vsi, f);
14232 
14233 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14234 
14235 	i40e_sync_vsi_filters(vsi);
14236 
14237 	i40e_vsi_delete(vsi);
14238 	i40e_vsi_free_q_vectors(vsi);
14239 	if (vsi->netdev) {
14240 		free_netdev(vsi->netdev);
14241 		vsi->netdev = NULL;
14242 	}
14243 	i40e_vsi_clear_rings(vsi);
14244 	i40e_vsi_clear(vsi);
14245 
14246 	/* If this was the last thing on the VEB, except for the
14247 	 * controlling VSI, remove the VEB, which puts the controlling
14248 	 * VSI onto the next level down in the switch.
14249 	 *
14250 	 * Well, okay, there's one more exception here: don't remove
14251 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14252 	 * from up the network stack.
14253 	 */
14254 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14255 		if (pf->vsi[i] &&
14256 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14257 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14258 			n++;      /* count the VSIs */
14259 		}
14260 	}
14261 	for (i = 0; i < I40E_MAX_VEB; i++) {
14262 		if (!pf->veb[i])
14263 			continue;
14264 		if (pf->veb[i]->uplink_seid == uplink_seid)
14265 			n++;     /* count the VEBs */
14266 		if (pf->veb[i]->seid == uplink_seid)
14267 			veb = pf->veb[i];
14268 	}
14269 	if (n == 0 && veb && veb->uplink_seid != 0)
14270 		i40e_veb_release(veb);
14271 
14272 	return 0;
14273 }
14274 
14275 /**
14276  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14277  * @vsi: ptr to the VSI
14278  *
14279  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14280  * corresponding SW VSI structure and initializes num_queue_pairs for the
14281  * newly allocated VSI.
14282  *
14283  * Returns 0 on success or negative on failure
14284  **/
14285 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14286 {
14287 	int ret = -ENOENT;
14288 	struct i40e_pf *pf = vsi->back;
14289 
14290 	if (vsi->q_vectors[0]) {
14291 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14292 			 vsi->seid);
14293 		return -EEXIST;
14294 	}
14295 
14296 	if (vsi->base_vector) {
14297 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14298 			 vsi->seid, vsi->base_vector);
14299 		return -EEXIST;
14300 	}
14301 
14302 	ret = i40e_vsi_alloc_q_vectors(vsi);
14303 	if (ret) {
14304 		dev_info(&pf->pdev->dev,
14305 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14306 			 vsi->num_q_vectors, vsi->seid, ret);
14307 		vsi->num_q_vectors = 0;
14308 		goto vector_setup_out;
14309 	}
14310 
14311 	/* In Legacy mode, we do not have to get any other vector since we
14312 	 * piggyback on the misc/ICR0 for queue interrupts.
14313 	*/
14314 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14315 		return ret;
14316 	if (vsi->num_q_vectors)
14317 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14318 						 vsi->num_q_vectors, vsi->idx);
14319 	if (vsi->base_vector < 0) {
14320 		dev_info(&pf->pdev->dev,
14321 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14322 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14323 		i40e_vsi_free_q_vectors(vsi);
14324 		ret = -ENOENT;
14325 		goto vector_setup_out;
14326 	}
14327 
14328 vector_setup_out:
14329 	return ret;
14330 }
14331 
14332 /**
14333  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14334  * @vsi: pointer to the vsi.
14335  *
14336  * This re-allocates a vsi's queue resources.
14337  *
14338  * Returns pointer to the successfully allocated and configured VSI sw struct
14339  * on success, otherwise returns NULL on failure.
14340  **/
14341 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14342 {
14343 	u16 alloc_queue_pairs;
14344 	struct i40e_pf *pf;
14345 	u8 enabled_tc;
14346 	int ret;
14347 
14348 	if (!vsi)
14349 		return NULL;
14350 
14351 	pf = vsi->back;
14352 
14353 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14354 	i40e_vsi_clear_rings(vsi);
14355 
14356 	i40e_vsi_free_arrays(vsi, false);
14357 	i40e_set_num_rings_in_vsi(vsi);
14358 	ret = i40e_vsi_alloc_arrays(vsi, false);
14359 	if (ret)
14360 		goto err_vsi;
14361 
14362 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14363 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14364 
14365 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14366 	if (ret < 0) {
14367 		dev_info(&pf->pdev->dev,
14368 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14369 			 alloc_queue_pairs, vsi->seid, ret);
14370 		goto err_vsi;
14371 	}
14372 	vsi->base_queue = ret;
14373 
14374 	/* Update the FW view of the VSI. Force a reset of TC and queue
14375 	 * layout configurations.
14376 	 */
14377 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14378 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14379 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14380 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14381 	if (vsi->type == I40E_VSI_MAIN)
14382 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14383 
14384 	/* assign it some queues */
14385 	ret = i40e_alloc_rings(vsi);
14386 	if (ret)
14387 		goto err_rings;
14388 
14389 	/* map all of the rings to the q_vectors */
14390 	i40e_vsi_map_rings_to_vectors(vsi);
14391 	return vsi;
14392 
14393 err_rings:
14394 	i40e_vsi_free_q_vectors(vsi);
14395 	if (vsi->netdev_registered) {
14396 		vsi->netdev_registered = false;
14397 		unregister_netdev(vsi->netdev);
14398 		free_netdev(vsi->netdev);
14399 		vsi->netdev = NULL;
14400 	}
14401 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14402 err_vsi:
14403 	i40e_vsi_clear(vsi);
14404 	return NULL;
14405 }
14406 
14407 /**
14408  * i40e_vsi_setup - Set up a VSI by a given type
14409  * @pf: board private structure
14410  * @type: VSI type
14411  * @uplink_seid: the switch element to link to
14412  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14413  *
14414  * This allocates the sw VSI structure and its queue resources, then add a VSI
14415  * to the identified VEB.
14416  *
14417  * Returns pointer to the successfully allocated and configure VSI sw struct on
14418  * success, otherwise returns NULL on failure.
14419  **/
14420 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14421 				u16 uplink_seid, u32 param1)
14422 {
14423 	struct i40e_vsi *vsi = NULL;
14424 	struct i40e_veb *veb = NULL;
14425 	u16 alloc_queue_pairs;
14426 	int ret, i;
14427 	int v_idx;
14428 
14429 	/* The requested uplink_seid must be either
14430 	 *     - the PF's port seid
14431 	 *              no VEB is needed because this is the PF
14432 	 *              or this is a Flow Director special case VSI
14433 	 *     - seid of an existing VEB
14434 	 *     - seid of a VSI that owns an existing VEB
14435 	 *     - seid of a VSI that doesn't own a VEB
14436 	 *              a new VEB is created and the VSI becomes the owner
14437 	 *     - seid of the PF VSI, which is what creates the first VEB
14438 	 *              this is a special case of the previous
14439 	 *
14440 	 * Find which uplink_seid we were given and create a new VEB if needed
14441 	 */
14442 	for (i = 0; i < I40E_MAX_VEB; i++) {
14443 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14444 			veb = pf->veb[i];
14445 			break;
14446 		}
14447 	}
14448 
14449 	if (!veb && uplink_seid != pf->mac_seid) {
14450 
14451 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14452 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14453 				vsi = pf->vsi[i];
14454 				break;
14455 			}
14456 		}
14457 		if (!vsi) {
14458 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14459 				 uplink_seid);
14460 			return NULL;
14461 		}
14462 
14463 		if (vsi->uplink_seid == pf->mac_seid)
14464 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14465 					     vsi->tc_config.enabled_tc);
14466 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14467 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14468 					     vsi->tc_config.enabled_tc);
14469 		if (veb) {
14470 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14471 				dev_info(&vsi->back->pdev->dev,
14472 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14473 				return NULL;
14474 			}
14475 			/* We come up by default in VEPA mode if SRIOV is not
14476 			 * already enabled, in which case we can't force VEPA
14477 			 * mode.
14478 			 */
14479 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14480 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14481 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14482 			}
14483 			i40e_config_bridge_mode(veb);
14484 		}
14485 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14486 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14487 				veb = pf->veb[i];
14488 		}
14489 		if (!veb) {
14490 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14491 			return NULL;
14492 		}
14493 
14494 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14495 		uplink_seid = veb->seid;
14496 	}
14497 
14498 	/* get vsi sw struct */
14499 	v_idx = i40e_vsi_mem_alloc(pf, type);
14500 	if (v_idx < 0)
14501 		goto err_alloc;
14502 	vsi = pf->vsi[v_idx];
14503 	if (!vsi)
14504 		goto err_alloc;
14505 	vsi->type = type;
14506 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14507 
14508 	if (type == I40E_VSI_MAIN)
14509 		pf->lan_vsi = v_idx;
14510 	else if (type == I40E_VSI_SRIOV)
14511 		vsi->vf_id = param1;
14512 	/* assign it some queues */
14513 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14514 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14515 
14516 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14517 	if (ret < 0) {
14518 		dev_info(&pf->pdev->dev,
14519 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14520 			 alloc_queue_pairs, vsi->seid, ret);
14521 		goto err_vsi;
14522 	}
14523 	vsi->base_queue = ret;
14524 
14525 	/* get a VSI from the hardware */
14526 	vsi->uplink_seid = uplink_seid;
14527 	ret = i40e_add_vsi(vsi);
14528 	if (ret)
14529 		goto err_vsi;
14530 
14531 	switch (vsi->type) {
14532 	/* setup the netdev if needed */
14533 	case I40E_VSI_MAIN:
14534 	case I40E_VSI_VMDQ2:
14535 		ret = i40e_config_netdev(vsi);
14536 		if (ret)
14537 			goto err_netdev;
14538 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14539 		if (ret)
14540 			goto err_netdev;
14541 		ret = register_netdev(vsi->netdev);
14542 		if (ret)
14543 			goto err_netdev;
14544 		vsi->netdev_registered = true;
14545 		netif_carrier_off(vsi->netdev);
14546 #ifdef CONFIG_I40E_DCB
14547 		/* Setup DCB netlink interface */
14548 		i40e_dcbnl_setup(vsi);
14549 #endif /* CONFIG_I40E_DCB */
14550 		fallthrough;
14551 	case I40E_VSI_FDIR:
14552 		/* set up vectors and rings if needed */
14553 		ret = i40e_vsi_setup_vectors(vsi);
14554 		if (ret)
14555 			goto err_msix;
14556 
14557 		ret = i40e_alloc_rings(vsi);
14558 		if (ret)
14559 			goto err_rings;
14560 
14561 		/* map all of the rings to the q_vectors */
14562 		i40e_vsi_map_rings_to_vectors(vsi);
14563 
14564 		i40e_vsi_reset_stats(vsi);
14565 		break;
14566 	default:
14567 		/* no netdev or rings for the other VSI types */
14568 		break;
14569 	}
14570 
14571 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14572 	    (vsi->type == I40E_VSI_VMDQ2)) {
14573 		ret = i40e_vsi_config_rss(vsi);
14574 	}
14575 	return vsi;
14576 
14577 err_rings:
14578 	i40e_vsi_free_q_vectors(vsi);
14579 err_msix:
14580 	if (vsi->netdev_registered) {
14581 		vsi->netdev_registered = false;
14582 		unregister_netdev(vsi->netdev);
14583 		free_netdev(vsi->netdev);
14584 		vsi->netdev = NULL;
14585 	}
14586 err_netdev:
14587 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14588 err_vsi:
14589 	i40e_vsi_clear(vsi);
14590 err_alloc:
14591 	return NULL;
14592 }
14593 
14594 /**
14595  * i40e_veb_get_bw_info - Query VEB BW information
14596  * @veb: the veb to query
14597  *
14598  * Query the Tx scheduler BW configuration data for given VEB
14599  **/
14600 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14601 {
14602 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14603 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14604 	struct i40e_pf *pf = veb->pf;
14605 	struct i40e_hw *hw = &pf->hw;
14606 	u32 tc_bw_max;
14607 	int ret = 0;
14608 	int i;
14609 
14610 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14611 						  &bw_data, NULL);
14612 	if (ret) {
14613 		dev_info(&pf->pdev->dev,
14614 			 "query veb bw config failed, err %pe aq_err %s\n",
14615 			 ERR_PTR(ret),
14616 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14617 		goto out;
14618 	}
14619 
14620 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14621 						   &ets_data, NULL);
14622 	if (ret) {
14623 		dev_info(&pf->pdev->dev,
14624 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14625 			 ERR_PTR(ret),
14626 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14627 		goto out;
14628 	}
14629 
14630 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14631 	veb->bw_max_quanta = ets_data.tc_bw_max;
14632 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14633 	veb->enabled_tc = ets_data.tc_valid_bits;
14634 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14635 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14636 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14637 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14638 		veb->bw_tc_limit_credits[i] =
14639 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14640 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14641 	}
14642 
14643 out:
14644 	return ret;
14645 }
14646 
14647 /**
14648  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14649  * @pf: board private structure
14650  *
14651  * On error: returns error code (negative)
14652  * On success: returns vsi index in PF (positive)
14653  **/
14654 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14655 {
14656 	int ret = -ENOENT;
14657 	struct i40e_veb *veb;
14658 	int i;
14659 
14660 	/* Need to protect the allocation of switch elements at the PF level */
14661 	mutex_lock(&pf->switch_mutex);
14662 
14663 	/* VEB list may be fragmented if VEB creation/destruction has
14664 	 * been happening.  We can afford to do a quick scan to look
14665 	 * for any free slots in the list.
14666 	 *
14667 	 * find next empty veb slot, looping back around if necessary
14668 	 */
14669 	i = 0;
14670 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14671 		i++;
14672 	if (i >= I40E_MAX_VEB) {
14673 		ret = -ENOMEM;
14674 		goto err_alloc_veb;  /* out of VEB slots! */
14675 	}
14676 
14677 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14678 	if (!veb) {
14679 		ret = -ENOMEM;
14680 		goto err_alloc_veb;
14681 	}
14682 	veb->pf = pf;
14683 	veb->idx = i;
14684 	veb->enabled_tc = 1;
14685 
14686 	pf->veb[i] = veb;
14687 	ret = i;
14688 err_alloc_veb:
14689 	mutex_unlock(&pf->switch_mutex);
14690 	return ret;
14691 }
14692 
14693 /**
14694  * i40e_switch_branch_release - Delete a branch of the switch tree
14695  * @branch: where to start deleting
14696  *
14697  * This uses recursion to find the tips of the branch to be
14698  * removed, deleting until we get back to and can delete this VEB.
14699  **/
14700 static void i40e_switch_branch_release(struct i40e_veb *branch)
14701 {
14702 	struct i40e_pf *pf = branch->pf;
14703 	u16 branch_seid = branch->seid;
14704 	u16 veb_idx = branch->idx;
14705 	int i;
14706 
14707 	/* release any VEBs on this VEB - RECURSION */
14708 	for (i = 0; i < I40E_MAX_VEB; i++) {
14709 		if (!pf->veb[i])
14710 			continue;
14711 		if (pf->veb[i]->uplink_seid == branch->seid)
14712 			i40e_switch_branch_release(pf->veb[i]);
14713 	}
14714 
14715 	/* Release the VSIs on this VEB, but not the owner VSI.
14716 	 *
14717 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14718 	 *       the VEB itself, so don't use (*branch) after this loop.
14719 	 */
14720 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14721 		if (!pf->vsi[i])
14722 			continue;
14723 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14724 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14725 			i40e_vsi_release(pf->vsi[i]);
14726 		}
14727 	}
14728 
14729 	/* There's one corner case where the VEB might not have been
14730 	 * removed, so double check it here and remove it if needed.
14731 	 * This case happens if the veb was created from the debugfs
14732 	 * commands and no VSIs were added to it.
14733 	 */
14734 	if (pf->veb[veb_idx])
14735 		i40e_veb_release(pf->veb[veb_idx]);
14736 }
14737 
14738 /**
14739  * i40e_veb_clear - remove veb struct
14740  * @veb: the veb to remove
14741  **/
14742 static void i40e_veb_clear(struct i40e_veb *veb)
14743 {
14744 	if (!veb)
14745 		return;
14746 
14747 	if (veb->pf) {
14748 		struct i40e_pf *pf = veb->pf;
14749 
14750 		mutex_lock(&pf->switch_mutex);
14751 		if (pf->veb[veb->idx] == veb)
14752 			pf->veb[veb->idx] = NULL;
14753 		mutex_unlock(&pf->switch_mutex);
14754 	}
14755 
14756 	kfree(veb);
14757 }
14758 
14759 /**
14760  * i40e_veb_release - Delete a VEB and free its resources
14761  * @veb: the VEB being removed
14762  **/
14763 void i40e_veb_release(struct i40e_veb *veb)
14764 {
14765 	struct i40e_vsi *vsi = NULL;
14766 	struct i40e_pf *pf;
14767 	int i, n = 0;
14768 
14769 	pf = veb->pf;
14770 
14771 	/* find the remaining VSI and check for extras */
14772 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14773 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14774 			n++;
14775 			vsi = pf->vsi[i];
14776 		}
14777 	}
14778 	if (n != 1) {
14779 		dev_info(&pf->pdev->dev,
14780 			 "can't remove VEB %d with %d VSIs left\n",
14781 			 veb->seid, n);
14782 		return;
14783 	}
14784 
14785 	/* move the remaining VSI to uplink veb */
14786 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14787 	if (veb->uplink_seid) {
14788 		vsi->uplink_seid = veb->uplink_seid;
14789 		if (veb->uplink_seid == pf->mac_seid)
14790 			vsi->veb_idx = I40E_NO_VEB;
14791 		else
14792 			vsi->veb_idx = veb->veb_idx;
14793 	} else {
14794 		/* floating VEB */
14795 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14796 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14797 	}
14798 
14799 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14800 	i40e_veb_clear(veb);
14801 }
14802 
14803 /**
14804  * i40e_add_veb - create the VEB in the switch
14805  * @veb: the VEB to be instantiated
14806  * @vsi: the controlling VSI
14807  **/
14808 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14809 {
14810 	struct i40e_pf *pf = veb->pf;
14811 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14812 	int ret;
14813 
14814 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14815 			      veb->enabled_tc, false,
14816 			      &veb->seid, enable_stats, NULL);
14817 
14818 	/* get a VEB from the hardware */
14819 	if (ret) {
14820 		dev_info(&pf->pdev->dev,
14821 			 "couldn't add VEB, err %pe aq_err %s\n",
14822 			 ERR_PTR(ret),
14823 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14824 		return -EPERM;
14825 	}
14826 
14827 	/* get statistics counter */
14828 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14829 					 &veb->stats_idx, NULL, NULL, NULL);
14830 	if (ret) {
14831 		dev_info(&pf->pdev->dev,
14832 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14833 			 ERR_PTR(ret),
14834 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14835 		return -EPERM;
14836 	}
14837 	ret = i40e_veb_get_bw_info(veb);
14838 	if (ret) {
14839 		dev_info(&pf->pdev->dev,
14840 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14841 			 ERR_PTR(ret),
14842 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14843 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14844 		return -ENOENT;
14845 	}
14846 
14847 	vsi->uplink_seid = veb->seid;
14848 	vsi->veb_idx = veb->idx;
14849 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14850 
14851 	return 0;
14852 }
14853 
14854 /**
14855  * i40e_veb_setup - Set up a VEB
14856  * @pf: board private structure
14857  * @flags: VEB setup flags
14858  * @uplink_seid: the switch element to link to
14859  * @vsi_seid: the initial VSI seid
14860  * @enabled_tc: Enabled TC bit-map
14861  *
14862  * This allocates the sw VEB structure and links it into the switch
14863  * It is possible and legal for this to be a duplicate of an already
14864  * existing VEB.  It is also possible for both uplink and vsi seids
14865  * to be zero, in order to create a floating VEB.
14866  *
14867  * Returns pointer to the successfully allocated VEB sw struct on
14868  * success, otherwise returns NULL on failure.
14869  **/
14870 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14871 				u16 uplink_seid, u16 vsi_seid,
14872 				u8 enabled_tc)
14873 {
14874 	struct i40e_veb *veb, *uplink_veb = NULL;
14875 	int vsi_idx, veb_idx;
14876 	int ret;
14877 
14878 	/* if one seid is 0, the other must be 0 to create a floating relay */
14879 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14880 	    (uplink_seid + vsi_seid != 0)) {
14881 		dev_info(&pf->pdev->dev,
14882 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14883 			 uplink_seid, vsi_seid);
14884 		return NULL;
14885 	}
14886 
14887 	/* make sure there is such a vsi and uplink */
14888 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14889 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14890 			break;
14891 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14892 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14893 			 vsi_seid);
14894 		return NULL;
14895 	}
14896 
14897 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14898 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14899 			if (pf->veb[veb_idx] &&
14900 			    pf->veb[veb_idx]->seid == uplink_seid) {
14901 				uplink_veb = pf->veb[veb_idx];
14902 				break;
14903 			}
14904 		}
14905 		if (!uplink_veb) {
14906 			dev_info(&pf->pdev->dev,
14907 				 "uplink seid %d not found\n", uplink_seid);
14908 			return NULL;
14909 		}
14910 	}
14911 
14912 	/* get veb sw struct */
14913 	veb_idx = i40e_veb_mem_alloc(pf);
14914 	if (veb_idx < 0)
14915 		goto err_alloc;
14916 	veb = pf->veb[veb_idx];
14917 	veb->flags = flags;
14918 	veb->uplink_seid = uplink_seid;
14919 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14920 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14921 
14922 	/* create the VEB in the switch */
14923 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14924 	if (ret)
14925 		goto err_veb;
14926 	if (vsi_idx == pf->lan_vsi)
14927 		pf->lan_veb = veb->idx;
14928 
14929 	return veb;
14930 
14931 err_veb:
14932 	i40e_veb_clear(veb);
14933 err_alloc:
14934 	return NULL;
14935 }
14936 
14937 /**
14938  * i40e_setup_pf_switch_element - set PF vars based on switch type
14939  * @pf: board private structure
14940  * @ele: element we are building info from
14941  * @num_reported: total number of elements
14942  * @printconfig: should we print the contents
14943  *
14944  * helper function to assist in extracting a few useful SEID values.
14945  **/
14946 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14947 				struct i40e_aqc_switch_config_element_resp *ele,
14948 				u16 num_reported, bool printconfig)
14949 {
14950 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14951 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14952 	u8 element_type = ele->element_type;
14953 	u16 seid = le16_to_cpu(ele->seid);
14954 
14955 	if (printconfig)
14956 		dev_info(&pf->pdev->dev,
14957 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14958 			 element_type, seid, uplink_seid, downlink_seid);
14959 
14960 	switch (element_type) {
14961 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14962 		pf->mac_seid = seid;
14963 		break;
14964 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14965 		/* Main VEB? */
14966 		if (uplink_seid != pf->mac_seid)
14967 			break;
14968 		if (pf->lan_veb >= I40E_MAX_VEB) {
14969 			int v;
14970 
14971 			/* find existing or else empty VEB */
14972 			for (v = 0; v < I40E_MAX_VEB; v++) {
14973 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14974 					pf->lan_veb = v;
14975 					break;
14976 				}
14977 			}
14978 			if (pf->lan_veb >= I40E_MAX_VEB) {
14979 				v = i40e_veb_mem_alloc(pf);
14980 				if (v < 0)
14981 					break;
14982 				pf->lan_veb = v;
14983 			}
14984 		}
14985 		if (pf->lan_veb >= I40E_MAX_VEB)
14986 			break;
14987 
14988 		pf->veb[pf->lan_veb]->seid = seid;
14989 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14990 		pf->veb[pf->lan_veb]->pf = pf;
14991 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14992 		break;
14993 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14994 		if (num_reported != 1)
14995 			break;
14996 		/* This is immediately after a reset so we can assume this is
14997 		 * the PF's VSI
14998 		 */
14999 		pf->mac_seid = uplink_seid;
15000 		pf->pf_seid = downlink_seid;
15001 		pf->main_vsi_seid = seid;
15002 		if (printconfig)
15003 			dev_info(&pf->pdev->dev,
15004 				 "pf_seid=%d main_vsi_seid=%d\n",
15005 				 pf->pf_seid, pf->main_vsi_seid);
15006 		break;
15007 	case I40E_SWITCH_ELEMENT_TYPE_PF:
15008 	case I40E_SWITCH_ELEMENT_TYPE_VF:
15009 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
15010 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
15011 	case I40E_SWITCH_ELEMENT_TYPE_PE:
15012 	case I40E_SWITCH_ELEMENT_TYPE_PA:
15013 		/* ignore these for now */
15014 		break;
15015 	default:
15016 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
15017 			 element_type, seid);
15018 		break;
15019 	}
15020 }
15021 
15022 /**
15023  * i40e_fetch_switch_configuration - Get switch config from firmware
15024  * @pf: board private structure
15025  * @printconfig: should we print the contents
15026  *
15027  * Get the current switch configuration from the device and
15028  * extract a few useful SEID values.
15029  **/
15030 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15031 {
15032 	struct i40e_aqc_get_switch_config_resp *sw_config;
15033 	u16 next_seid = 0;
15034 	int ret = 0;
15035 	u8 *aq_buf;
15036 	int i;
15037 
15038 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15039 	if (!aq_buf)
15040 		return -ENOMEM;
15041 
15042 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15043 	do {
15044 		u16 num_reported, num_total;
15045 
15046 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15047 						I40E_AQ_LARGE_BUF,
15048 						&next_seid, NULL);
15049 		if (ret) {
15050 			dev_info(&pf->pdev->dev,
15051 				 "get switch config failed err %d aq_err %s\n",
15052 				 ret,
15053 				 i40e_aq_str(&pf->hw,
15054 					     pf->hw.aq.asq_last_status));
15055 			kfree(aq_buf);
15056 			return -ENOENT;
15057 		}
15058 
15059 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15060 		num_total = le16_to_cpu(sw_config->header.num_total);
15061 
15062 		if (printconfig)
15063 			dev_info(&pf->pdev->dev,
15064 				 "header: %d reported %d total\n",
15065 				 num_reported, num_total);
15066 
15067 		for (i = 0; i < num_reported; i++) {
15068 			struct i40e_aqc_switch_config_element_resp *ele =
15069 				&sw_config->element[i];
15070 
15071 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15072 						     printconfig);
15073 		}
15074 	} while (next_seid != 0);
15075 
15076 	kfree(aq_buf);
15077 	return ret;
15078 }
15079 
15080 /**
15081  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15082  * @pf: board private structure
15083  * @reinit: if the Main VSI needs to re-initialized.
15084  * @lock_acquired: indicates whether or not the lock has been acquired
15085  *
15086  * Returns 0 on success, negative value on failure
15087  **/
15088 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15089 {
15090 	u16 flags = 0;
15091 	int ret;
15092 
15093 	/* find out what's out there already */
15094 	ret = i40e_fetch_switch_configuration(pf, false);
15095 	if (ret) {
15096 		dev_info(&pf->pdev->dev,
15097 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15098 			 ERR_PTR(ret),
15099 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15100 		return ret;
15101 	}
15102 	i40e_pf_reset_stats(pf);
15103 
15104 	/* set the switch config bit for the whole device to
15105 	 * support limited promisc or true promisc
15106 	 * when user requests promisc. The default is limited
15107 	 * promisc.
15108 	*/
15109 
15110 	if ((pf->hw.pf_id == 0) &&
15111 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15112 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15113 		pf->last_sw_conf_flags = flags;
15114 	}
15115 
15116 	if (pf->hw.pf_id == 0) {
15117 		u16 valid_flags;
15118 
15119 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15120 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15121 						NULL);
15122 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15123 			dev_info(&pf->pdev->dev,
15124 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15125 				 ERR_PTR(ret),
15126 				 i40e_aq_str(&pf->hw,
15127 					     pf->hw.aq.asq_last_status));
15128 			/* not a fatal problem, just keep going */
15129 		}
15130 		pf->last_sw_conf_valid_flags = valid_flags;
15131 	}
15132 
15133 	/* first time setup */
15134 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15135 		struct i40e_vsi *vsi = NULL;
15136 		u16 uplink_seid;
15137 
15138 		/* Set up the PF VSI associated with the PF's main VSI
15139 		 * that is already in the HW switch
15140 		 */
15141 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15142 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15143 		else
15144 			uplink_seid = pf->mac_seid;
15145 		if (pf->lan_vsi == I40E_NO_VSI)
15146 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15147 		else if (reinit)
15148 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15149 		if (!vsi) {
15150 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15151 			i40e_cloud_filter_exit(pf);
15152 			i40e_fdir_teardown(pf);
15153 			return -EAGAIN;
15154 		}
15155 	} else {
15156 		/* force a reset of TC and queue layout configurations */
15157 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15158 
15159 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15160 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15161 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15162 	}
15163 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15164 
15165 	i40e_fdir_sb_setup(pf);
15166 
15167 	/* Setup static PF queue filter control settings */
15168 	ret = i40e_setup_pf_filter_control(pf);
15169 	if (ret) {
15170 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15171 			 ret);
15172 		/* Failure here should not stop continuing other steps */
15173 	}
15174 
15175 	/* enable RSS in the HW, even for only one queue, as the stack can use
15176 	 * the hash
15177 	 */
15178 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15179 		i40e_pf_config_rss(pf);
15180 
15181 	/* fill in link information and enable LSE reporting */
15182 	i40e_link_event(pf);
15183 
15184 	/* Initialize user-specific link properties */
15185 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15186 				  I40E_AQ_AN_COMPLETED) ? true : false);
15187 
15188 	i40e_ptp_init(pf);
15189 
15190 	if (!lock_acquired)
15191 		rtnl_lock();
15192 
15193 	/* repopulate tunnel port filters */
15194 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15195 
15196 	if (!lock_acquired)
15197 		rtnl_unlock();
15198 
15199 	return ret;
15200 }
15201 
15202 /**
15203  * i40e_determine_queue_usage - Work out queue distribution
15204  * @pf: board private structure
15205  **/
15206 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15207 {
15208 	int queues_left;
15209 	int q_max;
15210 
15211 	pf->num_lan_qps = 0;
15212 
15213 	/* Find the max queues to be put into basic use.  We'll always be
15214 	 * using TC0, whether or not DCB is running, and TC0 will get the
15215 	 * big RSS set.
15216 	 */
15217 	queues_left = pf->hw.func_caps.num_tx_qp;
15218 
15219 	if ((queues_left == 1) ||
15220 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15221 		/* one qp for PF, no queues for anything else */
15222 		queues_left = 0;
15223 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15224 
15225 		/* make sure all the fancies are disabled */
15226 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15227 			       I40E_FLAG_IWARP_ENABLED	|
15228 			       I40E_FLAG_FD_SB_ENABLED	|
15229 			       I40E_FLAG_FD_ATR_ENABLED	|
15230 			       I40E_FLAG_DCB_CAPABLE	|
15231 			       I40E_FLAG_DCB_ENABLED	|
15232 			       I40E_FLAG_SRIOV_ENABLED	|
15233 			       I40E_FLAG_VMDQ_ENABLED);
15234 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15235 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15236 				  I40E_FLAG_FD_SB_ENABLED |
15237 				  I40E_FLAG_FD_ATR_ENABLED |
15238 				  I40E_FLAG_DCB_CAPABLE))) {
15239 		/* one qp for PF */
15240 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15241 		queues_left -= pf->num_lan_qps;
15242 
15243 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15244 			       I40E_FLAG_IWARP_ENABLED	|
15245 			       I40E_FLAG_FD_SB_ENABLED	|
15246 			       I40E_FLAG_FD_ATR_ENABLED	|
15247 			       I40E_FLAG_DCB_ENABLED	|
15248 			       I40E_FLAG_VMDQ_ENABLED);
15249 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15250 	} else {
15251 		/* Not enough queues for all TCs */
15252 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15253 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15254 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15255 					I40E_FLAG_DCB_ENABLED);
15256 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15257 		}
15258 
15259 		/* limit lan qps to the smaller of qps, cpus or msix */
15260 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15261 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15262 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15263 		pf->num_lan_qps = q_max;
15264 
15265 		queues_left -= pf->num_lan_qps;
15266 	}
15267 
15268 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15269 		if (queues_left > 1) {
15270 			queues_left -= 1; /* save 1 queue for FD */
15271 		} else {
15272 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15273 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15274 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15275 		}
15276 	}
15277 
15278 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15279 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15280 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15281 					(queues_left / pf->num_vf_qps));
15282 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15283 	}
15284 
15285 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15286 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15287 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15288 					  (queues_left / pf->num_vmdq_qps));
15289 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15290 	}
15291 
15292 	pf->queues_left = queues_left;
15293 	dev_dbg(&pf->pdev->dev,
15294 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15295 		pf->hw.func_caps.num_tx_qp,
15296 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15297 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15298 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15299 		queues_left);
15300 }
15301 
15302 /**
15303  * i40e_setup_pf_filter_control - Setup PF static filter control
15304  * @pf: PF to be setup
15305  *
15306  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15307  * settings. If PE/FCoE are enabled then it will also set the per PF
15308  * based filter sizes required for them. It also enables Flow director,
15309  * ethertype and macvlan type filter settings for the pf.
15310  *
15311  * Returns 0 on success, negative on failure
15312  **/
15313 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15314 {
15315 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15316 
15317 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15318 
15319 	/* Flow Director is enabled */
15320 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15321 		settings->enable_fdir = true;
15322 
15323 	/* Ethtype and MACVLAN filters enabled for PF */
15324 	settings->enable_ethtype = true;
15325 	settings->enable_macvlan = true;
15326 
15327 	if (i40e_set_filter_control(&pf->hw, settings))
15328 		return -ENOENT;
15329 
15330 	return 0;
15331 }
15332 
15333 #define INFO_STRING_LEN 255
15334 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15335 static void i40e_print_features(struct i40e_pf *pf)
15336 {
15337 	struct i40e_hw *hw = &pf->hw;
15338 	char *buf;
15339 	int i;
15340 
15341 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15342 	if (!buf)
15343 		return;
15344 
15345 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15346 #ifdef CONFIG_PCI_IOV
15347 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15348 #endif
15349 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15350 		      pf->hw.func_caps.num_vsis,
15351 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15352 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15353 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15354 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15355 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15356 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15357 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15358 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15359 	}
15360 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15361 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15362 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15363 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15364 	if (pf->flags & I40E_FLAG_PTP)
15365 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15366 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15367 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15368 	else
15369 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15370 
15371 	dev_info(&pf->pdev->dev, "%s\n", buf);
15372 	kfree(buf);
15373 	WARN_ON(i > INFO_STRING_LEN);
15374 }
15375 
15376 /**
15377  * i40e_get_platform_mac_addr - get platform-specific MAC address
15378  * @pdev: PCI device information struct
15379  * @pf: board private structure
15380  *
15381  * Look up the MAC address for the device. First we'll try
15382  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15383  * specific fallback. Otherwise, we'll default to the stored value in
15384  * firmware.
15385  **/
15386 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15387 {
15388 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15389 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15390 }
15391 
15392 /**
15393  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15394  * @fec_cfg: FEC option to set in flags
15395  * @flags: ptr to flags in which we set FEC option
15396  **/
15397 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15398 {
15399 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15400 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15401 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15402 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15403 		*flags |= I40E_FLAG_RS_FEC;
15404 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15405 	}
15406 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15407 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15408 		*flags |= I40E_FLAG_BASE_R_FEC;
15409 		*flags &= ~I40E_FLAG_RS_FEC;
15410 	}
15411 	if (fec_cfg == 0)
15412 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15413 }
15414 
15415 /**
15416  * i40e_check_recovery_mode - check if we are running transition firmware
15417  * @pf: board private structure
15418  *
15419  * Check registers indicating the firmware runs in recovery mode. Sets the
15420  * appropriate driver state.
15421  *
15422  * Returns true if the recovery mode was detected, false otherwise
15423  **/
15424 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15425 {
15426 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15427 
15428 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15429 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15430 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15431 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15432 
15433 		return true;
15434 	}
15435 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15436 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15437 
15438 	return false;
15439 }
15440 
15441 /**
15442  * i40e_pf_loop_reset - perform reset in a loop.
15443  * @pf: board private structure
15444  *
15445  * This function is useful when a NIC is about to enter recovery mode.
15446  * When a NIC's internal data structures are corrupted the NIC's
15447  * firmware is going to enter recovery mode.
15448  * Right after a POR it takes about 7 minutes for firmware to enter
15449  * recovery mode. Until that time a NIC is in some kind of intermediate
15450  * state. After that time period the NIC almost surely enters
15451  * recovery mode. The only way for a driver to detect intermediate
15452  * state is to issue a series of pf-resets and check a return value.
15453  * If a PF reset returns success then the firmware could be in recovery
15454  * mode so the caller of this code needs to check for recovery mode
15455  * if this function returns success. There is a little chance that
15456  * firmware will hang in intermediate state forever.
15457  * Since waiting 7 minutes is quite a lot of time this function waits
15458  * 10 seconds and then gives up by returning an error.
15459  *
15460  * Return 0 on success, negative on failure.
15461  **/
15462 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15463 {
15464 	/* wait max 10 seconds for PF reset to succeed */
15465 	const unsigned long time_end = jiffies + 10 * HZ;
15466 	struct i40e_hw *hw = &pf->hw;
15467 	int ret;
15468 
15469 	ret = i40e_pf_reset(hw);
15470 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15471 		usleep_range(10000, 20000);
15472 		ret = i40e_pf_reset(hw);
15473 	}
15474 
15475 	if (ret == I40E_SUCCESS)
15476 		pf->pfr_count++;
15477 	else
15478 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15479 
15480 	return ret;
15481 }
15482 
15483 /**
15484  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15485  * @pf: board private structure
15486  *
15487  * Check FW registers to determine if FW issued unexpected EMP Reset.
15488  * Every time when unexpected EMP Reset occurs the FW increments
15489  * a counter of unexpected EMP Resets. When the counter reaches 10
15490  * the FW should enter the Recovery mode
15491  *
15492  * Returns true if FW issued unexpected EMP Reset
15493  **/
15494 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15495 {
15496 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15497 			   I40E_GL_FWSTS_FWS1B_MASK;
15498 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15499 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15500 }
15501 
15502 /**
15503  * i40e_handle_resets - handle EMP resets and PF resets
15504  * @pf: board private structure
15505  *
15506  * Handle both EMP resets and PF resets and conclude whether there are
15507  * any issues regarding these resets. If there are any issues then
15508  * generate log entry.
15509  *
15510  * Return 0 if NIC is healthy or negative value when there are issues
15511  * with resets
15512  **/
15513 static int i40e_handle_resets(struct i40e_pf *pf)
15514 {
15515 	const int pfr = i40e_pf_loop_reset(pf);
15516 	const bool is_empr = i40e_check_fw_empr(pf);
15517 
15518 	if (is_empr || pfr != I40E_SUCCESS)
15519 		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");
15520 
15521 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15522 }
15523 
15524 /**
15525  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15526  * @pf: board private structure
15527  * @hw: ptr to the hardware info
15528  *
15529  * This function does a minimal setup of all subsystems needed for running
15530  * recovery mode.
15531  *
15532  * Returns 0 on success, negative on failure
15533  **/
15534 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15535 {
15536 	struct i40e_vsi *vsi;
15537 	int err;
15538 	int v_idx;
15539 
15540 	pci_set_drvdata(pf->pdev, pf);
15541 	pci_save_state(pf->pdev);
15542 
15543 	/* set up periodic task facility */
15544 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15545 	pf->service_timer_period = HZ;
15546 
15547 	INIT_WORK(&pf->service_task, i40e_service_task);
15548 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15549 
15550 	err = i40e_init_interrupt_scheme(pf);
15551 	if (err)
15552 		goto err_switch_setup;
15553 
15554 	/* The number of VSIs reported by the FW is the minimum guaranteed
15555 	 * to us; HW supports far more and we share the remaining pool with
15556 	 * the other PFs. We allocate space for more than the guarantee with
15557 	 * the understanding that we might not get them all later.
15558 	 */
15559 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15560 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15561 	else
15562 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15563 
15564 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15565 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15566 			  GFP_KERNEL);
15567 	if (!pf->vsi) {
15568 		err = -ENOMEM;
15569 		goto err_switch_setup;
15570 	}
15571 
15572 	/* We allocate one VSI which is needed as absolute minimum
15573 	 * in order to register the netdev
15574 	 */
15575 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15576 	if (v_idx < 0) {
15577 		err = v_idx;
15578 		goto err_switch_setup;
15579 	}
15580 	pf->lan_vsi = v_idx;
15581 	vsi = pf->vsi[v_idx];
15582 	if (!vsi) {
15583 		err = -EFAULT;
15584 		goto err_switch_setup;
15585 	}
15586 	vsi->alloc_queue_pairs = 1;
15587 	err = i40e_config_netdev(vsi);
15588 	if (err)
15589 		goto err_switch_setup;
15590 	err = register_netdev(vsi->netdev);
15591 	if (err)
15592 		goto err_switch_setup;
15593 	vsi->netdev_registered = true;
15594 	i40e_dbg_pf_init(pf);
15595 
15596 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15597 	if (err)
15598 		goto err_switch_setup;
15599 
15600 	/* tell the firmware that we're starting */
15601 	i40e_send_version(pf);
15602 
15603 	/* since everything's happy, start the service_task timer */
15604 	mod_timer(&pf->service_timer,
15605 		  round_jiffies(jiffies + pf->service_timer_period));
15606 
15607 	return 0;
15608 
15609 err_switch_setup:
15610 	i40e_reset_interrupt_capability(pf);
15611 	timer_shutdown_sync(&pf->service_timer);
15612 	i40e_shutdown_adminq(hw);
15613 	iounmap(hw->hw_addr);
15614 	pci_release_mem_regions(pf->pdev);
15615 	pci_disable_device(pf->pdev);
15616 	kfree(pf);
15617 
15618 	return err;
15619 }
15620 
15621 /**
15622  * i40e_set_subsystem_device_id - set subsystem device id
15623  * @hw: pointer to the hardware info
15624  *
15625  * Set PCI subsystem device id either from a pci_dev structure or
15626  * a specific FW register.
15627  **/
15628 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15629 {
15630 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15631 
15632 	hw->subsystem_device_id = pdev->subsystem_device ?
15633 		pdev->subsystem_device :
15634 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15635 }
15636 
15637 /**
15638  * i40e_probe - Device initialization routine
15639  * @pdev: PCI device information struct
15640  * @ent: entry in i40e_pci_tbl
15641  *
15642  * i40e_probe initializes a PF identified by a pci_dev structure.
15643  * The OS initialization, configuring of the PF private structure,
15644  * and a hardware reset occur.
15645  *
15646  * Returns 0 on success, negative on failure
15647  **/
15648 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15649 {
15650 	struct i40e_aq_get_phy_abilities_resp abilities;
15651 #ifdef CONFIG_I40E_DCB
15652 	enum i40e_get_fw_lldp_status_resp lldp_status;
15653 #endif /* CONFIG_I40E_DCB */
15654 	struct i40e_pf *pf;
15655 	struct i40e_hw *hw;
15656 	static u16 pfs_found;
15657 	u16 wol_nvm_bits;
15658 	u16 link_status;
15659 #ifdef CONFIG_I40E_DCB
15660 	int status;
15661 #endif /* CONFIG_I40E_DCB */
15662 	int err;
15663 	u32 val;
15664 	u32 i;
15665 
15666 	err = pci_enable_device_mem(pdev);
15667 	if (err)
15668 		return err;
15669 
15670 	/* set up for high or low dma */
15671 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15672 	if (err) {
15673 		dev_err(&pdev->dev,
15674 			"DMA configuration failed: 0x%x\n", err);
15675 		goto err_dma;
15676 	}
15677 
15678 	/* set up pci connections */
15679 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15680 	if (err) {
15681 		dev_info(&pdev->dev,
15682 			 "pci_request_selected_regions failed %d\n", err);
15683 		goto err_pci_reg;
15684 	}
15685 
15686 	pci_set_master(pdev);
15687 
15688 	/* Now that we have a PCI connection, we need to do the
15689 	 * low level device setup.  This is primarily setting up
15690 	 * the Admin Queue structures and then querying for the
15691 	 * device's current profile information.
15692 	 */
15693 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15694 	if (!pf) {
15695 		err = -ENOMEM;
15696 		goto err_pf_alloc;
15697 	}
15698 	pf->next_vsi = 0;
15699 	pf->pdev = pdev;
15700 	set_bit(__I40E_DOWN, pf->state);
15701 
15702 	hw = &pf->hw;
15703 	hw->back = pf;
15704 
15705 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15706 				I40E_MAX_CSR_SPACE);
15707 	/* We believe that the highest register to read is
15708 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15709 	 * is not less than that before mapping to prevent a
15710 	 * kernel panic.
15711 	 */
15712 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15713 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15714 			pf->ioremap_len);
15715 		err = -ENOMEM;
15716 		goto err_ioremap;
15717 	}
15718 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15719 	if (!hw->hw_addr) {
15720 		err = -EIO;
15721 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15722 			 (unsigned int)pci_resource_start(pdev, 0),
15723 			 pf->ioremap_len, err);
15724 		goto err_ioremap;
15725 	}
15726 	hw->vendor_id = pdev->vendor;
15727 	hw->device_id = pdev->device;
15728 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15729 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15730 	i40e_set_subsystem_device_id(hw);
15731 	hw->bus.device = PCI_SLOT(pdev->devfn);
15732 	hw->bus.func = PCI_FUNC(pdev->devfn);
15733 	hw->bus.bus_id = pdev->bus->number;
15734 	pf->instance = pfs_found;
15735 
15736 	/* Select something other than the 802.1ad ethertype for the
15737 	 * switch to use internally and drop on ingress.
15738 	 */
15739 	hw->switch_tag = 0xffff;
15740 	hw->first_tag = ETH_P_8021AD;
15741 	hw->second_tag = ETH_P_8021Q;
15742 
15743 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15744 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15745 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15746 
15747 	/* set up the locks for the AQ, do this only once in probe
15748 	 * and destroy them only once in remove
15749 	 */
15750 	mutex_init(&hw->aq.asq_mutex);
15751 	mutex_init(&hw->aq.arq_mutex);
15752 
15753 	pf->msg_enable = netif_msg_init(debug,
15754 					NETIF_MSG_DRV |
15755 					NETIF_MSG_PROBE |
15756 					NETIF_MSG_LINK);
15757 	if (debug < -1)
15758 		pf->hw.debug_mask = debug;
15759 
15760 	/* do a special CORER for clearing PXE mode once at init */
15761 	if (hw->revision_id == 0 &&
15762 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15763 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15764 		i40e_flush(hw);
15765 		msleep(200);
15766 		pf->corer_count++;
15767 
15768 		i40e_clear_pxe_mode(hw);
15769 	}
15770 
15771 	/* Reset here to make sure all is clean and to define PF 'n' */
15772 	i40e_clear_hw(hw);
15773 
15774 	err = i40e_set_mac_type(hw);
15775 	if (err) {
15776 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15777 			 err);
15778 		goto err_pf_reset;
15779 	}
15780 
15781 	err = i40e_handle_resets(pf);
15782 	if (err)
15783 		goto err_pf_reset;
15784 
15785 	i40e_check_recovery_mode(pf);
15786 
15787 	if (is_kdump_kernel()) {
15788 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15789 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15790 	} else {
15791 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15792 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15793 	}
15794 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15795 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15796 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15797 
15798 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15799 		 "%s-%s:misc",
15800 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15801 
15802 	err = i40e_init_shared_code(hw);
15803 	if (err) {
15804 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15805 			 err);
15806 		goto err_pf_reset;
15807 	}
15808 
15809 	/* set up a default setting for link flow control */
15810 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15811 
15812 	err = i40e_init_adminq(hw);
15813 	if (err) {
15814 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15815 			dev_info(&pdev->dev,
15816 				 "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",
15817 				 hw->aq.api_maj_ver,
15818 				 hw->aq.api_min_ver,
15819 				 I40E_FW_API_VERSION_MAJOR,
15820 				 I40E_FW_MINOR_VERSION(hw));
15821 		else
15822 			dev_info(&pdev->dev,
15823 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15824 
15825 		goto err_pf_reset;
15826 	}
15827 	i40e_get_oem_version(hw);
15828 
15829 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15830 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15831 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15832 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15833 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15834 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15835 
15836 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15837 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15838 		dev_dbg(&pdev->dev,
15839 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15840 			 hw->aq.api_maj_ver,
15841 			 hw->aq.api_min_ver,
15842 			 I40E_FW_API_VERSION_MAJOR,
15843 			 I40E_FW_MINOR_VERSION(hw));
15844 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15845 		dev_info(&pdev->dev,
15846 			 "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",
15847 			 hw->aq.api_maj_ver,
15848 			 hw->aq.api_min_ver,
15849 			 I40E_FW_API_VERSION_MAJOR,
15850 			 I40E_FW_MINOR_VERSION(hw));
15851 
15852 	i40e_verify_eeprom(pf);
15853 
15854 	/* Rev 0 hardware was never productized */
15855 	if (hw->revision_id < 1)
15856 		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");
15857 
15858 	i40e_clear_pxe_mode(hw);
15859 
15860 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15861 	if (err)
15862 		goto err_adminq_setup;
15863 
15864 	err = i40e_sw_init(pf);
15865 	if (err) {
15866 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15867 		goto err_sw_init;
15868 	}
15869 
15870 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15871 		return i40e_init_recovery_mode(pf, hw);
15872 
15873 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15874 				hw->func_caps.num_rx_qp, 0, 0);
15875 	if (err) {
15876 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15877 		goto err_init_lan_hmc;
15878 	}
15879 
15880 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15881 	if (err) {
15882 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15883 		err = -ENOENT;
15884 		goto err_configure_lan_hmc;
15885 	}
15886 
15887 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15888 	 * Ignore error return codes because if it was already disabled via
15889 	 * hardware settings this will fail
15890 	 */
15891 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15892 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15893 		i40e_aq_stop_lldp(hw, true, false, NULL);
15894 	}
15895 
15896 	/* allow a platform config to override the HW addr */
15897 	i40e_get_platform_mac_addr(pdev, pf);
15898 
15899 	if (!is_valid_ether_addr(hw->mac.addr)) {
15900 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15901 		err = -EIO;
15902 		goto err_mac_addr;
15903 	}
15904 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15905 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15906 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15907 	if (is_valid_ether_addr(hw->mac.port_addr))
15908 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15909 
15910 	i40e_ptp_alloc_pins(pf);
15911 	pci_set_drvdata(pdev, pf);
15912 	pci_save_state(pdev);
15913 
15914 #ifdef CONFIG_I40E_DCB
15915 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15916 	(!status &&
15917 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15918 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15919 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15920 	dev_info(&pdev->dev,
15921 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15922 			"FW LLDP is disabled\n" :
15923 			"FW LLDP is enabled\n");
15924 
15925 	/* Enable FW to write default DCB config on link-up */
15926 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15927 
15928 	err = i40e_init_pf_dcb(pf);
15929 	if (err) {
15930 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15931 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15932 		/* Continue without DCB enabled */
15933 	}
15934 #endif /* CONFIG_I40E_DCB */
15935 
15936 	/* set up periodic task facility */
15937 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15938 	pf->service_timer_period = HZ;
15939 
15940 	INIT_WORK(&pf->service_task, i40e_service_task);
15941 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15942 
15943 	/* NVM bit on means WoL disabled for the port */
15944 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15945 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15946 		pf->wol_en = false;
15947 	else
15948 		pf->wol_en = true;
15949 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15950 
15951 	/* set up the main switch operations */
15952 	i40e_determine_queue_usage(pf);
15953 	err = i40e_init_interrupt_scheme(pf);
15954 	if (err)
15955 		goto err_switch_setup;
15956 
15957 	/* Reduce Tx and Rx pairs for kdump
15958 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15959 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15960 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15961 	 */
15962 	if (is_kdump_kernel())
15963 		pf->num_lan_msix = 1;
15964 
15965 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15966 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15967 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15968 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15969 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15970 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15971 						    UDP_TUNNEL_TYPE_GENEVE;
15972 
15973 	/* The number of VSIs reported by the FW is the minimum guaranteed
15974 	 * to us; HW supports far more and we share the remaining pool with
15975 	 * the other PFs. We allocate space for more than the guarantee with
15976 	 * the understanding that we might not get them all later.
15977 	 */
15978 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15979 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15980 	else
15981 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15982 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15983 		dev_warn(&pf->pdev->dev,
15984 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15985 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15986 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15987 	}
15988 
15989 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15990 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15991 			  GFP_KERNEL);
15992 	if (!pf->vsi) {
15993 		err = -ENOMEM;
15994 		goto err_switch_setup;
15995 	}
15996 
15997 #ifdef CONFIG_PCI_IOV
15998 	/* prep for VF support */
15999 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16000 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16001 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16002 		if (pci_num_vf(pdev))
16003 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
16004 	}
16005 #endif
16006 	err = i40e_setup_pf_switch(pf, false, false);
16007 	if (err) {
16008 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
16009 		goto err_vsis;
16010 	}
16011 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
16012 
16013 	/* if FDIR VSI was set up, start it now */
16014 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16015 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
16016 			i40e_vsi_open(pf->vsi[i]);
16017 			break;
16018 		}
16019 	}
16020 
16021 	/* The driver only wants link up/down and module qualification
16022 	 * reports from firmware.  Note the negative logic.
16023 	 */
16024 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16025 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16026 					 I40E_AQ_EVENT_MEDIA_NA |
16027 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16028 	if (err)
16029 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16030 			 ERR_PTR(err),
16031 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16032 
16033 	/* Reconfigure hardware for allowing smaller MSS in the case
16034 	 * of TSO, so that we avoid the MDD being fired and causing
16035 	 * a reset in the case of small MSS+TSO.
16036 	 */
16037 	val = rd32(hw, I40E_REG_MSS);
16038 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16039 		val &= ~I40E_REG_MSS_MIN_MASK;
16040 		val |= I40E_64BYTE_MSS;
16041 		wr32(hw, I40E_REG_MSS, val);
16042 	}
16043 
16044 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
16045 		msleep(75);
16046 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16047 		if (err)
16048 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16049 				 ERR_PTR(err),
16050 				 i40e_aq_str(&pf->hw,
16051 					     pf->hw.aq.asq_last_status));
16052 	}
16053 	/* The main driver is (mostly) up and happy. We need to set this state
16054 	 * before setting up the misc vector or we get a race and the vector
16055 	 * ends up disabled forever.
16056 	 */
16057 	clear_bit(__I40E_DOWN, pf->state);
16058 
16059 	/* In case of MSIX we are going to setup the misc vector right here
16060 	 * to handle admin queue events etc. In case of legacy and MSI
16061 	 * the misc functionality and queue processing is combined in
16062 	 * the same vector and that gets setup at open.
16063 	 */
16064 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16065 		err = i40e_setup_misc_vector(pf);
16066 		if (err) {
16067 			dev_info(&pdev->dev,
16068 				 "setup of misc vector failed: %d\n", err);
16069 			i40e_cloud_filter_exit(pf);
16070 			i40e_fdir_teardown(pf);
16071 			goto err_vsis;
16072 		}
16073 	}
16074 
16075 #ifdef CONFIG_PCI_IOV
16076 	/* prep for VF support */
16077 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16078 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16079 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16080 		/* disable link interrupts for VFs */
16081 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16082 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16083 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16084 		i40e_flush(hw);
16085 
16086 		if (pci_num_vf(pdev)) {
16087 			dev_info(&pdev->dev,
16088 				 "Active VFs found, allocating resources.\n");
16089 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16090 			if (err)
16091 				dev_info(&pdev->dev,
16092 					 "Error %d allocating resources for existing VFs\n",
16093 					 err);
16094 		}
16095 	}
16096 #endif /* CONFIG_PCI_IOV */
16097 
16098 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16099 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16100 						      pf->num_iwarp_msix,
16101 						      I40E_IWARP_IRQ_PILE_ID);
16102 		if (pf->iwarp_base_vector < 0) {
16103 			dev_info(&pdev->dev,
16104 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16105 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16106 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16107 		}
16108 	}
16109 
16110 	i40e_dbg_pf_init(pf);
16111 
16112 	/* tell the firmware that we're starting */
16113 	i40e_send_version(pf);
16114 
16115 	/* since everything's happy, start the service_task timer */
16116 	mod_timer(&pf->service_timer,
16117 		  round_jiffies(jiffies + pf->service_timer_period));
16118 
16119 	/* add this PF to client device list and launch a client service task */
16120 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16121 		err = i40e_lan_add_device(pf);
16122 		if (err)
16123 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16124 				 err);
16125 	}
16126 
16127 #define PCI_SPEED_SIZE 8
16128 #define PCI_WIDTH_SIZE 8
16129 	/* Devices on the IOSF bus do not have this information
16130 	 * and will report PCI Gen 1 x 1 by default so don't bother
16131 	 * checking them.
16132 	 */
16133 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16134 		char speed[PCI_SPEED_SIZE] = "Unknown";
16135 		char width[PCI_WIDTH_SIZE] = "Unknown";
16136 
16137 		/* Get the negotiated link width and speed from PCI config
16138 		 * space
16139 		 */
16140 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16141 					  &link_status);
16142 
16143 		i40e_set_pci_config_data(hw, link_status);
16144 
16145 		switch (hw->bus.speed) {
16146 		case i40e_bus_speed_8000:
16147 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16148 		case i40e_bus_speed_5000:
16149 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16150 		case i40e_bus_speed_2500:
16151 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16152 		default:
16153 			break;
16154 		}
16155 		switch (hw->bus.width) {
16156 		case i40e_bus_width_pcie_x8:
16157 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16158 		case i40e_bus_width_pcie_x4:
16159 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16160 		case i40e_bus_width_pcie_x2:
16161 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16162 		case i40e_bus_width_pcie_x1:
16163 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16164 		default:
16165 			break;
16166 		}
16167 
16168 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16169 			 speed, width);
16170 
16171 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16172 		    hw->bus.speed < i40e_bus_speed_8000) {
16173 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16174 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16175 		}
16176 	}
16177 
16178 	/* get the requested speeds from the fw */
16179 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16180 	if (err)
16181 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16182 			ERR_PTR(err),
16183 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16184 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16185 
16186 	/* set the FEC config due to the board capabilities */
16187 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16188 
16189 	/* get the supported phy types from the fw */
16190 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16191 	if (err)
16192 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16193 			ERR_PTR(err),
16194 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16195 
16196 	/* make sure the MFS hasn't been set lower than the default */
16197 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16198 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16199 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16200 	if (val < MAX_FRAME_SIZE_DEFAULT)
16201 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16202 			 i, val);
16203 
16204 	/* Add a filter to drop all Flow control frames from any VSI from being
16205 	 * transmitted. By doing so we stop a malicious VF from sending out
16206 	 * PAUSE or PFC frames and potentially controlling traffic for other
16207 	 * PF/VF VSIs.
16208 	 * The FW can still send Flow control frames if enabled.
16209 	 */
16210 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16211 						       pf->main_vsi_seid);
16212 
16213 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16214 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16215 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16216 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16217 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16218 	/* print a string summarizing features */
16219 	i40e_print_features(pf);
16220 
16221 	return 0;
16222 
16223 	/* Unwind what we've done if something failed in the setup */
16224 err_vsis:
16225 	set_bit(__I40E_DOWN, pf->state);
16226 	i40e_clear_interrupt_scheme(pf);
16227 	kfree(pf->vsi);
16228 err_switch_setup:
16229 	i40e_reset_interrupt_capability(pf);
16230 	timer_shutdown_sync(&pf->service_timer);
16231 err_mac_addr:
16232 err_configure_lan_hmc:
16233 	(void)i40e_shutdown_lan_hmc(hw);
16234 err_init_lan_hmc:
16235 	kfree(pf->qp_pile);
16236 err_sw_init:
16237 err_adminq_setup:
16238 err_pf_reset:
16239 	iounmap(hw->hw_addr);
16240 err_ioremap:
16241 	kfree(pf);
16242 err_pf_alloc:
16243 	pci_release_mem_regions(pdev);
16244 err_pci_reg:
16245 err_dma:
16246 	pci_disable_device(pdev);
16247 	return err;
16248 }
16249 
16250 /**
16251  * i40e_remove - Device removal routine
16252  * @pdev: PCI device information struct
16253  *
16254  * i40e_remove is called by the PCI subsystem to alert the driver
16255  * that is should release a PCI device.  This could be caused by a
16256  * Hot-Plug event, or because the driver is going to be removed from
16257  * memory.
16258  **/
16259 static void i40e_remove(struct pci_dev *pdev)
16260 {
16261 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16262 	struct i40e_hw *hw = &pf->hw;
16263 	int ret_code;
16264 	int i;
16265 
16266 	i40e_dbg_pf_exit(pf);
16267 
16268 	i40e_ptp_stop(pf);
16269 
16270 	/* Disable RSS in hw */
16271 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16272 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16273 
16274 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16275 	 * flags, once they are set, i40e_rebuild should not be called as
16276 	 * i40e_prep_for_reset always returns early.
16277 	 */
16278 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16279 		usleep_range(1000, 2000);
16280 	set_bit(__I40E_IN_REMOVE, pf->state);
16281 
16282 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16283 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16284 		i40e_free_vfs(pf);
16285 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16286 	}
16287 	/* no more scheduling of any task */
16288 	set_bit(__I40E_SUSPENDED, pf->state);
16289 	set_bit(__I40E_DOWN, pf->state);
16290 	if (pf->service_timer.function)
16291 		timer_shutdown_sync(&pf->service_timer);
16292 	if (pf->service_task.func)
16293 		cancel_work_sync(&pf->service_task);
16294 
16295 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16296 		struct i40e_vsi *vsi = pf->vsi[0];
16297 
16298 		/* We know that we have allocated only one vsi for this PF,
16299 		 * it was just for registering netdevice, so the interface
16300 		 * could be visible in the 'ifconfig' output
16301 		 */
16302 		unregister_netdev(vsi->netdev);
16303 		free_netdev(vsi->netdev);
16304 
16305 		goto unmap;
16306 	}
16307 
16308 	/* Client close must be called explicitly here because the timer
16309 	 * has been stopped.
16310 	 */
16311 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16312 
16313 	i40e_fdir_teardown(pf);
16314 
16315 	/* If there is a switch structure or any orphans, remove them.
16316 	 * This will leave only the PF's VSI remaining.
16317 	 */
16318 	for (i = 0; i < I40E_MAX_VEB; i++) {
16319 		if (!pf->veb[i])
16320 			continue;
16321 
16322 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16323 		    pf->veb[i]->uplink_seid == 0)
16324 			i40e_switch_branch_release(pf->veb[i]);
16325 	}
16326 
16327 	/* Now we can shutdown the PF's VSI, just before we kill
16328 	 * adminq and hmc.
16329 	 */
16330 	if (pf->vsi[pf->lan_vsi])
16331 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16332 
16333 	i40e_cloud_filter_exit(pf);
16334 
16335 	/* remove attached clients */
16336 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16337 		ret_code = i40e_lan_del_device(pf);
16338 		if (ret_code)
16339 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16340 				 ret_code);
16341 	}
16342 
16343 	/* shutdown and destroy the HMC */
16344 	if (hw->hmc.hmc_obj) {
16345 		ret_code = i40e_shutdown_lan_hmc(hw);
16346 		if (ret_code)
16347 			dev_warn(&pdev->dev,
16348 				 "Failed to destroy the HMC resources: %d\n",
16349 				 ret_code);
16350 	}
16351 
16352 unmap:
16353 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16354 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16355 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16356 		free_irq(pf->pdev->irq, pf);
16357 
16358 	/* shutdown the adminq */
16359 	i40e_shutdown_adminq(hw);
16360 
16361 	/* destroy the locks only once, here */
16362 	mutex_destroy(&hw->aq.arq_mutex);
16363 	mutex_destroy(&hw->aq.asq_mutex);
16364 
16365 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16366 	rtnl_lock();
16367 	i40e_clear_interrupt_scheme(pf);
16368 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16369 		if (pf->vsi[i]) {
16370 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16371 				i40e_vsi_clear_rings(pf->vsi[i]);
16372 			i40e_vsi_clear(pf->vsi[i]);
16373 			pf->vsi[i] = NULL;
16374 		}
16375 	}
16376 	rtnl_unlock();
16377 
16378 	for (i = 0; i < I40E_MAX_VEB; i++) {
16379 		kfree(pf->veb[i]);
16380 		pf->veb[i] = NULL;
16381 	}
16382 
16383 	kfree(pf->qp_pile);
16384 	kfree(pf->vsi);
16385 
16386 	iounmap(hw->hw_addr);
16387 	kfree(pf);
16388 	pci_release_mem_regions(pdev);
16389 
16390 	pci_disable_device(pdev);
16391 }
16392 
16393 /**
16394  * i40e_pci_error_detected - warning that something funky happened in PCI land
16395  * @pdev: PCI device information struct
16396  * @error: the type of PCI error
16397  *
16398  * Called to warn that something happened and the error handling steps
16399  * are in progress.  Allows the driver to quiesce things, be ready for
16400  * remediation.
16401  **/
16402 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16403 						pci_channel_state_t error)
16404 {
16405 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16406 
16407 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16408 
16409 	if (!pf) {
16410 		dev_info(&pdev->dev,
16411 			 "Cannot recover - error happened during device probe\n");
16412 		return PCI_ERS_RESULT_DISCONNECT;
16413 	}
16414 
16415 	/* shutdown all operations */
16416 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16417 		i40e_prep_for_reset(pf);
16418 
16419 	/* Request a slot reset */
16420 	return PCI_ERS_RESULT_NEED_RESET;
16421 }
16422 
16423 /**
16424  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16425  * @pdev: PCI device information struct
16426  *
16427  * Called to find if the driver can work with the device now that
16428  * the pci slot has been reset.  If a basic connection seems good
16429  * (registers are readable and have sane content) then return a
16430  * happy little PCI_ERS_RESULT_xxx.
16431  **/
16432 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16433 {
16434 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16435 	pci_ers_result_t result;
16436 	u32 reg;
16437 
16438 	dev_dbg(&pdev->dev, "%s\n", __func__);
16439 	if (pci_enable_device_mem(pdev)) {
16440 		dev_info(&pdev->dev,
16441 			 "Cannot re-enable PCI device after reset.\n");
16442 		result = PCI_ERS_RESULT_DISCONNECT;
16443 	} else {
16444 		pci_set_master(pdev);
16445 		pci_restore_state(pdev);
16446 		pci_save_state(pdev);
16447 		pci_wake_from_d3(pdev, false);
16448 
16449 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16450 		if (reg == 0)
16451 			result = PCI_ERS_RESULT_RECOVERED;
16452 		else
16453 			result = PCI_ERS_RESULT_DISCONNECT;
16454 	}
16455 
16456 	return result;
16457 }
16458 
16459 /**
16460  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16461  * @pdev: PCI device information struct
16462  */
16463 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16464 {
16465 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16466 
16467 	i40e_prep_for_reset(pf);
16468 }
16469 
16470 /**
16471  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16472  * @pdev: PCI device information struct
16473  */
16474 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16475 {
16476 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16477 
16478 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16479 		return;
16480 
16481 	i40e_reset_and_rebuild(pf, false, false);
16482 }
16483 
16484 /**
16485  * i40e_pci_error_resume - restart operations after PCI error recovery
16486  * @pdev: PCI device information struct
16487  *
16488  * Called to allow the driver to bring things back up after PCI error
16489  * and/or reset recovery has finished.
16490  **/
16491 static void i40e_pci_error_resume(struct pci_dev *pdev)
16492 {
16493 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16494 
16495 	dev_dbg(&pdev->dev, "%s\n", __func__);
16496 	if (test_bit(__I40E_SUSPENDED, pf->state))
16497 		return;
16498 
16499 	i40e_handle_reset_warning(pf, false);
16500 }
16501 
16502 /**
16503  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16504  * using the mac_address_write admin q function
16505  * @pf: pointer to i40e_pf struct
16506  **/
16507 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16508 {
16509 	struct i40e_hw *hw = &pf->hw;
16510 	u8 mac_addr[6];
16511 	u16 flags = 0;
16512 	int ret;
16513 
16514 	/* Get current MAC address in case it's an LAA */
16515 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16516 		ether_addr_copy(mac_addr,
16517 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16518 	} else {
16519 		dev_err(&pf->pdev->dev,
16520 			"Failed to retrieve MAC address; using default\n");
16521 		ether_addr_copy(mac_addr, hw->mac.addr);
16522 	}
16523 
16524 	/* The FW expects the mac address write cmd to first be called with
16525 	 * one of these flags before calling it again with the multicast
16526 	 * enable flags.
16527 	 */
16528 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16529 
16530 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16531 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16532 
16533 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16534 	if (ret) {
16535 		dev_err(&pf->pdev->dev,
16536 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16537 		return;
16538 	}
16539 
16540 	flags = I40E_AQC_MC_MAG_EN
16541 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16542 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16543 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16544 	if (ret)
16545 		dev_err(&pf->pdev->dev,
16546 			"Failed to enable Multicast Magic Packet wake up\n");
16547 }
16548 
16549 /**
16550  * i40e_shutdown - PCI callback for shutting down
16551  * @pdev: PCI device information struct
16552  **/
16553 static void i40e_shutdown(struct pci_dev *pdev)
16554 {
16555 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16556 	struct i40e_hw *hw = &pf->hw;
16557 
16558 	set_bit(__I40E_SUSPENDED, pf->state);
16559 	set_bit(__I40E_DOWN, pf->state);
16560 
16561 	del_timer_sync(&pf->service_timer);
16562 	cancel_work_sync(&pf->service_task);
16563 	i40e_cloud_filter_exit(pf);
16564 	i40e_fdir_teardown(pf);
16565 
16566 	/* Client close must be called explicitly here because the timer
16567 	 * has been stopped.
16568 	 */
16569 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16570 
16571 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16572 		i40e_enable_mc_magic_wake(pf);
16573 
16574 	i40e_prep_for_reset(pf);
16575 
16576 	wr32(hw, I40E_PFPM_APM,
16577 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16578 	wr32(hw, I40E_PFPM_WUFC,
16579 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16580 
16581 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16582 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16583 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16584 		free_irq(pf->pdev->irq, pf);
16585 
16586 	/* Since we're going to destroy queues during the
16587 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16588 	 * whole section
16589 	 */
16590 	rtnl_lock();
16591 	i40e_clear_interrupt_scheme(pf);
16592 	rtnl_unlock();
16593 
16594 	if (system_state == SYSTEM_POWER_OFF) {
16595 		pci_wake_from_d3(pdev, pf->wol_en);
16596 		pci_set_power_state(pdev, PCI_D3hot);
16597 	}
16598 }
16599 
16600 /**
16601  * i40e_suspend - PM callback for moving to D3
16602  * @dev: generic device information structure
16603  **/
16604 static int __maybe_unused i40e_suspend(struct device *dev)
16605 {
16606 	struct i40e_pf *pf = dev_get_drvdata(dev);
16607 	struct i40e_hw *hw = &pf->hw;
16608 
16609 	/* If we're already suspended, then there is nothing to do */
16610 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16611 		return 0;
16612 
16613 	set_bit(__I40E_DOWN, pf->state);
16614 
16615 	/* Ensure service task will not be running */
16616 	del_timer_sync(&pf->service_timer);
16617 	cancel_work_sync(&pf->service_task);
16618 
16619 	/* Client close must be called explicitly here because the timer
16620 	 * has been stopped.
16621 	 */
16622 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16623 
16624 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16625 		i40e_enable_mc_magic_wake(pf);
16626 
16627 	/* Since we're going to destroy queues during the
16628 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16629 	 * whole section
16630 	 */
16631 	rtnl_lock();
16632 
16633 	i40e_prep_for_reset(pf);
16634 
16635 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16636 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16637 
16638 	/* Clear the interrupt scheme and release our IRQs so that the system
16639 	 * can safely hibernate even when there are a large number of CPUs.
16640 	 * Otherwise hibernation might fail when mapping all the vectors back
16641 	 * to CPU0.
16642 	 */
16643 	i40e_clear_interrupt_scheme(pf);
16644 
16645 	rtnl_unlock();
16646 
16647 	return 0;
16648 }
16649 
16650 /**
16651  * i40e_resume - PM callback for waking up from D3
16652  * @dev: generic device information structure
16653  **/
16654 static int __maybe_unused i40e_resume(struct device *dev)
16655 {
16656 	struct i40e_pf *pf = dev_get_drvdata(dev);
16657 	int err;
16658 
16659 	/* If we're not suspended, then there is nothing to do */
16660 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16661 		return 0;
16662 
16663 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16664 	 * since we're going to be restoring queues
16665 	 */
16666 	rtnl_lock();
16667 
16668 	/* We cleared the interrupt scheme when we suspended, so we need to
16669 	 * restore it now to resume device functionality.
16670 	 */
16671 	err = i40e_restore_interrupt_scheme(pf);
16672 	if (err) {
16673 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16674 			err);
16675 	}
16676 
16677 	clear_bit(__I40E_DOWN, pf->state);
16678 	i40e_reset_and_rebuild(pf, false, true);
16679 
16680 	rtnl_unlock();
16681 
16682 	/* Clear suspended state last after everything is recovered */
16683 	clear_bit(__I40E_SUSPENDED, pf->state);
16684 
16685 	/* Restart the service task */
16686 	mod_timer(&pf->service_timer,
16687 		  round_jiffies(jiffies + pf->service_timer_period));
16688 
16689 	return 0;
16690 }
16691 
16692 static const struct pci_error_handlers i40e_err_handler = {
16693 	.error_detected = i40e_pci_error_detected,
16694 	.slot_reset = i40e_pci_error_slot_reset,
16695 	.reset_prepare = i40e_pci_error_reset_prepare,
16696 	.reset_done = i40e_pci_error_reset_done,
16697 	.resume = i40e_pci_error_resume,
16698 };
16699 
16700 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16701 
16702 static struct pci_driver i40e_driver = {
16703 	.name     = i40e_driver_name,
16704 	.id_table = i40e_pci_tbl,
16705 	.probe    = i40e_probe,
16706 	.remove   = i40e_remove,
16707 	.driver   = {
16708 		.pm = &i40e_pm_ops,
16709 	},
16710 	.shutdown = i40e_shutdown,
16711 	.err_handler = &i40e_err_handler,
16712 	.sriov_configure = i40e_pci_sriov_configure,
16713 };
16714 
16715 /**
16716  * i40e_init_module - Driver registration routine
16717  *
16718  * i40e_init_module is the first routine called when the driver is
16719  * loaded. All it does is register with the PCI subsystem.
16720  **/
16721 static int __init i40e_init_module(void)
16722 {
16723 	int err;
16724 
16725 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16726 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16727 
16728 	/* There is no need to throttle the number of active tasks because
16729 	 * each device limits its own task using a state bit for scheduling
16730 	 * the service task, and the device tasks do not interfere with each
16731 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16732 	 * since we need to be able to guarantee forward progress even under
16733 	 * memory pressure.
16734 	 */
16735 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16736 	if (!i40e_wq) {
16737 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16738 		return -ENOMEM;
16739 	}
16740 
16741 	i40e_dbg_init();
16742 	err = pci_register_driver(&i40e_driver);
16743 	if (err) {
16744 		destroy_workqueue(i40e_wq);
16745 		i40e_dbg_exit();
16746 		return err;
16747 	}
16748 
16749 	return 0;
16750 }
16751 module_init(i40e_init_module);
16752 
16753 /**
16754  * i40e_exit_module - Driver exit cleanup routine
16755  *
16756  * i40e_exit_module is called just before the driver is removed
16757  * from memory.
16758  **/
16759 static void __exit i40e_exit_module(void)
16760 {
16761 	pci_unregister_driver(&i40e_driver);
16762 	destroy_workqueue(i40e_wq);
16763 	ida_destroy(&i40e_client_ida);
16764 	i40e_dbg_exit();
16765 }
16766 module_exit(i40e_exit_module);
16767