xref: /linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision 907bd786209ed8c735db94889386aa48d2409d6a)
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <generated/utsrelease.h>
5 #include <linux/crash_dump.h>
6 #include <linux/net/intel/libie/pctype.h>
7 #include <linux/net/intel/libie/rx.h>
8 #include <linux/if_bridge.h>
9 #include <linux/if_macvlan.h>
10 #include <linux/module.h>
11 #include <net/pkt_cls.h>
12 #include <net/xdp_sock_drv.h>
13 
14 /* Local includes */
15 #include "i40e.h"
16 #include "i40e_devids.h"
17 #include "i40e_diag.h"
18 #include "i40e_lan_hmc.h"
19 #include "i40e_virtchnl_pf.h"
20 #include "i40e_xsk.h"
21 
22 /* All i40e tracepoints are defined by the include below, which
23  * must be included exactly once across the whole kernel with
24  * CREATE_TRACE_POINTS defined
25  */
26 #define CREATE_TRACE_POINTS
27 #include "i40e_trace.h"
28 
29 const char i40e_driver_name[] = "i40e";
30 static const char i40e_driver_string[] =
31 			"Intel(R) Ethernet Connection XL710 Network Driver";
32 
33 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
34 
35 /* a bit of forward declarations */
36 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
37 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
38 static int i40e_add_vsi(struct i40e_vsi *vsi);
39 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
40 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
41 static int i40e_setup_misc_vector(struct i40e_pf *pf);
42 static void i40e_determine_queue_usage(struct i40e_pf *pf);
43 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
44 static void i40e_prep_for_reset(struct i40e_pf *pf);
45 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
46 				   bool lock_acquired);
47 static int i40e_reset(struct i40e_pf *pf);
48 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
49 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
50 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
51 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
52 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
53 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
54 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
55 static int i40e_get_capabilities(struct i40e_pf *pf,
56 				 enum i40e_admin_queue_opc list_type);
57 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
58 
59 /* i40e_pci_tbl - PCI Device ID Table
60  *
61  * Last entry must be all 0s
62  *
63  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
64  *   Class, Class Mask, private data (not used) }
65  */
66 static const struct pci_device_id i40e_pci_tbl[] = {
67 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710) },
68 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU) },
69 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B) },
70 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C) },
71 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A) },
72 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B) },
73 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C) },
74 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC) },
75 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T) },
76 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4) },
77 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC) },
78 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP) },
79 	/*
80 	 * This ID conflicts with ipw2200, but the devices can be differentiated
81 	 * because i40e devices use PCI_CLASS_NETWORK_ETHERNET and ipw2200
82 	 * devices use PCI_CLASS_NETWORK_OTHER.
83 	 */
84 	{
85 		PCI_DEVICE(PCI_VENDOR_ID_INTEL, I40E_DEV_ID_10G_B),
86 		.class = PCI_CLASS_NETWORK_ETHERNET << 8,
87 		.class_mask = 0xffff00,
88 	},
89 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722) },
90 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722) },
91 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722) },
92 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722) },
93 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722) },
94 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722) },
95 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A) },
96 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2) },
97 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A) },
98 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000) },
99 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000) },
100 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B) },
101 	{ PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28) },
102 	/* required last entry */
103 	{ }
104 };
105 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
106 
107 #define I40E_MAX_VF_COUNT 128
108 static int debug = -1;
109 module_param(debug, uint, 0);
110 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
111 
112 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
113 MODULE_IMPORT_NS("LIBIE");
114 MODULE_IMPORT_NS("LIBIE_ADMINQ");
115 MODULE_LICENSE("GPL v2");
116 
117 static struct workqueue_struct *i40e_wq;
118 
119 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
120 				  struct net_device *netdev, int delta)
121 {
122 	struct netdev_hw_addr_list *ha_list;
123 	struct netdev_hw_addr *ha;
124 
125 	if (!f || !netdev)
126 		return;
127 
128 	if (is_unicast_ether_addr(f->macaddr) || is_link_local_ether_addr(f->macaddr))
129 		ha_list = &netdev->uc;
130 	else
131 		ha_list = &netdev->mc;
132 
133 	netdev_hw_addr_list_for_each(ha, ha_list) {
134 		if (ether_addr_equal(ha->addr, f->macaddr)) {
135 			ha->refcount += delta;
136 			if (ha->refcount <= 0)
137 				ha->refcount = 1;
138 			break;
139 		}
140 	}
141 }
142 
143 /**
144  * i40e_hw_to_dev - get device pointer from the hardware structure
145  * @hw: pointer to the device HW structure
146  **/
147 struct device *i40e_hw_to_dev(struct i40e_hw *hw)
148 {
149 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
150 
151 	return &pf->pdev->dev;
152 }
153 
154 /**
155  * i40e_allocate_dma_mem - OS specific memory alloc for shared code
156  * @hw:   pointer to the HW structure
157  * @mem:  ptr to mem struct to fill out
158  * @size: size of memory requested
159  * @alignment: what to align the allocation to
160  **/
161 int i40e_allocate_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem,
162 			  u64 size, u32 alignment)
163 {
164 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
165 
166 	mem->size = ALIGN(size, alignment);
167 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
168 				     GFP_KERNEL);
169 	if (!mem->va)
170 		return -ENOMEM;
171 
172 	return 0;
173 }
174 
175 /**
176  * i40e_free_dma_mem - OS specific memory free for shared code
177  * @hw:   pointer to the HW structure
178  * @mem:  ptr to mem struct to free
179  **/
180 int i40e_free_dma_mem(struct i40e_hw *hw, struct i40e_dma_mem *mem)
181 {
182 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
183 
184 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
185 	mem->va = NULL;
186 	mem->pa = 0;
187 	mem->size = 0;
188 
189 	return 0;
190 }
191 
192 /**
193  * i40e_allocate_virt_mem - OS specific memory alloc for shared code
194  * @hw:   pointer to the HW structure
195  * @mem:  ptr to mem struct to fill out
196  * @size: size of memory requested
197  **/
198 int i40e_allocate_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem,
199 			   u32 size)
200 {
201 	mem->size = size;
202 	mem->va = kzalloc(size, GFP_KERNEL);
203 
204 	if (!mem->va)
205 		return -ENOMEM;
206 
207 	return 0;
208 }
209 
210 /**
211  * i40e_free_virt_mem - OS specific memory free for shared code
212  * @hw:   pointer to the HW structure
213  * @mem:  ptr to mem struct to free
214  **/
215 int i40e_free_virt_mem(struct i40e_hw *hw, struct i40e_virt_mem *mem)
216 {
217 	/* it's ok to kfree a NULL pointer */
218 	kfree(mem->va);
219 	mem->va = NULL;
220 	mem->size = 0;
221 
222 	return 0;
223 }
224 
225 /**
226  * i40e_get_lump - find a lump of free generic resource
227  * @pf: board private structure
228  * @pile: the pile of resource to search
229  * @needed: the number of items needed
230  * @id: an owner id to stick on the items assigned
231  *
232  * Returns the base item index of the lump, or negative for error
233  **/
234 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
235 			 u16 needed, u16 id)
236 {
237 	int ret = -ENOMEM;
238 	int i, j;
239 
240 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
241 		dev_info(&pf->pdev->dev,
242 			 "param err: pile=%s needed=%d id=0x%04x\n",
243 			 pile ? "<valid>" : "<null>", needed, id);
244 		return -EINVAL;
245 	}
246 
247 	/* Allocate last queue in the pile for FDIR VSI queue
248 	 * so it doesn't fragment the qp_pile
249 	 */
250 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
251 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
252 			dev_err(&pf->pdev->dev,
253 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
254 				pile->num_entries - 1);
255 			return -ENOMEM;
256 		}
257 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
258 		return pile->num_entries - 1;
259 	}
260 
261 	i = 0;
262 	while (i < pile->num_entries) {
263 		/* skip already allocated entries */
264 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
265 			i++;
266 			continue;
267 		}
268 
269 		/* do we have enough in this lump? */
270 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
271 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
272 				break;
273 		}
274 
275 		if (j == needed) {
276 			/* there was enough, so assign it to the requestor */
277 			for (j = 0; j < needed; j++)
278 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
279 			ret = i;
280 			break;
281 		}
282 
283 		/* not enough, so skip over it and continue looking */
284 		i += j;
285 	}
286 
287 	return ret;
288 }
289 
290 /**
291  * i40e_put_lump - return a lump of generic resource
292  * @pile: the pile of resource to search
293  * @index: the base item index
294  * @id: the owner id of the items assigned
295  *
296  * Returns the count of items in the lump
297  **/
298 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
299 {
300 	int valid_id = (id | I40E_PILE_VALID_BIT);
301 	int count = 0;
302 	u16 i;
303 
304 	if (!pile || index >= pile->num_entries)
305 		return -EINVAL;
306 
307 	for (i = index;
308 	     i < pile->num_entries && pile->list[i] == valid_id;
309 	     i++) {
310 		pile->list[i] = 0;
311 		count++;
312 	}
313 
314 
315 	return count;
316 }
317 
318 /**
319  * i40e_find_vsi_from_id - searches for the vsi with the given id
320  * @pf: the pf structure to search for the vsi
321  * @id: id of the vsi it is searching for
322  **/
323 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
324 {
325 	struct i40e_vsi *vsi;
326 	int i;
327 
328 	i40e_pf_for_each_vsi(pf, i, vsi)
329 		if (vsi->id == id)
330 			return vsi;
331 
332 	return NULL;
333 }
334 
335 /**
336  * i40e_service_event_schedule - Schedule the service task to wake up
337  * @pf: board private structure
338  *
339  * If not already scheduled, this puts the task into the work queue
340  **/
341 void i40e_service_event_schedule(struct i40e_pf *pf)
342 {
343 	if ((!test_bit(__I40E_DOWN, pf->state) &&
344 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
345 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
346 		queue_work(i40e_wq, &pf->service_task);
347 }
348 
349 /**
350  * i40e_tx_timeout - Respond to a Tx Hang
351  * @netdev: network interface device structure
352  * @txqueue: queue number timing out
353  *
354  * If any port has noticed a Tx timeout, it is likely that the whole
355  * device is munged, not just the one netdev port, so go for the full
356  * reset.
357  **/
358 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
359 {
360 	struct i40e_netdev_priv *np = netdev_priv(netdev);
361 	struct i40e_vsi *vsi = np->vsi;
362 	struct i40e_pf *pf = vsi->back;
363 	struct i40e_ring *tx_ring = NULL;
364 	unsigned int i;
365 	u32 head, val;
366 
367 	pf->tx_timeout_count++;
368 
369 	/* with txqueue index, find the tx_ring struct */
370 	for (i = 0; i < vsi->num_queue_pairs; i++) {
371 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
372 			if (txqueue ==
373 			    vsi->tx_rings[i]->queue_index) {
374 				tx_ring = vsi->tx_rings[i];
375 				break;
376 			}
377 		}
378 	}
379 
380 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
381 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
382 	else if (time_before(jiffies,
383 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
384 		return;   /* don't do any new action before the next timeout */
385 
386 	/* don't kick off another recovery if one is already pending */
387 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
388 		return;
389 
390 	if (tx_ring) {
391 		head = i40e_get_head(tx_ring);
392 		/* Read interrupt register */
393 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
394 			val = rd32(&pf->hw,
395 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
396 						tx_ring->vsi->base_vector - 1));
397 		else
398 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
399 
400 		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",
401 			    vsi->seid, txqueue, tx_ring->next_to_clean,
402 			    head, tx_ring->next_to_use,
403 			    readl(tx_ring->tail), val);
404 	}
405 
406 	pf->tx_timeout_last_recovery = jiffies;
407 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
408 		    pf->tx_timeout_recovery_level, txqueue);
409 
410 	switch (pf->tx_timeout_recovery_level) {
411 	case 1:
412 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
413 		break;
414 	case 2:
415 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
416 		break;
417 	case 3:
418 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
419 		break;
420 	default:
421 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
422 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
423 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
424 		break;
425 	}
426 
427 	i40e_service_event_schedule(pf);
428 	pf->tx_timeout_recovery_level++;
429 }
430 
431 /**
432  * i40e_get_vsi_stats_struct - Get System Network Statistics
433  * @vsi: the VSI we care about
434  *
435  * Returns the address of the device statistics structure.
436  * The statistics are actually updated from the service task.
437  **/
438 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
439 {
440 	return &vsi->net_stats;
441 }
442 
443 /**
444  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
445  * @ring: Tx ring to get statistics from
446  * @stats: statistics entry to be updated
447  **/
448 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
449 					    struct rtnl_link_stats64 *stats)
450 {
451 	u64 bytes, packets;
452 	unsigned int start;
453 
454 	do {
455 		start = u64_stats_fetch_begin(&ring->syncp);
456 		packets = ring->stats.packets;
457 		bytes   = ring->stats.bytes;
458 	} while (u64_stats_fetch_retry(&ring->syncp, start));
459 
460 	stats->tx_packets += packets;
461 	stats->tx_bytes   += bytes;
462 }
463 
464 /**
465  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
466  * @netdev: network interface device structure
467  * @stats: data structure to store statistics
468  *
469  * Returns the address of the device statistics structure.
470  * The statistics are actually updated from the service task.
471  **/
472 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
473 				  struct rtnl_link_stats64 *stats)
474 {
475 	struct i40e_netdev_priv *np = netdev_priv(netdev);
476 	struct i40e_vsi *vsi = np->vsi;
477 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
478 	struct i40e_ring *ring;
479 	int i;
480 
481 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
482 		return;
483 
484 	if (!vsi->tx_rings)
485 		return;
486 
487 	rcu_read_lock();
488 	for (i = 0; i < vsi->num_queue_pairs; i++) {
489 		u64 bytes, packets;
490 		unsigned int start;
491 
492 		ring = READ_ONCE(vsi->tx_rings[i]);
493 		if (!ring)
494 			continue;
495 		i40e_get_netdev_stats_struct_tx(ring, stats);
496 
497 		if (i40e_enabled_xdp_vsi(vsi)) {
498 			ring = READ_ONCE(vsi->xdp_rings[i]);
499 			if (!ring)
500 				continue;
501 			i40e_get_netdev_stats_struct_tx(ring, stats);
502 		}
503 
504 		ring = READ_ONCE(vsi->rx_rings[i]);
505 		if (!ring)
506 			continue;
507 		do {
508 			start   = u64_stats_fetch_begin(&ring->syncp);
509 			packets = ring->stats.packets;
510 			bytes   = ring->stats.bytes;
511 		} while (u64_stats_fetch_retry(&ring->syncp, start));
512 
513 		stats->rx_packets += packets;
514 		stats->rx_bytes   += bytes;
515 
516 	}
517 	rcu_read_unlock();
518 
519 	/* following stats updated by i40e_watchdog_subtask() */
520 	stats->multicast	= vsi_stats->multicast;
521 	stats->tx_errors	= vsi_stats->tx_errors;
522 	stats->tx_dropped	= vsi_stats->tx_dropped;
523 	stats->rx_errors	= vsi_stats->rx_errors;
524 	stats->rx_dropped	= vsi_stats->rx_dropped;
525 	stats->rx_missed_errors	= vsi_stats->rx_missed_errors;
526 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
527 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
528 }
529 
530 /**
531  * i40e_vsi_reset_stats - Resets all stats of the given vsi
532  * @vsi: the VSI to have its stats reset
533  **/
534 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
535 {
536 	struct rtnl_link_stats64 *ns;
537 	int i;
538 
539 	if (!vsi)
540 		return;
541 
542 	ns = i40e_get_vsi_stats_struct(vsi);
543 	memset(ns, 0, sizeof(*ns));
544 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
545 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
546 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
547 	if (vsi->rx_rings && vsi->rx_rings[0]) {
548 		for (i = 0; i < vsi->num_queue_pairs; i++) {
549 			memset(&vsi->rx_rings[i]->stats, 0,
550 			       sizeof(vsi->rx_rings[i]->stats));
551 			memset(&vsi->rx_rings[i]->rx_stats, 0,
552 			       sizeof(vsi->rx_rings[i]->rx_stats));
553 			memset(&vsi->tx_rings[i]->stats, 0,
554 			       sizeof(vsi->tx_rings[i]->stats));
555 			memset(&vsi->tx_rings[i]->tx_stats, 0,
556 			       sizeof(vsi->tx_rings[i]->tx_stats));
557 		}
558 	}
559 	vsi->stat_offsets_loaded = false;
560 }
561 
562 /**
563  * i40e_pf_reset_stats - Reset all of the stats for the given PF
564  * @pf: the PF to be reset
565  **/
566 void i40e_pf_reset_stats(struct i40e_pf *pf)
567 {
568 	struct i40e_veb *veb;
569 	int i;
570 
571 	memset(&pf->stats, 0, sizeof(pf->stats));
572 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
573 	pf->stat_offsets_loaded = false;
574 
575 	i40e_pf_for_each_veb(pf, i, veb) {
576 		memset(&veb->stats, 0, sizeof(veb->stats));
577 		memset(&veb->stats_offsets, 0, sizeof(veb->stats_offsets));
578 		memset(&veb->tc_stats, 0, sizeof(veb->tc_stats));
579 		memset(&veb->tc_stats_offsets, 0, sizeof(veb->tc_stats_offsets));
580 		veb->stat_offsets_loaded = false;
581 	}
582 	pf->hw_csum_rx_error = 0;
583 }
584 
585 /**
586  * i40e_compute_pci_to_hw_id - compute index form PCI function.
587  * @vsi: ptr to the VSI to read from.
588  * @hw: ptr to the hardware info.
589  **/
590 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
591 {
592 	int pf_count = i40e_get_pf_count(hw);
593 
594 	if (vsi->type == I40E_VSI_SRIOV)
595 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
596 
597 	return hw->port + BIT(7);
598 }
599 
600 /**
601  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
602  * @hw: ptr to the hardware info.
603  * @hireg: the high 32 bit reg to read.
604  * @loreg: the low 32 bit reg to read.
605  * @offset_loaded: has the initial offset been loaded yet.
606  * @offset: ptr to current offset value.
607  * @stat: ptr to the stat.
608  *
609  * Since the device stats are not reset at PFReset, they will not
610  * be zeroed when the driver starts.  We'll save the first values read
611  * and use them as offsets to be subtracted from the raw values in order
612  * to report stats that count from zero.
613  **/
614 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
615 			       bool offset_loaded, u64 *offset, u64 *stat)
616 {
617 	u64 new_data;
618 
619 	new_data = rd64(hw, loreg);
620 
621 	if (!offset_loaded || new_data < *offset)
622 		*offset = new_data;
623 	*stat = new_data - *offset;
624 }
625 
626 /**
627  * i40e_stat_update48 - read and update a 48 bit stat from the chip
628  * @hw: ptr to the hardware info
629  * @hireg: the high 32 bit reg to read
630  * @loreg: the low 32 bit reg to read
631  * @offset_loaded: has the initial offset been loaded yet
632  * @offset: ptr to current offset value
633  * @stat: ptr to the stat
634  *
635  * Since the device stats are not reset at PFReset, they likely will not
636  * be zeroed when the driver starts.  We'll save the first values read
637  * and use them as offsets to be subtracted from the raw values in order
638  * to report stats that count from zero.  In the process, we also manage
639  * the potential roll-over.
640  **/
641 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
642 			       bool offset_loaded, u64 *offset, u64 *stat)
643 {
644 	u64 new_data;
645 
646 	if (hw->device_id == I40E_DEV_ID_QEMU) {
647 		new_data = rd32(hw, loreg);
648 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
649 	} else {
650 		new_data = rd64(hw, loreg);
651 	}
652 	if (!offset_loaded)
653 		*offset = new_data;
654 	if (likely(new_data >= *offset))
655 		*stat = new_data - *offset;
656 	else
657 		*stat = (new_data + BIT_ULL(48)) - *offset;
658 	*stat &= 0xFFFFFFFFFFFFULL;
659 }
660 
661 /**
662  * i40e_stat_update32 - read and update a 32 bit stat from the chip
663  * @hw: ptr to the hardware info
664  * @reg: the hw reg to read
665  * @offset_loaded: has the initial offset been loaded yet
666  * @offset: ptr to current offset value
667  * @stat: ptr to the stat
668  **/
669 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
670 			       bool offset_loaded, u64 *offset, u64 *stat)
671 {
672 	u32 new_data;
673 
674 	new_data = rd32(hw, reg);
675 	if (!offset_loaded)
676 		*offset = new_data;
677 	if (likely(new_data >= *offset))
678 		*stat = (u32)(new_data - *offset);
679 	else
680 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
681 }
682 
683 /**
684  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
685  * @hw: ptr to the hardware info
686  * @reg: the hw reg to read and clear
687  * @stat: ptr to the stat
688  **/
689 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
690 {
691 	u32 new_data = rd32(hw, reg);
692 
693 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
694 	*stat += new_data;
695 }
696 
697 /**
698  * i40e_stats_update_rx_discards - update rx_discards.
699  * @vsi: ptr to the VSI to be updated.
700  * @hw: ptr to the hardware info.
701  * @stat_idx: VSI's stat_counter_idx.
702  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
703  * @stat_offset: ptr to stat_offset to store first read of specific register.
704  * @stat: ptr to VSI's stat to be updated.
705  **/
706 static void
707 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
708 			      int stat_idx, bool offset_loaded,
709 			      struct i40e_eth_stats *stat_offset,
710 			      struct i40e_eth_stats *stat)
711 {
712 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
713 			   &stat_offset->rx_discards, &stat->rx_discards);
714 	i40e_stat_update64(hw,
715 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
716 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
717 			   offset_loaded, &stat_offset->rx_discards_other,
718 			   &stat->rx_discards_other);
719 }
720 
721 /**
722  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
723  * @vsi: the VSI to be updated
724  **/
725 void i40e_update_eth_stats(struct i40e_vsi *vsi)
726 {
727 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
728 	struct i40e_pf *pf = vsi->back;
729 	struct i40e_hw *hw = &pf->hw;
730 	struct i40e_eth_stats *oes;
731 	struct i40e_eth_stats *es;     /* device's eth stats */
732 
733 	es = &vsi->eth_stats;
734 	oes = &vsi->eth_stats_offsets;
735 
736 	/* Gather up the stats that the hw collects */
737 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
738 			   vsi->stat_offsets_loaded,
739 			   &oes->tx_errors, &es->tx_errors);
740 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
741 			   vsi->stat_offsets_loaded,
742 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
743 
744 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
745 			   I40E_GLV_GORCL(stat_idx),
746 			   vsi->stat_offsets_loaded,
747 			   &oes->rx_bytes, &es->rx_bytes);
748 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
749 			   I40E_GLV_UPRCL(stat_idx),
750 			   vsi->stat_offsets_loaded,
751 			   &oes->rx_unicast, &es->rx_unicast);
752 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
753 			   I40E_GLV_MPRCL(stat_idx),
754 			   vsi->stat_offsets_loaded,
755 			   &oes->rx_multicast, &es->rx_multicast);
756 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
757 			   I40E_GLV_BPRCL(stat_idx),
758 			   vsi->stat_offsets_loaded,
759 			   &oes->rx_broadcast, &es->rx_broadcast);
760 
761 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
762 			   I40E_GLV_GOTCL(stat_idx),
763 			   vsi->stat_offsets_loaded,
764 			   &oes->tx_bytes, &es->tx_bytes);
765 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
766 			   I40E_GLV_UPTCL(stat_idx),
767 			   vsi->stat_offsets_loaded,
768 			   &oes->tx_unicast, &es->tx_unicast);
769 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
770 			   I40E_GLV_MPTCL(stat_idx),
771 			   vsi->stat_offsets_loaded,
772 			   &oes->tx_multicast, &es->tx_multicast);
773 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
774 			   I40E_GLV_BPTCL(stat_idx),
775 			   vsi->stat_offsets_loaded,
776 			   &oes->tx_broadcast, &es->tx_broadcast);
777 
778 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
779 				      vsi->stat_offsets_loaded, oes, es);
780 
781 	vsi->stat_offsets_loaded = true;
782 }
783 
784 /**
785  * i40e_update_veb_stats - Update Switch component statistics
786  * @veb: the VEB being updated
787  **/
788 void i40e_update_veb_stats(struct i40e_veb *veb)
789 {
790 	struct i40e_pf *pf = veb->pf;
791 	struct i40e_hw *hw = &pf->hw;
792 	struct i40e_eth_stats *oes;
793 	struct i40e_eth_stats *es;     /* device's eth stats */
794 	struct i40e_veb_tc_stats *veb_oes;
795 	struct i40e_veb_tc_stats *veb_es;
796 	int i, idx = 0;
797 
798 	idx = veb->stats_idx;
799 	es = &veb->stats;
800 	oes = &veb->stats_offsets;
801 	veb_es = &veb->tc_stats;
802 	veb_oes = &veb->tc_stats_offsets;
803 
804 	/* Gather up the stats that the hw collects */
805 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
806 			   veb->stat_offsets_loaded,
807 			   &oes->tx_discards, &es->tx_discards);
808 	if (hw->revision_id > 0)
809 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
810 				   veb->stat_offsets_loaded,
811 				   &oes->rx_unknown_protocol,
812 				   &es->rx_unknown_protocol);
813 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
814 			   veb->stat_offsets_loaded,
815 			   &oes->rx_bytes, &es->rx_bytes);
816 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
817 			   veb->stat_offsets_loaded,
818 			   &oes->rx_unicast, &es->rx_unicast);
819 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
820 			   veb->stat_offsets_loaded,
821 			   &oes->rx_multicast, &es->rx_multicast);
822 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
823 			   veb->stat_offsets_loaded,
824 			   &oes->rx_broadcast, &es->rx_broadcast);
825 
826 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
827 			   veb->stat_offsets_loaded,
828 			   &oes->tx_bytes, &es->tx_bytes);
829 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
830 			   veb->stat_offsets_loaded,
831 			   &oes->tx_unicast, &es->tx_unicast);
832 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
833 			   veb->stat_offsets_loaded,
834 			   &oes->tx_multicast, &es->tx_multicast);
835 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
836 			   veb->stat_offsets_loaded,
837 			   &oes->tx_broadcast, &es->tx_broadcast);
838 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
839 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
840 				   I40E_GLVEBTC_RPCL(i, idx),
841 				   veb->stat_offsets_loaded,
842 				   &veb_oes->tc_rx_packets[i],
843 				   &veb_es->tc_rx_packets[i]);
844 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
845 				   I40E_GLVEBTC_RBCL(i, idx),
846 				   veb->stat_offsets_loaded,
847 				   &veb_oes->tc_rx_bytes[i],
848 				   &veb_es->tc_rx_bytes[i]);
849 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
850 				   I40E_GLVEBTC_TPCL(i, idx),
851 				   veb->stat_offsets_loaded,
852 				   &veb_oes->tc_tx_packets[i],
853 				   &veb_es->tc_tx_packets[i]);
854 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
855 				   I40E_GLVEBTC_TBCL(i, idx),
856 				   veb->stat_offsets_loaded,
857 				   &veb_oes->tc_tx_bytes[i],
858 				   &veb_es->tc_tx_bytes[i]);
859 	}
860 	veb->stat_offsets_loaded = true;
861 }
862 
863 /**
864  * i40e_update_vsi_stats - Update the vsi statistics counters.
865  * @vsi: the VSI to be updated
866  *
867  * There are a few instances where we store the same stat in a
868  * couple of different structs.  This is partly because we have
869  * the netdev stats that need to be filled out, which is slightly
870  * different from the "eth_stats" defined by the chip and used in
871  * VF communications.  We sort it out here.
872  **/
873 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
874 {
875 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
876 	struct i40e_pf *pf = vsi->back;
877 	struct rtnl_link_stats64 *ons;
878 	struct rtnl_link_stats64 *ns;   /* netdev stats */
879 	struct i40e_eth_stats *oes;
880 	struct i40e_eth_stats *es;     /* device's eth stats */
881 	u64 tx_restart, tx_busy;
882 	struct i40e_ring *p;
883 	u64 bytes, packets;
884 	unsigned int start;
885 	u64 tx_linearize;
886 	u64 tx_force_wb;
887 	u64 tx_stopped;
888 	u64 rx_p, rx_b;
889 	u64 tx_p, tx_b;
890 	u16 q;
891 
892 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
893 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
894 		return;
895 
896 	ns = i40e_get_vsi_stats_struct(vsi);
897 	ons = &vsi->net_stats_offsets;
898 	es = &vsi->eth_stats;
899 	oes = &vsi->eth_stats_offsets;
900 
901 	/* Gather up the netdev and vsi stats that the driver collects
902 	 * on the fly during packet processing
903 	 */
904 	rx_b = rx_p = 0;
905 	tx_b = tx_p = 0;
906 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
907 	tx_stopped = 0;
908 	rx_page = 0;
909 	rx_buf = 0;
910 	rx_reuse = 0;
911 	rx_alloc = 0;
912 	rx_waive = 0;
913 	rx_busy = 0;
914 	rcu_read_lock();
915 	for (q = 0; q < vsi->num_queue_pairs; q++) {
916 		/* locate Tx ring */
917 		p = READ_ONCE(vsi->tx_rings[q]);
918 		if (!p)
919 			continue;
920 
921 		do {
922 			start = u64_stats_fetch_begin(&p->syncp);
923 			packets = p->stats.packets;
924 			bytes = p->stats.bytes;
925 		} while (u64_stats_fetch_retry(&p->syncp, start));
926 		tx_b += bytes;
927 		tx_p += packets;
928 		tx_restart += p->tx_stats.restart_queue;
929 		tx_busy += p->tx_stats.tx_busy;
930 		tx_linearize += p->tx_stats.tx_linearize;
931 		tx_force_wb += p->tx_stats.tx_force_wb;
932 		tx_stopped += p->tx_stats.tx_stopped;
933 
934 		/* locate Rx ring */
935 		p = READ_ONCE(vsi->rx_rings[q]);
936 		if (!p)
937 			continue;
938 
939 		do {
940 			start = u64_stats_fetch_begin(&p->syncp);
941 			packets = p->stats.packets;
942 			bytes = p->stats.bytes;
943 		} while (u64_stats_fetch_retry(&p->syncp, start));
944 		rx_b += bytes;
945 		rx_p += packets;
946 		rx_buf += p->rx_stats.alloc_buff_failed;
947 		rx_page += p->rx_stats.alloc_page_failed;
948 		rx_reuse += p->rx_stats.page_reuse_count;
949 		rx_alloc += p->rx_stats.page_alloc_count;
950 		rx_waive += p->rx_stats.page_waive_count;
951 		rx_busy += p->rx_stats.page_busy_count;
952 
953 		if (i40e_enabled_xdp_vsi(vsi)) {
954 			/* locate XDP ring */
955 			p = READ_ONCE(vsi->xdp_rings[q]);
956 			if (!p)
957 				continue;
958 
959 			do {
960 				start = u64_stats_fetch_begin(&p->syncp);
961 				packets = p->stats.packets;
962 				bytes = p->stats.bytes;
963 			} while (u64_stats_fetch_retry(&p->syncp, start));
964 			tx_b += bytes;
965 			tx_p += packets;
966 			tx_restart += p->tx_stats.restart_queue;
967 			tx_busy += p->tx_stats.tx_busy;
968 			tx_linearize += p->tx_stats.tx_linearize;
969 			tx_force_wb += p->tx_stats.tx_force_wb;
970 		}
971 	}
972 	rcu_read_unlock();
973 	vsi->tx_restart = tx_restart;
974 	vsi->tx_busy = tx_busy;
975 	vsi->tx_linearize = tx_linearize;
976 	vsi->tx_force_wb = tx_force_wb;
977 	vsi->tx_stopped = tx_stopped;
978 	vsi->rx_page_failed = rx_page;
979 	vsi->rx_buf_failed = rx_buf;
980 	vsi->rx_page_reuse = rx_reuse;
981 	vsi->rx_page_alloc = rx_alloc;
982 	vsi->rx_page_waive = rx_waive;
983 	vsi->rx_page_busy = rx_busy;
984 
985 	ns->rx_packets = rx_p;
986 	ns->rx_bytes = rx_b;
987 	ns->tx_packets = tx_p;
988 	ns->tx_bytes = tx_b;
989 
990 	/* update netdev stats from eth stats */
991 	i40e_update_eth_stats(vsi);
992 	ons->tx_errors = oes->tx_errors;
993 	ns->tx_errors = es->tx_errors;
994 	ons->multicast = oes->rx_multicast;
995 	ns->multicast = es->rx_multicast;
996 	ons->rx_dropped = oes->rx_discards_other;
997 	ns->rx_dropped = es->rx_discards_other;
998 	ons->rx_missed_errors = oes->rx_discards;
999 	ns->rx_missed_errors = es->rx_discards;
1000 	ons->tx_dropped = oes->tx_discards;
1001 	ns->tx_dropped = es->tx_discards;
1002 
1003 	/* pull in a couple PF stats if this is the main vsi */
1004 	if (vsi->type == I40E_VSI_MAIN) {
1005 		ns->rx_crc_errors = pf->stats.crc_errors;
1006 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
1007 		ns->rx_length_errors = pf->stats.rx_length_errors;
1008 	}
1009 }
1010 
1011 /**
1012  * i40e_update_pf_stats - Update the PF statistics counters.
1013  * @pf: the PF to be updated
1014  **/
1015 static void i40e_update_pf_stats(struct i40e_pf *pf)
1016 {
1017 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
1018 	struct i40e_hw_port_stats *nsd = &pf->stats;
1019 	struct i40e_hw *hw = &pf->hw;
1020 	u32 val;
1021 	int i;
1022 
1023 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1024 			   I40E_GLPRT_GORCL(hw->port),
1025 			   pf->stat_offsets_loaded,
1026 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1027 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1028 			   I40E_GLPRT_GOTCL(hw->port),
1029 			   pf->stat_offsets_loaded,
1030 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1031 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1032 			   pf->stat_offsets_loaded,
1033 			   &osd->eth.rx_discards,
1034 			   &nsd->eth.rx_discards);
1035 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1036 			   I40E_GLPRT_UPRCL(hw->port),
1037 			   pf->stat_offsets_loaded,
1038 			   &osd->eth.rx_unicast,
1039 			   &nsd->eth.rx_unicast);
1040 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1041 			   I40E_GLPRT_MPRCL(hw->port),
1042 			   pf->stat_offsets_loaded,
1043 			   &osd->eth.rx_multicast,
1044 			   &nsd->eth.rx_multicast);
1045 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1046 			   I40E_GLPRT_BPRCL(hw->port),
1047 			   pf->stat_offsets_loaded,
1048 			   &osd->eth.rx_broadcast,
1049 			   &nsd->eth.rx_broadcast);
1050 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1051 			   I40E_GLPRT_UPTCL(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->eth.tx_unicast,
1054 			   &nsd->eth.tx_unicast);
1055 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1056 			   I40E_GLPRT_MPTCL(hw->port),
1057 			   pf->stat_offsets_loaded,
1058 			   &osd->eth.tx_multicast,
1059 			   &nsd->eth.tx_multicast);
1060 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1061 			   I40E_GLPRT_BPTCL(hw->port),
1062 			   pf->stat_offsets_loaded,
1063 			   &osd->eth.tx_broadcast,
1064 			   &nsd->eth.tx_broadcast);
1065 
1066 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1067 			   pf->stat_offsets_loaded,
1068 			   &osd->tx_dropped_link_down,
1069 			   &nsd->tx_dropped_link_down);
1070 
1071 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1072 			   pf->stat_offsets_loaded,
1073 			   &osd->crc_errors, &nsd->crc_errors);
1074 
1075 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1078 
1079 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1080 			   pf->stat_offsets_loaded,
1081 			   &osd->mac_local_faults,
1082 			   &nsd->mac_local_faults);
1083 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1084 			   pf->stat_offsets_loaded,
1085 			   &osd->mac_remote_faults,
1086 			   &nsd->mac_remote_faults);
1087 
1088 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1089 			   pf->stat_offsets_loaded,
1090 			   &osd->rx_length_errors,
1091 			   &nsd->rx_length_errors);
1092 
1093 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1094 			   pf->stat_offsets_loaded,
1095 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1096 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1097 			   pf->stat_offsets_loaded,
1098 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1099 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1100 			   pf->stat_offsets_loaded,
1101 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1102 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1103 			   pf->stat_offsets_loaded,
1104 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1105 
1106 	for (i = 0; i < 8; i++) {
1107 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1108 				   pf->stat_offsets_loaded,
1109 				   &osd->priority_xoff_rx[i],
1110 				   &nsd->priority_xoff_rx[i]);
1111 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1112 				   pf->stat_offsets_loaded,
1113 				   &osd->priority_xon_rx[i],
1114 				   &nsd->priority_xon_rx[i]);
1115 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1116 				   pf->stat_offsets_loaded,
1117 				   &osd->priority_xon_tx[i],
1118 				   &nsd->priority_xon_tx[i]);
1119 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1120 				   pf->stat_offsets_loaded,
1121 				   &osd->priority_xoff_tx[i],
1122 				   &nsd->priority_xoff_tx[i]);
1123 		i40e_stat_update32(hw,
1124 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1125 				   pf->stat_offsets_loaded,
1126 				   &osd->priority_xon_2_xoff[i],
1127 				   &nsd->priority_xon_2_xoff[i]);
1128 	}
1129 
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1131 			   I40E_GLPRT_PRC64L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_64, &nsd->rx_size_64);
1134 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1135 			   I40E_GLPRT_PRC127L(hw->port),
1136 			   pf->stat_offsets_loaded,
1137 			   &osd->rx_size_127, &nsd->rx_size_127);
1138 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1139 			   I40E_GLPRT_PRC255L(hw->port),
1140 			   pf->stat_offsets_loaded,
1141 			   &osd->rx_size_255, &nsd->rx_size_255);
1142 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1143 			   I40E_GLPRT_PRC511L(hw->port),
1144 			   pf->stat_offsets_loaded,
1145 			   &osd->rx_size_511, &nsd->rx_size_511);
1146 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1147 			   I40E_GLPRT_PRC1023L(hw->port),
1148 			   pf->stat_offsets_loaded,
1149 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1150 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1151 			   I40E_GLPRT_PRC1522L(hw->port),
1152 			   pf->stat_offsets_loaded,
1153 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1154 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1155 			   I40E_GLPRT_PRC9522L(hw->port),
1156 			   pf->stat_offsets_loaded,
1157 			   &osd->rx_size_big, &nsd->rx_size_big);
1158 
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1160 			   I40E_GLPRT_PTC64L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_64, &nsd->tx_size_64);
1163 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1164 			   I40E_GLPRT_PTC127L(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->tx_size_127, &nsd->tx_size_127);
1167 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1168 			   I40E_GLPRT_PTC255L(hw->port),
1169 			   pf->stat_offsets_loaded,
1170 			   &osd->tx_size_255, &nsd->tx_size_255);
1171 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1172 			   I40E_GLPRT_PTC511L(hw->port),
1173 			   pf->stat_offsets_loaded,
1174 			   &osd->tx_size_511, &nsd->tx_size_511);
1175 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1176 			   I40E_GLPRT_PTC1023L(hw->port),
1177 			   pf->stat_offsets_loaded,
1178 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1179 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1180 			   I40E_GLPRT_PTC1522L(hw->port),
1181 			   pf->stat_offsets_loaded,
1182 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1183 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1184 			   I40E_GLPRT_PTC9522L(hw->port),
1185 			   pf->stat_offsets_loaded,
1186 			   &osd->tx_size_big, &nsd->tx_size_big);
1187 
1188 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1189 			   pf->stat_offsets_loaded,
1190 			   &osd->rx_undersize, &nsd->rx_undersize);
1191 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1192 			   pf->stat_offsets_loaded,
1193 			   &osd->rx_fragments, &nsd->rx_fragments);
1194 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1195 			   pf->stat_offsets_loaded,
1196 			   &osd->rx_oversize, &nsd->rx_oversize);
1197 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1198 			   pf->stat_offsets_loaded,
1199 			   &osd->rx_jabber, &nsd->rx_jabber);
1200 
1201 	/* FDIR stats */
1202 	i40e_stat_update_and_clear32(hw,
1203 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1204 			&nsd->fd_atr_match);
1205 	i40e_stat_update_and_clear32(hw,
1206 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1207 			&nsd->fd_sb_match);
1208 	i40e_stat_update_and_clear32(hw,
1209 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1210 			&nsd->fd_atr_tunnel_match);
1211 
1212 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1213 	nsd->tx_lpi_status =
1214 		       FIELD_GET(I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK, val);
1215 	nsd->rx_lpi_status =
1216 		       FIELD_GET(I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK, val);
1217 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1218 			   pf->stat_offsets_loaded,
1219 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1220 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1221 			   pf->stat_offsets_loaded,
1222 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1223 
1224 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
1225 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1226 		nsd->fd_sb_status = true;
1227 	else
1228 		nsd->fd_sb_status = false;
1229 
1230 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
1231 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1232 		nsd->fd_atr_status = true;
1233 	else
1234 		nsd->fd_atr_status = false;
1235 
1236 	pf->stat_offsets_loaded = true;
1237 }
1238 
1239 /**
1240  * i40e_update_stats - Update the various statistics counters.
1241  * @vsi: the VSI to be updated
1242  *
1243  * Update the various stats for this VSI and its related entities.
1244  **/
1245 void i40e_update_stats(struct i40e_vsi *vsi)
1246 {
1247 	struct i40e_pf *pf = vsi->back;
1248 
1249 	if (vsi->type == I40E_VSI_MAIN)
1250 		i40e_update_pf_stats(pf);
1251 
1252 	i40e_update_vsi_stats(vsi);
1253 }
1254 
1255 /**
1256  * i40e_count_all_filters - counts VSI MAC filters
1257  * @vsi: the VSI to be searched
1258  *
1259  * Return: count of MAC filters in any state.
1260  */
1261 int i40e_count_all_filters(struct i40e_vsi *vsi)
1262 {
1263 	struct i40e_mac_filter *f;
1264 	struct hlist_node *h;
1265 	int bkt, cnt = 0;
1266 
1267 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1268 		cnt++;
1269 
1270 	return cnt;
1271 }
1272 
1273 /**
1274  * i40e_count_active_filters - counts VSI MAC filters
1275  * @vsi: the VSI to be searched
1276  *
1277  * Return: count of active MAC filters.
1278  */
1279 int i40e_count_active_filters(struct i40e_vsi *vsi)
1280 {
1281 	struct i40e_mac_filter *f;
1282 	struct hlist_node *h;
1283 	int bkt;
1284 	int cnt = 0;
1285 
1286 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1287 		if (f->state == I40E_FILTER_NEW ||
1288 		    f->state == I40E_FILTER_NEW_SYNC ||
1289 		    f->state == I40E_FILTER_ACTIVE)
1290 			++cnt;
1291 	}
1292 
1293 	return cnt;
1294 }
1295 
1296 /**
1297  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1298  * @vsi: the VSI to be searched
1299  * @macaddr: the MAC address
1300  * @vlan: the vlan
1301  *
1302  * Returns ptr to the filter object or NULL
1303  **/
1304 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1305 						const u8 *macaddr, s16 vlan)
1306 {
1307 	struct i40e_mac_filter *f;
1308 	u64 key;
1309 
1310 	if (!vsi || !macaddr)
1311 		return NULL;
1312 
1313 	key = i40e_addr_to_hkey(macaddr);
1314 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1315 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1316 		    (vlan == f->vlan))
1317 			return f;
1318 	}
1319 	return NULL;
1320 }
1321 
1322 /**
1323  * i40e_find_mac - Find a mac addr in the macvlan filters list
1324  * @vsi: the VSI to be searched
1325  * @macaddr: the MAC address we are searching for
1326  *
1327  * Returns the first filter with the provided MAC address or NULL if
1328  * MAC address was not found
1329  **/
1330 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1331 {
1332 	struct i40e_mac_filter *f;
1333 	u64 key;
1334 
1335 	if (!vsi || !macaddr)
1336 		return NULL;
1337 
1338 	key = i40e_addr_to_hkey(macaddr);
1339 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1340 		if ((ether_addr_equal(macaddr, f->macaddr)))
1341 			return f;
1342 	}
1343 	return NULL;
1344 }
1345 
1346 /**
1347  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1348  * @vsi: the VSI to be searched
1349  *
1350  * Returns true if VSI is in vlan mode or false otherwise
1351  **/
1352 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1353 {
1354 	/* If we have a PVID, always operate in VLAN mode */
1355 	if (vsi->info.pvid)
1356 		return true;
1357 
1358 	/* We need to operate in VLAN mode whenever we have any filters with
1359 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1360 	 * time, incurring search cost repeatedly. However, we can notice two
1361 	 * things:
1362 	 *
1363 	 * 1) the only place where we can gain a VLAN filter is in
1364 	 *    i40e_add_filter.
1365 	 *
1366 	 * 2) the only place where filters are actually removed is in
1367 	 *    i40e_sync_filters_subtask.
1368 	 *
1369 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1370 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1371 	 * we have to perform the full search after deleting filters in
1372 	 * i40e_sync_filters_subtask, but we already have to search
1373 	 * filters here and can perform the check at the same time. This
1374 	 * results in avoiding embedding a loop for VLAN mode inside another
1375 	 * loop over all the filters, and should maintain correctness as noted
1376 	 * above.
1377 	 */
1378 	return vsi->has_vlan_filter;
1379 }
1380 
1381 /**
1382  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1383  * @vsi: the VSI to configure
1384  * @tmp_add_list: list of filters ready to be added
1385  * @tmp_del_list: list of filters ready to be deleted
1386  * @vlan_filters: the number of active VLAN filters
1387  *
1388  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1389  * behave as expected. If we have any active VLAN filters remaining or about
1390  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1391  * so that they only match against untagged traffic. If we no longer have any
1392  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1393  * so that they match against both tagged and untagged traffic. In this way,
1394  * we ensure that we correctly receive the desired traffic. This ensures that
1395  * when we have an active VLAN we will receive only untagged traffic and
1396  * traffic matching active VLANs. If we have no active VLANs then we will
1397  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1398  *
1399  * Finally, in a similar fashion, this function also corrects filters when
1400  * there is an active PVID assigned to this VSI.
1401  *
1402  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1403  *
1404  * This function is only expected to be called from within
1405  * i40e_sync_vsi_filters.
1406  *
1407  * NOTE: This function expects to be called while under the
1408  * mac_filter_hash_lock
1409  */
1410 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1411 					 struct hlist_head *tmp_add_list,
1412 					 struct hlist_head *tmp_del_list,
1413 					 int vlan_filters)
1414 {
1415 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1416 	struct i40e_mac_filter *f, *add_head;
1417 	struct i40e_new_mac_filter *new;
1418 	struct hlist_node *h;
1419 	int bkt, new_vlan;
1420 
1421 	/* To determine if a particular filter needs to be replaced we
1422 	 * have the three following conditions:
1423 	 *
1424 	 * a) if we have a PVID assigned, then all filters which are
1425 	 *    not marked as VLAN=PVID must be replaced with filters that
1426 	 *    are.
1427 	 * b) otherwise, if we have any active VLANS, all filters
1428 	 *    which are marked as VLAN=-1 must be replaced with
1429 	 *    filters marked as VLAN=0
1430 	 * c) finally, if we do not have any active VLANS, all filters
1431 	 *    which are marked as VLAN=0 must be replaced with filters
1432 	 *    marked as VLAN=-1
1433 	 */
1434 
1435 	/* Update the filters about to be added in place */
1436 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1437 		if (pvid && new->f->vlan != pvid)
1438 			new->f->vlan = pvid;
1439 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1440 			new->f->vlan = 0;
1441 		else if (!vlan_filters && new->f->vlan == 0)
1442 			new->f->vlan = I40E_VLAN_ANY;
1443 	}
1444 
1445 	/* Update the remaining active filters */
1446 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1447 		/* Combine the checks for whether a filter needs to be changed
1448 		 * and then determine the new VLAN inside the if block, in
1449 		 * order to avoid duplicating code for adding the new filter
1450 		 * then deleting the old filter.
1451 		 */
1452 		if ((pvid && f->vlan != pvid) ||
1453 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1454 		    (!vlan_filters && f->vlan == 0)) {
1455 			/* Determine the new vlan we will be adding */
1456 			if (pvid)
1457 				new_vlan = pvid;
1458 			else if (vlan_filters)
1459 				new_vlan = 0;
1460 			else
1461 				new_vlan = I40E_VLAN_ANY;
1462 
1463 			/* Create the new filter */
1464 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1465 			if (!add_head)
1466 				return -ENOMEM;
1467 
1468 			/* Create a temporary i40e_new_mac_filter */
1469 			new = kzalloc_obj(*new, GFP_ATOMIC);
1470 			if (!new)
1471 				return -ENOMEM;
1472 
1473 			new->f = add_head;
1474 			new->state = add_head->state;
1475 			if (add_head->state == I40E_FILTER_NEW)
1476 				add_head->state = I40E_FILTER_NEW_SYNC;
1477 
1478 			/* Add the new filter to the tmp list */
1479 			hlist_add_head(&new->hlist, tmp_add_list);
1480 
1481 			/* Put the original filter into the delete list */
1482 			f->state = I40E_FILTER_REMOVE;
1483 			hash_del(&f->hlist);
1484 			hlist_add_head(&f->hlist, tmp_del_list);
1485 		}
1486 	}
1487 
1488 	vsi->has_vlan_filter = !!vlan_filters;
1489 
1490 	return 0;
1491 }
1492 
1493 /**
1494  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1495  * @vsi: the vsi to configure
1496  * @new_mac: new mac filter to be added
1497  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1498  * @vlan_filters: the number of active VLAN filters
1499  * @trusted: flag if the VF is trusted
1500  *
1501  * Get new VLAN id based on current VLAN filters, trust, PVID
1502  * and vf-vlan-prune-disable flag.
1503  *
1504  * Returns the value of the new vlan filter or
1505  * the old value if no new filter is needed.
1506  */
1507 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1508 				struct i40e_new_mac_filter *new_mac,
1509 				struct i40e_mac_filter *f,
1510 				int vlan_filters,
1511 				bool trusted)
1512 {
1513 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1514 	struct i40e_pf *pf = vsi->back;
1515 	bool is_any;
1516 
1517 	if (new_mac)
1518 		f = new_mac->f;
1519 
1520 	if (pvid && f->vlan != pvid)
1521 		return pvid;
1522 
1523 	is_any = (trusted ||
1524 		  !test_bit(I40E_FLAG_VF_VLAN_PRUNING_ENA, pf->flags));
1525 
1526 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1527 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1528 	    (is_any && !vlan_filters && f->vlan == 0)) {
1529 		if (is_any)
1530 			return I40E_VLAN_ANY;
1531 		else
1532 			return 0;
1533 	}
1534 
1535 	return f->vlan;
1536 }
1537 
1538 /**
1539  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1540  * @vsi: the vsi to configure
1541  * @tmp_add_list: list of filters ready to be added
1542  * @tmp_del_list: list of filters ready to be deleted
1543  * @vlan_filters: the number of active VLAN filters
1544  * @trusted: flag if the VF is trusted
1545  *
1546  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1547  * and vf-vlan-prune-disable flag.
1548  *
1549  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1550  *
1551  * This function is only expected to be called from within
1552  * i40e_sync_vsi_filters.
1553  *
1554  * NOTE: This function expects to be called while under the
1555  * mac_filter_hash_lock
1556  */
1557 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1558 					    struct hlist_head *tmp_add_list,
1559 					    struct hlist_head *tmp_del_list,
1560 					    int vlan_filters,
1561 					    bool trusted)
1562 {
1563 	struct i40e_mac_filter *f, *add_head;
1564 	struct i40e_new_mac_filter *new_mac;
1565 	struct hlist_node *h;
1566 	int bkt, new_vlan;
1567 
1568 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1569 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1570 							vlan_filters, trusted);
1571 	}
1572 
1573 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1574 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1575 						trusted);
1576 		if (new_vlan != f->vlan) {
1577 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1578 			if (!add_head)
1579 				return -ENOMEM;
1580 			/* Create a temporary i40e_new_mac_filter */
1581 			new_mac = kzalloc_obj(*new_mac, GFP_ATOMIC);
1582 			if (!new_mac)
1583 				return -ENOMEM;
1584 			new_mac->f = add_head;
1585 			new_mac->state = add_head->state;
1586 			if (add_head->state == I40E_FILTER_NEW)
1587 				add_head->state = I40E_FILTER_NEW_SYNC;
1588 
1589 			/* Add the new filter to the tmp list */
1590 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1591 
1592 			/* Put the original filter into the delete list */
1593 			f->state = I40E_FILTER_REMOVE;
1594 			hash_del(&f->hlist);
1595 			hlist_add_head(&f->hlist, tmp_del_list);
1596 		}
1597 	}
1598 
1599 	vsi->has_vlan_filter = !!vlan_filters;
1600 	return 0;
1601 }
1602 
1603 /**
1604  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1605  * @vsi: the PF Main VSI - inappropriate for any other VSI
1606  * @macaddr: the MAC address
1607  *
1608  * Remove whatever filter the firmware set up so the driver can manage
1609  * its own filtering intelligently.
1610  **/
1611 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1612 {
1613 	struct i40e_aqc_remove_macvlan_element_data element;
1614 	struct i40e_pf *pf = vsi->back;
1615 
1616 	/* Only appropriate for the PF main VSI */
1617 	if (vsi->type != I40E_VSI_MAIN)
1618 		return;
1619 
1620 	memset(&element, 0, sizeof(element));
1621 	ether_addr_copy(element.mac_addr, macaddr);
1622 	element.vlan_tag = 0;
1623 	/* Ignore error returns, some firmware does it this way... */
1624 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1625 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1626 
1627 	memset(&element, 0, sizeof(element));
1628 	ether_addr_copy(element.mac_addr, macaddr);
1629 	element.vlan_tag = 0;
1630 	/* ...and some firmware does it this way. */
1631 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1632 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1633 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1634 }
1635 
1636 /**
1637  * i40e_add_filter - Add a mac/vlan filter to the VSI
1638  * @vsi: the VSI to be searched
1639  * @macaddr: the MAC address
1640  * @vlan: the vlan
1641  *
1642  * Returns ptr to the filter object or NULL when no memory available.
1643  *
1644  * NOTE: This function is expected to be called with mac_filter_hash_lock
1645  * being held.
1646  **/
1647 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1648 					const u8 *macaddr, s16 vlan)
1649 {
1650 	struct i40e_mac_filter *f;
1651 	u64 key;
1652 
1653 	if (!vsi || !macaddr)
1654 		return NULL;
1655 
1656 	f = i40e_find_filter(vsi, macaddr, vlan);
1657 	if (!f) {
1658 		f = kzalloc_obj(*f, GFP_ATOMIC);
1659 		if (!f)
1660 			return NULL;
1661 
1662 		/* Update the boolean indicating if we need to function in
1663 		 * VLAN mode.
1664 		 */
1665 		if (vlan >= 0)
1666 			vsi->has_vlan_filter = true;
1667 
1668 		ether_addr_copy(f->macaddr, macaddr);
1669 		f->vlan = vlan;
1670 		f->state = I40E_FILTER_NEW;
1671 		INIT_HLIST_NODE(&f->hlist);
1672 
1673 		key = i40e_addr_to_hkey(macaddr);
1674 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1675 
1676 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1677 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1678 	}
1679 
1680 	/* If we're asked to add a filter that has been marked for removal, it
1681 	 * is safe to simply restore it to active state. __i40e_del_filter
1682 	 * will have simply deleted any filters which were previously marked
1683 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1684 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1685 	 * task, just restore this filter to the ACTIVE state so that the
1686 	 * sync task leaves it in place
1687 	 */
1688 	if (f->state == I40E_FILTER_REMOVE)
1689 		f->state = I40E_FILTER_ACTIVE;
1690 
1691 	return f;
1692 }
1693 
1694 /**
1695  * __i40e_del_filter - Remove a specific filter from the VSI
1696  * @vsi: VSI to remove from
1697  * @f: the filter to remove from the list
1698  *
1699  * This function requires you've found * the exact filter you will remove
1700  * already, such as via i40e_find_filter or i40e_find_mac.
1701  *
1702  * NOTE: This function is expected to be called with mac_filter_hash_lock
1703  * being held.
1704  * ANOTHER NOTE: This function MUST be called from within the context of
1705  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1706  * instead of list_for_each_entry().
1707  **/
1708 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1709 {
1710 	if (!f)
1711 		return;
1712 
1713 	/* If the filter was never added to firmware then we can just delete it
1714 	 * directly and we don't want to set the status to remove or else an
1715 	 * admin queue command will unnecessarily fire.
1716 	 */
1717 	if ((f->state == I40E_FILTER_FAILED) ||
1718 	    (f->state == I40E_FILTER_NEW)) {
1719 		hash_del(&f->hlist);
1720 		kfree(f);
1721 	} else {
1722 		f->state = I40E_FILTER_REMOVE;
1723 	}
1724 
1725 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1726 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1727 }
1728 
1729 /**
1730  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1731  * @vsi: the VSI to be searched
1732  * @macaddr: the mac address to be filtered
1733  *
1734  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1735  * go through all the macvlan filters and add a macvlan filter for each
1736  * unique vlan that already exists. If a PVID has been assigned, instead only
1737  * add the macaddr to that VLAN.
1738  *
1739  * Returns last filter added on success, else NULL
1740  **/
1741 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1742 					    const u8 *macaddr)
1743 {
1744 	struct i40e_mac_filter *f, *add = NULL;
1745 	struct hlist_node *h;
1746 	int bkt;
1747 
1748 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1749 	if (vsi->info.pvid)
1750 		return i40e_add_filter(vsi, macaddr,
1751 				       le16_to_cpu(vsi->info.pvid));
1752 
1753 	if (!i40e_is_vsi_in_vlan(vsi))
1754 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1755 
1756 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1757 		if (f->state == I40E_FILTER_REMOVE)
1758 			continue;
1759 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1760 		if (!add)
1761 			return NULL;
1762 	}
1763 
1764 	return add;
1765 }
1766 
1767 /**
1768  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1769  * @vsi: the VSI to be searched
1770  * @macaddr: the mac address to be removed
1771  *
1772  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1773  * associated with.
1774  *
1775  * Returns 0 for success, or error
1776  **/
1777 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1778 {
1779 	struct i40e_mac_filter *f;
1780 	struct hlist_node *h;
1781 	bool found = false;
1782 	int bkt;
1783 
1784 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1785 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1786 		if (ether_addr_equal(macaddr, f->macaddr)) {
1787 			__i40e_del_filter(vsi, f);
1788 			found = true;
1789 		}
1790 	}
1791 
1792 	if (found)
1793 		return 0;
1794 	else
1795 		return -ENOENT;
1796 }
1797 
1798 /**
1799  * i40e_set_mac - NDO callback to set mac address
1800  * @netdev: network interface device structure
1801  * @p: pointer to an address structure
1802  *
1803  * Returns 0 on success, negative on failure
1804  **/
1805 static int i40e_set_mac(struct net_device *netdev, void *p)
1806 {
1807 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1808 	struct i40e_vsi *vsi = np->vsi;
1809 	struct i40e_pf *pf = vsi->back;
1810 	struct i40e_hw *hw = &pf->hw;
1811 	struct sockaddr *addr = p;
1812 
1813 	if (!is_valid_ether_addr(addr->sa_data))
1814 		return -EADDRNOTAVAIL;
1815 
1816 	if (test_bit(__I40E_DOWN, pf->state) ||
1817 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1818 		return -EADDRNOTAVAIL;
1819 
1820 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1821 		netdev_info(netdev, "returning to hw mac address %pM\n",
1822 			    hw->mac.addr);
1823 	else
1824 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1825 
1826 	/* Copy the address first, so that we avoid a possible race with
1827 	 * .set_rx_mode().
1828 	 * - Remove old address from MAC filter
1829 	 * - Copy new address
1830 	 * - Add new address to MAC filter
1831 	 */
1832 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1833 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1834 	eth_hw_addr_set(netdev, addr->sa_data);
1835 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1836 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1837 
1838 	if (vsi->type == I40E_VSI_MAIN) {
1839 		int ret;
1840 
1841 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1842 						addr->sa_data, NULL);
1843 		if (ret)
1844 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1845 				    ERR_PTR(ret),
1846 				    libie_aq_str(hw->aq.asq_last_status));
1847 	}
1848 
1849 	/* schedule our worker thread which will take care of
1850 	 * applying the new filter changes
1851 	 */
1852 	i40e_service_event_schedule(pf);
1853 	return 0;
1854 }
1855 
1856 /**
1857  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1858  * @vsi: vsi structure
1859  * @seed: RSS hash seed
1860  * @lut: pointer to lookup table of lut_size
1861  * @lut_size: size of the lookup table
1862  **/
1863 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1864 			      u8 *lut, u16 lut_size)
1865 {
1866 	struct i40e_pf *pf = vsi->back;
1867 	struct i40e_hw *hw = &pf->hw;
1868 	int ret = 0;
1869 
1870 	if (seed) {
1871 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1872 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1873 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1874 		if (ret) {
1875 			dev_info(&pf->pdev->dev,
1876 				 "Cannot set RSS key, err %pe aq_err %s\n",
1877 				 ERR_PTR(ret),
1878 				 libie_aq_str(hw->aq.asq_last_status));
1879 			return ret;
1880 		}
1881 	}
1882 	if (lut) {
1883 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1884 
1885 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1886 		if (ret) {
1887 			dev_info(&pf->pdev->dev,
1888 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1889 				 ERR_PTR(ret),
1890 				 libie_aq_str(hw->aq.asq_last_status));
1891 			return ret;
1892 		}
1893 	}
1894 	return ret;
1895 }
1896 
1897 /**
1898  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1899  * @vsi: VSI structure
1900  **/
1901 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1902 {
1903 	struct i40e_pf *pf = vsi->back;
1904 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1905 	u8 *lut;
1906 	int ret;
1907 
1908 	if (!test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
1909 		return 0;
1910 	if (!vsi->rss_size)
1911 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1912 				      vsi->num_queue_pairs);
1913 	if (!vsi->rss_size)
1914 		return -EINVAL;
1915 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1916 	if (!lut)
1917 		return -ENOMEM;
1918 
1919 	/* Use the user configured hash keys and lookup table if there is one,
1920 	 * otherwise use default
1921 	 */
1922 	if (vsi->rss_lut_user)
1923 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1924 	else
1925 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1926 	if (vsi->rss_hkey_user)
1927 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1928 	else
1929 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1930 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1931 	kfree(lut);
1932 	return ret;
1933 }
1934 
1935 /**
1936  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1937  * @vsi: the VSI being configured,
1938  * @ctxt: VSI context structure
1939  * @enabled_tc: number of traffic classes to enable
1940  *
1941  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1942  **/
1943 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1944 					   struct i40e_vsi_context *ctxt,
1945 					   u8 enabled_tc)
1946 {
1947 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1948 	int i, override_q, pow, num_qps, ret;
1949 	u8 netdev_tc = 0, offset = 0;
1950 
1951 	if (vsi->type != I40E_VSI_MAIN)
1952 		return -EINVAL;
1953 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1954 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1955 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1956 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1957 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1958 
1959 	/* find the next higher power-of-2 of num queue pairs */
1960 	pow = ilog2(num_qps);
1961 	if (!is_power_of_2(num_qps))
1962 		pow++;
1963 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1964 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1965 
1966 	/* Setup queue offset/count for all TCs for given VSI */
1967 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1968 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1969 		/* See if the given TC is enabled for the given VSI */
1970 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1971 			offset = vsi->mqprio_qopt.qopt.offset[i];
1972 			qcount = vsi->mqprio_qopt.qopt.count[i];
1973 			if (qcount > max_qcount)
1974 				max_qcount = qcount;
1975 			vsi->tc_config.tc_info[i].qoffset = offset;
1976 			vsi->tc_config.tc_info[i].qcount = qcount;
1977 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1978 		} else {
1979 			/* TC is not enabled so set the offset to
1980 			 * default queue and allocate one queue
1981 			 * for the given TC.
1982 			 */
1983 			vsi->tc_config.tc_info[i].qoffset = 0;
1984 			vsi->tc_config.tc_info[i].qcount = 1;
1985 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1986 		}
1987 	}
1988 
1989 	/* Set actual Tx/Rx queue pairs */
1990 	vsi->num_queue_pairs = offset + qcount;
1991 
1992 	/* Setup queue TC[0].qmap for given VSI context */
1993 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1994 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1995 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1996 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1997 
1998 	/* Reconfigure RSS for main VSI with max queue count */
1999 	vsi->rss_size = max_qcount;
2000 	ret = i40e_vsi_config_rss(vsi);
2001 	if (ret) {
2002 		dev_info(&vsi->back->pdev->dev,
2003 			 "Failed to reconfig rss for num_queues (%u)\n",
2004 			 max_qcount);
2005 		return ret;
2006 	}
2007 	vsi->reconfig_rss = true;
2008 	dev_dbg(&vsi->back->pdev->dev,
2009 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
2010 
2011 	/* Find queue count available for channel VSIs and starting offset
2012 	 * for channel VSIs
2013 	 */
2014 	override_q = vsi->mqprio_qopt.qopt.count[0];
2015 	if (override_q && override_q < vsi->num_queue_pairs) {
2016 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
2017 		vsi->next_base_queue = override_q;
2018 	}
2019 	return 0;
2020 }
2021 
2022 /**
2023  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2024  * @vsi: the VSI being setup
2025  * @ctxt: VSI context structure
2026  * @enabled_tc: Enabled TCs bitmap
2027  * @is_add: True if called before Add VSI
2028  *
2029  * Setup VSI queue mapping for enabled traffic classes.
2030  **/
2031 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2032 				     struct i40e_vsi_context *ctxt,
2033 				     u8 enabled_tc,
2034 				     bool is_add)
2035 {
2036 	struct i40e_pf *pf = vsi->back;
2037 	u16 num_tc_qps = 0;
2038 	u16 sections = 0;
2039 	u8 netdev_tc = 0;
2040 	u16 numtc = 1;
2041 	u16 qcount;
2042 	u8 offset;
2043 	u16 qmap;
2044 	int i;
2045 
2046 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2047 	offset = 0;
2048 	/* zero out queue mapping, it will get updated on the end of the function */
2049 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2050 
2051 	if (vsi->type == I40E_VSI_MAIN) {
2052 		/* This code helps add more queue to the VSI if we have
2053 		 * more cores than RSS can support, the higher cores will
2054 		 * be served by ATR or other filters. Furthermore, the
2055 		 * non-zero req_queue_pairs says that user requested a new
2056 		 * queue count via ethtool's set_channels, so use this
2057 		 * value for queues distribution across traffic classes
2058 		 * We need at least one queue pair for the interface
2059 		 * to be usable as we see in else statement.
2060 		 */
2061 		if (vsi->req_queue_pairs > 0)
2062 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2063 		else if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2064 			vsi->num_queue_pairs = pf->num_lan_msix;
2065 		else
2066 			vsi->num_queue_pairs = 1;
2067 	}
2068 
2069 	/* Number of queues per enabled TC */
2070 	if (vsi->type == I40E_VSI_MAIN ||
2071 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2072 		num_tc_qps = vsi->num_queue_pairs;
2073 	else
2074 		num_tc_qps = vsi->alloc_queue_pairs;
2075 
2076 	if (enabled_tc && test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
2077 		/* Find numtc from enabled TC bitmap */
2078 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2079 			if (enabled_tc & BIT(i)) /* TC is enabled */
2080 				numtc++;
2081 		}
2082 		if (!numtc) {
2083 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2084 			numtc = 1;
2085 		}
2086 		num_tc_qps = num_tc_qps / numtc;
2087 		num_tc_qps = min_t(int, num_tc_qps,
2088 				   i40e_pf_get_max_q_per_tc(pf));
2089 	}
2090 
2091 	vsi->tc_config.numtc = numtc;
2092 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2093 
2094 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2095 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
2096 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2097 
2098 	/* Setup queue offset/count for all TCs for given VSI */
2099 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2100 		/* See if the given TC is enabled for the given VSI */
2101 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2102 			/* TC is enabled */
2103 			int pow, num_qps;
2104 
2105 			switch (vsi->type) {
2106 			case I40E_VSI_MAIN:
2107 				if ((!test_bit(I40E_FLAG_FD_SB_ENA,
2108 					       pf->flags) &&
2109 				     !test_bit(I40E_FLAG_FD_ATR_ENA,
2110 					       pf->flags)) ||
2111 				    vsi->tc_config.enabled_tc != 1) {
2112 					qcount = min_t(int, pf->alloc_rss_size,
2113 						       num_tc_qps);
2114 					break;
2115 				}
2116 				fallthrough;
2117 			case I40E_VSI_FDIR:
2118 			case I40E_VSI_SRIOV:
2119 			case I40E_VSI_VMDQ2:
2120 			default:
2121 				qcount = num_tc_qps;
2122 				WARN_ON(i != 0);
2123 				break;
2124 			}
2125 			vsi->tc_config.tc_info[i].qoffset = offset;
2126 			vsi->tc_config.tc_info[i].qcount = qcount;
2127 
2128 			/* find the next higher power-of-2 of num queue pairs */
2129 			num_qps = qcount;
2130 			pow = 0;
2131 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2132 				pow++;
2133 				num_qps >>= 1;
2134 			}
2135 
2136 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2137 			qmap =
2138 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2139 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2140 
2141 			offset += qcount;
2142 		} else {
2143 			/* TC is not enabled so set the offset to
2144 			 * default queue and allocate one queue
2145 			 * for the given TC.
2146 			 */
2147 			vsi->tc_config.tc_info[i].qoffset = 0;
2148 			vsi->tc_config.tc_info[i].qcount = 1;
2149 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2150 
2151 			qmap = 0;
2152 		}
2153 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2154 	}
2155 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2156 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2157 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2158 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2159 		vsi->num_queue_pairs = offset;
2160 
2161 	/* Scheduler section valid can only be set for ADD VSI */
2162 	if (is_add) {
2163 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2164 
2165 		ctxt->info.up_enable_bits = enabled_tc;
2166 	}
2167 	if (vsi->type == I40E_VSI_SRIOV) {
2168 		ctxt->info.mapping_flags |=
2169 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2170 		for (i = 0; i < vsi->num_queue_pairs; i++)
2171 			ctxt->info.queue_mapping[i] =
2172 					       cpu_to_le16(vsi->base_queue + i);
2173 	} else {
2174 		ctxt->info.mapping_flags |=
2175 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2176 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2177 	}
2178 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2179 }
2180 
2181 /**
2182  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2183  * @netdev: the netdevice
2184  * @addr: address to add
2185  *
2186  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2187  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2188  */
2189 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2190 {
2191 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2192 	struct i40e_vsi *vsi = np->vsi;
2193 
2194 	if (i40e_add_mac_filter(vsi, addr))
2195 		return 0;
2196 	else
2197 		return -ENOMEM;
2198 }
2199 
2200 /**
2201  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2202  * @netdev: the netdevice
2203  * @addr: address to add
2204  *
2205  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2206  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2207  */
2208 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2209 {
2210 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2211 	struct i40e_vsi *vsi = np->vsi;
2212 
2213 	/* Under some circumstances, we might receive a request to delete
2214 	 * our own device address from our uc list. Because we store the
2215 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2216 	 * such requests and not delete our device address from this list.
2217 	 */
2218 	if (ether_addr_equal(addr, netdev->dev_addr))
2219 		return 0;
2220 
2221 	i40e_del_mac_filter(vsi, addr);
2222 
2223 	return 0;
2224 }
2225 
2226 /**
2227  * i40e_set_rx_mode - NDO callback to set the netdev filters
2228  * @netdev: network interface device structure
2229  **/
2230 static void i40e_set_rx_mode(struct net_device *netdev)
2231 {
2232 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2233 	struct i40e_vsi *vsi = np->vsi;
2234 
2235 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2236 
2237 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2238 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2239 
2240 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2241 
2242 	/* check for other flag changes */
2243 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2244 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2245 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2246 	}
2247 	i40e_service_event_schedule(vsi->back);
2248 }
2249 
2250 /**
2251  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2252  * @vsi: Pointer to VSI struct
2253  * @from: Pointer to list which contains MAC filter entries - changes to
2254  *        those entries needs to be undone.
2255  *
2256  * MAC filter entries from this list were slated for deletion.
2257  **/
2258 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2259 					 struct hlist_head *from)
2260 {
2261 	struct i40e_mac_filter *f;
2262 	struct hlist_node *h;
2263 
2264 	hlist_for_each_entry_safe(f, h, from, hlist) {
2265 		u64 key = i40e_addr_to_hkey(f->macaddr);
2266 
2267 		/* Move the element back into MAC filter list*/
2268 		hlist_del(&f->hlist);
2269 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2270 	}
2271 }
2272 
2273 /**
2274  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2275  * @vsi: Pointer to vsi struct
2276  * @from: Pointer to list which contains MAC filter entries - changes to
2277  *        those entries needs to be undone.
2278  *
2279  * MAC filter entries from this list were slated for addition.
2280  **/
2281 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2282 					 struct hlist_head *from)
2283 {
2284 	struct i40e_new_mac_filter *new;
2285 	struct hlist_node *h;
2286 
2287 	hlist_for_each_entry_safe(new, h, from, hlist) {
2288 		/* We can simply free the wrapper structure */
2289 		hlist_del(&new->hlist);
2290 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2291 		kfree(new);
2292 	}
2293 }
2294 
2295 /**
2296  * i40e_next_filter - Get the next non-broadcast filter from a list
2297  * @next: pointer to filter in list
2298  *
2299  * Returns the next non-broadcast filter in the list. Required so that we
2300  * ignore broadcast filters within the list, since these are not handled via
2301  * the normal firmware update path.
2302  */
2303 static
2304 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2305 {
2306 	hlist_for_each_entry_continue(next, hlist) {
2307 		if (!is_broadcast_ether_addr(next->f->macaddr))
2308 			return next;
2309 	}
2310 
2311 	return NULL;
2312 }
2313 
2314 /**
2315  * i40e_update_filter_state - Update filter state based on return data
2316  * from firmware
2317  * @count: Number of filters added
2318  * @add_list: return data from fw
2319  * @add_head: pointer to first filter in current batch
2320  *
2321  * MAC filter entries from list were slated to be added to device. Returns
2322  * number of successful filters. Note that 0 does NOT mean success!
2323  **/
2324 static int
2325 i40e_update_filter_state(int count,
2326 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2327 			 struct i40e_new_mac_filter *add_head)
2328 {
2329 	int retval = 0;
2330 	int i;
2331 
2332 	for (i = 0; i < count; i++) {
2333 		/* Always check status of each filter. We don't need to check
2334 		 * the firmware return status because we pre-set the filter
2335 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2336 		 * request to the adminq. Thus, if it no longer matches then
2337 		 * we know the filter is active.
2338 		 */
2339 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2340 			add_head->state = I40E_FILTER_FAILED;
2341 		} else {
2342 			add_head->state = I40E_FILTER_ACTIVE;
2343 			retval++;
2344 		}
2345 
2346 		add_head = i40e_next_filter(add_head);
2347 		if (!add_head)
2348 			break;
2349 	}
2350 
2351 	return retval;
2352 }
2353 
2354 /**
2355  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2356  * @vsi: ptr to the VSI
2357  * @vsi_name: name to display in messages
2358  * @list: the list of filters to send to firmware
2359  * @num_del: the number of filters to delete
2360  * @retval: Set to -EIO on failure to delete
2361  *
2362  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2363  * *retval instead of a return value so that success does not force ret_val to
2364  * be set to 0. This ensures that a sequence of calls to this function
2365  * preserve the previous value of *retval on successful delete.
2366  */
2367 static
2368 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2369 			  struct i40e_aqc_remove_macvlan_element_data *list,
2370 			  int num_del, int *retval)
2371 {
2372 	struct i40e_hw *hw = &vsi->back->hw;
2373 	enum libie_aq_err aq_status;
2374 	int aq_ret;
2375 
2376 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2377 					   &aq_status);
2378 
2379 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2380 	if (aq_ret && !(aq_status == LIBIE_AQ_RC_ENOENT)) {
2381 		*retval = -EIO;
2382 		dev_info(&vsi->back->pdev->dev,
2383 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2384 			 vsi_name, ERR_PTR(aq_ret), libie_aq_str(aq_status));
2385 	}
2386 }
2387 
2388 /**
2389  * i40e_aqc_add_filters - Request firmware to add a set of filters
2390  * @vsi: ptr to the VSI
2391  * @vsi_name: name to display in messages
2392  * @list: the list of filters to send to firmware
2393  * @add_head: Position in the add hlist
2394  * @num_add: the number of filters to add
2395  *
2396  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2397  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2398  * space for more filters.
2399  */
2400 static
2401 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2402 			  struct i40e_aqc_add_macvlan_element_data *list,
2403 			  struct i40e_new_mac_filter *add_head,
2404 			  int num_add)
2405 {
2406 	struct i40e_hw *hw = &vsi->back->hw;
2407 	enum libie_aq_err aq_status;
2408 	int fcnt;
2409 
2410 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2411 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2412 
2413 	if (fcnt != num_add) {
2414 		if (vsi->type == I40E_VSI_MAIN) {
2415 			if (!test_and_set_bit(__I40E_VSI_OVERFLOW_PROMISC,
2416 					      vsi->state)) {
2417 				dev_warn(&vsi->back->pdev->dev,
2418 					 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2419 					 libie_aq_str(aq_status), vsi_name);
2420 			}
2421 		} else if (vsi->type == I40E_VSI_SRIOV ||
2422 			   vsi->type == I40E_VSI_VMDQ1 ||
2423 			   vsi->type == I40E_VSI_VMDQ2) {
2424 			dev_warn(&vsi->back->pdev->dev,
2425 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2426 				 libie_aq_str(aq_status), vsi_name, vsi_name);
2427 		} else {
2428 			dev_warn(&vsi->back->pdev->dev,
2429 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2430 				 libie_aq_str(aq_status), vsi_name, vsi->type);
2431 		}
2432 	}
2433 }
2434 
2435 /**
2436  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2437  * @vsi: pointer to the VSI
2438  * @vsi_name: the VSI name
2439  * @f: filter data
2440  *
2441  * This function sets or clears the promiscuous broadcast flags for VLAN
2442  * filters in order to properly receive broadcast frames. Assumes that only
2443  * broadcast filters are passed.
2444  *
2445  * Returns status indicating success or failure;
2446  **/
2447 static int
2448 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2449 			  struct i40e_mac_filter *f)
2450 {
2451 	bool enable = f->state == I40E_FILTER_NEW ||
2452 		      f->state == I40E_FILTER_NEW_SYNC;
2453 	struct i40e_hw *hw = &vsi->back->hw;
2454 	int aq_ret;
2455 
2456 	if (f->vlan == I40E_VLAN_ANY) {
2457 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2458 						   vsi->seid,
2459 						   enable,
2460 						   NULL);
2461 	} else {
2462 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2463 							    vsi->seid,
2464 							    enable,
2465 							    f->vlan,
2466 							    NULL);
2467 	}
2468 
2469 	if (aq_ret) {
2470 		if (!test_and_set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state)) {
2471 			dev_warn(&vsi->back->pdev->dev,
2472 				 "Error %s, forcing overflow promiscuous on %s\n",
2473 				 libie_aq_str(hw->aq.asq_last_status), vsi_name);
2474 		}
2475 	}
2476 
2477 	return aq_ret;
2478 }
2479 
2480 /**
2481  * i40e_set_promiscuous - set promiscuous mode
2482  * @pf: board private structure
2483  * @promisc: promisc on or off
2484  *
2485  * There are different ways of setting promiscuous mode on a PF depending on
2486  * what state/environment we're in.  This identifies and sets it appropriately.
2487  * Returns 0 on success.
2488  **/
2489 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2490 {
2491 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
2492 	struct i40e_hw *hw = &pf->hw;
2493 	int aq_ret;
2494 
2495 	if (vsi->type == I40E_VSI_MAIN &&
2496 	    i40e_pf_get_main_veb(pf) &&
2497 	    !test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
2498 		/* set defport ON for Main VSI instead of true promisc
2499 		 * this way we will get all unicast/multicast and VLAN
2500 		 * promisc behavior but will not get VF or VMDq traffic
2501 		 * replicated on the Main VSI.
2502 		 */
2503 		if (promisc)
2504 			aq_ret = i40e_aq_set_default_vsi(hw,
2505 							 vsi->seid,
2506 							 NULL);
2507 		else
2508 			aq_ret = i40e_aq_clear_default_vsi(hw,
2509 							   vsi->seid,
2510 							   NULL);
2511 		if (aq_ret) {
2512 			dev_info(&pf->pdev->dev,
2513 				 "Set default VSI failed, err %pe, aq_err %s\n",
2514 				 ERR_PTR(aq_ret),
2515 				 libie_aq_str(hw->aq.asq_last_status));
2516 		}
2517 	} else {
2518 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2519 						  hw,
2520 						  vsi->seid,
2521 						  promisc, NULL,
2522 						  true);
2523 		if (aq_ret) {
2524 			dev_info(&pf->pdev->dev,
2525 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2526 				 ERR_PTR(aq_ret),
2527 				 libie_aq_str(hw->aq.asq_last_status));
2528 		}
2529 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2530 						  hw,
2531 						  vsi->seid,
2532 						  promisc, NULL);
2533 		if (aq_ret) {
2534 			dev_info(&pf->pdev->dev,
2535 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2536 				 ERR_PTR(aq_ret),
2537 				 libie_aq_str(hw->aq.asq_last_status));
2538 		}
2539 	}
2540 
2541 	if (!aq_ret)
2542 		pf->cur_promisc = promisc;
2543 
2544 	return aq_ret;
2545 }
2546 
2547 /**
2548  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2549  * @vsi: ptr to the VSI
2550  *
2551  * Push any outstanding VSI filter changes through the AdminQ.
2552  *
2553  * Returns 0 or error value
2554  **/
2555 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2556 {
2557 	struct hlist_head tmp_add_list, tmp_del_list;
2558 	struct i40e_mac_filter *f;
2559 	struct i40e_new_mac_filter *new, *add_head = NULL;
2560 	struct i40e_hw *hw = &vsi->back->hw;
2561 	bool old_overflow, new_overflow;
2562 	unsigned int failed_filters = 0;
2563 	unsigned int vlan_filters = 0;
2564 	char vsi_name[16] = "PF";
2565 	int filter_list_len = 0;
2566 	u32 changed_flags = 0;
2567 	struct hlist_node *h;
2568 	struct i40e_pf *pf;
2569 	int num_add = 0;
2570 	int num_del = 0;
2571 	int aq_ret = 0;
2572 	int retval = 0;
2573 	u16 cmd_flags;
2574 	int list_size;
2575 	int bkt;
2576 
2577 	/* empty array typed pointers, kcalloc later */
2578 	struct i40e_aqc_add_macvlan_element_data *add_list;
2579 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2580 
2581 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2582 		usleep_range(1000, 2000);
2583 	pf = vsi->back;
2584 
2585 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2586 
2587 	if (vsi->netdev) {
2588 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2589 		vsi->current_netdev_flags = vsi->netdev->flags;
2590 	}
2591 
2592 	INIT_HLIST_HEAD(&tmp_add_list);
2593 	INIT_HLIST_HEAD(&tmp_del_list);
2594 
2595 	if (vsi->type == I40E_VSI_SRIOV)
2596 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2597 	else if (vsi->type != I40E_VSI_MAIN)
2598 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2599 
2600 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2601 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2602 
2603 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2604 		/* Create a list of filters to delete. */
2605 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2606 			if (f->state == I40E_FILTER_REMOVE) {
2607 				/* Move the element into temporary del_list */
2608 				hash_del(&f->hlist);
2609 				hlist_add_head(&f->hlist, &tmp_del_list);
2610 
2611 				/* Avoid counting removed filters */
2612 				continue;
2613 			}
2614 			if (f->state == I40E_FILTER_NEW) {
2615 				/* Create a temporary i40e_new_mac_filter */
2616 				new = kzalloc_obj(*new, GFP_ATOMIC);
2617 				if (!new)
2618 					goto err_no_memory_locked;
2619 
2620 				/* Store pointer to the real filter */
2621 				new->f = f;
2622 				new->state = f->state;
2623 
2624 				/* Add it to the hash list */
2625 				hlist_add_head(&new->hlist, &tmp_add_list);
2626 				f->state = I40E_FILTER_NEW_SYNC;
2627 			}
2628 
2629 			/* Count the number of active (current and new) VLAN
2630 			 * filters we have now. Does not count filters which
2631 			 * are marked for deletion.
2632 			 */
2633 			if (f->vlan > 0)
2634 				vlan_filters++;
2635 		}
2636 
2637 		if (vsi->type != I40E_VSI_SRIOV)
2638 			retval = i40e_correct_mac_vlan_filters
2639 				(vsi, &tmp_add_list, &tmp_del_list,
2640 				 vlan_filters);
2641 		else if (pf->vf)
2642 			retval = i40e_correct_vf_mac_vlan_filters
2643 				(vsi, &tmp_add_list, &tmp_del_list,
2644 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2645 
2646 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2647 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2648 
2649 		if (retval)
2650 			goto err_no_memory_locked;
2651 
2652 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2653 	}
2654 
2655 	/* Now process 'del_list' outside the lock */
2656 	if (!hlist_empty(&tmp_del_list)) {
2657 		filter_list_len = hw->aq.asq_buf_size /
2658 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2659 		list_size = filter_list_len *
2660 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2661 		del_list = kzalloc(list_size, GFP_ATOMIC);
2662 		if (!del_list)
2663 			goto err_no_memory;
2664 
2665 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2666 			cmd_flags = 0;
2667 
2668 			/* handle broadcast filters by updating the broadcast
2669 			 * promiscuous flag and release filter list.
2670 			 */
2671 			if (is_broadcast_ether_addr(f->macaddr)) {
2672 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2673 
2674 				hlist_del(&f->hlist);
2675 				kfree(f);
2676 				continue;
2677 			}
2678 
2679 			/* add to delete list */
2680 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2681 			if (f->vlan == I40E_VLAN_ANY) {
2682 				del_list[num_del].vlan_tag = 0;
2683 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2684 			} else {
2685 				del_list[num_del].vlan_tag =
2686 					cpu_to_le16((u16)(f->vlan));
2687 			}
2688 
2689 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2690 			del_list[num_del].flags = cmd_flags;
2691 			num_del++;
2692 
2693 			/* flush a full buffer */
2694 			if (num_del == filter_list_len) {
2695 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2696 						     num_del, &retval);
2697 				memset(del_list, 0, list_size);
2698 				num_del = 0;
2699 			}
2700 			/* Release memory for MAC filter entries which were
2701 			 * synced up with HW.
2702 			 */
2703 			hlist_del(&f->hlist);
2704 			kfree(f);
2705 		}
2706 
2707 		if (num_del) {
2708 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2709 					     num_del, &retval);
2710 		}
2711 
2712 		kfree(del_list);
2713 		del_list = NULL;
2714 	}
2715 
2716 	if (!hlist_empty(&tmp_add_list)) {
2717 		/* Do all the adds now. */
2718 		filter_list_len = hw->aq.asq_buf_size /
2719 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2720 		list_size = filter_list_len *
2721 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2722 		add_list = kzalloc(list_size, GFP_ATOMIC);
2723 		if (!add_list)
2724 			goto err_no_memory;
2725 
2726 		num_add = 0;
2727 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2728 			/* handle broadcast filters by updating the broadcast
2729 			 * promiscuous flag instead of adding a MAC filter.
2730 			 */
2731 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2732 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2733 							      new->f))
2734 					new->state = I40E_FILTER_FAILED;
2735 				else
2736 					new->state = I40E_FILTER_ACTIVE;
2737 				continue;
2738 			}
2739 
2740 			/* add to add array */
2741 			if (num_add == 0)
2742 				add_head = new;
2743 			cmd_flags = 0;
2744 			ether_addr_copy(add_list[num_add].mac_addr,
2745 					new->f->macaddr);
2746 			if (new->f->vlan == I40E_VLAN_ANY) {
2747 				add_list[num_add].vlan_tag = 0;
2748 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2749 			} else {
2750 				add_list[num_add].vlan_tag =
2751 					cpu_to_le16((u16)(new->f->vlan));
2752 			}
2753 			add_list[num_add].queue_number = 0;
2754 			/* set invalid match method for later detection */
2755 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2756 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2757 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2758 			num_add++;
2759 
2760 			/* flush a full buffer */
2761 			if (num_add == filter_list_len) {
2762 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2763 						     add_head, num_add);
2764 				memset(add_list, 0, list_size);
2765 				num_add = 0;
2766 			}
2767 		}
2768 		if (num_add) {
2769 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2770 					     num_add);
2771 		}
2772 		/* Now move all of the filters from the temp add list back to
2773 		 * the VSI's list.
2774 		 */
2775 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2776 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2777 			/* Only update the state if we're still NEW */
2778 			if (new->f->state == I40E_FILTER_NEW ||
2779 			    new->f->state == I40E_FILTER_NEW_SYNC)
2780 				new->f->state = new->state;
2781 			hlist_del(&new->hlist);
2782 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2783 			kfree(new);
2784 		}
2785 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2786 		kfree(add_list);
2787 		add_list = NULL;
2788 	}
2789 
2790 	/* Determine the number of active and failed filters. */
2791 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2792 	vsi->active_filters = 0;
2793 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2794 		if (f->state == I40E_FILTER_ACTIVE)
2795 			vsi->active_filters++;
2796 		else if (f->state == I40E_FILTER_FAILED)
2797 			failed_filters++;
2798 	}
2799 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2800 
2801 	/* Check if we are able to exit overflow promiscuous mode. We can
2802 	 * safely exit if we didn't just enter, we no longer have any failed
2803 	 * filters, and we have reduced filters below the threshold value.
2804 	 */
2805 	if (old_overflow && !failed_filters &&
2806 	    vsi->active_filters < vsi->promisc_threshold) {
2807 		dev_info(&pf->pdev->dev,
2808 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2809 			 vsi_name);
2810 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2811 		vsi->promisc_threshold = 0;
2812 	}
2813 
2814 	/* if the VF is not trusted do not do promisc */
2815 	if (vsi->type == I40E_VSI_SRIOV && pf->vf &&
2816 	    !pf->vf[vsi->vf_id].trusted) {
2817 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2818 		goto out;
2819 	}
2820 
2821 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2822 
2823 	/* If we are entering overflow promiscuous, we need to calculate a new
2824 	 * threshold for when we are safe to exit
2825 	 */
2826 	if (!old_overflow && new_overflow)
2827 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2828 
2829 	/* check for changes in promiscuous modes */
2830 	if (changed_flags & IFF_ALLMULTI) {
2831 		bool cur_multipromisc;
2832 
2833 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2834 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2835 							       vsi->seid,
2836 							       cur_multipromisc,
2837 							       NULL);
2838 		if (aq_ret) {
2839 			retval = i40e_aq_rc_to_posix(aq_ret,
2840 						     hw->aq.asq_last_status);
2841 			dev_info(&pf->pdev->dev,
2842 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2843 				 vsi_name,
2844 				 ERR_PTR(aq_ret),
2845 				 libie_aq_str(hw->aq.asq_last_status));
2846 		} else {
2847 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2848 				 cur_multipromisc ? "entering" : "leaving");
2849 		}
2850 	}
2851 
2852 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2853 		bool cur_promisc;
2854 
2855 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2856 			       new_overflow);
2857 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2858 		if (aq_ret) {
2859 			retval = i40e_aq_rc_to_posix(aq_ret,
2860 						     hw->aq.asq_last_status);
2861 			dev_info(&pf->pdev->dev,
2862 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2863 				 cur_promisc ? "on" : "off",
2864 				 vsi_name,
2865 				 ERR_PTR(aq_ret),
2866 				 libie_aq_str(hw->aq.asq_last_status));
2867 		}
2868 	}
2869 out:
2870 	/* if something went wrong then set the changed flag so we try again */
2871 	if (retval)
2872 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2873 
2874 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2875 	return retval;
2876 
2877 err_no_memory:
2878 	/* Restore elements on the temporary add and delete lists */
2879 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2880 err_no_memory_locked:
2881 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2882 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2883 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2884 
2885 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2886 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2887 	return -ENOMEM;
2888 }
2889 
2890 /**
2891  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2892  * @pf: board private structure
2893  **/
2894 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2895 {
2896 	struct i40e_vsi *vsi;
2897 	int v;
2898 
2899 	if (!pf)
2900 		return;
2901 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2902 		return;
2903 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2904 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2905 		return;
2906 	}
2907 
2908 	i40e_pf_for_each_vsi(pf, v, vsi) {
2909 		if ((vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2910 		    !test_bit(__I40E_VSI_RELEASING, vsi->state)) {
2911 			int ret = i40e_sync_vsi_filters(vsi);
2912 
2913 			if (ret) {
2914 				/* come back and try again later */
2915 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2916 					pf->state);
2917 				break;
2918 			}
2919 		}
2920 	}
2921 }
2922 
2923 /**
2924  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2925  *
2926  * @vsi: VSI to calculate rx_buf_len from
2927  */
2928 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2929 {
2930 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags))
2931 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2932 
2933 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2934 }
2935 
2936 /**
2937  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2938  * @vsi: the vsi
2939  * @xdp_prog: XDP program
2940  **/
2941 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2942 				   struct bpf_prog *xdp_prog)
2943 {
2944 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2945 	u16 chain_len;
2946 
2947 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2948 		chain_len = 1;
2949 	else
2950 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2951 
2952 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2953 }
2954 
2955 /**
2956  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2957  * @netdev: network interface device structure
2958  * @new_mtu: new value for maximum frame size
2959  *
2960  * Returns 0 on success, negative on failure
2961  **/
2962 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2963 {
2964 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2965 	struct i40e_vsi *vsi = np->vsi;
2966 	struct i40e_pf *pf = vsi->back;
2967 	int frame_size;
2968 
2969 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2970 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2971 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2972 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2973 		return -EINVAL;
2974 	}
2975 
2976 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2977 		   netdev->mtu, new_mtu);
2978 	WRITE_ONCE(netdev->mtu, new_mtu);
2979 	if (netif_running(netdev))
2980 		i40e_vsi_reinit_locked(vsi);
2981 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2982 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2983 	return 0;
2984 }
2985 
2986 /**
2987  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2988  * @vsi: the vsi being adjusted
2989  **/
2990 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2991 {
2992 	struct i40e_vsi_context ctxt;
2993 	int ret;
2994 
2995 	/* Don't modify stripping options if a port VLAN is active */
2996 	if (vsi->info.pvid)
2997 		return;
2998 
2999 	if ((vsi->info.valid_sections &
3000 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3001 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
3002 		return;  /* already enabled */
3003 
3004 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3005 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3006 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
3007 
3008 	ctxt.seid = vsi->seid;
3009 	ctxt.info = vsi->info;
3010 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3011 	if (ret) {
3012 		dev_info(&vsi->back->pdev->dev,
3013 			 "update vlan stripping failed, err %pe aq_err %s\n",
3014 			 ERR_PTR(ret),
3015 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3016 	}
3017 }
3018 
3019 /**
3020  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3021  * @vsi: the vsi being adjusted
3022  **/
3023 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3024 {
3025 	struct i40e_vsi_context ctxt;
3026 	int ret;
3027 
3028 	/* Don't modify stripping options if a port VLAN is active */
3029 	if (vsi->info.pvid)
3030 		return;
3031 
3032 	if ((vsi->info.valid_sections &
3033 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3034 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3035 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3036 		return;  /* already disabled */
3037 
3038 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3039 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3040 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3041 
3042 	ctxt.seid = vsi->seid;
3043 	ctxt.info = vsi->info;
3044 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3045 	if (ret) {
3046 		dev_info(&vsi->back->pdev->dev,
3047 			 "update vlan stripping failed, err %pe aq_err %s\n",
3048 			 ERR_PTR(ret),
3049 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3050 	}
3051 }
3052 
3053 /**
3054  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3055  * @vsi: the vsi being configured
3056  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3057  *
3058  * This is a helper function for adding a new MAC/VLAN filter with the
3059  * specified VLAN for each existing MAC address already in the hash table.
3060  * This function does *not* perform any accounting to update filters based on
3061  * VLAN mode.
3062  *
3063  * NOTE: this function expects to be called while under the
3064  * mac_filter_hash_lock
3065  **/
3066 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3067 {
3068 	struct i40e_mac_filter *f, *add_f;
3069 	struct hlist_node *h;
3070 	int bkt;
3071 
3072 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3073 		/* If we're asked to add a filter that has been marked for
3074 		 * removal, it is safe to simply restore it to active state.
3075 		 * __i40e_del_filter will have simply deleted any filters which
3076 		 * were previously marked NEW or FAILED, so if it is currently
3077 		 * marked REMOVE it must have previously been ACTIVE. Since we
3078 		 * haven't yet run the sync filters task, just restore this
3079 		 * filter to the ACTIVE state so that the sync task leaves it
3080 		 * in place.
3081 		 */
3082 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3083 			f->state = I40E_FILTER_ACTIVE;
3084 			continue;
3085 		} else if (f->state == I40E_FILTER_REMOVE) {
3086 			continue;
3087 		}
3088 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3089 		if (!add_f) {
3090 			dev_info(&vsi->back->pdev->dev,
3091 				 "Could not add vlan filter %d for %pM\n",
3092 				 vid, f->macaddr);
3093 			return -ENOMEM;
3094 		}
3095 	}
3096 
3097 	return 0;
3098 }
3099 
3100 /**
3101  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3102  * @vsi: the VSI being configured
3103  * @vid: VLAN id to be added
3104  **/
3105 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3106 {
3107 	int err;
3108 
3109 	if (vsi->info.pvid)
3110 		return -EINVAL;
3111 
3112 	/* The network stack will attempt to add VID=0, with the intention to
3113 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3114 	 * these packets by default when configured to receive untagged
3115 	 * packets, so we don't need to add a filter for this case.
3116 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3117 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3118 	 * Thus, we do not want to actually add a filter for VID=0
3119 	 */
3120 	if (!vid)
3121 		return 0;
3122 
3123 	/* Locked once because all functions invoked below iterates list*/
3124 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3125 	err = i40e_add_vlan_all_mac(vsi, vid);
3126 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3127 	if (err)
3128 		return err;
3129 
3130 	/* schedule our worker thread which will take care of
3131 	 * applying the new filter changes
3132 	 */
3133 	i40e_service_event_schedule(vsi->back);
3134 	return 0;
3135 }
3136 
3137 /**
3138  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3139  * @vsi: the vsi being configured
3140  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3141  *
3142  * This function should be used to remove all VLAN filters which match the
3143  * given VID. It does not schedule the service event and does not take the
3144  * mac_filter_hash_lock so it may be combined with other operations under
3145  * a single invocation of the mac_filter_hash_lock.
3146  *
3147  * NOTE: this function expects to be called while under the
3148  * mac_filter_hash_lock
3149  */
3150 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3151 {
3152 	struct i40e_mac_filter *f;
3153 	struct hlist_node *h;
3154 	int bkt;
3155 
3156 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3157 		if (f->vlan == vid)
3158 			__i40e_del_filter(vsi, f);
3159 	}
3160 }
3161 
3162 /**
3163  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3164  * @vsi: the VSI being configured
3165  * @vid: VLAN id to be removed
3166  **/
3167 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3168 {
3169 	if (!vid || vsi->info.pvid)
3170 		return;
3171 
3172 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3173 	i40e_rm_vlan_all_mac(vsi, vid);
3174 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3175 
3176 	/* schedule our worker thread which will take care of
3177 	 * applying the new filter changes
3178 	 */
3179 	i40e_service_event_schedule(vsi->back);
3180 }
3181 
3182 /**
3183  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3184  * @netdev: network interface to be adjusted
3185  * @proto: unused protocol value
3186  * @vid: vlan id to be added
3187  *
3188  * net_device_ops implementation for adding vlan ids
3189  **/
3190 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3191 				__always_unused __be16 proto, u16 vid)
3192 {
3193 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3194 	struct i40e_vsi *vsi = np->vsi;
3195 	int ret = 0;
3196 
3197 	if (vid >= VLAN_N_VID)
3198 		return -EINVAL;
3199 
3200 	ret = i40e_vsi_add_vlan(vsi, vid);
3201 	if (!ret)
3202 		set_bit(vid, vsi->active_vlans);
3203 
3204 	return ret;
3205 }
3206 
3207 /**
3208  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3209  * @netdev: network interface to be adjusted
3210  * @proto: unused protocol value
3211  * @vid: vlan id to be added
3212  **/
3213 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3214 				    __always_unused __be16 proto, u16 vid)
3215 {
3216 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3217 	struct i40e_vsi *vsi = np->vsi;
3218 
3219 	if (vid >= VLAN_N_VID)
3220 		return;
3221 	set_bit(vid, vsi->active_vlans);
3222 }
3223 
3224 /**
3225  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3226  * @netdev: network interface to be adjusted
3227  * @proto: unused protocol value
3228  * @vid: vlan id to be removed
3229  *
3230  * net_device_ops implementation for removing vlan ids
3231  **/
3232 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3233 				 __always_unused __be16 proto, u16 vid)
3234 {
3235 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3236 	struct i40e_vsi *vsi = np->vsi;
3237 
3238 	/* return code is ignored as there is nothing a user
3239 	 * can do about failure to remove and a log message was
3240 	 * already printed from the other function
3241 	 */
3242 	i40e_vsi_kill_vlan(vsi, vid);
3243 
3244 	clear_bit(vid, vsi->active_vlans);
3245 
3246 	return 0;
3247 }
3248 
3249 /**
3250  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3251  * @vsi: the vsi being brought back up
3252  **/
3253 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3254 {
3255 	u16 vid;
3256 
3257 	if (!vsi->netdev)
3258 		return;
3259 
3260 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3261 		i40e_vlan_stripping_enable(vsi);
3262 	else
3263 		i40e_vlan_stripping_disable(vsi);
3264 
3265 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3266 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3267 					vid);
3268 }
3269 
3270 /**
3271  * i40e_vsi_add_pvid - Add pvid for the VSI
3272  * @vsi: the vsi being adjusted
3273  * @vid: the vlan id to set as a PVID
3274  **/
3275 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3276 {
3277 	struct i40e_vsi_context ctxt;
3278 	int ret;
3279 
3280 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3281 	vsi->info.pvid = cpu_to_le16(vid);
3282 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3283 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3284 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3285 
3286 	ctxt.seid = vsi->seid;
3287 	ctxt.info = vsi->info;
3288 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3289 	if (ret) {
3290 		dev_info(&vsi->back->pdev->dev,
3291 			 "add pvid failed, err %pe aq_err %s\n",
3292 			 ERR_PTR(ret),
3293 			 libie_aq_str(vsi->back->hw.aq.asq_last_status));
3294 		return -ENOENT;
3295 	}
3296 
3297 	return 0;
3298 }
3299 
3300 /**
3301  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3302  * @vsi: the vsi being adjusted
3303  *
3304  * Just use the vlan_rx_register() service to put it back to normal
3305  **/
3306 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3307 {
3308 	vsi->info.pvid = 0;
3309 
3310 	i40e_vlan_stripping_disable(vsi);
3311 }
3312 
3313 /**
3314  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3315  * @vsi: ptr to the VSI
3316  *
3317  * If this function returns with an error, then it's possible one or
3318  * more of the rings is populated (while the rest are not).  It is the
3319  * callers duty to clean those orphaned rings.
3320  *
3321  * Return 0 on success, negative on failure
3322  **/
3323 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3324 {
3325 	int i, err = 0;
3326 
3327 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3328 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3329 
3330 	if (!i40e_enabled_xdp_vsi(vsi))
3331 		return err;
3332 
3333 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3334 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3335 
3336 	return err;
3337 }
3338 
3339 /**
3340  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3341  * @vsi: ptr to the VSI
3342  *
3343  * Free VSI's transmit software resources
3344  **/
3345 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3346 {
3347 	int i;
3348 
3349 	if (vsi->tx_rings) {
3350 		for (i = 0; i < vsi->num_queue_pairs; i++)
3351 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3352 				i40e_free_tx_resources(vsi->tx_rings[i]);
3353 	}
3354 
3355 	if (vsi->xdp_rings) {
3356 		for (i = 0; i < vsi->num_queue_pairs; i++)
3357 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3358 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3359 	}
3360 }
3361 
3362 /**
3363  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3364  * @vsi: ptr to the VSI
3365  *
3366  * If this function returns with an error, then it's possible one or
3367  * more of the rings is populated (while the rest are not).  It is the
3368  * callers duty to clean those orphaned rings.
3369  *
3370  * Return 0 on success, negative on failure
3371  **/
3372 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3373 {
3374 	int i, err = 0;
3375 
3376 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3377 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3378 	return err;
3379 }
3380 
3381 /**
3382  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3383  * @vsi: ptr to the VSI
3384  *
3385  * Free all receive software resources
3386  **/
3387 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3388 {
3389 	int i;
3390 
3391 	if (!vsi->rx_rings)
3392 		return;
3393 
3394 	for (i = 0; i < vsi->num_queue_pairs; i++)
3395 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3396 			i40e_free_rx_resources(vsi->rx_rings[i]);
3397 }
3398 
3399 /**
3400  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3401  * @ring: The Tx ring to configure
3402  *
3403  * This enables/disables XPS for a given Tx descriptor ring
3404  * based on the TCs enabled for the VSI that ring belongs to.
3405  **/
3406 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3407 {
3408 	int cpu;
3409 
3410 	if (!ring->q_vector || !ring->netdev || ring->ch)
3411 		return;
3412 
3413 	/* We only initialize XPS once, so as not to overwrite user settings */
3414 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3415 		return;
3416 
3417 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3418 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3419 			    ring->queue_index);
3420 }
3421 
3422 /**
3423  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3424  * @ring: The Tx or Rx ring
3425  *
3426  * Returns the AF_XDP buffer pool or NULL.
3427  **/
3428 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3429 {
3430 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3431 	int qid = ring->queue_index;
3432 
3433 	if (ring_is_xdp(ring))
3434 		qid -= ring->vsi->alloc_queue_pairs;
3435 
3436 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3437 		return NULL;
3438 
3439 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3440 }
3441 
3442 /**
3443  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3444  * @ring: The Tx ring to configure
3445  *
3446  * Configure the Tx descriptor ring in the HMC context.
3447  **/
3448 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3449 {
3450 	struct i40e_vsi *vsi = ring->vsi;
3451 	u16 pf_q = vsi->base_queue + ring->queue_index;
3452 	struct i40e_hw *hw = &vsi->back->hw;
3453 	struct i40e_hmc_obj_txq tx_ctx;
3454 	u32 qtx_ctl = 0;
3455 	int err = 0;
3456 
3457 	if (ring_is_xdp(ring))
3458 		ring->xsk_pool = i40e_xsk_pool(ring);
3459 
3460 	/* some ATR related tx ring init */
3461 	if (test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags)) {
3462 		ring->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
3463 		ring->atr_count = 0;
3464 	} else {
3465 		ring->atr_sample_rate = 0;
3466 	}
3467 
3468 	/* configure XPS */
3469 	i40e_config_xps_tx_ring(ring);
3470 
3471 	/* clear the context structure first */
3472 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3473 
3474 	tx_ctx.new_context = 1;
3475 	tx_ctx.base = (ring->dma / 128);
3476 	tx_ctx.qlen = ring->count;
3477 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags) ||
3478 	    test_bit(I40E_FLAG_FD_ATR_ENA, vsi->back->flags))
3479 		tx_ctx.fd_ena = 1;
3480 	if (test_bit(I40E_FLAG_PTP_ENA, vsi->back->flags))
3481 		tx_ctx.timesync_ena = 1;
3482 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3483 	if (vsi->type != I40E_VSI_FDIR)
3484 		tx_ctx.head_wb_ena = 1;
3485 	tx_ctx.head_wb_addr = ring->dma +
3486 			      (ring->count * sizeof(struct i40e_tx_desc));
3487 
3488 	/* As part of VSI creation/update, FW allocates certain
3489 	 * Tx arbitration queue sets for each TC enabled for
3490 	 * the VSI. The FW returns the handles to these queue
3491 	 * sets as part of the response buffer to Add VSI,
3492 	 * Update VSI, etc. AQ commands. It is expected that
3493 	 * these queue set handles be associated with the Tx
3494 	 * queues by the driver as part of the TX queue context
3495 	 * initialization. This has to be done regardless of
3496 	 * DCB as by default everything is mapped to TC0.
3497 	 */
3498 
3499 	if (ring->ch)
3500 		tx_ctx.rdylist =
3501 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3502 
3503 	else
3504 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3505 
3506 	tx_ctx.rdylist_act = 0;
3507 
3508 	/* clear the context in the HMC */
3509 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3510 	if (err) {
3511 		dev_info(&vsi->back->pdev->dev,
3512 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3513 			 ring->queue_index, pf_q, err);
3514 		return -ENOMEM;
3515 	}
3516 
3517 	/* set the context in the HMC */
3518 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3519 	if (err) {
3520 		dev_info(&vsi->back->pdev->dev,
3521 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3522 			 ring->queue_index, pf_q, err);
3523 		return -ENOMEM;
3524 	}
3525 
3526 	/* Now associate this queue with this PCI function */
3527 	if (ring->ch) {
3528 		if (ring->ch->type == I40E_VSI_VMDQ2)
3529 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3530 		else
3531 			return -EINVAL;
3532 
3533 		qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3534 				      ring->ch->vsi_number);
3535 	} else {
3536 		if (vsi->type == I40E_VSI_VMDQ2) {
3537 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3538 			qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_VFVM_INDX_MASK,
3539 					      vsi->id);
3540 		} else {
3541 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3542 		}
3543 	}
3544 
3545 	qtx_ctl |= FIELD_PREP(I40E_QTX_CTL_PF_INDX_MASK, hw->pf_id);
3546 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3547 	i40e_flush(hw);
3548 
3549 	/* cache tail off for easier writes later */
3550 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3551 
3552 	return 0;
3553 }
3554 
3555 /**
3556  * i40e_rx_offset - Return expected offset into page to access data
3557  * @rx_ring: Ring we are requesting offset of
3558  *
3559  * Returns the offset value for ring into the data buffer.
3560  */
3561 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3562 {
3563 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3564 }
3565 
3566 /**
3567  * i40e_configure_rx_ring - Configure a receive ring context
3568  * @ring: The Rx ring to configure
3569  *
3570  * Configure the Rx descriptor ring in the HMC context.
3571  **/
3572 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3573 {
3574 	struct i40e_vsi *vsi = ring->vsi;
3575 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3576 	u16 pf_q = vsi->base_queue + ring->queue_index;
3577 	struct i40e_hw *hw = &vsi->back->hw;
3578 	struct i40e_hmc_obj_rxq rx_ctx;
3579 	u32 xdp_frame_sz;
3580 	int err = 0;
3581 	bool ok;
3582 
3583 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3584 
3585 	/* clear the context structure first */
3586 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3587 
3588 	ring->rx_buf_len = vsi->rx_buf_len;
3589 	xdp_frame_sz = i40e_rx_pg_size(ring) / 2;
3590 
3591 	/* XDP RX-queue info only needed for RX rings exposed to XDP */
3592 	if (ring->vsi->type != I40E_VSI_MAIN)
3593 		goto skip;
3594 
3595 	ring->xsk_pool = i40e_xsk_pool(ring);
3596 	if (ring->xsk_pool) {
3597 		xdp_frame_sz = xsk_pool_get_rx_frag_step(ring->xsk_pool);
3598 		ring->rx_buf_len = xsk_pool_get_rx_frame_size(ring->xsk_pool);
3599 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3600 					 ring->queue_index,
3601 					 ring->q_vector->napi.napi_id,
3602 					 xdp_frame_sz);
3603 		if (err)
3604 			return err;
3605 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3606 						 MEM_TYPE_XSK_BUFF_POOL,
3607 						 NULL);
3608 		if (err)
3609 			goto unreg_xdp;
3610 		dev_info(&vsi->back->pdev->dev,
3611 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3612 			 ring->queue_index);
3613 
3614 	} else {
3615 		err = __xdp_rxq_info_reg(&ring->xdp_rxq, ring->netdev,
3616 					 ring->queue_index,
3617 					 ring->q_vector->napi.napi_id,
3618 					 xdp_frame_sz);
3619 		if (err)
3620 			return err;
3621 		err = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3622 						 MEM_TYPE_PAGE_SHARED,
3623 						 NULL);
3624 		if (err)
3625 			goto unreg_xdp;
3626 	}
3627 
3628 skip:
3629 	xdp_init_buff(&ring->xdp_ctx.xdp, xdp_frame_sz, &ring->xdp_rxq);
3630 
3631 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3632 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3633 
3634 	rx_ctx.base = (ring->dma / 128);
3635 	rx_ctx.qlen = ring->count;
3636 
3637 	/* use 16 byte descriptors */
3638 	rx_ctx.dsize = 0;
3639 
3640 	/* descriptor type is always zero
3641 	 * rx_ctx.dtype = 0;
3642 	 */
3643 	rx_ctx.hsplit_0 = 0;
3644 
3645 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3646 	if (hw->revision_id == 0)
3647 		rx_ctx.lrxqthresh = 0;
3648 	else
3649 		rx_ctx.lrxqthresh = 1;
3650 	rx_ctx.crcstrip = 1;
3651 	rx_ctx.l2tsel = 1;
3652 	/* this controls whether VLAN is stripped from inner headers */
3653 	rx_ctx.showiv = 0;
3654 	/* set the prefena field to 1 because the manual says to */
3655 	rx_ctx.prefena = 1;
3656 
3657 	/* clear the context in the HMC */
3658 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3659 	if (err) {
3660 		dev_info(&vsi->back->pdev->dev,
3661 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3662 			 ring->queue_index, pf_q, err);
3663 		err = -ENOMEM;
3664 		goto unreg_xdp;
3665 	}
3666 
3667 	/* set the context in the HMC */
3668 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3669 	if (err) {
3670 		dev_info(&vsi->back->pdev->dev,
3671 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3672 			 ring->queue_index, pf_q, err);
3673 		err = -ENOMEM;
3674 		goto unreg_xdp;
3675 	}
3676 
3677 	/* configure Rx buffer alignment */
3678 	if (!vsi->netdev || test_bit(I40E_FLAG_LEGACY_RX_ENA, vsi->back->flags)) {
3679 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3680 			dev_info(&vsi->back->pdev->dev,
3681 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3682 			err = -EOPNOTSUPP;
3683 			goto unreg_xdp;
3684 		}
3685 		clear_ring_build_skb_enabled(ring);
3686 	} else {
3687 		set_ring_build_skb_enabled(ring);
3688 	}
3689 
3690 	ring->rx_offset = i40e_rx_offset(ring);
3691 
3692 	/* cache tail for quicker writes, and clear the reg before use */
3693 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3694 	writel(0, ring->tail);
3695 
3696 	if (ring->xsk_pool) {
3697 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3698 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3699 	} else {
3700 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3701 	}
3702 	if (!ok) {
3703 		/* Log this in case the user has forgotten to give the kernel
3704 		 * any buffers, even later in the application.
3705 		 */
3706 		dev_info(&vsi->back->pdev->dev,
3707 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3708 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3709 			 ring->queue_index, pf_q);
3710 	}
3711 
3712 	return 0;
3713 unreg_xdp:
3714 	if (ring->vsi->type == I40E_VSI_MAIN)
3715 		xdp_rxq_info_unreg(&ring->xdp_rxq);
3716 
3717 	return err;
3718 }
3719 
3720 /**
3721  * i40e_vsi_configure_tx - Configure the VSI for Tx
3722  * @vsi: VSI structure describing this set of rings and resources
3723  *
3724  * Configure the Tx VSI for operation.
3725  **/
3726 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3727 {
3728 	int err = 0;
3729 	u16 i;
3730 
3731 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3732 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3733 
3734 	if (err || !i40e_enabled_xdp_vsi(vsi))
3735 		return err;
3736 
3737 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3738 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3739 
3740 	return err;
3741 }
3742 
3743 /**
3744  * i40e_vsi_configure_rx - Configure the VSI for Rx
3745  * @vsi: the VSI being configured
3746  *
3747  * Configure the Rx VSI for operation.
3748  **/
3749 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3750 {
3751 	int err = 0;
3752 	u16 i;
3753 
3754 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3755 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3756 
3757 #if (PAGE_SIZE < 8192)
3758 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3759 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3760 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3761 		vsi->max_frame = vsi->rx_buf_len;
3762 	}
3763 #endif
3764 
3765 	/* set up individual rings */
3766 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3767 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3768 
3769 	return err;
3770 }
3771 
3772 /**
3773  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3774  * @vsi: ptr to the VSI
3775  **/
3776 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3777 {
3778 	struct i40e_ring *tx_ring, *rx_ring;
3779 	u16 qoffset, qcount;
3780 	int i, n;
3781 
3782 	if (!test_bit(I40E_FLAG_DCB_ENA, vsi->back->flags)) {
3783 		/* Reset the TC information */
3784 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3785 			rx_ring = vsi->rx_rings[i];
3786 			tx_ring = vsi->tx_rings[i];
3787 			rx_ring->dcb_tc = 0;
3788 			tx_ring->dcb_tc = 0;
3789 		}
3790 		return;
3791 	}
3792 
3793 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3794 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3795 			continue;
3796 
3797 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3798 		qcount = vsi->tc_config.tc_info[n].qcount;
3799 		for (i = qoffset; i < (qoffset + qcount); i++) {
3800 			rx_ring = vsi->rx_rings[i];
3801 			tx_ring = vsi->tx_rings[i];
3802 			rx_ring->dcb_tc = n;
3803 			tx_ring->dcb_tc = n;
3804 		}
3805 	}
3806 }
3807 
3808 /**
3809  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3810  * @vsi: ptr to the VSI
3811  **/
3812 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3813 {
3814 	if (vsi->netdev)
3815 		i40e_set_rx_mode(vsi->netdev);
3816 }
3817 
3818 /**
3819  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3820  * @pf: Pointer to the targeted PF
3821  *
3822  * Set all flow director counters to 0.
3823  */
3824 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3825 {
3826 	pf->fd_tcp4_filter_cnt = 0;
3827 	pf->fd_udp4_filter_cnt = 0;
3828 	pf->fd_sctp4_filter_cnt = 0;
3829 	pf->fd_ip4_filter_cnt = 0;
3830 	pf->fd_tcp6_filter_cnt = 0;
3831 	pf->fd_udp6_filter_cnt = 0;
3832 	pf->fd_sctp6_filter_cnt = 0;
3833 	pf->fd_ip6_filter_cnt = 0;
3834 }
3835 
3836 /**
3837  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3838  * @vsi: Pointer to the targeted VSI
3839  *
3840  * This function replays the hlist on the hw where all the SB Flow Director
3841  * filters were saved.
3842  **/
3843 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3844 {
3845 	struct i40e_fdir_filter *filter;
3846 	struct i40e_pf *pf = vsi->back;
3847 	struct hlist_node *node;
3848 
3849 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
3850 		return;
3851 
3852 	/* Reset FDir counters as we're replaying all existing filters */
3853 	i40e_reset_fdir_filter_cnt(pf);
3854 
3855 	hlist_for_each_entry_safe(filter, node,
3856 				  &pf->fdir_filter_list, fdir_node) {
3857 		i40e_add_del_fdir(vsi, filter, true);
3858 	}
3859 }
3860 
3861 /**
3862  * i40e_vsi_configure - Set up the VSI for action
3863  * @vsi: the VSI being configured
3864  **/
3865 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3866 {
3867 	int err;
3868 
3869 	i40e_set_vsi_rx_mode(vsi);
3870 	i40e_restore_vlan(vsi);
3871 	i40e_vsi_config_dcb_rings(vsi);
3872 	err = i40e_vsi_configure_tx(vsi);
3873 	if (!err)
3874 		err = i40e_vsi_configure_rx(vsi);
3875 
3876 	return err;
3877 }
3878 
3879 /**
3880  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3881  * @vsi: the VSI being configured
3882  **/
3883 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3884 {
3885 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3886 	struct i40e_pf *pf = vsi->back;
3887 	struct i40e_hw *hw = &pf->hw;
3888 	u16 vector;
3889 	int i, q;
3890 	u32 qp;
3891 
3892 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3893 	 * and PFINT_LNKLSTn registers, e.g.:
3894 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3895 	 */
3896 	qp = vsi->base_queue;
3897 	vector = vsi->base_vector;
3898 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3899 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3900 
3901 		q_vector->rx.next_update = jiffies + 1;
3902 		q_vector->rx.target_itr =
3903 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3904 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3905 		     q_vector->rx.target_itr >> 1);
3906 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3907 
3908 		q_vector->tx.next_update = jiffies + 1;
3909 		q_vector->tx.target_itr =
3910 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3911 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3912 		     q_vector->tx.target_itr >> 1);
3913 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3914 
3915 		/* Set ITR for software interrupts triggered after exiting
3916 		 * busy-loop polling.
3917 		 */
3918 		wr32(hw, I40E_PFINT_ITRN(I40E_SW_ITR, vector - 1),
3919 		     I40E_ITR_20K);
3920 
3921 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3922 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3923 
3924 		/* begin of linked list for RX queue assigned to this vector */
3925 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3926 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3927 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3928 			u32 val;
3929 
3930 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3931 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3932 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3933 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3934 			      (I40E_QUEUE_TYPE_TX <<
3935 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3936 
3937 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3938 
3939 			if (has_xdp) {
3940 				/* TX queue with next queue set to TX */
3941 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3942 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3943 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3944 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3945 				      (I40E_QUEUE_TYPE_TX <<
3946 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3947 
3948 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3949 			}
3950 			/* TX queue with next RX or end of linked list */
3951 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3952 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3953 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3954 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3955 			      (I40E_QUEUE_TYPE_RX <<
3956 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3957 
3958 			/* Terminate the linked list */
3959 			if (q == (q_vector->num_ringpairs - 1))
3960 				val |= (I40E_QUEUE_END_OF_LIST <<
3961 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3962 
3963 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3964 			qp++;
3965 		}
3966 	}
3967 
3968 	i40e_flush(hw);
3969 }
3970 
3971 /**
3972  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3973  * @pf: pointer to private device data structure
3974  **/
3975 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3976 {
3977 	struct i40e_hw *hw = &pf->hw;
3978 	u32 val;
3979 
3980 	/* clear things first */
3981 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3982 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3983 
3984 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3985 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3986 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3987 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3988 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3989 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3990 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3991 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3992 
3993 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
3994 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3995 
3996 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
3997 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3998 
3999 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
4000 
4001 	/* SW_ITR_IDX = 0, but don't change INTENA */
4002 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
4003 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
4004 
4005 	/* OTHER_ITR_IDX = 0 */
4006 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
4007 }
4008 
4009 /**
4010  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
4011  * @vsi: the VSI being configured
4012  **/
4013 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
4014 {
4015 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
4016 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4017 	struct i40e_pf *pf = vsi->back;
4018 	struct i40e_hw *hw = &pf->hw;
4019 
4020 	/* set the ITR configuration */
4021 	q_vector->rx.next_update = jiffies + 1;
4022 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
4023 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
4024 	q_vector->rx.current_itr = q_vector->rx.target_itr;
4025 	q_vector->tx.next_update = jiffies + 1;
4026 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
4027 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
4028 	q_vector->tx.current_itr = q_vector->tx.target_itr;
4029 
4030 	i40e_enable_misc_int_causes(pf);
4031 
4032 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4033 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4034 
4035 	/* Associate the queue pair to the vector and enable the queue
4036 	 * interrupt RX queue in linked list with next queue set to TX
4037 	 */
4038 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4039 
4040 	if (i40e_enabled_xdp_vsi(vsi)) {
4041 		/* TX queue in linked list with next queue set to TX */
4042 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4043 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4044 	}
4045 
4046 	/* last TX queue so the next RX queue doesn't matter */
4047 	wr32(hw, I40E_QINT_TQCTL(0),
4048 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4049 	i40e_flush(hw);
4050 }
4051 
4052 /**
4053  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4054  * @pf: board private structure
4055  **/
4056 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4057 {
4058 	struct i40e_hw *hw = &pf->hw;
4059 
4060 	wr32(hw, I40E_PFINT_DYN_CTL0,
4061 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4062 	i40e_flush(hw);
4063 }
4064 
4065 /**
4066  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4067  * @pf: board private structure
4068  **/
4069 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4070 {
4071 	struct i40e_hw *hw = &pf->hw;
4072 	u32 val;
4073 
4074 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4075 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4076 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4077 
4078 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4079 	i40e_flush(hw);
4080 }
4081 
4082 /**
4083  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4084  * @irq: interrupt number
4085  * @data: pointer to a q_vector
4086  **/
4087 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4088 {
4089 	struct i40e_q_vector *q_vector = data;
4090 
4091 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4092 		return IRQ_HANDLED;
4093 
4094 	napi_schedule_irqoff(&q_vector->napi);
4095 
4096 	return IRQ_HANDLED;
4097 }
4098 
4099 /**
4100  * i40e_irq_affinity_notify - Callback for affinity changes
4101  * @notify: context as to what irq was changed
4102  * @mask: the new affinity mask
4103  *
4104  * This is a callback function used by the irq_set_affinity_notifier function
4105  * so that we may register to receive changes to the irq affinity masks.
4106  **/
4107 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4108 				     const cpumask_t *mask)
4109 {
4110 	struct i40e_q_vector *q_vector =
4111 		container_of(notify, struct i40e_q_vector, affinity_notify);
4112 
4113 	cpumask_copy(&q_vector->affinity_mask, mask);
4114 }
4115 
4116 /**
4117  * i40e_irq_affinity_release - Callback for affinity notifier release
4118  * @ref: internal core kernel usage
4119  *
4120  * This is a callback function used by the irq_set_affinity_notifier function
4121  * to inform the current notification subscriber that they will no longer
4122  * receive notifications.
4123  **/
4124 static void i40e_irq_affinity_release(struct kref *ref) {}
4125 
4126 /**
4127  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4128  * @vsi: the VSI being configured
4129  * @basename: name for the vector
4130  *
4131  * Allocates MSI-X vectors and requests interrupts from the kernel.
4132  **/
4133 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4134 {
4135 	int q_vectors = vsi->num_q_vectors;
4136 	struct i40e_pf *pf = vsi->back;
4137 	int base = vsi->base_vector;
4138 	int rx_int_idx = 0;
4139 	int tx_int_idx = 0;
4140 	int vector, err;
4141 	int irq_num;
4142 	int cpu;
4143 
4144 	for (vector = 0; vector < q_vectors; vector++) {
4145 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4146 
4147 		irq_num = pf->msix_entries[base + vector].vector;
4148 
4149 		if (q_vector->tx.ring && q_vector->rx.ring) {
4150 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4151 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4152 			tx_int_idx++;
4153 		} else if (q_vector->rx.ring) {
4154 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4155 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4156 		} else if (q_vector->tx.ring) {
4157 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4158 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4159 		} else {
4160 			/* skip this unused q_vector */
4161 			continue;
4162 		}
4163 		err = request_irq(irq_num,
4164 				  vsi->irq_handler,
4165 				  0,
4166 				  q_vector->name,
4167 				  q_vector);
4168 		if (err) {
4169 			dev_info(&pf->pdev->dev,
4170 				 "MSIX request_irq failed, error: %d\n", err);
4171 			goto free_queue_irqs;
4172 		}
4173 
4174 		/* register for affinity change notifications */
4175 		q_vector->irq_num = irq_num;
4176 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4177 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4178 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4179 		/* Spread affinity hints out across online CPUs.
4180 		 *
4181 		 * get_cpu_mask returns a static constant mask with
4182 		 * a permanent lifetime so it's ok to pass to
4183 		 * irq_update_affinity_hint without making a copy.
4184 		 */
4185 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4186 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4187 	}
4188 
4189 	vsi->irqs_ready = true;
4190 	return 0;
4191 
4192 free_queue_irqs:
4193 	while (vector) {
4194 		vector--;
4195 		irq_num = pf->msix_entries[base + vector].vector;
4196 		irq_set_affinity_notifier(irq_num, NULL);
4197 		irq_update_affinity_hint(irq_num, NULL);
4198 		free_irq(irq_num, vsi->q_vectors[vector]);
4199 	}
4200 	return err;
4201 }
4202 
4203 /**
4204  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4205  * @vsi: the VSI being un-configured
4206  **/
4207 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4208 {
4209 	struct i40e_pf *pf = vsi->back;
4210 	struct i40e_hw *hw = &pf->hw;
4211 	int base = vsi->base_vector;
4212 	int i;
4213 
4214 	/* disable interrupt causation from each queue */
4215 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4216 		u32 val;
4217 
4218 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4219 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4220 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4221 
4222 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4223 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4224 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4225 
4226 		if (!i40e_enabled_xdp_vsi(vsi))
4227 			continue;
4228 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4229 	}
4230 
4231 	/* disable each interrupt */
4232 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4233 		for (i = vsi->base_vector;
4234 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4235 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4236 
4237 		i40e_flush(hw);
4238 		for (i = 0; i < vsi->num_q_vectors; i++)
4239 			synchronize_irq(pf->msix_entries[i + base].vector);
4240 	} else {
4241 		/* Legacy and MSI mode - this stops all interrupt handling */
4242 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4243 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4244 		i40e_flush(hw);
4245 		synchronize_irq(pf->pdev->irq);
4246 	}
4247 }
4248 
4249 /**
4250  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4251  * @vsi: the VSI being configured
4252  **/
4253 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4254 {
4255 	struct i40e_pf *pf = vsi->back;
4256 	int i;
4257 
4258 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4259 		for (i = 0; i < vsi->num_q_vectors; i++)
4260 			i40e_irq_dynamic_enable(vsi, i);
4261 	} else {
4262 		i40e_irq_dynamic_enable_icr0(pf);
4263 	}
4264 
4265 	i40e_flush(&pf->hw);
4266 	return 0;
4267 }
4268 
4269 /**
4270  * i40e_free_misc_vector - Free the vector that handles non-queue events
4271  * @pf: board private structure
4272  **/
4273 static void i40e_free_misc_vector(struct i40e_pf *pf)
4274 {
4275 	/* Disable ICR 0 */
4276 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4277 	i40e_flush(&pf->hw);
4278 
4279 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags) && pf->msix_entries) {
4280 		free_irq(pf->msix_entries[0].vector, pf);
4281 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4282 	}
4283 }
4284 
4285 /**
4286  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4287  * @irq: interrupt number
4288  * @data: pointer to a q_vector
4289  *
4290  * This is the handler used for all MSI/Legacy interrupts, and deals
4291  * with both queue and non-queue interrupts.  This is also used in
4292  * MSIX mode to handle the non-queue interrupts.
4293  **/
4294 static irqreturn_t i40e_intr(int irq, void *data)
4295 {
4296 	struct i40e_pf *pf = (struct i40e_pf *)data;
4297 	struct i40e_hw *hw = &pf->hw;
4298 	irqreturn_t ret = IRQ_NONE;
4299 	u32 icr0, icr0_remaining;
4300 	u32 val, ena_mask;
4301 
4302 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4303 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4304 
4305 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4306 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4307 		goto enable_intr;
4308 
4309 	/* if interrupt but no bits showing, must be SWINT */
4310 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4311 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4312 		pf->sw_int_count++;
4313 
4314 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
4315 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4316 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4317 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4318 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4319 	}
4320 
4321 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4322 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4323 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
4324 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4325 
4326 		/* We do not have a way to disarm Queue causes while leaving
4327 		 * interrupt enabled for all other causes, ideally
4328 		 * interrupt should be disabled while we are in NAPI but
4329 		 * this is not a performance path and napi_schedule()
4330 		 * can deal with rescheduling.
4331 		 */
4332 		if (!test_bit(__I40E_DOWN, pf->state))
4333 			napi_schedule_irqoff(&q_vector->napi);
4334 	}
4335 
4336 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4337 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4338 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4339 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4340 	}
4341 
4342 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4343 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4344 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4345 	}
4346 
4347 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4348 		/* disable any further VFLR event notifications */
4349 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4350 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4351 
4352 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4353 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4354 		} else {
4355 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4356 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4357 		}
4358 	}
4359 
4360 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4361 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4362 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4363 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4364 		val = rd32(hw, I40E_GLGEN_RSTAT);
4365 		val = FIELD_GET(I40E_GLGEN_RSTAT_RESET_TYPE_MASK, val);
4366 		if (val == I40E_RESET_CORER) {
4367 			pf->corer_count++;
4368 		} else if (val == I40E_RESET_GLOBR) {
4369 			pf->globr_count++;
4370 		} else if (val == I40E_RESET_EMPR) {
4371 			pf->empr_count++;
4372 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4373 		}
4374 	}
4375 
4376 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4377 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4378 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4379 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4380 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4381 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4382 	}
4383 
4384 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4385 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4386 
4387 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4388 			schedule_work(&pf->ptp_extts0_work);
4389 
4390 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4391 			i40e_ptp_tx_hwtstamp(pf);
4392 
4393 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4394 	}
4395 
4396 	/* If a critical error is pending we have no choice but to reset the
4397 	 * device.
4398 	 * Report and mask out any remaining unexpected interrupts.
4399 	 */
4400 	icr0_remaining = icr0 & ena_mask;
4401 	if (icr0_remaining) {
4402 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4403 			 icr0_remaining);
4404 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4405 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4406 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4407 			dev_info(&pf->pdev->dev, "device will be reset\n");
4408 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4409 			i40e_service_event_schedule(pf);
4410 		}
4411 		ena_mask &= ~icr0_remaining;
4412 	}
4413 	ret = IRQ_HANDLED;
4414 
4415 enable_intr:
4416 	/* re-enable interrupt causes */
4417 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4418 	if (!test_bit(__I40E_DOWN, pf->state) ||
4419 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4420 		i40e_service_event_schedule(pf);
4421 		i40e_irq_dynamic_enable_icr0(pf);
4422 	}
4423 
4424 	return ret;
4425 }
4426 
4427 /**
4428  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4429  * @tx_ring:  tx ring to clean
4430  * @budget:   how many cleans we're allowed
4431  *
4432  * Returns true if there's any budget left (e.g. the clean is finished)
4433  **/
4434 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4435 {
4436 	struct i40e_vsi *vsi = tx_ring->vsi;
4437 	u16 i = tx_ring->next_to_clean;
4438 	struct i40e_tx_buffer *tx_buf;
4439 	struct i40e_tx_desc *tx_desc;
4440 
4441 	tx_buf = &tx_ring->tx_bi[i];
4442 	tx_desc = I40E_TX_DESC(tx_ring, i);
4443 	i -= tx_ring->count;
4444 
4445 	do {
4446 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4447 
4448 		/* if next_to_watch is not set then there is no work pending */
4449 		if (!eop_desc)
4450 			break;
4451 
4452 		/* prevent any other reads prior to eop_desc */
4453 		smp_rmb();
4454 
4455 		/* if the descriptor isn't done, no work yet to do */
4456 		if (!(eop_desc->cmd_type_offset_bsz &
4457 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4458 			break;
4459 
4460 		/* clear next_to_watch to prevent false hangs */
4461 		tx_buf->next_to_watch = NULL;
4462 
4463 		tx_desc->buffer_addr = 0;
4464 		tx_desc->cmd_type_offset_bsz = 0;
4465 		/* move past filter desc */
4466 		tx_buf++;
4467 		tx_desc++;
4468 		i++;
4469 		if (unlikely(!i)) {
4470 			i -= tx_ring->count;
4471 			tx_buf = tx_ring->tx_bi;
4472 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4473 		}
4474 		/* unmap skb header data */
4475 		dma_unmap_single(tx_ring->dev,
4476 				 dma_unmap_addr(tx_buf, dma),
4477 				 dma_unmap_len(tx_buf, len),
4478 				 DMA_TO_DEVICE);
4479 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4480 			kfree(tx_buf->raw_buf);
4481 
4482 		tx_buf->raw_buf = NULL;
4483 		tx_buf->tx_flags = 0;
4484 		tx_buf->next_to_watch = NULL;
4485 		dma_unmap_len_set(tx_buf, len, 0);
4486 		tx_desc->buffer_addr = 0;
4487 		tx_desc->cmd_type_offset_bsz = 0;
4488 
4489 		/* move us past the eop_desc for start of next FD desc */
4490 		tx_buf++;
4491 		tx_desc++;
4492 		i++;
4493 		if (unlikely(!i)) {
4494 			i -= tx_ring->count;
4495 			tx_buf = tx_ring->tx_bi;
4496 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4497 		}
4498 
4499 		/* update budget accounting */
4500 		budget--;
4501 	} while (likely(budget));
4502 
4503 	i += tx_ring->count;
4504 	tx_ring->next_to_clean = i;
4505 
4506 	if (test_bit(I40E_FLAG_MSIX_ENA, vsi->back->flags))
4507 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4508 
4509 	return budget > 0;
4510 }
4511 
4512 /**
4513  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4514  * @irq: interrupt number
4515  * @data: pointer to a q_vector
4516  **/
4517 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4518 {
4519 	struct i40e_q_vector *q_vector = data;
4520 	struct i40e_vsi *vsi;
4521 
4522 	if (!q_vector->tx.ring)
4523 		return IRQ_HANDLED;
4524 
4525 	vsi = q_vector->tx.ring->vsi;
4526 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4527 
4528 	return IRQ_HANDLED;
4529 }
4530 
4531 /**
4532  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4533  * @vsi: the VSI being configured
4534  * @v_idx: vector index
4535  * @qp_idx: queue pair index
4536  **/
4537 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4538 {
4539 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4540 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4541 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4542 
4543 	tx_ring->q_vector = q_vector;
4544 	tx_ring->next = q_vector->tx.ring;
4545 	q_vector->tx.ring = tx_ring;
4546 	q_vector->tx.count++;
4547 
4548 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4549 	if (i40e_enabled_xdp_vsi(vsi)) {
4550 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4551 
4552 		xdp_ring->q_vector = q_vector;
4553 		xdp_ring->next = q_vector->tx.ring;
4554 		q_vector->tx.ring = xdp_ring;
4555 		q_vector->tx.count++;
4556 	}
4557 
4558 	rx_ring->q_vector = q_vector;
4559 	rx_ring->next = q_vector->rx.ring;
4560 	q_vector->rx.ring = rx_ring;
4561 	q_vector->rx.count++;
4562 }
4563 
4564 /**
4565  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4566  * @vsi: the VSI being configured
4567  *
4568  * This function maps descriptor rings to the queue-specific vectors
4569  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4570  * one vector per queue pair, but on a constrained vector budget, we
4571  * group the queue pairs as "efficiently" as possible.
4572  **/
4573 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4574 {
4575 	int qp_remaining = vsi->num_queue_pairs;
4576 	int q_vectors = vsi->num_q_vectors;
4577 	int num_ringpairs;
4578 	int v_start = 0;
4579 	int qp_idx = 0;
4580 
4581 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4582 	 * group them so there are multiple queues per vector.
4583 	 * It is also important to go through all the vectors available to be
4584 	 * sure that if we don't use all the vectors, that the remaining vectors
4585 	 * are cleared. This is especially important when decreasing the
4586 	 * number of queues in use.
4587 	 */
4588 	for (; v_start < q_vectors; v_start++) {
4589 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4590 
4591 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4592 
4593 		q_vector->num_ringpairs = num_ringpairs;
4594 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4595 
4596 		q_vector->rx.count = 0;
4597 		q_vector->tx.count = 0;
4598 		q_vector->rx.ring = NULL;
4599 		q_vector->tx.ring = NULL;
4600 
4601 		while (num_ringpairs--) {
4602 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4603 			qp_idx++;
4604 			qp_remaining--;
4605 		}
4606 	}
4607 }
4608 
4609 /**
4610  * i40e_vsi_request_irq - Request IRQ from the OS
4611  * @vsi: the VSI being configured
4612  * @basename: name for the vector
4613  **/
4614 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4615 {
4616 	struct i40e_pf *pf = vsi->back;
4617 	int err;
4618 
4619 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
4620 		err = i40e_vsi_request_irq_msix(vsi, basename);
4621 	else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags))
4622 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4623 				  pf->int_name, pf);
4624 	else
4625 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4626 				  pf->int_name, pf);
4627 
4628 	if (err)
4629 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4630 
4631 	return err;
4632 }
4633 
4634 #ifdef CONFIG_NET_POLL_CONTROLLER
4635 /**
4636  * i40e_netpoll - A Polling 'interrupt' handler
4637  * @netdev: network interface device structure
4638  *
4639  * This is used by netconsole to send skbs without having to re-enable
4640  * interrupts.  It's not called while the normal interrupt routine is executing.
4641  **/
4642 static void i40e_netpoll(struct net_device *netdev)
4643 {
4644 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4645 	struct i40e_vsi *vsi = np->vsi;
4646 	struct i40e_pf *pf = vsi->back;
4647 	int i;
4648 
4649 	/* if interface is down do nothing */
4650 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4651 		return;
4652 
4653 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4654 		for (i = 0; i < vsi->num_q_vectors; i++)
4655 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4656 	} else {
4657 		i40e_intr(pf->pdev->irq, netdev);
4658 	}
4659 }
4660 #endif
4661 
4662 #define I40E_QTX_ENA_WAIT_COUNT 50
4663 
4664 /**
4665  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4666  * @pf: the PF being configured
4667  * @pf_q: the PF queue
4668  * @enable: enable or disable state of the queue
4669  *
4670  * This routine will wait for the given Tx queue of the PF to reach the
4671  * enabled or disabled state.
4672  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4673  * multiple retries; else will return 0 in case of success.
4674  **/
4675 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4676 {
4677 	int i;
4678 	u32 tx_reg;
4679 
4680 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4681 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4682 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4683 			break;
4684 
4685 		usleep_range(10, 20);
4686 	}
4687 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4688 		return -ETIMEDOUT;
4689 
4690 	return 0;
4691 }
4692 
4693 /**
4694  * i40e_control_tx_q - Start or stop a particular Tx queue
4695  * @pf: the PF structure
4696  * @pf_q: the PF queue to configure
4697  * @enable: start or stop the queue
4698  *
4699  * This function enables or disables a single queue. Note that any delay
4700  * required after the operation is expected to be handled by the caller of
4701  * this function.
4702  **/
4703 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4704 {
4705 	struct i40e_hw *hw = &pf->hw;
4706 	u32 tx_reg;
4707 	int i;
4708 
4709 	/* warn the TX unit of coming changes */
4710 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4711 	if (!enable)
4712 		usleep_range(10, 20);
4713 
4714 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4715 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4716 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4717 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4718 			break;
4719 		usleep_range(1000, 2000);
4720 	}
4721 
4722 	/* Skip if the queue is already in the requested state */
4723 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4724 		return;
4725 
4726 	/* turn on/off the queue */
4727 	if (enable) {
4728 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4729 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4730 	} else {
4731 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4732 	}
4733 
4734 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4735 }
4736 
4737 /**
4738  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4739  * @seid: VSI SEID
4740  * @pf: the PF structure
4741  * @pf_q: the PF queue to configure
4742  * @is_xdp: true if the queue is used for XDP
4743  * @enable: start or stop the queue
4744  **/
4745 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4746 			   bool is_xdp, bool enable)
4747 {
4748 	int ret;
4749 
4750 	i40e_control_tx_q(pf, pf_q, enable);
4751 
4752 	/* wait for the change to finish */
4753 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4754 	if (ret) {
4755 		dev_info(&pf->pdev->dev,
4756 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4757 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4758 			 (enable ? "en" : "dis"));
4759 	}
4760 
4761 	return ret;
4762 }
4763 
4764 /**
4765  * i40e_vsi_enable_tx - Start a VSI's rings
4766  * @vsi: the VSI being configured
4767  **/
4768 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4769 {
4770 	struct i40e_pf *pf = vsi->back;
4771 	int i, pf_q, ret = 0;
4772 
4773 	pf_q = vsi->base_queue;
4774 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4775 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4776 					     pf_q,
4777 					     false /*is xdp*/, true);
4778 		if (ret)
4779 			break;
4780 
4781 		if (!i40e_enabled_xdp_vsi(vsi))
4782 			continue;
4783 
4784 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4785 					     pf_q + vsi->alloc_queue_pairs,
4786 					     true /*is xdp*/, true);
4787 		if (ret)
4788 			break;
4789 	}
4790 	return ret;
4791 }
4792 
4793 /**
4794  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4795  * @pf: the PF being configured
4796  * @pf_q: the PF queue
4797  * @enable: enable or disable state of the queue
4798  *
4799  * This routine will wait for the given Rx queue of the PF to reach the
4800  * enabled or disabled state.
4801  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4802  * multiple retries; else will return 0 in case of success.
4803  **/
4804 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4805 {
4806 	int i;
4807 	u32 rx_reg;
4808 
4809 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4810 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4811 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4812 			break;
4813 
4814 		usleep_range(10, 20);
4815 	}
4816 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4817 		return -ETIMEDOUT;
4818 
4819 	return 0;
4820 }
4821 
4822 /**
4823  * i40e_control_rx_q - Start or stop a particular Rx queue
4824  * @pf: the PF structure
4825  * @pf_q: the PF queue to configure
4826  * @enable: start or stop the queue
4827  *
4828  * This function enables or disables a single queue. Note that
4829  * any delay required after the operation is expected to be
4830  * handled by the caller of this function.
4831  **/
4832 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4833 {
4834 	struct i40e_hw *hw = &pf->hw;
4835 	u32 rx_reg;
4836 	int i;
4837 
4838 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4839 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4840 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4841 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4842 			break;
4843 		usleep_range(1000, 2000);
4844 	}
4845 
4846 	/* Skip if the queue is already in the requested state */
4847 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4848 		return;
4849 
4850 	/* turn on/off the queue */
4851 	if (enable)
4852 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4853 	else
4854 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4855 
4856 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4857 }
4858 
4859 /**
4860  * i40e_control_wait_rx_q
4861  * @pf: the PF structure
4862  * @pf_q: queue being configured
4863  * @enable: start or stop the rings
4864  *
4865  * This function enables or disables a single queue along with waiting
4866  * for the change to finish. The caller of this function should handle
4867  * the delays needed in the case of disabling queues.
4868  **/
4869 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4870 {
4871 	i40e_control_rx_q(pf, pf_q, enable);
4872 
4873 	/* wait for the change to finish */
4874 	return i40e_pf_rxq_wait(pf, pf_q, enable);
4875 }
4876 
4877 /**
4878  * i40e_vsi_enable_rx - Start a VSI's rings
4879  * @vsi: the VSI being configured
4880  **/
4881 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4882 {
4883 	struct i40e_pf *pf = vsi->back;
4884 	int i, pf_q, ret = 0;
4885 
4886 	pf_q = vsi->base_queue;
4887 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4888 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4889 		if (ret) {
4890 			dev_info(&pf->pdev->dev,
4891 				 "VSI seid %d Rx ring %d enable timeout\n",
4892 				 vsi->seid, pf_q);
4893 			break;
4894 		}
4895 	}
4896 
4897 	return ret;
4898 }
4899 
4900 /**
4901  * i40e_vsi_start_rings - Start a VSI's rings
4902  * @vsi: the VSI being configured
4903  **/
4904 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4905 {
4906 	int ret = 0;
4907 
4908 	/* do rx first for enable and last for disable */
4909 	ret = i40e_vsi_enable_rx(vsi);
4910 	if (ret)
4911 		return ret;
4912 	ret = i40e_vsi_enable_tx(vsi);
4913 
4914 	return ret;
4915 }
4916 
4917 #define I40E_DISABLE_TX_GAP_MSEC	50
4918 
4919 /**
4920  * i40e_vsi_stop_rings - Stop a VSI's rings
4921  * @vsi: the VSI being configured
4922  **/
4923 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4924 {
4925 	struct i40e_pf *pf = vsi->back;
4926 	u32 pf_q, tx_q_end, rx_q_end;
4927 
4928 	/* When port TX is suspended, don't wait */
4929 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4930 		return i40e_vsi_stop_rings_no_wait(vsi);
4931 
4932 	tx_q_end = vsi->base_queue +
4933 		vsi->alloc_queue_pairs * (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
4934 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4935 		i40e_pre_tx_queue_cfg(&pf->hw, pf_q, false);
4936 
4937 	rx_q_end = vsi->base_queue + vsi->num_queue_pairs;
4938 	for (pf_q = vsi->base_queue; pf_q < rx_q_end; pf_q++)
4939 		i40e_control_rx_q(pf, pf_q, false);
4940 
4941 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4942 	for (pf_q = vsi->base_queue; pf_q < tx_q_end; pf_q++)
4943 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4944 
4945 	i40e_vsi_wait_queues_disabled(vsi);
4946 }
4947 
4948 /**
4949  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4950  * @vsi: the VSI being shutdown
4951  *
4952  * This function stops all the rings for a VSI but does not delay to verify
4953  * that rings have been disabled. It is expected that the caller is shutting
4954  * down multiple VSIs at once and will delay together for all the VSIs after
4955  * initiating the shutdown. This is particularly useful for shutting down lots
4956  * of VFs together. Otherwise, a large delay can be incurred while configuring
4957  * each VSI in serial.
4958  **/
4959 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4960 {
4961 	struct i40e_pf *pf = vsi->back;
4962 	int i, pf_q;
4963 
4964 	pf_q = vsi->base_queue;
4965 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4966 		i40e_control_tx_q(pf, pf_q, false);
4967 		i40e_control_rx_q(pf, pf_q, false);
4968 	}
4969 }
4970 
4971 /**
4972  * i40e_vsi_free_irq - Free the irq association with the OS
4973  * @vsi: the VSI being configured
4974  **/
4975 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4976 {
4977 	struct i40e_pf *pf = vsi->back;
4978 	struct i40e_hw *hw = &pf->hw;
4979 	int base = vsi->base_vector;
4980 	u32 val, qp;
4981 	int i;
4982 
4983 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
4984 		if (!vsi->q_vectors)
4985 			return;
4986 
4987 		if (!vsi->irqs_ready)
4988 			return;
4989 
4990 		vsi->irqs_ready = false;
4991 		for (i = 0; i < vsi->num_q_vectors; i++) {
4992 			int irq_num;
4993 			u16 vector;
4994 
4995 			vector = i + base;
4996 			irq_num = pf->msix_entries[vector].vector;
4997 
4998 			/* free only the irqs that were actually requested */
4999 			if (!vsi->q_vectors[i] ||
5000 			    !vsi->q_vectors[i]->num_ringpairs)
5001 				continue;
5002 
5003 			/* clear the affinity notifier in the IRQ descriptor */
5004 			irq_set_affinity_notifier(irq_num, NULL);
5005 			/* remove our suggested affinity mask for this IRQ */
5006 			irq_update_affinity_hint(irq_num, NULL);
5007 			free_irq(irq_num, vsi->q_vectors[i]);
5008 
5009 			/* Tear down the interrupt queue link list
5010 			 *
5011 			 * We know that they come in pairs and always
5012 			 * the Rx first, then the Tx.  To clear the
5013 			 * link list, stick the EOL value into the
5014 			 * next_q field of the registers.
5015 			 */
5016 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
5017 			qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK,
5018 				       val);
5019 			val |= I40E_QUEUE_END_OF_LIST
5020 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5021 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5022 
5023 			while (qp != I40E_QUEUE_END_OF_LIST) {
5024 				u32 next;
5025 
5026 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5027 
5028 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5029 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5030 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5031 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5032 
5033 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5034 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5035 
5036 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5037 
5038 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5039 
5040 				next = FIELD_GET(I40E_QINT_TQCTL_NEXTQ_INDX_MASK,
5041 						 val);
5042 
5043 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5044 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5045 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5046 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5047 
5048 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5049 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5050 
5051 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5052 				qp = next;
5053 			}
5054 		}
5055 	} else {
5056 		free_irq(pf->pdev->irq, pf);
5057 
5058 		val = rd32(hw, I40E_PFINT_LNKLST0);
5059 		qp = FIELD_GET(I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK, val);
5060 		val |= I40E_QUEUE_END_OF_LIST
5061 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5062 		wr32(hw, I40E_PFINT_LNKLST0, val);
5063 
5064 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5065 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5066 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5067 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5068 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5069 
5070 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5071 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5072 
5073 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5074 
5075 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5076 
5077 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5078 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5079 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5080 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5081 
5082 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5083 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5084 
5085 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5086 	}
5087 }
5088 
5089 /**
5090  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5091  * @vsi: the VSI being configured
5092  * @v_idx: Index of vector to be freed
5093  *
5094  * This function frees the memory allocated to the q_vector.  In addition if
5095  * NAPI is enabled it will delete any references to the NAPI struct prior
5096  * to freeing the q_vector.
5097  **/
5098 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5099 {
5100 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5101 	struct i40e_ring *ring;
5102 
5103 	if (!q_vector)
5104 		return;
5105 
5106 	/* disassociate q_vector from rings */
5107 	i40e_for_each_ring(ring, q_vector->tx)
5108 		ring->q_vector = NULL;
5109 
5110 	i40e_for_each_ring(ring, q_vector->rx)
5111 		ring->q_vector = NULL;
5112 
5113 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5114 	if (vsi->netdev)
5115 		netif_napi_del(&q_vector->napi);
5116 
5117 	vsi->q_vectors[v_idx] = NULL;
5118 
5119 	kfree_rcu(q_vector, rcu);
5120 }
5121 
5122 /**
5123  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5124  * @vsi: the VSI being un-configured
5125  *
5126  * This frees the memory allocated to the q_vectors and
5127  * deletes references to the NAPI struct.
5128  **/
5129 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5130 {
5131 	int v_idx;
5132 
5133 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5134 		i40e_free_q_vector(vsi, v_idx);
5135 }
5136 
5137 /**
5138  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5139  * @pf: board private structure
5140  **/
5141 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5142 {
5143 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5144 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
5145 		pci_disable_msix(pf->pdev);
5146 		kfree(pf->msix_entries);
5147 		pf->msix_entries = NULL;
5148 		kfree(pf->irq_pile);
5149 		pf->irq_pile = NULL;
5150 	} else if (test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
5151 		pci_disable_msi(pf->pdev);
5152 	}
5153 	clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
5154 	clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
5155 }
5156 
5157 /**
5158  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5159  * @pf: board private structure
5160  *
5161  * We go through and clear interrupt specific resources and reset the structure
5162  * to pre-load conditions
5163  **/
5164 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5165 {
5166 	struct i40e_vsi *vsi;
5167 	int i;
5168 
5169 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5170 		i40e_free_misc_vector(pf);
5171 
5172 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5173 		      I40E_IWARP_IRQ_PILE_ID);
5174 
5175 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5176 
5177 	i40e_pf_for_each_vsi(pf, i, vsi)
5178 		i40e_vsi_free_q_vectors(vsi);
5179 
5180 	i40e_reset_interrupt_capability(pf);
5181 }
5182 
5183 /**
5184  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5185  * @vsi: the VSI being configured
5186  **/
5187 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5188 {
5189 	int q_idx;
5190 
5191 	if (!vsi->netdev)
5192 		return;
5193 
5194 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5195 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5196 
5197 		if (q_vector->rx.ring || q_vector->tx.ring)
5198 			napi_enable(&q_vector->napi);
5199 	}
5200 }
5201 
5202 /**
5203  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5204  * @vsi: the VSI being configured
5205  **/
5206 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5207 {
5208 	int q_idx;
5209 
5210 	if (!vsi->netdev)
5211 		return;
5212 
5213 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5214 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5215 
5216 		if (q_vector->rx.ring || q_vector->tx.ring)
5217 			napi_disable(&q_vector->napi);
5218 	}
5219 }
5220 
5221 /**
5222  * i40e_vsi_close - Shut down a VSI
5223  * @vsi: the vsi to be quelled
5224  **/
5225 static void i40e_vsi_close(struct i40e_vsi *vsi)
5226 {
5227 	struct i40e_pf *pf = vsi->back;
5228 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5229 		i40e_down(vsi);
5230 	i40e_vsi_free_irq(vsi);
5231 	i40e_vsi_free_tx_resources(vsi);
5232 	i40e_vsi_free_rx_resources(vsi);
5233 	vsi->current_netdev_flags = 0;
5234 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5235 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5236 		set_bit(__I40E_CLIENT_RESET, pf->state);
5237 }
5238 
5239 /**
5240  * i40e_quiesce_vsi - Pause a given VSI
5241  * @vsi: the VSI being paused
5242  **/
5243 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5244 {
5245 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5246 		return;
5247 
5248 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5249 	if (vsi->netdev && netif_running(vsi->netdev))
5250 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5251 	else
5252 		i40e_vsi_close(vsi);
5253 }
5254 
5255 /**
5256  * i40e_unquiesce_vsi - Resume a given VSI
5257  * @vsi: the VSI being resumed
5258  **/
5259 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5260 {
5261 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5262 		return;
5263 
5264 	if (vsi->netdev && netif_running(vsi->netdev))
5265 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5266 	else
5267 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5268 }
5269 
5270 /**
5271  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5272  * @pf: the PF
5273  **/
5274 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5275 {
5276 	struct i40e_vsi *vsi;
5277 	int v;
5278 
5279 	i40e_pf_for_each_vsi(pf, v, vsi)
5280 		i40e_quiesce_vsi(vsi);
5281 }
5282 
5283 /**
5284  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5285  * @pf: the PF
5286  **/
5287 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5288 {
5289 	struct i40e_vsi *vsi;
5290 	int v;
5291 
5292 	i40e_pf_for_each_vsi(pf, v, vsi)
5293 		i40e_unquiesce_vsi(vsi);
5294 }
5295 
5296 /**
5297  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5298  * @vsi: the VSI being configured
5299  *
5300  * Wait until all queues on a given VSI have been disabled.
5301  **/
5302 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5303 {
5304 	struct i40e_pf *pf = vsi->back;
5305 	int i, pf_q, ret;
5306 
5307 	pf_q = vsi->base_queue;
5308 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5309 		/* Check and wait for the Tx queue */
5310 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5311 		if (ret) {
5312 			dev_info(&pf->pdev->dev,
5313 				 "VSI seid %d Tx ring %d disable timeout\n",
5314 				 vsi->seid, pf_q);
5315 			return ret;
5316 		}
5317 
5318 		if (!i40e_enabled_xdp_vsi(vsi))
5319 			goto wait_rx;
5320 
5321 		/* Check and wait for the XDP Tx queue */
5322 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5323 				       false);
5324 		if (ret) {
5325 			dev_info(&pf->pdev->dev,
5326 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5327 				 vsi->seid, pf_q);
5328 			return ret;
5329 		}
5330 wait_rx:
5331 		/* Check and wait for the Rx queue */
5332 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5333 		if (ret) {
5334 			dev_info(&pf->pdev->dev,
5335 				 "VSI seid %d Rx ring %d disable timeout\n",
5336 				 vsi->seid, pf_q);
5337 			return ret;
5338 		}
5339 	}
5340 
5341 	return 0;
5342 }
5343 
5344 #ifdef CONFIG_I40E_DCB
5345 /**
5346  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5347  * @pf: the PF
5348  *
5349  * This function waits for the queues to be in disabled state for all the
5350  * VSIs that are managed by this PF.
5351  **/
5352 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5353 {
5354 	struct i40e_vsi *vsi;
5355 	int v, ret = 0;
5356 
5357 	i40e_pf_for_each_vsi(pf, v, vsi) {
5358 		ret = i40e_vsi_wait_queues_disabled(vsi);
5359 		if (ret)
5360 			break;
5361 	}
5362 
5363 	return ret;
5364 }
5365 
5366 #endif
5367 
5368 /**
5369  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5370  * @pf: pointer to PF
5371  *
5372  * Get TC map for ISCSI PF type that will include iSCSI TC
5373  * and LAN TC.
5374  **/
5375 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5376 {
5377 	struct i40e_dcb_app_priority_table app;
5378 	struct i40e_hw *hw = &pf->hw;
5379 	u8 enabled_tc = 1; /* TC0 is always enabled */
5380 	u8 tc, i;
5381 	/* Get the iSCSI APP TLV */
5382 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5383 
5384 	for (i = 0; i < dcbcfg->numapps; i++) {
5385 		app = dcbcfg->app[i];
5386 		if (app.selector == I40E_APP_SEL_TCPIP &&
5387 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5388 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5389 			enabled_tc |= BIT(tc);
5390 			break;
5391 		}
5392 	}
5393 
5394 	return enabled_tc;
5395 }
5396 
5397 /**
5398  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5399  * @dcbcfg: the corresponding DCBx configuration structure
5400  *
5401  * Return the number of TCs from given DCBx configuration
5402  **/
5403 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5404 {
5405 	int i, tc_unused = 0;
5406 	u8 num_tc = 0;
5407 	u8 ret = 0;
5408 
5409 	/* Scan the ETS Config Priority Table to find
5410 	 * traffic class enabled for a given priority
5411 	 * and create a bitmask of enabled TCs
5412 	 */
5413 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5414 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5415 
5416 	/* Now scan the bitmask to check for
5417 	 * contiguous TCs starting with TC0
5418 	 */
5419 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5420 		if (num_tc & BIT(i)) {
5421 			if (!tc_unused) {
5422 				ret++;
5423 			} else {
5424 				pr_err("Non-contiguous TC - Disabling DCB\n");
5425 				return 1;
5426 			}
5427 		} else {
5428 			tc_unused = 1;
5429 		}
5430 	}
5431 
5432 	/* There is always at least TC0 */
5433 	if (!ret)
5434 		ret = 1;
5435 
5436 	return ret;
5437 }
5438 
5439 /**
5440  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5441  * @dcbcfg: the corresponding DCBx configuration structure
5442  *
5443  * Query the current DCB configuration and return the number of
5444  * traffic classes enabled from the given DCBX config
5445  **/
5446 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5447 {
5448 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5449 	u8 enabled_tc = 1;
5450 	u8 i;
5451 
5452 	for (i = 0; i < num_tc; i++)
5453 		enabled_tc |= BIT(i);
5454 
5455 	return enabled_tc;
5456 }
5457 
5458 /**
5459  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5460  * @pf: PF being queried
5461  *
5462  * Query the current MQPRIO configuration and return the number of
5463  * traffic classes enabled.
5464  **/
5465 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5466 {
5467 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5468 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5469 	u8 enabled_tc = 1, i;
5470 
5471 	for (i = 1; i < num_tc; i++)
5472 		enabled_tc |= BIT(i);
5473 	return enabled_tc;
5474 }
5475 
5476 /**
5477  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5478  * @pf: PF being queried
5479  *
5480  * Return number of traffic classes enabled for the given PF
5481  **/
5482 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5483 {
5484 	u8 i, enabled_tc = 1;
5485 	u8 num_tc = 0;
5486 
5487 	if (i40e_is_tc_mqprio_enabled(pf)) {
5488 		struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
5489 
5490 		return vsi->mqprio_qopt.qopt.num_tc;
5491 	}
5492 
5493 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5494 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5495 		return 1;
5496 
5497 	/* SFP mode will be enabled for all TCs on port */
5498 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5499 		return i40e_dcb_get_num_tc(&pf->hw.local_dcbx_config);
5500 
5501 	/* MFP mode return count of enabled TCs for this PF */
5502 	if (pf->hw.func_caps.iscsi)
5503 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5504 	else
5505 		return 1; /* Only TC0 */
5506 
5507 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5508 		if (enabled_tc & BIT(i))
5509 			num_tc++;
5510 	}
5511 	return num_tc;
5512 }
5513 
5514 /**
5515  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5516  * @pf: PF being queried
5517  *
5518  * Return a bitmap for enabled traffic classes for this PF.
5519  **/
5520 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5521 {
5522 	if (i40e_is_tc_mqprio_enabled(pf))
5523 		return i40e_mqprio_get_enabled_tc(pf);
5524 
5525 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5526 	 * default TC
5527 	 */
5528 	if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags))
5529 		return I40E_DEFAULT_TRAFFIC_CLASS;
5530 
5531 	/* SFP mode we want PF to be enabled for all TCs */
5532 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
5533 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5534 
5535 	/* MFP enabled and iSCSI PF type */
5536 	if (pf->hw.func_caps.iscsi)
5537 		return i40e_get_iscsi_tc_map(pf);
5538 	else
5539 		return I40E_DEFAULT_TRAFFIC_CLASS;
5540 }
5541 
5542 /**
5543  * i40e_vsi_get_bw_info - Query VSI BW Information
5544  * @vsi: the VSI being queried
5545  *
5546  * Returns 0 on success, negative value on failure
5547  **/
5548 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5549 {
5550 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5551 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5552 	struct i40e_pf *pf = vsi->back;
5553 	struct i40e_hw *hw = &pf->hw;
5554 	u32 tc_bw_max;
5555 	int ret;
5556 	int i;
5557 
5558 	/* Get the VSI level BW configuration */
5559 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5560 	if (ret) {
5561 		dev_info(&pf->pdev->dev,
5562 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5563 			 ERR_PTR(ret),
5564 			 libie_aq_str(pf->hw.aq.asq_last_status));
5565 		return -EINVAL;
5566 	}
5567 
5568 	/* Get the VSI level BW configuration per TC */
5569 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5570 					       NULL);
5571 	if (ret) {
5572 		dev_info(&pf->pdev->dev,
5573 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5574 			 ERR_PTR(ret),
5575 			 libie_aq_str(pf->hw.aq.asq_last_status));
5576 		return -EINVAL;
5577 	}
5578 
5579 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5580 		dev_info(&pf->pdev->dev,
5581 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5582 			 bw_config.tc_valid_bits,
5583 			 bw_ets_config.tc_valid_bits);
5584 		/* Still continuing */
5585 	}
5586 
5587 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5588 	vsi->bw_max_quanta = bw_config.max_bw;
5589 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5590 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5591 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5592 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5593 		vsi->bw_ets_limit_credits[i] =
5594 					le16_to_cpu(bw_ets_config.credits[i]);
5595 		/* 3 bits out of 4 for each TC */
5596 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5597 	}
5598 
5599 	return 0;
5600 }
5601 
5602 /**
5603  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5604  * @vsi: the VSI being configured
5605  * @enabled_tc: TC bitmap
5606  * @bw_share: BW shared credits per TC
5607  *
5608  * Returns 0 on success, negative value on failure
5609  **/
5610 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5611 				       u8 *bw_share)
5612 {
5613 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5614 	struct i40e_pf *pf = vsi->back;
5615 	int ret;
5616 	int i;
5617 
5618 	/* There is no need to reset BW when mqprio mode is on.  */
5619 	if (i40e_is_tc_mqprio_enabled(pf))
5620 		return 0;
5621 	if (!vsi->mqprio_qopt.qopt.hw && !test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
5622 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5623 		if (ret)
5624 			dev_info(&pf->pdev->dev,
5625 				 "Failed to reset tx rate for vsi->seid %u\n",
5626 				 vsi->seid);
5627 		return ret;
5628 	}
5629 	memset(&bw_data, 0, sizeof(bw_data));
5630 	bw_data.tc_valid_bits = enabled_tc;
5631 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5632 		bw_data.tc_bw_credits[i] = bw_share[i];
5633 
5634 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5635 	if (ret) {
5636 		dev_info(&pf->pdev->dev,
5637 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5638 			 pf->hw.aq.asq_last_status);
5639 		return -EINVAL;
5640 	}
5641 
5642 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5643 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5644 
5645 	return 0;
5646 }
5647 
5648 /**
5649  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5650  * @vsi: the VSI being configured
5651  * @enabled_tc: TC map to be enabled
5652  *
5653  **/
5654 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5655 {
5656 	struct net_device *netdev = vsi->netdev;
5657 	struct i40e_pf *pf = vsi->back;
5658 	struct i40e_hw *hw = &pf->hw;
5659 	u8 netdev_tc = 0;
5660 	int i;
5661 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5662 
5663 	if (!netdev)
5664 		return;
5665 
5666 	if (!enabled_tc) {
5667 		netdev_reset_tc(netdev);
5668 		return;
5669 	}
5670 
5671 	/* Set up actual enabled TCs on the VSI */
5672 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5673 		return;
5674 
5675 	/* set per TC queues for the VSI */
5676 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5677 		/* Only set TC queues for enabled tcs
5678 		 *
5679 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5680 		 * enabled_tc bitmap would be 0x00001001; the driver
5681 		 * will set the numtc for netdev as 2 that will be
5682 		 * referenced by the netdev layer as TC 0 and 1.
5683 		 */
5684 		if (vsi->tc_config.enabled_tc & BIT(i))
5685 			netdev_set_tc_queue(netdev,
5686 					vsi->tc_config.tc_info[i].netdev_tc,
5687 					vsi->tc_config.tc_info[i].qcount,
5688 					vsi->tc_config.tc_info[i].qoffset);
5689 	}
5690 
5691 	if (i40e_is_tc_mqprio_enabled(pf))
5692 		return;
5693 
5694 	/* Assign UP2TC map for the VSI */
5695 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5696 		/* Get the actual TC# for the UP */
5697 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5698 		/* Get the mapped netdev TC# for the UP */
5699 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5700 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5701 	}
5702 }
5703 
5704 /**
5705  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5706  * @vsi: the VSI being configured
5707  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5708  **/
5709 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5710 				      struct i40e_vsi_context *ctxt)
5711 {
5712 	/* copy just the sections touched not the entire info
5713 	 * since not all sections are valid as returned by
5714 	 * update vsi params
5715 	 */
5716 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5717 	memcpy(&vsi->info.queue_mapping,
5718 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5719 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5720 	       sizeof(vsi->info.tc_mapping));
5721 }
5722 
5723 /**
5724  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5725  * @vsi: the VSI being reconfigured
5726  * @vsi_offset: offset from main VF VSI
5727  */
5728 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5729 {
5730 	struct i40e_vsi_context ctxt = {};
5731 	struct i40e_pf *pf;
5732 	struct i40e_hw *hw;
5733 	int ret;
5734 
5735 	if (!vsi)
5736 		return -EINVAL;
5737 	pf = vsi->back;
5738 	hw = &pf->hw;
5739 
5740 	ctxt.seid = vsi->seid;
5741 	ctxt.pf_num = hw->pf_id;
5742 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5743 	ctxt.uplink_seid = vsi->uplink_seid;
5744 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5745 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5746 	ctxt.info = vsi->info;
5747 
5748 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5749 				 false);
5750 	if (vsi->reconfig_rss) {
5751 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5752 				      vsi->num_queue_pairs);
5753 		ret = i40e_vsi_config_rss(vsi);
5754 		if (ret) {
5755 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5756 			return ret;
5757 		}
5758 		vsi->reconfig_rss = false;
5759 	}
5760 
5761 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5762 	if (ret) {
5763 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5764 			 ERR_PTR(ret),
5765 			 libie_aq_str(hw->aq.asq_last_status));
5766 		return ret;
5767 	}
5768 	/* update the local VSI info with updated queue map */
5769 	i40e_vsi_update_queue_map(vsi, &ctxt);
5770 	vsi->info.valid_sections = 0;
5771 
5772 	return ret;
5773 }
5774 
5775 /**
5776  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5777  * @vsi: VSI to be configured
5778  * @enabled_tc: TC bitmap
5779  *
5780  * This configures a particular VSI for TCs that are mapped to the
5781  * given TC bitmap. It uses default bandwidth share for TCs across
5782  * VSIs to configure TC for a particular VSI.
5783  *
5784  * NOTE:
5785  * It is expected that the VSI queues have been quisced before calling
5786  * this function.
5787  **/
5788 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5789 {
5790 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5791 	struct i40e_pf *pf = vsi->back;
5792 	struct i40e_hw *hw = &pf->hw;
5793 	struct i40e_vsi_context ctxt;
5794 	int ret = 0;
5795 	int i;
5796 
5797 	/* Check if enabled_tc is same as existing or new TCs */
5798 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5799 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5800 		return ret;
5801 
5802 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5803 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5804 		if (enabled_tc & BIT(i))
5805 			bw_share[i] = 1;
5806 	}
5807 
5808 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5809 	if (ret) {
5810 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5811 
5812 		dev_info(&pf->pdev->dev,
5813 			 "Failed configuring TC map %d for VSI %d\n",
5814 			 enabled_tc, vsi->seid);
5815 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5816 						  &bw_config, NULL);
5817 		if (ret) {
5818 			dev_info(&pf->pdev->dev,
5819 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5820 				 ERR_PTR(ret),
5821 				 libie_aq_str(hw->aq.asq_last_status));
5822 			goto out;
5823 		}
5824 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5825 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5826 
5827 			if (!valid_tc)
5828 				valid_tc = bw_config.tc_valid_bits;
5829 			/* Always enable TC0, no matter what */
5830 			valid_tc |= 1;
5831 			dev_info(&pf->pdev->dev,
5832 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5833 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5834 			enabled_tc = valid_tc;
5835 		}
5836 
5837 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5838 		if (ret) {
5839 			dev_err(&pf->pdev->dev,
5840 				"Unable to  configure TC map %d for VSI %d\n",
5841 				enabled_tc, vsi->seid);
5842 			goto out;
5843 		}
5844 	}
5845 
5846 	/* Update Queue Pairs Mapping for currently enabled UPs */
5847 	ctxt.seid = vsi->seid;
5848 	ctxt.pf_num = vsi->back->hw.pf_id;
5849 	ctxt.vf_num = 0;
5850 	ctxt.uplink_seid = vsi->uplink_seid;
5851 	ctxt.info = vsi->info;
5852 	if (i40e_is_tc_mqprio_enabled(pf)) {
5853 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5854 		if (ret)
5855 			goto out;
5856 	} else {
5857 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5858 	}
5859 
5860 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5861 	 * queues changed.
5862 	 */
5863 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5864 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5865 				      vsi->num_queue_pairs);
5866 		ret = i40e_vsi_config_rss(vsi);
5867 		if (ret) {
5868 			dev_info(&vsi->back->pdev->dev,
5869 				 "Failed to reconfig rss for num_queues\n");
5870 			return ret;
5871 		}
5872 		vsi->reconfig_rss = false;
5873 	}
5874 	if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
5875 		ctxt.info.valid_sections |=
5876 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5877 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5878 	}
5879 
5880 	/* Update the VSI after updating the VSI queue-mapping
5881 	 * information
5882 	 */
5883 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5884 	if (ret) {
5885 		dev_info(&pf->pdev->dev,
5886 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5887 			 ERR_PTR(ret),
5888 			 libie_aq_str(hw->aq.asq_last_status));
5889 		goto out;
5890 	}
5891 	/* update the local VSI info with updated queue map */
5892 	i40e_vsi_update_queue_map(vsi, &ctxt);
5893 	vsi->info.valid_sections = 0;
5894 
5895 	/* Update current VSI BW information */
5896 	ret = i40e_vsi_get_bw_info(vsi);
5897 	if (ret) {
5898 		dev_info(&pf->pdev->dev,
5899 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5900 			 ERR_PTR(ret),
5901 			 libie_aq_str(hw->aq.asq_last_status));
5902 		goto out;
5903 	}
5904 
5905 	/* Update the netdev TC setup */
5906 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5907 out:
5908 	return ret;
5909 }
5910 
5911 /**
5912  * i40e_vsi_reconfig_tc - Reconfigure VSI Tx Scheduler for stored TC map
5913  * @vsi: VSI to be reconfigured
5914  *
5915  * This reconfigures a particular VSI for TCs that are mapped to the
5916  * TC bitmap stored previously for the VSI.
5917  *
5918  * Context: It is expected that the VSI queues have been quisced before
5919  *          calling this function.
5920  *
5921  * Return: 0 on success, negative value on failure
5922  **/
5923 static int i40e_vsi_reconfig_tc(struct i40e_vsi *vsi)
5924 {
5925 	u8 enabled_tc;
5926 
5927 	enabled_tc = vsi->tc_config.enabled_tc;
5928 	vsi->tc_config.enabled_tc = 0;
5929 
5930 	return i40e_vsi_config_tc(vsi, enabled_tc);
5931 }
5932 
5933 /**
5934  * i40e_get_link_speed - Returns link speed for the interface
5935  * @vsi: VSI to be configured
5936  *
5937  **/
5938 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5939 {
5940 	struct i40e_pf *pf = vsi->back;
5941 
5942 	switch (pf->hw.phy.link_info.link_speed) {
5943 	case I40E_LINK_SPEED_40GB:
5944 		return 40000;
5945 	case I40E_LINK_SPEED_25GB:
5946 		return 25000;
5947 	case I40E_LINK_SPEED_20GB:
5948 		return 20000;
5949 	case I40E_LINK_SPEED_10GB:
5950 		return 10000;
5951 	case I40E_LINK_SPEED_1GB:
5952 		return 1000;
5953 	default:
5954 		return -EINVAL;
5955 	}
5956 }
5957 
5958 /**
5959  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5960  * @vsi: Pointer to vsi structure
5961  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5962  *
5963  * Helper function to convert units before send to set BW limit
5964  **/
5965 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5966 {
5967 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5968 		dev_warn(&vsi->back->pdev->dev,
5969 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5970 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5971 	} else {
5972 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5973 	}
5974 
5975 	return max_tx_rate;
5976 }
5977 
5978 /**
5979  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5980  * @vsi: VSI to be configured
5981  * @seid: seid of the channel/VSI
5982  * @max_tx_rate: max TX rate to be configured as BW limit
5983  *
5984  * Helper function to set BW limit for a given VSI
5985  **/
5986 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5987 {
5988 	struct i40e_pf *pf = vsi->back;
5989 	u64 credits = 0;
5990 	int speed = 0;
5991 	int ret = 0;
5992 
5993 	speed = i40e_get_link_speed(vsi);
5994 	if (max_tx_rate > speed) {
5995 		dev_err(&pf->pdev->dev,
5996 			"Invalid max tx rate %llu specified for VSI seid %d.",
5997 			max_tx_rate, seid);
5998 		return -EINVAL;
5999 	}
6000 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
6001 		dev_warn(&pf->pdev->dev,
6002 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
6003 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
6004 	}
6005 
6006 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
6007 	credits = max_tx_rate;
6008 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
6009 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
6010 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
6011 	if (ret)
6012 		dev_err(&pf->pdev->dev,
6013 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
6014 			max_tx_rate, seid, ERR_PTR(ret),
6015 			libie_aq_str(pf->hw.aq.asq_last_status));
6016 	return ret;
6017 }
6018 
6019 /**
6020  * i40e_remove_queue_channels - Remove queue channels for the TCs
6021  * @vsi: VSI to be configured
6022  *
6023  * Remove queue channels for the TCs
6024  **/
6025 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
6026 {
6027 	struct i40e_cloud_filter *cfilter;
6028 	enum libie_aq_err last_aq_status;
6029 	struct i40e_channel *ch, *ch_tmp;
6030 	struct i40e_pf *pf = vsi->back;
6031 	struct hlist_node *node;
6032 	int ret, i;
6033 
6034 	/* Reset rss size that was stored when reconfiguring rss for
6035 	 * channel VSIs with non-power-of-2 queue count.
6036 	 */
6037 	vsi->current_rss_size = 0;
6038 
6039 	/* perform cleanup for channels if they exist */
6040 	if (list_empty(&vsi->ch_list))
6041 		return;
6042 
6043 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6044 		struct i40e_vsi *p_vsi;
6045 
6046 		list_del(&ch->list);
6047 		p_vsi = ch->parent_vsi;
6048 		if (!p_vsi || !ch->initialized) {
6049 			kfree(ch);
6050 			continue;
6051 		}
6052 		/* Reset queue contexts */
6053 		for (i = 0; i < ch->num_queue_pairs; i++) {
6054 			struct i40e_ring *tx_ring, *rx_ring;
6055 			u16 pf_q;
6056 
6057 			pf_q = ch->base_queue + i;
6058 			tx_ring = vsi->tx_rings[pf_q];
6059 			tx_ring->ch = NULL;
6060 
6061 			rx_ring = vsi->rx_rings[pf_q];
6062 			rx_ring->ch = NULL;
6063 		}
6064 
6065 		/* Reset BW configured for this VSI via mqprio */
6066 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6067 		if (ret)
6068 			dev_info(&vsi->back->pdev->dev,
6069 				 "Failed to reset tx rate for ch->seid %u\n",
6070 				 ch->seid);
6071 
6072 		/* delete cloud filters associated with this channel */
6073 		hlist_for_each_entry_safe(cfilter, node,
6074 					  &pf->cloud_filter_list, cloud_node) {
6075 			if (cfilter->seid != ch->seid)
6076 				continue;
6077 
6078 			hash_del(&cfilter->cloud_node);
6079 			if (cfilter->dst_port)
6080 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6081 									cfilter,
6082 									false);
6083 			else
6084 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6085 								false);
6086 			last_aq_status = pf->hw.aq.asq_last_status;
6087 			if (ret)
6088 				dev_info(&pf->pdev->dev,
6089 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6090 					 ERR_PTR(ret),
6091 					 libie_aq_str(last_aq_status));
6092 			kfree(cfilter);
6093 		}
6094 
6095 		/* delete VSI from FW */
6096 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6097 					     NULL);
6098 		if (ret)
6099 			dev_err(&vsi->back->pdev->dev,
6100 				"unable to remove channel (%d) for parent VSI(%d)\n",
6101 				ch->seid, p_vsi->seid);
6102 		kfree(ch);
6103 	}
6104 	INIT_LIST_HEAD(&vsi->ch_list);
6105 }
6106 
6107 /**
6108  * i40e_get_max_queues_for_channel
6109  * @vsi: ptr to VSI to which channels are associated with
6110  *
6111  * Helper function which returns max value among the queue counts set on the
6112  * channels/TCs created.
6113  **/
6114 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6115 {
6116 	struct i40e_channel *ch, *ch_tmp;
6117 	int max = 0;
6118 
6119 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6120 		if (!ch->initialized)
6121 			continue;
6122 		if (ch->num_queue_pairs > max)
6123 			max = ch->num_queue_pairs;
6124 	}
6125 
6126 	return max;
6127 }
6128 
6129 /**
6130  * i40e_validate_num_queues - validate num_queues w.r.t channel
6131  * @pf: ptr to PF device
6132  * @num_queues: number of queues
6133  * @vsi: the parent VSI
6134  * @reconfig_rss: indicates should the RSS be reconfigured or not
6135  *
6136  * This function validates number of queues in the context of new channel
6137  * which is being established and determines if RSS should be reconfigured
6138  * or not for parent VSI.
6139  **/
6140 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6141 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6142 {
6143 	int max_ch_queues;
6144 
6145 	if (!reconfig_rss)
6146 		return -EINVAL;
6147 
6148 	*reconfig_rss = false;
6149 	if (vsi->current_rss_size) {
6150 		if (num_queues > vsi->current_rss_size) {
6151 			dev_dbg(&pf->pdev->dev,
6152 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6153 				num_queues, vsi->current_rss_size);
6154 			return -EINVAL;
6155 		} else if ((num_queues < vsi->current_rss_size) &&
6156 			   (!is_power_of_2(num_queues))) {
6157 			dev_dbg(&pf->pdev->dev,
6158 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6159 				num_queues, vsi->current_rss_size);
6160 			return -EINVAL;
6161 		}
6162 	}
6163 
6164 	if (!is_power_of_2(num_queues)) {
6165 		/* Find the max num_queues configured for channel if channel
6166 		 * exist.
6167 		 * if channel exist, then enforce 'num_queues' to be more than
6168 		 * max ever queues configured for channel.
6169 		 */
6170 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6171 		if (num_queues < max_ch_queues) {
6172 			dev_dbg(&pf->pdev->dev,
6173 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6174 				num_queues, max_ch_queues);
6175 			return -EINVAL;
6176 		}
6177 		*reconfig_rss = true;
6178 	}
6179 
6180 	return 0;
6181 }
6182 
6183 /**
6184  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6185  * @vsi: the VSI being setup
6186  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6187  *
6188  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6189  **/
6190 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6191 {
6192 	struct i40e_pf *pf = vsi->back;
6193 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6194 	struct i40e_hw *hw = &pf->hw;
6195 	int local_rss_size;
6196 	u8 *lut;
6197 	int ret;
6198 
6199 	if (!vsi->rss_size)
6200 		return -EINVAL;
6201 
6202 	if (rss_size > vsi->rss_size)
6203 		return -EINVAL;
6204 
6205 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6206 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6207 	if (!lut)
6208 		return -ENOMEM;
6209 
6210 	/* Ignoring user configured lut if there is one */
6211 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6212 
6213 	/* Use user configured hash key if there is one, otherwise
6214 	 * use default.
6215 	 */
6216 	if (vsi->rss_hkey_user)
6217 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6218 	else
6219 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6220 
6221 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6222 	if (ret) {
6223 		dev_info(&pf->pdev->dev,
6224 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6225 			 ERR_PTR(ret),
6226 			 libie_aq_str(hw->aq.asq_last_status));
6227 		kfree(lut);
6228 		return ret;
6229 	}
6230 	kfree(lut);
6231 
6232 	/* Do the update w.r.t. storing rss_size */
6233 	if (!vsi->orig_rss_size)
6234 		vsi->orig_rss_size = vsi->rss_size;
6235 	vsi->current_rss_size = local_rss_size;
6236 
6237 	return ret;
6238 }
6239 
6240 /**
6241  * i40e_channel_setup_queue_map - Setup a channel queue map
6242  * @pf: ptr to PF device
6243  * @ctxt: VSI context structure
6244  * @ch: ptr to channel structure
6245  *
6246  * Setup queue map for a specific channel
6247  **/
6248 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6249 					 struct i40e_vsi_context *ctxt,
6250 					 struct i40e_channel *ch)
6251 {
6252 	u16 qcount, qmap, sections = 0;
6253 	u8 offset = 0;
6254 	int pow;
6255 
6256 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6257 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6258 
6259 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6260 	ch->num_queue_pairs = qcount;
6261 
6262 	/* find the next higher power-of-2 of num queue pairs */
6263 	pow = ilog2(qcount);
6264 	if (!is_power_of_2(qcount))
6265 		pow++;
6266 
6267 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6268 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6269 
6270 	/* Setup queue TC[0].qmap for given VSI context */
6271 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6272 
6273 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6274 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6275 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6276 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6277 }
6278 
6279 /**
6280  * i40e_add_channel - add a channel by adding VSI
6281  * @pf: ptr to PF device
6282  * @uplink_seid: underlying HW switching element (VEB) ID
6283  * @ch: ptr to channel structure
6284  *
6285  * Add a channel (VSI) using add_vsi and queue_map
6286  **/
6287 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6288 			    struct i40e_channel *ch)
6289 {
6290 	struct i40e_hw *hw = &pf->hw;
6291 	struct i40e_vsi_context ctxt;
6292 	u8 enabled_tc = 0x1; /* TC0 enabled */
6293 	int ret;
6294 
6295 	if (ch->type != I40E_VSI_VMDQ2) {
6296 		dev_info(&pf->pdev->dev,
6297 			 "add new vsi failed, ch->type %d\n", ch->type);
6298 		return -EINVAL;
6299 	}
6300 
6301 	memset(&ctxt, 0, sizeof(ctxt));
6302 	ctxt.pf_num = hw->pf_id;
6303 	ctxt.vf_num = 0;
6304 	ctxt.uplink_seid = uplink_seid;
6305 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6306 	if (ch->type == I40E_VSI_VMDQ2)
6307 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6308 
6309 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6310 		ctxt.info.valid_sections |=
6311 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6312 		ctxt.info.switch_id =
6313 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6314 	}
6315 
6316 	/* Set queue map for a given VSI context */
6317 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6318 
6319 	/* Now time to create VSI */
6320 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6321 	if (ret) {
6322 		dev_info(&pf->pdev->dev,
6323 			 "add new vsi failed, err %pe aq_err %s\n",
6324 			 ERR_PTR(ret),
6325 			 libie_aq_str(pf->hw.aq.asq_last_status));
6326 		return -ENOENT;
6327 	}
6328 
6329 	/* Success, update channel, set enabled_tc only if the channel
6330 	 * is not a macvlan
6331 	 */
6332 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6333 	ch->seid = ctxt.seid;
6334 	ch->vsi_number = ctxt.vsi_number;
6335 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6336 
6337 	/* copy just the sections touched not the entire info
6338 	 * since not all sections are valid as returned by
6339 	 * update vsi params
6340 	 */
6341 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6342 	memcpy(&ch->info.queue_mapping,
6343 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6344 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6345 	       sizeof(ctxt.info.tc_mapping));
6346 
6347 	return 0;
6348 }
6349 
6350 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6351 				  u8 *bw_share)
6352 {
6353 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6354 	int ret;
6355 	int i;
6356 
6357 	memset(&bw_data, 0, sizeof(bw_data));
6358 	bw_data.tc_valid_bits = ch->enabled_tc;
6359 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6360 		bw_data.tc_bw_credits[i] = bw_share[i];
6361 
6362 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6363 				       &bw_data, NULL);
6364 	if (ret) {
6365 		dev_info(&vsi->back->pdev->dev,
6366 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6367 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6368 		return -EINVAL;
6369 	}
6370 
6371 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6372 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6373 
6374 	return 0;
6375 }
6376 
6377 /**
6378  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6379  * @pf: ptr to PF device
6380  * @vsi: the VSI being setup
6381  * @ch: ptr to channel structure
6382  *
6383  * Configure TX rings associated with channel (VSI) since queues are being
6384  * from parent VSI.
6385  **/
6386 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6387 				       struct i40e_vsi *vsi,
6388 				       struct i40e_channel *ch)
6389 {
6390 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6391 	int ret;
6392 	int i;
6393 
6394 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6395 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6396 		if (ch->enabled_tc & BIT(i))
6397 			bw_share[i] = 1;
6398 	}
6399 
6400 	/* configure BW for new VSI */
6401 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6402 	if (ret) {
6403 		dev_info(&vsi->back->pdev->dev,
6404 			 "Failed configuring TC map %d for channel (seid %u)\n",
6405 			 ch->enabled_tc, ch->seid);
6406 		return ret;
6407 	}
6408 
6409 	for (i = 0; i < ch->num_queue_pairs; i++) {
6410 		struct i40e_ring *tx_ring, *rx_ring;
6411 		u16 pf_q;
6412 
6413 		pf_q = ch->base_queue + i;
6414 
6415 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6416 		 * context
6417 		 */
6418 		tx_ring = vsi->tx_rings[pf_q];
6419 		tx_ring->ch = ch;
6420 
6421 		/* Get the RX ring ptr */
6422 		rx_ring = vsi->rx_rings[pf_q];
6423 		rx_ring->ch = ch;
6424 	}
6425 
6426 	return 0;
6427 }
6428 
6429 /**
6430  * i40e_setup_hw_channel - setup new channel
6431  * @pf: ptr to PF device
6432  * @vsi: the VSI being setup
6433  * @ch: ptr to channel structure
6434  * @uplink_seid: underlying HW switching element (VEB) ID
6435  * @type: type of channel to be created (VMDq2/VF)
6436  *
6437  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6438  * and configures TX rings accordingly
6439  **/
6440 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6441 					struct i40e_vsi *vsi,
6442 					struct i40e_channel *ch,
6443 					u16 uplink_seid, u8 type)
6444 {
6445 	int ret;
6446 
6447 	ch->initialized = false;
6448 	ch->base_queue = vsi->next_base_queue;
6449 	ch->type = type;
6450 
6451 	/* Proceed with creation of channel (VMDq2) VSI */
6452 	ret = i40e_add_channel(pf, uplink_seid, ch);
6453 	if (ret) {
6454 		dev_info(&pf->pdev->dev,
6455 			 "failed to add_channel using uplink_seid %u\n",
6456 			 uplink_seid);
6457 		return ret;
6458 	}
6459 
6460 	/* Mark the successful creation of channel */
6461 	ch->initialized = true;
6462 
6463 	/* Reconfigure TX queues using QTX_CTL register */
6464 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6465 	if (ret) {
6466 		dev_info(&pf->pdev->dev,
6467 			 "failed to configure TX rings for channel %u\n",
6468 			 ch->seid);
6469 		return ret;
6470 	}
6471 
6472 	/* update 'next_base_queue' */
6473 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6474 	dev_dbg(&pf->pdev->dev,
6475 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6476 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6477 		ch->num_queue_pairs,
6478 		vsi->next_base_queue);
6479 	return ret;
6480 }
6481 
6482 /**
6483  * i40e_setup_channel - setup new channel using uplink element
6484  * @pf: ptr to PF device
6485  * @vsi: pointer to the VSI to set up the channel within
6486  * @ch: ptr to channel structure
6487  *
6488  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6489  * and uplink switching element (uplink_seid)
6490  **/
6491 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6492 			       struct i40e_channel *ch)
6493 {
6494 	struct i40e_vsi *main_vsi;
6495 	u8 vsi_type;
6496 	u16 seid;
6497 	int ret;
6498 
6499 	if (vsi->type == I40E_VSI_MAIN) {
6500 		vsi_type = I40E_VSI_VMDQ2;
6501 	} else {
6502 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6503 			vsi->type);
6504 		return false;
6505 	}
6506 
6507 	/* underlying switching element */
6508 	main_vsi = i40e_pf_get_main_vsi(pf);
6509 	seid = main_vsi->uplink_seid;
6510 
6511 	/* create channel (VSI), configure TX rings */
6512 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6513 	if (ret) {
6514 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6515 		return false;
6516 	}
6517 
6518 	return ch->initialized ? true : false;
6519 }
6520 
6521 /**
6522  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6523  * @vsi: ptr to VSI which has PF backing
6524  *
6525  * Sets up switch mode correctly if it needs to be changed and perform
6526  * what are allowed modes.
6527  **/
6528 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6529 {
6530 	u8 mode;
6531 	struct i40e_pf *pf = vsi->back;
6532 	struct i40e_hw *hw = &pf->hw;
6533 	int ret;
6534 
6535 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6536 	if (ret)
6537 		return -EINVAL;
6538 
6539 	if (hw->dev_caps.switch_mode) {
6540 		/* if switch mode is set, support mode2 (non-tunneled for
6541 		 * cloud filter) for now
6542 		 */
6543 		u32 switch_mode = hw->dev_caps.switch_mode &
6544 				  I40E_SWITCH_MODE_MASK;
6545 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6546 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6547 				return 0;
6548 			dev_err(&pf->pdev->dev,
6549 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6550 				hw->dev_caps.switch_mode);
6551 			return -EINVAL;
6552 		}
6553 	}
6554 
6555 	/* Set Bit 7 to be valid */
6556 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6557 
6558 	/* Set L4type for TCP support */
6559 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6560 
6561 	/* Set cloud filter mode */
6562 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6563 
6564 	/* Prep mode field for set_switch_config */
6565 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6566 					pf->last_sw_conf_valid_flags,
6567 					mode, NULL);
6568 	if (ret && hw->aq.asq_last_status != LIBIE_AQ_RC_ESRCH)
6569 		dev_err(&pf->pdev->dev,
6570 			"couldn't set switch config bits, err %pe aq_err %s\n",
6571 			ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
6572 
6573 	return ret;
6574 }
6575 
6576 /**
6577  * i40e_create_queue_channel - function to create channel
6578  * @vsi: VSI to be configured
6579  * @ch: ptr to channel (it contains channel specific params)
6580  *
6581  * This function creates channel (VSI) using num_queues specified by user,
6582  * reconfigs RSS if needed.
6583  **/
6584 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6585 			      struct i40e_channel *ch)
6586 {
6587 	struct i40e_pf *pf = vsi->back;
6588 	bool reconfig_rss;
6589 	int err;
6590 
6591 	if (!ch)
6592 		return -EINVAL;
6593 
6594 	if (!ch->num_queue_pairs) {
6595 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6596 			ch->num_queue_pairs);
6597 		return -EINVAL;
6598 	}
6599 
6600 	/* validate user requested num_queues for channel */
6601 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6602 				       &reconfig_rss);
6603 	if (err) {
6604 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6605 			 ch->num_queue_pairs);
6606 		return -EINVAL;
6607 	}
6608 
6609 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6610 	 * VSI to be added switch to VEB mode.
6611 	 */
6612 
6613 	if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
6614 		set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
6615 
6616 		if (vsi->type == I40E_VSI_MAIN) {
6617 			if (i40e_is_tc_mqprio_enabled(pf))
6618 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6619 			else
6620 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6621 		}
6622 		/* now onwards for main VSI, number of queues will be value
6623 		 * of TC0's queue count
6624 		 */
6625 	}
6626 
6627 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6628 	 * it should be more than num_queues
6629 	 */
6630 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6631 		dev_dbg(&pf->pdev->dev,
6632 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6633 			vsi->cnt_q_avail, ch->num_queue_pairs);
6634 		return -EINVAL;
6635 	}
6636 
6637 	/* reconfig_rss only if vsi type is MAIN_VSI */
6638 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6639 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6640 		if (err) {
6641 			dev_info(&pf->pdev->dev,
6642 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6643 				 ch->num_queue_pairs);
6644 			return -EINVAL;
6645 		}
6646 	}
6647 
6648 	if (!i40e_setup_channel(pf, vsi, ch)) {
6649 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6650 		return -EINVAL;
6651 	}
6652 
6653 	dev_info(&pf->pdev->dev,
6654 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6655 		 ch->seid, ch->num_queue_pairs);
6656 
6657 	/* configure VSI for BW limit */
6658 	if (ch->max_tx_rate) {
6659 		u64 credits = ch->max_tx_rate;
6660 
6661 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6662 			return -EINVAL;
6663 
6664 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6665 		dev_dbg(&pf->pdev->dev,
6666 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6667 			ch->max_tx_rate,
6668 			credits,
6669 			ch->seid);
6670 	}
6671 
6672 	/* in case of VF, this will be main SRIOV VSI */
6673 	ch->parent_vsi = vsi;
6674 
6675 	/* and update main_vsi's count for queue_available to use */
6676 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6677 
6678 	return 0;
6679 }
6680 
6681 /**
6682  * i40e_configure_queue_channels - Add queue channel for the given TCs
6683  * @vsi: VSI to be configured
6684  *
6685  * Configures queue channel mapping to the given TCs
6686  **/
6687 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6688 {
6689 	struct i40e_channel *ch;
6690 	u64 max_rate = 0;
6691 	int ret = 0, i;
6692 
6693 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6694 	vsi->tc_seid_map[0] = vsi->seid;
6695 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6696 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6697 			ch = kzalloc_obj(*ch);
6698 			if (!ch) {
6699 				ret = -ENOMEM;
6700 				goto err_free;
6701 			}
6702 
6703 			INIT_LIST_HEAD(&ch->list);
6704 			ch->num_queue_pairs =
6705 				vsi->tc_config.tc_info[i].qcount;
6706 			ch->base_queue =
6707 				vsi->tc_config.tc_info[i].qoffset;
6708 
6709 			/* Bandwidth limit through tc interface is in bytes/s,
6710 			 * change to Mbit/s
6711 			 */
6712 			max_rate = vsi->mqprio_qopt.max_rate[i];
6713 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6714 			ch->max_tx_rate = max_rate;
6715 
6716 			list_add_tail(&ch->list, &vsi->ch_list);
6717 
6718 			ret = i40e_create_queue_channel(vsi, ch);
6719 			if (ret) {
6720 				dev_err(&vsi->back->pdev->dev,
6721 					"Failed creating queue channel with TC%d: queues %d\n",
6722 					i, ch->num_queue_pairs);
6723 				goto err_free;
6724 			}
6725 			vsi->tc_seid_map[i] = ch->seid;
6726 		}
6727 	}
6728 
6729 	/* reset to reconfigure TX queue contexts */
6730 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6731 	return ret;
6732 
6733 err_free:
6734 	i40e_remove_queue_channels(vsi);
6735 	return ret;
6736 }
6737 
6738 /**
6739  * i40e_veb_config_tc - Configure TCs for given VEB
6740  * @veb: given VEB
6741  * @enabled_tc: TC bitmap
6742  *
6743  * Configures given TC bitmap for VEB (switching) element
6744  **/
6745 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6746 {
6747 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6748 	struct i40e_pf *pf = veb->pf;
6749 	int ret = 0;
6750 	int i;
6751 
6752 	/* No TCs or already enabled TCs just return */
6753 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6754 		return ret;
6755 
6756 	bw_data.tc_valid_bits = enabled_tc;
6757 	/* bw_data.absolute_credits is not set (relative) */
6758 
6759 	/* Enable ETS TCs with equal BW Share for now */
6760 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6761 		if (enabled_tc & BIT(i))
6762 			bw_data.tc_bw_share_credits[i] = 1;
6763 	}
6764 
6765 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6766 						   &bw_data, NULL);
6767 	if (ret) {
6768 		dev_info(&pf->pdev->dev,
6769 			 "VEB bw config failed, err %pe aq_err %s\n",
6770 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6771 		goto out;
6772 	}
6773 
6774 	/* Update the BW information */
6775 	ret = i40e_veb_get_bw_info(veb);
6776 	if (ret) {
6777 		dev_info(&pf->pdev->dev,
6778 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6779 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6780 	}
6781 
6782 out:
6783 	return ret;
6784 }
6785 
6786 #ifdef CONFIG_I40E_DCB
6787 /**
6788  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6789  * @pf: PF struct
6790  *
6791  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6792  * the caller would've quiesce all the VSIs before calling
6793  * this function
6794  **/
6795 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6796 {
6797 	struct i40e_vsi *vsi;
6798 	struct i40e_veb *veb;
6799 	u8 tc_map = 0;
6800 	int ret;
6801 	int v;
6802 
6803 	/* Enable the TCs available on PF to all VEBs */
6804 	tc_map = i40e_pf_get_tc_map(pf);
6805 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6806 		return;
6807 
6808 	i40e_pf_for_each_veb(pf, v, veb) {
6809 		ret = i40e_veb_config_tc(veb, tc_map);
6810 		if (ret) {
6811 			dev_info(&pf->pdev->dev,
6812 				 "Failed configuring TC for VEB seid=%d\n",
6813 				 veb->seid);
6814 			/* Will try to configure as many components */
6815 		}
6816 	}
6817 
6818 	/* Update each VSI */
6819 	i40e_pf_for_each_vsi(pf, v, vsi) {
6820 		/* - Enable all TCs for the LAN VSI
6821 		 * - For all others keep them at TC0 for now
6822 		 */
6823 		if (vsi->type == I40E_VSI_MAIN)
6824 			tc_map = i40e_pf_get_tc_map(pf);
6825 		else
6826 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6827 
6828 		ret = i40e_vsi_config_tc(vsi, tc_map);
6829 		if (ret) {
6830 			dev_info(&pf->pdev->dev,
6831 				 "Failed configuring TC for VSI seid=%d\n",
6832 				 vsi->seid);
6833 			/* Will try to configure as many components */
6834 		} else {
6835 			/* Re-configure VSI vectors based on updated TC map */
6836 			i40e_vsi_map_rings_to_vectors(vsi);
6837 			if (vsi->netdev)
6838 				i40e_dcbnl_set_all(vsi);
6839 		}
6840 	}
6841 }
6842 
6843 /**
6844  * i40e_resume_port_tx - Resume port Tx
6845  * @pf: PF struct
6846  *
6847  * Resume a port's Tx and issue a PF reset in case of failure to
6848  * resume.
6849  **/
6850 static int i40e_resume_port_tx(struct i40e_pf *pf)
6851 {
6852 	struct i40e_hw *hw = &pf->hw;
6853 	int ret;
6854 
6855 	ret = i40e_aq_resume_port_tx(hw, NULL);
6856 	if (ret) {
6857 		dev_info(&pf->pdev->dev,
6858 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6859 			  ERR_PTR(ret),
6860 			  libie_aq_str(pf->hw.aq.asq_last_status));
6861 		/* Schedule PF reset to recover */
6862 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6863 		i40e_service_event_schedule(pf);
6864 	}
6865 
6866 	return ret;
6867 }
6868 
6869 /**
6870  * i40e_suspend_port_tx - Suspend port Tx
6871  * @pf: PF struct
6872  *
6873  * Suspend a port's Tx and issue a PF reset in case of failure.
6874  **/
6875 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6876 {
6877 	struct i40e_hw *hw = &pf->hw;
6878 	int ret;
6879 
6880 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6881 	if (ret) {
6882 		dev_info(&pf->pdev->dev,
6883 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6884 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6885 		/* Schedule PF reset to recover */
6886 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6887 		i40e_service_event_schedule(pf);
6888 	}
6889 
6890 	return ret;
6891 }
6892 
6893 /**
6894  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6895  * @pf: PF being configured
6896  * @new_cfg: New DCBX configuration
6897  *
6898  * Program DCB settings into HW and reconfigure VEB/VSIs on
6899  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6900  **/
6901 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6902 				  struct i40e_dcbx_config *new_cfg)
6903 {
6904 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6905 	int ret;
6906 
6907 	/* Check if need reconfiguration */
6908 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6909 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6910 		return 0;
6911 	}
6912 
6913 	/* Config change disable all VSIs */
6914 	i40e_pf_quiesce_all_vsi(pf);
6915 
6916 	/* Copy the new config to the current config */
6917 	*old_cfg = *new_cfg;
6918 	old_cfg->etsrec = old_cfg->etscfg;
6919 	ret = i40e_set_dcb_config(&pf->hw);
6920 	if (ret) {
6921 		dev_info(&pf->pdev->dev,
6922 			 "Set DCB Config failed, err %pe aq_err %s\n",
6923 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
6924 		goto out;
6925 	}
6926 
6927 	/* Changes in configuration update VEB/VSI */
6928 	i40e_dcb_reconfigure(pf);
6929 out:
6930 	/* In case of reset do not try to resume anything */
6931 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6932 		/* Re-start the VSIs if disabled */
6933 		ret = i40e_resume_port_tx(pf);
6934 		/* In case of error no point in resuming VSIs */
6935 		if (ret)
6936 			goto err;
6937 		i40e_pf_unquiesce_all_vsi(pf);
6938 	}
6939 err:
6940 	return ret;
6941 }
6942 
6943 /**
6944  * i40e_hw_dcb_config - Program new DCBX settings into HW
6945  * @pf: PF being configured
6946  * @new_cfg: New DCBX configuration
6947  *
6948  * Program DCB settings into HW and reconfigure VEB/VSIs on
6949  * given PF
6950  **/
6951 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6952 {
6953 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6954 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6955 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6956 	struct i40e_dcbx_config *old_cfg;
6957 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6958 	struct i40e_rx_pb_config pb_cfg;
6959 	struct i40e_hw *hw = &pf->hw;
6960 	u8 num_ports = hw->num_ports;
6961 	bool need_reconfig;
6962 	int ret = -EINVAL;
6963 	u8 lltc_map = 0;
6964 	u8 tc_map = 0;
6965 	u8 new_numtc;
6966 	u8 i;
6967 
6968 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6969 	/* Un-pack information to Program ETS HW via shared API
6970 	 * numtc, tcmap
6971 	 * LLTC map
6972 	 * ETS/NON-ETS arbiter mode
6973 	 * max exponent (credit refills)
6974 	 * Total number of ports
6975 	 * PFC priority bit-map
6976 	 * Priority Table
6977 	 * BW % per TC
6978 	 * Arbiter mode between UPs sharing same TC
6979 	 * TSA table (ETS or non-ETS)
6980 	 * EEE enabled or not
6981 	 * MFS TC table
6982 	 */
6983 
6984 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6985 
6986 	memset(&ets_data, 0, sizeof(ets_data));
6987 	for (i = 0; i < new_numtc; i++) {
6988 		tc_map |= BIT(i);
6989 		switch (new_cfg->etscfg.tsatable[i]) {
6990 		case I40E_IEEE_TSA_ETS:
6991 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6992 			ets_data.tc_bw_share_credits[i] =
6993 					new_cfg->etscfg.tcbwtable[i];
6994 			break;
6995 		case I40E_IEEE_TSA_STRICT:
6996 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6997 			lltc_map |= BIT(i);
6998 			ets_data.tc_bw_share_credits[i] =
6999 					I40E_DCB_STRICT_PRIO_CREDITS;
7000 			break;
7001 		default:
7002 			/* Invalid TSA type */
7003 			need_reconfig = false;
7004 			goto out;
7005 		}
7006 	}
7007 
7008 	old_cfg = &hw->local_dcbx_config;
7009 	/* Check if need reconfiguration */
7010 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
7011 
7012 	/* If needed, enable/disable frame tagging, disable all VSIs
7013 	 * and suspend port tx
7014 	 */
7015 	if (need_reconfig) {
7016 		/* Enable DCB tagging only when more than one TC */
7017 		if (new_numtc > 1)
7018 			set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7019 		else
7020 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7021 
7022 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
7023 		/* Reconfiguration needed quiesce all VSIs */
7024 		i40e_pf_quiesce_all_vsi(pf);
7025 		ret = i40e_suspend_port_tx(pf);
7026 		if (ret)
7027 			goto err;
7028 	}
7029 
7030 	/* Configure Port ETS Tx Scheduler */
7031 	ets_data.tc_valid_bits = tc_map;
7032 	ets_data.tc_strict_priority_flags = lltc_map;
7033 	ret = i40e_aq_config_switch_comp_ets
7034 		(hw, pf->mac_seid, &ets_data,
7035 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7036 	if (ret) {
7037 		dev_info(&pf->pdev->dev,
7038 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7039 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
7040 		goto out;
7041 	}
7042 
7043 	/* Configure Rx ETS HW */
7044 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7045 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7046 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7047 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7048 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7049 				   lltc_map);
7050 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7051 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7052 				     prio_type);
7053 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7054 			       new_cfg->etscfg.prioritytable);
7055 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7056 
7057 	/* Configure Rx Packet Buffers in HW */
7058 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7059 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
7060 
7061 		mfs_tc[i] = main_vsi->netdev->mtu;
7062 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7063 	}
7064 
7065 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7066 					 false, new_cfg->pfc.pfcenable,
7067 					 mfs_tc, &pb_cfg);
7068 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7069 
7070 	/* Update the local Rx Packet buffer config */
7071 	pf->pb_cfg = pb_cfg;
7072 
7073 	/* Inform the FW about changes to DCB configuration */
7074 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7075 	if (ret) {
7076 		dev_info(&pf->pdev->dev,
7077 			 "DCB Updated failed, err %pe aq_err %s\n",
7078 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
7079 		goto out;
7080 	}
7081 
7082 	/* Update the port DCBx configuration */
7083 	*old_cfg = *new_cfg;
7084 
7085 	/* Changes in configuration update VEB/VSI */
7086 	i40e_dcb_reconfigure(pf);
7087 out:
7088 	/* Re-start the VSIs if disabled */
7089 	if (need_reconfig) {
7090 		ret = i40e_resume_port_tx(pf);
7091 
7092 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7093 		/* In case of error no point in resuming VSIs */
7094 		if (ret)
7095 			goto err;
7096 
7097 		/* Wait for the PF's queues to be disabled */
7098 		ret = i40e_pf_wait_queues_disabled(pf);
7099 		if (ret) {
7100 			/* Schedule PF reset to recover */
7101 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7102 			i40e_service_event_schedule(pf);
7103 			goto err;
7104 		} else {
7105 			i40e_pf_unquiesce_all_vsi(pf);
7106 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7107 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7108 		}
7109 		/* registers are set, lets apply */
7110 		if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps))
7111 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7112 	}
7113 
7114 err:
7115 	return ret;
7116 }
7117 
7118 /**
7119  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7120  * @pf: PF being queried
7121  *
7122  * Set default DCB configuration in case DCB is to be done in SW.
7123  **/
7124 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7125 {
7126 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7127 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7128 	struct i40e_hw *hw = &pf->hw;
7129 	int err;
7130 
7131 	if (test_bit(I40E_HW_CAP_USE_SET_LLDP_MIB, pf->hw.caps)) {
7132 		/* Update the local cached instance with TC0 ETS */
7133 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7134 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7135 		pf->tmp_cfg.etscfg.maxtcs = 0;
7136 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7137 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7138 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7139 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7140 		/* FW needs one App to configure HW */
7141 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7142 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7143 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7144 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7145 
7146 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7147 	}
7148 
7149 	memset(&ets_data, 0, sizeof(ets_data));
7150 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7151 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7152 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7153 
7154 	/* Enable ETS on the Physical port */
7155 	err = i40e_aq_config_switch_comp_ets
7156 		(hw, pf->mac_seid, &ets_data,
7157 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7158 	if (err) {
7159 		dev_info(&pf->pdev->dev,
7160 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7161 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7162 		err = -ENOENT;
7163 		goto out;
7164 	}
7165 
7166 	/* Update the local cached instance with TC0 ETS */
7167 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7168 	dcb_cfg->etscfg.cbs = 0;
7169 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7170 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7171 
7172 out:
7173 	return err;
7174 }
7175 
7176 /**
7177  * i40e_init_pf_dcb - Initialize DCB configuration
7178  * @pf: PF being configured
7179  *
7180  * Query the current DCB configuration and cache it
7181  * in the hardware structure
7182  **/
7183 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7184 {
7185 	struct i40e_hw *hw = &pf->hw;
7186 	int err;
7187 
7188 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7189 	 * Also do not enable DCBx if FW LLDP agent is disabled
7190 	 */
7191 	if (test_bit(I40E_HW_CAP_NO_DCB_SUPPORT, pf->hw.caps)) {
7192 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7193 		err = -EOPNOTSUPP;
7194 		goto out;
7195 	}
7196 	if (test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) {
7197 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7198 		err = i40e_dcb_sw_default_config(pf);
7199 		if (err) {
7200 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7201 			goto out;
7202 		}
7203 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7204 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7205 			       DCB_CAP_DCBX_VER_IEEE;
7206 		/* at init capable but disabled */
7207 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7208 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7209 		goto out;
7210 	}
7211 	err = i40e_init_dcb(hw, true);
7212 	if (!err) {
7213 		/* Device/Function is not DCBX capable */
7214 		if ((!hw->func_caps.dcb) ||
7215 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7216 			dev_info(&pf->pdev->dev,
7217 				 "DCBX offload is not supported or is disabled for this PF.\n");
7218 		} else {
7219 			/* When status is not DISABLED then DCBX in FW */
7220 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7221 				       DCB_CAP_DCBX_VER_IEEE;
7222 
7223 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
7224 			/* Enable DCB tagging only when more than one TC
7225 			 * or explicitly disable if only one TC
7226 			 */
7227 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7228 				set_bit(I40E_FLAG_DCB_ENA, pf->flags);
7229 			else
7230 				clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
7231 			dev_dbg(&pf->pdev->dev,
7232 				"DCBX offload is supported for this PF.\n");
7233 		}
7234 	} else if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_EPERM) {
7235 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7236 		set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags);
7237 	} else {
7238 		dev_info(&pf->pdev->dev,
7239 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7240 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7241 	}
7242 
7243 out:
7244 	return err;
7245 }
7246 #endif /* CONFIG_I40E_DCB */
7247 
7248 static void i40e_print_link_message_eee(struct i40e_vsi *vsi,
7249 					const char *speed, const char *fc)
7250 {
7251 	struct ethtool_keee kedata;
7252 
7253 	memzero_explicit(&kedata, sizeof(kedata));
7254 	if (vsi->netdev->ethtool_ops->get_eee)
7255 		vsi->netdev->ethtool_ops->get_eee(vsi->netdev, &kedata);
7256 
7257 	if (!linkmode_empty(kedata.supported))
7258 		netdev_info(vsi->netdev,
7259 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s, EEE: %s\n",
7260 			    speed, fc,
7261 			    kedata.eee_enabled ? "Enabled" : "Disabled");
7262 	else
7263 		netdev_info(vsi->netdev,
7264 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7265 			    speed, fc);
7266 }
7267 
7268 /**
7269  * i40e_print_link_message - print link up or down
7270  * @vsi: the VSI for which link needs a message
7271  * @isup: true of link is up, false otherwise
7272  */
7273 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7274 {
7275 	enum i40e_aq_link_speed new_speed;
7276 	struct i40e_pf *pf = vsi->back;
7277 	char *speed = "Unknown";
7278 	char *fc = "Unknown";
7279 	char *fec = "";
7280 	char *req_fec = "";
7281 	char *an = "";
7282 
7283 	if (isup)
7284 		new_speed = pf->hw.phy.link_info.link_speed;
7285 	else
7286 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7287 
7288 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7289 		return;
7290 	vsi->current_isup = isup;
7291 	vsi->current_speed = new_speed;
7292 	if (!isup) {
7293 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7294 		return;
7295 	}
7296 
7297 	/* Warn user if link speed on NPAR enabled partition is not at
7298 	 * least 10GB
7299 	 */
7300 	if (pf->hw.func_caps.npar_enable &&
7301 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7302 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7303 		netdev_warn(vsi->netdev,
7304 			    "The partition detected link speed that is less than 10Gbps\n");
7305 
7306 	switch (pf->hw.phy.link_info.link_speed) {
7307 	case I40E_LINK_SPEED_40GB:
7308 		speed = "40 G";
7309 		break;
7310 	case I40E_LINK_SPEED_20GB:
7311 		speed = "20 G";
7312 		break;
7313 	case I40E_LINK_SPEED_25GB:
7314 		speed = "25 G";
7315 		break;
7316 	case I40E_LINK_SPEED_10GB:
7317 		speed = "10 G";
7318 		break;
7319 	case I40E_LINK_SPEED_5GB:
7320 		speed = "5 G";
7321 		break;
7322 	case I40E_LINK_SPEED_2_5GB:
7323 		speed = "2.5 G";
7324 		break;
7325 	case I40E_LINK_SPEED_1GB:
7326 		speed = "1000 M";
7327 		break;
7328 	case I40E_LINK_SPEED_100MB:
7329 		speed = "100 M";
7330 		break;
7331 	default:
7332 		break;
7333 	}
7334 
7335 	switch (pf->hw.fc.current_mode) {
7336 	case I40E_FC_FULL:
7337 		fc = "RX/TX";
7338 		break;
7339 	case I40E_FC_TX_PAUSE:
7340 		fc = "TX";
7341 		break;
7342 	case I40E_FC_RX_PAUSE:
7343 		fc = "RX";
7344 		break;
7345 	default:
7346 		fc = "None";
7347 		break;
7348 	}
7349 
7350 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7351 		req_fec = "None";
7352 		fec = "None";
7353 		an = "False";
7354 
7355 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7356 			an = "True";
7357 
7358 		if (pf->hw.phy.link_info.fec_info &
7359 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7360 			fec = "CL74 FC-FEC/BASE-R";
7361 		else if (pf->hw.phy.link_info.fec_info &
7362 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7363 			fec = "CL108 RS-FEC";
7364 
7365 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7366 		 * both RS and FC are requested
7367 		 */
7368 		if (vsi->back->hw.phy.link_info.req_fec_info &
7369 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7370 			if (vsi->back->hw.phy.link_info.req_fec_info &
7371 			    I40E_AQ_REQUEST_FEC_RS)
7372 				req_fec = "CL108 RS-FEC";
7373 			else
7374 				req_fec = "CL74 FC-FEC/BASE-R";
7375 		}
7376 		netdev_info(vsi->netdev,
7377 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7378 			    speed, req_fec, fec, an, fc);
7379 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7380 		req_fec = "None";
7381 		fec = "None";
7382 		an = "False";
7383 
7384 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7385 			an = "True";
7386 
7387 		if (pf->hw.phy.link_info.fec_info &
7388 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7389 			fec = "CL74 FC-FEC/BASE-R";
7390 
7391 		if (pf->hw.phy.link_info.req_fec_info &
7392 		    I40E_AQ_REQUEST_FEC_KR)
7393 			req_fec = "CL74 FC-FEC/BASE-R";
7394 
7395 		netdev_info(vsi->netdev,
7396 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7397 			    speed, req_fec, fec, an, fc);
7398 	} else {
7399 		i40e_print_link_message_eee(vsi, speed, fc);
7400 	}
7401 
7402 }
7403 
7404 /**
7405  * i40e_up_complete - Finish the last steps of bringing up a connection
7406  * @vsi: the VSI being configured
7407  **/
7408 static int i40e_up_complete(struct i40e_vsi *vsi)
7409 {
7410 	struct i40e_pf *pf = vsi->back;
7411 	int err;
7412 
7413 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
7414 		i40e_vsi_configure_msix(vsi);
7415 	else
7416 		i40e_configure_msi_and_legacy(vsi);
7417 
7418 	/* start rings */
7419 	err = i40e_vsi_start_rings(vsi);
7420 	if (err)
7421 		return err;
7422 
7423 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7424 	i40e_napi_enable_all(vsi);
7425 	i40e_vsi_enable_irq(vsi);
7426 
7427 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7428 	    (vsi->netdev)) {
7429 		i40e_print_link_message(vsi, true);
7430 		netif_tx_start_all_queues(vsi->netdev);
7431 		netif_carrier_on(vsi->netdev);
7432 	}
7433 
7434 	/* replay FDIR SB filters */
7435 	if (vsi->type == I40E_VSI_FDIR) {
7436 		/* reset fd counters */
7437 		pf->fd_add_err = 0;
7438 		pf->fd_atr_cnt = 0;
7439 		i40e_fdir_filter_restore(vsi);
7440 	}
7441 
7442 	/* On the next run of the service_task, notify any clients of the new
7443 	 * opened netdev
7444 	 */
7445 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7446 	i40e_service_event_schedule(pf);
7447 
7448 	return 0;
7449 }
7450 
7451 /**
7452  * i40e_vsi_reinit_locked - Reset the VSI
7453  * @vsi: the VSI being configured
7454  *
7455  * Rebuild the ring structs after some configuration
7456  * has changed, e.g. MTU size.
7457  **/
7458 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7459 {
7460 	struct i40e_pf *pf = vsi->back;
7461 
7462 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7463 		usleep_range(1000, 2000);
7464 	i40e_down(vsi);
7465 
7466 	i40e_up(vsi);
7467 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7468 }
7469 
7470 /**
7471  * i40e_force_link_state - Force the link status
7472  * @pf: board private structure
7473  * @is_up: whether the link state should be forced up or down
7474  **/
7475 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7476 {
7477 	struct i40e_aq_get_phy_abilities_resp abilities;
7478 	struct i40e_aq_set_phy_config config = {0};
7479 	bool non_zero_phy_type = is_up;
7480 	struct i40e_hw *hw = &pf->hw;
7481 	u64 mask;
7482 	u8 speed;
7483 	int err;
7484 
7485 	/* Card might've been put in an unstable state by other drivers
7486 	 * and applications, which causes incorrect speed values being
7487 	 * set on startup. In order to clear speed registers, we call
7488 	 * get_phy_capabilities twice, once to get initial state of
7489 	 * available speeds, and once to get current PHY config.
7490 	 */
7491 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7492 					   NULL);
7493 	if (err) {
7494 		dev_err(&pf->pdev->dev,
7495 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7496 			ERR_PTR(err), libie_aq_str(hw->aq.asq_last_status));
7497 		return err;
7498 	}
7499 	speed = abilities.link_speed;
7500 
7501 	/* Get the current phy config */
7502 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7503 					   NULL);
7504 	if (err) {
7505 		dev_err(&pf->pdev->dev,
7506 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7507 			ERR_PTR(err), libie_aq_str(hw->aq.asq_last_status));
7508 		return err;
7509 	}
7510 
7511 	/* If link needs to go up, but was not forced to go down,
7512 	 * and its speed values are OK, no need for a flap
7513 	 * if non_zero_phy_type was set, still need to force up
7514 	 */
7515 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags))
7516 		non_zero_phy_type = true;
7517 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7518 		return 0;
7519 
7520 	/* To force link we need to set bits for all supported PHY types,
7521 	 * but there are now more than 32, so we need to split the bitmap
7522 	 * across two fields.
7523 	 */
7524 	mask = I40E_PHY_TYPES_BITMASK;
7525 	config.phy_type =
7526 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7527 	config.phy_type_ext =
7528 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7529 	/* Copy the old settings, except of phy_type */
7530 	config.abilities = abilities.abilities;
7531 	if (test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags)) {
7532 		if (is_up)
7533 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7534 		else
7535 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7536 	}
7537 	if (abilities.link_speed != 0)
7538 		config.link_speed = abilities.link_speed;
7539 	else
7540 		config.link_speed = speed;
7541 	config.eee_capability = abilities.eee_capability;
7542 	config.eeer = abilities.eeer_val;
7543 	config.low_power_ctrl = abilities.d3_lpan;
7544 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7545 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7546 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7547 
7548 	if (err) {
7549 		dev_err(&pf->pdev->dev,
7550 			"set phy config ret =  %pe last_status =  %s\n",
7551 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
7552 		return err;
7553 	}
7554 
7555 	/* Update the link info */
7556 	err = i40e_update_link_info(hw);
7557 	if (err) {
7558 		/* Wait a little bit (on 40G cards it sometimes takes a really
7559 		 * long time for link to come back from the atomic reset)
7560 		 * and try once more
7561 		 */
7562 		msleep(1000);
7563 		i40e_update_link_info(hw);
7564 	}
7565 
7566 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7567 
7568 	return 0;
7569 }
7570 
7571 /**
7572  * i40e_up - Bring the connection back up after being down
7573  * @vsi: the VSI being configured
7574  **/
7575 int i40e_up(struct i40e_vsi *vsi)
7576 {
7577 	int err;
7578 
7579 	if (vsi->type == I40E_VSI_MAIN &&
7580 	    (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7581 	     test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7582 		i40e_force_link_state(vsi->back, true);
7583 
7584 	err = i40e_vsi_configure(vsi);
7585 	if (!err)
7586 		err = i40e_up_complete(vsi);
7587 
7588 	return err;
7589 }
7590 
7591 /**
7592  * i40e_down - Shutdown the connection processing
7593  * @vsi: the VSI being stopped
7594  **/
7595 void i40e_down(struct i40e_vsi *vsi)
7596 {
7597 	int i;
7598 
7599 	/* It is assumed that the caller of this function
7600 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7601 	 */
7602 	if (vsi->netdev) {
7603 		netif_carrier_off(vsi->netdev);
7604 		netif_tx_disable(vsi->netdev);
7605 	}
7606 	i40e_vsi_disable_irq(vsi);
7607 	i40e_vsi_stop_rings(vsi);
7608 	if (vsi->type == I40E_VSI_MAIN &&
7609 	   (test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, vsi->back->flags) ||
7610 	    test_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, vsi->back->flags)))
7611 		i40e_force_link_state(vsi->back, false);
7612 	i40e_napi_disable_all(vsi);
7613 
7614 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7615 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7616 		if (i40e_enabled_xdp_vsi(vsi)) {
7617 			/* Make sure that in-progress ndo_xdp_xmit and
7618 			 * ndo_xsk_wakeup calls are completed.
7619 			 */
7620 			synchronize_rcu();
7621 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7622 		}
7623 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7624 	}
7625 
7626 }
7627 
7628 /**
7629  * i40e_validate_mqprio_qopt- validate queue mapping info
7630  * @vsi: the VSI being configured
7631  * @mqprio_qopt: queue parametrs
7632  **/
7633 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7634 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7635 {
7636 	u64 sum_max_rate = 0;
7637 	u64 max_rate = 0;
7638 	int i;
7639 
7640 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7641 	    mqprio_qopt->qopt.num_tc < 1 ||
7642 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7643 		return -EINVAL;
7644 	for (i = 0; ; i++) {
7645 		if (!mqprio_qopt->qopt.count[i])
7646 			return -EINVAL;
7647 		if (mqprio_qopt->min_rate[i]) {
7648 			dev_err(&vsi->back->pdev->dev,
7649 				"Invalid min tx rate (greater than 0) specified\n");
7650 			return -EINVAL;
7651 		}
7652 		max_rate = mqprio_qopt->max_rate[i];
7653 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7654 		sum_max_rate += max_rate;
7655 
7656 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7657 			break;
7658 		if (mqprio_qopt->qopt.offset[i + 1] !=
7659 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7660 			return -EINVAL;
7661 	}
7662 	if (vsi->num_queue_pairs <
7663 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7664 		dev_err(&vsi->back->pdev->dev,
7665 			"Failed to create traffic channel, insufficient number of queues.\n");
7666 		return -EINVAL;
7667 	}
7668 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7669 		dev_err(&vsi->back->pdev->dev,
7670 			"Invalid max tx rate specified\n");
7671 		return -EINVAL;
7672 	}
7673 	return 0;
7674 }
7675 
7676 /**
7677  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7678  * @vsi: the VSI being configured
7679  **/
7680 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7681 {
7682 	u16 qcount;
7683 	int i;
7684 
7685 	/* Only TC0 is enabled */
7686 	vsi->tc_config.numtc = 1;
7687 	vsi->tc_config.enabled_tc = 1;
7688 	qcount = min_t(int, vsi->alloc_queue_pairs,
7689 		       i40e_pf_get_max_q_per_tc(vsi->back));
7690 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7691 		/* For the TC that is not enabled set the offset to default
7692 		 * queue and allocate one queue for the given TC.
7693 		 */
7694 		vsi->tc_config.tc_info[i].qoffset = 0;
7695 		if (i == 0)
7696 			vsi->tc_config.tc_info[i].qcount = qcount;
7697 		else
7698 			vsi->tc_config.tc_info[i].qcount = 1;
7699 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7700 	}
7701 }
7702 
7703 /**
7704  * i40e_del_macvlan_filter
7705  * @hw: pointer to the HW structure
7706  * @seid: seid of the channel VSI
7707  * @macaddr: the mac address to apply as a filter
7708  * @aq_err: store the admin Q error
7709  *
7710  * This function deletes a mac filter on the channel VSI which serves as the
7711  * macvlan. Returns 0 on success.
7712  **/
7713 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7714 				   const u8 *macaddr, int *aq_err)
7715 {
7716 	struct i40e_aqc_remove_macvlan_element_data element;
7717 	int status;
7718 
7719 	memset(&element, 0, sizeof(element));
7720 	ether_addr_copy(element.mac_addr, macaddr);
7721 	element.vlan_tag = 0;
7722 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7723 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7724 	*aq_err = hw->aq.asq_last_status;
7725 
7726 	return status;
7727 }
7728 
7729 /**
7730  * i40e_add_macvlan_filter
7731  * @hw: pointer to the HW structure
7732  * @seid: seid of the channel VSI
7733  * @macaddr: the mac address to apply as a filter
7734  * @aq_err: store the admin Q error
7735  *
7736  * This function adds a mac filter on the channel VSI which serves as the
7737  * macvlan. Returns 0 on success.
7738  **/
7739 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7740 				   const u8 *macaddr, int *aq_err)
7741 {
7742 	struct i40e_aqc_add_macvlan_element_data element;
7743 	u16 cmd_flags = 0;
7744 	int status;
7745 
7746 	ether_addr_copy(element.mac_addr, macaddr);
7747 	element.vlan_tag = 0;
7748 	element.queue_number = 0;
7749 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7750 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7751 	element.flags = cpu_to_le16(cmd_flags);
7752 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7753 	*aq_err = hw->aq.asq_last_status;
7754 
7755 	return status;
7756 }
7757 
7758 /**
7759  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7760  * @vsi: the VSI we want to access
7761  * @ch: the channel we want to access
7762  */
7763 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7764 {
7765 	struct i40e_ring *tx_ring, *rx_ring;
7766 	u16 pf_q;
7767 	int i;
7768 
7769 	for (i = 0; i < ch->num_queue_pairs; i++) {
7770 		pf_q = ch->base_queue + i;
7771 		tx_ring = vsi->tx_rings[pf_q];
7772 		tx_ring->ch = NULL;
7773 		rx_ring = vsi->rx_rings[pf_q];
7774 		rx_ring->ch = NULL;
7775 	}
7776 }
7777 
7778 /**
7779  * i40e_free_macvlan_channels
7780  * @vsi: the VSI we want to access
7781  *
7782  * This function frees the Qs of the channel VSI from
7783  * the stack and also deletes the channel VSIs which
7784  * serve as macvlans.
7785  */
7786 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7787 {
7788 	struct i40e_channel *ch, *ch_tmp;
7789 	int ret;
7790 
7791 	if (list_empty(&vsi->macvlan_list))
7792 		return;
7793 
7794 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7795 		struct i40e_vsi *parent_vsi;
7796 
7797 		if (i40e_is_channel_macvlan(ch)) {
7798 			i40e_reset_ch_rings(vsi, ch);
7799 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7800 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7801 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7802 			kfree(ch->fwd);
7803 			ch->fwd = NULL;
7804 		}
7805 
7806 		list_del(&ch->list);
7807 		parent_vsi = ch->parent_vsi;
7808 		if (!parent_vsi || !ch->initialized) {
7809 			kfree(ch);
7810 			continue;
7811 		}
7812 
7813 		/* remove the VSI */
7814 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7815 					     NULL);
7816 		if (ret)
7817 			dev_err(&vsi->back->pdev->dev,
7818 				"unable to remove channel (%d) for parent VSI(%d)\n",
7819 				ch->seid, parent_vsi->seid);
7820 		kfree(ch);
7821 	}
7822 	vsi->macvlan_cnt = 0;
7823 }
7824 
7825 /**
7826  * i40e_fwd_ring_up - bring the macvlan device up
7827  * @vsi: the VSI we want to access
7828  * @vdev: macvlan netdevice
7829  * @fwd: the private fwd structure
7830  */
7831 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7832 			    struct i40e_fwd_adapter *fwd)
7833 {
7834 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7835 	int ret = 0, num_tc = 1,  i, aq_err;
7836 	struct i40e_pf *pf = vsi->back;
7837 	struct i40e_hw *hw = &pf->hw;
7838 
7839 	/* Go through the list and find an available channel */
7840 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7841 		if (!i40e_is_channel_macvlan(iter)) {
7842 			iter->fwd = fwd;
7843 			/* record configuration for macvlan interface in vdev */
7844 			for (i = 0; i < num_tc; i++)
7845 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7846 							     i,
7847 							     iter->num_queue_pairs,
7848 							     iter->base_queue);
7849 			for (i = 0; i < iter->num_queue_pairs; i++) {
7850 				struct i40e_ring *tx_ring, *rx_ring;
7851 				u16 pf_q;
7852 
7853 				pf_q = iter->base_queue + i;
7854 
7855 				/* Get to TX ring ptr */
7856 				tx_ring = vsi->tx_rings[pf_q];
7857 				tx_ring->ch = iter;
7858 
7859 				/* Get the RX ring ptr */
7860 				rx_ring = vsi->rx_rings[pf_q];
7861 				rx_ring->ch = iter;
7862 			}
7863 			ch = iter;
7864 			break;
7865 		}
7866 	}
7867 
7868 	if (!ch)
7869 		return -EINVAL;
7870 
7871 	/* Guarantee all rings are updated before we update the
7872 	 * MAC address filter.
7873 	 */
7874 	wmb();
7875 
7876 	/* Add a mac filter */
7877 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7878 	if (ret) {
7879 		/* if we cannot add the MAC rule then disable the offload */
7880 		macvlan_release_l2fw_offload(vdev);
7881 		for (i = 0; i < ch->num_queue_pairs; i++) {
7882 			struct i40e_ring *rx_ring;
7883 			u16 pf_q;
7884 
7885 			pf_q = ch->base_queue + i;
7886 			rx_ring = vsi->rx_rings[pf_q];
7887 			rx_ring->netdev = NULL;
7888 		}
7889 		dev_info(&pf->pdev->dev,
7890 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7891 			  ERR_PTR(ret), libie_aq_str(aq_err));
7892 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7893 	}
7894 
7895 	return ret;
7896 }
7897 
7898 /**
7899  * i40e_setup_macvlans - create the channels which will be macvlans
7900  * @vsi: the VSI we want to access
7901  * @macvlan_cnt: no. of macvlans to be setup
7902  * @qcnt: no. of Qs per macvlan
7903  * @vdev: macvlan netdevice
7904  */
7905 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7906 			       struct net_device *vdev)
7907 {
7908 	struct i40e_pf *pf = vsi->back;
7909 	struct i40e_hw *hw = &pf->hw;
7910 	struct i40e_vsi_context ctxt;
7911 	u16 sections, qmap, num_qps;
7912 	struct i40e_channel *ch;
7913 	int i, pow, ret = 0;
7914 	u8 offset = 0;
7915 
7916 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7917 		return -EINVAL;
7918 
7919 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7920 
7921 	/* find the next higher power-of-2 of num queue pairs */
7922 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7923 
7924 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7925 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7926 
7927 	/* Setup context bits for the main VSI */
7928 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7929 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7930 	memset(&ctxt, 0, sizeof(ctxt));
7931 	ctxt.seid = vsi->seid;
7932 	ctxt.pf_num = vsi->back->hw.pf_id;
7933 	ctxt.vf_num = 0;
7934 	ctxt.uplink_seid = vsi->uplink_seid;
7935 	ctxt.info = vsi->info;
7936 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7937 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7938 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7939 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7940 
7941 	/* Reconfigure RSS for main VSI with new max queue count */
7942 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7943 	ret = i40e_vsi_config_rss(vsi);
7944 	if (ret) {
7945 		dev_info(&pf->pdev->dev,
7946 			 "Failed to reconfig RSS for num_queues (%u)\n",
7947 			 vsi->rss_size);
7948 		return ret;
7949 	}
7950 	vsi->reconfig_rss = true;
7951 	dev_dbg(&vsi->back->pdev->dev,
7952 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7953 	vsi->next_base_queue = num_qps;
7954 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7955 
7956 	/* Update the VSI after updating the VSI queue-mapping
7957 	 * information
7958 	 */
7959 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7960 	if (ret) {
7961 		dev_info(&pf->pdev->dev,
7962 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7963 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
7964 		return ret;
7965 	}
7966 	/* update the local VSI info with updated queue map */
7967 	i40e_vsi_update_queue_map(vsi, &ctxt);
7968 	vsi->info.valid_sections = 0;
7969 
7970 	/* Create channels for macvlans */
7971 	INIT_LIST_HEAD(&vsi->macvlan_list);
7972 	for (i = 0; i < macvlan_cnt; i++) {
7973 		ch = kzalloc_obj(*ch);
7974 		if (!ch) {
7975 			ret = -ENOMEM;
7976 			goto err_free;
7977 		}
7978 		INIT_LIST_HEAD(&ch->list);
7979 		ch->num_queue_pairs = qcnt;
7980 		if (!i40e_setup_channel(pf, vsi, ch)) {
7981 			ret = -EINVAL;
7982 			kfree(ch);
7983 			goto err_free;
7984 		}
7985 		ch->parent_vsi = vsi;
7986 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7987 		vsi->macvlan_cnt++;
7988 		list_add_tail(&ch->list, &vsi->macvlan_list);
7989 	}
7990 
7991 	return ret;
7992 
7993 err_free:
7994 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7995 	i40e_free_macvlan_channels(vsi);
7996 
7997 	return ret;
7998 }
7999 
8000 /**
8001  * i40e_fwd_add - configure macvlans
8002  * @netdev: net device to configure
8003  * @vdev: macvlan netdevice
8004  **/
8005 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
8006 {
8007 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8008 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
8009 	struct i40e_vsi *vsi = np->vsi;
8010 	struct i40e_pf *pf = vsi->back;
8011 	struct i40e_fwd_adapter *fwd;
8012 	int avail_macvlan, ret;
8013 
8014 	if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8015 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
8016 		return ERR_PTR(-EINVAL);
8017 	}
8018 	if (i40e_is_tc_mqprio_enabled(pf)) {
8019 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
8020 		return ERR_PTR(-EINVAL);
8021 	}
8022 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
8023 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
8024 		return ERR_PTR(-EINVAL);
8025 	}
8026 
8027 	/* The macvlan device has to be a single Q device so that the
8028 	 * tc_to_txq field can be reused to pick the tx queue.
8029 	 */
8030 	if (netif_is_multiqueue(vdev))
8031 		return ERR_PTR(-ERANGE);
8032 
8033 	if (!vsi->macvlan_cnt) {
8034 		/* reserve bit 0 for the pf device */
8035 		set_bit(0, vsi->fwd_bitmask);
8036 
8037 		/* Try to reserve as many queues as possible for macvlans. First
8038 		 * reserve 3/4th of max vectors, then half, then quarter and
8039 		 * calculate Qs per macvlan as you go
8040 		 */
8041 		vectors = pf->num_lan_msix;
8042 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
8043 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8044 			q_per_macvlan = 4;
8045 			macvlan_cnt = (vectors - 32) / 4;
8046 		} else if (vectors <= 64 && vectors > 32) {
8047 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8048 			q_per_macvlan = 2;
8049 			macvlan_cnt = (vectors - 16) / 2;
8050 		} else if (vectors <= 32 && vectors > 16) {
8051 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8052 			q_per_macvlan = 1;
8053 			macvlan_cnt = vectors - 16;
8054 		} else if (vectors <= 16 && vectors > 8) {
8055 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8056 			q_per_macvlan = 1;
8057 			macvlan_cnt = vectors - 8;
8058 		} else {
8059 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8060 			q_per_macvlan = 1;
8061 			macvlan_cnt = vectors - 1;
8062 		}
8063 
8064 		if (macvlan_cnt == 0)
8065 			return ERR_PTR(-EBUSY);
8066 
8067 		/* Quiesce VSI queues */
8068 		i40e_quiesce_vsi(vsi);
8069 
8070 		/* sets up the macvlans but does not "enable" them */
8071 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8072 					  vdev);
8073 		if (ret)
8074 			return ERR_PTR(ret);
8075 
8076 		/* Unquiesce VSI */
8077 		i40e_unquiesce_vsi(vsi);
8078 	}
8079 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8080 					    vsi->macvlan_cnt);
8081 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8082 		return ERR_PTR(-EBUSY);
8083 
8084 	/* create the fwd struct */
8085 	fwd = kzalloc_obj(*fwd);
8086 	if (!fwd)
8087 		return ERR_PTR(-ENOMEM);
8088 
8089 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8090 	fwd->bit_no = avail_macvlan;
8091 	netdev_set_sb_channel(vdev, avail_macvlan);
8092 	fwd->netdev = vdev;
8093 
8094 	if (!netif_running(netdev))
8095 		return fwd;
8096 
8097 	/* Set fwd ring up */
8098 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8099 	if (ret) {
8100 		/* unbind the queues and drop the subordinate channel config */
8101 		netdev_unbind_sb_channel(netdev, vdev);
8102 		netdev_set_sb_channel(vdev, 0);
8103 
8104 		kfree(fwd);
8105 		return ERR_PTR(-EINVAL);
8106 	}
8107 
8108 	return fwd;
8109 }
8110 
8111 /**
8112  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8113  * @vsi: the VSI we want to access
8114  */
8115 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8116 {
8117 	struct i40e_channel *ch, *ch_tmp;
8118 	struct i40e_pf *pf = vsi->back;
8119 	struct i40e_hw *hw = &pf->hw;
8120 	int aq_err, ret = 0;
8121 
8122 	if (list_empty(&vsi->macvlan_list))
8123 		return;
8124 
8125 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8126 		if (i40e_is_channel_macvlan(ch)) {
8127 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8128 						      i40e_channel_mac(ch),
8129 						      &aq_err);
8130 			if (!ret) {
8131 				/* Reset queue contexts */
8132 				i40e_reset_ch_rings(vsi, ch);
8133 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8134 				netdev_unbind_sb_channel(vsi->netdev,
8135 							 ch->fwd->netdev);
8136 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8137 				kfree(ch->fwd);
8138 				ch->fwd = NULL;
8139 			}
8140 		}
8141 	}
8142 }
8143 
8144 /**
8145  * i40e_fwd_del - delete macvlan interfaces
8146  * @netdev: net device to configure
8147  * @vdev: macvlan netdevice
8148  */
8149 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8150 {
8151 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8152 	struct i40e_fwd_adapter *fwd = vdev;
8153 	struct i40e_channel *ch, *ch_tmp;
8154 	struct i40e_vsi *vsi = np->vsi;
8155 	struct i40e_pf *pf = vsi->back;
8156 	struct i40e_hw *hw = &pf->hw;
8157 	int aq_err, ret = 0;
8158 
8159 	/* Find the channel associated with the macvlan and del mac filter */
8160 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8161 		if (i40e_is_channel_macvlan(ch) &&
8162 		    ether_addr_equal(i40e_channel_mac(ch),
8163 				     fwd->netdev->dev_addr)) {
8164 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8165 						      i40e_channel_mac(ch),
8166 						      &aq_err);
8167 			if (!ret) {
8168 				/* Reset queue contexts */
8169 				i40e_reset_ch_rings(vsi, ch);
8170 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8171 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8172 				netdev_set_sb_channel(fwd->netdev, 0);
8173 				kfree(ch->fwd);
8174 				ch->fwd = NULL;
8175 			} else {
8176 				dev_info(&pf->pdev->dev,
8177 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8178 					  ERR_PTR(ret), libie_aq_str(aq_err));
8179 			}
8180 			break;
8181 		}
8182 	}
8183 }
8184 
8185 /**
8186  * i40e_setup_tc - configure multiple traffic classes
8187  * @netdev: net device to configure
8188  * @type_data: tc offload data
8189  **/
8190 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8191 {
8192 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8193 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8194 	struct i40e_vsi *vsi = np->vsi;
8195 	struct i40e_pf *pf = vsi->back;
8196 	u8 enabled_tc = 0, num_tc, hw;
8197 	bool need_reset = false;
8198 	int old_queue_pairs;
8199 	int ret = -EINVAL;
8200 	u16 mode;
8201 	int i;
8202 
8203 	old_queue_pairs = vsi->num_queue_pairs;
8204 	num_tc = mqprio_qopt->qopt.num_tc;
8205 	hw = mqprio_qopt->qopt.hw;
8206 	mode = mqprio_qopt->mode;
8207 	if (!hw) {
8208 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8209 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8210 		goto config_tc;
8211 	}
8212 
8213 	/* Check if MFP enabled */
8214 	if (test_bit(I40E_FLAG_MFP_ENA, pf->flags)) {
8215 		netdev_info(netdev,
8216 			    "Configuring TC not supported in MFP mode\n");
8217 		return ret;
8218 	}
8219 	switch (mode) {
8220 	case TC_MQPRIO_MODE_DCB:
8221 		clear_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8222 
8223 		/* Check if DCB enabled to continue */
8224 		if (!test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8225 			netdev_info(netdev,
8226 				    "DCB is not enabled for adapter\n");
8227 			return ret;
8228 		}
8229 
8230 		/* Check whether tc count is within enabled limit */
8231 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8232 			netdev_info(netdev,
8233 				    "TC count greater than enabled on link for adapter\n");
8234 			return ret;
8235 		}
8236 		break;
8237 	case TC_MQPRIO_MODE_CHANNEL:
8238 		if (test_bit(I40E_FLAG_DCB_ENA, pf->flags)) {
8239 			netdev_info(netdev,
8240 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8241 			return ret;
8242 		}
8243 		if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
8244 			return ret;
8245 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8246 		if (ret)
8247 			return ret;
8248 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8249 		       sizeof(*mqprio_qopt));
8250 		set_bit(I40E_FLAG_TC_MQPRIO_ENA, pf->flags);
8251 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
8252 		break;
8253 	default:
8254 		return -EINVAL;
8255 	}
8256 
8257 config_tc:
8258 	/* Generate TC map for number of tc requested */
8259 	for (i = 0; i < num_tc; i++)
8260 		enabled_tc |= BIT(i);
8261 
8262 	/* Requesting same TC configuration as already enabled */
8263 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8264 	    mode != TC_MQPRIO_MODE_CHANNEL)
8265 		return 0;
8266 
8267 	/* Quiesce VSI queues */
8268 	i40e_quiesce_vsi(vsi);
8269 
8270 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8271 		i40e_remove_queue_channels(vsi);
8272 
8273 	/* Configure VSI for enabled TCs */
8274 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8275 	if (ret) {
8276 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8277 			    vsi->seid);
8278 		need_reset = true;
8279 		goto exit;
8280 	} else if (enabled_tc &&
8281 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8282 		netdev_info(netdev,
8283 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8284 			    vsi->tc_config.tc_info[0].qcount);
8285 		ret = -EINVAL;
8286 		need_reset = true;
8287 		goto exit;
8288 	}
8289 
8290 	dev_info(&vsi->back->pdev->dev,
8291 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8292 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8293 
8294 	if (i40e_is_tc_mqprio_enabled(pf)) {
8295 		if (vsi->mqprio_qopt.max_rate[0]) {
8296 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8297 						  vsi->mqprio_qopt.max_rate[0]);
8298 
8299 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8300 			if (!ret) {
8301 				u64 credits = max_tx_rate;
8302 
8303 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8304 				dev_dbg(&vsi->back->pdev->dev,
8305 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8306 					max_tx_rate,
8307 					credits,
8308 					vsi->seid);
8309 			} else {
8310 				need_reset = true;
8311 				goto exit;
8312 			}
8313 		}
8314 		ret = i40e_configure_queue_channels(vsi);
8315 		if (ret) {
8316 			vsi->num_queue_pairs = old_queue_pairs;
8317 			netdev_info(netdev,
8318 				    "Failed configuring queue channels\n");
8319 			need_reset = true;
8320 			goto exit;
8321 		}
8322 	}
8323 
8324 exit:
8325 	/* Reset the configuration data to defaults, only TC0 is enabled */
8326 	if (need_reset) {
8327 		i40e_vsi_set_default_tc_config(vsi);
8328 		need_reset = false;
8329 	}
8330 
8331 	/* Unquiesce VSI */
8332 	i40e_unquiesce_vsi(vsi);
8333 	return ret;
8334 }
8335 
8336 /**
8337  * i40e_set_cld_element - sets cloud filter element data
8338  * @filter: cloud filter rule
8339  * @cld: ptr to cloud filter element data
8340  *
8341  * This is helper function to copy data into cloud filter element
8342  **/
8343 static inline void
8344 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8345 		     struct i40e_aqc_cloud_filters_element_data *cld)
8346 {
8347 	u32 ipa;
8348 	int i;
8349 
8350 	memset(cld, 0, sizeof(*cld));
8351 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8352 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8353 
8354 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8355 		return;
8356 
8357 	if (filter->n_proto == ETH_P_IPV6) {
8358 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8359 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8360 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8361 
8362 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8363 		}
8364 	} else {
8365 		ipa = be32_to_cpu(filter->dst_ipv4);
8366 
8367 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8368 	}
8369 
8370 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8371 
8372 	/* tenant_id is not supported by FW now, once the support is enabled
8373 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8374 	 */
8375 	if (filter->tenant_id)
8376 		return;
8377 }
8378 
8379 /**
8380  * i40e_add_del_cloud_filter - Add/del cloud filter
8381  * @vsi: pointer to VSI
8382  * @filter: cloud filter rule
8383  * @add: if true, add, if false, delete
8384  *
8385  * Add or delete a cloud filter for a specific flow spec.
8386  * Returns 0 if the filter were successfully added.
8387  **/
8388 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8389 			      struct i40e_cloud_filter *filter, bool add)
8390 {
8391 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8392 	struct i40e_pf *pf = vsi->back;
8393 	int ret;
8394 	static const u16 flag_table[128] = {
8395 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8396 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8397 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8398 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8399 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8400 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8401 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8402 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8403 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8404 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8405 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8406 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8407 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8408 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8409 	};
8410 
8411 	if (filter->flags >= ARRAY_SIZE(flag_table))
8412 		return -EIO;
8413 
8414 	memset(&cld_filter, 0, sizeof(cld_filter));
8415 
8416 	/* copy element needed to add cloud filter from filter */
8417 	i40e_set_cld_element(filter, &cld_filter);
8418 
8419 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8420 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8421 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8422 
8423 	if (filter->n_proto == ETH_P_IPV6)
8424 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8425 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8426 	else
8427 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8428 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8429 
8430 	if (add)
8431 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8432 						&cld_filter, 1);
8433 	else
8434 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8435 						&cld_filter, 1);
8436 	if (ret)
8437 		dev_dbg(&pf->pdev->dev,
8438 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8439 			add ? "add" : "delete", filter->dst_port, ret,
8440 			pf->hw.aq.asq_last_status);
8441 	else
8442 		dev_info(&pf->pdev->dev,
8443 			 "%s cloud filter for VSI: %d\n",
8444 			 add ? "Added" : "Deleted", filter->seid);
8445 	return ret;
8446 }
8447 
8448 /**
8449  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8450  * @vsi: pointer to VSI
8451  * @filter: cloud filter rule
8452  * @add: if true, add, if false, delete
8453  *
8454  * Add or delete a cloud filter for a specific flow spec using big buffer.
8455  * Returns 0 if the filter were successfully added.
8456  **/
8457 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8458 				      struct i40e_cloud_filter *filter,
8459 				      bool add)
8460 {
8461 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8462 	struct i40e_pf *pf = vsi->back;
8463 	int ret;
8464 
8465 	/* Both (src/dst) valid mac_addr are not supported */
8466 	if ((is_valid_ether_addr(filter->dst_mac) &&
8467 	     is_valid_ether_addr(filter->src_mac)) ||
8468 	    (is_multicast_ether_addr(filter->dst_mac) &&
8469 	     is_multicast_ether_addr(filter->src_mac)))
8470 		return -EOPNOTSUPP;
8471 
8472 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8473 	 * ports are not supported via big buffer now.
8474 	 */
8475 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8476 		return -EOPNOTSUPP;
8477 
8478 	/* adding filter using src_port/src_ip is not supported at this stage */
8479 	if (filter->src_port ||
8480 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8481 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8482 		return -EOPNOTSUPP;
8483 
8484 	memset(&cld_filter, 0, sizeof(cld_filter));
8485 
8486 	/* copy element needed to add cloud filter from filter */
8487 	i40e_set_cld_element(filter, &cld_filter.element);
8488 
8489 	if (is_valid_ether_addr(filter->dst_mac) ||
8490 	    is_valid_ether_addr(filter->src_mac) ||
8491 	    is_multicast_ether_addr(filter->dst_mac) ||
8492 	    is_multicast_ether_addr(filter->src_mac)) {
8493 		/* MAC + IP : unsupported mode */
8494 		if (filter->dst_ipv4)
8495 			return -EOPNOTSUPP;
8496 
8497 		/* since we validated that L4 port must be valid before
8498 		 * we get here, start with respective "flags" value
8499 		 * and update if vlan is present or not
8500 		 */
8501 		cld_filter.element.flags =
8502 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8503 
8504 		if (filter->vlan_id) {
8505 			cld_filter.element.flags =
8506 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8507 		}
8508 
8509 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8510 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8511 		cld_filter.element.flags =
8512 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8513 		if (filter->n_proto == ETH_P_IPV6)
8514 			cld_filter.element.flags |=
8515 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8516 		else
8517 			cld_filter.element.flags |=
8518 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8519 	} else {
8520 		dev_err(&pf->pdev->dev,
8521 			"either mac or ip has to be valid for cloud filter\n");
8522 		return -EINVAL;
8523 	}
8524 
8525 	/* Now copy L4 port in Byte 6..7 in general fields */
8526 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8527 						be16_to_cpu(filter->dst_port);
8528 
8529 	if (add) {
8530 		/* Validate current device switch mode, change if necessary */
8531 		ret = i40e_validate_and_set_switch_mode(vsi);
8532 		if (ret) {
8533 			dev_err(&pf->pdev->dev,
8534 				"failed to set switch mode, ret %d\n",
8535 				ret);
8536 			return ret;
8537 		}
8538 
8539 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8540 						   &cld_filter, 1);
8541 	} else {
8542 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8543 						   &cld_filter, 1);
8544 	}
8545 
8546 	if (ret)
8547 		dev_dbg(&pf->pdev->dev,
8548 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8549 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8550 	else
8551 		dev_info(&pf->pdev->dev,
8552 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8553 			 add ? "add" : "delete", filter->seid,
8554 			 ntohs(filter->dst_port));
8555 	return ret;
8556 }
8557 
8558 /**
8559  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8560  * @vsi: Pointer to VSI
8561  * @f: Pointer to struct flow_cls_offload
8562  * @filter: Pointer to cloud filter structure
8563  *
8564  **/
8565 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8566 				 struct flow_cls_offload *f,
8567 				 struct i40e_cloud_filter *filter)
8568 {
8569 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8570 	struct flow_dissector *dissector = rule->match.dissector;
8571 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8572 	struct i40e_pf *pf = vsi->back;
8573 	u8 field_flags = 0;
8574 
8575 	if (dissector->used_keys &
8576 	    ~(BIT_ULL(FLOW_DISSECTOR_KEY_CONTROL) |
8577 	      BIT_ULL(FLOW_DISSECTOR_KEY_BASIC) |
8578 	      BIT_ULL(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8579 	      BIT_ULL(FLOW_DISSECTOR_KEY_VLAN) |
8580 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8581 	      BIT_ULL(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8582 	      BIT_ULL(FLOW_DISSECTOR_KEY_PORTS) |
8583 	      BIT_ULL(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8584 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%llx\n",
8585 			dissector->used_keys);
8586 		return -EOPNOTSUPP;
8587 	}
8588 
8589 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8590 		struct flow_match_enc_keyid match;
8591 
8592 		flow_rule_match_enc_keyid(rule, &match);
8593 		if (match.mask->keyid != 0)
8594 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8595 
8596 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8597 	}
8598 
8599 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8600 		struct flow_match_basic match;
8601 
8602 		flow_rule_match_basic(rule, &match);
8603 		n_proto_key = ntohs(match.key->n_proto);
8604 		n_proto_mask = ntohs(match.mask->n_proto);
8605 
8606 		if (n_proto_key == ETH_P_ALL) {
8607 			n_proto_key = 0;
8608 			n_proto_mask = 0;
8609 		}
8610 		filter->n_proto = n_proto_key & n_proto_mask;
8611 		filter->ip_proto = match.key->ip_proto;
8612 	}
8613 
8614 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8615 		struct flow_match_eth_addrs match;
8616 
8617 		flow_rule_match_eth_addrs(rule, &match);
8618 
8619 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8620 		if (!is_zero_ether_addr(match.mask->dst)) {
8621 			if (is_broadcast_ether_addr(match.mask->dst)) {
8622 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8623 			} else {
8624 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8625 					match.mask->dst);
8626 				return -EIO;
8627 			}
8628 		}
8629 
8630 		if (!is_zero_ether_addr(match.mask->src)) {
8631 			if (is_broadcast_ether_addr(match.mask->src)) {
8632 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8633 			} else {
8634 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8635 					match.mask->src);
8636 				return -EIO;
8637 			}
8638 		}
8639 		ether_addr_copy(filter->dst_mac, match.key->dst);
8640 		ether_addr_copy(filter->src_mac, match.key->src);
8641 	}
8642 
8643 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8644 		struct flow_match_vlan match;
8645 
8646 		flow_rule_match_vlan(rule, &match);
8647 		if (match.mask->vlan_id) {
8648 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8649 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8650 
8651 			} else {
8652 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8653 					match.mask->vlan_id);
8654 				return -EIO;
8655 			}
8656 		}
8657 
8658 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8659 	}
8660 
8661 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8662 		struct flow_match_control match;
8663 
8664 		flow_rule_match_control(rule, &match);
8665 		addr_type = match.key->addr_type;
8666 
8667 		if (flow_rule_has_control_flags(match.mask->flags,
8668 						f->common.extack))
8669 			return -EOPNOTSUPP;
8670 	}
8671 
8672 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8673 		struct flow_match_ipv4_addrs match;
8674 
8675 		flow_rule_match_ipv4_addrs(rule, &match);
8676 		if (match.mask->dst) {
8677 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8678 				field_flags |= I40E_CLOUD_FIELD_IIP;
8679 			} else {
8680 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8681 					&match.mask->dst);
8682 				return -EIO;
8683 			}
8684 		}
8685 
8686 		if (match.mask->src) {
8687 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8688 				field_flags |= I40E_CLOUD_FIELD_IIP;
8689 			} else {
8690 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8691 					&match.mask->src);
8692 				return -EIO;
8693 			}
8694 		}
8695 
8696 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8697 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8698 			return -EIO;
8699 		}
8700 		filter->dst_ipv4 = match.key->dst;
8701 		filter->src_ipv4 = match.key->src;
8702 	}
8703 
8704 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8705 		struct flow_match_ipv6_addrs match;
8706 
8707 		flow_rule_match_ipv6_addrs(rule, &match);
8708 
8709 		/* src and dest IPV6 address should not be LOOPBACK
8710 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8711 		 */
8712 		if (ipv6_addr_loopback(&match.key->dst) ||
8713 		    ipv6_addr_loopback(&match.key->src)) {
8714 			dev_err(&pf->pdev->dev,
8715 				"Bad ipv6, addr is LOOPBACK\n");
8716 			return -EIO;
8717 		}
8718 		if (!ipv6_addr_any(&match.mask->dst) ||
8719 		    !ipv6_addr_any(&match.mask->src))
8720 			field_flags |= I40E_CLOUD_FIELD_IIP;
8721 
8722 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8723 		       sizeof(filter->src_ipv6));
8724 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8725 		       sizeof(filter->dst_ipv6));
8726 	}
8727 
8728 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8729 		struct flow_match_ports match;
8730 
8731 		flow_rule_match_ports(rule, &match);
8732 		if (match.mask->src) {
8733 			if (match.mask->src == cpu_to_be16(0xffff)) {
8734 				field_flags |= I40E_CLOUD_FIELD_IIP;
8735 			} else {
8736 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8737 					be16_to_cpu(match.mask->src));
8738 				return -EIO;
8739 			}
8740 		}
8741 
8742 		if (match.mask->dst) {
8743 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8744 				field_flags |= I40E_CLOUD_FIELD_IIP;
8745 			} else {
8746 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8747 					be16_to_cpu(match.mask->dst));
8748 				return -EIO;
8749 			}
8750 		}
8751 
8752 		filter->dst_port = match.key->dst;
8753 		filter->src_port = match.key->src;
8754 
8755 		switch (filter->ip_proto) {
8756 		case IPPROTO_TCP:
8757 		case IPPROTO_UDP:
8758 			break;
8759 		default:
8760 			dev_err(&pf->pdev->dev,
8761 				"Only UDP and TCP transport are supported\n");
8762 			return -EINVAL;
8763 		}
8764 	}
8765 	filter->flags = field_flags;
8766 	return 0;
8767 }
8768 
8769 /**
8770  * i40e_handle_tclass: Forward to a traffic class on the device
8771  * @vsi: Pointer to VSI
8772  * @tc: traffic class index on the device
8773  * @filter: Pointer to cloud filter structure
8774  *
8775  **/
8776 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8777 			      struct i40e_cloud_filter *filter)
8778 {
8779 	struct i40e_channel *ch, *ch_tmp;
8780 
8781 	/* direct to a traffic class on the same device */
8782 	if (tc == 0) {
8783 		filter->seid = vsi->seid;
8784 		return 0;
8785 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8786 		if (!filter->dst_port) {
8787 			dev_err(&vsi->back->pdev->dev,
8788 				"Specify destination port to direct to traffic class that is not default\n");
8789 			return -EINVAL;
8790 		}
8791 		if (list_empty(&vsi->ch_list))
8792 			return -EINVAL;
8793 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8794 					 list) {
8795 			if (ch->seid == vsi->tc_seid_map[tc])
8796 				filter->seid = ch->seid;
8797 		}
8798 		return 0;
8799 	}
8800 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8801 	return -EINVAL;
8802 }
8803 
8804 /**
8805  * i40e_configure_clsflower - Configure tc flower filters
8806  * @vsi: Pointer to VSI
8807  * @cls_flower: Pointer to struct flow_cls_offload
8808  *
8809  **/
8810 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8811 				    struct flow_cls_offload *cls_flower)
8812 {
8813 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8814 	struct i40e_cloud_filter *filter = NULL;
8815 	struct i40e_pf *pf = vsi->back;
8816 	int err = 0;
8817 
8818 	if (tc < 0) {
8819 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8820 		return -EOPNOTSUPP;
8821 	}
8822 
8823 	if (!tc) {
8824 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8825 		return -EINVAL;
8826 	}
8827 
8828 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8829 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8830 		return -EBUSY;
8831 
8832 	if (pf->fdir_pf_active_filters ||
8833 	    (!hlist_empty(&pf->fdir_filter_list))) {
8834 		dev_err(&vsi->back->pdev->dev,
8835 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8836 		return -EINVAL;
8837 	}
8838 
8839 	if (test_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags)) {
8840 		dev_err(&vsi->back->pdev->dev,
8841 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8842 		clear_bit(I40E_FLAG_FD_SB_ENA, vsi->back->flags);
8843 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, vsi->back->flags);
8844 	}
8845 
8846 	filter = kzalloc_obj(*filter);
8847 	if (!filter)
8848 		return -ENOMEM;
8849 
8850 	filter->cookie = cls_flower->cookie;
8851 
8852 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8853 	if (err < 0)
8854 		goto err;
8855 
8856 	err = i40e_handle_tclass(vsi, tc, filter);
8857 	if (err < 0)
8858 		goto err;
8859 
8860 	/* Add cloud filter */
8861 	if (filter->dst_port)
8862 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8863 	else
8864 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8865 
8866 	if (err) {
8867 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8868 			err);
8869 		goto err;
8870 	}
8871 
8872 	/* add filter to the ordered list */
8873 	INIT_HLIST_NODE(&filter->cloud_node);
8874 
8875 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8876 
8877 	pf->num_cloud_filters++;
8878 
8879 	return err;
8880 err:
8881 	kfree(filter);
8882 	return err;
8883 }
8884 
8885 /**
8886  * i40e_find_cloud_filter - Find the could filter in the list
8887  * @vsi: Pointer to VSI
8888  * @cookie: filter specific cookie
8889  *
8890  **/
8891 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8892 							unsigned long *cookie)
8893 {
8894 	struct i40e_cloud_filter *filter = NULL;
8895 	struct hlist_node *node2;
8896 
8897 	hlist_for_each_entry_safe(filter, node2,
8898 				  &vsi->back->cloud_filter_list, cloud_node)
8899 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8900 			return filter;
8901 	return NULL;
8902 }
8903 
8904 /**
8905  * i40e_delete_clsflower - Remove tc flower filters
8906  * @vsi: Pointer to VSI
8907  * @cls_flower: Pointer to struct flow_cls_offload
8908  *
8909  **/
8910 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8911 				 struct flow_cls_offload *cls_flower)
8912 {
8913 	struct i40e_cloud_filter *filter = NULL;
8914 	struct i40e_pf *pf = vsi->back;
8915 	int err = 0;
8916 
8917 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8918 
8919 	if (!filter)
8920 		return -EINVAL;
8921 
8922 	hash_del(&filter->cloud_node);
8923 
8924 	if (filter->dst_port)
8925 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8926 	else
8927 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8928 
8929 	kfree(filter);
8930 	if (err) {
8931 		dev_err(&pf->pdev->dev,
8932 			"Failed to delete cloud filter, err %pe\n",
8933 			ERR_PTR(err));
8934 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8935 	}
8936 
8937 	pf->num_cloud_filters--;
8938 	if (!pf->num_cloud_filters)
8939 		if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
8940 		    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
8941 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
8942 			clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
8943 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
8944 		}
8945 	return 0;
8946 }
8947 
8948 /**
8949  * i40e_setup_tc_cls_flower - flower classifier offloads
8950  * @np: net device to configure
8951  * @cls_flower: offload data
8952  **/
8953 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8954 				    struct flow_cls_offload *cls_flower)
8955 {
8956 	struct i40e_vsi *vsi = np->vsi;
8957 
8958 	switch (cls_flower->command) {
8959 	case FLOW_CLS_REPLACE:
8960 		return i40e_configure_clsflower(vsi, cls_flower);
8961 	case FLOW_CLS_DESTROY:
8962 		return i40e_delete_clsflower(vsi, cls_flower);
8963 	case FLOW_CLS_STATS:
8964 		return -EOPNOTSUPP;
8965 	default:
8966 		return -EOPNOTSUPP;
8967 	}
8968 }
8969 
8970 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8971 				  void *cb_priv)
8972 {
8973 	struct i40e_netdev_priv *np = cb_priv;
8974 
8975 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8976 		return -EOPNOTSUPP;
8977 
8978 	switch (type) {
8979 	case TC_SETUP_CLSFLOWER:
8980 		return i40e_setup_tc_cls_flower(np, type_data);
8981 
8982 	default:
8983 		return -EOPNOTSUPP;
8984 	}
8985 }
8986 
8987 static LIST_HEAD(i40e_block_cb_list);
8988 
8989 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8990 			   void *type_data)
8991 {
8992 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8993 
8994 	switch (type) {
8995 	case TC_SETUP_QDISC_MQPRIO:
8996 		return i40e_setup_tc(netdev, type_data);
8997 	case TC_SETUP_BLOCK:
8998 		return flow_block_cb_setup_simple(type_data,
8999 						  &i40e_block_cb_list,
9000 						  i40e_setup_tc_block_cb,
9001 						  np, np, true);
9002 	default:
9003 		return -EOPNOTSUPP;
9004 	}
9005 }
9006 
9007 /**
9008  * i40e_open - Called when a network interface is made active
9009  * @netdev: network interface device structure
9010  *
9011  * The open entry point is called when a network interface is made
9012  * active by the system (IFF_UP).  At this point all resources needed
9013  * for transmit and receive operations are allocated, the interrupt
9014  * handler is registered with the OS, the netdev watchdog subtask is
9015  * enabled, and the stack is notified that the interface is ready.
9016  *
9017  * Returns 0 on success, negative value on failure
9018  **/
9019 int i40e_open(struct net_device *netdev)
9020 {
9021 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9022 	struct i40e_vsi *vsi = np->vsi;
9023 	struct i40e_pf *pf = vsi->back;
9024 	int err;
9025 
9026 	/* disallow open during test or if eeprom is broken */
9027 	if (test_bit(__I40E_TESTING, pf->state) ||
9028 	    test_bit(__I40E_BAD_EEPROM, pf->state))
9029 		return -EBUSY;
9030 
9031 	netif_carrier_off(netdev);
9032 
9033 	if (i40e_force_link_state(pf, true))
9034 		return -EAGAIN;
9035 
9036 	err = i40e_vsi_open(vsi);
9037 	if (err)
9038 		return err;
9039 
9040 	/* configure global TSO hardware offload settings */
9041 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
9042 						       TCP_FLAG_FIN) >> 16);
9043 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
9044 						       TCP_FLAG_FIN |
9045 						       TCP_FLAG_CWR) >> 16);
9046 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9047 
9048 	return 0;
9049 }
9050 
9051 /**
9052  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9053  * @vsi: vsi structure
9054  *
9055  * This updates netdev's number of tx/rx queues
9056  *
9057  * Returns status of setting tx/rx queues
9058  **/
9059 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9060 {
9061 	int ret;
9062 
9063 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9064 					   vsi->num_queue_pairs);
9065 	if (ret)
9066 		return ret;
9067 
9068 	return netif_set_real_num_tx_queues(vsi->netdev,
9069 					    vsi->num_queue_pairs);
9070 }
9071 
9072 /**
9073  * i40e_vsi_open -
9074  * @vsi: the VSI to open
9075  *
9076  * Finish initialization of the VSI.
9077  *
9078  * Returns 0 on success, negative value on failure
9079  *
9080  * Note: expects to be called while under rtnl_lock()
9081  **/
9082 int i40e_vsi_open(struct i40e_vsi *vsi)
9083 {
9084 	struct i40e_pf *pf = vsi->back;
9085 	char int_name[I40E_INT_NAME_STR_LEN];
9086 	int err;
9087 
9088 	/* allocate descriptors */
9089 	err = i40e_vsi_setup_tx_resources(vsi);
9090 	if (err)
9091 		goto err_setup_tx;
9092 	err = i40e_vsi_setup_rx_resources(vsi);
9093 	if (err)
9094 		goto err_setup_rx;
9095 
9096 	err = i40e_vsi_configure(vsi);
9097 	if (err)
9098 		goto err_setup_rx;
9099 
9100 	if (vsi->netdev) {
9101 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9102 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9103 		err = i40e_vsi_request_irq(vsi, int_name);
9104 		if (err)
9105 			goto err_setup_rx;
9106 
9107 		/* Notify the stack of the actual queue counts. */
9108 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9109 		if (err)
9110 			goto err_set_queues;
9111 
9112 	} else if (vsi->type == I40E_VSI_FDIR) {
9113 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9114 			 dev_driver_string(&pf->pdev->dev),
9115 			 dev_name(&pf->pdev->dev));
9116 		err = i40e_vsi_request_irq(vsi, int_name);
9117 		if (err)
9118 			goto err_setup_rx;
9119 
9120 	} else {
9121 		err = -EINVAL;
9122 		goto err_setup_rx;
9123 	}
9124 
9125 	err = i40e_up_complete(vsi);
9126 	if (err)
9127 		goto err_up_complete;
9128 
9129 	return 0;
9130 
9131 err_up_complete:
9132 	i40e_down(vsi);
9133 err_set_queues:
9134 	i40e_vsi_free_irq(vsi);
9135 err_setup_rx:
9136 	i40e_vsi_free_rx_resources(vsi);
9137 err_setup_tx:
9138 	i40e_vsi_free_tx_resources(vsi);
9139 	if (vsi->type == I40E_VSI_MAIN)
9140 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9141 
9142 	return err;
9143 }
9144 
9145 /**
9146  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9147  * @pf: Pointer to PF
9148  *
9149  * This function destroys the hlist where all the Flow Director
9150  * filters were saved.
9151  **/
9152 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9153 {
9154 	struct i40e_fdir_filter *filter;
9155 	struct i40e_flex_pit *pit_entry, *tmp;
9156 	struct hlist_node *node2;
9157 
9158 	hlist_for_each_entry_safe(filter, node2,
9159 				  &pf->fdir_filter_list, fdir_node) {
9160 		hlist_del(&filter->fdir_node);
9161 		kfree(filter);
9162 	}
9163 
9164 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9165 		list_del(&pit_entry->list);
9166 		kfree(pit_entry);
9167 	}
9168 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9169 
9170 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9171 		list_del(&pit_entry->list);
9172 		kfree(pit_entry);
9173 	}
9174 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9175 
9176 	pf->fdir_pf_active_filters = 0;
9177 	i40e_reset_fdir_filter_cnt(pf);
9178 
9179 	/* Reprogram the default input set for TCP/IPv4 */
9180 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9181 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9182 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9183 
9184 	/* Reprogram the default input set for TCP/IPv6 */
9185 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_TCP,
9186 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9187 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9188 
9189 	/* Reprogram the default input set for UDP/IPv4 */
9190 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_UDP,
9191 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9192 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9193 
9194 	/* Reprogram the default input set for UDP/IPv6 */
9195 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_UDP,
9196 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9197 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9198 
9199 	/* Reprogram the default input set for SCTP/IPv4 */
9200 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_SCTP,
9201 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9202 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9203 
9204 	/* Reprogram the default input set for SCTP/IPv6 */
9205 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_SCTP,
9206 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9207 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9208 
9209 	/* Reprogram the default input set for Other/IPv4 */
9210 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_OTHER,
9211 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9212 
9213 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV4,
9214 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9215 
9216 	/* Reprogram the default input set for Other/IPv6 */
9217 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV6_OTHER,
9218 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9219 
9220 	i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_FRAG_IPV6,
9221 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9222 }
9223 
9224 /**
9225  * i40e_cloud_filter_exit - Cleans up the cloud filters
9226  * @pf: Pointer to PF
9227  *
9228  * This function destroys the hlist where all the cloud filters
9229  * were saved.
9230  **/
9231 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9232 {
9233 	struct i40e_cloud_filter *cfilter;
9234 	struct hlist_node *node;
9235 
9236 	hlist_for_each_entry_safe(cfilter, node,
9237 				  &pf->cloud_filter_list, cloud_node) {
9238 		hlist_del(&cfilter->cloud_node);
9239 		kfree(cfilter);
9240 	}
9241 	pf->num_cloud_filters = 0;
9242 
9243 	if (test_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags) &&
9244 	    !test_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags)) {
9245 		set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
9246 		clear_bit(I40E_FLAG_FD_SB_TO_CLOUD_FILTER, pf->flags);
9247 		clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
9248 	}
9249 }
9250 
9251 /**
9252  * i40e_close - Disables a network interface
9253  * @netdev: network interface device structure
9254  *
9255  * The close entry point is called when an interface is de-activated
9256  * by the OS.  The hardware is still under the driver's control, but
9257  * this netdev interface is disabled.
9258  *
9259  * Returns 0, this is not allowed to fail
9260  **/
9261 int i40e_close(struct net_device *netdev)
9262 {
9263 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9264 	struct i40e_vsi *vsi = np->vsi;
9265 
9266 	i40e_vsi_close(vsi);
9267 
9268 	return 0;
9269 }
9270 
9271 /**
9272  * i40e_do_reset - Start a PF or Core Reset sequence
9273  * @pf: board private structure
9274  * @reset_flags: which reset is requested
9275  * @lock_acquired: indicates whether or not the lock has been acquired
9276  * before this function was called.
9277  *
9278  * The essential difference in resets is that the PF Reset
9279  * doesn't clear the packet buffers, doesn't reset the PE
9280  * firmware, and doesn't bother the other PFs on the chip.
9281  **/
9282 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9283 {
9284 	struct i40e_vsi *vsi;
9285 	u32 val;
9286 	int i;
9287 
9288 	/* do the biggest reset indicated */
9289 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9290 
9291 		/* Request a Global Reset
9292 		 *
9293 		 * This will start the chip's countdown to the actual full
9294 		 * chip reset event, and a warning interrupt to be sent
9295 		 * to all PFs, including the requestor.  Our handler
9296 		 * for the warning interrupt will deal with the shutdown
9297 		 * and recovery of the switch setup.
9298 		 */
9299 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9300 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9301 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9302 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9303 
9304 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9305 
9306 		/* Request a Core Reset
9307 		 *
9308 		 * Same as Global Reset, except does *not* include the MAC/PHY
9309 		 */
9310 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9311 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9312 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9313 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9314 		i40e_flush(&pf->hw);
9315 
9316 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9317 
9318 		/* Request a PF Reset
9319 		 *
9320 		 * Resets only the PF-specific registers
9321 		 *
9322 		 * This goes directly to the tear-down and rebuild of
9323 		 * the switch, since we need to do all the recovery as
9324 		 * for the Core Reset.
9325 		 */
9326 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9327 		i40e_handle_reset_warning(pf, lock_acquired);
9328 
9329 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9330 		/* Request a PF Reset
9331 		 *
9332 		 * Resets PF and reinitializes PFs VSI.
9333 		 */
9334 		i40e_prep_for_reset(pf);
9335 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9336 		dev_info(&pf->pdev->dev,
9337 			 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
9338 			 "FW LLDP is disabled\n" :
9339 			 "FW LLDP is enabled\n");
9340 
9341 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9342 		/* Find the VSI(s) that requested a re-init */
9343 		dev_info(&pf->pdev->dev, "VSI reinit requested\n");
9344 
9345 		i40e_pf_for_each_vsi(pf, i, vsi) {
9346 			if (test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9347 					       vsi->state))
9348 				i40e_vsi_reinit_locked(vsi);
9349 		}
9350 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9351 		/* Find the VSI(s) that needs to be brought down */
9352 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9353 
9354 		i40e_pf_for_each_vsi(pf, i, vsi) {
9355 			if (test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9356 					       vsi->state)) {
9357 				set_bit(__I40E_VSI_DOWN, vsi->state);
9358 				i40e_down(vsi);
9359 			}
9360 		}
9361 	} else {
9362 		dev_info(&pf->pdev->dev,
9363 			 "bad reset request 0x%08x\n", reset_flags);
9364 	}
9365 }
9366 
9367 #ifdef CONFIG_I40E_DCB
9368 /**
9369  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9370  * @pf: board private structure
9371  * @old_cfg: current DCB config
9372  * @new_cfg: new DCB config
9373  **/
9374 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9375 			    struct i40e_dcbx_config *old_cfg,
9376 			    struct i40e_dcbx_config *new_cfg)
9377 {
9378 	bool need_reconfig = false;
9379 
9380 	/* Check if ETS configuration has changed */
9381 	if (memcmp(&new_cfg->etscfg,
9382 		   &old_cfg->etscfg,
9383 		   sizeof(new_cfg->etscfg))) {
9384 		/* If Priority Table has changed reconfig is needed */
9385 		if (memcmp(&new_cfg->etscfg.prioritytable,
9386 			   &old_cfg->etscfg.prioritytable,
9387 			   sizeof(new_cfg->etscfg.prioritytable))) {
9388 			need_reconfig = true;
9389 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9390 		}
9391 
9392 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9393 			   &old_cfg->etscfg.tcbwtable,
9394 			   sizeof(new_cfg->etscfg.tcbwtable)))
9395 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9396 
9397 		if (memcmp(&new_cfg->etscfg.tsatable,
9398 			   &old_cfg->etscfg.tsatable,
9399 			   sizeof(new_cfg->etscfg.tsatable)))
9400 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9401 	}
9402 
9403 	/* Check if PFC configuration has changed */
9404 	if (memcmp(&new_cfg->pfc,
9405 		   &old_cfg->pfc,
9406 		   sizeof(new_cfg->pfc))) {
9407 		need_reconfig = true;
9408 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9409 	}
9410 
9411 	/* Check if APP Table has changed */
9412 	if (memcmp(&new_cfg->app,
9413 		   &old_cfg->app,
9414 		   sizeof(new_cfg->app))) {
9415 		need_reconfig = true;
9416 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9417 	}
9418 
9419 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9420 	return need_reconfig;
9421 }
9422 
9423 /**
9424  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9425  * @pf: board private structure
9426  * @e: event info posted on ARQ
9427  **/
9428 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9429 				  struct i40e_arq_event_info *e)
9430 {
9431 	struct i40e_aqc_lldp_get_mib *mib = libie_aq_raw(&e->desc);
9432 	struct i40e_hw *hw = &pf->hw;
9433 	struct i40e_dcbx_config tmp_dcbx_cfg;
9434 	bool need_reconfig = false;
9435 	int ret = 0;
9436 	u8 type;
9437 
9438 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9439 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9440 	    (hw->phy.link_info.link_speed &
9441 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9442 	     !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9443 		/* let firmware decide if the DCB should be disabled */
9444 		set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9445 
9446 	/* Not DCB capable or capability disabled */
9447 	if (!test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
9448 		return ret;
9449 
9450 	/* Ignore if event is not for Nearest Bridge */
9451 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9452 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9453 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9454 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9455 		return ret;
9456 
9457 	/* Check MIB Type and return if event for Remote MIB update */
9458 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9459 	dev_dbg(&pf->pdev->dev,
9460 		"LLDP event mib type %s\n", type ? "remote" : "local");
9461 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9462 		/* Update the remote cached instance and return */
9463 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9464 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9465 				&hw->remote_dcbx_config);
9466 		goto exit;
9467 	}
9468 
9469 	/* Store the old configuration */
9470 	tmp_dcbx_cfg = hw->local_dcbx_config;
9471 
9472 	/* Reset the old DCBx configuration data */
9473 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9474 	/* Get updated DCBX data from firmware */
9475 	ret = i40e_get_dcb_config(&pf->hw);
9476 	if (ret) {
9477 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9478 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9479 		    (hw->phy.link_info.link_speed &
9480 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9481 			dev_warn(&pf->pdev->dev,
9482 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9483 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9484 		} else {
9485 			dev_info(&pf->pdev->dev,
9486 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9487 				 ERR_PTR(ret),
9488 				 libie_aq_str(pf->hw.aq.asq_last_status));
9489 		}
9490 		goto exit;
9491 	}
9492 
9493 	/* No change detected in DCBX configs */
9494 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9495 		    sizeof(tmp_dcbx_cfg))) {
9496 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9497 		goto exit;
9498 	}
9499 
9500 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9501 					       &hw->local_dcbx_config);
9502 
9503 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9504 
9505 	if (!need_reconfig)
9506 		goto exit;
9507 
9508 	/* Enable DCB tagging only when more than one TC */
9509 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9510 		set_bit(I40E_FLAG_DCB_ENA, pf->flags);
9511 	else
9512 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9513 
9514 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9515 	/* Reconfiguration needed quiesce all VSIs */
9516 	i40e_pf_quiesce_all_vsi(pf);
9517 
9518 	/* Changes in configuration update VEB/VSI */
9519 	i40e_dcb_reconfigure(pf);
9520 
9521 	ret = i40e_resume_port_tx(pf);
9522 
9523 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9524 	/* In case of error no point in resuming VSIs */
9525 	if (ret)
9526 		goto exit;
9527 
9528 	/* Wait for the PF's queues to be disabled */
9529 	ret = i40e_pf_wait_queues_disabled(pf);
9530 	if (ret) {
9531 		/* Schedule PF reset to recover */
9532 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9533 		i40e_service_event_schedule(pf);
9534 	} else {
9535 		i40e_pf_unquiesce_all_vsi(pf);
9536 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9537 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9538 	}
9539 
9540 exit:
9541 	return ret;
9542 }
9543 #endif /* CONFIG_I40E_DCB */
9544 
9545 /**
9546  * i40e_do_reset_safe - Protected reset path for userland calls.
9547  * @pf: board private structure
9548  * @reset_flags: which reset is requested
9549  *
9550  **/
9551 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9552 {
9553 	rtnl_lock();
9554 	i40e_do_reset(pf, reset_flags, true);
9555 	rtnl_unlock();
9556 }
9557 
9558 /**
9559  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9560  * @pf: board private structure
9561  * @e: event info posted on ARQ
9562  *
9563  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9564  * and VF queues
9565  **/
9566 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9567 					   struct i40e_arq_event_info *e)
9568 {
9569 	struct i40e_aqc_lan_overflow *data = libie_aq_raw(&e->desc);
9570 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9571 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9572 	struct i40e_hw *hw = &pf->hw;
9573 	struct i40e_vf *vf;
9574 	u16 vf_id;
9575 
9576 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9577 		queue, qtx_ctl);
9578 
9579 	if (FIELD_GET(I40E_QTX_CTL_PFVF_Q_MASK, qtx_ctl) !=
9580 	    I40E_QTX_CTL_VF_QUEUE)
9581 		return;
9582 
9583 	/* Queue belongs to VF, find the VF and issue VF reset */
9584 	vf_id = FIELD_GET(I40E_QTX_CTL_VFVM_INDX_MASK, qtx_ctl);
9585 	vf_id -= hw->func_caps.vf_base_id;
9586 	vf = &pf->vf[vf_id];
9587 	i40e_vc_notify_vf_reset(vf);
9588 	/* Allow VF to process pending reset notification */
9589 	msleep(20);
9590 	i40e_reset_vf(vf, false);
9591 }
9592 
9593 /**
9594  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9595  * @pf: board private structure
9596  **/
9597 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9598 {
9599 	u32 val, fcnt_prog;
9600 
9601 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9602 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9603 		    FIELD_GET(I40E_PFQF_FDSTAT_BEST_CNT_MASK, val);
9604 	return fcnt_prog;
9605 }
9606 
9607 /**
9608  * i40e_get_global_fd_count - Get total FD filters programmed on device
9609  * @pf: board private structure
9610  **/
9611 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9612 {
9613 	u32 val, fcnt_prog;
9614 
9615 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9616 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9617 		    FIELD_GET(I40E_GLQF_FDCNT_0_BESTCNT_MASK, val);
9618 	return fcnt_prog;
9619 }
9620 
9621 /**
9622  * i40e_reenable_fdir_sb - Restore FDir SB capability
9623  * @pf: board private structure
9624  **/
9625 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9626 {
9627 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9628 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
9629 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9630 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9631 }
9632 
9633 /**
9634  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9635  * @pf: board private structure
9636  **/
9637 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9638 {
9639 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9640 		/* ATR uses the same filtering logic as SB rules. It only
9641 		 * functions properly if the input set mask is at the default
9642 		 * settings. It is safe to restore the default input set
9643 		 * because there are no active TCPv4 filter rules.
9644 		 */
9645 		i40e_write_fd_input_set(pf, LIBIE_FILTER_PCTYPE_NONF_IPV4_TCP,
9646 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9647 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9648 
9649 		if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
9650 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9651 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9652 	}
9653 }
9654 
9655 /**
9656  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9657  * @pf: board private structure
9658  * @filter: FDir filter to remove
9659  */
9660 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9661 				       struct i40e_fdir_filter *filter)
9662 {
9663 	/* Update counters */
9664 	pf->fdir_pf_active_filters--;
9665 	pf->fd_inv = 0;
9666 
9667 	switch (filter->flow_type) {
9668 	case TCP_V4_FLOW:
9669 		pf->fd_tcp4_filter_cnt--;
9670 		break;
9671 	case UDP_V4_FLOW:
9672 		pf->fd_udp4_filter_cnt--;
9673 		break;
9674 	case SCTP_V4_FLOW:
9675 		pf->fd_sctp4_filter_cnt--;
9676 		break;
9677 	case TCP_V6_FLOW:
9678 		pf->fd_tcp6_filter_cnt--;
9679 		break;
9680 	case UDP_V6_FLOW:
9681 		pf->fd_udp6_filter_cnt--;
9682 		break;
9683 	case SCTP_V6_FLOW:
9684 		pf->fd_udp6_filter_cnt--;
9685 		break;
9686 	case IP_USER_FLOW:
9687 		switch (filter->ipl4_proto) {
9688 		case IPPROTO_TCP:
9689 			pf->fd_tcp4_filter_cnt--;
9690 			break;
9691 		case IPPROTO_UDP:
9692 			pf->fd_udp4_filter_cnt--;
9693 			break;
9694 		case IPPROTO_SCTP:
9695 			pf->fd_sctp4_filter_cnt--;
9696 			break;
9697 		case IPPROTO_IP:
9698 			pf->fd_ip4_filter_cnt--;
9699 			break;
9700 		}
9701 		break;
9702 	case IPV6_USER_FLOW:
9703 		switch (filter->ipl4_proto) {
9704 		case IPPROTO_TCP:
9705 			pf->fd_tcp6_filter_cnt--;
9706 			break;
9707 		case IPPROTO_UDP:
9708 			pf->fd_udp6_filter_cnt--;
9709 			break;
9710 		case IPPROTO_SCTP:
9711 			pf->fd_sctp6_filter_cnt--;
9712 			break;
9713 		case IPPROTO_IP:
9714 			pf->fd_ip6_filter_cnt--;
9715 			break;
9716 		}
9717 		break;
9718 	}
9719 
9720 	/* Remove the filter from the list and free memory */
9721 	hlist_del(&filter->fdir_node);
9722 	kfree(filter);
9723 }
9724 
9725 /**
9726  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9727  * @pf: board private structure
9728  **/
9729 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9730 {
9731 	struct i40e_fdir_filter *filter;
9732 	u32 fcnt_prog, fcnt_avail;
9733 	struct hlist_node *node;
9734 
9735 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9736 		return;
9737 
9738 	/* Check if we have enough room to re-enable FDir SB capability. */
9739 	fcnt_prog = i40e_get_global_fd_count(pf);
9740 	fcnt_avail = pf->fdir_pf_filter_count;
9741 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9742 	    (pf->fd_add_err == 0) ||
9743 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9744 		i40e_reenable_fdir_sb(pf);
9745 
9746 	/* We should wait for even more space before re-enabling ATR.
9747 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9748 	 * rules active.
9749 	 */
9750 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9751 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9752 		i40e_reenable_fdir_atr(pf);
9753 
9754 	/* if hw had a problem adding a filter, delete it */
9755 	if (pf->fd_inv > 0) {
9756 		hlist_for_each_entry_safe(filter, node,
9757 					  &pf->fdir_filter_list, fdir_node)
9758 			if (filter->fd_id == pf->fd_inv)
9759 				i40e_delete_invalid_filter(pf, filter);
9760 	}
9761 }
9762 
9763 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9764 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9765 /**
9766  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9767  * @pf: board private structure
9768  **/
9769 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9770 {
9771 	unsigned long min_flush_time;
9772 	int flush_wait_retry = 50;
9773 	bool disable_atr = false;
9774 	int fd_room;
9775 	int reg;
9776 
9777 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9778 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9779 		return;
9780 
9781 	/* If the flush is happening too quick and we have mostly SB rules we
9782 	 * should not re-enable ATR for some time.
9783 	 */
9784 	min_flush_time = pf->fd_flush_timestamp +
9785 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9786 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9787 
9788 	if (!(time_after(jiffies, min_flush_time)) &&
9789 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9790 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9791 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9792 		disable_atr = true;
9793 	}
9794 
9795 	pf->fd_flush_timestamp = jiffies;
9796 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9797 	/* flush all filters */
9798 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9799 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9800 	i40e_flush(&pf->hw);
9801 	pf->fd_flush_cnt++;
9802 	pf->fd_add_err = 0;
9803 	do {
9804 		/* Check FD flush status every 5-6msec */
9805 		usleep_range(5000, 6000);
9806 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9807 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9808 			break;
9809 	} while (flush_wait_retry--);
9810 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9811 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9812 	} else {
9813 		/* replay sideband filters */
9814 		i40e_fdir_filter_restore(i40e_pf_get_main_vsi(pf));
9815 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9816 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9817 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9818 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9819 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9820 	}
9821 }
9822 
9823 /**
9824  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9825  * @pf: board private structure
9826  **/
9827 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9828 {
9829 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9830 }
9831 
9832 /**
9833  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9834  * @pf: board private structure
9835  **/
9836 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9837 {
9838 
9839 	/* if interface is down do nothing */
9840 	if (test_bit(__I40E_DOWN, pf->state))
9841 		return;
9842 
9843 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9844 		i40e_fdir_flush_and_replay(pf);
9845 
9846 	i40e_fdir_check_and_reenable(pf);
9847 
9848 }
9849 
9850 /**
9851  * i40e_vsi_link_event - notify VSI of a link event
9852  * @vsi: vsi to be notified
9853  * @link_up: link up or down
9854  **/
9855 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9856 {
9857 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9858 		return;
9859 
9860 	switch (vsi->type) {
9861 	case I40E_VSI_MAIN:
9862 		if (!vsi->netdev || !vsi->netdev_registered)
9863 			break;
9864 
9865 		if (link_up) {
9866 			netif_carrier_on(vsi->netdev);
9867 			netif_tx_wake_all_queues(vsi->netdev);
9868 		} else {
9869 			netif_carrier_off(vsi->netdev);
9870 			netif_tx_stop_all_queues(vsi->netdev);
9871 		}
9872 		break;
9873 
9874 	case I40E_VSI_SRIOV:
9875 	case I40E_VSI_VMDQ2:
9876 	case I40E_VSI_CTRL:
9877 	case I40E_VSI_IWARP:
9878 	case I40E_VSI_MIRROR:
9879 	default:
9880 		/* there is no notification for other VSIs */
9881 		break;
9882 	}
9883 }
9884 
9885 /**
9886  * i40e_veb_link_event - notify elements on the veb of a link event
9887  * @veb: veb to be notified
9888  * @link_up: link up or down
9889  **/
9890 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9891 {
9892 	struct i40e_vsi *vsi;
9893 	struct i40e_pf *pf;
9894 	int i;
9895 
9896 	if (!veb || !veb->pf)
9897 		return;
9898 	pf = veb->pf;
9899 
9900 	/* Send link event to contained VSIs */
9901 	i40e_pf_for_each_vsi(pf, i, vsi)
9902 		if (vsi->uplink_seid == veb->seid)
9903 			i40e_vsi_link_event(vsi, link_up);
9904 }
9905 
9906 /**
9907  * i40e_link_event - Update netif_carrier status
9908  * @pf: board private structure
9909  **/
9910 static void i40e_link_event(struct i40e_pf *pf)
9911 {
9912 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
9913 	struct i40e_veb *veb = i40e_pf_get_main_veb(pf);
9914 	u8 new_link_speed, old_link_speed;
9915 	bool new_link, old_link;
9916 	int status;
9917 #ifdef CONFIG_I40E_DCB
9918 	int err;
9919 #endif /* CONFIG_I40E_DCB */
9920 
9921 	/* set this to force the get_link_status call to refresh state */
9922 	pf->hw.phy.get_link_info = true;
9923 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9924 	status = i40e_get_link_status(&pf->hw, &new_link);
9925 
9926 	/* On success, disable temp link polling */
9927 	if (status == 0) {
9928 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9929 	} else {
9930 		/* Enable link polling temporarily until i40e_get_link_status
9931 		 * returns 0
9932 		 */
9933 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9934 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9935 			status);
9936 		return;
9937 	}
9938 
9939 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9940 	new_link_speed = pf->hw.phy.link_info.link_speed;
9941 
9942 	if (new_link == old_link &&
9943 	    new_link_speed == old_link_speed &&
9944 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9945 	     new_link == netif_carrier_ok(vsi->netdev)))
9946 		return;
9947 
9948 	if (!new_link && old_link)
9949 		pf->link_down_events++;
9950 
9951 	i40e_print_link_message(vsi, new_link);
9952 
9953 	/* Notify the base of the switch tree connected to
9954 	 * the link.  Floating VEBs are not notified.
9955 	 */
9956 	if (veb)
9957 		i40e_veb_link_event(veb, new_link);
9958 	else
9959 		i40e_vsi_link_event(vsi, new_link);
9960 
9961 	if (pf->vf)
9962 		i40e_vc_notify_link_state(pf);
9963 
9964 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
9965 		i40e_ptp_set_increment(pf);
9966 #ifdef CONFIG_I40E_DCB
9967 	if (new_link == old_link)
9968 		return;
9969 	/* Not SW DCB so firmware will take care of default settings */
9970 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9971 		return;
9972 
9973 	/* We cover here only link down, as after link up in case of SW DCB
9974 	 * SW LLDP agent will take care of setting it up
9975 	 */
9976 	if (!new_link) {
9977 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9978 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9979 		err = i40e_dcb_sw_default_config(pf);
9980 		if (err) {
9981 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9982 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9983 		} else {
9984 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9985 				       DCB_CAP_DCBX_VER_IEEE;
9986 			set_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
9987 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
9988 		}
9989 	}
9990 #endif /* CONFIG_I40E_DCB */
9991 }
9992 
9993 /**
9994  * i40e_watchdog_subtask - periodic checks not using event driven response
9995  * @pf: board private structure
9996  **/
9997 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9998 {
9999 	struct i40e_vsi *vsi;
10000 	struct i40e_veb *veb;
10001 	int i;
10002 
10003 	/* if interface is down do nothing */
10004 	if (test_bit(__I40E_DOWN, pf->state) ||
10005 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
10006 		return;
10007 
10008 	/* make sure we don't do these things too often */
10009 	if (time_before(jiffies, (pf->service_timer_previous +
10010 				  pf->service_timer_period)))
10011 		return;
10012 	pf->service_timer_previous = jiffies;
10013 
10014 	if (test_bit(I40E_FLAG_LINK_POLLING_ENA, pf->flags) ||
10015 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
10016 		i40e_link_event(pf);
10017 
10018 	/* Update the stats for active netdevs so the network stack
10019 	 * can look at updated numbers whenever it cares to
10020 	 */
10021 	i40e_pf_for_each_vsi(pf, i, vsi)
10022 		if (vsi->netdev)
10023 			i40e_update_stats(vsi);
10024 
10025 	if (test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags)) {
10026 		/* Update the stats for the active switching components */
10027 		i40e_pf_for_each_veb(pf, i, veb)
10028 			i40e_update_veb_stats(veb);
10029 	}
10030 
10031 	i40e_ptp_rx_hang(pf);
10032 	i40e_ptp_tx_hang(pf);
10033 }
10034 
10035 /**
10036  * i40e_reset_subtask - Set up for resetting the device and driver
10037  * @pf: board private structure
10038  **/
10039 static void i40e_reset_subtask(struct i40e_pf *pf)
10040 {
10041 	u32 reset_flags = 0;
10042 
10043 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10044 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10045 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10046 	}
10047 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10048 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10049 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10050 	}
10051 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10052 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10053 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10054 	}
10055 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10056 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10057 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10058 	}
10059 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10060 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10061 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10062 	}
10063 
10064 	/* If there's a recovery already waiting, it takes
10065 	 * precedence before starting a new reset sequence.
10066 	 */
10067 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10068 		i40e_prep_for_reset(pf);
10069 		i40e_reset(pf);
10070 		i40e_rebuild(pf, false, false);
10071 	}
10072 
10073 	/* If we're already down or resetting, just bail */
10074 	if (reset_flags &&
10075 	    !test_bit(__I40E_DOWN, pf->state) &&
10076 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10077 		i40e_do_reset(pf, reset_flags, false);
10078 	}
10079 }
10080 
10081 /**
10082  * i40e_handle_link_event - Handle link event
10083  * @pf: board private structure
10084  * @e: event info posted on ARQ
10085  **/
10086 static void i40e_handle_link_event(struct i40e_pf *pf,
10087 				   struct i40e_arq_event_info *e)
10088 {
10089 	struct i40e_aqc_get_link_status *status = libie_aq_raw(&e->desc);
10090 
10091 	/* Do a new status request to re-enable LSE reporting
10092 	 * and load new status information into the hw struct
10093 	 * This completely ignores any state information
10094 	 * in the ARQ event info, instead choosing to always
10095 	 * issue the AQ update link status command.
10096 	 */
10097 	i40e_link_event(pf);
10098 
10099 	/* Check if module meets thermal requirements */
10100 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10101 		dev_err(&pf->pdev->dev,
10102 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10103 		dev_err(&pf->pdev->dev,
10104 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10105 	} else {
10106 		/* check for unqualified module, if link is down, suppress
10107 		 * the message if link was forced to be down.
10108 		 */
10109 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10110 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10111 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10112 		    (!test_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags))) {
10113 			dev_err(&pf->pdev->dev,
10114 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10115 			dev_err(&pf->pdev->dev,
10116 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10117 		}
10118 	}
10119 }
10120 
10121 /**
10122  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10123  * @pf: board private structure
10124  **/
10125 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10126 {
10127 	struct i40e_arq_event_info event;
10128 	struct i40e_hw *hw = &pf->hw;
10129 	u16 pending, i = 0;
10130 	u16 opcode;
10131 	u32 oldval;
10132 	int ret;
10133 	u32 val;
10134 
10135 	/* Do not run clean AQ when PF reset fails */
10136 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10137 		return;
10138 
10139 	/* check for error indications */
10140 	val = rd32(&pf->hw, I40E_PF_ARQLEN);
10141 	oldval = val;
10142 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10143 		if (hw->debug_mask & I40E_DEBUG_AQ)
10144 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10145 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10146 	}
10147 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10148 		if (hw->debug_mask & I40E_DEBUG_AQ)
10149 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10150 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10151 		pf->arq_overflows++;
10152 	}
10153 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10154 		if (hw->debug_mask & I40E_DEBUG_AQ)
10155 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10156 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10157 	}
10158 	if (oldval != val)
10159 		wr32(&pf->hw, I40E_PF_ARQLEN, val);
10160 
10161 	val = rd32(&pf->hw, I40E_PF_ATQLEN);
10162 	oldval = val;
10163 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10164 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10165 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10166 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10167 	}
10168 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10169 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10170 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10171 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10172 	}
10173 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10174 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10175 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10176 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10177 	}
10178 	if (oldval != val)
10179 		wr32(&pf->hw, I40E_PF_ATQLEN, val);
10180 
10181 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10182 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10183 	if (!event.msg_buf)
10184 		return;
10185 
10186 	do {
10187 		ret = i40e_clean_arq_element(hw, &event, &pending);
10188 		if (ret == -EALREADY)
10189 			break;
10190 		else if (ret) {
10191 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10192 			break;
10193 		}
10194 
10195 		opcode = le16_to_cpu(event.desc.opcode);
10196 		switch (opcode) {
10197 
10198 		case i40e_aqc_opc_get_link_status:
10199 			rtnl_lock();
10200 			i40e_handle_link_event(pf, &event);
10201 			rtnl_unlock();
10202 			break;
10203 		case i40e_aqc_opc_send_msg_to_pf:
10204 			ret = i40e_vc_process_vf_msg(pf,
10205 					le16_to_cpu(event.desc.retval),
10206 					le32_to_cpu(event.desc.cookie_high),
10207 					le32_to_cpu(event.desc.cookie_low),
10208 					event.msg_buf,
10209 					event.msg_len);
10210 			break;
10211 		case i40e_aqc_opc_lldp_update_mib:
10212 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10213 #ifdef CONFIG_I40E_DCB
10214 			rtnl_lock();
10215 			i40e_handle_lldp_event(pf, &event);
10216 			rtnl_unlock();
10217 #endif /* CONFIG_I40E_DCB */
10218 			break;
10219 		case i40e_aqc_opc_event_lan_overflow:
10220 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10221 			i40e_handle_lan_overflow_event(pf, &event);
10222 			break;
10223 		case i40e_aqc_opc_send_msg_to_peer:
10224 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10225 			break;
10226 		case i40e_aqc_opc_nvm_erase:
10227 		case i40e_aqc_opc_nvm_update:
10228 		case i40e_aqc_opc_oem_post_update:
10229 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10230 				   "ARQ NVM operation 0x%04x completed\n",
10231 				   opcode);
10232 			break;
10233 		default:
10234 			dev_info(&pf->pdev->dev,
10235 				 "ARQ: Unknown event 0x%04x ignored\n",
10236 				 opcode);
10237 			break;
10238 		}
10239 	} while (i++ < I40E_AQ_WORK_LIMIT);
10240 
10241 	if (i < I40E_AQ_WORK_LIMIT)
10242 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10243 
10244 	/* re-enable Admin queue interrupt cause */
10245 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10246 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10247 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10248 	i40e_flush(hw);
10249 
10250 	kfree(event.msg_buf);
10251 }
10252 
10253 /**
10254  * i40e_verify_eeprom - make sure eeprom is good to use
10255  * @pf: board private structure
10256  **/
10257 static void i40e_verify_eeprom(struct i40e_pf *pf)
10258 {
10259 	int err;
10260 
10261 	err = i40e_diag_eeprom_test(&pf->hw);
10262 	if (err) {
10263 		/* retry in case of garbage read */
10264 		err = i40e_diag_eeprom_test(&pf->hw);
10265 		if (err) {
10266 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10267 				 err);
10268 			set_bit(__I40E_BAD_EEPROM, pf->state);
10269 		}
10270 	}
10271 
10272 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10273 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10274 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10275 	}
10276 }
10277 
10278 /**
10279  * i40e_enable_pf_switch_lb
10280  * @pf: pointer to the PF structure
10281  *
10282  * enable switch loop back or die - no point in a return value
10283  **/
10284 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10285 {
10286 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10287 	struct i40e_vsi_context ctxt;
10288 	int ret;
10289 
10290 	ctxt.seid = pf->main_vsi_seid;
10291 	ctxt.pf_num = pf->hw.pf_id;
10292 	ctxt.vf_num = 0;
10293 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10294 	if (ret) {
10295 		dev_info(&pf->pdev->dev,
10296 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10297 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10298 		return;
10299 	}
10300 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10301 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10302 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10303 
10304 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10305 	if (ret) {
10306 		dev_info(&pf->pdev->dev,
10307 			 "update vsi switch failed, err %pe aq_err %s\n",
10308 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10309 	}
10310 }
10311 
10312 /**
10313  * i40e_disable_pf_switch_lb
10314  * @pf: pointer to the PF structure
10315  *
10316  * disable switch loop back or die - no point in a return value
10317  **/
10318 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10319 {
10320 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10321 	struct i40e_vsi_context ctxt;
10322 	int ret;
10323 
10324 	ctxt.seid = pf->main_vsi_seid;
10325 	ctxt.pf_num = pf->hw.pf_id;
10326 	ctxt.vf_num = 0;
10327 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10328 	if (ret) {
10329 		dev_info(&pf->pdev->dev,
10330 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10331 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10332 		return;
10333 	}
10334 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10335 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10336 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10337 
10338 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10339 	if (ret) {
10340 		dev_info(&pf->pdev->dev,
10341 			 "update vsi switch failed, err %pe aq_err %s\n",
10342 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10343 	}
10344 }
10345 
10346 /**
10347  * i40e_config_bridge_mode - Configure the HW bridge mode
10348  * @veb: pointer to the bridge instance
10349  *
10350  * Configure the loop back mode for the LAN VSI that is downlink to the
10351  * specified HW bridge instance. It is expected this function is called
10352  * when a new HW bridge is instantiated.
10353  **/
10354 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10355 {
10356 	struct i40e_pf *pf = veb->pf;
10357 
10358 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10359 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10360 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10361 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10362 		i40e_disable_pf_switch_lb(pf);
10363 	else
10364 		i40e_enable_pf_switch_lb(pf);
10365 }
10366 
10367 /**
10368  * i40e_reconstitute_veb - rebuild the VEB and VSIs connected to it
10369  * @veb: pointer to the VEB instance
10370  *
10371  * This is a function that builds the attached VSIs. We track the connections
10372  * through our own index numbers because the seid's from the HW could change
10373  * across the reset.
10374  **/
10375 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10376 {
10377 	struct i40e_vsi *ctl_vsi = NULL;
10378 	struct i40e_pf *pf = veb->pf;
10379 	struct i40e_vsi *vsi;
10380 	int v, ret;
10381 
10382 	/* As we do not maintain PV (port virtualizer) switch element then
10383 	 * there can be only one non-floating VEB that have uplink to MAC SEID
10384 	 * and its control VSI is the main one.
10385 	 */
10386 	if (WARN_ON(veb->uplink_seid && veb->uplink_seid != pf->mac_seid)) {
10387 		dev_err(&pf->pdev->dev,
10388 			"Invalid uplink SEID for VEB %d\n", veb->idx);
10389 		return -ENOENT;
10390 	}
10391 
10392 	if (veb->uplink_seid == pf->mac_seid) {
10393 		/* Check that the LAN VSI has VEB owning flag set */
10394 		ctl_vsi = i40e_pf_get_main_vsi(pf);
10395 
10396 		if (WARN_ON(ctl_vsi->veb_idx != veb->idx ||
10397 			    !(ctl_vsi->flags & I40E_VSI_FLAG_VEB_OWNER))) {
10398 			dev_err(&pf->pdev->dev,
10399 				"Invalid control VSI for VEB %d\n", veb->idx);
10400 			return -ENOENT;
10401 		}
10402 
10403 		/* Add the control VSI to switch */
10404 		ret = i40e_add_vsi(ctl_vsi);
10405 		if (ret) {
10406 			dev_err(&pf->pdev->dev,
10407 				"Rebuild of owner VSI for VEB %d failed: %d\n",
10408 				veb->idx, ret);
10409 			return ret;
10410 		}
10411 
10412 		i40e_vsi_reset_stats(ctl_vsi);
10413 	}
10414 
10415 	/* create the VEB in the switch and move the VSI onto the VEB */
10416 	ret = i40e_add_veb(veb, ctl_vsi);
10417 	if (ret)
10418 		return ret;
10419 
10420 	if (veb->uplink_seid) {
10421 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
10422 			veb->bridge_mode = BRIDGE_MODE_VEB;
10423 		else
10424 			veb->bridge_mode = BRIDGE_MODE_VEPA;
10425 		i40e_config_bridge_mode(veb);
10426 	}
10427 
10428 	/* create the remaining VSIs attached to this VEB */
10429 	i40e_pf_for_each_vsi(pf, v, vsi) {
10430 		if (vsi == ctl_vsi)
10431 			continue;
10432 
10433 		if (vsi->veb_idx == veb->idx) {
10434 			vsi->uplink_seid = veb->seid;
10435 			ret = i40e_add_vsi(vsi);
10436 			if (ret) {
10437 				dev_info(&pf->pdev->dev,
10438 					 "rebuild of vsi_idx %d failed: %d\n",
10439 					 v, ret);
10440 				return ret;
10441 			}
10442 			i40e_vsi_reset_stats(vsi);
10443 		}
10444 	}
10445 
10446 	return ret;
10447 }
10448 
10449 /**
10450  * i40e_get_capabilities - get info about the HW
10451  * @pf: the PF struct
10452  * @list_type: AQ capability to be queried
10453  **/
10454 static int i40e_get_capabilities(struct i40e_pf *pf,
10455 				 enum i40e_admin_queue_opc list_type)
10456 {
10457 	struct libie_aqc_list_caps_elem *cap_buf;
10458 	u16 data_size;
10459 	int buf_len;
10460 	int err;
10461 
10462 	buf_len = 40 * sizeof(struct libie_aqc_list_caps_elem);
10463 	do {
10464 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10465 		if (!cap_buf)
10466 			return -ENOMEM;
10467 
10468 		/* this loads the data into the hw struct for us */
10469 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10470 						    &data_size, list_type,
10471 						    NULL);
10472 		/* data loaded, buffer no longer needed */
10473 		kfree(cap_buf);
10474 
10475 		if (pf->hw.aq.asq_last_status == LIBIE_AQ_RC_ENOMEM) {
10476 			/* retry with a larger buffer */
10477 			buf_len = data_size;
10478 		} else if (pf->hw.aq.asq_last_status != LIBIE_AQ_RC_OK || err) {
10479 			dev_info(&pf->pdev->dev,
10480 				 "capability discovery failed, err %pe aq_err %s\n",
10481 				 ERR_PTR(err),
10482 				 libie_aq_str(pf->hw.aq.asq_last_status));
10483 			return -ENODEV;
10484 		}
10485 	} while (err);
10486 
10487 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10488 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10489 			dev_info(&pf->pdev->dev,
10490 				 "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",
10491 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10492 				 pf->hw.func_caps.num_msix_vectors,
10493 				 pf->hw.func_caps.num_msix_vectors_vf,
10494 				 pf->hw.func_caps.fd_filters_guaranteed,
10495 				 pf->hw.func_caps.fd_filters_best_effort,
10496 				 pf->hw.func_caps.num_tx_qp,
10497 				 pf->hw.func_caps.num_vsis);
10498 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10499 			dev_info(&pf->pdev->dev,
10500 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10501 				 pf->hw.dev_caps.switch_mode,
10502 				 pf->hw.dev_caps.valid_functions);
10503 			dev_info(&pf->pdev->dev,
10504 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10505 				 pf->hw.dev_caps.sr_iov_1_1,
10506 				 pf->hw.dev_caps.num_vfs);
10507 			dev_info(&pf->pdev->dev,
10508 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10509 				 pf->hw.dev_caps.num_vsis,
10510 				 pf->hw.dev_caps.num_rx_qp,
10511 				 pf->hw.dev_caps.num_tx_qp);
10512 		}
10513 	}
10514 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10515 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10516 		       + pf->hw.func_caps.num_vfs)
10517 		if (pf->hw.revision_id == 0 &&
10518 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10519 			dev_info(&pf->pdev->dev,
10520 				 "got num_vsis %d, setting num_vsis to %d\n",
10521 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10522 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10523 		}
10524 	}
10525 	return 0;
10526 }
10527 
10528 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10529 
10530 /**
10531  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10532  * @pf: board private structure
10533  **/
10534 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10535 {
10536 	struct i40e_vsi *main_vsi, *vsi;
10537 
10538 	/* quick workaround for an NVM issue that leaves a critical register
10539 	 * uninitialized
10540 	 */
10541 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10542 		static const u32 hkey[] = {
10543 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10544 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10545 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10546 			0x95b3a76d};
10547 		int i;
10548 
10549 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10550 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10551 	}
10552 
10553 	if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
10554 		return;
10555 
10556 	/* find existing VSI and see if it needs configuring */
10557 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10558 
10559 	/* create a new VSI if none exists */
10560 	if (!vsi) {
10561 		main_vsi = i40e_pf_get_main_vsi(pf);
10562 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR, main_vsi->seid, 0);
10563 		if (!vsi) {
10564 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10565 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
10566 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
10567 			return;
10568 		}
10569 	}
10570 
10571 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10572 }
10573 
10574 /**
10575  * i40e_fdir_teardown - release the Flow Director resources
10576  * @pf: board private structure
10577  **/
10578 static void i40e_fdir_teardown(struct i40e_pf *pf)
10579 {
10580 	struct i40e_vsi *vsi;
10581 
10582 	i40e_fdir_filter_exit(pf);
10583 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10584 	if (vsi)
10585 		i40e_vsi_release(vsi);
10586 }
10587 
10588 /**
10589  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10590  * @vsi: PF main vsi
10591  * @seid: seid of main or channel VSIs
10592  *
10593  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10594  * existed before reset
10595  **/
10596 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10597 {
10598 	struct i40e_cloud_filter *cfilter;
10599 	struct i40e_pf *pf = vsi->back;
10600 	struct hlist_node *node;
10601 	int ret;
10602 
10603 	/* Add cloud filters back if they exist */
10604 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10605 				  cloud_node) {
10606 		if (cfilter->seid != seid)
10607 			continue;
10608 
10609 		if (cfilter->dst_port)
10610 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10611 								true);
10612 		else
10613 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10614 
10615 		if (ret) {
10616 			dev_dbg(&pf->pdev->dev,
10617 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10618 				ERR_PTR(ret),
10619 				libie_aq_str(pf->hw.aq.asq_last_status));
10620 			return ret;
10621 		}
10622 	}
10623 	return 0;
10624 }
10625 
10626 /**
10627  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10628  * @vsi: PF main vsi
10629  *
10630  * Rebuilds channel VSIs if they existed before reset
10631  **/
10632 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10633 {
10634 	struct i40e_channel *ch, *ch_tmp;
10635 	int ret;
10636 
10637 	if (list_empty(&vsi->ch_list))
10638 		return 0;
10639 
10640 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10641 		if (!ch->initialized)
10642 			break;
10643 		/* Proceed with creation of channel (VMDq2) VSI */
10644 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10645 		if (ret) {
10646 			dev_info(&vsi->back->pdev->dev,
10647 				 "failed to rebuild channels using uplink_seid %u\n",
10648 				 vsi->uplink_seid);
10649 			return ret;
10650 		}
10651 		/* Reconfigure TX queues using QTX_CTL register */
10652 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10653 		if (ret) {
10654 			dev_info(&vsi->back->pdev->dev,
10655 				 "failed to configure TX rings for channel %u\n",
10656 				 ch->seid);
10657 			return ret;
10658 		}
10659 		/* update 'next_base_queue' */
10660 		vsi->next_base_queue = vsi->next_base_queue +
10661 							ch->num_queue_pairs;
10662 		if (ch->max_tx_rate) {
10663 			u64 credits = ch->max_tx_rate;
10664 
10665 			if (i40e_set_bw_limit(vsi, ch->seid,
10666 					      ch->max_tx_rate))
10667 				return -EINVAL;
10668 
10669 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10670 			dev_dbg(&vsi->back->pdev->dev,
10671 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10672 				ch->max_tx_rate,
10673 				credits,
10674 				ch->seid);
10675 		}
10676 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10677 		if (ret) {
10678 			dev_dbg(&vsi->back->pdev->dev,
10679 				"Failed to rebuild cloud filters for channel VSI %u\n",
10680 				ch->seid);
10681 			return ret;
10682 		}
10683 	}
10684 	return 0;
10685 }
10686 
10687 /**
10688  * i40e_clean_xps_state - clean xps state for every tx_ring
10689  * @vsi: ptr to the VSI
10690  **/
10691 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10692 {
10693 	int i;
10694 
10695 	if (vsi->tx_rings)
10696 		for (i = 0; i < vsi->num_queue_pairs; i++)
10697 			if (vsi->tx_rings[i])
10698 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10699 					  vsi->tx_rings[i]->state);
10700 }
10701 
10702 /**
10703  * i40e_prep_for_reset - prep for the core to reset
10704  * @pf: board private structure
10705  *
10706  * Close up the VFs and other things in prep for PF Reset.
10707   **/
10708 static void i40e_prep_for_reset(struct i40e_pf *pf)
10709 {
10710 	struct i40e_hw *hw = &pf->hw;
10711 	struct i40e_vsi *vsi;
10712 	int ret = 0;
10713 	u32 v;
10714 
10715 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10716 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10717 		return;
10718 	if (i40e_check_asq_alive(&pf->hw))
10719 		i40e_vc_notify_reset(pf);
10720 
10721 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10722 
10723 	/* quiesce the VSIs and their queues that are not already DOWN */
10724 	i40e_pf_quiesce_all_vsi(pf);
10725 
10726 	i40e_pf_for_each_vsi(pf, v, vsi) {
10727 		i40e_clean_xps_state(vsi);
10728 		vsi->seid = 0;
10729 	}
10730 
10731 	i40e_shutdown_adminq(&pf->hw);
10732 
10733 	/* call shutdown HMC */
10734 	if (hw->hmc.hmc_obj) {
10735 		ret = i40e_shutdown_lan_hmc(hw);
10736 		if (ret)
10737 			dev_warn(&pf->pdev->dev,
10738 				 "shutdown_lan_hmc failed: %d\n", ret);
10739 	}
10740 
10741 	/* Save the current PTP time so that we can restore the time after the
10742 	 * reset completes.
10743 	 */
10744 	i40e_ptp_save_hw_time(pf);
10745 }
10746 
10747 /**
10748  * i40e_send_version - update firmware with driver version
10749  * @pf: PF struct
10750  */
10751 static void i40e_send_version(struct i40e_pf *pf)
10752 {
10753 	struct i40e_driver_version dv;
10754 
10755 	dv.major_version = 0xff;
10756 	dv.minor_version = 0xff;
10757 	dv.build_version = 0xff;
10758 	dv.subbuild_version = 0;
10759 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10760 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10761 }
10762 
10763 /**
10764  * i40e_get_oem_version - get OEM specific version information
10765  * @hw: pointer to the hardware structure
10766  **/
10767 static void i40e_get_oem_version(struct i40e_hw *hw)
10768 {
10769 	u16 block_offset = 0xffff;
10770 	u16 block_length = 0;
10771 	u16 capabilities = 0;
10772 	u16 gen_snap = 0;
10773 	u16 release = 0;
10774 
10775 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10776 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10777 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10778 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10779 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10780 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10781 #define I40E_NVM_OEM_LENGTH			3
10782 
10783 	/* Check if pointer to OEM version block is valid. */
10784 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10785 	if (block_offset == 0xffff)
10786 		return;
10787 
10788 	/* Check if OEM version block has correct length. */
10789 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10790 			   &block_length);
10791 	if (block_length < I40E_NVM_OEM_LENGTH)
10792 		return;
10793 
10794 	/* Check if OEM version format is as expected. */
10795 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10796 			   &capabilities);
10797 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10798 		return;
10799 
10800 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10801 			   &gen_snap);
10802 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10803 			   &release);
10804 	hw->nvm.oem_ver =
10805 		FIELD_PREP(I40E_OEM_GEN_MASK | I40E_OEM_SNAP_MASK, gen_snap) |
10806 		FIELD_PREP(I40E_OEM_RELEASE_MASK, release);
10807 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10808 }
10809 
10810 /**
10811  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10812  * @pf: board private structure
10813  **/
10814 static int i40e_reset(struct i40e_pf *pf)
10815 {
10816 	struct i40e_hw *hw = &pf->hw;
10817 	int ret;
10818 
10819 	ret = i40e_pf_reset(hw);
10820 	if (ret) {
10821 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10822 		set_bit(__I40E_RESET_FAILED, pf->state);
10823 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10824 	} else {
10825 		pf->pfr_count++;
10826 	}
10827 	return ret;
10828 }
10829 
10830 /**
10831  * i40e_rebuild - rebuild using a saved config
10832  * @pf: board private structure
10833  * @reinit: if the Main VSI needs to re-initialized.
10834  * @lock_acquired: indicates whether or not the lock has been acquired
10835  * before this function was called.
10836  **/
10837 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10838 {
10839 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10840 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
10841 	struct i40e_hw *hw = &pf->hw;
10842 	struct i40e_veb *veb;
10843 	int ret;
10844 	u32 val;
10845 	int v;
10846 
10847 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10848 	    is_recovery_mode_reported)
10849 		i40e_set_ethtool_ops(vsi->netdev);
10850 
10851 	if (test_bit(__I40E_DOWN, pf->state) &&
10852 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10853 		goto clear_recovery;
10854 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10855 
10856 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10857 	ret = i40e_init_adminq(&pf->hw);
10858 	if (ret) {
10859 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10860 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10861 		goto clear_recovery;
10862 	}
10863 	i40e_get_oem_version(&pf->hw);
10864 
10865 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10866 		/* The following delay is necessary for firmware update. */
10867 		mdelay(1000);
10868 	}
10869 
10870 	/* re-verify the eeprom if we just had an EMP reset */
10871 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10872 		i40e_verify_eeprom(pf);
10873 
10874 	/* if we are going out of or into recovery mode we have to act
10875 	 * accordingly with regard to resources initialization
10876 	 * and deinitialization
10877 	 */
10878 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10879 		if (i40e_get_capabilities(pf,
10880 					  i40e_aqc_opc_list_func_capabilities))
10881 			goto end_unlock;
10882 
10883 		if (is_recovery_mode_reported) {
10884 			/* we're staying in recovery mode so we'll reinitialize
10885 			 * misc vector here
10886 			 */
10887 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10888 				goto end_unlock;
10889 		} else {
10890 			if (!lock_acquired)
10891 				rtnl_lock();
10892 			/* we're going out of recovery mode so we'll free
10893 			 * the IRQ allocated specifically for recovery mode
10894 			 * and restore the interrupt scheme
10895 			 */
10896 			free_irq(pf->pdev->irq, pf);
10897 			i40e_clear_interrupt_scheme(pf);
10898 			if (i40e_restore_interrupt_scheme(pf))
10899 				goto end_unlock;
10900 		}
10901 
10902 		/* tell the firmware that we're starting */
10903 		i40e_send_version(pf);
10904 
10905 		/* bail out in case recovery mode was detected, as there is
10906 		 * no need for further configuration.
10907 		 */
10908 		goto end_unlock;
10909 	}
10910 
10911 	i40e_clear_pxe_mode(hw);
10912 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10913 	if (ret)
10914 		goto end_core_reset;
10915 
10916 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10917 				hw->func_caps.num_rx_qp, 0, 0);
10918 	if (ret) {
10919 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10920 		goto end_core_reset;
10921 	}
10922 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10923 	if (ret) {
10924 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10925 		goto end_core_reset;
10926 	}
10927 
10928 #ifdef CONFIG_I40E_DCB
10929 	/* Enable FW to write a default DCB config on link-up
10930 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10931 	 * is not supported with new link speed
10932 	 */
10933 	if (i40e_is_tc_mqprio_enabled(pf)) {
10934 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10935 	} else {
10936 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10937 		    (hw->phy.link_info.link_speed &
10938 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10939 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10940 			dev_warn(&pf->pdev->dev,
10941 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10942 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10943 		} else {
10944 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10945 			ret = i40e_init_pf_dcb(pf);
10946 			if (ret) {
10947 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10948 					 ret);
10949 				clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
10950 				/* Continue without DCB enabled */
10951 			}
10952 		}
10953 	}
10954 
10955 #endif /* CONFIG_I40E_DCB */
10956 	if (!lock_acquired)
10957 		rtnl_lock();
10958 	ret = i40e_setup_pf_switch(pf, reinit, true);
10959 	if (ret)
10960 		goto end_unlock;
10961 
10962 	/* The driver only wants link up/down and module qualification
10963 	 * reports from firmware.  Note the negative logic.
10964 	 */
10965 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10966 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10967 					 I40E_AQ_EVENT_MEDIA_NA |
10968 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10969 	if (ret)
10970 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10971 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
10972 
10973 	/* Rebuild the VSIs and VEBs that existed before reset.
10974 	 * They are still in our local switch element arrays, so only
10975 	 * need to rebuild the switch model in the HW.
10976 	 *
10977 	 * If there were VEBs but the reconstitution failed, we'll try
10978 	 * to recover minimal use by getting the basic PF VSI working.
10979 	 */
10980 	if (vsi->uplink_seid != pf->mac_seid) {
10981 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10982 
10983 		/* Rebuild VEBs */
10984 		i40e_pf_for_each_veb(pf, v, veb) {
10985 			ret = i40e_reconstitute_veb(veb);
10986 			if (!ret)
10987 				continue;
10988 
10989 			/* If Main VEB failed, we're in deep doodoo,
10990 			 * so give up rebuilding the switch and set up
10991 			 * for minimal rebuild of PF VSI.
10992 			 * If orphan failed, we'll report the error
10993 			 * but try to keep going.
10994 			 */
10995 			if (veb->uplink_seid == pf->mac_seid) {
10996 				dev_info(&pf->pdev->dev,
10997 					 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10998 					 ret);
10999 				vsi->uplink_seid = pf->mac_seid;
11000 				break;
11001 			} else if (veb->uplink_seid == 0) {
11002 				dev_info(&pf->pdev->dev,
11003 					 "rebuild of orphan VEB failed: %d\n",
11004 					 ret);
11005 			}
11006 		}
11007 	}
11008 
11009 	if (vsi->uplink_seid == pf->mac_seid) {
11010 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11011 		/* no VEB, so rebuild only the Main VSI */
11012 		ret = i40e_add_vsi(vsi);
11013 		if (ret) {
11014 			dev_info(&pf->pdev->dev,
11015 				 "rebuild of Main VSI failed: %d\n", ret);
11016 			goto end_unlock;
11017 		}
11018 	}
11019 
11020 	if (vsi->mqprio_qopt.max_rate[0]) {
11021 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11022 						  vsi->mqprio_qopt.max_rate[0]);
11023 		u64 credits = 0;
11024 
11025 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11026 		if (ret)
11027 			goto end_unlock;
11028 
11029 		credits = max_tx_rate;
11030 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11031 		dev_dbg(&vsi->back->pdev->dev,
11032 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11033 			max_tx_rate,
11034 			credits,
11035 			vsi->seid);
11036 	}
11037 
11038 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11039 	if (ret)
11040 		goto end_unlock;
11041 
11042 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11043 	 * for this main VSI if they exist
11044 	 */
11045 	ret = i40e_rebuild_channels(vsi);
11046 	if (ret)
11047 		goto end_unlock;
11048 
11049 	/* Reconfigure hardware for allowing smaller MSS in the case
11050 	 * of TSO, so that we avoid the MDD being fired and causing
11051 	 * a reset in the case of small MSS+TSO.
11052 	 */
11053 #define I40E_REG_MSS          0x000E64DC
11054 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11055 #define I40E_64BYTE_MSS       0x400000
11056 	val = rd32(hw, I40E_REG_MSS);
11057 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11058 		val &= ~I40E_REG_MSS_MIN_MASK;
11059 		val |= I40E_64BYTE_MSS;
11060 		wr32(hw, I40E_REG_MSS, val);
11061 	}
11062 
11063 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
11064 		msleep(75);
11065 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11066 		if (ret)
11067 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11068 				 ERR_PTR(ret),
11069 				 libie_aq_str(pf->hw.aq.asq_last_status));
11070 	}
11071 	/* reinit the misc interrupt */
11072 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
11073 		ret = i40e_setup_misc_vector(pf);
11074 		if (ret)
11075 			goto end_unlock;
11076 	}
11077 
11078 	/* Add a filter to drop all Flow control frames from any VSI from being
11079 	 * transmitted. By doing so we stop a malicious VF from sending out
11080 	 * PAUSE or PFC frames and potentially controlling traffic for other
11081 	 * PF/VF VSIs.
11082 	 * The FW can still send Flow control frames if enabled.
11083 	 */
11084 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11085 						       pf->main_vsi_seid);
11086 
11087 	/* restart the VSIs that were rebuilt and running before the reset */
11088 	i40e_pf_unquiesce_all_vsi(pf);
11089 
11090 	/* Release the RTNL lock before we start resetting VFs */
11091 	if (!lock_acquired)
11092 		rtnl_unlock();
11093 
11094 	/* Restore promiscuous settings */
11095 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11096 	if (ret)
11097 		dev_warn(&pf->pdev->dev,
11098 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11099 			 pf->cur_promisc ? "on" : "off",
11100 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
11101 
11102 	i40e_reset_all_vfs(pf, true);
11103 
11104 	/* tell the firmware that we're starting */
11105 	i40e_send_version(pf);
11106 
11107 	/* We've already released the lock, so don't do it again */
11108 	goto end_core_reset;
11109 
11110 end_unlock:
11111 	if (!lock_acquired)
11112 		rtnl_unlock();
11113 end_core_reset:
11114 	clear_bit(__I40E_RESET_FAILED, pf->state);
11115 clear_recovery:
11116 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11117 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11118 }
11119 
11120 /**
11121  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11122  * @pf: board private structure
11123  * @reinit: if the Main VSI needs to re-initialized.
11124  * @lock_acquired: indicates whether or not the lock has been acquired
11125  * before this function was called.
11126  **/
11127 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11128 				   bool lock_acquired)
11129 {
11130 	int ret;
11131 
11132 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11133 		return;
11134 	/* Now we wait for GRST to settle out.
11135 	 * We don't have to delete the VEBs or VSIs from the hw switch
11136 	 * because the reset will make them disappear.
11137 	 */
11138 	ret = i40e_reset(pf);
11139 	if (!ret)
11140 		i40e_rebuild(pf, reinit, lock_acquired);
11141 	else
11142 		dev_err(&pf->pdev->dev, "%s: i40e_reset() FAILED", __func__);
11143 }
11144 
11145 /**
11146  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11147  * @pf: board private structure
11148  *
11149  * Close up the VFs and other things in prep for a Core Reset,
11150  * then get ready to rebuild the world.
11151  * @lock_acquired: indicates whether or not the lock has been acquired
11152  * before this function was called.
11153  **/
11154 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11155 {
11156 	i40e_prep_for_reset(pf);
11157 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11158 }
11159 
11160 /**
11161  * i40e_print_vf_mdd_event - print VF Tx/Rx malicious driver detect event
11162  * @pf: board private structure
11163  * @vf: pointer to the VF structure
11164  * @is_tx: true - for Tx event, false - for  Rx
11165  */
11166 static void i40e_print_vf_mdd_event(struct i40e_pf *pf, struct i40e_vf *vf,
11167 				    bool is_tx)
11168 {
11169 	dev_err(&pf->pdev->dev, is_tx ?
11170 		"%lld Tx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n" :
11171 		"%lld Rx Malicious Driver Detection events detected on PF %d VF %d MAC %pm. mdd-auto-reset-vfs=%s\n",
11172 		is_tx ? vf->mdd_tx_events.count : vf->mdd_rx_events.count,
11173 		pf->hw.pf_id,
11174 		vf->vf_id,
11175 		vf->default_lan_addr.addr,
11176 		str_on_off(test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)));
11177 }
11178 
11179 /**
11180  * i40e_print_vfs_mdd_events - print VFs malicious driver detect event
11181  * @pf: pointer to the PF structure
11182  *
11183  * Called from i40e_handle_mdd_event to rate limit and print VFs MDD events.
11184  */
11185 static void i40e_print_vfs_mdd_events(struct i40e_pf *pf)
11186 {
11187 	unsigned int i;
11188 
11189 	/* check that there are pending MDD events to print */
11190 	if (!test_and_clear_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state))
11191 		return;
11192 
11193 	if (!__ratelimit(&pf->mdd_message_rate_limit))
11194 		return;
11195 
11196 	for (i = 0; i < pf->num_alloc_vfs; i++) {
11197 		struct i40e_vf *vf = &pf->vf[i];
11198 		bool is_printed = false;
11199 
11200 		/* only print Rx MDD event message if there are new events */
11201 		if (vf->mdd_rx_events.count != vf->mdd_rx_events.last_printed) {
11202 			vf->mdd_rx_events.last_printed = vf->mdd_rx_events.count;
11203 			i40e_print_vf_mdd_event(pf, vf, false);
11204 			is_printed = true;
11205 		}
11206 
11207 		/* only print Tx MDD event message if there are new events */
11208 		if (vf->mdd_tx_events.count != vf->mdd_tx_events.last_printed) {
11209 			vf->mdd_tx_events.last_printed = vf->mdd_tx_events.count;
11210 			i40e_print_vf_mdd_event(pf, vf, true);
11211 			is_printed = true;
11212 		}
11213 
11214 		if (is_printed && !test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags))
11215 			dev_info(&pf->pdev->dev,
11216 				 "Use PF Control I/F to re-enable the VF #%d\n",
11217 				 i);
11218 	}
11219 }
11220 
11221 /**
11222  * i40e_handle_mdd_event
11223  * @pf: pointer to the PF structure
11224  *
11225  * Called from the MDD irq handler to identify possibly malicious vfs
11226  **/
11227 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11228 {
11229 	struct i40e_hw *hw = &pf->hw;
11230 	bool mdd_detected = false;
11231 	struct i40e_vf *vf;
11232 	u32 reg;
11233 	int i;
11234 
11235 	if (!test_and_clear_bit(__I40E_MDD_EVENT_PENDING, pf->state)) {
11236 		/* Since the VF MDD event logging is rate limited, check if
11237 		 * there are pending MDD events.
11238 		 */
11239 		i40e_print_vfs_mdd_events(pf);
11240 		return;
11241 	}
11242 
11243 	/* find what triggered the MDD event */
11244 	reg = rd32(hw, I40E_GL_MDET_TX);
11245 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11246 		u8 pf_num = FIELD_GET(I40E_GL_MDET_TX_PF_NUM_MASK, reg);
11247 		u16 vf_num = FIELD_GET(I40E_GL_MDET_TX_VF_NUM_MASK, reg);
11248 		u8 event = FIELD_GET(I40E_GL_MDET_TX_EVENT_MASK, reg);
11249 		u16 queue = FIELD_GET(I40E_GL_MDET_TX_QUEUE_MASK, reg) -
11250 				pf->hw.func_caps.base_queue;
11251 		if (netif_msg_tx_err(pf))
11252 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11253 				 event, queue, pf_num, vf_num);
11254 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11255 		mdd_detected = true;
11256 	}
11257 	reg = rd32(hw, I40E_GL_MDET_RX);
11258 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11259 		u8 func = FIELD_GET(I40E_GL_MDET_RX_FUNCTION_MASK, reg);
11260 		u8 event = FIELD_GET(I40E_GL_MDET_RX_EVENT_MASK, reg);
11261 		u16 queue = FIELD_GET(I40E_GL_MDET_RX_QUEUE_MASK, reg) -
11262 				pf->hw.func_caps.base_queue;
11263 		if (netif_msg_rx_err(pf))
11264 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11265 				 event, queue, func);
11266 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11267 		mdd_detected = true;
11268 	}
11269 
11270 	if (mdd_detected) {
11271 		reg = rd32(hw, I40E_PF_MDET_TX);
11272 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11273 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11274 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11275 		}
11276 		reg = rd32(hw, I40E_PF_MDET_RX);
11277 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11278 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11279 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11280 		}
11281 	}
11282 
11283 	/* see if one of the VFs needs its hand slapped */
11284 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11285 		bool is_mdd_on_tx = false;
11286 		bool is_mdd_on_rx = false;
11287 
11288 		vf = &(pf->vf[i]);
11289 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11290 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11291 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11292 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11293 			vf->mdd_tx_events.count++;
11294 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11295 			is_mdd_on_tx = true;
11296 		}
11297 
11298 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11299 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11300 			set_bit(__I40E_MDD_VF_PRINT_PENDING, pf->state);
11301 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11302 			vf->mdd_rx_events.count++;
11303 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11304 			is_mdd_on_rx = true;
11305 		}
11306 
11307 		if ((is_mdd_on_tx || is_mdd_on_rx) &&
11308 		    test_bit(I40E_FLAG_MDD_AUTO_RESET_VF, pf->flags)) {
11309 			/* VF MDD event counters will be cleared by
11310 			 * reset, so print the event prior to reset.
11311 			 */
11312 			if (is_mdd_on_rx)
11313 				i40e_print_vf_mdd_event(pf, vf, false);
11314 			if (is_mdd_on_tx)
11315 				i40e_print_vf_mdd_event(pf, vf, true);
11316 
11317 			i40e_vc_reset_vf(vf, true);
11318 		}
11319 	}
11320 
11321 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11322 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11323 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11324 	i40e_flush(hw);
11325 
11326 	i40e_print_vfs_mdd_events(pf);
11327 }
11328 
11329 /**
11330  * i40e_service_task - Run the driver's async subtasks
11331  * @work: pointer to work_struct containing our data
11332  **/
11333 static void i40e_service_task(struct work_struct *work)
11334 {
11335 	struct i40e_pf *pf = container_of(work,
11336 					  struct i40e_pf,
11337 					  service_task);
11338 	unsigned long start_time = jiffies;
11339 
11340 	/* don't bother with service tasks if a reset is in progress */
11341 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11342 	    test_bit(__I40E_SUSPENDED, pf->state))
11343 		return;
11344 
11345 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11346 		return;
11347 
11348 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11349 		i40e_detect_recover_hung(pf);
11350 		i40e_sync_filters_subtask(pf);
11351 		i40e_reset_subtask(pf);
11352 		i40e_handle_mdd_event(pf);
11353 		i40e_vc_process_vflr_event(pf);
11354 		i40e_watchdog_subtask(pf);
11355 		i40e_fdir_reinit_subtask(pf);
11356 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11357 			/* Client subtask will reopen next time through. */
11358 			i40e_notify_client_of_netdev_close(pf, true);
11359 		} else {
11360 			i40e_client_subtask(pf);
11361 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11362 					       pf->state))
11363 				i40e_notify_client_of_l2_param_changes(pf);
11364 		}
11365 		i40e_sync_filters_subtask(pf);
11366 	} else {
11367 		i40e_reset_subtask(pf);
11368 	}
11369 
11370 	i40e_clean_adminq_subtask(pf);
11371 
11372 	/* flush memory to make sure state is correct before next watchdog */
11373 	smp_mb__before_atomic();
11374 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11375 
11376 	/* If the tasks have taken longer than one timer cycle or there
11377 	 * is more work to be done, reschedule the service task now
11378 	 * rather than wait for the timer to tick again.
11379 	 */
11380 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11381 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11382 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11383 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11384 		i40e_service_event_schedule(pf);
11385 }
11386 
11387 /**
11388  * i40e_service_timer - timer callback
11389  * @t: timer list pointer
11390  **/
11391 static void i40e_service_timer(struct timer_list *t)
11392 {
11393 	struct i40e_pf *pf = timer_container_of(pf, t, service_timer);
11394 
11395 	mod_timer(&pf->service_timer,
11396 		  round_jiffies(jiffies + pf->service_timer_period));
11397 	i40e_service_event_schedule(pf);
11398 }
11399 
11400 /**
11401  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11402  * @vsi: the VSI being configured
11403  **/
11404 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11405 {
11406 	struct i40e_pf *pf = vsi->back;
11407 
11408 	switch (vsi->type) {
11409 	case I40E_VSI_MAIN:
11410 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11411 		if (!vsi->num_tx_desc)
11412 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11413 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11414 		if (!vsi->num_rx_desc)
11415 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11416 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11417 		if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11418 			vsi->num_q_vectors = pf->num_lan_msix;
11419 		else
11420 			vsi->num_q_vectors = 1;
11421 
11422 		break;
11423 
11424 	case I40E_VSI_FDIR:
11425 		vsi->alloc_queue_pairs = 1;
11426 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11427 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11428 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11429 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11430 		vsi->num_q_vectors = pf->num_fdsb_msix;
11431 		break;
11432 
11433 	case I40E_VSI_VMDQ2:
11434 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11435 		if (!vsi->num_tx_desc)
11436 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11437 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11438 		if (!vsi->num_rx_desc)
11439 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11440 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11441 		vsi->num_q_vectors = pf->num_vmdq_msix;
11442 		break;
11443 
11444 	case I40E_VSI_SRIOV:
11445 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11446 		if (!vsi->num_tx_desc)
11447 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11448 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11449 		if (!vsi->num_rx_desc)
11450 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11451 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11452 		break;
11453 
11454 	default:
11455 		WARN_ON(1);
11456 		return -ENODATA;
11457 	}
11458 
11459 	if (is_kdump_kernel()) {
11460 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11461 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11462 	}
11463 
11464 	return 0;
11465 }
11466 
11467 /**
11468  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11469  * @vsi: VSI pointer
11470  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11471  *
11472  * On error: returns error code (negative)
11473  * On success: returns 0
11474  **/
11475 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11476 {
11477 	struct i40e_ring **next_rings;
11478 	int size;
11479 	int ret = 0;
11480 
11481 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11482 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11483 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11484 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11485 	if (!vsi->tx_rings)
11486 		return -ENOMEM;
11487 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11488 	if (i40e_enabled_xdp_vsi(vsi)) {
11489 		vsi->xdp_rings = next_rings;
11490 		next_rings += vsi->alloc_queue_pairs;
11491 	}
11492 	vsi->rx_rings = next_rings;
11493 
11494 	if (alloc_qvectors) {
11495 		/* allocate memory for q_vector pointers */
11496 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11497 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11498 		if (!vsi->q_vectors) {
11499 			ret = -ENOMEM;
11500 			goto err_vectors;
11501 		}
11502 	}
11503 	return ret;
11504 
11505 err_vectors:
11506 	kfree(vsi->tx_rings);
11507 	return ret;
11508 }
11509 
11510 /**
11511  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11512  * @pf: board private structure
11513  * @type: type of VSI
11514  *
11515  * On error: returns error code (negative)
11516  * On success: returns vsi index in PF (positive)
11517  **/
11518 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11519 {
11520 	int ret = -ENODEV;
11521 	struct i40e_vsi *vsi;
11522 	int vsi_idx;
11523 	int i;
11524 
11525 	/* Need to protect the allocation of the VSIs at the PF level */
11526 	mutex_lock(&pf->switch_mutex);
11527 
11528 	/* VSI list may be fragmented if VSI creation/destruction has
11529 	 * been happening.  We can afford to do a quick scan to look
11530 	 * for any free VSIs in the list.
11531 	 *
11532 	 * find next empty vsi slot, looping back around if necessary
11533 	 */
11534 	i = pf->next_vsi;
11535 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11536 		i++;
11537 	if (i >= pf->num_alloc_vsi) {
11538 		i = 0;
11539 		while (i < pf->next_vsi && pf->vsi[i])
11540 			i++;
11541 	}
11542 
11543 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11544 		vsi_idx = i;             /* Found one! */
11545 	} else {
11546 		ret = -ENODEV;
11547 		goto unlock_pf;  /* out of VSI slots! */
11548 	}
11549 	pf->next_vsi = ++i;
11550 
11551 	vsi = kzalloc_obj(*vsi);
11552 	if (!vsi) {
11553 		ret = -ENOMEM;
11554 		goto unlock_pf;
11555 	}
11556 	vsi->type = type;
11557 	vsi->back = pf;
11558 	set_bit(__I40E_VSI_DOWN, vsi->state);
11559 	vsi->flags = 0;
11560 	vsi->idx = vsi_idx;
11561 	vsi->int_rate_limit = 0;
11562 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11563 				pf->rss_table_size : 64;
11564 	vsi->netdev_registered = false;
11565 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11566 	hash_init(vsi->mac_filter_hash);
11567 	vsi->irqs_ready = false;
11568 
11569 	if (type == I40E_VSI_MAIN) {
11570 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11571 		if (!vsi->af_xdp_zc_qps)
11572 			goto err_rings;
11573 	}
11574 
11575 	ret = i40e_set_num_rings_in_vsi(vsi);
11576 	if (ret)
11577 		goto err_rings;
11578 
11579 	ret = i40e_vsi_alloc_arrays(vsi, true);
11580 	if (ret)
11581 		goto err_rings;
11582 
11583 	/* Setup default MSIX irq handler for VSI */
11584 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11585 
11586 	/* Initialize VSI lock */
11587 	spin_lock_init(&vsi->mac_filter_hash_lock);
11588 	pf->vsi[vsi_idx] = vsi;
11589 	ret = vsi_idx;
11590 	goto unlock_pf;
11591 
11592 err_rings:
11593 	bitmap_free(vsi->af_xdp_zc_qps);
11594 	pf->next_vsi = i - 1;
11595 	kfree(vsi);
11596 unlock_pf:
11597 	mutex_unlock(&pf->switch_mutex);
11598 	return ret;
11599 }
11600 
11601 /**
11602  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11603  * @vsi: VSI pointer
11604  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11605  *
11606  * On error: returns error code (negative)
11607  * On success: returns 0
11608  **/
11609 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11610 {
11611 	/* free the ring and vector containers */
11612 	if (free_qvectors) {
11613 		kfree(vsi->q_vectors);
11614 		vsi->q_vectors = NULL;
11615 	}
11616 	kfree(vsi->tx_rings);
11617 	vsi->tx_rings = NULL;
11618 	vsi->rx_rings = NULL;
11619 	vsi->xdp_rings = NULL;
11620 }
11621 
11622 /**
11623  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11624  * and lookup table
11625  * @vsi: Pointer to VSI structure
11626  */
11627 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11628 {
11629 	if (!vsi)
11630 		return;
11631 
11632 	kfree(vsi->rss_hkey_user);
11633 	vsi->rss_hkey_user = NULL;
11634 
11635 	kfree(vsi->rss_lut_user);
11636 	vsi->rss_lut_user = NULL;
11637 }
11638 
11639 /**
11640  * i40e_vsi_clear - Deallocate the VSI provided
11641  * @vsi: the VSI being un-configured
11642  **/
11643 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11644 {
11645 	struct i40e_pf *pf;
11646 
11647 	if (!vsi)
11648 		return 0;
11649 
11650 	if (!vsi->back)
11651 		goto free_vsi;
11652 	pf = vsi->back;
11653 
11654 	mutex_lock(&pf->switch_mutex);
11655 	if (!pf->vsi[vsi->idx]) {
11656 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11657 			vsi->idx, vsi->idx, vsi->type);
11658 		goto unlock_vsi;
11659 	}
11660 
11661 	if (pf->vsi[vsi->idx] != vsi) {
11662 		dev_err(&pf->pdev->dev,
11663 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11664 			pf->vsi[vsi->idx]->idx,
11665 			pf->vsi[vsi->idx]->type,
11666 			vsi->idx, vsi->type);
11667 		goto unlock_vsi;
11668 	}
11669 
11670 	/* updates the PF for this cleared vsi */
11671 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11672 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11673 
11674 	bitmap_free(vsi->af_xdp_zc_qps);
11675 	i40e_vsi_free_arrays(vsi, true);
11676 	i40e_clear_rss_config_user(vsi);
11677 
11678 	pf->vsi[vsi->idx] = NULL;
11679 	if (vsi->idx < pf->next_vsi)
11680 		pf->next_vsi = vsi->idx;
11681 
11682 unlock_vsi:
11683 	mutex_unlock(&pf->switch_mutex);
11684 free_vsi:
11685 	kfree(vsi);
11686 
11687 	return 0;
11688 }
11689 
11690 /**
11691  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11692  * @vsi: the VSI being cleaned
11693  **/
11694 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11695 {
11696 	int i;
11697 
11698 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11699 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11700 			kfree_rcu(vsi->tx_rings[i], rcu);
11701 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11702 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11703 			if (vsi->xdp_rings)
11704 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11705 		}
11706 	}
11707 }
11708 
11709 /**
11710  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11711  * @vsi: the VSI being configured
11712  **/
11713 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11714 {
11715 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11716 	struct i40e_pf *pf = vsi->back;
11717 	struct i40e_ring *ring;
11718 
11719 	/* Set basic values in the rings to be used later during open() */
11720 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11721 		/* allocate space for both Tx and Rx in one shot */
11722 		ring = kzalloc_objs(struct i40e_ring, qpv);
11723 		if (!ring)
11724 			goto err_out;
11725 
11726 		ring->queue_index = i;
11727 		ring->reg_idx = vsi->base_queue + i;
11728 		ring->ring_active = false;
11729 		ring->vsi = vsi;
11730 		ring->netdev = vsi->netdev;
11731 		ring->dev = &pf->pdev->dev;
11732 		ring->count = vsi->num_tx_desc;
11733 		ring->size = 0;
11734 		ring->dcb_tc = 0;
11735 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11736 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11737 		ring->itr_setting = pf->tx_itr_default;
11738 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11739 
11740 		if (!i40e_enabled_xdp_vsi(vsi))
11741 			goto setup_rx;
11742 
11743 		ring->queue_index = vsi->alloc_queue_pairs + i;
11744 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11745 		ring->ring_active = false;
11746 		ring->vsi = vsi;
11747 		ring->netdev = NULL;
11748 		ring->dev = &pf->pdev->dev;
11749 		ring->count = vsi->num_tx_desc;
11750 		ring->size = 0;
11751 		ring->dcb_tc = 0;
11752 		if (test_bit(I40E_HW_CAP_WB_ON_ITR, vsi->back->hw.caps))
11753 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11754 		set_ring_xdp(ring);
11755 		ring->itr_setting = pf->tx_itr_default;
11756 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11757 
11758 setup_rx:
11759 		ring->queue_index = i;
11760 		ring->reg_idx = vsi->base_queue + i;
11761 		ring->ring_active = false;
11762 		ring->vsi = vsi;
11763 		ring->netdev = vsi->netdev;
11764 		ring->dev = &pf->pdev->dev;
11765 		ring->count = vsi->num_rx_desc;
11766 		ring->size = 0;
11767 		ring->dcb_tc = 0;
11768 		ring->itr_setting = pf->rx_itr_default;
11769 		WRITE_ONCE(vsi->rx_rings[i], ring);
11770 	}
11771 
11772 	return 0;
11773 
11774 err_out:
11775 	i40e_vsi_clear_rings(vsi);
11776 	return -ENOMEM;
11777 }
11778 
11779 /**
11780  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11781  * @pf: board private structure
11782  * @vectors: the number of MSI-X vectors to request
11783  *
11784  * Returns the number of vectors reserved, or error
11785  **/
11786 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11787 {
11788 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11789 					I40E_MIN_MSIX, vectors);
11790 	if (vectors < 0) {
11791 		dev_info(&pf->pdev->dev,
11792 			 "MSI-X vector reservation failed: %d\n", vectors);
11793 		vectors = 0;
11794 	}
11795 
11796 	return vectors;
11797 }
11798 
11799 /**
11800  * i40e_init_msix - Setup the MSIX capability
11801  * @pf: board private structure
11802  *
11803  * Work with the OS to set up the MSIX vectors needed.
11804  *
11805  * Returns the number of vectors reserved or negative on failure
11806  **/
11807 static int i40e_init_msix(struct i40e_pf *pf)
11808 {
11809 	struct i40e_hw *hw = &pf->hw;
11810 	int cpus, extra_vectors;
11811 	int vectors_left;
11812 	int v_budget, i;
11813 	int v_actual;
11814 	int iwarp_requested = 0;
11815 
11816 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
11817 		return -ENODEV;
11818 
11819 	/* The number of vectors we'll request will be comprised of:
11820 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11821 	 *   - The number of LAN queue pairs
11822 	 *	- Queues being used for RSS.
11823 	 *		We don't need as many as max_rss_size vectors.
11824 	 *		use rss_size instead in the calculation since that
11825 	 *		is governed by number of cpus in the system.
11826 	 *	- assumes symmetric Tx/Rx pairing
11827 	 *   - The number of VMDq pairs
11828 	 *   - The CPU count within the NUMA node if iWARP is enabled
11829 	 * Once we count this up, try the request.
11830 	 *
11831 	 * If we can't get what we want, we'll simplify to nearly nothing
11832 	 * and try again.  If that still fails, we punt.
11833 	 */
11834 	vectors_left = hw->func_caps.num_msix_vectors;
11835 	v_budget = 0;
11836 
11837 	/* reserve one vector for miscellaneous handler */
11838 	if (vectors_left) {
11839 		v_budget++;
11840 		vectors_left--;
11841 	}
11842 
11843 	/* reserve some vectors for the main PF traffic queues. Initially we
11844 	 * only reserve at most 50% of the available vectors, in the case that
11845 	 * the number of online CPUs is large. This ensures that we can enable
11846 	 * extra features as well. Once we've enabled the other features, we
11847 	 * will use any remaining vectors to reach as close as we can to the
11848 	 * number of online CPUs.
11849 	 */
11850 	cpus = num_online_cpus();
11851 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11852 	vectors_left -= pf->num_lan_msix;
11853 
11854 	/* reserve one vector for sideband flow director */
11855 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11856 		if (vectors_left) {
11857 			pf->num_fdsb_msix = 1;
11858 			v_budget++;
11859 			vectors_left--;
11860 		} else {
11861 			pf->num_fdsb_msix = 0;
11862 		}
11863 	}
11864 
11865 	/* can we reserve enough for iWARP? */
11866 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11867 		iwarp_requested = pf->num_iwarp_msix;
11868 
11869 		if (!vectors_left)
11870 			pf->num_iwarp_msix = 0;
11871 		else if (vectors_left < pf->num_iwarp_msix)
11872 			pf->num_iwarp_msix = 1;
11873 		v_budget += pf->num_iwarp_msix;
11874 		vectors_left -= pf->num_iwarp_msix;
11875 	}
11876 
11877 	/* any vectors left over go for VMDq support */
11878 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags)) {
11879 		if (!vectors_left) {
11880 			pf->num_vmdq_msix = 0;
11881 			pf->num_vmdq_qps = 0;
11882 		} else {
11883 			int vmdq_vecs_wanted =
11884 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11885 			int vmdq_vecs =
11886 				min_t(int, vectors_left, vmdq_vecs_wanted);
11887 
11888 			/* if we're short on vectors for what's desired, we limit
11889 			 * the queues per vmdq.  If this is still more than are
11890 			 * available, the user will need to change the number of
11891 			 * queues/vectors used by the PF later with the ethtool
11892 			 * channels command
11893 			 */
11894 			if (vectors_left < vmdq_vecs_wanted) {
11895 				pf->num_vmdq_qps = 1;
11896 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11897 				vmdq_vecs = min_t(int,
11898 						  vectors_left,
11899 						  vmdq_vecs_wanted);
11900 			}
11901 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11902 
11903 			v_budget += vmdq_vecs;
11904 			vectors_left -= vmdq_vecs;
11905 		}
11906 	}
11907 
11908 	/* On systems with a large number of SMP cores, we previously limited
11909 	 * the number of vectors for num_lan_msix to be at most 50% of the
11910 	 * available vectors, to allow for other features. Now, we add back
11911 	 * the remaining vectors. However, we ensure that the total
11912 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11913 	 * calculate the number of vectors we can add without going over the
11914 	 * cap of CPUs. For systems with a small number of CPUs this will be
11915 	 * zero.
11916 	 */
11917 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11918 	pf->num_lan_msix += extra_vectors;
11919 	vectors_left -= extra_vectors;
11920 
11921 	WARN(vectors_left < 0,
11922 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11923 
11924 	v_budget += pf->num_lan_msix;
11925 	pf->msix_entries = kzalloc_objs(struct msix_entry, v_budget);
11926 	if (!pf->msix_entries)
11927 		return -ENOMEM;
11928 
11929 	for (i = 0; i < v_budget; i++)
11930 		pf->msix_entries[i].entry = i;
11931 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11932 
11933 	if (v_actual < I40E_MIN_MSIX) {
11934 		clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
11935 		kfree(pf->msix_entries);
11936 		pf->msix_entries = NULL;
11937 		pci_disable_msix(pf->pdev);
11938 		return -ENODEV;
11939 
11940 	} else if (v_actual == I40E_MIN_MSIX) {
11941 		/* Adjust for minimal MSIX use */
11942 		pf->num_vmdq_vsis = 0;
11943 		pf->num_vmdq_qps = 0;
11944 		pf->num_lan_qps = 1;
11945 		pf->num_lan_msix = 1;
11946 
11947 	} else if (v_actual != v_budget) {
11948 		/* If we have limited resources, we will start with no vectors
11949 		 * for the special features and then allocate vectors to some
11950 		 * of these features based on the policy and at the end disable
11951 		 * the features that did not get any vectors.
11952 		 */
11953 		int vec;
11954 
11955 		dev_info(&pf->pdev->dev,
11956 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11957 			 v_actual, v_budget);
11958 		/* reserve the misc vector */
11959 		vec = v_actual - 1;
11960 
11961 		/* Scale vector usage down */
11962 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11963 		pf->num_vmdq_vsis = 1;
11964 		pf->num_vmdq_qps = 1;
11965 
11966 		/* partition out the remaining vectors */
11967 		switch (vec) {
11968 		case 2:
11969 			pf->num_lan_msix = 1;
11970 			break;
11971 		case 3:
11972 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11973 				pf->num_lan_msix = 1;
11974 				pf->num_iwarp_msix = 1;
11975 			} else {
11976 				pf->num_lan_msix = 2;
11977 			}
11978 			break;
11979 		default:
11980 			if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
11981 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11982 						 iwarp_requested);
11983 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11984 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11985 			} else {
11986 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11987 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11988 			}
11989 			if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
11990 				pf->num_fdsb_msix = 1;
11991 				vec--;
11992 			}
11993 			pf->num_lan_msix = min_t(int,
11994 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11995 							      pf->num_lan_msix);
11996 			pf->num_lan_qps = pf->num_lan_msix;
11997 			break;
11998 		}
11999 	}
12000 
12001 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) && pf->num_fdsb_msix == 0) {
12002 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
12003 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12004 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12005 	}
12006 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) && pf->num_vmdq_msix == 0) {
12007 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
12008 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12009 	}
12010 
12011 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags) &&
12012 	    pf->num_iwarp_msix == 0) {
12013 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
12014 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12015 	}
12016 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
12017 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
12018 		   pf->num_lan_msix,
12019 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
12020 		   pf->num_fdsb_msix,
12021 		   pf->num_iwarp_msix);
12022 
12023 	return v_actual;
12024 }
12025 
12026 /**
12027  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
12028  * @vsi: the VSI being configured
12029  * @v_idx: index of the vector in the vsi struct
12030  *
12031  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
12032  **/
12033 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
12034 {
12035 	struct i40e_q_vector *q_vector;
12036 
12037 	/* allocate q_vector */
12038 	q_vector = kzalloc_obj(struct i40e_q_vector);
12039 	if (!q_vector)
12040 		return -ENOMEM;
12041 
12042 	q_vector->vsi = vsi;
12043 	q_vector->v_idx = v_idx;
12044 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
12045 
12046 	if (vsi->netdev)
12047 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
12048 
12049 	/* tie q_vector and vsi together */
12050 	vsi->q_vectors[v_idx] = q_vector;
12051 
12052 	return 0;
12053 }
12054 
12055 /**
12056  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
12057  * @vsi: the VSI being configured
12058  *
12059  * We allocate one q_vector per queue interrupt.  If allocation fails we
12060  * return -ENOMEM.
12061  **/
12062 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
12063 {
12064 	struct i40e_pf *pf = vsi->back;
12065 	int err, v_idx, num_q_vectors;
12066 
12067 	/* if not MSIX, give the one vector only to the LAN VSI */
12068 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12069 		num_q_vectors = vsi->num_q_vectors;
12070 	else if (vsi->type == I40E_VSI_MAIN)
12071 		num_q_vectors = 1;
12072 	else
12073 		return -EINVAL;
12074 
12075 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12076 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12077 		if (err)
12078 			goto err_out;
12079 	}
12080 
12081 	return 0;
12082 
12083 err_out:
12084 	while (v_idx--)
12085 		i40e_free_q_vector(vsi, v_idx);
12086 
12087 	return err;
12088 }
12089 
12090 /**
12091  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12092  * @pf: board private structure to initialize
12093  **/
12094 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12095 {
12096 	int vectors = 0;
12097 	ssize_t size;
12098 
12099 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12100 		vectors = i40e_init_msix(pf);
12101 		if (vectors < 0) {
12102 			clear_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12103 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12104 			clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
12105 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
12106 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
12107 			clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12108 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12109 			clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12110 			clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12111 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12112 
12113 			/* rework the queue expectations without MSIX */
12114 			i40e_determine_queue_usage(pf);
12115 		}
12116 	}
12117 
12118 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
12119 	    test_bit(I40E_FLAG_MSI_ENA, pf->flags)) {
12120 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12121 		vectors = pci_enable_msi(pf->pdev);
12122 		if (vectors < 0) {
12123 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12124 				 vectors);
12125 			clear_bit(I40E_FLAG_MSI_ENA, pf->flags);
12126 		}
12127 		vectors = 1;  /* one MSI or Legacy vector */
12128 	}
12129 
12130 	if (!test_bit(I40E_FLAG_MSI_ENA, pf->flags) &&
12131 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
12132 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12133 
12134 	/* set up vector assignment tracking */
12135 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12136 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12137 	if (!pf->irq_pile)
12138 		return -ENOMEM;
12139 
12140 	pf->irq_pile->num_entries = vectors;
12141 
12142 	/* track first vector for misc interrupts, ignore return */
12143 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12144 
12145 	return 0;
12146 }
12147 
12148 /**
12149  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12150  * @pf: private board data structure
12151  *
12152  * Restore the interrupt scheme that was cleared when we suspended the
12153  * device. This should be called during resume to re-allocate the q_vectors
12154  * and reacquire IRQs.
12155  */
12156 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12157 {
12158 	struct i40e_vsi *vsi;
12159 	int err, i;
12160 
12161 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12162 	 * scheme. We need to re-enabled them here in order to attempt to
12163 	 * re-acquire the MSI or MSI-X vectors
12164 	 */
12165 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12166 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12167 
12168 	err = i40e_init_interrupt_scheme(pf);
12169 	if (err)
12170 		return err;
12171 
12172 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12173 	 * rings together again.
12174 	 */
12175 	i40e_pf_for_each_vsi(pf, i, vsi) {
12176 		err = i40e_vsi_alloc_q_vectors(vsi);
12177 		if (err)
12178 			goto err_unwind;
12179 
12180 		i40e_vsi_map_rings_to_vectors(vsi);
12181 	}
12182 
12183 	err = i40e_setup_misc_vector(pf);
12184 	if (err)
12185 		goto err_unwind;
12186 
12187 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags))
12188 		i40e_client_update_msix_info(pf);
12189 
12190 	return 0;
12191 
12192 err_unwind:
12193 	while (i--) {
12194 		if (pf->vsi[i])
12195 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12196 	}
12197 
12198 	return err;
12199 }
12200 
12201 /**
12202  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12203  * non queue events in recovery mode
12204  * @pf: board private structure
12205  *
12206  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12207  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12208  * This is handled differently than in recovery mode since no Tx/Rx resources
12209  * are being allocated.
12210  **/
12211 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12212 {
12213 	int err;
12214 
12215 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
12216 		err = i40e_setup_misc_vector(pf);
12217 
12218 		if (err) {
12219 			dev_info(&pf->pdev->dev,
12220 				 "MSI-X misc vector request failed, error %d\n",
12221 				 err);
12222 			return err;
12223 		}
12224 	} else {
12225 		u32 flags = test_bit(I40E_FLAG_MSI_ENA, pf->flags) ? 0 : IRQF_SHARED;
12226 
12227 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12228 				  pf->int_name, pf);
12229 
12230 		if (err) {
12231 			dev_info(&pf->pdev->dev,
12232 				 "MSI/legacy misc vector request failed, error %d\n",
12233 				 err);
12234 			return err;
12235 		}
12236 		i40e_enable_misc_int_causes(pf);
12237 		i40e_irq_dynamic_enable_icr0(pf);
12238 	}
12239 
12240 	return 0;
12241 }
12242 
12243 /**
12244  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12245  * @pf: board private structure
12246  *
12247  * This sets up the handler for MSIX 0, which is used to manage the
12248  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12249  * when in MSI or Legacy interrupt mode.
12250  **/
12251 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12252 {
12253 	struct i40e_hw *hw = &pf->hw;
12254 	int err = 0;
12255 
12256 	/* Only request the IRQ once, the first time through. */
12257 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12258 		err = request_irq(pf->msix_entries[0].vector,
12259 				  i40e_intr, 0, pf->int_name, pf);
12260 		if (err) {
12261 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12262 			dev_info(&pf->pdev->dev,
12263 				 "request_irq for %s failed: %d\n",
12264 				 pf->int_name, err);
12265 			return -EFAULT;
12266 		}
12267 	}
12268 
12269 	i40e_enable_misc_int_causes(pf);
12270 
12271 	/* associate no queues to the misc vector */
12272 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12273 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12274 
12275 	i40e_flush(hw);
12276 
12277 	i40e_irq_dynamic_enable_icr0(pf);
12278 
12279 	return err;
12280 }
12281 
12282 /**
12283  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12284  * @vsi: Pointer to vsi structure
12285  * @seed: Buffter to store the hash keys
12286  * @lut: Buffer to store the lookup table entries
12287  * @lut_size: Size of buffer to store the lookup table entries
12288  *
12289  * Return 0 on success, negative on failure
12290  */
12291 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12292 			   u8 *lut, u16 lut_size)
12293 {
12294 	struct i40e_pf *pf = vsi->back;
12295 	struct i40e_hw *hw = &pf->hw;
12296 	int ret = 0;
12297 
12298 	if (seed) {
12299 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12300 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12301 		if (ret) {
12302 			dev_info(&pf->pdev->dev,
12303 				 "Cannot get RSS key, err %pe aq_err %s\n",
12304 				 ERR_PTR(ret),
12305 				 libie_aq_str(pf->hw.aq.asq_last_status));
12306 			return ret;
12307 		}
12308 	}
12309 
12310 	if (lut) {
12311 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12312 
12313 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12314 		if (ret) {
12315 			dev_info(&pf->pdev->dev,
12316 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12317 				 ERR_PTR(ret),
12318 				 libie_aq_str(pf->hw.aq.asq_last_status));
12319 			return ret;
12320 		}
12321 	}
12322 
12323 	return ret;
12324 }
12325 
12326 /**
12327  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12328  * @vsi: Pointer to vsi structure
12329  * @seed: RSS hash seed
12330  * @lut: Lookup table
12331  * @lut_size: Lookup table size
12332  *
12333  * Returns 0 on success, negative on failure
12334  **/
12335 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12336 			       const u8 *lut, u16 lut_size)
12337 {
12338 	struct i40e_pf *pf = vsi->back;
12339 	struct i40e_hw *hw = &pf->hw;
12340 	u16 vf_id = vsi->vf_id;
12341 	u8 i;
12342 
12343 	/* Fill out hash function seed */
12344 	if (seed) {
12345 		u32 *seed_dw = (u32 *)seed;
12346 
12347 		if (vsi->type == I40E_VSI_MAIN) {
12348 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12349 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12350 		} else if (vsi->type == I40E_VSI_SRIOV) {
12351 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12352 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12353 		} else {
12354 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12355 		}
12356 	}
12357 
12358 	if (lut) {
12359 		u32 *lut_dw = (u32 *)lut;
12360 
12361 		if (vsi->type == I40E_VSI_MAIN) {
12362 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12363 				return -EINVAL;
12364 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12365 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12366 		} else if (vsi->type == I40E_VSI_SRIOV) {
12367 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12368 				return -EINVAL;
12369 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12370 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12371 		} else {
12372 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12373 		}
12374 	}
12375 	i40e_flush(hw);
12376 
12377 	return 0;
12378 }
12379 
12380 /**
12381  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12382  * @vsi: Pointer to VSI structure
12383  * @seed: Buffer to store the keys
12384  * @lut: Buffer to store the lookup table entries
12385  * @lut_size: Size of buffer to store the lookup table entries
12386  *
12387  * Returns 0 on success, negative on failure
12388  */
12389 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12390 			    u8 *lut, u16 lut_size)
12391 {
12392 	struct i40e_pf *pf = vsi->back;
12393 	struct i40e_hw *hw = &pf->hw;
12394 	u16 i;
12395 
12396 	if (seed) {
12397 		u32 *seed_dw = (u32 *)seed;
12398 
12399 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12400 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12401 	}
12402 	if (lut) {
12403 		u32 *lut_dw = (u32 *)lut;
12404 
12405 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12406 			return -EINVAL;
12407 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12408 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12409 	}
12410 
12411 	return 0;
12412 }
12413 
12414 /**
12415  * i40e_config_rss - Configure RSS keys and lut
12416  * @vsi: Pointer to VSI structure
12417  * @seed: RSS hash seed
12418  * @lut: Lookup table
12419  * @lut_size: Lookup table size
12420  *
12421  * Returns 0 on success, negative on failure
12422  */
12423 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12424 {
12425 	struct i40e_pf *pf = vsi->back;
12426 
12427 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12428 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12429 	else
12430 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12431 }
12432 
12433 /**
12434  * i40e_get_rss - Get RSS keys and lut
12435  * @vsi: Pointer to VSI structure
12436  * @seed: Buffer to store the keys
12437  * @lut: Buffer to store the lookup table entries
12438  * @lut_size: Size of buffer to store the lookup table entries
12439  *
12440  * Returns 0 on success, negative on failure
12441  */
12442 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12443 {
12444 	struct i40e_pf *pf = vsi->back;
12445 
12446 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps))
12447 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12448 	else
12449 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12450 }
12451 
12452 /**
12453  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12454  * @pf: Pointer to board private structure
12455  * @lut: Lookup table
12456  * @rss_table_size: Lookup table size
12457  * @rss_size: Range of queue number for hashing
12458  */
12459 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12460 		       u16 rss_table_size, u16 rss_size)
12461 {
12462 	u16 i;
12463 
12464 	for (i = 0; i < rss_table_size; i++)
12465 		lut[i] = i % rss_size;
12466 }
12467 
12468 /**
12469  * i40e_pf_config_rss - Prepare for RSS if used
12470  * @pf: board private structure
12471  **/
12472 static int i40e_pf_config_rss(struct i40e_pf *pf)
12473 {
12474 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12475 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12476 	u8 *lut;
12477 	struct i40e_hw *hw = &pf->hw;
12478 	u32 reg_val;
12479 	u64 hena;
12480 	int ret;
12481 
12482 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12483 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12484 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12485 	hena |= i40e_pf_get_default_rss_hashcfg(pf);
12486 
12487 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12488 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12489 
12490 	/* Determine the RSS table size based on the hardware capabilities */
12491 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12492 	reg_val = (pf->rss_table_size == 512) ?
12493 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12494 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12495 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12496 
12497 	/* Determine the RSS size of the VSI */
12498 	if (!vsi->rss_size) {
12499 		u16 qcount;
12500 		/* If the firmware does something weird during VSI init, we
12501 		 * could end up with zero TCs. Check for that to avoid
12502 		 * divide-by-zero. It probably won't pass traffic, but it also
12503 		 * won't panic.
12504 		 */
12505 		qcount = vsi->num_queue_pairs /
12506 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12507 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12508 	}
12509 	if (!vsi->rss_size)
12510 		return -EINVAL;
12511 
12512 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12513 	if (!lut)
12514 		return -ENOMEM;
12515 
12516 	/* Use user configured lut if there is one, otherwise use default */
12517 	if (vsi->rss_lut_user)
12518 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12519 	else
12520 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12521 
12522 	/* Use user configured hash key if there is one, otherwise
12523 	 * use default.
12524 	 */
12525 	if (vsi->rss_hkey_user)
12526 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12527 	else
12528 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12529 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12530 	kfree(lut);
12531 
12532 	return ret;
12533 }
12534 
12535 /**
12536  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12537  * @pf: board private structure
12538  * @queue_count: the requested queue count for rss.
12539  *
12540  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12541  * count which may be different from the requested queue count.
12542  * Note: expects to be called while under rtnl_lock()
12543  **/
12544 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12545 {
12546 	struct i40e_vsi *vsi = i40e_pf_get_main_vsi(pf);
12547 	int new_rss_size;
12548 
12549 	if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags))
12550 		return 0;
12551 
12552 	queue_count = min_t(int, queue_count, num_online_cpus());
12553 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12554 
12555 	if (queue_count != vsi->num_queue_pairs) {
12556 		u16 qcount;
12557 
12558 		vsi->req_queue_pairs = queue_count;
12559 		i40e_prep_for_reset(pf);
12560 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12561 			return pf->alloc_rss_size;
12562 
12563 		pf->alloc_rss_size = new_rss_size;
12564 
12565 		i40e_reset_and_rebuild(pf, true, true);
12566 
12567 		/* Discard the user configured hash keys and lut, if less
12568 		 * queues are enabled.
12569 		 */
12570 		if (queue_count < vsi->rss_size) {
12571 			i40e_clear_rss_config_user(vsi);
12572 			dev_dbg(&pf->pdev->dev,
12573 				"discard user configured hash keys and lut\n");
12574 		}
12575 
12576 		/* Reset vsi->rss_size, as number of enabled queues changed */
12577 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12578 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12579 
12580 		i40e_pf_config_rss(pf);
12581 	}
12582 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12583 		 vsi->req_queue_pairs, pf->rss_size_max);
12584 	return pf->alloc_rss_size;
12585 }
12586 
12587 /**
12588  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12589  * @pf: board private structure
12590  **/
12591 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12592 {
12593 	bool min_valid, max_valid;
12594 	u32 max_bw, min_bw;
12595 	int status;
12596 
12597 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12598 					   &min_valid, &max_valid);
12599 
12600 	if (!status) {
12601 		if (min_valid)
12602 			pf->min_bw = min_bw;
12603 		if (max_valid)
12604 			pf->max_bw = max_bw;
12605 	}
12606 
12607 	return status;
12608 }
12609 
12610 /**
12611  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12612  * @pf: board private structure
12613  **/
12614 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12615 {
12616 	struct i40e_aqc_configure_partition_bw_data bw_data;
12617 	int status;
12618 
12619 	memset(&bw_data, 0, sizeof(bw_data));
12620 
12621 	/* Set the valid bit for this PF */
12622 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12623 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12624 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12625 
12626 	/* Set the new bandwidths */
12627 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12628 
12629 	return status;
12630 }
12631 
12632 /**
12633  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12634  * if total port shutdown feature is enabled for this PF
12635  * @pf: board private structure
12636  **/
12637 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12638 {
12639 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12640 #define I40E_FEATURES_ENABLE_PTR		0x2A
12641 #define I40E_CURRENT_SETTING_PTR		0x2B
12642 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12643 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12644 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12645 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12646 	u16 sr_emp_sr_settings_ptr = 0;
12647 	u16 features_enable = 0;
12648 	u16 link_behavior = 0;
12649 	int read_status = 0;
12650 	bool ret = false;
12651 
12652 	read_status = i40e_read_nvm_word(&pf->hw,
12653 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12654 					 &sr_emp_sr_settings_ptr);
12655 	if (read_status)
12656 		goto err_nvm;
12657 	read_status = i40e_read_nvm_word(&pf->hw,
12658 					 sr_emp_sr_settings_ptr +
12659 					 I40E_FEATURES_ENABLE_PTR,
12660 					 &features_enable);
12661 	if (read_status)
12662 		goto err_nvm;
12663 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12664 		read_status = i40e_read_nvm_module_data(&pf->hw,
12665 							I40E_SR_EMP_SR_SETTINGS_PTR,
12666 							I40E_CURRENT_SETTING_PTR,
12667 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12668 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12669 							&link_behavior);
12670 		if (read_status)
12671 			goto err_nvm;
12672 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12673 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12674 	}
12675 	return ret;
12676 
12677 err_nvm:
12678 	dev_warn(&pf->pdev->dev,
12679 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12680 		 ERR_PTR(read_status));
12681 	return ret;
12682 }
12683 
12684 /**
12685  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12686  * @pf: board private structure to initialize
12687  *
12688  * i40e_sw_init initializes the Adapter private data structure.
12689  * Fields are initialized based on PCI device information and
12690  * OS network device settings (MTU size).
12691  **/
12692 static int i40e_sw_init(struct i40e_pf *pf)
12693 {
12694 	int err = 0;
12695 	int size;
12696 	u16 pow;
12697 
12698 	/* Set default capability flags */
12699 	bitmap_zero(pf->flags, I40E_PF_FLAGS_NBITS);
12700 	set_bit(I40E_FLAG_MSI_ENA, pf->flags);
12701 	set_bit(I40E_FLAG_MSIX_ENA, pf->flags);
12702 
12703 	/* Set default ITR */
12704 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12705 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12706 
12707 	/* Depending on PF configurations, it is possible that the RSS
12708 	 * maximum might end up larger than the available queues
12709 	 */
12710 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12711 	pf->alloc_rss_size = 1;
12712 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12713 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12714 				 pf->hw.func_caps.num_tx_qp);
12715 
12716 	/* find the next higher power-of-2 of num cpus */
12717 	pow = roundup_pow_of_two(num_online_cpus());
12718 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12719 
12720 	if (pf->hw.func_caps.rss) {
12721 		set_bit(I40E_FLAG_RSS_ENA, pf->flags);
12722 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12723 					   num_online_cpus());
12724 	}
12725 
12726 	/* MFP mode enabled */
12727 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12728 		set_bit(I40E_FLAG_MFP_ENA, pf->flags);
12729 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12730 		if (i40e_get_partition_bw_setting(pf)) {
12731 			dev_warn(&pf->pdev->dev,
12732 				 "Could not get partition bw settings\n");
12733 		} else {
12734 			dev_info(&pf->pdev->dev,
12735 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12736 				 pf->min_bw, pf->max_bw);
12737 
12738 			/* nudge the Tx scheduler */
12739 			i40e_set_partition_bw_setting(pf);
12740 		}
12741 	}
12742 
12743 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12744 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12745 		set_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
12746 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
12747 		    pf->hw.num_partitions > 1)
12748 			dev_info(&pf->pdev->dev,
12749 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12750 		else
12751 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12752 		pf->fdir_pf_filter_count =
12753 				 pf->hw.func_caps.fd_filters_guaranteed;
12754 		pf->hw.fdir_shared_filter_count =
12755 				 pf->hw.func_caps.fd_filters_best_effort;
12756 	}
12757 
12758 	/* Enable HW ATR eviction if possible */
12759 	if (test_bit(I40E_HW_CAP_ATR_EVICT, pf->hw.caps))
12760 		set_bit(I40E_FLAG_HW_ATR_EVICT_ENA, pf->flags);
12761 
12762 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12763 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12764 		set_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
12765 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12766 	}
12767 
12768 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12769 		set_bit(I40E_FLAG_IWARP_ENA, pf->flags);
12770 		/* IWARP needs one extra vector for CQP just like MISC.*/
12771 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12772 	}
12773 	/* Stopping FW LLDP engine is supported on XL710 and X722
12774 	 * starting from FW versions determined in i40e_init_adminq.
12775 	 * Stopping the FW LLDP engine is not supported on XL710
12776 	 * if NPAR is functioning so unset this hw flag in this case.
12777 	 */
12778 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12779 	    pf->hw.func_caps.npar_enable)
12780 		clear_bit(I40E_HW_CAP_FW_LLDP_STOPPABLE, pf->hw.caps);
12781 
12782 #ifdef CONFIG_PCI_IOV
12783 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12784 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12785 		set_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
12786 		pf->num_req_vfs = min_t(int,
12787 					pf->hw.func_caps.num_vfs,
12788 					I40E_MAX_VF_COUNT);
12789 	}
12790 #endif /* CONFIG_PCI_IOV */
12791 	pf->lan_veb = I40E_NO_VEB;
12792 	pf->lan_vsi = I40E_NO_VSI;
12793 
12794 	/* By default FW has this off for performance reasons */
12795 	clear_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
12796 
12797 	/* set up queue assignment tracking */
12798 	size = sizeof(struct i40e_lump_tracking)
12799 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12800 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12801 	if (!pf->qp_pile) {
12802 		err = -ENOMEM;
12803 		goto sw_init_done;
12804 	}
12805 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12806 
12807 	pf->tx_timeout_recovery_level = 1;
12808 
12809 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12810 	    i40e_is_total_port_shutdown_enabled(pf)) {
12811 		/* Link down on close must be on when total port shutdown
12812 		 * is enabled for a given port
12813 		 */
12814 		set_bit(I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENA, pf->flags);
12815 		set_bit(I40E_FLAG_LINK_DOWN_ON_CLOSE_ENA, pf->flags);
12816 		dev_info(&pf->pdev->dev,
12817 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12818 	}
12819 	mutex_init(&pf->switch_mutex);
12820 
12821 sw_init_done:
12822 	return err;
12823 }
12824 
12825 /**
12826  * i40e_set_ntuple - set the ntuple feature flag and take action
12827  * @pf: board private structure to initialize
12828  * @features: the feature set that the stack is suggesting
12829  *
12830  * returns a bool to indicate if reset needs to happen
12831  **/
12832 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12833 {
12834 	bool need_reset = false;
12835 
12836 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12837 	 * the state changed, we need to reset.
12838 	 */
12839 	if (features & NETIF_F_NTUPLE) {
12840 		/* Enable filters and mark for reset */
12841 		if (!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags))
12842 			need_reset = true;
12843 		/* enable FD_SB only if there is MSI-X vector and no cloud
12844 		 * filters exist
12845 		 */
12846 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12847 			set_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12848 			clear_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12849 		}
12850 	} else {
12851 		/* turn off filters, mark for reset and clear SW filter list */
12852 		if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
12853 			need_reset = true;
12854 			i40e_fdir_filter_exit(pf);
12855 		}
12856 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
12857 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12858 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
12859 
12860 		/* reset fd counters */
12861 		pf->fd_add_err = 0;
12862 		pf->fd_atr_cnt = 0;
12863 		/* if ATR was auto disabled it can be re-enabled. */
12864 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12865 			if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
12866 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12867 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12868 	}
12869 	return need_reset;
12870 }
12871 
12872 /**
12873  * i40e_clear_rss_lut - clear the rx hash lookup table
12874  * @vsi: the VSI being configured
12875  **/
12876 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12877 {
12878 	struct i40e_pf *pf = vsi->back;
12879 	struct i40e_hw *hw = &pf->hw;
12880 	u16 vf_id = vsi->vf_id;
12881 	u8 i;
12882 
12883 	if (vsi->type == I40E_VSI_MAIN) {
12884 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12885 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12886 	} else if (vsi->type == I40E_VSI_SRIOV) {
12887 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12888 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12889 	} else {
12890 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12891 	}
12892 }
12893 
12894 /**
12895  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12896  * @vsi: ptr to VSI
12897  * @ena: flag to indicate the on/off setting
12898  */
12899 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12900 {
12901 	bool if_running = netif_running(vsi->netdev) &&
12902 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12903 	int ret;
12904 
12905 	if (if_running)
12906 		i40e_down(vsi);
12907 
12908 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12909 	if (ret)
12910 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12911 	if (if_running)
12912 		i40e_up(vsi);
12913 
12914 	return ret;
12915 }
12916 
12917 /**
12918  * i40e_set_features - set the netdev feature flags
12919  * @netdev: ptr to the netdev being adjusted
12920  * @features: the feature set that the stack is suggesting
12921  * Note: expects to be called while under rtnl_lock()
12922  **/
12923 static int i40e_set_features(struct net_device *netdev,
12924 			     netdev_features_t features)
12925 {
12926 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12927 	struct i40e_vsi *vsi = np->vsi;
12928 	struct i40e_pf *pf = vsi->back;
12929 	bool need_reset;
12930 
12931 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12932 		i40e_pf_config_rss(pf);
12933 	else if (!(features & NETIF_F_RXHASH) &&
12934 		 netdev->features & NETIF_F_RXHASH)
12935 		i40e_clear_rss_lut(vsi);
12936 
12937 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12938 		i40e_vlan_stripping_enable(vsi);
12939 	else
12940 		i40e_vlan_stripping_disable(vsi);
12941 
12942 	if (!(features & NETIF_F_HW_TC) &&
12943 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12944 		dev_err(&pf->pdev->dev,
12945 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12946 		return -EINVAL;
12947 	}
12948 
12949 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12950 		i40e_del_all_macvlans(vsi);
12951 
12952 	need_reset = i40e_set_ntuple(pf, features);
12953 
12954 	if (need_reset)
12955 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12956 
12957 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
12958 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
12959 
12960 	return 0;
12961 }
12962 
12963 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12964 				    unsigned int table, unsigned int idx,
12965 				    struct udp_tunnel_info *ti)
12966 {
12967 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12968 	struct i40e_hw *hw = &np->vsi->back->hw;
12969 	u8 type, filter_index;
12970 	int ret;
12971 
12972 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12973 						   I40E_AQC_TUNNEL_TYPE_NGE;
12974 
12975 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12976 				     NULL);
12977 	if (ret) {
12978 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
12979 			    ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
12980 		return -EIO;
12981 	}
12982 
12983 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12984 	return 0;
12985 }
12986 
12987 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12988 				      unsigned int table, unsigned int idx,
12989 				      struct udp_tunnel_info *ti)
12990 {
12991 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12992 	struct i40e_hw *hw = &np->vsi->back->hw;
12993 	int ret;
12994 
12995 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
12996 	if (ret) {
12997 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
12998 			    ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
12999 		return -EIO;
13000 	}
13001 
13002 	return 0;
13003 }
13004 
13005 static int i40e_get_phys_port_id(struct net_device *netdev,
13006 				 struct netdev_phys_item_id *ppid)
13007 {
13008 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13009 	struct i40e_pf *pf = np->vsi->back;
13010 	struct i40e_hw *hw = &pf->hw;
13011 
13012 	if (!test_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps))
13013 		return -EOPNOTSUPP;
13014 
13015 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13016 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13017 
13018 	return 0;
13019 }
13020 
13021 /**
13022  * i40e_ndo_fdb_add - add an entry to the hardware database
13023  * @ndm: the input from the stack
13024  * @tb: pointer to array of nladdr (unused)
13025  * @dev: the net device pointer
13026  * @addr: the MAC address entry being added
13027  * @vid: VLAN ID
13028  * @flags: instructions from stack about fdb operation
13029  * @notified: whether notification was emitted
13030  * @extack: netlink extended ack, unused currently
13031  */
13032 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13033 			    struct net_device *dev,
13034 			    const unsigned char *addr, u16 vid,
13035 			    u16 flags, bool *notified,
13036 			    struct netlink_ext_ack *extack)
13037 {
13038 	struct i40e_netdev_priv *np = netdev_priv(dev);
13039 	struct i40e_pf *pf = np->vsi->back;
13040 	int err = 0;
13041 
13042 	if (!test_bit(I40E_FLAG_SRIOV_ENA, pf->flags))
13043 		return -EOPNOTSUPP;
13044 
13045 	if (vid) {
13046 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13047 		return -EINVAL;
13048 	}
13049 
13050 	/* Hardware does not support aging addresses so if a
13051 	 * ndm_state is given only allow permanent addresses
13052 	 */
13053 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13054 		netdev_info(dev, "FDB only supports static addresses\n");
13055 		return -EINVAL;
13056 	}
13057 
13058 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13059 		err = dev_uc_add_excl(dev, addr);
13060 	else if (is_multicast_ether_addr(addr))
13061 		err = dev_mc_add_excl(dev, addr);
13062 	else
13063 		err = -EINVAL;
13064 
13065 	/* Only return duplicate errors if NLM_F_EXCL is set */
13066 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13067 		err = 0;
13068 
13069 	return err;
13070 }
13071 
13072 /**
13073  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13074  * @dev: the netdev being configured
13075  * @nlh: RTNL message
13076  * @flags: bridge flags
13077  * @extack: netlink extended ack
13078  *
13079  * Inserts a new hardware bridge if not already created and
13080  * enables the bridging mode requested (VEB or VEPA). If the
13081  * hardware bridge has already been inserted and the request
13082  * is to change the mode then that requires a PF reset to
13083  * allow rebuild of the components with required hardware
13084  * bridge mode enabled.
13085  *
13086  * Note: expects to be called while under rtnl_lock()
13087  **/
13088 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13089 				   struct nlmsghdr *nlh,
13090 				   u16 flags,
13091 				   struct netlink_ext_ack *extack)
13092 {
13093 	struct i40e_netdev_priv *np = netdev_priv(dev);
13094 	struct i40e_vsi *vsi = np->vsi;
13095 	struct i40e_pf *pf = vsi->back;
13096 	struct nlattr *attr, *br_spec;
13097 	struct i40e_veb *veb;
13098 	int rem;
13099 
13100 	/* Only for PF VSI for now */
13101 	if (vsi->type != I40E_VSI_MAIN)
13102 		return -EOPNOTSUPP;
13103 
13104 	/* Find the HW bridge for PF VSI */
13105 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13106 
13107 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13108 	if (!br_spec)
13109 		return -EINVAL;
13110 
13111 	nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) {
13112 		__u16 mode = nla_get_u16(attr);
13113 
13114 		if ((mode != BRIDGE_MODE_VEPA) &&
13115 		    (mode != BRIDGE_MODE_VEB))
13116 			return -EINVAL;
13117 
13118 		/* Insert a new HW bridge */
13119 		if (!veb) {
13120 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
13121 					     vsi->tc_config.enabled_tc);
13122 			if (veb) {
13123 				veb->bridge_mode = mode;
13124 				i40e_config_bridge_mode(veb);
13125 			} else {
13126 				/* No Bridge HW offload available */
13127 				return -ENOENT;
13128 			}
13129 			break;
13130 		} else if (mode != veb->bridge_mode) {
13131 			/* Existing HW bridge but different mode needs reset */
13132 			veb->bridge_mode = mode;
13133 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13134 			if (mode == BRIDGE_MODE_VEB)
13135 				set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13136 			else
13137 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
13138 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13139 			break;
13140 		}
13141 	}
13142 
13143 	return 0;
13144 }
13145 
13146 /**
13147  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13148  * @skb: skb buff
13149  * @pid: process id
13150  * @seq: RTNL message seq #
13151  * @dev: the netdev being configured
13152  * @filter_mask: unused
13153  * @nlflags: netlink flags passed in
13154  *
13155  * Return the mode in which the hardware bridge is operating in
13156  * i.e VEB or VEPA.
13157  **/
13158 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13159 				   struct net_device *dev,
13160 				   u32 __always_unused filter_mask,
13161 				   int nlflags)
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;
13167 
13168 	/* Only for PF VSI for now */
13169 	if (vsi->type != I40E_VSI_MAIN)
13170 		return -EOPNOTSUPP;
13171 
13172 	/* Find the HW bridge for the PF VSI */
13173 	veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
13174 	if (!veb)
13175 		return 0;
13176 
13177 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13178 				       0, 0, nlflags, filter_mask, NULL);
13179 }
13180 
13181 /**
13182  * i40e_features_check - Validate encapsulated packet conforms to limits
13183  * @skb: skb buff
13184  * @dev: This physical port's netdev
13185  * @features: Offload features that the stack believes apply
13186  **/
13187 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13188 					     struct net_device *dev,
13189 					     netdev_features_t features)
13190 {
13191 	size_t len;
13192 
13193 	/* No point in doing any of this if neither checksum nor GSO are
13194 	 * being requested for this frame.  We can rule out both by just
13195 	 * checking for CHECKSUM_PARTIAL
13196 	 */
13197 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13198 		return features;
13199 
13200 	/* We cannot support GSO if the MSS is going to be less than
13201 	 * 64 bytes.  If it is then we need to drop support for GSO.
13202 	 */
13203 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13204 		features &= ~NETIF_F_GSO_MASK;
13205 
13206 	/* MACLEN can support at most 63 words */
13207 	len = skb_network_offset(skb);
13208 	if (len & ~(63 * 2))
13209 		goto out_err;
13210 
13211 	/* IPLEN and EIPLEN can support at most 127 dwords */
13212 	len = skb_network_header_len(skb);
13213 	if (len & ~(127 * 4))
13214 		goto out_err;
13215 
13216 	if (skb->encapsulation) {
13217 		/* L4TUNLEN can support 127 words */
13218 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13219 		if (len & ~(127 * 2))
13220 			goto out_err;
13221 
13222 		/* IPLEN can support at most 127 dwords */
13223 		len = skb_inner_transport_header(skb) -
13224 		      skb_inner_network_header(skb);
13225 		if (len & ~(127 * 4))
13226 			goto out_err;
13227 	}
13228 
13229 	/* No need to validate L4LEN as TCP is the only protocol with a
13230 	 * flexible value and we support all possible values supported
13231 	 * by TCP, which is at most 15 dwords
13232 	 */
13233 
13234 	return features;
13235 out_err:
13236 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13237 }
13238 
13239 /**
13240  * i40e_xdp_setup - add/remove an XDP program
13241  * @vsi: VSI to changed
13242  * @prog: XDP program
13243  * @extack: netlink extended ack
13244  **/
13245 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13246 			  struct netlink_ext_ack *extack)
13247 {
13248 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13249 	struct i40e_pf *pf = vsi->back;
13250 	struct bpf_prog *old_prog;
13251 	bool need_reset;
13252 	int i;
13253 
13254 	/* VSI shall be deleted in a moment, block loading new programs */
13255 	if (prog && test_bit(__I40E_IN_REMOVE, pf->state))
13256 		return -EINVAL;
13257 
13258 	/* Don't allow frames that span over multiple buffers */
13259 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13260 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13261 		return -EINVAL;
13262 	}
13263 
13264 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13265 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13266 	if (need_reset)
13267 		i40e_prep_for_reset(pf);
13268 
13269 	old_prog = xchg(&vsi->xdp_prog, prog);
13270 
13271 	if (need_reset) {
13272 		if (!prog) {
13273 			xdp_features_clear_redirect_target(vsi->netdev);
13274 			/* Wait until ndo_xsk_wakeup completes. */
13275 			synchronize_rcu();
13276 		}
13277 		i40e_reset_and_rebuild(pf, true, true);
13278 	}
13279 
13280 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13281 		if (i40e_realloc_rx_bi_zc(vsi, true))
13282 			return -ENOMEM;
13283 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13284 		if (i40e_realloc_rx_bi_zc(vsi, false))
13285 			return -ENOMEM;
13286 	}
13287 
13288 	for (i = 0; i < vsi->num_queue_pairs; i++)
13289 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13290 
13291 	if (old_prog)
13292 		bpf_prog_put(old_prog);
13293 
13294 	/* Kick start the NAPI context if there is an AF_XDP socket open
13295 	 * on that queue id. This so that receiving will start.
13296 	 */
13297 	if (need_reset && prog) {
13298 		for (i = 0; i < vsi->num_queue_pairs; i++)
13299 			if (vsi->xdp_rings[i]->xsk_pool)
13300 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13301 						      XDP_WAKEUP_RX);
13302 		xdp_features_set_redirect_target(vsi->netdev, true);
13303 	}
13304 
13305 	return 0;
13306 }
13307 
13308 /**
13309  * i40e_enter_busy_conf - Enters busy config state
13310  * @vsi: vsi
13311  *
13312  * Returns 0 on success, <0 for failure.
13313  **/
13314 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13315 {
13316 	struct i40e_pf *pf = vsi->back;
13317 	int timeout = 50;
13318 
13319 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13320 		timeout--;
13321 		if (!timeout)
13322 			return -EBUSY;
13323 		usleep_range(1000, 2000);
13324 	}
13325 
13326 	return 0;
13327 }
13328 
13329 /**
13330  * i40e_exit_busy_conf - Exits busy config state
13331  * @vsi: vsi
13332  **/
13333 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13334 {
13335 	struct i40e_pf *pf = vsi->back;
13336 
13337 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13338 }
13339 
13340 /**
13341  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13342  * @vsi: vsi
13343  * @queue_pair: queue pair
13344  **/
13345 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13346 {
13347 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13348 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13349 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13350 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13351 	if (i40e_enabled_xdp_vsi(vsi)) {
13352 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13353 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13354 	}
13355 }
13356 
13357 /**
13358  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13359  * @vsi: vsi
13360  * @queue_pair: queue pair
13361  **/
13362 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13363 {
13364 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13365 	if (i40e_enabled_xdp_vsi(vsi)) {
13366 		/* Make sure that in-progress ndo_xdp_xmit calls are
13367 		 * completed.
13368 		 */
13369 		synchronize_rcu();
13370 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13371 	}
13372 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13373 }
13374 
13375 /**
13376  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13377  * @vsi: vsi
13378  * @queue_pair: queue pair
13379  * @enable: true for enable, false for disable
13380  **/
13381 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13382 					bool enable)
13383 {
13384 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13385 	struct i40e_q_vector *q_vector = rxr->q_vector;
13386 
13387 	if (!vsi->netdev)
13388 		return;
13389 
13390 	/* All rings in a qp belong to the same qvector. */
13391 	if (q_vector->rx.ring || q_vector->tx.ring) {
13392 		if (enable)
13393 			napi_enable(&q_vector->napi);
13394 		else
13395 			napi_disable(&q_vector->napi);
13396 	}
13397 }
13398 
13399 /**
13400  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13401  * @vsi: vsi
13402  * @queue_pair: queue pair
13403  * @enable: true for enable, false for disable
13404  *
13405  * Returns 0 on success, <0 on failure.
13406  **/
13407 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13408 					bool enable)
13409 {
13410 	struct i40e_pf *pf = vsi->back;
13411 	int pf_q, ret = 0;
13412 
13413 	pf_q = vsi->base_queue + queue_pair;
13414 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13415 				     false /*is xdp*/, enable);
13416 	if (ret) {
13417 		dev_info(&pf->pdev->dev,
13418 			 "VSI seid %d Tx ring %d %sable timeout\n",
13419 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13420 		return ret;
13421 	}
13422 
13423 	i40e_control_rx_q(pf, pf_q, enable);
13424 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13425 	if (ret) {
13426 		dev_info(&pf->pdev->dev,
13427 			 "VSI seid %d Rx ring %d %sable timeout\n",
13428 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13429 		return ret;
13430 	}
13431 
13432 	/* Due to HW errata, on Rx disable only, the register can
13433 	 * indicate done before it really is. Needs 50ms to be sure
13434 	 */
13435 	if (!enable)
13436 		mdelay(50);
13437 
13438 	if (!i40e_enabled_xdp_vsi(vsi))
13439 		return ret;
13440 
13441 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13442 				     pf_q + vsi->alloc_queue_pairs,
13443 				     true /*is xdp*/, enable);
13444 	if (ret) {
13445 		dev_info(&pf->pdev->dev,
13446 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13447 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13448 	}
13449 
13450 	return ret;
13451 }
13452 
13453 /**
13454  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13455  * @vsi: vsi
13456  * @queue_pair: queue_pair
13457  **/
13458 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13459 {
13460 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13461 	struct i40e_pf *pf = vsi->back;
13462 	struct i40e_hw *hw = &pf->hw;
13463 
13464 	/* All rings in a qp belong to the same qvector. */
13465 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
13466 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13467 	else
13468 		i40e_irq_dynamic_enable_icr0(pf);
13469 
13470 	i40e_flush(hw);
13471 }
13472 
13473 /**
13474  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13475  * @vsi: vsi
13476  * @queue_pair: queue_pair
13477  **/
13478 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13479 {
13480 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13481 	struct i40e_pf *pf = vsi->back;
13482 	struct i40e_hw *hw = &pf->hw;
13483 
13484 	/* For simplicity, instead of removing the qp interrupt causes
13485 	 * from the interrupt linked list, we simply disable the interrupt, and
13486 	 * leave the list intact.
13487 	 *
13488 	 * All rings in a qp belong to the same qvector.
13489 	 */
13490 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
13491 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13492 
13493 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13494 		i40e_flush(hw);
13495 		synchronize_irq(pf->msix_entries[intpf].vector);
13496 	} else {
13497 		/* Legacy and MSI mode - this stops all interrupt handling */
13498 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13499 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13500 		i40e_flush(hw);
13501 		synchronize_irq(pf->pdev->irq);
13502 	}
13503 }
13504 
13505 /**
13506  * i40e_queue_pair_disable - Disables a queue pair
13507  * @vsi: vsi
13508  * @queue_pair: queue pair
13509  *
13510  * Returns 0 on success, <0 on failure.
13511  **/
13512 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13513 {
13514 	int err;
13515 
13516 	err = i40e_enter_busy_conf(vsi);
13517 	if (err)
13518 		return err;
13519 
13520 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13521 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13522 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13523 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13524 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13525 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13526 
13527 	return err;
13528 }
13529 
13530 /**
13531  * i40e_queue_pair_enable - Enables a queue pair
13532  * @vsi: vsi
13533  * @queue_pair: queue pair
13534  *
13535  * Returns 0 on success, <0 on failure.
13536  **/
13537 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13538 {
13539 	int err;
13540 
13541 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13542 	if (err)
13543 		return err;
13544 
13545 	if (i40e_enabled_xdp_vsi(vsi)) {
13546 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13547 		if (err)
13548 			return err;
13549 	}
13550 
13551 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13552 	if (err)
13553 		return err;
13554 
13555 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13556 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13557 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13558 
13559 	i40e_exit_busy_conf(vsi);
13560 
13561 	return err;
13562 }
13563 
13564 /**
13565  * i40e_xdp - implements ndo_bpf for i40e
13566  * @dev: netdevice
13567  * @xdp: XDP command
13568  **/
13569 static int i40e_xdp(struct net_device *dev,
13570 		    struct netdev_bpf *xdp)
13571 {
13572 	struct i40e_netdev_priv *np = netdev_priv(dev);
13573 	struct i40e_vsi *vsi = np->vsi;
13574 
13575 	if (vsi->type != I40E_VSI_MAIN)
13576 		return -EINVAL;
13577 
13578 	switch (xdp->command) {
13579 	case XDP_SETUP_PROG:
13580 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13581 	case XDP_SETUP_XSK_POOL:
13582 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13583 					   xdp->xsk.queue_id);
13584 	default:
13585 		return -EINVAL;
13586 	}
13587 }
13588 
13589 static int i40e_xdp_rx_hash(const struct xdp_md *_ctx, u32 *hash,
13590 			    enum xdp_rss_hash_type *rss_type)
13591 {
13592 	const struct i40e_xdp_buff *ctx = (const void *)_ctx;
13593 	const union i40e_rx_desc *desc = ctx->desc;
13594 	struct libeth_rx_pt rx_ptype;
13595 	u8 raw_rx_ptype;
13596 	u64 status;
13597 
13598 	status = le64_to_cpu(desc->wb.qword1.status_error_len);
13599 	raw_rx_ptype = FIELD_GET(I40E_RXD_QW1_PTYPE_MASK, status);
13600 	rx_ptype = libie_rx_pt_parse(raw_rx_ptype);
13601 
13602 	if (!libeth_rx_pt_has_hash(ctx->xdp.rxq->dev, rx_ptype) ||
13603 	    FIELD_GET(I40E_RX_DESC_STATUS_FLTSTAT_MASK, status) !=
13604 		    I40E_RX_DESC_FLTSTAT_RSS_HASH)
13605 		return -ENODATA;
13606 
13607 	*hash = le32_to_cpu(desc->wb.qword0.hi_dword.rss);
13608 	*rss_type = rx_ptype.hash_type;
13609 
13610 	return 0;
13611 }
13612 
13613 static int i40e_xdp_rx_vlan_tag(const struct xdp_md *_ctx, __be16 *vlan_proto,
13614 				u16 *vlan_tci)
13615 {
13616 	const struct i40e_xdp_buff *ctx = (const void *)_ctx;
13617 	const union i40e_rx_desc *desc = ctx->desc;
13618 	u64 status;
13619 
13620 	status = le64_to_cpu(desc->wb.qword1.status_error_len);
13621 
13622 	if (!(status & BIT(I40E_RX_DESC_STATUS_L2TAG1P_SHIFT)))
13623 		return -ENODATA;
13624 
13625 	*vlan_proto = cpu_to_be16(ETH_P_8021Q);
13626 	*vlan_tci = le16_to_cpu(desc->wb.qword0.lo_dword.l2tag1);
13627 
13628 	return 0;
13629 }
13630 
13631 static const struct xdp_metadata_ops i40e_xdp_metadata_ops = {
13632 	.xmo_rx_hash		= i40e_xdp_rx_hash,
13633 	.xmo_rx_vlan_tag	= i40e_xdp_rx_vlan_tag,
13634 };
13635 
13636 static const struct net_device_ops i40e_netdev_ops = {
13637 	.ndo_open		= i40e_open,
13638 	.ndo_stop		= i40e_close,
13639 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13640 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13641 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13642 	.ndo_validate_addr	= eth_validate_addr,
13643 	.ndo_set_mac_address	= i40e_set_mac,
13644 	.ndo_change_mtu		= i40e_change_mtu,
13645 	.ndo_tx_timeout		= i40e_tx_timeout,
13646 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13647 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13648 #ifdef CONFIG_NET_POLL_CONTROLLER
13649 	.ndo_poll_controller	= i40e_netpoll,
13650 #endif
13651 	.ndo_setup_tc		= __i40e_setup_tc,
13652 	.ndo_select_queue	= i40e_lan_select_queue,
13653 	.ndo_set_features	= i40e_set_features,
13654 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13655 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13656 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13657 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13658 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13659 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13660 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13661 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13662 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13663 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13664 	.ndo_features_check	= i40e_features_check,
13665 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13666 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13667 	.ndo_bpf		= i40e_xdp,
13668 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13669 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13670 	.ndo_dfwd_add_station	= i40e_fwd_add,
13671 	.ndo_dfwd_del_station	= i40e_fwd_del,
13672 	.ndo_hwtstamp_get	= i40e_ptp_hwtstamp_get,
13673 	.ndo_hwtstamp_set	= i40e_ptp_hwtstamp_set,
13674 };
13675 
13676 /**
13677  * i40e_config_netdev - Setup the netdev flags
13678  * @vsi: the VSI being configured
13679  *
13680  * Returns 0 on success, negative value on failure
13681  **/
13682 static int i40e_config_netdev(struct i40e_vsi *vsi)
13683 {
13684 	struct i40e_pf *pf = vsi->back;
13685 	struct i40e_hw *hw = &pf->hw;
13686 	struct i40e_netdev_priv *np;
13687 	struct net_device *netdev;
13688 	u8 broadcast[ETH_ALEN];
13689 	u8 mac_addr[ETH_ALEN];
13690 	int etherdev_size;
13691 	netdev_features_t hw_enc_features;
13692 	netdev_features_t hw_features;
13693 
13694 	etherdev_size = sizeof(struct i40e_netdev_priv);
13695 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13696 	if (!netdev)
13697 		return -ENOMEM;
13698 
13699 	vsi->netdev = netdev;
13700 	np = netdev_priv(netdev);
13701 	np->vsi = vsi;
13702 
13703 	hw_enc_features = NETIF_F_SG			|
13704 			  NETIF_F_HW_CSUM		|
13705 			  NETIF_F_HIGHDMA		|
13706 			  NETIF_F_SOFT_FEATURES		|
13707 			  NETIF_F_TSO			|
13708 			  NETIF_F_TSO_ECN		|
13709 			  NETIF_F_TSO6			|
13710 			  NETIF_F_GSO_GRE		|
13711 			  NETIF_F_GSO_GRE_CSUM		|
13712 			  NETIF_F_GSO_PARTIAL		|
13713 			  NETIF_F_GSO_IPXIP4		|
13714 			  NETIF_F_GSO_IPXIP6		|
13715 			  NETIF_F_GSO_UDP_TUNNEL	|
13716 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13717 			  NETIF_F_GSO_UDP_L4		|
13718 			  NETIF_F_SCTP_CRC		|
13719 			  NETIF_F_RXHASH		|
13720 			  NETIF_F_RXCSUM		|
13721 			  0;
13722 
13723 	if (!test_bit(I40E_HW_CAP_OUTER_UDP_CSUM, pf->hw.caps))
13724 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13725 
13726 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13727 
13728 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13729 
13730 	netdev->hw_enc_features |= hw_enc_features;
13731 
13732 	/* record features VLANs can make use of */
13733 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13734 
13735 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13736 				   NETIF_F_GSO_GRE_CSUM |	\
13737 				   NETIF_F_GSO_IPXIP4 |		\
13738 				   NETIF_F_GSO_IPXIP6 |		\
13739 				   NETIF_F_GSO_UDP_TUNNEL |	\
13740 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13741 
13742 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13743 	netdev->features |= NETIF_F_GSO_PARTIAL |
13744 			    I40E_GSO_PARTIAL_FEATURES;
13745 
13746 	netdev->mpls_features |= NETIF_F_SG;
13747 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13748 	netdev->mpls_features |= NETIF_F_TSO;
13749 	netdev->mpls_features |= NETIF_F_TSO6;
13750 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13751 
13752 	/* enable macvlan offloads */
13753 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13754 
13755 	hw_features = hw_enc_features		|
13756 		      NETIF_F_HW_VLAN_CTAG_TX	|
13757 		      NETIF_F_HW_VLAN_CTAG_RX;
13758 
13759 	if (!test_bit(I40E_FLAG_MFP_ENA, pf->flags))
13760 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13761 
13762 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13763 
13764 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13765 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13766 
13767 	netdev->features &= ~NETIF_F_HW_TC;
13768 
13769 	if (vsi->type == I40E_VSI_MAIN) {
13770 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13771 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13772 		/* The following steps are necessary for two reasons. First,
13773 		 * some older NVM configurations load a default MAC-VLAN
13774 		 * filter that will accept any tagged packet, and we want to
13775 		 * replace this with a normal filter. Additionally, it is
13776 		 * possible our MAC address was provided by the platform using
13777 		 * Open Firmware or similar.
13778 		 *
13779 		 * Thus, we need to remove the default filter and install one
13780 		 * specific to the MAC address.
13781 		 */
13782 		i40e_rm_default_mac_filter(vsi, mac_addr);
13783 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13784 		i40e_add_mac_filter(vsi, mac_addr);
13785 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13786 
13787 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13788 				       NETDEV_XDP_ACT_REDIRECT |
13789 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13790 				       NETDEV_XDP_ACT_RX_SG;
13791 		netdev->xdp_zc_max_segs = I40E_MAX_BUFFER_TXD;
13792 	} else {
13793 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13794 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13795 		 * the end, which is 4 bytes long, so force truncation of the
13796 		 * original name by IFNAMSIZ - 4
13797 		 */
13798 		struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
13799 
13800 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d", IFNAMSIZ - 4,
13801 			 main_vsi->netdev->name);
13802 		eth_random_addr(mac_addr);
13803 
13804 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13805 		i40e_add_mac_filter(vsi, mac_addr);
13806 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13807 	}
13808 
13809 	/* Add the broadcast filter so that we initially will receive
13810 	 * broadcast packets. Note that when a new VLAN is first added the
13811 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13812 	 * specific filters as part of transitioning into "vlan" operation.
13813 	 * When more VLANs are added, the driver will copy each existing MAC
13814 	 * filter and add it for the new VLAN.
13815 	 *
13816 	 * Broadcast filters are handled specially by
13817 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13818 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13819 	 * filter. The subtask will update the correct broadcast promiscuous
13820 	 * bits as VLANs become active or inactive.
13821 	 */
13822 	eth_broadcast_addr(broadcast);
13823 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13824 	i40e_add_mac_filter(vsi, broadcast);
13825 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13826 
13827 	eth_hw_addr_set(netdev, mac_addr);
13828 	ether_addr_copy(netdev->perm_addr, mac_addr);
13829 
13830 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13831 	netdev->neigh_priv_len = sizeof(u32) * 4;
13832 
13833 	netdev->priv_flags |= IFF_UNICAST_FLT;
13834 	/* Setup netdev TC information */
13835 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13836 
13837 	netdev->netdev_ops = &i40e_netdev_ops;
13838 	netdev->xdp_metadata_ops = &i40e_xdp_metadata_ops;
13839 	netdev->watchdog_timeo = 5 * HZ;
13840 	i40e_set_ethtool_ops(netdev);
13841 
13842 	/* MTU range: 68 - 9706 */
13843 	netdev->min_mtu = ETH_MIN_MTU;
13844 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13845 
13846 	return 0;
13847 }
13848 
13849 /**
13850  * i40e_vsi_delete - Delete a VSI from the switch
13851  * @vsi: the VSI being removed
13852  *
13853  * Returns 0 on success, negative value on failure
13854  **/
13855 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13856 {
13857 	/* remove default VSI is not allowed */
13858 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13859 		return;
13860 
13861 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13862 }
13863 
13864 /**
13865  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13866  * @vsi: the VSI being queried
13867  *
13868  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13869  **/
13870 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13871 {
13872 	struct i40e_veb *veb;
13873 	struct i40e_pf *pf = vsi->back;
13874 
13875 	/* Uplink is not a bridge so default to VEB */
13876 	if (vsi->veb_idx >= I40E_MAX_VEB)
13877 		return 1;
13878 
13879 	veb = pf->veb[vsi->veb_idx];
13880 	if (!veb) {
13881 		dev_info(&pf->pdev->dev,
13882 			 "There is no veb associated with the bridge\n");
13883 		return -ENOENT;
13884 	}
13885 
13886 	/* Uplink is a bridge in VEPA mode */
13887 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13888 		return 0;
13889 	} else {
13890 		/* Uplink is a bridge in VEB mode */
13891 		return 1;
13892 	}
13893 
13894 	/* VEPA is now default bridge, so return 0 */
13895 	return 0;
13896 }
13897 
13898 /**
13899  * i40e_add_vsi - Add a VSI to the switch
13900  * @vsi: the VSI being configured
13901  *
13902  * This initializes a VSI context depending on the VSI type to be added and
13903  * passes it down to the add_vsi aq command.
13904  **/
13905 static int i40e_add_vsi(struct i40e_vsi *vsi)
13906 {
13907 	int ret = -ENODEV;
13908 	struct i40e_pf *pf = vsi->back;
13909 	struct i40e_hw *hw = &pf->hw;
13910 	struct i40e_vsi_context ctxt;
13911 	struct i40e_mac_filter *f;
13912 	struct hlist_node *h;
13913 	int bkt;
13914 
13915 	u8 enabled_tc = 0x1; /* TC0 enabled */
13916 	int f_count = 0;
13917 
13918 	memset(&ctxt, 0, sizeof(ctxt));
13919 	switch (vsi->type) {
13920 	case I40E_VSI_MAIN:
13921 		/* The PF's main VSI is already setup as part of the
13922 		 * device initialization, so we'll not bother with
13923 		 * the add_vsi call, but we will retrieve the current
13924 		 * VSI context.
13925 		 */
13926 		ctxt.seid = pf->main_vsi_seid;
13927 		ctxt.pf_num = pf->hw.pf_id;
13928 		ctxt.vf_num = 0;
13929 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13930 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13931 		if (ret) {
13932 			dev_info(&pf->pdev->dev,
13933 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13934 				 ERR_PTR(ret),
13935 				 libie_aq_str(pf->hw.aq.asq_last_status));
13936 			return -ENOENT;
13937 		}
13938 		vsi->info = ctxt.info;
13939 		vsi->info.valid_sections = 0;
13940 
13941 		vsi->seid = ctxt.seid;
13942 		vsi->id = ctxt.vsi_number;
13943 
13944 		enabled_tc = i40e_pf_get_tc_map(pf);
13945 
13946 		/* Source pruning is enabled by default, so the flag is
13947 		 * negative logic - if it's set, we need to fiddle with
13948 		 * the VSI to disable source pruning.
13949 		 */
13950 		if (test_bit(I40E_FLAG_SOURCE_PRUNING_DIS, pf->flags)) {
13951 			memset(&ctxt, 0, sizeof(ctxt));
13952 			ctxt.seid = pf->main_vsi_seid;
13953 			ctxt.pf_num = pf->hw.pf_id;
13954 			ctxt.vf_num = 0;
13955 			ctxt.info.valid_sections |=
13956 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13957 			ctxt.info.switch_id =
13958 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13959 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13960 			if (ret) {
13961 				dev_info(&pf->pdev->dev,
13962 					 "update vsi failed, err %d aq_err %s\n",
13963 					 ret,
13964 					 libie_aq_str(pf->hw.aq.asq_last_status));
13965 				ret = -ENOENT;
13966 				goto err;
13967 			}
13968 		}
13969 
13970 		/* MFP mode setup queue map and update VSI */
13971 		if (test_bit(I40E_FLAG_MFP_ENA, pf->flags) &&
13972 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13973 			memset(&ctxt, 0, sizeof(ctxt));
13974 			ctxt.seid = pf->main_vsi_seid;
13975 			ctxt.pf_num = pf->hw.pf_id;
13976 			ctxt.vf_num = 0;
13977 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13978 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13979 			if (ret) {
13980 				dev_info(&pf->pdev->dev,
13981 					 "update vsi failed, err %pe aq_err %s\n",
13982 					 ERR_PTR(ret),
13983 					 libie_aq_str(pf->hw.aq.asq_last_status));
13984 				ret = -ENOENT;
13985 				goto err;
13986 			}
13987 			/* update the local VSI info queue map */
13988 			i40e_vsi_update_queue_map(vsi, &ctxt);
13989 			vsi->info.valid_sections = 0;
13990 		} else {
13991 			/* Default/Main VSI is only enabled for TC0
13992 			 * reconfigure it to enable all TCs that are
13993 			 * available on the port in SFP mode.
13994 			 * For MFP case the iSCSI PF would use this
13995 			 * flow to enable LAN+iSCSI TC.
13996 			 */
13997 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13998 			if (ret) {
13999 				/* Single TC condition is not fatal,
14000 				 * message and continue
14001 				 */
14002 				dev_info(&pf->pdev->dev,
14003 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14004 					 enabled_tc,
14005 					 ERR_PTR(ret),
14006 					 libie_aq_str(pf->hw.aq.asq_last_status));
14007 			}
14008 		}
14009 		break;
14010 
14011 	case I40E_VSI_FDIR:
14012 		ctxt.pf_num = hw->pf_id;
14013 		ctxt.vf_num = 0;
14014 		ctxt.uplink_seid = vsi->uplink_seid;
14015 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14016 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14017 		if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags) &&
14018 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14019 			ctxt.info.valid_sections |=
14020 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14021 			ctxt.info.switch_id =
14022 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14023 		}
14024 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14025 		break;
14026 
14027 	case I40E_VSI_VMDQ2:
14028 		ctxt.pf_num = hw->pf_id;
14029 		ctxt.vf_num = 0;
14030 		ctxt.uplink_seid = vsi->uplink_seid;
14031 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14032 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14033 
14034 		/* This VSI is connected to VEB so the switch_id
14035 		 * should be set to zero by default.
14036 		 */
14037 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14038 			ctxt.info.valid_sections |=
14039 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14040 			ctxt.info.switch_id =
14041 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14042 		}
14043 
14044 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14045 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14046 		break;
14047 
14048 	case I40E_VSI_SRIOV:
14049 		ctxt.pf_num = hw->pf_id;
14050 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14051 		ctxt.uplink_seid = vsi->uplink_seid;
14052 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14053 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14054 
14055 		/* This VSI is connected to VEB so the switch_id
14056 		 * should be set to zero by default.
14057 		 */
14058 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14059 			ctxt.info.valid_sections |=
14060 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14061 			ctxt.info.switch_id =
14062 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14063 		}
14064 
14065 		if (test_bit(I40E_FLAG_IWARP_ENA, vsi->back->flags)) {
14066 			ctxt.info.valid_sections |=
14067 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14068 			ctxt.info.queueing_opt_flags |=
14069 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14070 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14071 		}
14072 
14073 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14074 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14075 		if (pf->vf[vsi->vf_id].spoofchk) {
14076 			ctxt.info.valid_sections |=
14077 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14078 			ctxt.info.sec_flags |=
14079 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14080 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14081 		}
14082 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14083 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14084 		break;
14085 
14086 	case I40E_VSI_IWARP:
14087 		/* send down message to iWARP */
14088 		break;
14089 
14090 	default:
14091 		return -ENODEV;
14092 	}
14093 
14094 	if (vsi->type != I40E_VSI_MAIN) {
14095 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14096 		if (ret) {
14097 			dev_info(&vsi->back->pdev->dev,
14098 				 "add vsi failed, err %pe aq_err %s\n",
14099 				 ERR_PTR(ret),
14100 				 libie_aq_str(pf->hw.aq.asq_last_status));
14101 			ret = -ENOENT;
14102 			goto err;
14103 		}
14104 		vsi->info = ctxt.info;
14105 		vsi->info.valid_sections = 0;
14106 		vsi->seid = ctxt.seid;
14107 		vsi->id = ctxt.vsi_number;
14108 	}
14109 
14110 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14111 	vsi->active_filters = 0;
14112 	/* If macvlan filters already exist, force them to get loaded */
14113 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14114 		f->state = I40E_FILTER_NEW;
14115 		f_count++;
14116 	}
14117 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14118 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14119 
14120 	if (f_count) {
14121 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14122 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14123 	}
14124 
14125 	/* Update VSI BW information */
14126 	ret = i40e_vsi_get_bw_info(vsi);
14127 	if (ret) {
14128 		dev_info(&pf->pdev->dev,
14129 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14130 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14131 		/* VSI is already added so not tearing that up */
14132 		ret = 0;
14133 	}
14134 
14135 err:
14136 	return ret;
14137 }
14138 
14139 /**
14140  * i40e_vsi_release - Delete a VSI and free its resources
14141  * @vsi: the VSI being removed
14142  *
14143  * Returns 0 on success or < 0 on error
14144  **/
14145 int i40e_vsi_release(struct i40e_vsi *vsi)
14146 {
14147 	struct i40e_mac_filter *f;
14148 	struct hlist_node *h;
14149 	struct i40e_veb *veb;
14150 	struct i40e_pf *pf;
14151 	u16 uplink_seid;
14152 	int i, n, bkt;
14153 
14154 	pf = vsi->back;
14155 
14156 	/* release of a VEB-owner or last VSI is not allowed */
14157 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14158 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14159 			 vsi->seid, vsi->uplink_seid);
14160 		return -ENODEV;
14161 	}
14162 	if (vsi->type == I40E_VSI_MAIN && !test_bit(__I40E_DOWN, pf->state)) {
14163 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14164 		return -ENODEV;
14165 	}
14166 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14167 	uplink_seid = vsi->uplink_seid;
14168 
14169 	if (vsi->type != I40E_VSI_SRIOV) {
14170 		if (vsi->netdev_registered) {
14171 			vsi->netdev_registered = false;
14172 			if (vsi->netdev) {
14173 				/* results in a call to i40e_close() */
14174 				unregister_netdev(vsi->netdev);
14175 			}
14176 		} else {
14177 			i40e_vsi_close(vsi);
14178 		}
14179 		i40e_vsi_disable_irq(vsi);
14180 	}
14181 
14182 	if (vsi->type == I40E_VSI_MAIN)
14183 		i40e_devlink_destroy_port(pf);
14184 
14185 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14186 
14187 	/* clear the sync flag on all filters */
14188 	if (vsi->netdev) {
14189 		__dev_uc_unsync(vsi->netdev, NULL);
14190 		__dev_mc_unsync(vsi->netdev, NULL);
14191 	}
14192 
14193 	/* make sure any remaining filters are marked for deletion */
14194 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14195 		__i40e_del_filter(vsi, f);
14196 
14197 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14198 
14199 	i40e_sync_vsi_filters(vsi);
14200 
14201 	i40e_vsi_delete(vsi);
14202 	i40e_vsi_free_q_vectors(vsi);
14203 	if (vsi->netdev) {
14204 		free_netdev(vsi->netdev);
14205 		vsi->netdev = NULL;
14206 	}
14207 	i40e_vsi_clear_rings(vsi);
14208 	i40e_vsi_clear(vsi);
14209 
14210 	/* If this was the last thing on the VEB, except for the
14211 	 * controlling VSI, remove the VEB, which puts the controlling
14212 	 * VSI onto the uplink port.
14213 	 *
14214 	 * Well, okay, there's one more exception here: don't remove
14215 	 * the floating VEBs yet.  We'll wait for an explicit remove request
14216 	 * from up the network stack.
14217 	 */
14218 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14219 	if (veb && veb->uplink_seid) {
14220 		n = 0;
14221 
14222 		/* Count non-controlling VSIs present on  the VEB */
14223 		i40e_pf_for_each_vsi(pf, i, vsi)
14224 			if (vsi->uplink_seid == uplink_seid &&
14225 			    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14226 				n++;
14227 
14228 		/* If there is no VSI except the control one then release
14229 		 * the VEB and put the control VSI onto VEB uplink.
14230 		 */
14231 		if (!n)
14232 			i40e_veb_release(veb);
14233 	}
14234 
14235 	return 0;
14236 }
14237 
14238 /**
14239  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14240  * @vsi: ptr to the VSI
14241  *
14242  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14243  * corresponding SW VSI structure and initializes num_queue_pairs for the
14244  * newly allocated VSI.
14245  *
14246  * Returns 0 on success or negative on failure
14247  **/
14248 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14249 {
14250 	int ret = -ENOENT;
14251 	struct i40e_pf *pf = vsi->back;
14252 
14253 	if (vsi->q_vectors[0]) {
14254 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14255 			 vsi->seid);
14256 		return -EEXIST;
14257 	}
14258 
14259 	if (vsi->base_vector) {
14260 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14261 			 vsi->seid, vsi->base_vector);
14262 		return -EEXIST;
14263 	}
14264 
14265 	ret = i40e_vsi_alloc_q_vectors(vsi);
14266 	if (ret) {
14267 		dev_info(&pf->pdev->dev,
14268 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14269 			 vsi->num_q_vectors, vsi->seid, ret);
14270 		vsi->num_q_vectors = 0;
14271 		goto vector_setup_out;
14272 	}
14273 
14274 	/* In Legacy mode, we do not have to get any other vector since we
14275 	 * piggyback on the misc/ICR0 for queue interrupts.
14276 	*/
14277 	if (!test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
14278 		return ret;
14279 	if (vsi->num_q_vectors)
14280 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14281 						 vsi->num_q_vectors, vsi->idx);
14282 	if (vsi->base_vector < 0) {
14283 		dev_info(&pf->pdev->dev,
14284 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14285 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14286 		i40e_vsi_free_q_vectors(vsi);
14287 		ret = -ENOENT;
14288 		goto vector_setup_out;
14289 	}
14290 
14291 vector_setup_out:
14292 	return ret;
14293 }
14294 
14295 /**
14296  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14297  * @vsi: pointer to the vsi.
14298  *
14299  * This re-allocates a vsi's queue resources.
14300  *
14301  * Returns pointer to the successfully allocated and configured VSI sw struct
14302  * on success, otherwise returns NULL on failure.
14303  **/
14304 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14305 {
14306 	struct i40e_vsi *main_vsi;
14307 	u16 alloc_queue_pairs;
14308 	struct i40e_pf *pf;
14309 	int ret;
14310 
14311 	if (!vsi)
14312 		return NULL;
14313 
14314 	pf = vsi->back;
14315 
14316 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14317 	i40e_vsi_clear_rings(vsi);
14318 
14319 	i40e_vsi_free_arrays(vsi, false);
14320 	i40e_set_num_rings_in_vsi(vsi);
14321 	ret = i40e_vsi_alloc_arrays(vsi, false);
14322 	if (ret)
14323 		goto err_vsi;
14324 
14325 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14326 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14327 
14328 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14329 	if (ret < 0) {
14330 		dev_info(&pf->pdev->dev,
14331 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14332 			 alloc_queue_pairs, vsi->seid, ret);
14333 		goto err_vsi;
14334 	}
14335 	vsi->base_queue = ret;
14336 
14337 	/* Update the FW view of the VSI. Force a reset of TC and queue
14338 	 * layout configurations.
14339 	 */
14340 	main_vsi = i40e_pf_get_main_vsi(pf);
14341 	main_vsi->seid = pf->main_vsi_seid;
14342 	i40e_vsi_reconfig_tc(main_vsi);
14343 
14344 	if (vsi->type == I40E_VSI_MAIN)
14345 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14346 
14347 	/* assign it some queues */
14348 	ret = i40e_alloc_rings(vsi);
14349 	if (ret)
14350 		goto err_rings;
14351 
14352 	/* map all of the rings to the q_vectors */
14353 	i40e_vsi_map_rings_to_vectors(vsi);
14354 	return vsi;
14355 
14356 err_rings:
14357 	i40e_vsi_free_q_vectors(vsi);
14358 	if (vsi->netdev_registered) {
14359 		vsi->netdev_registered = false;
14360 		unregister_netdev(vsi->netdev);
14361 		free_netdev(vsi->netdev);
14362 		vsi->netdev = NULL;
14363 	}
14364 	if (vsi->type == I40E_VSI_MAIN)
14365 		i40e_devlink_destroy_port(pf);
14366 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14367 err_vsi:
14368 	i40e_vsi_clear(vsi);
14369 	return NULL;
14370 }
14371 
14372 /**
14373  * i40e_vsi_setup - Set up a VSI by a given type
14374  * @pf: board private structure
14375  * @type: VSI type
14376  * @uplink_seid: the switch element to link to
14377  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14378  *
14379  * This allocates the sw VSI structure and its queue resources, then add a VSI
14380  * to the identified VEB.
14381  *
14382  * Returns pointer to the successfully allocated and configure VSI sw struct on
14383  * success, otherwise returns NULL on failure.
14384  **/
14385 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14386 				u16 uplink_seid, u32 param1)
14387 {
14388 	struct i40e_vsi *vsi = NULL;
14389 	struct i40e_veb *veb = NULL;
14390 	u16 alloc_queue_pairs;
14391 	int v_idx;
14392 	int ret;
14393 
14394 	/* The requested uplink_seid must be either
14395 	 *     - the PF's port seid
14396 	 *              no VEB is needed because this is the PF
14397 	 *              or this is a Flow Director special case VSI
14398 	 *     - seid of an existing VEB
14399 	 *     - seid of a VSI that owns an existing VEB
14400 	 *     - seid of a VSI that doesn't own a VEB
14401 	 *              a new VEB is created and the VSI becomes the owner
14402 	 *     - seid of the PF VSI, which is what creates the first VEB
14403 	 *              this is a special case of the previous
14404 	 *
14405 	 * Find which uplink_seid we were given and create a new VEB if needed
14406 	 */
14407 	veb = i40e_pf_get_veb_by_seid(pf, uplink_seid);
14408 	if (!veb && uplink_seid != pf->mac_seid) {
14409 		vsi = i40e_pf_get_vsi_by_seid(pf, uplink_seid);
14410 		if (!vsi) {
14411 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14412 				 uplink_seid);
14413 			return NULL;
14414 		}
14415 
14416 		if (vsi->uplink_seid == pf->mac_seid)
14417 			veb = i40e_veb_setup(pf, pf->mac_seid, vsi->seid,
14418 					     vsi->tc_config.enabled_tc);
14419 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14420 			veb = i40e_veb_setup(pf, vsi->uplink_seid, vsi->seid,
14421 					     vsi->tc_config.enabled_tc);
14422 		if (veb) {
14423 			if (vsi->type != I40E_VSI_MAIN) {
14424 				dev_info(&vsi->back->pdev->dev,
14425 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14426 				return NULL;
14427 			}
14428 			/* We come up by default in VEPA mode if SRIOV is not
14429 			 * already enabled, in which case we can't force VEPA
14430 			 * mode.
14431 			 */
14432 			if (!test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags)) {
14433 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14434 				clear_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
14435 			}
14436 			i40e_config_bridge_mode(veb);
14437 		}
14438 		veb = i40e_pf_get_veb_by_seid(pf, vsi->uplink_seid);
14439 		if (!veb) {
14440 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14441 			return NULL;
14442 		}
14443 
14444 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14445 		uplink_seid = veb->seid;
14446 	}
14447 
14448 	/* get vsi sw struct */
14449 	v_idx = i40e_vsi_mem_alloc(pf, type);
14450 	if (v_idx < 0)
14451 		goto err_alloc;
14452 	vsi = pf->vsi[v_idx];
14453 	if (!vsi)
14454 		goto err_alloc;
14455 	vsi->type = type;
14456 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14457 
14458 	if (type == I40E_VSI_MAIN)
14459 		pf->lan_vsi = v_idx;
14460 	else if (type == I40E_VSI_SRIOV)
14461 		vsi->vf_id = param1;
14462 	/* assign it some queues */
14463 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14464 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14465 
14466 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14467 	if (ret < 0) {
14468 		dev_info(&pf->pdev->dev,
14469 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14470 			 alloc_queue_pairs, vsi->seid, ret);
14471 		goto err_vsi;
14472 	}
14473 	vsi->base_queue = ret;
14474 
14475 	/* get a VSI from the hardware */
14476 	vsi->uplink_seid = uplink_seid;
14477 	ret = i40e_add_vsi(vsi);
14478 	if (ret)
14479 		goto err_vsi;
14480 
14481 	switch (vsi->type) {
14482 	/* setup the netdev if needed */
14483 	case I40E_VSI_MAIN:
14484 	case I40E_VSI_VMDQ2:
14485 		ret = i40e_config_netdev(vsi);
14486 		if (ret)
14487 			goto err_netdev;
14488 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14489 		if (ret)
14490 			goto err_netdev;
14491 		if (vsi->type == I40E_VSI_MAIN) {
14492 			ret = i40e_devlink_create_port(pf);
14493 			if (ret)
14494 				goto err_netdev;
14495 			SET_NETDEV_DEVLINK_PORT(vsi->netdev, &pf->devlink_port);
14496 		}
14497 		ret = register_netdev(vsi->netdev);
14498 		if (ret)
14499 			goto err_dl_port;
14500 		vsi->netdev_registered = true;
14501 		netif_carrier_off(vsi->netdev);
14502 #ifdef CONFIG_I40E_DCB
14503 		/* Setup DCB netlink interface */
14504 		i40e_dcbnl_setup(vsi);
14505 #endif /* CONFIG_I40E_DCB */
14506 		fallthrough;
14507 	case I40E_VSI_FDIR:
14508 		/* set up vectors and rings if needed */
14509 		ret = i40e_vsi_setup_vectors(vsi);
14510 		if (ret)
14511 			goto err_msix;
14512 
14513 		ret = i40e_alloc_rings(vsi);
14514 		if (ret)
14515 			goto err_rings;
14516 
14517 		/* map all of the rings to the q_vectors */
14518 		i40e_vsi_map_rings_to_vectors(vsi);
14519 
14520 		i40e_vsi_reset_stats(vsi);
14521 		break;
14522 	default:
14523 		/* no netdev or rings for the other VSI types */
14524 		break;
14525 	}
14526 
14527 	if (test_bit(I40E_HW_CAP_RSS_AQ, pf->hw.caps) &&
14528 	    vsi->type == I40E_VSI_VMDQ2) {
14529 		ret = i40e_vsi_config_rss(vsi);
14530 		if (ret)
14531 			goto err_config;
14532 	}
14533 	return vsi;
14534 
14535 err_config:
14536 	i40e_vsi_clear_rings(vsi);
14537 err_rings:
14538 	i40e_vsi_free_q_vectors(vsi);
14539 err_msix:
14540 	if (vsi->netdev_registered) {
14541 		vsi->netdev_registered = false;
14542 		unregister_netdev(vsi->netdev);
14543 		free_netdev(vsi->netdev);
14544 		vsi->netdev = NULL;
14545 	}
14546 err_dl_port:
14547 	if (vsi->type == I40E_VSI_MAIN)
14548 		i40e_devlink_destroy_port(pf);
14549 err_netdev:
14550 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14551 err_vsi:
14552 	i40e_vsi_clear(vsi);
14553 err_alloc:
14554 	return NULL;
14555 }
14556 
14557 /**
14558  * i40e_veb_get_bw_info - Query VEB BW information
14559  * @veb: the veb to query
14560  *
14561  * Query the Tx scheduler BW configuration data for given VEB
14562  **/
14563 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14564 {
14565 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14566 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14567 	struct i40e_pf *pf = veb->pf;
14568 	struct i40e_hw *hw = &pf->hw;
14569 	u32 tc_bw_max;
14570 	int ret = 0;
14571 	int i;
14572 
14573 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14574 						  &bw_data, NULL);
14575 	if (ret) {
14576 		dev_info(&pf->pdev->dev,
14577 			 "query veb bw config failed, err %pe aq_err %s\n",
14578 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
14579 		goto out;
14580 	}
14581 
14582 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14583 						   &ets_data, NULL);
14584 	if (ret) {
14585 		dev_info(&pf->pdev->dev,
14586 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14587 			 ERR_PTR(ret), libie_aq_str(hw->aq.asq_last_status));
14588 		goto out;
14589 	}
14590 
14591 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14592 	veb->bw_max_quanta = ets_data.tc_bw_max;
14593 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14594 	veb->enabled_tc = ets_data.tc_valid_bits;
14595 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14596 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14597 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14598 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14599 		veb->bw_tc_limit_credits[i] =
14600 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14601 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14602 	}
14603 
14604 out:
14605 	return ret;
14606 }
14607 
14608 /**
14609  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14610  * @pf: board private structure
14611  *
14612  * On error: returns error code (negative)
14613  * On success: returns vsi index in PF (positive)
14614  **/
14615 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14616 {
14617 	int ret = -ENOENT;
14618 	struct i40e_veb *veb;
14619 	int i;
14620 
14621 	/* Need to protect the allocation of switch elements at the PF level */
14622 	mutex_lock(&pf->switch_mutex);
14623 
14624 	/* VEB list may be fragmented if VEB creation/destruction has
14625 	 * been happening.  We can afford to do a quick scan to look
14626 	 * for any free slots in the list.
14627 	 *
14628 	 * find next empty veb slot, looping back around if necessary
14629 	 */
14630 	i = 0;
14631 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14632 		i++;
14633 	if (i >= I40E_MAX_VEB) {
14634 		ret = -ENOMEM;
14635 		goto err_alloc_veb;  /* out of VEB slots! */
14636 	}
14637 
14638 	veb = kzalloc_obj(*veb);
14639 	if (!veb) {
14640 		ret = -ENOMEM;
14641 		goto err_alloc_veb;
14642 	}
14643 	veb->pf = pf;
14644 	veb->idx = i;
14645 	veb->enabled_tc = 1;
14646 
14647 	pf->veb[i] = veb;
14648 	ret = i;
14649 err_alloc_veb:
14650 	mutex_unlock(&pf->switch_mutex);
14651 	return ret;
14652 }
14653 
14654 /**
14655  * i40e_switch_branch_release - Delete a branch of the switch tree
14656  * @branch: where to start deleting
14657  *
14658  * This uses recursion to find the tips of the branch to be
14659  * removed, deleting until we get back to and can delete this VEB.
14660  **/
14661 static void i40e_switch_branch_release(struct i40e_veb *branch)
14662 {
14663 	struct i40e_pf *pf = branch->pf;
14664 	u16 branch_seid = branch->seid;
14665 	u16 veb_idx = branch->idx;
14666 	struct i40e_vsi *vsi;
14667 	struct i40e_veb *veb;
14668 	int i;
14669 
14670 	/* release any VEBs on this VEB - RECURSION */
14671 	i40e_pf_for_each_veb(pf, i, veb)
14672 		if (veb->uplink_seid == branch->seid)
14673 			i40e_switch_branch_release(veb);
14674 
14675 	/* Release the VSIs on this VEB, but not the owner VSI.
14676 	 *
14677 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14678 	 *       the VEB itself, so don't use (*branch) after this loop.
14679 	 */
14680 	i40e_pf_for_each_vsi(pf, i, vsi)
14681 		if (vsi->uplink_seid == branch_seid &&
14682 		    (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14683 			i40e_vsi_release(vsi);
14684 
14685 	/* There's one corner case where the VEB might not have been
14686 	 * removed, so double check it here and remove it if needed.
14687 	 * This case happens if the veb was created from the debugfs
14688 	 * commands and no VSIs were added to it.
14689 	 */
14690 	if (pf->veb[veb_idx])
14691 		i40e_veb_release(pf->veb[veb_idx]);
14692 }
14693 
14694 /**
14695  * i40e_veb_clear - remove veb struct
14696  * @veb: the veb to remove
14697  **/
14698 static void i40e_veb_clear(struct i40e_veb *veb)
14699 {
14700 	if (!veb)
14701 		return;
14702 
14703 	if (veb->pf) {
14704 		struct i40e_pf *pf = veb->pf;
14705 
14706 		mutex_lock(&pf->switch_mutex);
14707 		if (pf->veb[veb->idx] == veb)
14708 			pf->veb[veb->idx] = NULL;
14709 		mutex_unlock(&pf->switch_mutex);
14710 	}
14711 
14712 	kfree(veb);
14713 }
14714 
14715 /**
14716  * i40e_veb_release - Delete a VEB and free its resources
14717  * @veb: the VEB being removed
14718  **/
14719 void i40e_veb_release(struct i40e_veb *veb)
14720 {
14721 	struct i40e_vsi *vsi, *vsi_it;
14722 	struct i40e_pf *pf;
14723 	int i, n = 0;
14724 
14725 	pf = veb->pf;
14726 
14727 	/* find the remaining VSI and check for extras */
14728 	i40e_pf_for_each_vsi(pf, i, vsi_it)
14729 		if (vsi_it->uplink_seid == veb->seid) {
14730 			if (vsi_it->flags & I40E_VSI_FLAG_VEB_OWNER)
14731 				vsi = vsi_it;
14732 			n++;
14733 		}
14734 
14735 	/* Floating VEB has to be empty and regular one must have
14736 	 * single owner VSI.
14737 	 */
14738 	if ((veb->uplink_seid && n != 1) || (!veb->uplink_seid && n != 0)) {
14739 		dev_info(&pf->pdev->dev,
14740 			 "can't remove VEB %d with %d VSIs left\n",
14741 			 veb->seid, n);
14742 		return;
14743 	}
14744 
14745 	/* For regular VEB move the owner VSI to uplink port */
14746 	if (veb->uplink_seid) {
14747 		vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14748 		vsi->uplink_seid = veb->uplink_seid;
14749 		vsi->veb_idx = I40E_NO_VEB;
14750 	}
14751 
14752 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14753 	i40e_veb_clear(veb);
14754 }
14755 
14756 /**
14757  * i40e_add_veb - create the VEB in the switch
14758  * @veb: the VEB to be instantiated
14759  * @vsi: the controlling VSI
14760  **/
14761 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14762 {
14763 	struct i40e_pf *pf = veb->pf;
14764 	bool enable_stats = !!test_bit(I40E_FLAG_VEB_STATS_ENA, pf->flags);
14765 	int ret;
14766 
14767 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi ? vsi->seid : 0,
14768 			      veb->enabled_tc, vsi ? false : true,
14769 			      &veb->seid, enable_stats, NULL);
14770 
14771 	/* get a VEB from the hardware */
14772 	if (ret) {
14773 		dev_info(&pf->pdev->dev,
14774 			 "couldn't add VEB, err %pe aq_err %s\n",
14775 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14776 		return -EPERM;
14777 	}
14778 
14779 	/* get statistics counter */
14780 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14781 					 &veb->stats_idx, NULL, NULL, NULL);
14782 	if (ret) {
14783 		dev_info(&pf->pdev->dev,
14784 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14785 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14786 		return -EPERM;
14787 	}
14788 	ret = i40e_veb_get_bw_info(veb);
14789 	if (ret) {
14790 		dev_info(&pf->pdev->dev,
14791 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14792 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
14793 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14794 		return -ENOENT;
14795 	}
14796 
14797 	if (vsi) {
14798 		vsi->uplink_seid = veb->seid;
14799 		vsi->veb_idx = veb->idx;
14800 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14801 	}
14802 
14803 	return 0;
14804 }
14805 
14806 /**
14807  * i40e_veb_setup - Set up a VEB
14808  * @pf: board private structure
14809  * @uplink_seid: the switch element to link to
14810  * @vsi_seid: the initial VSI seid
14811  * @enabled_tc: Enabled TC bit-map
14812  *
14813  * This allocates the sw VEB structure and links it into the switch
14814  * It is possible and legal for this to be a duplicate of an already
14815  * existing VEB.  It is also possible for both uplink and vsi seids
14816  * to be zero, in order to create a floating VEB.
14817  *
14818  * Returns pointer to the successfully allocated VEB sw struct on
14819  * success, otherwise returns NULL on failure.
14820  **/
14821 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 uplink_seid,
14822 				u16 vsi_seid, u8 enabled_tc)
14823 {
14824 	struct i40e_vsi *vsi = NULL;
14825 	struct i40e_veb *veb;
14826 	int veb_idx;
14827 	int ret;
14828 
14829 	/* if one seid is 0, the other must be 0 to create a floating relay */
14830 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14831 	    (uplink_seid + vsi_seid != 0)) {
14832 		dev_info(&pf->pdev->dev,
14833 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14834 			 uplink_seid, vsi_seid);
14835 		return NULL;
14836 	}
14837 
14838 	/* make sure there is such a vsi and uplink */
14839 	if (vsi_seid) {
14840 		vsi = i40e_pf_get_vsi_by_seid(pf, vsi_seid);
14841 		if (!vsi) {
14842 			dev_err(&pf->pdev->dev, "vsi seid %d not found\n",
14843 				vsi_seid);
14844 			return NULL;
14845 		}
14846 	}
14847 
14848 	/* get veb sw struct */
14849 	veb_idx = i40e_veb_mem_alloc(pf);
14850 	if (veb_idx < 0)
14851 		goto err_alloc;
14852 	veb = pf->veb[veb_idx];
14853 	veb->uplink_seid = uplink_seid;
14854 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14855 
14856 	/* create the VEB in the switch */
14857 	ret = i40e_add_veb(veb, vsi);
14858 	if (ret)
14859 		goto err_veb;
14860 
14861 	if (vsi && vsi->idx == pf->lan_vsi)
14862 		pf->lan_veb = veb->idx;
14863 
14864 	return veb;
14865 
14866 err_veb:
14867 	i40e_veb_clear(veb);
14868 err_alloc:
14869 	return NULL;
14870 }
14871 
14872 /**
14873  * i40e_setup_pf_switch_element - set PF vars based on switch type
14874  * @pf: board private structure
14875  * @ele: element we are building info from
14876  * @num_reported: total number of elements
14877  * @printconfig: should we print the contents
14878  *
14879  * helper function to assist in extracting a few useful SEID values.
14880  **/
14881 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14882 				struct i40e_aqc_switch_config_element_resp *ele,
14883 				u16 num_reported, bool printconfig)
14884 {
14885 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14886 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14887 	u8 element_type = ele->element_type;
14888 	u16 seid = le16_to_cpu(ele->seid);
14889 	struct i40e_veb *veb;
14890 
14891 	if (printconfig)
14892 		dev_info(&pf->pdev->dev,
14893 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14894 			 element_type, seid, uplink_seid, downlink_seid);
14895 
14896 	switch (element_type) {
14897 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14898 		pf->mac_seid = seid;
14899 		break;
14900 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14901 		/* Main VEB? */
14902 		if (uplink_seid != pf->mac_seid)
14903 			break;
14904 		veb = i40e_pf_get_main_veb(pf);
14905 		if (!veb) {
14906 			int v;
14907 
14908 			/* find existing or else empty VEB */
14909 			veb = i40e_pf_get_veb_by_seid(pf, seid);
14910 			if (veb) {
14911 				pf->lan_veb = veb->idx;
14912 			} else {
14913 				v = i40e_veb_mem_alloc(pf);
14914 				if (v < 0)
14915 					break;
14916 				pf->lan_veb = v;
14917 			}
14918 		}
14919 
14920 		/* Try to get again main VEB as pf->lan_veb may have changed */
14921 		veb = i40e_pf_get_main_veb(pf);
14922 		if (!veb)
14923 			break;
14924 
14925 		veb->seid = seid;
14926 		veb->uplink_seid = pf->mac_seid;
14927 		veb->pf = pf;
14928 		break;
14929 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14930 		if (num_reported != 1)
14931 			break;
14932 		/* This is immediately after a reset so we can assume this is
14933 		 * the PF's VSI
14934 		 */
14935 		pf->mac_seid = uplink_seid;
14936 		pf->main_vsi_seid = seid;
14937 		if (printconfig)
14938 			dev_info(&pf->pdev->dev,
14939 				 "pf_seid=%d main_vsi_seid=%d\n",
14940 				 downlink_seid, pf->main_vsi_seid);
14941 		break;
14942 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14943 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14944 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14945 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14946 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14947 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14948 		/* ignore these for now */
14949 		break;
14950 	default:
14951 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14952 			 element_type, seid);
14953 		break;
14954 	}
14955 }
14956 
14957 /**
14958  * i40e_fetch_switch_configuration - Get switch config from firmware
14959  * @pf: board private structure
14960  * @printconfig: should we print the contents
14961  *
14962  * Get the current switch configuration from the device and
14963  * extract a few useful SEID values.
14964  **/
14965 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14966 {
14967 	struct i40e_aqc_get_switch_config_resp *sw_config;
14968 	u16 next_seid = 0;
14969 	int ret = 0;
14970 	u8 *aq_buf;
14971 	int i;
14972 
14973 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14974 	if (!aq_buf)
14975 		return -ENOMEM;
14976 
14977 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14978 	do {
14979 		u16 num_reported, num_total;
14980 
14981 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14982 						I40E_AQ_LARGE_BUF,
14983 						&next_seid, NULL);
14984 		if (ret) {
14985 			dev_info(&pf->pdev->dev,
14986 				 "get switch config failed err %d aq_err %s\n",
14987 				 ret, libie_aq_str(pf->hw.aq.asq_last_status));
14988 			kfree(aq_buf);
14989 			return -ENOENT;
14990 		}
14991 
14992 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14993 		num_total = le16_to_cpu(sw_config->header.num_total);
14994 
14995 		if (printconfig)
14996 			dev_info(&pf->pdev->dev,
14997 				 "header: %d reported %d total\n",
14998 				 num_reported, num_total);
14999 
15000 		for (i = 0; i < num_reported; i++) {
15001 			struct i40e_aqc_switch_config_element_resp *ele =
15002 				&sw_config->element[i];
15003 
15004 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15005 						     printconfig);
15006 		}
15007 	} while (next_seid != 0);
15008 
15009 	kfree(aq_buf);
15010 	return ret;
15011 }
15012 
15013 /**
15014  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15015  * @pf: board private structure
15016  * @reinit: if the Main VSI needs to re-initialized.
15017  * @lock_acquired: indicates whether or not the lock has been acquired
15018  *
15019  * Returns 0 on success, negative value on failure
15020  **/
15021 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15022 {
15023 	struct i40e_vsi *main_vsi;
15024 	u16 flags = 0;
15025 	int ret;
15026 
15027 	/* find out what's out there already */
15028 	ret = i40e_fetch_switch_configuration(pf, false);
15029 	if (ret) {
15030 		dev_info(&pf->pdev->dev,
15031 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15032 			 ERR_PTR(ret), libie_aq_str(pf->hw.aq.asq_last_status));
15033 		return ret;
15034 	}
15035 	i40e_pf_reset_stats(pf);
15036 
15037 	/* set the switch config bit for the whole device to
15038 	 * support limited promisc or true promisc
15039 	 * when user requests promisc. The default is limited
15040 	 * promisc.
15041 	*/
15042 
15043 	if ((pf->hw.pf_id == 0) &&
15044 	    !test_bit(I40E_FLAG_TRUE_PROMISC_ENA, pf->flags)) {
15045 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15046 		pf->last_sw_conf_flags = flags;
15047 	}
15048 
15049 	if (pf->hw.pf_id == 0) {
15050 		u16 valid_flags;
15051 
15052 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15053 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15054 						NULL);
15055 		if (ret && pf->hw.aq.asq_last_status != LIBIE_AQ_RC_ESRCH) {
15056 			dev_info(&pf->pdev->dev,
15057 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15058 				 ERR_PTR(ret),
15059 				 libie_aq_str(pf->hw.aq.asq_last_status));
15060 			/* not a fatal problem, just keep going */
15061 		}
15062 		pf->last_sw_conf_valid_flags = valid_flags;
15063 	}
15064 
15065 	/* first time setup */
15066 	main_vsi = i40e_pf_get_main_vsi(pf);
15067 	if (!main_vsi || reinit) {
15068 		struct i40e_veb *veb;
15069 		u16 uplink_seid;
15070 
15071 		/* Set up the PF VSI associated with the PF's main VSI
15072 		 * that is already in the HW switch
15073 		 */
15074 		veb = i40e_pf_get_main_veb(pf);
15075 		if (veb)
15076 			uplink_seid = veb->seid;
15077 		else
15078 			uplink_seid = pf->mac_seid;
15079 		if (!main_vsi)
15080 			main_vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN,
15081 						  uplink_seid, 0);
15082 		else if (reinit)
15083 			main_vsi = i40e_vsi_reinit_setup(main_vsi);
15084 		if (!main_vsi) {
15085 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15086 			i40e_cloud_filter_exit(pf);
15087 			i40e_fdir_teardown(pf);
15088 			return -EAGAIN;
15089 		}
15090 	} else {
15091 		/* force a reset of TC and queue layout configurations */
15092 		main_vsi->seid = pf->main_vsi_seid;
15093 		i40e_vsi_reconfig_tc(main_vsi);
15094 	}
15095 	i40e_vlan_stripping_disable(main_vsi);
15096 
15097 	i40e_fdir_sb_setup(pf);
15098 
15099 	/* Setup static PF queue filter control settings */
15100 	ret = i40e_setup_pf_filter_control(pf);
15101 	if (ret) {
15102 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15103 			 ret);
15104 		/* Failure here should not stop continuing other steps */
15105 	}
15106 
15107 	/* enable RSS in the HW, even for only one queue, as the stack can use
15108 	 * the hash
15109 	 */
15110 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15111 		i40e_pf_config_rss(pf);
15112 
15113 	/* fill in link information and enable LSE reporting */
15114 	i40e_link_event(pf);
15115 
15116 	i40e_ptp_init(pf);
15117 
15118 	if (!lock_acquired)
15119 		rtnl_lock();
15120 
15121 	/* repopulate tunnel port filters */
15122 	udp_tunnel_nic_reset_ntf(main_vsi->netdev);
15123 
15124 	if (!lock_acquired)
15125 		rtnl_unlock();
15126 
15127 	return ret;
15128 }
15129 
15130 /**
15131  * i40e_determine_queue_usage - Work out queue distribution
15132  * @pf: board private structure
15133  **/
15134 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15135 {
15136 	int queues_left;
15137 	int q_max;
15138 
15139 	pf->num_lan_qps = 0;
15140 
15141 	/* Find the max queues to be put into basic use.  We'll always be
15142 	 * using TC0, whether or not DCB is running, and TC0 will get the
15143 	 * big RSS set.
15144 	 */
15145 	queues_left = pf->hw.func_caps.num_tx_qp;
15146 
15147 	if ((queues_left == 1) ||
15148 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15149 		/* one qp for PF, no queues for anything else */
15150 		queues_left = 0;
15151 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15152 
15153 		/* make sure all the fancies are disabled */
15154 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15155 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15156 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15157 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15158 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15159 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15160 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
15161 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15162 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15163 	} else if (!test_bit(I40E_FLAG_RSS_ENA, pf->flags) &&
15164 		   !test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) &&
15165 		   !test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags) &&
15166 		   !test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags)) {
15167 		/* one qp for PF */
15168 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15169 		queues_left -= pf->num_lan_qps;
15170 
15171 		clear_bit(I40E_FLAG_RSS_ENA, pf->flags);
15172 		clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
15173 		clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15174 		clear_bit(I40E_FLAG_FD_ATR_ENA, pf->flags);
15175 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15176 		clear_bit(I40E_FLAG_VMDQ_ENA, pf->flags);
15177 		set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15178 	} else {
15179 		/* Not enough queues for all TCs */
15180 		if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags) &&
15181 		    queues_left < I40E_MAX_TRAFFIC_CLASS) {
15182 			clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15183 			clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15184 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15185 		}
15186 
15187 		/* limit lan qps to the smaller of qps, cpus or msix */
15188 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15189 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15190 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15191 		pf->num_lan_qps = q_max;
15192 
15193 		queues_left -= pf->num_lan_qps;
15194 	}
15195 
15196 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15197 		if (queues_left > 1) {
15198 			queues_left -= 1; /* save 1 queue for FD */
15199 		} else {
15200 			clear_bit(I40E_FLAG_FD_SB_ENA, pf->flags);
15201 			set_bit(I40E_FLAG_FD_SB_INACTIVE, pf->flags);
15202 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15203 		}
15204 	}
15205 
15206 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15207 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15208 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15209 					(queues_left / pf->num_vf_qps));
15210 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15211 	}
15212 
15213 	if (test_bit(I40E_FLAG_VMDQ_ENA, pf->flags) &&
15214 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15215 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15216 					  (queues_left / pf->num_vmdq_qps));
15217 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15218 	}
15219 
15220 	pf->queues_left = queues_left;
15221 	dev_dbg(&pf->pdev->dev,
15222 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15223 		pf->hw.func_caps.num_tx_qp,
15224 		!!test_bit(I40E_FLAG_FD_SB_ENA, pf->flags),
15225 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15226 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15227 		queues_left);
15228 }
15229 
15230 /**
15231  * i40e_setup_pf_filter_control - Setup PF static filter control
15232  * @pf: PF to be setup
15233  *
15234  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15235  * settings. If PE/FCoE are enabled then it will also set the per PF
15236  * based filter sizes required for them. It also enables Flow director,
15237  * ethertype and macvlan type filter settings for the pf.
15238  *
15239  * Returns 0 on success, negative on failure
15240  **/
15241 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15242 {
15243 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15244 
15245 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15246 
15247 	/* Flow Director is enabled */
15248 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags) ||
15249 	    test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15250 		settings->enable_fdir = true;
15251 
15252 	/* Ethtype and MACVLAN filters enabled for PF */
15253 	settings->enable_ethtype = true;
15254 	settings->enable_macvlan = true;
15255 
15256 	if (i40e_set_filter_control(&pf->hw, settings))
15257 		return -ENOENT;
15258 
15259 	return 0;
15260 }
15261 
15262 #define INFO_STRING_LEN 255
15263 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15264 static void i40e_print_features(struct i40e_pf *pf)
15265 {
15266 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
15267 	struct i40e_hw *hw = &pf->hw;
15268 	char *buf;
15269 	int i;
15270 
15271 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15272 	if (!buf)
15273 		return;
15274 
15275 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15276 #ifdef CONFIG_PCI_IOV
15277 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15278 #endif
15279 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15280 		       pf->hw.func_caps.num_vsis, main_vsi->num_queue_pairs);
15281 	if (test_bit(I40E_FLAG_RSS_ENA, pf->flags))
15282 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15283 	if (test_bit(I40E_FLAG_FD_ATR_ENA, pf->flags))
15284 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15285 	if (test_bit(I40E_FLAG_FD_SB_ENA, pf->flags)) {
15286 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15287 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15288 	}
15289 	if (test_bit(I40E_FLAG_DCB_CAPABLE, pf->flags))
15290 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15291 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15292 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15293 	if (test_bit(I40E_FLAG_PTP_ENA, pf->flags))
15294 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15295 	if (test_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags))
15296 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15297 	else
15298 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15299 
15300 	dev_info(&pf->pdev->dev, "%s\n", buf);
15301 	kfree(buf);
15302 	WARN_ON(i > INFO_STRING_LEN);
15303 }
15304 
15305 /**
15306  * i40e_get_platform_mac_addr - get platform-specific MAC address
15307  * @pdev: PCI device information struct
15308  * @pf: board private structure
15309  *
15310  * Look up the MAC address for the device. First we'll try
15311  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15312  * specific fallback. Otherwise, we'll default to the stored value in
15313  * firmware.
15314  **/
15315 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15316 {
15317 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15318 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15319 }
15320 
15321 /**
15322  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15323  * @fec_cfg: FEC option to set in flags
15324  * @flags: ptr to flags in which we set FEC option
15325  **/
15326 void i40e_set_fec_in_flags(u8 fec_cfg, unsigned long *flags)
15327 {
15328 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO) {
15329 		set_bit(I40E_FLAG_RS_FEC, flags);
15330 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15331 	}
15332 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15333 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15334 		set_bit(I40E_FLAG_RS_FEC, flags);
15335 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15336 	}
15337 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15338 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15339 		set_bit(I40E_FLAG_BASE_R_FEC, flags);
15340 		clear_bit(I40E_FLAG_RS_FEC, flags);
15341 	}
15342 	if (fec_cfg == 0) {
15343 		clear_bit(I40E_FLAG_RS_FEC, flags);
15344 		clear_bit(I40E_FLAG_BASE_R_FEC, flags);
15345 	}
15346 }
15347 
15348 /**
15349  * i40e_check_recovery_mode - check if we are running transition firmware
15350  * @pf: board private structure
15351  *
15352  * Check registers indicating the firmware runs in recovery mode. Sets the
15353  * appropriate driver state.
15354  *
15355  * Returns true if the recovery mode was detected, false otherwise
15356  **/
15357 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15358 {
15359 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15360 
15361 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15362 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15363 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15364 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15365 
15366 		return true;
15367 	}
15368 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15369 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15370 
15371 	return false;
15372 }
15373 
15374 /**
15375  * i40e_pf_loop_reset - perform reset in a loop.
15376  * @pf: board private structure
15377  *
15378  * This function is useful when a NIC is about to enter recovery mode.
15379  * When a NIC's internal data structures are corrupted the NIC's
15380  * firmware is going to enter recovery mode.
15381  * Right after a POR it takes about 7 minutes for firmware to enter
15382  * recovery mode. Until that time a NIC is in some kind of intermediate
15383  * state. After that time period the NIC almost surely enters
15384  * recovery mode. The only way for a driver to detect intermediate
15385  * state is to issue a series of pf-resets and check a return value.
15386  * If a PF reset returns success then the firmware could be in recovery
15387  * mode so the caller of this code needs to check for recovery mode
15388  * if this function returns success. There is a little chance that
15389  * firmware will hang in intermediate state forever.
15390  * Since waiting 7 minutes is quite a lot of time this function waits
15391  * 10 seconds and then gives up by returning an error.
15392  *
15393  * Return 0 on success, negative on failure.
15394  **/
15395 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15396 {
15397 	/* wait max 10 seconds for PF reset to succeed */
15398 	const unsigned long time_end = jiffies + 10 * HZ;
15399 	struct i40e_hw *hw = &pf->hw;
15400 	int ret;
15401 
15402 	ret = i40e_pf_reset(hw);
15403 	while (ret != 0 && time_before(jiffies, time_end)) {
15404 		usleep_range(10000, 20000);
15405 		ret = i40e_pf_reset(hw);
15406 	}
15407 
15408 	if (ret == 0)
15409 		pf->pfr_count++;
15410 	else
15411 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15412 
15413 	return ret;
15414 }
15415 
15416 /**
15417  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15418  * @pf: board private structure
15419  *
15420  * Check FW registers to determine if FW issued unexpected EMP Reset.
15421  * Every time when unexpected EMP Reset occurs the FW increments
15422  * a counter of unexpected EMP Resets. When the counter reaches 10
15423  * the FW should enter the Recovery mode
15424  *
15425  * Returns true if FW issued unexpected EMP Reset
15426  **/
15427 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15428 {
15429 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15430 			   I40E_GL_FWSTS_FWS1B_MASK;
15431 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15432 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15433 }
15434 
15435 /**
15436  * i40e_handle_resets - handle EMP resets and PF resets
15437  * @pf: board private structure
15438  *
15439  * Handle both EMP resets and PF resets and conclude whether there are
15440  * any issues regarding these resets. If there are any issues then
15441  * generate log entry.
15442  *
15443  * Return 0 if NIC is healthy or negative value when there are issues
15444  * with resets
15445  **/
15446 static int i40e_handle_resets(struct i40e_pf *pf)
15447 {
15448 	const int pfr = i40e_pf_loop_reset(pf);
15449 	const bool is_empr = i40e_check_fw_empr(pf);
15450 
15451 	if (is_empr || pfr != 0)
15452 		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");
15453 
15454 	return is_empr ? -EIO : pfr;
15455 }
15456 
15457 /**
15458  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15459  * @pf: board private structure
15460  * @hw: ptr to the hardware info
15461  *
15462  * This function does a minimal setup of all subsystems needed for running
15463  * recovery mode.
15464  *
15465  * Returns 0 on success, negative on failure
15466  **/
15467 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15468 {
15469 	struct i40e_vsi *vsi;
15470 	int err;
15471 	int v_idx;
15472 
15473 	pci_set_drvdata(pf->pdev, pf);
15474 	pci_save_state(pf->pdev);
15475 
15476 	/* set up periodic task facility */
15477 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15478 	pf->service_timer_period = HZ;
15479 
15480 	INIT_WORK(&pf->service_task, i40e_service_task);
15481 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15482 
15483 	err = i40e_init_interrupt_scheme(pf);
15484 	if (err)
15485 		goto err_switch_setup;
15486 
15487 	/* The number of VSIs reported by the FW is the minimum guaranteed
15488 	 * to us; HW supports far more and we share the remaining pool with
15489 	 * the other PFs. We allocate space for more than the guarantee with
15490 	 * the understanding that we might not get them all later.
15491 	 */
15492 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15493 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15494 	else
15495 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15496 
15497 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15498 	pf->vsi = kzalloc_objs(struct i40e_vsi *, pf->num_alloc_vsi);
15499 	if (!pf->vsi) {
15500 		err = -ENOMEM;
15501 		goto err_switch_setup;
15502 	}
15503 
15504 	/* We allocate one VSI which is needed as absolute minimum
15505 	 * in order to register the netdev
15506 	 */
15507 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15508 	if (v_idx < 0) {
15509 		err = v_idx;
15510 		goto err_switch_setup;
15511 	}
15512 	pf->lan_vsi = v_idx;
15513 	vsi = pf->vsi[v_idx];
15514 	if (!vsi) {
15515 		err = -EFAULT;
15516 		goto err_switch_setup;
15517 	}
15518 	vsi->alloc_queue_pairs = 1;
15519 	err = i40e_config_netdev(vsi);
15520 	if (err)
15521 		goto err_switch_setup;
15522 	err = register_netdev(vsi->netdev);
15523 	if (err)
15524 		goto err_switch_setup;
15525 	vsi->netdev_registered = true;
15526 	i40e_dbg_pf_init(pf);
15527 
15528 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15529 	if (err)
15530 		goto err_switch_setup;
15531 
15532 	/* tell the firmware that we're starting */
15533 	i40e_send_version(pf);
15534 
15535 	/* since everything's happy, start the service_task timer */
15536 	mod_timer(&pf->service_timer,
15537 		  round_jiffies(jiffies + pf->service_timer_period));
15538 
15539 	return 0;
15540 
15541 err_switch_setup:
15542 	i40e_reset_interrupt_capability(pf);
15543 	timer_shutdown_sync(&pf->service_timer);
15544 	i40e_shutdown_adminq(hw);
15545 	iounmap(hw->hw_addr);
15546 	pci_release_mem_regions(pf->pdev);
15547 	pci_disable_device(pf->pdev);
15548 	i40e_free_pf(pf);
15549 
15550 	return err;
15551 }
15552 
15553 /**
15554  * i40e_set_subsystem_device_id - set subsystem device id
15555  * @hw: pointer to the hardware info
15556  *
15557  * Set PCI subsystem device id either from a pci_dev structure or
15558  * a specific FW register.
15559  **/
15560 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15561 {
15562 	struct i40e_pf *pf = i40e_hw_to_pf(hw);
15563 
15564 	hw->subsystem_device_id = pf->pdev->subsystem_device ?
15565 		pf->pdev->subsystem_device :
15566 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15567 }
15568 
15569 /**
15570  * i40e_probe - Device initialization routine
15571  * @pdev: PCI device information struct
15572  * @ent: entry in i40e_pci_tbl
15573  *
15574  * i40e_probe initializes a PF identified by a pci_dev structure.
15575  * The OS initialization, configuring of the PF private structure,
15576  * and a hardware reset occur.
15577  *
15578  * Returns 0 on success, negative on failure
15579  **/
15580 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15581 {
15582 	struct i40e_aq_get_phy_abilities_resp abilities;
15583 #ifdef CONFIG_I40E_DCB
15584 	enum i40e_get_fw_lldp_status_resp lldp_status;
15585 #endif /* CONFIG_I40E_DCB */
15586 	struct i40e_vsi *vsi;
15587 	struct i40e_pf *pf;
15588 	struct i40e_hw *hw;
15589 	u16 wol_nvm_bits;
15590 	char nvm_ver[32];
15591 	u16 link_status;
15592 #ifdef CONFIG_I40E_DCB
15593 	int status;
15594 #endif /* CONFIG_I40E_DCB */
15595 	int err;
15596 	u32 val;
15597 
15598 	err = pci_enable_device_mem(pdev);
15599 	if (err)
15600 		return err;
15601 
15602 	/* set up for high or low dma */
15603 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15604 	if (err) {
15605 		dev_err(&pdev->dev,
15606 			"DMA configuration failed: 0x%x\n", err);
15607 		goto err_dma;
15608 	}
15609 
15610 	/* set up pci connections */
15611 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15612 	if (err) {
15613 		dev_info(&pdev->dev,
15614 			 "pci_request_selected_regions failed %d\n", err);
15615 		goto err_pci_reg;
15616 	}
15617 
15618 	pci_set_master(pdev);
15619 
15620 	/* Now that we have a PCI connection, we need to do the
15621 	 * low level device setup.  This is primarily setting up
15622 	 * the Admin Queue structures and then querying for the
15623 	 * device's current profile information.
15624 	 */
15625 	pf = i40e_alloc_pf(&pdev->dev);
15626 	if (!pf) {
15627 		err = -ENOMEM;
15628 		goto err_pf_alloc;
15629 	}
15630 	pf->next_vsi = 0;
15631 	pf->pdev = pdev;
15632 	set_bit(__I40E_DOWN, pf->state);
15633 
15634 	hw = &pf->hw;
15635 
15636 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15637 				I40E_MAX_CSR_SPACE);
15638 	/* We believe that the highest register to read is
15639 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15640 	 * is not less than that before mapping to prevent a
15641 	 * kernel panic.
15642 	 */
15643 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15644 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15645 			pf->ioremap_len);
15646 		err = -ENOMEM;
15647 		goto err_ioremap;
15648 	}
15649 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15650 	if (!hw->hw_addr) {
15651 		err = -EIO;
15652 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15653 			 (unsigned int)pci_resource_start(pdev, 0),
15654 			 pf->ioremap_len, err);
15655 		goto err_ioremap;
15656 	}
15657 	hw->vendor_id = pdev->vendor;
15658 	hw->device_id = pdev->device;
15659 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15660 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15661 	i40e_set_subsystem_device_id(hw);
15662 	hw->bus.device = PCI_SLOT(pdev->devfn);
15663 	hw->bus.func = PCI_FUNC(pdev->devfn);
15664 	hw->bus.bus_id = pdev->bus->number;
15665 
15666 	/* Select something other than the 802.1ad ethertype for the
15667 	 * switch to use internally and drop on ingress.
15668 	 */
15669 	hw->switch_tag = 0xffff;
15670 	hw->first_tag = ETH_P_8021AD;
15671 	hw->second_tag = ETH_P_8021Q;
15672 
15673 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15674 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15675 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15676 
15677 	/* set up the locks for the AQ, do this only once in probe
15678 	 * and destroy them only once in remove
15679 	 */
15680 	mutex_init(&hw->aq.asq_mutex);
15681 	mutex_init(&hw->aq.arq_mutex);
15682 
15683 	pf->msg_enable = netif_msg_init(debug,
15684 					NETIF_MSG_DRV |
15685 					NETIF_MSG_PROBE |
15686 					NETIF_MSG_LINK);
15687 	if (debug < -1)
15688 		pf->hw.debug_mask = debug;
15689 
15690 	/* do a special CORER for clearing PXE mode once at init */
15691 	if (hw->revision_id == 0 &&
15692 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15693 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15694 		i40e_flush(hw);
15695 		msleep(200);
15696 		pf->corer_count++;
15697 
15698 		i40e_clear_pxe_mode(hw);
15699 	}
15700 
15701 	/* Reset here to make sure all is clean and to define PF 'n' */
15702 	i40e_clear_hw(hw);
15703 
15704 	err = i40e_set_mac_type(hw);
15705 	if (err) {
15706 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15707 			 err);
15708 		goto err_pf_reset;
15709 	}
15710 
15711 	err = i40e_handle_resets(pf);
15712 	if (err)
15713 		goto err_pf_reset;
15714 
15715 	i40e_check_recovery_mode(pf);
15716 
15717 	if (is_kdump_kernel()) {
15718 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15719 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15720 	} else {
15721 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15722 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15723 	}
15724 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15725 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15726 
15727 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15728 		 "%s-%s:misc",
15729 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15730 
15731 	err = i40e_init_shared_code(hw);
15732 	if (err) {
15733 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15734 			 err);
15735 		goto err_pf_reset;
15736 	}
15737 
15738 	/* set up a default setting for link flow control */
15739 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15740 
15741 	err = i40e_init_adminq(hw);
15742 	if (err) {
15743 		if (err == -EIO)
15744 			dev_info(&pdev->dev,
15745 				 "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",
15746 				 hw->aq.api_maj_ver,
15747 				 hw->aq.api_min_ver,
15748 				 I40E_FW_API_VERSION_MAJOR,
15749 				 I40E_FW_MINOR_VERSION(hw));
15750 		else
15751 			dev_info(&pdev->dev,
15752 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15753 
15754 		goto err_pf_reset;
15755 	}
15756 	i40e_get_oem_version(hw);
15757 	i40e_get_pba_string(hw);
15758 
15759 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15760 	i40e_nvm_version_str(hw, nvm_ver, sizeof(nvm_ver));
15761 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15762 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15763 		 hw->aq.api_maj_ver, hw->aq.api_min_ver, nvm_ver,
15764 		 hw->vendor_id, hw->device_id, hw->subsystem_vendor_id,
15765 		 hw->subsystem_device_id);
15766 
15767 	if (i40e_is_aq_api_ver_ge(hw, I40E_FW_API_VERSION_MAJOR,
15768 				  I40E_FW_MINOR_VERSION(hw) + 1))
15769 		dev_dbg(&pdev->dev,
15770 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15771 			 hw->aq.api_maj_ver,
15772 			 hw->aq.api_min_ver,
15773 			 I40E_FW_API_VERSION_MAJOR,
15774 			 I40E_FW_MINOR_VERSION(hw));
15775 	else if (i40e_is_aq_api_ver_lt(hw, 1, 4))
15776 		dev_info(&pdev->dev,
15777 			 "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",
15778 			 hw->aq.api_maj_ver,
15779 			 hw->aq.api_min_ver,
15780 			 I40E_FW_API_VERSION_MAJOR,
15781 			 I40E_FW_MINOR_VERSION(hw));
15782 
15783 	i40e_verify_eeprom(pf);
15784 
15785 	/* Rev 0 hardware was never productized */
15786 	if (hw->revision_id < 1)
15787 		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");
15788 
15789 	i40e_clear_pxe_mode(hw);
15790 
15791 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15792 	if (err)
15793 		goto err_adminq_setup;
15794 
15795 	err = i40e_sw_init(pf);
15796 	if (err) {
15797 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15798 		goto err_sw_init;
15799 	}
15800 
15801 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15802 		return i40e_init_recovery_mode(pf, hw);
15803 
15804 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15805 				hw->func_caps.num_rx_qp, 0, 0);
15806 	if (err) {
15807 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15808 		goto err_init_lan_hmc;
15809 	}
15810 
15811 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15812 	if (err) {
15813 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15814 		err = -ENOENT;
15815 		goto err_configure_lan_hmc;
15816 	}
15817 
15818 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15819 	 * Ignore error return codes because if it was already disabled via
15820 	 * hardware settings this will fail
15821 	 */
15822 	if (test_bit(I40E_HW_CAP_STOP_FW_LLDP, pf->hw.caps)) {
15823 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15824 		i40e_aq_stop_lldp(hw, true, false, NULL);
15825 	}
15826 
15827 	/* allow a platform config to override the HW addr */
15828 	i40e_get_platform_mac_addr(pdev, pf);
15829 
15830 	if (!is_valid_ether_addr(hw->mac.addr)) {
15831 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15832 		err = -EIO;
15833 		goto err_mac_addr;
15834 	}
15835 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15836 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15837 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15838 	if (is_valid_ether_addr(hw->mac.port_addr))
15839 		set_bit(I40E_HW_CAP_PORT_ID_VALID, pf->hw.caps);
15840 
15841 	i40e_ptp_alloc_pins(pf);
15842 	pci_set_drvdata(pdev, pf);
15843 	pci_save_state(pdev);
15844 
15845 #ifdef CONFIG_I40E_DCB
15846 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15847 	(!status &&
15848 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15849 		(clear_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags)) :
15850 		(set_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags));
15851 	dev_info(&pdev->dev,
15852 		 test_bit(I40E_FLAG_FW_LLDP_DIS, pf->flags) ?
15853 			"FW LLDP is disabled\n" :
15854 			"FW LLDP is enabled\n");
15855 
15856 	/* Enable FW to write default DCB config on link-up */
15857 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15858 
15859 	err = i40e_init_pf_dcb(pf);
15860 	if (err) {
15861 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15862 		clear_bit(I40E_FLAG_DCB_CAPABLE, pf->flags);
15863 		clear_bit(I40E_FLAG_DCB_ENA, pf->flags);
15864 		/* Continue without DCB enabled */
15865 	}
15866 #endif /* CONFIG_I40E_DCB */
15867 
15868 	/* set up periodic task facility */
15869 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15870 	pf->service_timer_period = HZ;
15871 
15872 	INIT_WORK(&pf->service_task, i40e_service_task);
15873 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15874 
15875 	/* NVM bit on means WoL disabled for the port */
15876 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15877 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15878 		pf->wol_en = false;
15879 	else
15880 		pf->wol_en = true;
15881 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15882 
15883 	/* set up the main switch operations */
15884 	i40e_determine_queue_usage(pf);
15885 	err = i40e_init_interrupt_scheme(pf);
15886 	if (err)
15887 		goto err_switch_setup;
15888 
15889 	/* Reduce Tx and Rx pairs for kdump
15890 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15891 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15892 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15893 	 */
15894 	if (is_kdump_kernel())
15895 		pf->num_lan_msix = 1;
15896 
15897 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15898 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15899 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15900 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15901 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15902 						    UDP_TUNNEL_TYPE_GENEVE;
15903 
15904 	/* The number of VSIs reported by the FW is the minimum guaranteed
15905 	 * to us; HW supports far more and we share the remaining pool with
15906 	 * the other PFs. We allocate space for more than the guarantee with
15907 	 * the understanding that we might not get them all later.
15908 	 */
15909 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15910 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15911 	else
15912 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15913 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15914 		dev_warn(&pf->pdev->dev,
15915 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15916 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15917 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15918 	}
15919 
15920 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15921 	pf->vsi = kzalloc_objs(struct i40e_vsi *, pf->num_alloc_vsi);
15922 	if (!pf->vsi) {
15923 		err = -ENOMEM;
15924 		goto err_switch_setup;
15925 	}
15926 
15927 #ifdef CONFIG_PCI_IOV
15928 	/* prep for VF support */
15929 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
15930 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
15931 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15932 		if (pci_num_vf(pdev))
15933 			set_bit(I40E_FLAG_VEB_MODE_ENA, pf->flags);
15934 	}
15935 #endif
15936 	err = i40e_setup_pf_switch(pf, false, false);
15937 	if (err) {
15938 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15939 		goto err_vsis;
15940 	}
15941 
15942 	vsi = i40e_pf_get_main_vsi(pf);
15943 	INIT_LIST_HEAD(&vsi->ch_list);
15944 
15945 	/* if FDIR VSI was set up, start it now */
15946 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
15947 	if (vsi)
15948 		i40e_vsi_open(vsi);
15949 
15950 	/* The driver only wants link up/down and module qualification
15951 	 * reports from firmware.  Note the negative logic.
15952 	 */
15953 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15954 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15955 					 I40E_AQ_EVENT_MEDIA_NA |
15956 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15957 	if (err)
15958 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
15959 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
15960 
15961 	/* VF MDD event logs are rate limited to one second intervals */
15962 	ratelimit_state_init(&pf->mdd_message_rate_limit, 1 * HZ, 1);
15963 
15964 	/* Reconfigure hardware for allowing smaller MSS in the case
15965 	 * of TSO, so that we avoid the MDD being fired and causing
15966 	 * a reset in the case of small MSS+TSO.
15967 	 */
15968 	val = rd32(hw, I40E_REG_MSS);
15969 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15970 		val &= ~I40E_REG_MSS_MIN_MASK;
15971 		val |= I40E_64BYTE_MSS;
15972 		wr32(hw, I40E_REG_MSS, val);
15973 	}
15974 
15975 	if (test_bit(I40E_HW_CAP_RESTART_AUTONEG, pf->hw.caps)) {
15976 		msleep(75);
15977 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15978 		if (err)
15979 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
15980 				 ERR_PTR(err),
15981 				 libie_aq_str(pf->hw.aq.asq_last_status));
15982 	}
15983 	/* The main driver is (mostly) up and happy. We need to set this state
15984 	 * before setting up the misc vector or we get a race and the vector
15985 	 * ends up disabled forever.
15986 	 */
15987 	clear_bit(__I40E_DOWN, pf->state);
15988 
15989 	/* In case of MSIX we are going to setup the misc vector right here
15990 	 * to handle admin queue events etc. In case of legacy and MSI
15991 	 * the misc functionality and queue processing is combined in
15992 	 * the same vector and that gets setup at open.
15993 	 */
15994 	if (test_bit(I40E_FLAG_MSIX_ENA, pf->flags)) {
15995 		err = i40e_setup_misc_vector(pf);
15996 		if (err) {
15997 			dev_info(&pdev->dev,
15998 				 "setup of misc vector failed: %d\n", err);
15999 			i40e_cloud_filter_exit(pf);
16000 			i40e_fdir_teardown(pf);
16001 			goto err_vsis;
16002 		}
16003 	}
16004 
16005 #ifdef CONFIG_PCI_IOV
16006 	/* prep for VF support */
16007 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags) &&
16008 	    test_bit(I40E_FLAG_MSIX_ENA, pf->flags) &&
16009 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16010 		/* disable link interrupts for VFs */
16011 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16012 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16013 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16014 		i40e_flush(hw);
16015 
16016 		if (pci_num_vf(pdev)) {
16017 			dev_info(&pdev->dev,
16018 				 "Active VFs found, allocating resources.\n");
16019 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16020 			if (err)
16021 				dev_info(&pdev->dev,
16022 					 "Error %d allocating resources for existing VFs\n",
16023 					 err);
16024 		}
16025 	}
16026 #endif /* CONFIG_PCI_IOV */
16027 
16028 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16029 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16030 						      pf->num_iwarp_msix,
16031 						      I40E_IWARP_IRQ_PILE_ID);
16032 		if (pf->iwarp_base_vector < 0) {
16033 			dev_info(&pdev->dev,
16034 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16035 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16036 			clear_bit(I40E_FLAG_IWARP_ENA, pf->flags);
16037 		}
16038 	}
16039 
16040 	i40e_dbg_pf_init(pf);
16041 
16042 	/* tell the firmware that we're starting */
16043 	i40e_send_version(pf);
16044 
16045 	/* since everything's happy, start the service_task timer */
16046 	mod_timer(&pf->service_timer,
16047 		  round_jiffies(jiffies + pf->service_timer_period));
16048 
16049 	/* add this PF to client device list and launch a client service task */
16050 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16051 		err = i40e_lan_add_device(pf);
16052 		if (err)
16053 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16054 				 err);
16055 	}
16056 
16057 #define PCI_SPEED_SIZE 8
16058 #define PCI_WIDTH_SIZE 8
16059 	/* Devices on the IOSF bus do not have this information
16060 	 * and will report PCI Gen 1 x 1 by default so don't bother
16061 	 * checking them.
16062 	 */
16063 	if (!test_bit(I40E_HW_CAP_NO_PCI_LINK_CHECK, pf->hw.caps)) {
16064 		char speed[PCI_SPEED_SIZE] = "Unknown";
16065 		char width[PCI_WIDTH_SIZE] = "Unknown";
16066 
16067 		/* Get the negotiated link width and speed from PCI config
16068 		 * space
16069 		 */
16070 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16071 					  &link_status);
16072 
16073 		i40e_set_pci_config_data(hw, link_status);
16074 
16075 		switch (hw->bus.speed) {
16076 		case i40e_bus_speed_8000:
16077 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16078 		case i40e_bus_speed_5000:
16079 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16080 		case i40e_bus_speed_2500:
16081 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16082 		default:
16083 			break;
16084 		}
16085 		switch (hw->bus.width) {
16086 		case i40e_bus_width_pcie_x8:
16087 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16088 		case i40e_bus_width_pcie_x4:
16089 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16090 		case i40e_bus_width_pcie_x2:
16091 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16092 		case i40e_bus_width_pcie_x1:
16093 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16094 		default:
16095 			break;
16096 		}
16097 
16098 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16099 			 speed, width);
16100 
16101 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16102 		    hw->bus.speed < i40e_bus_speed_8000) {
16103 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16104 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16105 		}
16106 	}
16107 
16108 	/* get the requested speeds from the fw */
16109 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16110 	if (err)
16111 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16112 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16113 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16114 
16115 	/* set the FEC config due to the board capabilities */
16116 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, pf->flags);
16117 
16118 	/* get the supported phy types from the fw */
16119 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16120 	if (err)
16121 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16122 			ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16123 
16124 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16125 
16126 	err = i40e_aq_set_mac_config(hw, MAX_FRAME_SIZE_DEFAULT, NULL);
16127 	if (err)
16128 		dev_warn(&pdev->dev, "set mac config ret = %pe last_status = %s\n",
16129 			 ERR_PTR(err), libie_aq_str(pf->hw.aq.asq_last_status));
16130 
16131 	/* Make sure the MFS is set to the expected value */
16132 	val = rd32(hw, I40E_PRTGL_SAH);
16133 	FIELD_MODIFY(I40E_PRTGL_SAH_MFS_MASK, &val, MAX_FRAME_SIZE_DEFAULT);
16134 	wr32(hw, I40E_PRTGL_SAH, val);
16135 
16136 	/* Add a filter to drop all Flow control frames from any VSI from being
16137 	 * transmitted. By doing so we stop a malicious VF from sending out
16138 	 * PAUSE or PFC frames and potentially controlling traffic for other
16139 	 * PF/VF VSIs.
16140 	 * The FW can still send Flow control frames if enabled.
16141 	 */
16142 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16143 						       pf->main_vsi_seid);
16144 
16145 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16146 	    (pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16147 		set_bit(I40E_HW_CAP_PHY_CONTROLS_LEDS, pf->hw.caps);
16148 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16149 		set_bit(I40E_HW_CAP_CRT_RETIMER, pf->hw.caps);
16150 	/* print a string summarizing features */
16151 	i40e_print_features(pf);
16152 
16153 	i40e_devlink_register(pf);
16154 
16155 	return 0;
16156 
16157 	/* Unwind what we've done if something failed in the setup */
16158 err_vsis:
16159 	set_bit(__I40E_DOWN, pf->state);
16160 	i40e_ptp_stop(pf);
16161 	i40e_clear_interrupt_scheme(pf);
16162 	kfree(pf->vsi);
16163 err_switch_setup:
16164 	i40e_ptp_free_pins(pf);
16165 	i40e_reset_interrupt_capability(pf);
16166 	timer_shutdown_sync(&pf->service_timer);
16167 err_mac_addr:
16168 err_configure_lan_hmc:
16169 	(void)i40e_shutdown_lan_hmc(hw);
16170 err_init_lan_hmc:
16171 	kfree(pf->qp_pile);
16172 err_sw_init:
16173 err_adminq_setup:
16174 err_pf_reset:
16175 	iounmap(hw->hw_addr);
16176 err_ioremap:
16177 	i40e_free_pf(pf);
16178 err_pf_alloc:
16179 	pci_release_mem_regions(pdev);
16180 err_pci_reg:
16181 err_dma:
16182 	pci_disable_device(pdev);
16183 	return err;
16184 }
16185 
16186 /**
16187  * i40e_remove - Device removal routine
16188  * @pdev: PCI device information struct
16189  *
16190  * i40e_remove is called by the PCI subsystem to alert the driver
16191  * that is should release a PCI device.  This could be caused by a
16192  * Hot-Plug event, or because the driver is going to be removed from
16193  * memory.
16194  **/
16195 static void i40e_remove(struct pci_dev *pdev)
16196 {
16197 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16198 	struct i40e_hw *hw = &pf->hw;
16199 	struct i40e_vsi *vsi;
16200 	struct i40e_veb *veb;
16201 	int ret_code;
16202 	int i;
16203 
16204 	i40e_devlink_unregister(pf);
16205 
16206 	i40e_dbg_pf_exit(pf);
16207 
16208 	i40e_ptp_stop(pf);
16209 
16210 	/* Disable RSS in hw */
16211 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16212 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16213 
16214 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16215 	 * flags, once they are set, i40e_rebuild should not be called as
16216 	 * i40e_prep_for_reset always returns early.
16217 	 */
16218 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16219 		usleep_range(1000, 2000);
16220 	set_bit(__I40E_IN_REMOVE, pf->state);
16221 
16222 	if (test_bit(I40E_FLAG_SRIOV_ENA, pf->flags)) {
16223 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16224 		i40e_free_vfs(pf);
16225 		clear_bit(I40E_FLAG_SRIOV_ENA, pf->flags);
16226 	}
16227 	/* no more scheduling of any task */
16228 	set_bit(__I40E_SUSPENDED, pf->state);
16229 	set_bit(__I40E_DOWN, pf->state);
16230 	if (pf->service_timer.function)
16231 		timer_shutdown_sync(&pf->service_timer);
16232 	if (pf->service_task.func)
16233 		cancel_work_sync(&pf->service_task);
16234 
16235 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16236 		struct i40e_vsi *vsi = pf->vsi[0];
16237 
16238 		/* We know that we have allocated only one vsi for this PF,
16239 		 * it was just for registering netdevice, so the interface
16240 		 * could be visible in the 'ifconfig' output
16241 		 */
16242 		unregister_netdev(vsi->netdev);
16243 		free_netdev(vsi->netdev);
16244 
16245 		goto unmap;
16246 	}
16247 
16248 	/* Client close must be called explicitly here because the timer
16249 	 * has been stopped.
16250 	 */
16251 	i40e_notify_client_of_netdev_close(pf, false);
16252 
16253 	i40e_fdir_teardown(pf);
16254 
16255 	/* If there is a switch structure or any orphans, remove them.
16256 	 * This will leave only the PF's VSI remaining.
16257 	 */
16258 	i40e_pf_for_each_veb(pf, i, veb)
16259 		if (veb->uplink_seid == pf->mac_seid ||
16260 		    veb->uplink_seid == 0)
16261 			i40e_switch_branch_release(veb);
16262 
16263 	/* Now we can shutdown the PF's VSIs, just before we kill
16264 	 * adminq and hmc.
16265 	 */
16266 	i40e_pf_for_each_vsi(pf, i, vsi) {
16267 		i40e_vsi_close(vsi);
16268 		i40e_vsi_release(vsi);
16269 		pf->vsi[i] = NULL;
16270 	}
16271 
16272 	i40e_cloud_filter_exit(pf);
16273 
16274 	/* remove attached clients */
16275 	if (test_bit(I40E_FLAG_IWARP_ENA, pf->flags)) {
16276 		ret_code = i40e_lan_del_device(pf);
16277 		if (ret_code)
16278 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16279 				 ret_code);
16280 	}
16281 
16282 	/* shutdown and destroy the HMC */
16283 	if (hw->hmc.hmc_obj) {
16284 		ret_code = i40e_shutdown_lan_hmc(hw);
16285 		if (ret_code)
16286 			dev_warn(&pdev->dev,
16287 				 "Failed to destroy the HMC resources: %d\n",
16288 				 ret_code);
16289 	}
16290 
16291 unmap:
16292 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16293 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16294 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16295 		free_irq(pf->pdev->irq, pf);
16296 
16297 	/* shutdown the adminq */
16298 	i40e_shutdown_adminq(hw);
16299 
16300 	/* destroy the locks only once, here */
16301 	mutex_destroy(&hw->aq.arq_mutex);
16302 	mutex_destroy(&hw->aq.asq_mutex);
16303 
16304 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16305 	rtnl_lock();
16306 	i40e_clear_interrupt_scheme(pf);
16307 	i40e_pf_for_each_vsi(pf, i, vsi) {
16308 		if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16309 			i40e_vsi_clear_rings(vsi);
16310 
16311 		i40e_vsi_clear(vsi);
16312 		pf->vsi[i] = NULL;
16313 	}
16314 	rtnl_unlock();
16315 
16316 	i40e_pf_for_each_veb(pf, i, veb) {
16317 		kfree(veb);
16318 		pf->veb[i] = NULL;
16319 	}
16320 
16321 	kfree(pf->qp_pile);
16322 	kfree(pf->vsi);
16323 
16324 	iounmap(hw->hw_addr);
16325 	i40e_free_pf(pf);
16326 	pci_release_mem_regions(pdev);
16327 
16328 	pci_disable_device(pdev);
16329 }
16330 
16331 /**
16332  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16333  * using the mac_address_write admin q function
16334  * @pf: pointer to i40e_pf struct
16335  **/
16336 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16337 {
16338 	struct i40e_vsi *main_vsi = i40e_pf_get_main_vsi(pf);
16339 	struct i40e_hw *hw = &pf->hw;
16340 	u8 mac_addr[6];
16341 	u16 flags = 0;
16342 	int ret;
16343 
16344 	/* Get current MAC address in case it's an LAA */
16345 	if (main_vsi && main_vsi->netdev) {
16346 		ether_addr_copy(mac_addr, main_vsi->netdev->dev_addr);
16347 	} else {
16348 		dev_err(&pf->pdev->dev,
16349 			"Failed to retrieve MAC address; using default\n");
16350 		ether_addr_copy(mac_addr, hw->mac.addr);
16351 	}
16352 
16353 	/* The FW expects the mac address write cmd to first be called with
16354 	 * one of these flags before calling it again with the multicast
16355 	 * enable flags.
16356 	 */
16357 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16358 
16359 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16360 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16361 
16362 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16363 	if (ret) {
16364 		dev_err(&pf->pdev->dev,
16365 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16366 		return;
16367 	}
16368 
16369 	flags = I40E_AQC_MC_MAG_EN
16370 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16371 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16372 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16373 	if (ret)
16374 		dev_err(&pf->pdev->dev,
16375 			"Failed to enable Multicast Magic Packet wake up\n");
16376 }
16377 
16378 /**
16379  * i40e_io_suspend - suspend all IO operations
16380  * @pf: pointer to i40e_pf struct
16381  *
16382  **/
16383 static int i40e_io_suspend(struct i40e_pf *pf)
16384 {
16385 	struct i40e_hw *hw = &pf->hw;
16386 
16387 	set_bit(__I40E_DOWN, pf->state);
16388 
16389 	/* Ensure service task will not be running */
16390 	timer_delete_sync(&pf->service_timer);
16391 	cancel_work_sync(&pf->service_task);
16392 
16393 	/* Client close must be called explicitly here because the timer
16394 	 * has been stopped.
16395 	 */
16396 	i40e_notify_client_of_netdev_close(pf, false);
16397 
16398 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16399 	    pf->wol_en)
16400 		i40e_enable_mc_magic_wake(pf);
16401 
16402 	/* Since we're going to destroy queues during the
16403 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16404 	 * whole section
16405 	 */
16406 	rtnl_lock();
16407 
16408 	i40e_prep_for_reset(pf);
16409 
16410 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16411 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16412 
16413 	/* Clear the interrupt scheme and release our IRQs so that the system
16414 	 * can safely hibernate even when there are a large number of CPUs.
16415 	 * Otherwise hibernation might fail when mapping all the vectors back
16416 	 * to CPU0.
16417 	 */
16418 	i40e_clear_interrupt_scheme(pf);
16419 
16420 	rtnl_unlock();
16421 
16422 	return 0;
16423 }
16424 
16425 /**
16426  * i40e_io_resume - resume IO operations
16427  * @pf: pointer to i40e_pf struct
16428  *
16429  **/
16430 static int i40e_io_resume(struct i40e_pf *pf)
16431 {
16432 	struct device *dev = &pf->pdev->dev;
16433 	int err;
16434 
16435 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16436 	 * since we're going to be restoring queues
16437 	 */
16438 	rtnl_lock();
16439 
16440 	/* We cleared the interrupt scheme when we suspended, so we need to
16441 	 * restore it now to resume device functionality.
16442 	 */
16443 	err = i40e_restore_interrupt_scheme(pf);
16444 	if (err) {
16445 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16446 			err);
16447 	}
16448 
16449 	clear_bit(__I40E_DOWN, pf->state);
16450 	i40e_reset_and_rebuild(pf, false, true);
16451 
16452 	rtnl_unlock();
16453 
16454 	/* Clear suspended state last after everything is recovered */
16455 	clear_bit(__I40E_SUSPENDED, pf->state);
16456 
16457 	/* Restart the service task */
16458 	mod_timer(&pf->service_timer,
16459 		  round_jiffies(jiffies + pf->service_timer_period));
16460 
16461 	return 0;
16462 }
16463 
16464 /**
16465  * i40e_pci_error_detected - warning that something funky happened in PCI land
16466  * @pdev: PCI device information struct
16467  * @error: the type of PCI error
16468  *
16469  * Called to warn that something happened and the error handling steps
16470  * are in progress.  Allows the driver to quiesce things, be ready for
16471  * remediation.
16472  **/
16473 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16474 						pci_channel_state_t error)
16475 {
16476 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16477 
16478 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16479 
16480 	if (!pf) {
16481 		dev_info(&pdev->dev,
16482 			 "Cannot recover - error happened during device probe\n");
16483 		return PCI_ERS_RESULT_DISCONNECT;
16484 	}
16485 
16486 	/* shutdown all operations */
16487 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16488 		i40e_io_suspend(pf);
16489 
16490 	/* Request a slot reset */
16491 	return PCI_ERS_RESULT_NEED_RESET;
16492 }
16493 
16494 /**
16495  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16496  * @pdev: PCI device information struct
16497  *
16498  * Called to find if the driver can work with the device now that
16499  * the pci slot has been reset.  If a basic connection seems good
16500  * (registers are readable and have sane content) then return a
16501  * happy little PCI_ERS_RESULT_xxx.
16502  **/
16503 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16504 {
16505 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16506 	pci_ers_result_t result;
16507 	u32 reg;
16508 
16509 	dev_dbg(&pdev->dev, "%s\n", __func__);
16510 	/* enable I/O and memory of the device  */
16511 	if (pci_enable_device(pdev)) {
16512 		dev_info(&pdev->dev,
16513 			 "Cannot re-enable PCI device after reset.\n");
16514 		result = PCI_ERS_RESULT_DISCONNECT;
16515 	} else {
16516 		pci_set_master(pdev);
16517 		pci_restore_state(pdev);
16518 		pci_wake_from_d3(pdev, false);
16519 
16520 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16521 		if (reg == 0)
16522 			result = PCI_ERS_RESULT_RECOVERED;
16523 		else
16524 			result = PCI_ERS_RESULT_DISCONNECT;
16525 	}
16526 
16527 	return result;
16528 }
16529 
16530 /**
16531  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16532  * @pdev: PCI device information struct
16533  */
16534 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16535 {
16536 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16537 
16538 	i40e_prep_for_reset(pf);
16539 }
16540 
16541 /**
16542  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16543  * @pdev: PCI device information struct
16544  */
16545 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16546 {
16547 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16548 
16549 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16550 		return;
16551 
16552 	i40e_reset_and_rebuild(pf, false, false);
16553 #ifdef CONFIG_PCI_IOV
16554 	i40e_restore_all_vfs_msi_state(pdev);
16555 #endif /* CONFIG_PCI_IOV */
16556 }
16557 
16558 /**
16559  * i40e_pci_error_resume - restart operations after PCI error recovery
16560  * @pdev: PCI device information struct
16561  *
16562  * Called to allow the driver to bring things back up after PCI error
16563  * and/or reset recovery has finished.
16564  **/
16565 static void i40e_pci_error_resume(struct pci_dev *pdev)
16566 {
16567 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16568 
16569 	dev_dbg(&pdev->dev, "%s\n", __func__);
16570 	if (test_bit(__I40E_SUSPENDED, pf->state))
16571 		return;
16572 
16573 	i40e_io_resume(pf);
16574 }
16575 
16576 /**
16577  * i40e_shutdown - PCI callback for shutting down
16578  * @pdev: PCI device information struct
16579  **/
16580 static void i40e_shutdown(struct pci_dev *pdev)
16581 {
16582 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16583 	struct i40e_hw *hw = &pf->hw;
16584 
16585 	set_bit(__I40E_SUSPENDED, pf->state);
16586 	set_bit(__I40E_DOWN, pf->state);
16587 
16588 	timer_delete_sync(&pf->service_timer);
16589 	cancel_work_sync(&pf->service_task);
16590 	i40e_cloud_filter_exit(pf);
16591 	i40e_fdir_teardown(pf);
16592 
16593 	/* Client close must be called explicitly here because the timer
16594 	 * has been stopped.
16595 	 */
16596 	i40e_notify_client_of_netdev_close(pf, false);
16597 
16598 	if (test_bit(I40E_HW_CAP_WOL_MC_MAGIC_PKT_WAKE, pf->hw.caps) &&
16599 	    pf->wol_en)
16600 		i40e_enable_mc_magic_wake(pf);
16601 
16602 	i40e_prep_for_reset(pf);
16603 
16604 	wr32(hw, I40E_PFPM_APM,
16605 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16606 	wr32(hw, I40E_PFPM_WUFC,
16607 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16608 
16609 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16610 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16611 	    !test_bit(I40E_FLAG_MSIX_ENA, pf->flags))
16612 		free_irq(pf->pdev->irq, pf);
16613 
16614 	/* Since we're going to destroy queues during the
16615 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16616 	 * whole section
16617 	 */
16618 	rtnl_lock();
16619 	i40e_clear_interrupt_scheme(pf);
16620 	rtnl_unlock();
16621 
16622 	if (system_state == SYSTEM_POWER_OFF) {
16623 		pci_wake_from_d3(pdev, pf->wol_en);
16624 		pci_set_power_state(pdev, PCI_D3hot);
16625 	}
16626 }
16627 
16628 /**
16629  * i40e_suspend - PM callback for moving to D3
16630  * @dev: generic device information structure
16631  **/
16632 static int i40e_suspend(struct device *dev)
16633 {
16634 	struct i40e_pf *pf = dev_get_drvdata(dev);
16635 
16636 	/* If we're already suspended, then there is nothing to do */
16637 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16638 		return 0;
16639 	return i40e_io_suspend(pf);
16640 }
16641 
16642 /**
16643  * i40e_resume - PM callback for waking up from D3
16644  * @dev: generic device information structure
16645  **/
16646 static int i40e_resume(struct device *dev)
16647 {
16648 	struct i40e_pf *pf = dev_get_drvdata(dev);
16649 
16650 	/* If we're not suspended, then there is nothing to do */
16651 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16652 		return 0;
16653 	return i40e_io_resume(pf);
16654 }
16655 
16656 static const struct pci_error_handlers i40e_err_handler = {
16657 	.error_detected = i40e_pci_error_detected,
16658 	.slot_reset = i40e_pci_error_slot_reset,
16659 	.reset_prepare = i40e_pci_error_reset_prepare,
16660 	.reset_done = i40e_pci_error_reset_done,
16661 	.resume = i40e_pci_error_resume,
16662 };
16663 
16664 static DEFINE_SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16665 
16666 static struct pci_driver i40e_driver = {
16667 	.name     = i40e_driver_name,
16668 	.id_table = i40e_pci_tbl,
16669 	.probe    = i40e_probe,
16670 	.remove   = i40e_remove,
16671 	.driver.pm = pm_sleep_ptr(&i40e_pm_ops),
16672 	.shutdown = i40e_shutdown,
16673 	.err_handler = &i40e_err_handler,
16674 	.sriov_configure = i40e_pci_sriov_configure,
16675 };
16676 
16677 /**
16678  * i40e_init_module - Driver registration routine
16679  *
16680  * i40e_init_module is the first routine called when the driver is
16681  * loaded. All it does is register with the PCI subsystem.
16682  **/
16683 static int __init i40e_init_module(void)
16684 {
16685 	int err;
16686 
16687 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16688 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16689 
16690 	/* There is no need to throttle the number of active tasks because
16691 	 * each device limits its own task using a state bit for scheduling
16692 	 * the service task, and the device tasks do not interfere with each
16693 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16694 	 * since we need to be able to guarantee forward progress even under
16695 	 * memory pressure.
16696 	 */
16697 	i40e_wq = alloc_workqueue("%s", WQ_PERCPU, 0, i40e_driver_name);
16698 	if (!i40e_wq) {
16699 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16700 		return -ENOMEM;
16701 	}
16702 
16703 	i40e_dbg_init();
16704 	err = pci_register_driver(&i40e_driver);
16705 	if (err) {
16706 		destroy_workqueue(i40e_wq);
16707 		i40e_dbg_exit();
16708 		return err;
16709 	}
16710 
16711 	return 0;
16712 }
16713 module_init(i40e_init_module);
16714 
16715 /**
16716  * i40e_exit_module - Driver exit cleanup routine
16717  *
16718  * i40e_exit_module is called just before the driver is removed
16719  * from memory.
16720  **/
16721 static void __exit i40e_exit_module(void)
16722 {
16723 	pci_unregister_driver(&i40e_driver);
16724 	destroy_workqueue(i40e_wq);
16725 	ida_destroy(&i40e_client_ida);
16726 	i40e_dbg_exit();
16727 }
16728 module_exit(i40e_exit_module);
16729