1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright(c) 2013 - 2021 Intel Corporation. */
3 
4 #include <linux/etherdevice.h>
5 #include <linux/of_net.h>
6 #include <linux/pci.h>
7 #include <linux/bpf.h>
8 #include <generated/utsrelease.h>
9 #include <linux/crash_dump.h>
10 
11 /* Local includes */
12 #include "i40e.h"
13 #include "i40e_diag.h"
14 #include "i40e_xsk.h"
15 #include <net/udp_tunnel.h>
16 #include <net/xdp_sock_drv.h>
17 /* All i40e tracepoints are defined by the include below, which
18  * must be included exactly once across the whole kernel with
19  * CREATE_TRACE_POINTS defined
20  */
21 #define CREATE_TRACE_POINTS
22 #include "i40e_trace.h"
23 
24 const char i40e_driver_name[] = "i40e";
25 static const char i40e_driver_string[] =
26 			"Intel(R) Ethernet Connection XL710 Network Driver";
27 
28 static const char i40e_copyright[] = "Copyright (c) 2013 - 2019 Intel Corporation.";
29 
30 /* a bit of forward declarations */
31 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi);
32 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired);
33 static int i40e_add_vsi(struct i40e_vsi *vsi);
34 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi);
35 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired);
36 static int i40e_setup_misc_vector(struct i40e_pf *pf);
37 static void i40e_determine_queue_usage(struct i40e_pf *pf);
38 static int i40e_setup_pf_filter_control(struct i40e_pf *pf);
39 static void i40e_prep_for_reset(struct i40e_pf *pf);
40 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
41 				   bool lock_acquired);
42 static int i40e_reset(struct i40e_pf *pf);
43 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired);
44 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf);
45 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf);
46 static bool i40e_check_recovery_mode(struct i40e_pf *pf);
47 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw);
48 static void i40e_fdir_sb_setup(struct i40e_pf *pf);
49 static int i40e_veb_get_bw_info(struct i40e_veb *veb);
50 static int i40e_get_capabilities(struct i40e_pf *pf,
51 				 enum i40e_admin_queue_opc list_type);
52 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf);
53 
54 /* i40e_pci_tbl - PCI Device ID Table
55  *
56  * Last entry must be all 0s
57  *
58  * { Vendor ID, Device ID, SubVendor ID, SubDevice ID,
59  *   Class, Class Mask, private data (not used) }
60  */
61 static const struct pci_device_id i40e_pci_tbl[] = {
62 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_XL710), 0},
63 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QEMU), 0},
64 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_B), 0},
65 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_C), 0},
66 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_A), 0},
67 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_B), 0},
68 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_C), 0},
69 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_BC), 0},
70 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T), 0},
71 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T4), 0},
72 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_BC), 0},
73 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_SFP), 0},
74 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_B), 0},
75 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_KX_X722), 0},
76 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_QSFP_X722), 0},
77 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722), 0},
78 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_1G_BASE_T_X722), 0},
79 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_10G_BASE_T_X722), 0},
80 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_I_X722), 0},
81 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_SFP_X722_A), 0},
82 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2), 0},
83 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_20G_KR2_A), 0},
84 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_X710_N3000), 0},
85 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_XXV710_N3000), 0},
86 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_B), 0},
87 	{PCI_VDEVICE(INTEL, I40E_DEV_ID_25G_SFP28), 0},
88 	/* required last entry */
89 	{0, }
90 };
91 MODULE_DEVICE_TABLE(pci, i40e_pci_tbl);
92 
93 #define I40E_MAX_VF_COUNT 128
94 static int debug = -1;
95 module_param(debug, uint, 0);
96 MODULE_PARM_DESC(debug, "Debug level (0=none,...,16=all), Debug mask (0x8XXXXXXX)");
97 
98 MODULE_AUTHOR("Intel Corporation, <e1000-devel@lists.sourceforge.net>");
99 MODULE_DESCRIPTION("Intel(R) Ethernet Connection XL710 Network Driver");
100 MODULE_LICENSE("GPL v2");
101 
102 static struct workqueue_struct *i40e_wq;
103 
104 static void netdev_hw_addr_refcnt(struct i40e_mac_filter *f,
105 				  struct net_device *netdev, int delta)
106 {
107 	struct netdev_hw_addr *ha;
108 
109 	if (!f || !netdev)
110 		return;
111 
112 	netdev_for_each_mc_addr(ha, netdev) {
113 		if (ether_addr_equal(ha->addr, f->macaddr)) {
114 			ha->refcount += delta;
115 			if (ha->refcount <= 0)
116 				ha->refcount = 1;
117 			break;
118 		}
119 	}
120 }
121 
122 /**
123  * i40e_allocate_dma_mem_d - OS specific memory alloc for shared code
124  * @hw:   pointer to the HW structure
125  * @mem:  ptr to mem struct to fill out
126  * @size: size of memory requested
127  * @alignment: what to align the allocation to
128  **/
129 int i40e_allocate_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem,
130 			    u64 size, u32 alignment)
131 {
132 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
133 
134 	mem->size = ALIGN(size, alignment);
135 	mem->va = dma_alloc_coherent(&pf->pdev->dev, mem->size, &mem->pa,
136 				     GFP_KERNEL);
137 	if (!mem->va)
138 		return -ENOMEM;
139 
140 	return 0;
141 }
142 
143 /**
144  * i40e_free_dma_mem_d - OS specific memory free for shared code
145  * @hw:   pointer to the HW structure
146  * @mem:  ptr to mem struct to free
147  **/
148 int i40e_free_dma_mem_d(struct i40e_hw *hw, struct i40e_dma_mem *mem)
149 {
150 	struct i40e_pf *pf = (struct i40e_pf *)hw->back;
151 
152 	dma_free_coherent(&pf->pdev->dev, mem->size, mem->va, mem->pa);
153 	mem->va = NULL;
154 	mem->pa = 0;
155 	mem->size = 0;
156 
157 	return 0;
158 }
159 
160 /**
161  * i40e_allocate_virt_mem_d - OS specific memory alloc for shared code
162  * @hw:   pointer to the HW structure
163  * @mem:  ptr to mem struct to fill out
164  * @size: size of memory requested
165  **/
166 int i40e_allocate_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem,
167 			     u32 size)
168 {
169 	mem->size = size;
170 	mem->va = kzalloc(size, GFP_KERNEL);
171 
172 	if (!mem->va)
173 		return -ENOMEM;
174 
175 	return 0;
176 }
177 
178 /**
179  * i40e_free_virt_mem_d - OS specific memory free for shared code
180  * @hw:   pointer to the HW structure
181  * @mem:  ptr to mem struct to free
182  **/
183 int i40e_free_virt_mem_d(struct i40e_hw *hw, struct i40e_virt_mem *mem)
184 {
185 	/* it's ok to kfree a NULL pointer */
186 	kfree(mem->va);
187 	mem->va = NULL;
188 	mem->size = 0;
189 
190 	return 0;
191 }
192 
193 /**
194  * i40e_get_lump - find a lump of free generic resource
195  * @pf: board private structure
196  * @pile: the pile of resource to search
197  * @needed: the number of items needed
198  * @id: an owner id to stick on the items assigned
199  *
200  * Returns the base item index of the lump, or negative for error
201  **/
202 static int i40e_get_lump(struct i40e_pf *pf, struct i40e_lump_tracking *pile,
203 			 u16 needed, u16 id)
204 {
205 	int ret = -ENOMEM;
206 	int i, j;
207 
208 	if (!pile || needed == 0 || id >= I40E_PILE_VALID_BIT) {
209 		dev_info(&pf->pdev->dev,
210 			 "param err: pile=%s needed=%d id=0x%04x\n",
211 			 pile ? "<valid>" : "<null>", needed, id);
212 		return -EINVAL;
213 	}
214 
215 	/* Allocate last queue in the pile for FDIR VSI queue
216 	 * so it doesn't fragment the qp_pile
217 	 */
218 	if (pile == pf->qp_pile && pf->vsi[id]->type == I40E_VSI_FDIR) {
219 		if (pile->list[pile->num_entries - 1] & I40E_PILE_VALID_BIT) {
220 			dev_err(&pf->pdev->dev,
221 				"Cannot allocate queue %d for I40E_VSI_FDIR\n",
222 				pile->num_entries - 1);
223 			return -ENOMEM;
224 		}
225 		pile->list[pile->num_entries - 1] = id | I40E_PILE_VALID_BIT;
226 		return pile->num_entries - 1;
227 	}
228 
229 	i = 0;
230 	while (i < pile->num_entries) {
231 		/* skip already allocated entries */
232 		if (pile->list[i] & I40E_PILE_VALID_BIT) {
233 			i++;
234 			continue;
235 		}
236 
237 		/* do we have enough in this lump? */
238 		for (j = 0; (j < needed) && ((i+j) < pile->num_entries); j++) {
239 			if (pile->list[i+j] & I40E_PILE_VALID_BIT)
240 				break;
241 		}
242 
243 		if (j == needed) {
244 			/* there was enough, so assign it to the requestor */
245 			for (j = 0; j < needed; j++)
246 				pile->list[i+j] = id | I40E_PILE_VALID_BIT;
247 			ret = i;
248 			break;
249 		}
250 
251 		/* not enough, so skip over it and continue looking */
252 		i += j;
253 	}
254 
255 	return ret;
256 }
257 
258 /**
259  * i40e_put_lump - return a lump of generic resource
260  * @pile: the pile of resource to search
261  * @index: the base item index
262  * @id: the owner id of the items assigned
263  *
264  * Returns the count of items in the lump
265  **/
266 static int i40e_put_lump(struct i40e_lump_tracking *pile, u16 index, u16 id)
267 {
268 	int valid_id = (id | I40E_PILE_VALID_BIT);
269 	int count = 0;
270 	u16 i;
271 
272 	if (!pile || index >= pile->num_entries)
273 		return -EINVAL;
274 
275 	for (i = index;
276 	     i < pile->num_entries && pile->list[i] == valid_id;
277 	     i++) {
278 		pile->list[i] = 0;
279 		count++;
280 	}
281 
282 
283 	return count;
284 }
285 
286 /**
287  * i40e_find_vsi_from_id - searches for the vsi with the given id
288  * @pf: the pf structure to search for the vsi
289  * @id: id of the vsi it is searching for
290  **/
291 struct i40e_vsi *i40e_find_vsi_from_id(struct i40e_pf *pf, u16 id)
292 {
293 	int i;
294 
295 	for (i = 0; i < pf->num_alloc_vsi; i++)
296 		if (pf->vsi[i] && (pf->vsi[i]->id == id))
297 			return pf->vsi[i];
298 
299 	return NULL;
300 }
301 
302 /**
303  * i40e_service_event_schedule - Schedule the service task to wake up
304  * @pf: board private structure
305  *
306  * If not already scheduled, this puts the task into the work queue
307  **/
308 void i40e_service_event_schedule(struct i40e_pf *pf)
309 {
310 	if ((!test_bit(__I40E_DOWN, pf->state) &&
311 	     !test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) ||
312 	      test_bit(__I40E_RECOVERY_MODE, pf->state))
313 		queue_work(i40e_wq, &pf->service_task);
314 }
315 
316 /**
317  * i40e_tx_timeout - Respond to a Tx Hang
318  * @netdev: network interface device structure
319  * @txqueue: queue number timing out
320  *
321  * If any port has noticed a Tx timeout, it is likely that the whole
322  * device is munged, not just the one netdev port, so go for the full
323  * reset.
324  **/
325 static void i40e_tx_timeout(struct net_device *netdev, unsigned int txqueue)
326 {
327 	struct i40e_netdev_priv *np = netdev_priv(netdev);
328 	struct i40e_vsi *vsi = np->vsi;
329 	struct i40e_pf *pf = vsi->back;
330 	struct i40e_ring *tx_ring = NULL;
331 	unsigned int i;
332 	u32 head, val;
333 
334 	pf->tx_timeout_count++;
335 
336 	/* with txqueue index, find the tx_ring struct */
337 	for (i = 0; i < vsi->num_queue_pairs; i++) {
338 		if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc) {
339 			if (txqueue ==
340 			    vsi->tx_rings[i]->queue_index) {
341 				tx_ring = vsi->tx_rings[i];
342 				break;
343 			}
344 		}
345 	}
346 
347 	if (time_after(jiffies, (pf->tx_timeout_last_recovery + HZ*20)))
348 		pf->tx_timeout_recovery_level = 1;  /* reset after some time */
349 	else if (time_before(jiffies,
350 		      (pf->tx_timeout_last_recovery + netdev->watchdog_timeo)))
351 		return;   /* don't do any new action before the next timeout */
352 
353 	/* don't kick off another recovery if one is already pending */
354 	if (test_and_set_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state))
355 		return;
356 
357 	if (tx_ring) {
358 		head = i40e_get_head(tx_ring);
359 		/* Read interrupt register */
360 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
361 			val = rd32(&pf->hw,
362 			     I40E_PFINT_DYN_CTLN(tx_ring->q_vector->v_idx +
363 						tx_ring->vsi->base_vector - 1));
364 		else
365 			val = rd32(&pf->hw, I40E_PFINT_DYN_CTL0);
366 
367 		netdev_info(netdev, "tx_timeout: VSI_seid: %d, Q %d, NTC: 0x%x, HWB: 0x%x, NTU: 0x%x, TAIL: 0x%x, INT: 0x%x\n",
368 			    vsi->seid, txqueue, tx_ring->next_to_clean,
369 			    head, tx_ring->next_to_use,
370 			    readl(tx_ring->tail), val);
371 	}
372 
373 	pf->tx_timeout_last_recovery = jiffies;
374 	netdev_info(netdev, "tx_timeout recovery level %d, txqueue %d\n",
375 		    pf->tx_timeout_recovery_level, txqueue);
376 
377 	switch (pf->tx_timeout_recovery_level) {
378 	case 1:
379 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
380 		break;
381 	case 2:
382 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
383 		break;
384 	case 3:
385 		set_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
386 		break;
387 	default:
388 		netdev_err(netdev, "tx_timeout recovery unsuccessful, device is in non-recoverable state.\n");
389 		set_bit(__I40E_DOWN_REQUESTED, pf->state);
390 		set_bit(__I40E_VSI_DOWN_REQUESTED, vsi->state);
391 		break;
392 	}
393 
394 	i40e_service_event_schedule(pf);
395 	pf->tx_timeout_recovery_level++;
396 }
397 
398 /**
399  * i40e_get_vsi_stats_struct - Get System Network Statistics
400  * @vsi: the VSI we care about
401  *
402  * Returns the address of the device statistics structure.
403  * The statistics are actually updated from the service task.
404  **/
405 struct rtnl_link_stats64 *i40e_get_vsi_stats_struct(struct i40e_vsi *vsi)
406 {
407 	return &vsi->net_stats;
408 }
409 
410 /**
411  * i40e_get_netdev_stats_struct_tx - populate stats from a Tx ring
412  * @ring: Tx ring to get statistics from
413  * @stats: statistics entry to be updated
414  **/
415 static void i40e_get_netdev_stats_struct_tx(struct i40e_ring *ring,
416 					    struct rtnl_link_stats64 *stats)
417 {
418 	u64 bytes, packets;
419 	unsigned int start;
420 
421 	do {
422 		start = u64_stats_fetch_begin(&ring->syncp);
423 		packets = ring->stats.packets;
424 		bytes   = ring->stats.bytes;
425 	} while (u64_stats_fetch_retry(&ring->syncp, start));
426 
427 	stats->tx_packets += packets;
428 	stats->tx_bytes   += bytes;
429 }
430 
431 /**
432  * i40e_get_netdev_stats_struct - Get statistics for netdev interface
433  * @netdev: network interface device structure
434  * @stats: data structure to store statistics
435  *
436  * Returns the address of the device statistics structure.
437  * The statistics are actually updated from the service task.
438  **/
439 static void i40e_get_netdev_stats_struct(struct net_device *netdev,
440 				  struct rtnl_link_stats64 *stats)
441 {
442 	struct i40e_netdev_priv *np = netdev_priv(netdev);
443 	struct i40e_vsi *vsi = np->vsi;
444 	struct rtnl_link_stats64 *vsi_stats = i40e_get_vsi_stats_struct(vsi);
445 	struct i40e_ring *ring;
446 	int i;
447 
448 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
449 		return;
450 
451 	if (!vsi->tx_rings)
452 		return;
453 
454 	rcu_read_lock();
455 	for (i = 0; i < vsi->num_queue_pairs; i++) {
456 		u64 bytes, packets;
457 		unsigned int start;
458 
459 		ring = READ_ONCE(vsi->tx_rings[i]);
460 		if (!ring)
461 			continue;
462 		i40e_get_netdev_stats_struct_tx(ring, stats);
463 
464 		if (i40e_enabled_xdp_vsi(vsi)) {
465 			ring = READ_ONCE(vsi->xdp_rings[i]);
466 			if (!ring)
467 				continue;
468 			i40e_get_netdev_stats_struct_tx(ring, stats);
469 		}
470 
471 		ring = READ_ONCE(vsi->rx_rings[i]);
472 		if (!ring)
473 			continue;
474 		do {
475 			start   = u64_stats_fetch_begin(&ring->syncp);
476 			packets = ring->stats.packets;
477 			bytes   = ring->stats.bytes;
478 		} while (u64_stats_fetch_retry(&ring->syncp, start));
479 
480 		stats->rx_packets += packets;
481 		stats->rx_bytes   += bytes;
482 
483 	}
484 	rcu_read_unlock();
485 
486 	/* following stats updated by i40e_watchdog_subtask() */
487 	stats->multicast	= vsi_stats->multicast;
488 	stats->tx_errors	= vsi_stats->tx_errors;
489 	stats->tx_dropped	= vsi_stats->tx_dropped;
490 	stats->rx_errors	= vsi_stats->rx_errors;
491 	stats->rx_dropped	= vsi_stats->rx_dropped;
492 	stats->rx_crc_errors	= vsi_stats->rx_crc_errors;
493 	stats->rx_length_errors	= vsi_stats->rx_length_errors;
494 }
495 
496 /**
497  * i40e_vsi_reset_stats - Resets all stats of the given vsi
498  * @vsi: the VSI to have its stats reset
499  **/
500 void i40e_vsi_reset_stats(struct i40e_vsi *vsi)
501 {
502 	struct rtnl_link_stats64 *ns;
503 	int i;
504 
505 	if (!vsi)
506 		return;
507 
508 	ns = i40e_get_vsi_stats_struct(vsi);
509 	memset(ns, 0, sizeof(*ns));
510 	memset(&vsi->net_stats_offsets, 0, sizeof(vsi->net_stats_offsets));
511 	memset(&vsi->eth_stats, 0, sizeof(vsi->eth_stats));
512 	memset(&vsi->eth_stats_offsets, 0, sizeof(vsi->eth_stats_offsets));
513 	if (vsi->rx_rings && vsi->rx_rings[0]) {
514 		for (i = 0; i < vsi->num_queue_pairs; i++) {
515 			memset(&vsi->rx_rings[i]->stats, 0,
516 			       sizeof(vsi->rx_rings[i]->stats));
517 			memset(&vsi->rx_rings[i]->rx_stats, 0,
518 			       sizeof(vsi->rx_rings[i]->rx_stats));
519 			memset(&vsi->tx_rings[i]->stats, 0,
520 			       sizeof(vsi->tx_rings[i]->stats));
521 			memset(&vsi->tx_rings[i]->tx_stats, 0,
522 			       sizeof(vsi->tx_rings[i]->tx_stats));
523 		}
524 	}
525 	vsi->stat_offsets_loaded = false;
526 }
527 
528 /**
529  * i40e_pf_reset_stats - Reset all of the stats for the given PF
530  * @pf: the PF to be reset
531  **/
532 void i40e_pf_reset_stats(struct i40e_pf *pf)
533 {
534 	int i;
535 
536 	memset(&pf->stats, 0, sizeof(pf->stats));
537 	memset(&pf->stats_offsets, 0, sizeof(pf->stats_offsets));
538 	pf->stat_offsets_loaded = false;
539 
540 	for (i = 0; i < I40E_MAX_VEB; i++) {
541 		if (pf->veb[i]) {
542 			memset(&pf->veb[i]->stats, 0,
543 			       sizeof(pf->veb[i]->stats));
544 			memset(&pf->veb[i]->stats_offsets, 0,
545 			       sizeof(pf->veb[i]->stats_offsets));
546 			memset(&pf->veb[i]->tc_stats, 0,
547 			       sizeof(pf->veb[i]->tc_stats));
548 			memset(&pf->veb[i]->tc_stats_offsets, 0,
549 			       sizeof(pf->veb[i]->tc_stats_offsets));
550 			pf->veb[i]->stat_offsets_loaded = false;
551 		}
552 	}
553 	pf->hw_csum_rx_error = 0;
554 }
555 
556 /**
557  * i40e_compute_pci_to_hw_id - compute index form PCI function.
558  * @vsi: ptr to the VSI to read from.
559  * @hw: ptr to the hardware info.
560  **/
561 static u32 i40e_compute_pci_to_hw_id(struct i40e_vsi *vsi, struct i40e_hw *hw)
562 {
563 	int pf_count = i40e_get_pf_count(hw);
564 
565 	if (vsi->type == I40E_VSI_SRIOV)
566 		return (hw->port * BIT(7)) / pf_count + vsi->vf_id;
567 
568 	return hw->port + BIT(7);
569 }
570 
571 /**
572  * i40e_stat_update64 - read and update a 64 bit stat from the chip.
573  * @hw: ptr to the hardware info.
574  * @hireg: the high 32 bit reg to read.
575  * @loreg: the low 32 bit reg to read.
576  * @offset_loaded: has the initial offset been loaded yet.
577  * @offset: ptr to current offset value.
578  * @stat: ptr to the stat.
579  *
580  * Since the device stats are not reset at PFReset, they will not
581  * be zeroed when the driver starts.  We'll save the first values read
582  * and use them as offsets to be subtracted from the raw values in order
583  * to report stats that count from zero.
584  **/
585 static void i40e_stat_update64(struct i40e_hw *hw, u32 hireg, u32 loreg,
586 			       bool offset_loaded, u64 *offset, u64 *stat)
587 {
588 	u64 new_data;
589 
590 	new_data = rd64(hw, loreg);
591 
592 	if (!offset_loaded || new_data < *offset)
593 		*offset = new_data;
594 	*stat = new_data - *offset;
595 }
596 
597 /**
598  * i40e_stat_update48 - read and update a 48 bit stat from the chip
599  * @hw: ptr to the hardware info
600  * @hireg: the high 32 bit reg to read
601  * @loreg: the low 32 bit reg to read
602  * @offset_loaded: has the initial offset been loaded yet
603  * @offset: ptr to current offset value
604  * @stat: ptr to the stat
605  *
606  * Since the device stats are not reset at PFReset, they likely will not
607  * be zeroed when the driver starts.  We'll save the first values read
608  * and use them as offsets to be subtracted from the raw values in order
609  * to report stats that count from zero.  In the process, we also manage
610  * the potential roll-over.
611  **/
612 static void i40e_stat_update48(struct i40e_hw *hw, u32 hireg, u32 loreg,
613 			       bool offset_loaded, u64 *offset, u64 *stat)
614 {
615 	u64 new_data;
616 
617 	if (hw->device_id == I40E_DEV_ID_QEMU) {
618 		new_data = rd32(hw, loreg);
619 		new_data |= ((u64)(rd32(hw, hireg) & 0xFFFF)) << 32;
620 	} else {
621 		new_data = rd64(hw, loreg);
622 	}
623 	if (!offset_loaded)
624 		*offset = new_data;
625 	if (likely(new_data >= *offset))
626 		*stat = new_data - *offset;
627 	else
628 		*stat = (new_data + BIT_ULL(48)) - *offset;
629 	*stat &= 0xFFFFFFFFFFFFULL;
630 }
631 
632 /**
633  * i40e_stat_update32 - read and update a 32 bit stat from the chip
634  * @hw: ptr to the hardware info
635  * @reg: the hw reg to read
636  * @offset_loaded: has the initial offset been loaded yet
637  * @offset: ptr to current offset value
638  * @stat: ptr to the stat
639  **/
640 static void i40e_stat_update32(struct i40e_hw *hw, u32 reg,
641 			       bool offset_loaded, u64 *offset, u64 *stat)
642 {
643 	u32 new_data;
644 
645 	new_data = rd32(hw, reg);
646 	if (!offset_loaded)
647 		*offset = new_data;
648 	if (likely(new_data >= *offset))
649 		*stat = (u32)(new_data - *offset);
650 	else
651 		*stat = (u32)((new_data + BIT_ULL(32)) - *offset);
652 }
653 
654 /**
655  * i40e_stat_update_and_clear32 - read and clear hw reg, update a 32 bit stat
656  * @hw: ptr to the hardware info
657  * @reg: the hw reg to read and clear
658  * @stat: ptr to the stat
659  **/
660 static void i40e_stat_update_and_clear32(struct i40e_hw *hw, u32 reg, u64 *stat)
661 {
662 	u32 new_data = rd32(hw, reg);
663 
664 	wr32(hw, reg, 1); /* must write a nonzero value to clear register */
665 	*stat += new_data;
666 }
667 
668 /**
669  * i40e_stats_update_rx_discards - update rx_discards.
670  * @vsi: ptr to the VSI to be updated.
671  * @hw: ptr to the hardware info.
672  * @stat_idx: VSI's stat_counter_idx.
673  * @offset_loaded: ptr to the VSI's stat_offsets_loaded.
674  * @stat_offset: ptr to stat_offset to store first read of specific register.
675  * @stat: ptr to VSI's stat to be updated.
676  **/
677 static void
678 i40e_stats_update_rx_discards(struct i40e_vsi *vsi, struct i40e_hw *hw,
679 			      int stat_idx, bool offset_loaded,
680 			      struct i40e_eth_stats *stat_offset,
681 			      struct i40e_eth_stats *stat)
682 {
683 	u64 rx_rdpc, rx_rxerr;
684 
685 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx), offset_loaded,
686 			   &stat_offset->rx_discards, &rx_rdpc);
687 	i40e_stat_update64(hw,
688 			   I40E_GL_RXERR1H(i40e_compute_pci_to_hw_id(vsi, hw)),
689 			   I40E_GL_RXERR1L(i40e_compute_pci_to_hw_id(vsi, hw)),
690 			   offset_loaded, &stat_offset->rx_discards_other,
691 			   &rx_rxerr);
692 
693 	stat->rx_discards = rx_rdpc + rx_rxerr;
694 }
695 
696 /**
697  * i40e_update_eth_stats - Update VSI-specific ethernet statistics counters.
698  * @vsi: the VSI to be updated
699  **/
700 void i40e_update_eth_stats(struct i40e_vsi *vsi)
701 {
702 	int stat_idx = le16_to_cpu(vsi->info.stat_counter_idx);
703 	struct i40e_pf *pf = vsi->back;
704 	struct i40e_hw *hw = &pf->hw;
705 	struct i40e_eth_stats *oes;
706 	struct i40e_eth_stats *es;     /* device's eth stats */
707 
708 	es = &vsi->eth_stats;
709 	oes = &vsi->eth_stats_offsets;
710 
711 	/* Gather up the stats that the hw collects */
712 	i40e_stat_update32(hw, I40E_GLV_TEPC(stat_idx),
713 			   vsi->stat_offsets_loaded,
714 			   &oes->tx_errors, &es->tx_errors);
715 	i40e_stat_update32(hw, I40E_GLV_RDPC(stat_idx),
716 			   vsi->stat_offsets_loaded,
717 			   &oes->rx_discards, &es->rx_discards);
718 	i40e_stat_update32(hw, I40E_GLV_RUPP(stat_idx),
719 			   vsi->stat_offsets_loaded,
720 			   &oes->rx_unknown_protocol, &es->rx_unknown_protocol);
721 
722 	i40e_stat_update48(hw, I40E_GLV_GORCH(stat_idx),
723 			   I40E_GLV_GORCL(stat_idx),
724 			   vsi->stat_offsets_loaded,
725 			   &oes->rx_bytes, &es->rx_bytes);
726 	i40e_stat_update48(hw, I40E_GLV_UPRCH(stat_idx),
727 			   I40E_GLV_UPRCL(stat_idx),
728 			   vsi->stat_offsets_loaded,
729 			   &oes->rx_unicast, &es->rx_unicast);
730 	i40e_stat_update48(hw, I40E_GLV_MPRCH(stat_idx),
731 			   I40E_GLV_MPRCL(stat_idx),
732 			   vsi->stat_offsets_loaded,
733 			   &oes->rx_multicast, &es->rx_multicast);
734 	i40e_stat_update48(hw, I40E_GLV_BPRCH(stat_idx),
735 			   I40E_GLV_BPRCL(stat_idx),
736 			   vsi->stat_offsets_loaded,
737 			   &oes->rx_broadcast, &es->rx_broadcast);
738 
739 	i40e_stat_update48(hw, I40E_GLV_GOTCH(stat_idx),
740 			   I40E_GLV_GOTCL(stat_idx),
741 			   vsi->stat_offsets_loaded,
742 			   &oes->tx_bytes, &es->tx_bytes);
743 	i40e_stat_update48(hw, I40E_GLV_UPTCH(stat_idx),
744 			   I40E_GLV_UPTCL(stat_idx),
745 			   vsi->stat_offsets_loaded,
746 			   &oes->tx_unicast, &es->tx_unicast);
747 	i40e_stat_update48(hw, I40E_GLV_MPTCH(stat_idx),
748 			   I40E_GLV_MPTCL(stat_idx),
749 			   vsi->stat_offsets_loaded,
750 			   &oes->tx_multicast, &es->tx_multicast);
751 	i40e_stat_update48(hw, I40E_GLV_BPTCH(stat_idx),
752 			   I40E_GLV_BPTCL(stat_idx),
753 			   vsi->stat_offsets_loaded,
754 			   &oes->tx_broadcast, &es->tx_broadcast);
755 
756 	i40e_stats_update_rx_discards(vsi, hw, stat_idx,
757 				      vsi->stat_offsets_loaded, oes, es);
758 
759 	vsi->stat_offsets_loaded = true;
760 }
761 
762 /**
763  * i40e_update_veb_stats - Update Switch component statistics
764  * @veb: the VEB being updated
765  **/
766 void i40e_update_veb_stats(struct i40e_veb *veb)
767 {
768 	struct i40e_pf *pf = veb->pf;
769 	struct i40e_hw *hw = &pf->hw;
770 	struct i40e_eth_stats *oes;
771 	struct i40e_eth_stats *es;     /* device's eth stats */
772 	struct i40e_veb_tc_stats *veb_oes;
773 	struct i40e_veb_tc_stats *veb_es;
774 	int i, idx = 0;
775 
776 	idx = veb->stats_idx;
777 	es = &veb->stats;
778 	oes = &veb->stats_offsets;
779 	veb_es = &veb->tc_stats;
780 	veb_oes = &veb->tc_stats_offsets;
781 
782 	/* Gather up the stats that the hw collects */
783 	i40e_stat_update32(hw, I40E_GLSW_TDPC(idx),
784 			   veb->stat_offsets_loaded,
785 			   &oes->tx_discards, &es->tx_discards);
786 	if (hw->revision_id > 0)
787 		i40e_stat_update32(hw, I40E_GLSW_RUPP(idx),
788 				   veb->stat_offsets_loaded,
789 				   &oes->rx_unknown_protocol,
790 				   &es->rx_unknown_protocol);
791 	i40e_stat_update48(hw, I40E_GLSW_GORCH(idx), I40E_GLSW_GORCL(idx),
792 			   veb->stat_offsets_loaded,
793 			   &oes->rx_bytes, &es->rx_bytes);
794 	i40e_stat_update48(hw, I40E_GLSW_UPRCH(idx), I40E_GLSW_UPRCL(idx),
795 			   veb->stat_offsets_loaded,
796 			   &oes->rx_unicast, &es->rx_unicast);
797 	i40e_stat_update48(hw, I40E_GLSW_MPRCH(idx), I40E_GLSW_MPRCL(idx),
798 			   veb->stat_offsets_loaded,
799 			   &oes->rx_multicast, &es->rx_multicast);
800 	i40e_stat_update48(hw, I40E_GLSW_BPRCH(idx), I40E_GLSW_BPRCL(idx),
801 			   veb->stat_offsets_loaded,
802 			   &oes->rx_broadcast, &es->rx_broadcast);
803 
804 	i40e_stat_update48(hw, I40E_GLSW_GOTCH(idx), I40E_GLSW_GOTCL(idx),
805 			   veb->stat_offsets_loaded,
806 			   &oes->tx_bytes, &es->tx_bytes);
807 	i40e_stat_update48(hw, I40E_GLSW_UPTCH(idx), I40E_GLSW_UPTCL(idx),
808 			   veb->stat_offsets_loaded,
809 			   &oes->tx_unicast, &es->tx_unicast);
810 	i40e_stat_update48(hw, I40E_GLSW_MPTCH(idx), I40E_GLSW_MPTCL(idx),
811 			   veb->stat_offsets_loaded,
812 			   &oes->tx_multicast, &es->tx_multicast);
813 	i40e_stat_update48(hw, I40E_GLSW_BPTCH(idx), I40E_GLSW_BPTCL(idx),
814 			   veb->stat_offsets_loaded,
815 			   &oes->tx_broadcast, &es->tx_broadcast);
816 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
817 		i40e_stat_update48(hw, I40E_GLVEBTC_RPCH(i, idx),
818 				   I40E_GLVEBTC_RPCL(i, idx),
819 				   veb->stat_offsets_loaded,
820 				   &veb_oes->tc_rx_packets[i],
821 				   &veb_es->tc_rx_packets[i]);
822 		i40e_stat_update48(hw, I40E_GLVEBTC_RBCH(i, idx),
823 				   I40E_GLVEBTC_RBCL(i, idx),
824 				   veb->stat_offsets_loaded,
825 				   &veb_oes->tc_rx_bytes[i],
826 				   &veb_es->tc_rx_bytes[i]);
827 		i40e_stat_update48(hw, I40E_GLVEBTC_TPCH(i, idx),
828 				   I40E_GLVEBTC_TPCL(i, idx),
829 				   veb->stat_offsets_loaded,
830 				   &veb_oes->tc_tx_packets[i],
831 				   &veb_es->tc_tx_packets[i]);
832 		i40e_stat_update48(hw, I40E_GLVEBTC_TBCH(i, idx),
833 				   I40E_GLVEBTC_TBCL(i, idx),
834 				   veb->stat_offsets_loaded,
835 				   &veb_oes->tc_tx_bytes[i],
836 				   &veb_es->tc_tx_bytes[i]);
837 	}
838 	veb->stat_offsets_loaded = true;
839 }
840 
841 /**
842  * i40e_update_vsi_stats - Update the vsi statistics counters.
843  * @vsi: the VSI to be updated
844  *
845  * There are a few instances where we store the same stat in a
846  * couple of different structs.  This is partly because we have
847  * the netdev stats that need to be filled out, which is slightly
848  * different from the "eth_stats" defined by the chip and used in
849  * VF communications.  We sort it out here.
850  **/
851 static void i40e_update_vsi_stats(struct i40e_vsi *vsi)
852 {
853 	u64 rx_page, rx_buf, rx_reuse, rx_alloc, rx_waive, rx_busy;
854 	struct i40e_pf *pf = vsi->back;
855 	struct rtnl_link_stats64 *ons;
856 	struct rtnl_link_stats64 *ns;   /* netdev stats */
857 	struct i40e_eth_stats *oes;
858 	struct i40e_eth_stats *es;     /* device's eth stats */
859 	u64 tx_restart, tx_busy;
860 	struct i40e_ring *p;
861 	u64 bytes, packets;
862 	unsigned int start;
863 	u64 tx_linearize;
864 	u64 tx_force_wb;
865 	u64 tx_stopped;
866 	u64 rx_p, rx_b;
867 	u64 tx_p, tx_b;
868 	u16 q;
869 
870 	if (test_bit(__I40E_VSI_DOWN, vsi->state) ||
871 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
872 		return;
873 
874 	ns = i40e_get_vsi_stats_struct(vsi);
875 	ons = &vsi->net_stats_offsets;
876 	es = &vsi->eth_stats;
877 	oes = &vsi->eth_stats_offsets;
878 
879 	/* Gather up the netdev and vsi stats that the driver collects
880 	 * on the fly during packet processing
881 	 */
882 	rx_b = rx_p = 0;
883 	tx_b = tx_p = 0;
884 	tx_restart = tx_busy = tx_linearize = tx_force_wb = 0;
885 	tx_stopped = 0;
886 	rx_page = 0;
887 	rx_buf = 0;
888 	rx_reuse = 0;
889 	rx_alloc = 0;
890 	rx_waive = 0;
891 	rx_busy = 0;
892 	rcu_read_lock();
893 	for (q = 0; q < vsi->num_queue_pairs; q++) {
894 		/* locate Tx ring */
895 		p = READ_ONCE(vsi->tx_rings[q]);
896 		if (!p)
897 			continue;
898 
899 		do {
900 			start = u64_stats_fetch_begin(&p->syncp);
901 			packets = p->stats.packets;
902 			bytes = p->stats.bytes;
903 		} while (u64_stats_fetch_retry(&p->syncp, start));
904 		tx_b += bytes;
905 		tx_p += packets;
906 		tx_restart += p->tx_stats.restart_queue;
907 		tx_busy += p->tx_stats.tx_busy;
908 		tx_linearize += p->tx_stats.tx_linearize;
909 		tx_force_wb += p->tx_stats.tx_force_wb;
910 		tx_stopped += p->tx_stats.tx_stopped;
911 
912 		/* locate Rx ring */
913 		p = READ_ONCE(vsi->rx_rings[q]);
914 		if (!p)
915 			continue;
916 
917 		do {
918 			start = u64_stats_fetch_begin(&p->syncp);
919 			packets = p->stats.packets;
920 			bytes = p->stats.bytes;
921 		} while (u64_stats_fetch_retry(&p->syncp, start));
922 		rx_b += bytes;
923 		rx_p += packets;
924 		rx_buf += p->rx_stats.alloc_buff_failed;
925 		rx_page += p->rx_stats.alloc_page_failed;
926 		rx_reuse += p->rx_stats.page_reuse_count;
927 		rx_alloc += p->rx_stats.page_alloc_count;
928 		rx_waive += p->rx_stats.page_waive_count;
929 		rx_busy += p->rx_stats.page_busy_count;
930 
931 		if (i40e_enabled_xdp_vsi(vsi)) {
932 			/* locate XDP ring */
933 			p = READ_ONCE(vsi->xdp_rings[q]);
934 			if (!p)
935 				continue;
936 
937 			do {
938 				start = u64_stats_fetch_begin(&p->syncp);
939 				packets = p->stats.packets;
940 				bytes = p->stats.bytes;
941 			} while (u64_stats_fetch_retry(&p->syncp, start));
942 			tx_b += bytes;
943 			tx_p += packets;
944 			tx_restart += p->tx_stats.restart_queue;
945 			tx_busy += p->tx_stats.tx_busy;
946 			tx_linearize += p->tx_stats.tx_linearize;
947 			tx_force_wb += p->tx_stats.tx_force_wb;
948 		}
949 	}
950 	rcu_read_unlock();
951 	vsi->tx_restart = tx_restart;
952 	vsi->tx_busy = tx_busy;
953 	vsi->tx_linearize = tx_linearize;
954 	vsi->tx_force_wb = tx_force_wb;
955 	vsi->tx_stopped = tx_stopped;
956 	vsi->rx_page_failed = rx_page;
957 	vsi->rx_buf_failed = rx_buf;
958 	vsi->rx_page_reuse = rx_reuse;
959 	vsi->rx_page_alloc = rx_alloc;
960 	vsi->rx_page_waive = rx_waive;
961 	vsi->rx_page_busy = rx_busy;
962 
963 	ns->rx_packets = rx_p;
964 	ns->rx_bytes = rx_b;
965 	ns->tx_packets = tx_p;
966 	ns->tx_bytes = tx_b;
967 
968 	/* update netdev stats from eth stats */
969 	i40e_update_eth_stats(vsi);
970 	ons->tx_errors = oes->tx_errors;
971 	ns->tx_errors = es->tx_errors;
972 	ons->multicast = oes->rx_multicast;
973 	ns->multicast = es->rx_multicast;
974 	ons->rx_dropped = oes->rx_discards;
975 	ns->rx_dropped = es->rx_discards;
976 	ons->tx_dropped = oes->tx_discards;
977 	ns->tx_dropped = es->tx_discards;
978 
979 	/* pull in a couple PF stats if this is the main vsi */
980 	if (vsi == pf->vsi[pf->lan_vsi]) {
981 		ns->rx_crc_errors = pf->stats.crc_errors;
982 		ns->rx_errors = pf->stats.crc_errors + pf->stats.illegal_bytes;
983 		ns->rx_length_errors = pf->stats.rx_length_errors;
984 	}
985 }
986 
987 /**
988  * i40e_update_pf_stats - Update the PF statistics counters.
989  * @pf: the PF to be updated
990  **/
991 static void i40e_update_pf_stats(struct i40e_pf *pf)
992 {
993 	struct i40e_hw_port_stats *osd = &pf->stats_offsets;
994 	struct i40e_hw_port_stats *nsd = &pf->stats;
995 	struct i40e_hw *hw = &pf->hw;
996 	u32 val;
997 	int i;
998 
999 	i40e_stat_update48(hw, I40E_GLPRT_GORCH(hw->port),
1000 			   I40E_GLPRT_GORCL(hw->port),
1001 			   pf->stat_offsets_loaded,
1002 			   &osd->eth.rx_bytes, &nsd->eth.rx_bytes);
1003 	i40e_stat_update48(hw, I40E_GLPRT_GOTCH(hw->port),
1004 			   I40E_GLPRT_GOTCL(hw->port),
1005 			   pf->stat_offsets_loaded,
1006 			   &osd->eth.tx_bytes, &nsd->eth.tx_bytes);
1007 	i40e_stat_update32(hw, I40E_GLPRT_RDPC(hw->port),
1008 			   pf->stat_offsets_loaded,
1009 			   &osd->eth.rx_discards,
1010 			   &nsd->eth.rx_discards);
1011 	i40e_stat_update48(hw, I40E_GLPRT_UPRCH(hw->port),
1012 			   I40E_GLPRT_UPRCL(hw->port),
1013 			   pf->stat_offsets_loaded,
1014 			   &osd->eth.rx_unicast,
1015 			   &nsd->eth.rx_unicast);
1016 	i40e_stat_update48(hw, I40E_GLPRT_MPRCH(hw->port),
1017 			   I40E_GLPRT_MPRCL(hw->port),
1018 			   pf->stat_offsets_loaded,
1019 			   &osd->eth.rx_multicast,
1020 			   &nsd->eth.rx_multicast);
1021 	i40e_stat_update48(hw, I40E_GLPRT_BPRCH(hw->port),
1022 			   I40E_GLPRT_BPRCL(hw->port),
1023 			   pf->stat_offsets_loaded,
1024 			   &osd->eth.rx_broadcast,
1025 			   &nsd->eth.rx_broadcast);
1026 	i40e_stat_update48(hw, I40E_GLPRT_UPTCH(hw->port),
1027 			   I40E_GLPRT_UPTCL(hw->port),
1028 			   pf->stat_offsets_loaded,
1029 			   &osd->eth.tx_unicast,
1030 			   &nsd->eth.tx_unicast);
1031 	i40e_stat_update48(hw, I40E_GLPRT_MPTCH(hw->port),
1032 			   I40E_GLPRT_MPTCL(hw->port),
1033 			   pf->stat_offsets_loaded,
1034 			   &osd->eth.tx_multicast,
1035 			   &nsd->eth.tx_multicast);
1036 	i40e_stat_update48(hw, I40E_GLPRT_BPTCH(hw->port),
1037 			   I40E_GLPRT_BPTCL(hw->port),
1038 			   pf->stat_offsets_loaded,
1039 			   &osd->eth.tx_broadcast,
1040 			   &nsd->eth.tx_broadcast);
1041 
1042 	i40e_stat_update32(hw, I40E_GLPRT_TDOLD(hw->port),
1043 			   pf->stat_offsets_loaded,
1044 			   &osd->tx_dropped_link_down,
1045 			   &nsd->tx_dropped_link_down);
1046 
1047 	i40e_stat_update32(hw, I40E_GLPRT_CRCERRS(hw->port),
1048 			   pf->stat_offsets_loaded,
1049 			   &osd->crc_errors, &nsd->crc_errors);
1050 
1051 	i40e_stat_update32(hw, I40E_GLPRT_ILLERRC(hw->port),
1052 			   pf->stat_offsets_loaded,
1053 			   &osd->illegal_bytes, &nsd->illegal_bytes);
1054 
1055 	i40e_stat_update32(hw, I40E_GLPRT_MLFC(hw->port),
1056 			   pf->stat_offsets_loaded,
1057 			   &osd->mac_local_faults,
1058 			   &nsd->mac_local_faults);
1059 	i40e_stat_update32(hw, I40E_GLPRT_MRFC(hw->port),
1060 			   pf->stat_offsets_loaded,
1061 			   &osd->mac_remote_faults,
1062 			   &nsd->mac_remote_faults);
1063 
1064 	i40e_stat_update32(hw, I40E_GLPRT_RLEC(hw->port),
1065 			   pf->stat_offsets_loaded,
1066 			   &osd->rx_length_errors,
1067 			   &nsd->rx_length_errors);
1068 
1069 	i40e_stat_update32(hw, I40E_GLPRT_LXONRXC(hw->port),
1070 			   pf->stat_offsets_loaded,
1071 			   &osd->link_xon_rx, &nsd->link_xon_rx);
1072 	i40e_stat_update32(hw, I40E_GLPRT_LXONTXC(hw->port),
1073 			   pf->stat_offsets_loaded,
1074 			   &osd->link_xon_tx, &nsd->link_xon_tx);
1075 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFRXC(hw->port),
1076 			   pf->stat_offsets_loaded,
1077 			   &osd->link_xoff_rx, &nsd->link_xoff_rx);
1078 	i40e_stat_update32(hw, I40E_GLPRT_LXOFFTXC(hw->port),
1079 			   pf->stat_offsets_loaded,
1080 			   &osd->link_xoff_tx, &nsd->link_xoff_tx);
1081 
1082 	for (i = 0; i < 8; i++) {
1083 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFRXC(hw->port, i),
1084 				   pf->stat_offsets_loaded,
1085 				   &osd->priority_xoff_rx[i],
1086 				   &nsd->priority_xoff_rx[i]);
1087 		i40e_stat_update32(hw, I40E_GLPRT_PXONRXC(hw->port, i),
1088 				   pf->stat_offsets_loaded,
1089 				   &osd->priority_xon_rx[i],
1090 				   &nsd->priority_xon_rx[i]);
1091 		i40e_stat_update32(hw, I40E_GLPRT_PXONTXC(hw->port, i),
1092 				   pf->stat_offsets_loaded,
1093 				   &osd->priority_xon_tx[i],
1094 				   &nsd->priority_xon_tx[i]);
1095 		i40e_stat_update32(hw, I40E_GLPRT_PXOFFTXC(hw->port, i),
1096 				   pf->stat_offsets_loaded,
1097 				   &osd->priority_xoff_tx[i],
1098 				   &nsd->priority_xoff_tx[i]);
1099 		i40e_stat_update32(hw,
1100 				   I40E_GLPRT_RXON2OFFCNT(hw->port, i),
1101 				   pf->stat_offsets_loaded,
1102 				   &osd->priority_xon_2_xoff[i],
1103 				   &nsd->priority_xon_2_xoff[i]);
1104 	}
1105 
1106 	i40e_stat_update48(hw, I40E_GLPRT_PRC64H(hw->port),
1107 			   I40E_GLPRT_PRC64L(hw->port),
1108 			   pf->stat_offsets_loaded,
1109 			   &osd->rx_size_64, &nsd->rx_size_64);
1110 	i40e_stat_update48(hw, I40E_GLPRT_PRC127H(hw->port),
1111 			   I40E_GLPRT_PRC127L(hw->port),
1112 			   pf->stat_offsets_loaded,
1113 			   &osd->rx_size_127, &nsd->rx_size_127);
1114 	i40e_stat_update48(hw, I40E_GLPRT_PRC255H(hw->port),
1115 			   I40E_GLPRT_PRC255L(hw->port),
1116 			   pf->stat_offsets_loaded,
1117 			   &osd->rx_size_255, &nsd->rx_size_255);
1118 	i40e_stat_update48(hw, I40E_GLPRT_PRC511H(hw->port),
1119 			   I40E_GLPRT_PRC511L(hw->port),
1120 			   pf->stat_offsets_loaded,
1121 			   &osd->rx_size_511, &nsd->rx_size_511);
1122 	i40e_stat_update48(hw, I40E_GLPRT_PRC1023H(hw->port),
1123 			   I40E_GLPRT_PRC1023L(hw->port),
1124 			   pf->stat_offsets_loaded,
1125 			   &osd->rx_size_1023, &nsd->rx_size_1023);
1126 	i40e_stat_update48(hw, I40E_GLPRT_PRC1522H(hw->port),
1127 			   I40E_GLPRT_PRC1522L(hw->port),
1128 			   pf->stat_offsets_loaded,
1129 			   &osd->rx_size_1522, &nsd->rx_size_1522);
1130 	i40e_stat_update48(hw, I40E_GLPRT_PRC9522H(hw->port),
1131 			   I40E_GLPRT_PRC9522L(hw->port),
1132 			   pf->stat_offsets_loaded,
1133 			   &osd->rx_size_big, &nsd->rx_size_big);
1134 
1135 	i40e_stat_update48(hw, I40E_GLPRT_PTC64H(hw->port),
1136 			   I40E_GLPRT_PTC64L(hw->port),
1137 			   pf->stat_offsets_loaded,
1138 			   &osd->tx_size_64, &nsd->tx_size_64);
1139 	i40e_stat_update48(hw, I40E_GLPRT_PTC127H(hw->port),
1140 			   I40E_GLPRT_PTC127L(hw->port),
1141 			   pf->stat_offsets_loaded,
1142 			   &osd->tx_size_127, &nsd->tx_size_127);
1143 	i40e_stat_update48(hw, I40E_GLPRT_PTC255H(hw->port),
1144 			   I40E_GLPRT_PTC255L(hw->port),
1145 			   pf->stat_offsets_loaded,
1146 			   &osd->tx_size_255, &nsd->tx_size_255);
1147 	i40e_stat_update48(hw, I40E_GLPRT_PTC511H(hw->port),
1148 			   I40E_GLPRT_PTC511L(hw->port),
1149 			   pf->stat_offsets_loaded,
1150 			   &osd->tx_size_511, &nsd->tx_size_511);
1151 	i40e_stat_update48(hw, I40E_GLPRT_PTC1023H(hw->port),
1152 			   I40E_GLPRT_PTC1023L(hw->port),
1153 			   pf->stat_offsets_loaded,
1154 			   &osd->tx_size_1023, &nsd->tx_size_1023);
1155 	i40e_stat_update48(hw, I40E_GLPRT_PTC1522H(hw->port),
1156 			   I40E_GLPRT_PTC1522L(hw->port),
1157 			   pf->stat_offsets_loaded,
1158 			   &osd->tx_size_1522, &nsd->tx_size_1522);
1159 	i40e_stat_update48(hw, I40E_GLPRT_PTC9522H(hw->port),
1160 			   I40E_GLPRT_PTC9522L(hw->port),
1161 			   pf->stat_offsets_loaded,
1162 			   &osd->tx_size_big, &nsd->tx_size_big);
1163 
1164 	i40e_stat_update32(hw, I40E_GLPRT_RUC(hw->port),
1165 			   pf->stat_offsets_loaded,
1166 			   &osd->rx_undersize, &nsd->rx_undersize);
1167 	i40e_stat_update32(hw, I40E_GLPRT_RFC(hw->port),
1168 			   pf->stat_offsets_loaded,
1169 			   &osd->rx_fragments, &nsd->rx_fragments);
1170 	i40e_stat_update32(hw, I40E_GLPRT_ROC(hw->port),
1171 			   pf->stat_offsets_loaded,
1172 			   &osd->rx_oversize, &nsd->rx_oversize);
1173 	i40e_stat_update32(hw, I40E_GLPRT_RJC(hw->port),
1174 			   pf->stat_offsets_loaded,
1175 			   &osd->rx_jabber, &nsd->rx_jabber);
1176 
1177 	/* FDIR stats */
1178 	i40e_stat_update_and_clear32(hw,
1179 			I40E_GLQF_PCNT(I40E_FD_ATR_STAT_IDX(hw->pf_id)),
1180 			&nsd->fd_atr_match);
1181 	i40e_stat_update_and_clear32(hw,
1182 			I40E_GLQF_PCNT(I40E_FD_SB_STAT_IDX(hw->pf_id)),
1183 			&nsd->fd_sb_match);
1184 	i40e_stat_update_and_clear32(hw,
1185 			I40E_GLQF_PCNT(I40E_FD_ATR_TUNNEL_STAT_IDX(hw->pf_id)),
1186 			&nsd->fd_atr_tunnel_match);
1187 
1188 	val = rd32(hw, I40E_PRTPM_EEE_STAT);
1189 	nsd->tx_lpi_status =
1190 		       (val & I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_MASK) >>
1191 			I40E_PRTPM_EEE_STAT_TX_LPI_STATUS_SHIFT;
1192 	nsd->rx_lpi_status =
1193 		       (val & I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_MASK) >>
1194 			I40E_PRTPM_EEE_STAT_RX_LPI_STATUS_SHIFT;
1195 	i40e_stat_update32(hw, I40E_PRTPM_TLPIC,
1196 			   pf->stat_offsets_loaded,
1197 			   &osd->tx_lpi_count, &nsd->tx_lpi_count);
1198 	i40e_stat_update32(hw, I40E_PRTPM_RLPIC,
1199 			   pf->stat_offsets_loaded,
1200 			   &osd->rx_lpi_count, &nsd->rx_lpi_count);
1201 
1202 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED &&
1203 	    !test_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
1204 		nsd->fd_sb_status = true;
1205 	else
1206 		nsd->fd_sb_status = false;
1207 
1208 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED &&
1209 	    !test_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
1210 		nsd->fd_atr_status = true;
1211 	else
1212 		nsd->fd_atr_status = false;
1213 
1214 	pf->stat_offsets_loaded = true;
1215 }
1216 
1217 /**
1218  * i40e_update_stats - Update the various statistics counters.
1219  * @vsi: the VSI to be updated
1220  *
1221  * Update the various stats for this VSI and its related entities.
1222  **/
1223 void i40e_update_stats(struct i40e_vsi *vsi)
1224 {
1225 	struct i40e_pf *pf = vsi->back;
1226 
1227 	if (vsi == pf->vsi[pf->lan_vsi])
1228 		i40e_update_pf_stats(pf);
1229 
1230 	i40e_update_vsi_stats(vsi);
1231 }
1232 
1233 /**
1234  * i40e_count_filters - counts VSI mac filters
1235  * @vsi: the VSI to be searched
1236  *
1237  * Returns count of mac filters
1238  **/
1239 int i40e_count_filters(struct i40e_vsi *vsi)
1240 {
1241 	struct i40e_mac_filter *f;
1242 	struct hlist_node *h;
1243 	int bkt;
1244 	int cnt = 0;
1245 
1246 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
1247 		++cnt;
1248 
1249 	return cnt;
1250 }
1251 
1252 /**
1253  * i40e_find_filter - Search VSI filter list for specific mac/vlan filter
1254  * @vsi: the VSI to be searched
1255  * @macaddr: the MAC address
1256  * @vlan: the vlan
1257  *
1258  * Returns ptr to the filter object or NULL
1259  **/
1260 static struct i40e_mac_filter *i40e_find_filter(struct i40e_vsi *vsi,
1261 						const u8 *macaddr, s16 vlan)
1262 {
1263 	struct i40e_mac_filter *f;
1264 	u64 key;
1265 
1266 	if (!vsi || !macaddr)
1267 		return NULL;
1268 
1269 	key = i40e_addr_to_hkey(macaddr);
1270 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1271 		if ((ether_addr_equal(macaddr, f->macaddr)) &&
1272 		    (vlan == f->vlan))
1273 			return f;
1274 	}
1275 	return NULL;
1276 }
1277 
1278 /**
1279  * i40e_find_mac - Find a mac addr in the macvlan filters list
1280  * @vsi: the VSI to be searched
1281  * @macaddr: the MAC address we are searching for
1282  *
1283  * Returns the first filter with the provided MAC address or NULL if
1284  * MAC address was not found
1285  **/
1286 struct i40e_mac_filter *i40e_find_mac(struct i40e_vsi *vsi, const u8 *macaddr)
1287 {
1288 	struct i40e_mac_filter *f;
1289 	u64 key;
1290 
1291 	if (!vsi || !macaddr)
1292 		return NULL;
1293 
1294 	key = i40e_addr_to_hkey(macaddr);
1295 	hash_for_each_possible(vsi->mac_filter_hash, f, hlist, key) {
1296 		if ((ether_addr_equal(macaddr, f->macaddr)))
1297 			return f;
1298 	}
1299 	return NULL;
1300 }
1301 
1302 /**
1303  * i40e_is_vsi_in_vlan - Check if VSI is in vlan mode
1304  * @vsi: the VSI to be searched
1305  *
1306  * Returns true if VSI is in vlan mode or false otherwise
1307  **/
1308 bool i40e_is_vsi_in_vlan(struct i40e_vsi *vsi)
1309 {
1310 	/* If we have a PVID, always operate in VLAN mode */
1311 	if (vsi->info.pvid)
1312 		return true;
1313 
1314 	/* We need to operate in VLAN mode whenever we have any filters with
1315 	 * a VLAN other than I40E_VLAN_ALL. We could check the table each
1316 	 * time, incurring search cost repeatedly. However, we can notice two
1317 	 * things:
1318 	 *
1319 	 * 1) the only place where we can gain a VLAN filter is in
1320 	 *    i40e_add_filter.
1321 	 *
1322 	 * 2) the only place where filters are actually removed is in
1323 	 *    i40e_sync_filters_subtask.
1324 	 *
1325 	 * Thus, we can simply use a boolean value, has_vlan_filters which we
1326 	 * will set to true when we add a VLAN filter in i40e_add_filter. Then
1327 	 * we have to perform the full search after deleting filters in
1328 	 * i40e_sync_filters_subtask, but we already have to search
1329 	 * filters here and can perform the check at the same time. This
1330 	 * results in avoiding embedding a loop for VLAN mode inside another
1331 	 * loop over all the filters, and should maintain correctness as noted
1332 	 * above.
1333 	 */
1334 	return vsi->has_vlan_filter;
1335 }
1336 
1337 /**
1338  * i40e_correct_mac_vlan_filters - Correct non-VLAN filters if necessary
1339  * @vsi: the VSI to configure
1340  * @tmp_add_list: list of filters ready to be added
1341  * @tmp_del_list: list of filters ready to be deleted
1342  * @vlan_filters: the number of active VLAN filters
1343  *
1344  * Update VLAN=0 and VLAN=-1 (I40E_VLAN_ANY) filters properly so that they
1345  * behave as expected. If we have any active VLAN filters remaining or about
1346  * to be added then we need to update non-VLAN filters to be marked as VLAN=0
1347  * so that they only match against untagged traffic. If we no longer have any
1348  * active VLAN filters, we need to make all non-VLAN filters marked as VLAN=-1
1349  * so that they match against both tagged and untagged traffic. In this way,
1350  * we ensure that we correctly receive the desired traffic. This ensures that
1351  * when we have an active VLAN we will receive only untagged traffic and
1352  * traffic matching active VLANs. If we have no active VLANs then we will
1353  * operate in non-VLAN mode and receive all traffic, tagged or untagged.
1354  *
1355  * Finally, in a similar fashion, this function also corrects filters when
1356  * there is an active PVID assigned to this VSI.
1357  *
1358  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1359  *
1360  * This function is only expected to be called from within
1361  * i40e_sync_vsi_filters.
1362  *
1363  * NOTE: This function expects to be called while under the
1364  * mac_filter_hash_lock
1365  */
1366 static int i40e_correct_mac_vlan_filters(struct i40e_vsi *vsi,
1367 					 struct hlist_head *tmp_add_list,
1368 					 struct hlist_head *tmp_del_list,
1369 					 int vlan_filters)
1370 {
1371 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1372 	struct i40e_mac_filter *f, *add_head;
1373 	struct i40e_new_mac_filter *new;
1374 	struct hlist_node *h;
1375 	int bkt, new_vlan;
1376 
1377 	/* To determine if a particular filter needs to be replaced we
1378 	 * have the three following conditions:
1379 	 *
1380 	 * a) if we have a PVID assigned, then all filters which are
1381 	 *    not marked as VLAN=PVID must be replaced with filters that
1382 	 *    are.
1383 	 * b) otherwise, if we have any active VLANS, all filters
1384 	 *    which are marked as VLAN=-1 must be replaced with
1385 	 *    filters marked as VLAN=0
1386 	 * c) finally, if we do not have any active VLANS, all filters
1387 	 *    which are marked as VLAN=0 must be replaced with filters
1388 	 *    marked as VLAN=-1
1389 	 */
1390 
1391 	/* Update the filters about to be added in place */
1392 	hlist_for_each_entry(new, tmp_add_list, hlist) {
1393 		if (pvid && new->f->vlan != pvid)
1394 			new->f->vlan = pvid;
1395 		else if (vlan_filters && new->f->vlan == I40E_VLAN_ANY)
1396 			new->f->vlan = 0;
1397 		else if (!vlan_filters && new->f->vlan == 0)
1398 			new->f->vlan = I40E_VLAN_ANY;
1399 	}
1400 
1401 	/* Update the remaining active filters */
1402 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1403 		/* Combine the checks for whether a filter needs to be changed
1404 		 * and then determine the new VLAN inside the if block, in
1405 		 * order to avoid duplicating code for adding the new filter
1406 		 * then deleting the old filter.
1407 		 */
1408 		if ((pvid && f->vlan != pvid) ||
1409 		    (vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1410 		    (!vlan_filters && f->vlan == 0)) {
1411 			/* Determine the new vlan we will be adding */
1412 			if (pvid)
1413 				new_vlan = pvid;
1414 			else if (vlan_filters)
1415 				new_vlan = 0;
1416 			else
1417 				new_vlan = I40E_VLAN_ANY;
1418 
1419 			/* Create the new filter */
1420 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1421 			if (!add_head)
1422 				return -ENOMEM;
1423 
1424 			/* Create a temporary i40e_new_mac_filter */
1425 			new = kzalloc(sizeof(*new), GFP_ATOMIC);
1426 			if (!new)
1427 				return -ENOMEM;
1428 
1429 			new->f = add_head;
1430 			new->state = add_head->state;
1431 
1432 			/* Add the new filter to the tmp list */
1433 			hlist_add_head(&new->hlist, tmp_add_list);
1434 
1435 			/* Put the original filter into the delete list */
1436 			f->state = I40E_FILTER_REMOVE;
1437 			hash_del(&f->hlist);
1438 			hlist_add_head(&f->hlist, tmp_del_list);
1439 		}
1440 	}
1441 
1442 	vsi->has_vlan_filter = !!vlan_filters;
1443 
1444 	return 0;
1445 }
1446 
1447 /**
1448  * i40e_get_vf_new_vlan - Get new vlan id on a vf
1449  * @vsi: the vsi to configure
1450  * @new_mac: new mac filter to be added
1451  * @f: existing mac filter, replaced with new_mac->f if new_mac is not NULL
1452  * @vlan_filters: the number of active VLAN filters
1453  * @trusted: flag if the VF is trusted
1454  *
1455  * Get new VLAN id based on current VLAN filters, trust, PVID
1456  * and vf-vlan-prune-disable flag.
1457  *
1458  * Returns the value of the new vlan filter or
1459  * the old value if no new filter is needed.
1460  */
1461 static s16 i40e_get_vf_new_vlan(struct i40e_vsi *vsi,
1462 				struct i40e_new_mac_filter *new_mac,
1463 				struct i40e_mac_filter *f,
1464 				int vlan_filters,
1465 				bool trusted)
1466 {
1467 	s16 pvid = le16_to_cpu(vsi->info.pvid);
1468 	struct i40e_pf *pf = vsi->back;
1469 	bool is_any;
1470 
1471 	if (new_mac)
1472 		f = new_mac->f;
1473 
1474 	if (pvid && f->vlan != pvid)
1475 		return pvid;
1476 
1477 	is_any = (trusted ||
1478 		  !(pf->flags & I40E_FLAG_VF_VLAN_PRUNING));
1479 
1480 	if ((vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1481 	    (!is_any && !vlan_filters && f->vlan == I40E_VLAN_ANY) ||
1482 	    (is_any && !vlan_filters && f->vlan == 0)) {
1483 		if (is_any)
1484 			return I40E_VLAN_ANY;
1485 		else
1486 			return 0;
1487 	}
1488 
1489 	return f->vlan;
1490 }
1491 
1492 /**
1493  * i40e_correct_vf_mac_vlan_filters - Correct non-VLAN VF filters if necessary
1494  * @vsi: the vsi to configure
1495  * @tmp_add_list: list of filters ready to be added
1496  * @tmp_del_list: list of filters ready to be deleted
1497  * @vlan_filters: the number of active VLAN filters
1498  * @trusted: flag if the VF is trusted
1499  *
1500  * Correct VF VLAN filters based on current VLAN filters, trust, PVID
1501  * and vf-vlan-prune-disable flag.
1502  *
1503  * In case of memory allocation failure return -ENOMEM. Otherwise, return 0.
1504  *
1505  * This function is only expected to be called from within
1506  * i40e_sync_vsi_filters.
1507  *
1508  * NOTE: This function expects to be called while under the
1509  * mac_filter_hash_lock
1510  */
1511 static int i40e_correct_vf_mac_vlan_filters(struct i40e_vsi *vsi,
1512 					    struct hlist_head *tmp_add_list,
1513 					    struct hlist_head *tmp_del_list,
1514 					    int vlan_filters,
1515 					    bool trusted)
1516 {
1517 	struct i40e_mac_filter *f, *add_head;
1518 	struct i40e_new_mac_filter *new_mac;
1519 	struct hlist_node *h;
1520 	int bkt, new_vlan;
1521 
1522 	hlist_for_each_entry(new_mac, tmp_add_list, hlist) {
1523 		new_mac->f->vlan = i40e_get_vf_new_vlan(vsi, new_mac, NULL,
1524 							vlan_filters, trusted);
1525 	}
1526 
1527 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1528 		new_vlan = i40e_get_vf_new_vlan(vsi, NULL, f, vlan_filters,
1529 						trusted);
1530 		if (new_vlan != f->vlan) {
1531 			add_head = i40e_add_filter(vsi, f->macaddr, new_vlan);
1532 			if (!add_head)
1533 				return -ENOMEM;
1534 			/* Create a temporary i40e_new_mac_filter */
1535 			new_mac = kzalloc(sizeof(*new_mac), GFP_ATOMIC);
1536 			if (!new_mac)
1537 				return -ENOMEM;
1538 			new_mac->f = add_head;
1539 			new_mac->state = add_head->state;
1540 
1541 			/* Add the new filter to the tmp list */
1542 			hlist_add_head(&new_mac->hlist, tmp_add_list);
1543 
1544 			/* Put the original filter into the delete list */
1545 			f->state = I40E_FILTER_REMOVE;
1546 			hash_del(&f->hlist);
1547 			hlist_add_head(&f->hlist, tmp_del_list);
1548 		}
1549 	}
1550 
1551 	vsi->has_vlan_filter = !!vlan_filters;
1552 	return 0;
1553 }
1554 
1555 /**
1556  * i40e_rm_default_mac_filter - Remove the default MAC filter set by NVM
1557  * @vsi: the PF Main VSI - inappropriate for any other VSI
1558  * @macaddr: the MAC address
1559  *
1560  * Remove whatever filter the firmware set up so the driver can manage
1561  * its own filtering intelligently.
1562  **/
1563 static void i40e_rm_default_mac_filter(struct i40e_vsi *vsi, u8 *macaddr)
1564 {
1565 	struct i40e_aqc_remove_macvlan_element_data element;
1566 	struct i40e_pf *pf = vsi->back;
1567 
1568 	/* Only appropriate for the PF main VSI */
1569 	if (vsi->type != I40E_VSI_MAIN)
1570 		return;
1571 
1572 	memset(&element, 0, sizeof(element));
1573 	ether_addr_copy(element.mac_addr, macaddr);
1574 	element.vlan_tag = 0;
1575 	/* Ignore error returns, some firmware does it this way... */
1576 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
1577 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1578 
1579 	memset(&element, 0, sizeof(element));
1580 	ether_addr_copy(element.mac_addr, macaddr);
1581 	element.vlan_tag = 0;
1582 	/* ...and some firmware does it this way. */
1583 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH |
1584 			I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
1585 	i40e_aq_remove_macvlan(&pf->hw, vsi->seid, &element, 1, NULL);
1586 }
1587 
1588 /**
1589  * i40e_add_filter - Add a mac/vlan filter to the VSI
1590  * @vsi: the VSI to be searched
1591  * @macaddr: the MAC address
1592  * @vlan: the vlan
1593  *
1594  * Returns ptr to the filter object or NULL when no memory available.
1595  *
1596  * NOTE: This function is expected to be called with mac_filter_hash_lock
1597  * being held.
1598  **/
1599 struct i40e_mac_filter *i40e_add_filter(struct i40e_vsi *vsi,
1600 					const u8 *macaddr, s16 vlan)
1601 {
1602 	struct i40e_mac_filter *f;
1603 	u64 key;
1604 
1605 	if (!vsi || !macaddr)
1606 		return NULL;
1607 
1608 	f = i40e_find_filter(vsi, macaddr, vlan);
1609 	if (!f) {
1610 		f = kzalloc(sizeof(*f), GFP_ATOMIC);
1611 		if (!f)
1612 			return NULL;
1613 
1614 		/* Update the boolean indicating if we need to function in
1615 		 * VLAN mode.
1616 		 */
1617 		if (vlan >= 0)
1618 			vsi->has_vlan_filter = true;
1619 
1620 		ether_addr_copy(f->macaddr, macaddr);
1621 		f->vlan = vlan;
1622 		f->state = I40E_FILTER_NEW;
1623 		INIT_HLIST_NODE(&f->hlist);
1624 
1625 		key = i40e_addr_to_hkey(macaddr);
1626 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
1627 
1628 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1629 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1630 	}
1631 
1632 	/* If we're asked to add a filter that has been marked for removal, it
1633 	 * is safe to simply restore it to active state. __i40e_del_filter
1634 	 * will have simply deleted any filters which were previously marked
1635 	 * NEW or FAILED, so if it is currently marked REMOVE it must have
1636 	 * previously been ACTIVE. Since we haven't yet run the sync filters
1637 	 * task, just restore this filter to the ACTIVE state so that the
1638 	 * sync task leaves it in place
1639 	 */
1640 	if (f->state == I40E_FILTER_REMOVE)
1641 		f->state = I40E_FILTER_ACTIVE;
1642 
1643 	return f;
1644 }
1645 
1646 /**
1647  * __i40e_del_filter - Remove a specific filter from the VSI
1648  * @vsi: VSI to remove from
1649  * @f: the filter to remove from the list
1650  *
1651  * This function should be called instead of i40e_del_filter only if you know
1652  * the exact filter you will remove already, such as via i40e_find_filter or
1653  * i40e_find_mac.
1654  *
1655  * NOTE: This function is expected to be called with mac_filter_hash_lock
1656  * being held.
1657  * ANOTHER NOTE: This function MUST be called from within the context of
1658  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1659  * instead of list_for_each_entry().
1660  **/
1661 void __i40e_del_filter(struct i40e_vsi *vsi, struct i40e_mac_filter *f)
1662 {
1663 	if (!f)
1664 		return;
1665 
1666 	/* If the filter was never added to firmware then we can just delete it
1667 	 * directly and we don't want to set the status to remove or else an
1668 	 * admin queue command will unnecessarily fire.
1669 	 */
1670 	if ((f->state == I40E_FILTER_FAILED) ||
1671 	    (f->state == I40E_FILTER_NEW)) {
1672 		hash_del(&f->hlist);
1673 		kfree(f);
1674 	} else {
1675 		f->state = I40E_FILTER_REMOVE;
1676 	}
1677 
1678 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
1679 	set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
1680 }
1681 
1682 /**
1683  * i40e_del_filter - Remove a MAC/VLAN filter from the VSI
1684  * @vsi: the VSI to be searched
1685  * @macaddr: the MAC address
1686  * @vlan: the VLAN
1687  *
1688  * NOTE: This function is expected to be called with mac_filter_hash_lock
1689  * being held.
1690  * ANOTHER NOTE: This function MUST be called from within the context of
1691  * the "safe" variants of any list iterators, e.g. list_for_each_entry_safe()
1692  * instead of list_for_each_entry().
1693  **/
1694 void i40e_del_filter(struct i40e_vsi *vsi, const u8 *macaddr, s16 vlan)
1695 {
1696 	struct i40e_mac_filter *f;
1697 
1698 	if (!vsi || !macaddr)
1699 		return;
1700 
1701 	f = i40e_find_filter(vsi, macaddr, vlan);
1702 	__i40e_del_filter(vsi, f);
1703 }
1704 
1705 /**
1706  * i40e_add_mac_filter - Add a MAC filter for all active VLANs
1707  * @vsi: the VSI to be searched
1708  * @macaddr: the mac address to be filtered
1709  *
1710  * If we're not in VLAN mode, just add the filter to I40E_VLAN_ANY. Otherwise,
1711  * go through all the macvlan filters and add a macvlan filter for each
1712  * unique vlan that already exists. If a PVID has been assigned, instead only
1713  * add the macaddr to that VLAN.
1714  *
1715  * Returns last filter added on success, else NULL
1716  **/
1717 struct i40e_mac_filter *i40e_add_mac_filter(struct i40e_vsi *vsi,
1718 					    const u8 *macaddr)
1719 {
1720 	struct i40e_mac_filter *f, *add = NULL;
1721 	struct hlist_node *h;
1722 	int bkt;
1723 
1724 	if (vsi->info.pvid)
1725 		return i40e_add_filter(vsi, macaddr,
1726 				       le16_to_cpu(vsi->info.pvid));
1727 
1728 	if (!i40e_is_vsi_in_vlan(vsi))
1729 		return i40e_add_filter(vsi, macaddr, I40E_VLAN_ANY);
1730 
1731 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1732 		if (f->state == I40E_FILTER_REMOVE)
1733 			continue;
1734 		add = i40e_add_filter(vsi, macaddr, f->vlan);
1735 		if (!add)
1736 			return NULL;
1737 	}
1738 
1739 	return add;
1740 }
1741 
1742 /**
1743  * i40e_del_mac_filter - Remove a MAC filter from all VLANs
1744  * @vsi: the VSI to be searched
1745  * @macaddr: the mac address to be removed
1746  *
1747  * Removes a given MAC address from a VSI regardless of what VLAN it has been
1748  * associated with.
1749  *
1750  * Returns 0 for success, or error
1751  **/
1752 int i40e_del_mac_filter(struct i40e_vsi *vsi, const u8 *macaddr)
1753 {
1754 	struct i40e_mac_filter *f;
1755 	struct hlist_node *h;
1756 	bool found = false;
1757 	int bkt;
1758 
1759 	lockdep_assert_held(&vsi->mac_filter_hash_lock);
1760 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
1761 		if (ether_addr_equal(macaddr, f->macaddr)) {
1762 			__i40e_del_filter(vsi, f);
1763 			found = true;
1764 		}
1765 	}
1766 
1767 	if (found)
1768 		return 0;
1769 	else
1770 		return -ENOENT;
1771 }
1772 
1773 /**
1774  * i40e_set_mac - NDO callback to set mac address
1775  * @netdev: network interface device structure
1776  * @p: pointer to an address structure
1777  *
1778  * Returns 0 on success, negative on failure
1779  **/
1780 static int i40e_set_mac(struct net_device *netdev, void *p)
1781 {
1782 	struct i40e_netdev_priv *np = netdev_priv(netdev);
1783 	struct i40e_vsi *vsi = np->vsi;
1784 	struct i40e_pf *pf = vsi->back;
1785 	struct i40e_hw *hw = &pf->hw;
1786 	struct sockaddr *addr = p;
1787 
1788 	if (!is_valid_ether_addr(addr->sa_data))
1789 		return -EADDRNOTAVAIL;
1790 
1791 	if (test_bit(__I40E_DOWN, pf->state) ||
1792 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1793 		return -EADDRNOTAVAIL;
1794 
1795 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1796 		netdev_info(netdev, "returning to hw mac address %pM\n",
1797 			    hw->mac.addr);
1798 	else
1799 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1800 
1801 	/* Copy the address first, so that we avoid a possible race with
1802 	 * .set_rx_mode().
1803 	 * - Remove old address from MAC filter
1804 	 * - Copy new address
1805 	 * - Add new address to MAC filter
1806 	 */
1807 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1808 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1809 	eth_hw_addr_set(netdev, addr->sa_data);
1810 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1811 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1812 
1813 	if (vsi->type == I40E_VSI_MAIN) {
1814 		int ret;
1815 
1816 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1817 						addr->sa_data, NULL);
1818 		if (ret)
1819 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %pe, AQ ret %s\n",
1820 				    ERR_PTR(ret),
1821 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1822 	}
1823 
1824 	/* schedule our worker thread which will take care of
1825 	 * applying the new filter changes
1826 	 */
1827 	i40e_service_event_schedule(pf);
1828 	return 0;
1829 }
1830 
1831 /**
1832  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1833  * @vsi: vsi structure
1834  * @seed: RSS hash seed
1835  * @lut: pointer to lookup table of lut_size
1836  * @lut_size: size of the lookup table
1837  **/
1838 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1839 			      u8 *lut, u16 lut_size)
1840 {
1841 	struct i40e_pf *pf = vsi->back;
1842 	struct i40e_hw *hw = &pf->hw;
1843 	int ret = 0;
1844 
1845 	if (seed) {
1846 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1847 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1848 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1849 		if (ret) {
1850 			dev_info(&pf->pdev->dev,
1851 				 "Cannot set RSS key, err %pe aq_err %s\n",
1852 				 ERR_PTR(ret),
1853 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1854 			return ret;
1855 		}
1856 	}
1857 	if (lut) {
1858 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1859 
1860 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1861 		if (ret) {
1862 			dev_info(&pf->pdev->dev,
1863 				 "Cannot set RSS lut, err %pe aq_err %s\n",
1864 				 ERR_PTR(ret),
1865 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1866 			return ret;
1867 		}
1868 	}
1869 	return ret;
1870 }
1871 
1872 /**
1873  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1874  * @vsi: VSI structure
1875  **/
1876 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1877 {
1878 	struct i40e_pf *pf = vsi->back;
1879 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1880 	u8 *lut;
1881 	int ret;
1882 
1883 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1884 		return 0;
1885 	if (!vsi->rss_size)
1886 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1887 				      vsi->num_queue_pairs);
1888 	if (!vsi->rss_size)
1889 		return -EINVAL;
1890 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1891 	if (!lut)
1892 		return -ENOMEM;
1893 
1894 	/* Use the user configured hash keys and lookup table if there is one,
1895 	 * otherwise use default
1896 	 */
1897 	if (vsi->rss_lut_user)
1898 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1899 	else
1900 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1901 	if (vsi->rss_hkey_user)
1902 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1903 	else
1904 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1905 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1906 	kfree(lut);
1907 	return ret;
1908 }
1909 
1910 /**
1911  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1912  * @vsi: the VSI being configured,
1913  * @ctxt: VSI context structure
1914  * @enabled_tc: number of traffic classes to enable
1915  *
1916  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1917  **/
1918 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1919 					   struct i40e_vsi_context *ctxt,
1920 					   u8 enabled_tc)
1921 {
1922 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1923 	int i, override_q, pow, num_qps, ret;
1924 	u8 netdev_tc = 0, offset = 0;
1925 
1926 	if (vsi->type != I40E_VSI_MAIN)
1927 		return -EINVAL;
1928 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1929 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1930 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1931 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1932 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1933 
1934 	/* find the next higher power-of-2 of num queue pairs */
1935 	pow = ilog2(num_qps);
1936 	if (!is_power_of_2(num_qps))
1937 		pow++;
1938 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1939 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1940 
1941 	/* Setup queue offset/count for all TCs for given VSI */
1942 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1943 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1944 		/* See if the given TC is enabled for the given VSI */
1945 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1946 			offset = vsi->mqprio_qopt.qopt.offset[i];
1947 			qcount = vsi->mqprio_qopt.qopt.count[i];
1948 			if (qcount > max_qcount)
1949 				max_qcount = qcount;
1950 			vsi->tc_config.tc_info[i].qoffset = offset;
1951 			vsi->tc_config.tc_info[i].qcount = qcount;
1952 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1953 		} else {
1954 			/* TC is not enabled so set the offset to
1955 			 * default queue and allocate one queue
1956 			 * for the given TC.
1957 			 */
1958 			vsi->tc_config.tc_info[i].qoffset = 0;
1959 			vsi->tc_config.tc_info[i].qcount = 1;
1960 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1961 		}
1962 	}
1963 
1964 	/* Set actual Tx/Rx queue pairs */
1965 	vsi->num_queue_pairs = offset + qcount;
1966 
1967 	/* Setup queue TC[0].qmap for given VSI context */
1968 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1969 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1970 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1971 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1972 
1973 	/* Reconfigure RSS for main VSI with max queue count */
1974 	vsi->rss_size = max_qcount;
1975 	ret = i40e_vsi_config_rss(vsi);
1976 	if (ret) {
1977 		dev_info(&vsi->back->pdev->dev,
1978 			 "Failed to reconfig rss for num_queues (%u)\n",
1979 			 max_qcount);
1980 		return ret;
1981 	}
1982 	vsi->reconfig_rss = true;
1983 	dev_dbg(&vsi->back->pdev->dev,
1984 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1985 
1986 	/* Find queue count available for channel VSIs and starting offset
1987 	 * for channel VSIs
1988 	 */
1989 	override_q = vsi->mqprio_qopt.qopt.count[0];
1990 	if (override_q && override_q < vsi->num_queue_pairs) {
1991 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1992 		vsi->next_base_queue = override_q;
1993 	}
1994 	return 0;
1995 }
1996 
1997 /**
1998  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
1999  * @vsi: the VSI being setup
2000  * @ctxt: VSI context structure
2001  * @enabled_tc: Enabled TCs bitmap
2002  * @is_add: True if called before Add VSI
2003  *
2004  * Setup VSI queue mapping for enabled traffic classes.
2005  **/
2006 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2007 				     struct i40e_vsi_context *ctxt,
2008 				     u8 enabled_tc,
2009 				     bool is_add)
2010 {
2011 	struct i40e_pf *pf = vsi->back;
2012 	u16 num_tc_qps = 0;
2013 	u16 sections = 0;
2014 	u8 netdev_tc = 0;
2015 	u16 numtc = 1;
2016 	u16 qcount;
2017 	u8 offset;
2018 	u16 qmap;
2019 	int i;
2020 
2021 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2022 	offset = 0;
2023 	/* zero out queue mapping, it will get updated on the end of the function */
2024 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2025 
2026 	if (vsi->type == I40E_VSI_MAIN) {
2027 		/* This code helps add more queue to the VSI if we have
2028 		 * more cores than RSS can support, the higher cores will
2029 		 * be served by ATR or other filters. Furthermore, the
2030 		 * non-zero req_queue_pairs says that user requested a new
2031 		 * queue count via ethtool's set_channels, so use this
2032 		 * value for queues distribution across traffic classes
2033 		 * We need at least one queue pair for the interface
2034 		 * to be usable as we see in else statement.
2035 		 */
2036 		if (vsi->req_queue_pairs > 0)
2037 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2038 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2039 			vsi->num_queue_pairs = pf->num_lan_msix;
2040 		else
2041 			vsi->num_queue_pairs = 1;
2042 	}
2043 
2044 	/* Number of queues per enabled TC */
2045 	if (vsi->type == I40E_VSI_MAIN ||
2046 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2047 		num_tc_qps = vsi->num_queue_pairs;
2048 	else
2049 		num_tc_qps = vsi->alloc_queue_pairs;
2050 
2051 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2052 		/* Find numtc from enabled TC bitmap */
2053 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2054 			if (enabled_tc & BIT(i)) /* TC is enabled */
2055 				numtc++;
2056 		}
2057 		if (!numtc) {
2058 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2059 			numtc = 1;
2060 		}
2061 		num_tc_qps = num_tc_qps / numtc;
2062 		num_tc_qps = min_t(int, num_tc_qps,
2063 				   i40e_pf_get_max_q_per_tc(pf));
2064 	}
2065 
2066 	vsi->tc_config.numtc = numtc;
2067 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2068 
2069 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2070 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2071 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2072 
2073 	/* Setup queue offset/count for all TCs for given VSI */
2074 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2075 		/* See if the given TC is enabled for the given VSI */
2076 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2077 			/* TC is enabled */
2078 			int pow, num_qps;
2079 
2080 			switch (vsi->type) {
2081 			case I40E_VSI_MAIN:
2082 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2083 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2084 				    vsi->tc_config.enabled_tc != 1) {
2085 					qcount = min_t(int, pf->alloc_rss_size,
2086 						       num_tc_qps);
2087 					break;
2088 				}
2089 				fallthrough;
2090 			case I40E_VSI_FDIR:
2091 			case I40E_VSI_SRIOV:
2092 			case I40E_VSI_VMDQ2:
2093 			default:
2094 				qcount = num_tc_qps;
2095 				WARN_ON(i != 0);
2096 				break;
2097 			}
2098 			vsi->tc_config.tc_info[i].qoffset = offset;
2099 			vsi->tc_config.tc_info[i].qcount = qcount;
2100 
2101 			/* find the next higher power-of-2 of num queue pairs */
2102 			num_qps = qcount;
2103 			pow = 0;
2104 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2105 				pow++;
2106 				num_qps >>= 1;
2107 			}
2108 
2109 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2110 			qmap =
2111 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2112 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2113 
2114 			offset += qcount;
2115 		} else {
2116 			/* TC is not enabled so set the offset to
2117 			 * default queue and allocate one queue
2118 			 * for the given TC.
2119 			 */
2120 			vsi->tc_config.tc_info[i].qoffset = 0;
2121 			vsi->tc_config.tc_info[i].qcount = 1;
2122 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2123 
2124 			qmap = 0;
2125 		}
2126 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2127 	}
2128 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2129 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2130 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2131 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2132 		vsi->num_queue_pairs = offset;
2133 
2134 	/* Scheduler section valid can only be set for ADD VSI */
2135 	if (is_add) {
2136 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2137 
2138 		ctxt->info.up_enable_bits = enabled_tc;
2139 	}
2140 	if (vsi->type == I40E_VSI_SRIOV) {
2141 		ctxt->info.mapping_flags |=
2142 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2143 		for (i = 0; i < vsi->num_queue_pairs; i++)
2144 			ctxt->info.queue_mapping[i] =
2145 					       cpu_to_le16(vsi->base_queue + i);
2146 	} else {
2147 		ctxt->info.mapping_flags |=
2148 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2149 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2150 	}
2151 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2152 }
2153 
2154 /**
2155  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2156  * @netdev: the netdevice
2157  * @addr: address to add
2158  *
2159  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2160  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2161  */
2162 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2163 {
2164 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2165 	struct i40e_vsi *vsi = np->vsi;
2166 
2167 	if (i40e_add_mac_filter(vsi, addr))
2168 		return 0;
2169 	else
2170 		return -ENOMEM;
2171 }
2172 
2173 /**
2174  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2175  * @netdev: the netdevice
2176  * @addr: address to add
2177  *
2178  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2179  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2180  */
2181 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2182 {
2183 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2184 	struct i40e_vsi *vsi = np->vsi;
2185 
2186 	/* Under some circumstances, we might receive a request to delete
2187 	 * our own device address from our uc list. Because we store the
2188 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2189 	 * such requests and not delete our device address from this list.
2190 	 */
2191 	if (ether_addr_equal(addr, netdev->dev_addr))
2192 		return 0;
2193 
2194 	i40e_del_mac_filter(vsi, addr);
2195 
2196 	return 0;
2197 }
2198 
2199 /**
2200  * i40e_set_rx_mode - NDO callback to set the netdev filters
2201  * @netdev: network interface device structure
2202  **/
2203 static void i40e_set_rx_mode(struct net_device *netdev)
2204 {
2205 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2206 	struct i40e_vsi *vsi = np->vsi;
2207 
2208 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2209 
2210 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2211 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2212 
2213 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2214 
2215 	/* check for other flag changes */
2216 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2217 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2218 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2219 	}
2220 }
2221 
2222 /**
2223  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2224  * @vsi: Pointer to VSI struct
2225  * @from: Pointer to list which contains MAC filter entries - changes to
2226  *        those entries needs to be undone.
2227  *
2228  * MAC filter entries from this list were slated for deletion.
2229  **/
2230 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2231 					 struct hlist_head *from)
2232 {
2233 	struct i40e_mac_filter *f;
2234 	struct hlist_node *h;
2235 
2236 	hlist_for_each_entry_safe(f, h, from, hlist) {
2237 		u64 key = i40e_addr_to_hkey(f->macaddr);
2238 
2239 		/* Move the element back into MAC filter list*/
2240 		hlist_del(&f->hlist);
2241 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2242 	}
2243 }
2244 
2245 /**
2246  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2247  * @vsi: Pointer to vsi struct
2248  * @from: Pointer to list which contains MAC filter entries - changes to
2249  *        those entries needs to be undone.
2250  *
2251  * MAC filter entries from this list were slated for addition.
2252  **/
2253 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2254 					 struct hlist_head *from)
2255 {
2256 	struct i40e_new_mac_filter *new;
2257 	struct hlist_node *h;
2258 
2259 	hlist_for_each_entry_safe(new, h, from, hlist) {
2260 		/* We can simply free the wrapper structure */
2261 		hlist_del(&new->hlist);
2262 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2263 		kfree(new);
2264 	}
2265 }
2266 
2267 /**
2268  * i40e_next_filter - Get the next non-broadcast filter from a list
2269  * @next: pointer to filter in list
2270  *
2271  * Returns the next non-broadcast filter in the list. Required so that we
2272  * ignore broadcast filters within the list, since these are not handled via
2273  * the normal firmware update path.
2274  */
2275 static
2276 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2277 {
2278 	hlist_for_each_entry_continue(next, hlist) {
2279 		if (!is_broadcast_ether_addr(next->f->macaddr))
2280 			return next;
2281 	}
2282 
2283 	return NULL;
2284 }
2285 
2286 /**
2287  * i40e_update_filter_state - Update filter state based on return data
2288  * from firmware
2289  * @count: Number of filters added
2290  * @add_list: return data from fw
2291  * @add_head: pointer to first filter in current batch
2292  *
2293  * MAC filter entries from list were slated to be added to device. Returns
2294  * number of successful filters. Note that 0 does NOT mean success!
2295  **/
2296 static int
2297 i40e_update_filter_state(int count,
2298 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2299 			 struct i40e_new_mac_filter *add_head)
2300 {
2301 	int retval = 0;
2302 	int i;
2303 
2304 	for (i = 0; i < count; i++) {
2305 		/* Always check status of each filter. We don't need to check
2306 		 * the firmware return status because we pre-set the filter
2307 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2308 		 * request to the adminq. Thus, if it no longer matches then
2309 		 * we know the filter is active.
2310 		 */
2311 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2312 			add_head->state = I40E_FILTER_FAILED;
2313 		} else {
2314 			add_head->state = I40E_FILTER_ACTIVE;
2315 			retval++;
2316 		}
2317 
2318 		add_head = i40e_next_filter(add_head);
2319 		if (!add_head)
2320 			break;
2321 	}
2322 
2323 	return retval;
2324 }
2325 
2326 /**
2327  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2328  * @vsi: ptr to the VSI
2329  * @vsi_name: name to display in messages
2330  * @list: the list of filters to send to firmware
2331  * @num_del: the number of filters to delete
2332  * @retval: Set to -EIO on failure to delete
2333  *
2334  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2335  * *retval instead of a return value so that success does not force ret_val to
2336  * be set to 0. This ensures that a sequence of calls to this function
2337  * preserve the previous value of *retval on successful delete.
2338  */
2339 static
2340 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2341 			  struct i40e_aqc_remove_macvlan_element_data *list,
2342 			  int num_del, int *retval)
2343 {
2344 	struct i40e_hw *hw = &vsi->back->hw;
2345 	enum i40e_admin_queue_err aq_status;
2346 	int aq_ret;
2347 
2348 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2349 					   &aq_status);
2350 
2351 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2352 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2353 		*retval = -EIO;
2354 		dev_info(&vsi->back->pdev->dev,
2355 			 "ignoring delete macvlan error on %s, err %pe, aq_err %s\n",
2356 			 vsi_name, ERR_PTR(aq_ret),
2357 			 i40e_aq_str(hw, aq_status));
2358 	}
2359 }
2360 
2361 /**
2362  * i40e_aqc_add_filters - Request firmware to add a set of filters
2363  * @vsi: ptr to the VSI
2364  * @vsi_name: name to display in messages
2365  * @list: the list of filters to send to firmware
2366  * @add_head: Position in the add hlist
2367  * @num_add: the number of filters to add
2368  *
2369  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2370  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2371  * space for more filters.
2372  */
2373 static
2374 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2375 			  struct i40e_aqc_add_macvlan_element_data *list,
2376 			  struct i40e_new_mac_filter *add_head,
2377 			  int num_add)
2378 {
2379 	struct i40e_hw *hw = &vsi->back->hw;
2380 	enum i40e_admin_queue_err aq_status;
2381 	int fcnt;
2382 
2383 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2384 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2385 
2386 	if (fcnt != num_add) {
2387 		if (vsi->type == I40E_VSI_MAIN) {
2388 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2389 			dev_warn(&vsi->back->pdev->dev,
2390 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2391 				 i40e_aq_str(hw, aq_status), vsi_name);
2392 		} else if (vsi->type == I40E_VSI_SRIOV ||
2393 			   vsi->type == I40E_VSI_VMDQ1 ||
2394 			   vsi->type == I40E_VSI_VMDQ2) {
2395 			dev_warn(&vsi->back->pdev->dev,
2396 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2397 				 i40e_aq_str(hw, aq_status), vsi_name,
2398 					     vsi_name);
2399 		} else {
2400 			dev_warn(&vsi->back->pdev->dev,
2401 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2402 				 i40e_aq_str(hw, aq_status), vsi_name,
2403 					     vsi->type);
2404 		}
2405 	}
2406 }
2407 
2408 /**
2409  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2410  * @vsi: pointer to the VSI
2411  * @vsi_name: the VSI name
2412  * @f: filter data
2413  *
2414  * This function sets or clears the promiscuous broadcast flags for VLAN
2415  * filters in order to properly receive broadcast frames. Assumes that only
2416  * broadcast filters are passed.
2417  *
2418  * Returns status indicating success or failure;
2419  **/
2420 static int
2421 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2422 			  struct i40e_mac_filter *f)
2423 {
2424 	bool enable = f->state == I40E_FILTER_NEW;
2425 	struct i40e_hw *hw = &vsi->back->hw;
2426 	int aq_ret;
2427 
2428 	if (f->vlan == I40E_VLAN_ANY) {
2429 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2430 						   vsi->seid,
2431 						   enable,
2432 						   NULL);
2433 	} else {
2434 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2435 							    vsi->seid,
2436 							    enable,
2437 							    f->vlan,
2438 							    NULL);
2439 	}
2440 
2441 	if (aq_ret) {
2442 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2443 		dev_warn(&vsi->back->pdev->dev,
2444 			 "Error %s, forcing overflow promiscuous on %s\n",
2445 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2446 			 vsi_name);
2447 	}
2448 
2449 	return aq_ret;
2450 }
2451 
2452 /**
2453  * i40e_set_promiscuous - set promiscuous mode
2454  * @pf: board private structure
2455  * @promisc: promisc on or off
2456  *
2457  * There are different ways of setting promiscuous mode on a PF depending on
2458  * what state/environment we're in.  This identifies and sets it appropriately.
2459  * Returns 0 on success.
2460  **/
2461 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2462 {
2463 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2464 	struct i40e_hw *hw = &pf->hw;
2465 	int aq_ret;
2466 
2467 	if (vsi->type == I40E_VSI_MAIN &&
2468 	    pf->lan_veb != I40E_NO_VEB &&
2469 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2470 		/* set defport ON for Main VSI instead of true promisc
2471 		 * this way we will get all unicast/multicast and VLAN
2472 		 * promisc behavior but will not get VF or VMDq traffic
2473 		 * replicated on the Main VSI.
2474 		 */
2475 		if (promisc)
2476 			aq_ret = i40e_aq_set_default_vsi(hw,
2477 							 vsi->seid,
2478 							 NULL);
2479 		else
2480 			aq_ret = i40e_aq_clear_default_vsi(hw,
2481 							   vsi->seid,
2482 							   NULL);
2483 		if (aq_ret) {
2484 			dev_info(&pf->pdev->dev,
2485 				 "Set default VSI failed, err %pe, aq_err %s\n",
2486 				 ERR_PTR(aq_ret),
2487 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2488 		}
2489 	} else {
2490 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2491 						  hw,
2492 						  vsi->seid,
2493 						  promisc, NULL,
2494 						  true);
2495 		if (aq_ret) {
2496 			dev_info(&pf->pdev->dev,
2497 				 "set unicast promisc failed, err %pe, aq_err %s\n",
2498 				 ERR_PTR(aq_ret),
2499 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2500 		}
2501 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2502 						  hw,
2503 						  vsi->seid,
2504 						  promisc, NULL);
2505 		if (aq_ret) {
2506 			dev_info(&pf->pdev->dev,
2507 				 "set multicast promisc failed, err %pe, aq_err %s\n",
2508 				 ERR_PTR(aq_ret),
2509 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2510 		}
2511 	}
2512 
2513 	if (!aq_ret)
2514 		pf->cur_promisc = promisc;
2515 
2516 	return aq_ret;
2517 }
2518 
2519 /**
2520  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2521  * @vsi: ptr to the VSI
2522  *
2523  * Push any outstanding VSI filter changes through the AdminQ.
2524  *
2525  * Returns 0 or error value
2526  **/
2527 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2528 {
2529 	struct hlist_head tmp_add_list, tmp_del_list;
2530 	struct i40e_mac_filter *f;
2531 	struct i40e_new_mac_filter *new, *add_head = NULL;
2532 	struct i40e_hw *hw = &vsi->back->hw;
2533 	bool old_overflow, new_overflow;
2534 	unsigned int failed_filters = 0;
2535 	unsigned int vlan_filters = 0;
2536 	char vsi_name[16] = "PF";
2537 	int filter_list_len = 0;
2538 	u32 changed_flags = 0;
2539 	struct hlist_node *h;
2540 	struct i40e_pf *pf;
2541 	int num_add = 0;
2542 	int num_del = 0;
2543 	int aq_ret = 0;
2544 	int retval = 0;
2545 	u16 cmd_flags;
2546 	int list_size;
2547 	int bkt;
2548 
2549 	/* empty array typed pointers, kcalloc later */
2550 	struct i40e_aqc_add_macvlan_element_data *add_list;
2551 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2552 
2553 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2554 		usleep_range(1000, 2000);
2555 	pf = vsi->back;
2556 
2557 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2558 
2559 	if (vsi->netdev) {
2560 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2561 		vsi->current_netdev_flags = vsi->netdev->flags;
2562 	}
2563 
2564 	INIT_HLIST_HEAD(&tmp_add_list);
2565 	INIT_HLIST_HEAD(&tmp_del_list);
2566 
2567 	if (vsi->type == I40E_VSI_SRIOV)
2568 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2569 	else if (vsi->type != I40E_VSI_MAIN)
2570 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2571 
2572 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2573 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2574 
2575 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2576 		/* Create a list of filters to delete. */
2577 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2578 			if (f->state == I40E_FILTER_REMOVE) {
2579 				/* Move the element into temporary del_list */
2580 				hash_del(&f->hlist);
2581 				hlist_add_head(&f->hlist, &tmp_del_list);
2582 
2583 				/* Avoid counting removed filters */
2584 				continue;
2585 			}
2586 			if (f->state == I40E_FILTER_NEW) {
2587 				/* Create a temporary i40e_new_mac_filter */
2588 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2589 				if (!new)
2590 					goto err_no_memory_locked;
2591 
2592 				/* Store pointer to the real filter */
2593 				new->f = f;
2594 				new->state = f->state;
2595 
2596 				/* Add it to the hash list */
2597 				hlist_add_head(&new->hlist, &tmp_add_list);
2598 			}
2599 
2600 			/* Count the number of active (current and new) VLAN
2601 			 * filters we have now. Does not count filters which
2602 			 * are marked for deletion.
2603 			 */
2604 			if (f->vlan > 0)
2605 				vlan_filters++;
2606 		}
2607 
2608 		if (vsi->type != I40E_VSI_SRIOV)
2609 			retval = i40e_correct_mac_vlan_filters
2610 				(vsi, &tmp_add_list, &tmp_del_list,
2611 				 vlan_filters);
2612 		else
2613 			retval = i40e_correct_vf_mac_vlan_filters
2614 				(vsi, &tmp_add_list, &tmp_del_list,
2615 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2616 
2617 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2618 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2619 
2620 		if (retval)
2621 			goto err_no_memory_locked;
2622 
2623 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2624 	}
2625 
2626 	/* Now process 'del_list' outside the lock */
2627 	if (!hlist_empty(&tmp_del_list)) {
2628 		filter_list_len = hw->aq.asq_buf_size /
2629 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2630 		list_size = filter_list_len *
2631 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2632 		del_list = kzalloc(list_size, GFP_ATOMIC);
2633 		if (!del_list)
2634 			goto err_no_memory;
2635 
2636 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2637 			cmd_flags = 0;
2638 
2639 			/* handle broadcast filters by updating the broadcast
2640 			 * promiscuous flag and release filter list.
2641 			 */
2642 			if (is_broadcast_ether_addr(f->macaddr)) {
2643 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2644 
2645 				hlist_del(&f->hlist);
2646 				kfree(f);
2647 				continue;
2648 			}
2649 
2650 			/* add to delete list */
2651 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2652 			if (f->vlan == I40E_VLAN_ANY) {
2653 				del_list[num_del].vlan_tag = 0;
2654 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2655 			} else {
2656 				del_list[num_del].vlan_tag =
2657 					cpu_to_le16((u16)(f->vlan));
2658 			}
2659 
2660 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2661 			del_list[num_del].flags = cmd_flags;
2662 			num_del++;
2663 
2664 			/* flush a full buffer */
2665 			if (num_del == filter_list_len) {
2666 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2667 						     num_del, &retval);
2668 				memset(del_list, 0, list_size);
2669 				num_del = 0;
2670 			}
2671 			/* Release memory for MAC filter entries which were
2672 			 * synced up with HW.
2673 			 */
2674 			hlist_del(&f->hlist);
2675 			kfree(f);
2676 		}
2677 
2678 		if (num_del) {
2679 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2680 					     num_del, &retval);
2681 		}
2682 
2683 		kfree(del_list);
2684 		del_list = NULL;
2685 	}
2686 
2687 	if (!hlist_empty(&tmp_add_list)) {
2688 		/* Do all the adds now. */
2689 		filter_list_len = hw->aq.asq_buf_size /
2690 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2691 		list_size = filter_list_len *
2692 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2693 		add_list = kzalloc(list_size, GFP_ATOMIC);
2694 		if (!add_list)
2695 			goto err_no_memory;
2696 
2697 		num_add = 0;
2698 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2699 			/* handle broadcast filters by updating the broadcast
2700 			 * promiscuous flag instead of adding a MAC filter.
2701 			 */
2702 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2703 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2704 							      new->f))
2705 					new->state = I40E_FILTER_FAILED;
2706 				else
2707 					new->state = I40E_FILTER_ACTIVE;
2708 				continue;
2709 			}
2710 
2711 			/* add to add array */
2712 			if (num_add == 0)
2713 				add_head = new;
2714 			cmd_flags = 0;
2715 			ether_addr_copy(add_list[num_add].mac_addr,
2716 					new->f->macaddr);
2717 			if (new->f->vlan == I40E_VLAN_ANY) {
2718 				add_list[num_add].vlan_tag = 0;
2719 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2720 			} else {
2721 				add_list[num_add].vlan_tag =
2722 					cpu_to_le16((u16)(new->f->vlan));
2723 			}
2724 			add_list[num_add].queue_number = 0;
2725 			/* set invalid match method for later detection */
2726 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2727 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2728 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2729 			num_add++;
2730 
2731 			/* flush a full buffer */
2732 			if (num_add == filter_list_len) {
2733 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2734 						     add_head, num_add);
2735 				memset(add_list, 0, list_size);
2736 				num_add = 0;
2737 			}
2738 		}
2739 		if (num_add) {
2740 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2741 					     num_add);
2742 		}
2743 		/* Now move all of the filters from the temp add list back to
2744 		 * the VSI's list.
2745 		 */
2746 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2747 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2748 			/* Only update the state if we're still NEW */
2749 			if (new->f->state == I40E_FILTER_NEW)
2750 				new->f->state = new->state;
2751 			hlist_del(&new->hlist);
2752 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2753 			kfree(new);
2754 		}
2755 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2756 		kfree(add_list);
2757 		add_list = NULL;
2758 	}
2759 
2760 	/* Determine the number of active and failed filters. */
2761 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2762 	vsi->active_filters = 0;
2763 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2764 		if (f->state == I40E_FILTER_ACTIVE)
2765 			vsi->active_filters++;
2766 		else if (f->state == I40E_FILTER_FAILED)
2767 			failed_filters++;
2768 	}
2769 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2770 
2771 	/* Check if we are able to exit overflow promiscuous mode. We can
2772 	 * safely exit if we didn't just enter, we no longer have any failed
2773 	 * filters, and we have reduced filters below the threshold value.
2774 	 */
2775 	if (old_overflow && !failed_filters &&
2776 	    vsi->active_filters < vsi->promisc_threshold) {
2777 		dev_info(&pf->pdev->dev,
2778 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2779 			 vsi_name);
2780 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2781 		vsi->promisc_threshold = 0;
2782 	}
2783 
2784 	/* if the VF is not trusted do not do promisc */
2785 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2786 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2787 		goto out;
2788 	}
2789 
2790 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2791 
2792 	/* If we are entering overflow promiscuous, we need to calculate a new
2793 	 * threshold for when we are safe to exit
2794 	 */
2795 	if (!old_overflow && new_overflow)
2796 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2797 
2798 	/* check for changes in promiscuous modes */
2799 	if (changed_flags & IFF_ALLMULTI) {
2800 		bool cur_multipromisc;
2801 
2802 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2803 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2804 							       vsi->seid,
2805 							       cur_multipromisc,
2806 							       NULL);
2807 		if (aq_ret) {
2808 			retval = i40e_aq_rc_to_posix(aq_ret,
2809 						     hw->aq.asq_last_status);
2810 			dev_info(&pf->pdev->dev,
2811 				 "set multi promisc failed on %s, err %pe aq_err %s\n",
2812 				 vsi_name,
2813 				 ERR_PTR(aq_ret),
2814 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2815 		} else {
2816 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2817 				 cur_multipromisc ? "entering" : "leaving");
2818 		}
2819 	}
2820 
2821 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2822 		bool cur_promisc;
2823 
2824 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2825 			       new_overflow);
2826 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2827 		if (aq_ret) {
2828 			retval = i40e_aq_rc_to_posix(aq_ret,
2829 						     hw->aq.asq_last_status);
2830 			dev_info(&pf->pdev->dev,
2831 				 "Setting promiscuous %s failed on %s, err %pe aq_err %s\n",
2832 				 cur_promisc ? "on" : "off",
2833 				 vsi_name,
2834 				 ERR_PTR(aq_ret),
2835 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2836 		}
2837 	}
2838 out:
2839 	/* if something went wrong then set the changed flag so we try again */
2840 	if (retval)
2841 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2842 
2843 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2844 	return retval;
2845 
2846 err_no_memory:
2847 	/* Restore elements on the temporary add and delete lists */
2848 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2849 err_no_memory_locked:
2850 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2851 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2852 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2853 
2854 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2855 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2856 	return -ENOMEM;
2857 }
2858 
2859 /**
2860  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2861  * @pf: board private structure
2862  **/
2863 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2864 {
2865 	int v;
2866 
2867 	if (!pf)
2868 		return;
2869 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2870 		return;
2871 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2872 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2873 		return;
2874 	}
2875 
2876 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2877 		if (pf->vsi[v] &&
2878 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2879 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2880 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2881 
2882 			if (ret) {
2883 				/* come back and try again later */
2884 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2885 					pf->state);
2886 				break;
2887 			}
2888 		}
2889 	}
2890 }
2891 
2892 /**
2893  * i40e_calculate_vsi_rx_buf_len - Calculates buffer length
2894  *
2895  * @vsi: VSI to calculate rx_buf_len from
2896  */
2897 static u16 i40e_calculate_vsi_rx_buf_len(struct i40e_vsi *vsi)
2898 {
2899 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2900 		return SKB_WITH_OVERHEAD(I40E_RXBUFFER_2048);
2901 
2902 	return PAGE_SIZE < 8192 ? I40E_RXBUFFER_3072 : I40E_RXBUFFER_2048;
2903 }
2904 
2905 /**
2906  * i40e_max_vsi_frame_size - returns the maximum allowed frame size for VSI
2907  * @vsi: the vsi
2908  * @xdp_prog: XDP program
2909  **/
2910 static int i40e_max_vsi_frame_size(struct i40e_vsi *vsi,
2911 				   struct bpf_prog *xdp_prog)
2912 {
2913 	u16 rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
2914 	u16 chain_len;
2915 
2916 	if (xdp_prog && !xdp_prog->aux->xdp_has_frags)
2917 		chain_len = 1;
2918 	else
2919 		chain_len = I40E_MAX_CHAINED_RX_BUFFERS;
2920 
2921 	return min_t(u16, rx_buf_len * chain_len, I40E_MAX_RXBUFFER);
2922 }
2923 
2924 /**
2925  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2926  * @netdev: network interface device structure
2927  * @new_mtu: new value for maximum frame size
2928  *
2929  * Returns 0 on success, negative on failure
2930  **/
2931 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2932 {
2933 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2934 	struct i40e_vsi *vsi = np->vsi;
2935 	struct i40e_pf *pf = vsi->back;
2936 	int frame_size;
2937 
2938 	frame_size = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
2939 	if (new_mtu > frame_size - I40E_PACKET_HDR_PAD) {
2940 		netdev_err(netdev, "Error changing mtu to %d, Max is %d\n",
2941 			   new_mtu, frame_size - I40E_PACKET_HDR_PAD);
2942 		return -EINVAL;
2943 	}
2944 
2945 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2946 		   netdev->mtu, new_mtu);
2947 	netdev->mtu = new_mtu;
2948 	if (netif_running(netdev))
2949 		i40e_vsi_reinit_locked(vsi);
2950 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2951 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2952 	return 0;
2953 }
2954 
2955 /**
2956  * i40e_ioctl - Access the hwtstamp interface
2957  * @netdev: network interface device structure
2958  * @ifr: interface request data
2959  * @cmd: ioctl command
2960  **/
2961 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2962 {
2963 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2964 	struct i40e_pf *pf = np->vsi->back;
2965 
2966 	switch (cmd) {
2967 	case SIOCGHWTSTAMP:
2968 		return i40e_ptp_get_ts_config(pf, ifr);
2969 	case SIOCSHWTSTAMP:
2970 		return i40e_ptp_set_ts_config(pf, ifr);
2971 	default:
2972 		return -EOPNOTSUPP;
2973 	}
2974 }
2975 
2976 /**
2977  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2978  * @vsi: the vsi being adjusted
2979  **/
2980 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2981 {
2982 	struct i40e_vsi_context ctxt;
2983 	int ret;
2984 
2985 	/* Don't modify stripping options if a port VLAN is active */
2986 	if (vsi->info.pvid)
2987 		return;
2988 
2989 	if ((vsi->info.valid_sections &
2990 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2991 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2992 		return;  /* already enabled */
2993 
2994 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2995 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2996 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2997 
2998 	ctxt.seid = vsi->seid;
2999 	ctxt.info = vsi->info;
3000 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3001 	if (ret) {
3002 		dev_info(&vsi->back->pdev->dev,
3003 			 "update vlan stripping failed, err %pe aq_err %s\n",
3004 			 ERR_PTR(ret),
3005 			 i40e_aq_str(&vsi->back->hw,
3006 				     vsi->back->hw.aq.asq_last_status));
3007 	}
3008 }
3009 
3010 /**
3011  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
3012  * @vsi: the vsi being adjusted
3013  **/
3014 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3015 {
3016 	struct i40e_vsi_context ctxt;
3017 	int ret;
3018 
3019 	/* Don't modify stripping options if a port VLAN is active */
3020 	if (vsi->info.pvid)
3021 		return;
3022 
3023 	if ((vsi->info.valid_sections &
3024 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3025 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3026 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3027 		return;  /* already disabled */
3028 
3029 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3030 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3031 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3032 
3033 	ctxt.seid = vsi->seid;
3034 	ctxt.info = vsi->info;
3035 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3036 	if (ret) {
3037 		dev_info(&vsi->back->pdev->dev,
3038 			 "update vlan stripping failed, err %pe aq_err %s\n",
3039 			 ERR_PTR(ret),
3040 			 i40e_aq_str(&vsi->back->hw,
3041 				     vsi->back->hw.aq.asq_last_status));
3042 	}
3043 }
3044 
3045 /**
3046  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3047  * @vsi: the vsi being configured
3048  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3049  *
3050  * This is a helper function for adding a new MAC/VLAN filter with the
3051  * specified VLAN for each existing MAC address already in the hash table.
3052  * This function does *not* perform any accounting to update filters based on
3053  * VLAN mode.
3054  *
3055  * NOTE: this function expects to be called while under the
3056  * mac_filter_hash_lock
3057  **/
3058 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3059 {
3060 	struct i40e_mac_filter *f, *add_f;
3061 	struct hlist_node *h;
3062 	int bkt;
3063 
3064 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3065 		/* If we're asked to add a filter that has been marked for
3066 		 * removal, it is safe to simply restore it to active state.
3067 		 * __i40e_del_filter will have simply deleted any filters which
3068 		 * were previously marked NEW or FAILED, so if it is currently
3069 		 * marked REMOVE it must have previously been ACTIVE. Since we
3070 		 * haven't yet run the sync filters task, just restore this
3071 		 * filter to the ACTIVE state so that the sync task leaves it
3072 		 * in place.
3073 		 */
3074 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3075 			f->state = I40E_FILTER_ACTIVE;
3076 			continue;
3077 		} else if (f->state == I40E_FILTER_REMOVE) {
3078 			continue;
3079 		}
3080 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3081 		if (!add_f) {
3082 			dev_info(&vsi->back->pdev->dev,
3083 				 "Could not add vlan filter %d for %pM\n",
3084 				 vid, f->macaddr);
3085 			return -ENOMEM;
3086 		}
3087 	}
3088 
3089 	return 0;
3090 }
3091 
3092 /**
3093  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3094  * @vsi: the VSI being configured
3095  * @vid: VLAN id to be added
3096  **/
3097 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3098 {
3099 	int err;
3100 
3101 	if (vsi->info.pvid)
3102 		return -EINVAL;
3103 
3104 	/* The network stack will attempt to add VID=0, with the intention to
3105 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3106 	 * these packets by default when configured to receive untagged
3107 	 * packets, so we don't need to add a filter for this case.
3108 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3109 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3110 	 * Thus, we do not want to actually add a filter for VID=0
3111 	 */
3112 	if (!vid)
3113 		return 0;
3114 
3115 	/* Locked once because all functions invoked below iterates list*/
3116 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3117 	err = i40e_add_vlan_all_mac(vsi, vid);
3118 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3119 	if (err)
3120 		return err;
3121 
3122 	/* schedule our worker thread which will take care of
3123 	 * applying the new filter changes
3124 	 */
3125 	i40e_service_event_schedule(vsi->back);
3126 	return 0;
3127 }
3128 
3129 /**
3130  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3131  * @vsi: the vsi being configured
3132  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3133  *
3134  * This function should be used to remove all VLAN filters which match the
3135  * given VID. It does not schedule the service event and does not take the
3136  * mac_filter_hash_lock so it may be combined with other operations under
3137  * a single invocation of the mac_filter_hash_lock.
3138  *
3139  * NOTE: this function expects to be called while under the
3140  * mac_filter_hash_lock
3141  */
3142 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3143 {
3144 	struct i40e_mac_filter *f;
3145 	struct hlist_node *h;
3146 	int bkt;
3147 
3148 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3149 		if (f->vlan == vid)
3150 			__i40e_del_filter(vsi, f);
3151 	}
3152 }
3153 
3154 /**
3155  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3156  * @vsi: the VSI being configured
3157  * @vid: VLAN id to be removed
3158  **/
3159 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3160 {
3161 	if (!vid || vsi->info.pvid)
3162 		return;
3163 
3164 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3165 	i40e_rm_vlan_all_mac(vsi, vid);
3166 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3167 
3168 	/* schedule our worker thread which will take care of
3169 	 * applying the new filter changes
3170 	 */
3171 	i40e_service_event_schedule(vsi->back);
3172 }
3173 
3174 /**
3175  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3176  * @netdev: network interface to be adjusted
3177  * @proto: unused protocol value
3178  * @vid: vlan id to be added
3179  *
3180  * net_device_ops implementation for adding vlan ids
3181  **/
3182 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3183 				__always_unused __be16 proto, u16 vid)
3184 {
3185 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3186 	struct i40e_vsi *vsi = np->vsi;
3187 	int ret = 0;
3188 
3189 	if (vid >= VLAN_N_VID)
3190 		return -EINVAL;
3191 
3192 	ret = i40e_vsi_add_vlan(vsi, vid);
3193 	if (!ret)
3194 		set_bit(vid, vsi->active_vlans);
3195 
3196 	return ret;
3197 }
3198 
3199 /**
3200  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3201  * @netdev: network interface to be adjusted
3202  * @proto: unused protocol value
3203  * @vid: vlan id to be added
3204  **/
3205 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3206 				    __always_unused __be16 proto, u16 vid)
3207 {
3208 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3209 	struct i40e_vsi *vsi = np->vsi;
3210 
3211 	if (vid >= VLAN_N_VID)
3212 		return;
3213 	set_bit(vid, vsi->active_vlans);
3214 }
3215 
3216 /**
3217  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3218  * @netdev: network interface to be adjusted
3219  * @proto: unused protocol value
3220  * @vid: vlan id to be removed
3221  *
3222  * net_device_ops implementation for removing vlan ids
3223  **/
3224 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3225 				 __always_unused __be16 proto, u16 vid)
3226 {
3227 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3228 	struct i40e_vsi *vsi = np->vsi;
3229 
3230 	/* return code is ignored as there is nothing a user
3231 	 * can do about failure to remove and a log message was
3232 	 * already printed from the other function
3233 	 */
3234 	i40e_vsi_kill_vlan(vsi, vid);
3235 
3236 	clear_bit(vid, vsi->active_vlans);
3237 
3238 	return 0;
3239 }
3240 
3241 /**
3242  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3243  * @vsi: the vsi being brought back up
3244  **/
3245 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3246 {
3247 	u16 vid;
3248 
3249 	if (!vsi->netdev)
3250 		return;
3251 
3252 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3253 		i40e_vlan_stripping_enable(vsi);
3254 	else
3255 		i40e_vlan_stripping_disable(vsi);
3256 
3257 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3258 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3259 					vid);
3260 }
3261 
3262 /**
3263  * i40e_vsi_add_pvid - Add pvid for the VSI
3264  * @vsi: the vsi being adjusted
3265  * @vid: the vlan id to set as a PVID
3266  **/
3267 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3268 {
3269 	struct i40e_vsi_context ctxt;
3270 	int ret;
3271 
3272 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3273 	vsi->info.pvid = cpu_to_le16(vid);
3274 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3275 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3276 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3277 
3278 	ctxt.seid = vsi->seid;
3279 	ctxt.info = vsi->info;
3280 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3281 	if (ret) {
3282 		dev_info(&vsi->back->pdev->dev,
3283 			 "add pvid failed, err %pe aq_err %s\n",
3284 			 ERR_PTR(ret),
3285 			 i40e_aq_str(&vsi->back->hw,
3286 				     vsi->back->hw.aq.asq_last_status));
3287 		return -ENOENT;
3288 	}
3289 
3290 	return 0;
3291 }
3292 
3293 /**
3294  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3295  * @vsi: the vsi being adjusted
3296  *
3297  * Just use the vlan_rx_register() service to put it back to normal
3298  **/
3299 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3300 {
3301 	vsi->info.pvid = 0;
3302 
3303 	i40e_vlan_stripping_disable(vsi);
3304 }
3305 
3306 /**
3307  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3308  * @vsi: ptr to the VSI
3309  *
3310  * If this function returns with an error, then it's possible one or
3311  * more of the rings is populated (while the rest are not).  It is the
3312  * callers duty to clean those orphaned rings.
3313  *
3314  * Return 0 on success, negative on failure
3315  **/
3316 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3317 {
3318 	int i, err = 0;
3319 
3320 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3321 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3322 
3323 	if (!i40e_enabled_xdp_vsi(vsi))
3324 		return err;
3325 
3326 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3327 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3328 
3329 	return err;
3330 }
3331 
3332 /**
3333  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3334  * @vsi: ptr to the VSI
3335  *
3336  * Free VSI's transmit software resources
3337  **/
3338 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3339 {
3340 	int i;
3341 
3342 	if (vsi->tx_rings) {
3343 		for (i = 0; i < vsi->num_queue_pairs; i++)
3344 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3345 				i40e_free_tx_resources(vsi->tx_rings[i]);
3346 	}
3347 
3348 	if (vsi->xdp_rings) {
3349 		for (i = 0; i < vsi->num_queue_pairs; i++)
3350 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3351 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3352 	}
3353 }
3354 
3355 /**
3356  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3357  * @vsi: ptr to the VSI
3358  *
3359  * If this function returns with an error, then it's possible one or
3360  * more of the rings is populated (while the rest are not).  It is the
3361  * callers duty to clean those orphaned rings.
3362  *
3363  * Return 0 on success, negative on failure
3364  **/
3365 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3366 {
3367 	int i, err = 0;
3368 
3369 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3370 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3371 	return err;
3372 }
3373 
3374 /**
3375  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3376  * @vsi: ptr to the VSI
3377  *
3378  * Free all receive software resources
3379  **/
3380 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3381 {
3382 	int i;
3383 
3384 	if (!vsi->rx_rings)
3385 		return;
3386 
3387 	for (i = 0; i < vsi->num_queue_pairs; i++)
3388 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3389 			i40e_free_rx_resources(vsi->rx_rings[i]);
3390 }
3391 
3392 /**
3393  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3394  * @ring: The Tx ring to configure
3395  *
3396  * This enables/disables XPS for a given Tx descriptor ring
3397  * based on the TCs enabled for the VSI that ring belongs to.
3398  **/
3399 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3400 {
3401 	int cpu;
3402 
3403 	if (!ring->q_vector || !ring->netdev || ring->ch)
3404 		return;
3405 
3406 	/* We only initialize XPS once, so as not to overwrite user settings */
3407 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3408 		return;
3409 
3410 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3411 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3412 			    ring->queue_index);
3413 }
3414 
3415 /**
3416  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3417  * @ring: The Tx or Rx ring
3418  *
3419  * Returns the AF_XDP buffer pool or NULL.
3420  **/
3421 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3422 {
3423 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3424 	int qid = ring->queue_index;
3425 
3426 	if (ring_is_xdp(ring))
3427 		qid -= ring->vsi->alloc_queue_pairs;
3428 
3429 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3430 		return NULL;
3431 
3432 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3433 }
3434 
3435 /**
3436  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3437  * @ring: The Tx ring to configure
3438  *
3439  * Configure the Tx descriptor ring in the HMC context.
3440  **/
3441 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3442 {
3443 	struct i40e_vsi *vsi = ring->vsi;
3444 	u16 pf_q = vsi->base_queue + ring->queue_index;
3445 	struct i40e_hw *hw = &vsi->back->hw;
3446 	struct i40e_hmc_obj_txq tx_ctx;
3447 	u32 qtx_ctl = 0;
3448 	int err = 0;
3449 
3450 	if (ring_is_xdp(ring))
3451 		ring->xsk_pool = i40e_xsk_pool(ring);
3452 
3453 	/* some ATR related tx ring init */
3454 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3455 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3456 		ring->atr_count = 0;
3457 	} else {
3458 		ring->atr_sample_rate = 0;
3459 	}
3460 
3461 	/* configure XPS */
3462 	i40e_config_xps_tx_ring(ring);
3463 
3464 	/* clear the context structure first */
3465 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3466 
3467 	tx_ctx.new_context = 1;
3468 	tx_ctx.base = (ring->dma / 128);
3469 	tx_ctx.qlen = ring->count;
3470 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3471 					       I40E_FLAG_FD_ATR_ENABLED));
3472 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3473 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3474 	if (vsi->type != I40E_VSI_FDIR)
3475 		tx_ctx.head_wb_ena = 1;
3476 	tx_ctx.head_wb_addr = ring->dma +
3477 			      (ring->count * sizeof(struct i40e_tx_desc));
3478 
3479 	/* As part of VSI creation/update, FW allocates certain
3480 	 * Tx arbitration queue sets for each TC enabled for
3481 	 * the VSI. The FW returns the handles to these queue
3482 	 * sets as part of the response buffer to Add VSI,
3483 	 * Update VSI, etc. AQ commands. It is expected that
3484 	 * these queue set handles be associated with the Tx
3485 	 * queues by the driver as part of the TX queue context
3486 	 * initialization. This has to be done regardless of
3487 	 * DCB as by default everything is mapped to TC0.
3488 	 */
3489 
3490 	if (ring->ch)
3491 		tx_ctx.rdylist =
3492 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3493 
3494 	else
3495 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3496 
3497 	tx_ctx.rdylist_act = 0;
3498 
3499 	/* clear the context in the HMC */
3500 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3501 	if (err) {
3502 		dev_info(&vsi->back->pdev->dev,
3503 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3504 			 ring->queue_index, pf_q, err);
3505 		return -ENOMEM;
3506 	}
3507 
3508 	/* set the context in the HMC */
3509 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3510 	if (err) {
3511 		dev_info(&vsi->back->pdev->dev,
3512 			 "Failed to set 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 	/* Now associate this queue with this PCI function */
3518 	if (ring->ch) {
3519 		if (ring->ch->type == I40E_VSI_VMDQ2)
3520 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3521 		else
3522 			return -EINVAL;
3523 
3524 		qtx_ctl |= (ring->ch->vsi_number <<
3525 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3526 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3527 	} else {
3528 		if (vsi->type == I40E_VSI_VMDQ2) {
3529 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3530 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3531 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3532 		} else {
3533 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3534 		}
3535 	}
3536 
3537 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3538 		    I40E_QTX_CTL_PF_INDX_MASK);
3539 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3540 	i40e_flush(hw);
3541 
3542 	/* cache tail off for easier writes later */
3543 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3544 
3545 	return 0;
3546 }
3547 
3548 /**
3549  * i40e_rx_offset - Return expected offset into page to access data
3550  * @rx_ring: Ring we are requesting offset of
3551  *
3552  * Returns the offset value for ring into the data buffer.
3553  */
3554 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3555 {
3556 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3557 }
3558 
3559 /**
3560  * i40e_configure_rx_ring - Configure a receive ring context
3561  * @ring: The Rx ring to configure
3562  *
3563  * Configure the Rx descriptor ring in the HMC context.
3564  **/
3565 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3566 {
3567 	struct i40e_vsi *vsi = ring->vsi;
3568 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3569 	u16 pf_q = vsi->base_queue + ring->queue_index;
3570 	struct i40e_hw *hw = &vsi->back->hw;
3571 	struct i40e_hmc_obj_rxq rx_ctx;
3572 	int err = 0;
3573 	bool ok;
3574 	int ret;
3575 
3576 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3577 
3578 	/* clear the context structure first */
3579 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3580 
3581 	if (ring->vsi->type == I40E_VSI_MAIN)
3582 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3583 
3584 	ring->xsk_pool = i40e_xsk_pool(ring);
3585 	if (ring->xsk_pool) {
3586 		ring->rx_buf_len =
3587 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3588 		/* For AF_XDP ZC, we disallow packets to span on
3589 		 * multiple buffers, thus letting us skip that
3590 		 * handling in the fast-path.
3591 		 */
3592 		chain_len = 1;
3593 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3594 						 MEM_TYPE_XSK_BUFF_POOL,
3595 						 NULL);
3596 		if (ret)
3597 			return ret;
3598 		dev_info(&vsi->back->pdev->dev,
3599 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3600 			 ring->queue_index);
3601 
3602 	} else {
3603 		ring->rx_buf_len = vsi->rx_buf_len;
3604 		if (ring->vsi->type == I40E_VSI_MAIN) {
3605 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3606 							 MEM_TYPE_PAGE_SHARED,
3607 							 NULL);
3608 			if (ret)
3609 				return ret;
3610 		}
3611 	}
3612 
3613 	xdp_init_buff(&ring->xdp, i40e_rx_pg_size(ring) / 2, &ring->xdp_rxq);
3614 
3615 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3616 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3617 
3618 	rx_ctx.base = (ring->dma / 128);
3619 	rx_ctx.qlen = ring->count;
3620 
3621 	/* use 16 byte descriptors */
3622 	rx_ctx.dsize = 0;
3623 
3624 	/* descriptor type is always zero
3625 	 * rx_ctx.dtype = 0;
3626 	 */
3627 	rx_ctx.hsplit_0 = 0;
3628 
3629 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3630 	if (hw->revision_id == 0)
3631 		rx_ctx.lrxqthresh = 0;
3632 	else
3633 		rx_ctx.lrxqthresh = 1;
3634 	rx_ctx.crcstrip = 1;
3635 	rx_ctx.l2tsel = 1;
3636 	/* this controls whether VLAN is stripped from inner headers */
3637 	rx_ctx.showiv = 0;
3638 	/* set the prefena field to 1 because the manual says to */
3639 	rx_ctx.prefena = 1;
3640 
3641 	/* clear the context in the HMC */
3642 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3643 	if (err) {
3644 		dev_info(&vsi->back->pdev->dev,
3645 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3646 			 ring->queue_index, pf_q, err);
3647 		return -ENOMEM;
3648 	}
3649 
3650 	/* set the context in the HMC */
3651 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3652 	if (err) {
3653 		dev_info(&vsi->back->pdev->dev,
3654 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3655 			 ring->queue_index, pf_q, err);
3656 		return -ENOMEM;
3657 	}
3658 
3659 	/* configure Rx buffer alignment */
3660 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3661 		if (I40E_2K_TOO_SMALL_WITH_PADDING) {
3662 			dev_info(&vsi->back->pdev->dev,
3663 				 "2k Rx buffer is too small to fit standard MTU and skb_shared_info\n");
3664 			return -EOPNOTSUPP;
3665 		}
3666 		clear_ring_build_skb_enabled(ring);
3667 	} else {
3668 		set_ring_build_skb_enabled(ring);
3669 	}
3670 
3671 	ring->rx_offset = i40e_rx_offset(ring);
3672 
3673 	/* cache tail for quicker writes, and clear the reg before use */
3674 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3675 	writel(0, ring->tail);
3676 
3677 	if (ring->xsk_pool) {
3678 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3679 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3680 	} else {
3681 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3682 	}
3683 	if (!ok) {
3684 		/* Log this in case the user has forgotten to give the kernel
3685 		 * any buffers, even later in the application.
3686 		 */
3687 		dev_info(&vsi->back->pdev->dev,
3688 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3689 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3690 			 ring->queue_index, pf_q);
3691 	}
3692 
3693 	return 0;
3694 }
3695 
3696 /**
3697  * i40e_vsi_configure_tx - Configure the VSI for Tx
3698  * @vsi: VSI structure describing this set of rings and resources
3699  *
3700  * Configure the Tx VSI for operation.
3701  **/
3702 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3703 {
3704 	int err = 0;
3705 	u16 i;
3706 
3707 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3708 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3709 
3710 	if (err || !i40e_enabled_xdp_vsi(vsi))
3711 		return err;
3712 
3713 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3714 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3715 
3716 	return err;
3717 }
3718 
3719 /**
3720  * i40e_vsi_configure_rx - Configure the VSI for Rx
3721  * @vsi: the VSI being configured
3722  *
3723  * Configure the Rx VSI for operation.
3724  **/
3725 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3726 {
3727 	int err = 0;
3728 	u16 i;
3729 
3730 	vsi->max_frame = i40e_max_vsi_frame_size(vsi, vsi->xdp_prog);
3731 	vsi->rx_buf_len = i40e_calculate_vsi_rx_buf_len(vsi);
3732 
3733 #if (PAGE_SIZE < 8192)
3734 	if (vsi->netdev && !I40E_2K_TOO_SMALL_WITH_PADDING &&
3735 	    vsi->netdev->mtu <= ETH_DATA_LEN) {
3736 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3737 		vsi->max_frame = vsi->rx_buf_len;
3738 	}
3739 #endif
3740 
3741 	/* set up individual rings */
3742 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3743 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3744 
3745 	return err;
3746 }
3747 
3748 /**
3749  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3750  * @vsi: ptr to the VSI
3751  **/
3752 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3753 {
3754 	struct i40e_ring *tx_ring, *rx_ring;
3755 	u16 qoffset, qcount;
3756 	int i, n;
3757 
3758 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3759 		/* Reset the TC information */
3760 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3761 			rx_ring = vsi->rx_rings[i];
3762 			tx_ring = vsi->tx_rings[i];
3763 			rx_ring->dcb_tc = 0;
3764 			tx_ring->dcb_tc = 0;
3765 		}
3766 		return;
3767 	}
3768 
3769 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3770 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3771 			continue;
3772 
3773 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3774 		qcount = vsi->tc_config.tc_info[n].qcount;
3775 		for (i = qoffset; i < (qoffset + qcount); i++) {
3776 			rx_ring = vsi->rx_rings[i];
3777 			tx_ring = vsi->tx_rings[i];
3778 			rx_ring->dcb_tc = n;
3779 			tx_ring->dcb_tc = n;
3780 		}
3781 	}
3782 }
3783 
3784 /**
3785  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3786  * @vsi: ptr to the VSI
3787  **/
3788 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3789 {
3790 	if (vsi->netdev)
3791 		i40e_set_rx_mode(vsi->netdev);
3792 }
3793 
3794 /**
3795  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3796  * @pf: Pointer to the targeted PF
3797  *
3798  * Set all flow director counters to 0.
3799  */
3800 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3801 {
3802 	pf->fd_tcp4_filter_cnt = 0;
3803 	pf->fd_udp4_filter_cnt = 0;
3804 	pf->fd_sctp4_filter_cnt = 0;
3805 	pf->fd_ip4_filter_cnt = 0;
3806 	pf->fd_tcp6_filter_cnt = 0;
3807 	pf->fd_udp6_filter_cnt = 0;
3808 	pf->fd_sctp6_filter_cnt = 0;
3809 	pf->fd_ip6_filter_cnt = 0;
3810 }
3811 
3812 /**
3813  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3814  * @vsi: Pointer to the targeted VSI
3815  *
3816  * This function replays the hlist on the hw where all the SB Flow Director
3817  * filters were saved.
3818  **/
3819 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3820 {
3821 	struct i40e_fdir_filter *filter;
3822 	struct i40e_pf *pf = vsi->back;
3823 	struct hlist_node *node;
3824 
3825 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3826 		return;
3827 
3828 	/* Reset FDir counters as we're replaying all existing filters */
3829 	i40e_reset_fdir_filter_cnt(pf);
3830 
3831 	hlist_for_each_entry_safe(filter, node,
3832 				  &pf->fdir_filter_list, fdir_node) {
3833 		i40e_add_del_fdir(vsi, filter, true);
3834 	}
3835 }
3836 
3837 /**
3838  * i40e_vsi_configure - Set up the VSI for action
3839  * @vsi: the VSI being configured
3840  **/
3841 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3842 {
3843 	int err;
3844 
3845 	i40e_set_vsi_rx_mode(vsi);
3846 	i40e_restore_vlan(vsi);
3847 	i40e_vsi_config_dcb_rings(vsi);
3848 	err = i40e_vsi_configure_tx(vsi);
3849 	if (!err)
3850 		err = i40e_vsi_configure_rx(vsi);
3851 
3852 	return err;
3853 }
3854 
3855 /**
3856  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3857  * @vsi: the VSI being configured
3858  **/
3859 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3860 {
3861 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3862 	struct i40e_pf *pf = vsi->back;
3863 	struct i40e_hw *hw = &pf->hw;
3864 	u16 vector;
3865 	int i, q;
3866 	u32 qp;
3867 
3868 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3869 	 * and PFINT_LNKLSTn registers, e.g.:
3870 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3871 	 */
3872 	qp = vsi->base_queue;
3873 	vector = vsi->base_vector;
3874 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3875 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3876 
3877 		q_vector->rx.next_update = jiffies + 1;
3878 		q_vector->rx.target_itr =
3879 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3880 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3881 		     q_vector->rx.target_itr >> 1);
3882 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3883 
3884 		q_vector->tx.next_update = jiffies + 1;
3885 		q_vector->tx.target_itr =
3886 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3887 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3888 		     q_vector->tx.target_itr >> 1);
3889 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3890 
3891 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3892 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3893 
3894 		/* begin of linked list for RX queue assigned to this vector */
3895 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3896 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3897 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3898 			u32 val;
3899 
3900 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3901 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3902 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3903 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3904 			      (I40E_QUEUE_TYPE_TX <<
3905 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3906 
3907 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3908 
3909 			if (has_xdp) {
3910 				/* TX queue with next queue set to TX */
3911 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3912 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3913 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3914 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3915 				      (I40E_QUEUE_TYPE_TX <<
3916 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3917 
3918 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3919 			}
3920 			/* TX queue with next RX or end of linked list */
3921 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3922 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3923 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3924 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3925 			      (I40E_QUEUE_TYPE_RX <<
3926 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3927 
3928 			/* Terminate the linked list */
3929 			if (q == (q_vector->num_ringpairs - 1))
3930 				val |= (I40E_QUEUE_END_OF_LIST <<
3931 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3932 
3933 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3934 			qp++;
3935 		}
3936 	}
3937 
3938 	i40e_flush(hw);
3939 }
3940 
3941 /**
3942  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3943  * @pf: pointer to private device data structure
3944  **/
3945 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3946 {
3947 	struct i40e_hw *hw = &pf->hw;
3948 	u32 val;
3949 
3950 	/* clear things first */
3951 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3952 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3953 
3954 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3955 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3956 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3957 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3958 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3959 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3960 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3961 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3962 
3963 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3964 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3965 
3966 	if (pf->flags & I40E_FLAG_PTP)
3967 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3968 
3969 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3970 
3971 	/* SW_ITR_IDX = 0, but don't change INTENA */
3972 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3973 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3974 
3975 	/* OTHER_ITR_IDX = 0 */
3976 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3977 }
3978 
3979 /**
3980  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3981  * @vsi: the VSI being configured
3982  **/
3983 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3984 {
3985 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3986 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3987 	struct i40e_pf *pf = vsi->back;
3988 	struct i40e_hw *hw = &pf->hw;
3989 
3990 	/* set the ITR configuration */
3991 	q_vector->rx.next_update = jiffies + 1;
3992 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3993 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3994 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3995 	q_vector->tx.next_update = jiffies + 1;
3996 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3997 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3998 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3999 
4000 	i40e_enable_misc_int_causes(pf);
4001 
4002 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
4003 	wr32(hw, I40E_PFINT_LNKLST0, 0);
4004 
4005 	/* Associate the queue pair to the vector and enable the queue
4006 	 * interrupt RX queue in linked list with next queue set to TX
4007 	 */
4008 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
4009 
4010 	if (i40e_enabled_xdp_vsi(vsi)) {
4011 		/* TX queue in linked list with next queue set to TX */
4012 		wr32(hw, I40E_QINT_TQCTL(nextqp),
4013 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
4014 	}
4015 
4016 	/* last TX queue so the next RX queue doesn't matter */
4017 	wr32(hw, I40E_QINT_TQCTL(0),
4018 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4019 	i40e_flush(hw);
4020 }
4021 
4022 /**
4023  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4024  * @pf: board private structure
4025  **/
4026 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4027 {
4028 	struct i40e_hw *hw = &pf->hw;
4029 
4030 	wr32(hw, I40E_PFINT_DYN_CTL0,
4031 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4032 	i40e_flush(hw);
4033 }
4034 
4035 /**
4036  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4037  * @pf: board private structure
4038  **/
4039 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4040 {
4041 	struct i40e_hw *hw = &pf->hw;
4042 	u32 val;
4043 
4044 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4045 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4046 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4047 
4048 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4049 	i40e_flush(hw);
4050 }
4051 
4052 /**
4053  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4054  * @irq: interrupt number
4055  * @data: pointer to a q_vector
4056  **/
4057 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4058 {
4059 	struct i40e_q_vector *q_vector = data;
4060 
4061 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4062 		return IRQ_HANDLED;
4063 
4064 	napi_schedule_irqoff(&q_vector->napi);
4065 
4066 	return IRQ_HANDLED;
4067 }
4068 
4069 /**
4070  * i40e_irq_affinity_notify - Callback for affinity changes
4071  * @notify: context as to what irq was changed
4072  * @mask: the new affinity mask
4073  *
4074  * This is a callback function used by the irq_set_affinity_notifier function
4075  * so that we may register to receive changes to the irq affinity masks.
4076  **/
4077 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4078 				     const cpumask_t *mask)
4079 {
4080 	struct i40e_q_vector *q_vector =
4081 		container_of(notify, struct i40e_q_vector, affinity_notify);
4082 
4083 	cpumask_copy(&q_vector->affinity_mask, mask);
4084 }
4085 
4086 /**
4087  * i40e_irq_affinity_release - Callback for affinity notifier release
4088  * @ref: internal core kernel usage
4089  *
4090  * This is a callback function used by the irq_set_affinity_notifier function
4091  * to inform the current notification subscriber that they will no longer
4092  * receive notifications.
4093  **/
4094 static void i40e_irq_affinity_release(struct kref *ref) {}
4095 
4096 /**
4097  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4098  * @vsi: the VSI being configured
4099  * @basename: name for the vector
4100  *
4101  * Allocates MSI-X vectors and requests interrupts from the kernel.
4102  **/
4103 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4104 {
4105 	int q_vectors = vsi->num_q_vectors;
4106 	struct i40e_pf *pf = vsi->back;
4107 	int base = vsi->base_vector;
4108 	int rx_int_idx = 0;
4109 	int tx_int_idx = 0;
4110 	int vector, err;
4111 	int irq_num;
4112 	int cpu;
4113 
4114 	for (vector = 0; vector < q_vectors; vector++) {
4115 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4116 
4117 		irq_num = pf->msix_entries[base + vector].vector;
4118 
4119 		if (q_vector->tx.ring && q_vector->rx.ring) {
4120 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4121 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4122 			tx_int_idx++;
4123 		} else if (q_vector->rx.ring) {
4124 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4125 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4126 		} else if (q_vector->tx.ring) {
4127 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4128 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4129 		} else {
4130 			/* skip this unused q_vector */
4131 			continue;
4132 		}
4133 		err = request_irq(irq_num,
4134 				  vsi->irq_handler,
4135 				  0,
4136 				  q_vector->name,
4137 				  q_vector);
4138 		if (err) {
4139 			dev_info(&pf->pdev->dev,
4140 				 "MSIX request_irq failed, error: %d\n", err);
4141 			goto free_queue_irqs;
4142 		}
4143 
4144 		/* register for affinity change notifications */
4145 		q_vector->irq_num = irq_num;
4146 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4147 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4148 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4149 		/* Spread affinity hints out across online CPUs.
4150 		 *
4151 		 * get_cpu_mask returns a static constant mask with
4152 		 * a permanent lifetime so it's ok to pass to
4153 		 * irq_update_affinity_hint without making a copy.
4154 		 */
4155 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4156 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4157 	}
4158 
4159 	vsi->irqs_ready = true;
4160 	return 0;
4161 
4162 free_queue_irqs:
4163 	while (vector) {
4164 		vector--;
4165 		irq_num = pf->msix_entries[base + vector].vector;
4166 		irq_set_affinity_notifier(irq_num, NULL);
4167 		irq_update_affinity_hint(irq_num, NULL);
4168 		free_irq(irq_num, &vsi->q_vectors[vector]);
4169 	}
4170 	return err;
4171 }
4172 
4173 /**
4174  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4175  * @vsi: the VSI being un-configured
4176  **/
4177 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4178 {
4179 	struct i40e_pf *pf = vsi->back;
4180 	struct i40e_hw *hw = &pf->hw;
4181 	int base = vsi->base_vector;
4182 	int i;
4183 
4184 	/* disable interrupt causation from each queue */
4185 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4186 		u32 val;
4187 
4188 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4189 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4190 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4191 
4192 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4193 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4194 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4195 
4196 		if (!i40e_enabled_xdp_vsi(vsi))
4197 			continue;
4198 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4199 	}
4200 
4201 	/* disable each interrupt */
4202 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4203 		for (i = vsi->base_vector;
4204 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4205 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4206 
4207 		i40e_flush(hw);
4208 		for (i = 0; i < vsi->num_q_vectors; i++)
4209 			synchronize_irq(pf->msix_entries[i + base].vector);
4210 	} else {
4211 		/* Legacy and MSI mode - this stops all interrupt handling */
4212 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4213 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4214 		i40e_flush(hw);
4215 		synchronize_irq(pf->pdev->irq);
4216 	}
4217 }
4218 
4219 /**
4220  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4221  * @vsi: the VSI being configured
4222  **/
4223 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4224 {
4225 	struct i40e_pf *pf = vsi->back;
4226 	int i;
4227 
4228 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4229 		for (i = 0; i < vsi->num_q_vectors; i++)
4230 			i40e_irq_dynamic_enable(vsi, i);
4231 	} else {
4232 		i40e_irq_dynamic_enable_icr0(pf);
4233 	}
4234 
4235 	i40e_flush(&pf->hw);
4236 	return 0;
4237 }
4238 
4239 /**
4240  * i40e_free_misc_vector - Free the vector that handles non-queue events
4241  * @pf: board private structure
4242  **/
4243 static void i40e_free_misc_vector(struct i40e_pf *pf)
4244 {
4245 	/* Disable ICR 0 */
4246 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4247 	i40e_flush(&pf->hw);
4248 
4249 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4250 		free_irq(pf->msix_entries[0].vector, pf);
4251 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4252 	}
4253 }
4254 
4255 /**
4256  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4257  * @irq: interrupt number
4258  * @data: pointer to a q_vector
4259  *
4260  * This is the handler used for all MSI/Legacy interrupts, and deals
4261  * with both queue and non-queue interrupts.  This is also used in
4262  * MSIX mode to handle the non-queue interrupts.
4263  **/
4264 static irqreturn_t i40e_intr(int irq, void *data)
4265 {
4266 	struct i40e_pf *pf = (struct i40e_pf *)data;
4267 	struct i40e_hw *hw = &pf->hw;
4268 	irqreturn_t ret = IRQ_NONE;
4269 	u32 icr0, icr0_remaining;
4270 	u32 val, ena_mask;
4271 
4272 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4273 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4274 
4275 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4276 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4277 		goto enable_intr;
4278 
4279 	/* if interrupt but no bits showing, must be SWINT */
4280 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4281 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4282 		pf->sw_int_count++;
4283 
4284 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4285 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4286 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4287 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4288 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4289 	}
4290 
4291 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4292 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4293 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4294 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4295 
4296 		/* We do not have a way to disarm Queue causes while leaving
4297 		 * interrupt enabled for all other causes, ideally
4298 		 * interrupt should be disabled while we are in NAPI but
4299 		 * this is not a performance path and napi_schedule()
4300 		 * can deal with rescheduling.
4301 		 */
4302 		if (!test_bit(__I40E_DOWN, pf->state))
4303 			napi_schedule_irqoff(&q_vector->napi);
4304 	}
4305 
4306 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4307 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4308 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4309 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4310 	}
4311 
4312 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4313 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4314 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4315 	}
4316 
4317 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4318 		/* disable any further VFLR event notifications */
4319 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4320 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4321 
4322 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4323 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4324 		} else {
4325 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4326 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4327 		}
4328 	}
4329 
4330 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4331 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4332 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4333 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4334 		val = rd32(hw, I40E_GLGEN_RSTAT);
4335 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4336 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4337 		if (val == I40E_RESET_CORER) {
4338 			pf->corer_count++;
4339 		} else if (val == I40E_RESET_GLOBR) {
4340 			pf->globr_count++;
4341 		} else if (val == I40E_RESET_EMPR) {
4342 			pf->empr_count++;
4343 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4344 		}
4345 	}
4346 
4347 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4348 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4349 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4350 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4351 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4352 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4353 	}
4354 
4355 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4356 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4357 
4358 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4359 			schedule_work(&pf->ptp_extts0_work);
4360 
4361 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4362 			i40e_ptp_tx_hwtstamp(pf);
4363 
4364 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4365 	}
4366 
4367 	/* If a critical error is pending we have no choice but to reset the
4368 	 * device.
4369 	 * Report and mask out any remaining unexpected interrupts.
4370 	 */
4371 	icr0_remaining = icr0 & ena_mask;
4372 	if (icr0_remaining) {
4373 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4374 			 icr0_remaining);
4375 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4376 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4377 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4378 			dev_info(&pf->pdev->dev, "device will be reset\n");
4379 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4380 			i40e_service_event_schedule(pf);
4381 		}
4382 		ena_mask &= ~icr0_remaining;
4383 	}
4384 	ret = IRQ_HANDLED;
4385 
4386 enable_intr:
4387 	/* re-enable interrupt causes */
4388 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4389 	if (!test_bit(__I40E_DOWN, pf->state) ||
4390 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4391 		i40e_service_event_schedule(pf);
4392 		i40e_irq_dynamic_enable_icr0(pf);
4393 	}
4394 
4395 	return ret;
4396 }
4397 
4398 /**
4399  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4400  * @tx_ring:  tx ring to clean
4401  * @budget:   how many cleans we're allowed
4402  *
4403  * Returns true if there's any budget left (e.g. the clean is finished)
4404  **/
4405 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4406 {
4407 	struct i40e_vsi *vsi = tx_ring->vsi;
4408 	u16 i = tx_ring->next_to_clean;
4409 	struct i40e_tx_buffer *tx_buf;
4410 	struct i40e_tx_desc *tx_desc;
4411 
4412 	tx_buf = &tx_ring->tx_bi[i];
4413 	tx_desc = I40E_TX_DESC(tx_ring, i);
4414 	i -= tx_ring->count;
4415 
4416 	do {
4417 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4418 
4419 		/* if next_to_watch is not set then there is no work pending */
4420 		if (!eop_desc)
4421 			break;
4422 
4423 		/* prevent any other reads prior to eop_desc */
4424 		smp_rmb();
4425 
4426 		/* if the descriptor isn't done, no work yet to do */
4427 		if (!(eop_desc->cmd_type_offset_bsz &
4428 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4429 			break;
4430 
4431 		/* clear next_to_watch to prevent false hangs */
4432 		tx_buf->next_to_watch = NULL;
4433 
4434 		tx_desc->buffer_addr = 0;
4435 		tx_desc->cmd_type_offset_bsz = 0;
4436 		/* move past filter desc */
4437 		tx_buf++;
4438 		tx_desc++;
4439 		i++;
4440 		if (unlikely(!i)) {
4441 			i -= tx_ring->count;
4442 			tx_buf = tx_ring->tx_bi;
4443 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4444 		}
4445 		/* unmap skb header data */
4446 		dma_unmap_single(tx_ring->dev,
4447 				 dma_unmap_addr(tx_buf, dma),
4448 				 dma_unmap_len(tx_buf, len),
4449 				 DMA_TO_DEVICE);
4450 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4451 			kfree(tx_buf->raw_buf);
4452 
4453 		tx_buf->raw_buf = NULL;
4454 		tx_buf->tx_flags = 0;
4455 		tx_buf->next_to_watch = NULL;
4456 		dma_unmap_len_set(tx_buf, len, 0);
4457 		tx_desc->buffer_addr = 0;
4458 		tx_desc->cmd_type_offset_bsz = 0;
4459 
4460 		/* move us past the eop_desc for start of next FD desc */
4461 		tx_buf++;
4462 		tx_desc++;
4463 		i++;
4464 		if (unlikely(!i)) {
4465 			i -= tx_ring->count;
4466 			tx_buf = tx_ring->tx_bi;
4467 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4468 		}
4469 
4470 		/* update budget accounting */
4471 		budget--;
4472 	} while (likely(budget));
4473 
4474 	i += tx_ring->count;
4475 	tx_ring->next_to_clean = i;
4476 
4477 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4478 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4479 
4480 	return budget > 0;
4481 }
4482 
4483 /**
4484  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4485  * @irq: interrupt number
4486  * @data: pointer to a q_vector
4487  **/
4488 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4489 {
4490 	struct i40e_q_vector *q_vector = data;
4491 	struct i40e_vsi *vsi;
4492 
4493 	if (!q_vector->tx.ring)
4494 		return IRQ_HANDLED;
4495 
4496 	vsi = q_vector->tx.ring->vsi;
4497 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4498 
4499 	return IRQ_HANDLED;
4500 }
4501 
4502 /**
4503  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4504  * @vsi: the VSI being configured
4505  * @v_idx: vector index
4506  * @qp_idx: queue pair index
4507  **/
4508 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4509 {
4510 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4511 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4512 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4513 
4514 	tx_ring->q_vector = q_vector;
4515 	tx_ring->next = q_vector->tx.ring;
4516 	q_vector->tx.ring = tx_ring;
4517 	q_vector->tx.count++;
4518 
4519 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4520 	if (i40e_enabled_xdp_vsi(vsi)) {
4521 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4522 
4523 		xdp_ring->q_vector = q_vector;
4524 		xdp_ring->next = q_vector->tx.ring;
4525 		q_vector->tx.ring = xdp_ring;
4526 		q_vector->tx.count++;
4527 	}
4528 
4529 	rx_ring->q_vector = q_vector;
4530 	rx_ring->next = q_vector->rx.ring;
4531 	q_vector->rx.ring = rx_ring;
4532 	q_vector->rx.count++;
4533 }
4534 
4535 /**
4536  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4537  * @vsi: the VSI being configured
4538  *
4539  * This function maps descriptor rings to the queue-specific vectors
4540  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4541  * one vector per queue pair, but on a constrained vector budget, we
4542  * group the queue pairs as "efficiently" as possible.
4543  **/
4544 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4545 {
4546 	int qp_remaining = vsi->num_queue_pairs;
4547 	int q_vectors = vsi->num_q_vectors;
4548 	int num_ringpairs;
4549 	int v_start = 0;
4550 	int qp_idx = 0;
4551 
4552 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4553 	 * group them so there are multiple queues per vector.
4554 	 * It is also important to go through all the vectors available to be
4555 	 * sure that if we don't use all the vectors, that the remaining vectors
4556 	 * are cleared. This is especially important when decreasing the
4557 	 * number of queues in use.
4558 	 */
4559 	for (; v_start < q_vectors; v_start++) {
4560 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4561 
4562 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4563 
4564 		q_vector->num_ringpairs = num_ringpairs;
4565 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4566 
4567 		q_vector->rx.count = 0;
4568 		q_vector->tx.count = 0;
4569 		q_vector->rx.ring = NULL;
4570 		q_vector->tx.ring = NULL;
4571 
4572 		while (num_ringpairs--) {
4573 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4574 			qp_idx++;
4575 			qp_remaining--;
4576 		}
4577 	}
4578 }
4579 
4580 /**
4581  * i40e_vsi_request_irq - Request IRQ from the OS
4582  * @vsi: the VSI being configured
4583  * @basename: name for the vector
4584  **/
4585 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4586 {
4587 	struct i40e_pf *pf = vsi->back;
4588 	int err;
4589 
4590 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4591 		err = i40e_vsi_request_irq_msix(vsi, basename);
4592 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4593 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4594 				  pf->int_name, pf);
4595 	else
4596 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4597 				  pf->int_name, pf);
4598 
4599 	if (err)
4600 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4601 
4602 	return err;
4603 }
4604 
4605 #ifdef CONFIG_NET_POLL_CONTROLLER
4606 /**
4607  * i40e_netpoll - A Polling 'interrupt' handler
4608  * @netdev: network interface device structure
4609  *
4610  * This is used by netconsole to send skbs without having to re-enable
4611  * interrupts.  It's not called while the normal interrupt routine is executing.
4612  **/
4613 static void i40e_netpoll(struct net_device *netdev)
4614 {
4615 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4616 	struct i40e_vsi *vsi = np->vsi;
4617 	struct i40e_pf *pf = vsi->back;
4618 	int i;
4619 
4620 	/* if interface is down do nothing */
4621 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4622 		return;
4623 
4624 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4625 		for (i = 0; i < vsi->num_q_vectors; i++)
4626 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4627 	} else {
4628 		i40e_intr(pf->pdev->irq, netdev);
4629 	}
4630 }
4631 #endif
4632 
4633 #define I40E_QTX_ENA_WAIT_COUNT 50
4634 
4635 /**
4636  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4637  * @pf: the PF being configured
4638  * @pf_q: the PF queue
4639  * @enable: enable or disable state of the queue
4640  *
4641  * This routine will wait for the given Tx queue of the PF to reach the
4642  * enabled or disabled state.
4643  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4644  * multiple retries; else will return 0 in case of success.
4645  **/
4646 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4647 {
4648 	int i;
4649 	u32 tx_reg;
4650 
4651 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4652 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4653 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4654 			break;
4655 
4656 		usleep_range(10, 20);
4657 	}
4658 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4659 		return -ETIMEDOUT;
4660 
4661 	return 0;
4662 }
4663 
4664 /**
4665  * i40e_control_tx_q - Start or stop a particular Tx queue
4666  * @pf: the PF structure
4667  * @pf_q: the PF queue to configure
4668  * @enable: start or stop the queue
4669  *
4670  * This function enables or disables a single queue. Note that any delay
4671  * required after the operation is expected to be handled by the caller of
4672  * this function.
4673  **/
4674 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4675 {
4676 	struct i40e_hw *hw = &pf->hw;
4677 	u32 tx_reg;
4678 	int i;
4679 
4680 	/* warn the TX unit of coming changes */
4681 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4682 	if (!enable)
4683 		usleep_range(10, 20);
4684 
4685 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4686 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4687 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4688 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4689 			break;
4690 		usleep_range(1000, 2000);
4691 	}
4692 
4693 	/* Skip if the queue is already in the requested state */
4694 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4695 		return;
4696 
4697 	/* turn on/off the queue */
4698 	if (enable) {
4699 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4700 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4701 	} else {
4702 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4703 	}
4704 
4705 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4706 }
4707 
4708 /**
4709  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4710  * @seid: VSI SEID
4711  * @pf: the PF structure
4712  * @pf_q: the PF queue to configure
4713  * @is_xdp: true if the queue is used for XDP
4714  * @enable: start or stop the queue
4715  **/
4716 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4717 			   bool is_xdp, bool enable)
4718 {
4719 	int ret;
4720 
4721 	i40e_control_tx_q(pf, pf_q, enable);
4722 
4723 	/* wait for the change to finish */
4724 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4725 	if (ret) {
4726 		dev_info(&pf->pdev->dev,
4727 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4728 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4729 			 (enable ? "en" : "dis"));
4730 	}
4731 
4732 	return ret;
4733 }
4734 
4735 /**
4736  * i40e_vsi_enable_tx - Start a VSI's rings
4737  * @vsi: the VSI being configured
4738  **/
4739 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4740 {
4741 	struct i40e_pf *pf = vsi->back;
4742 	int i, pf_q, ret = 0;
4743 
4744 	pf_q = vsi->base_queue;
4745 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4746 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4747 					     pf_q,
4748 					     false /*is xdp*/, true);
4749 		if (ret)
4750 			break;
4751 
4752 		if (!i40e_enabled_xdp_vsi(vsi))
4753 			continue;
4754 
4755 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4756 					     pf_q + vsi->alloc_queue_pairs,
4757 					     true /*is xdp*/, true);
4758 		if (ret)
4759 			break;
4760 	}
4761 	return ret;
4762 }
4763 
4764 /**
4765  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4766  * @pf: the PF being configured
4767  * @pf_q: the PF queue
4768  * @enable: enable or disable state of the queue
4769  *
4770  * This routine will wait for the given Rx queue of the PF to reach the
4771  * enabled or disabled state.
4772  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4773  * multiple retries; else will return 0 in case of success.
4774  **/
4775 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4776 {
4777 	int i;
4778 	u32 rx_reg;
4779 
4780 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4781 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4782 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4783 			break;
4784 
4785 		usleep_range(10, 20);
4786 	}
4787 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4788 		return -ETIMEDOUT;
4789 
4790 	return 0;
4791 }
4792 
4793 /**
4794  * i40e_control_rx_q - Start or stop a particular Rx queue
4795  * @pf: the PF structure
4796  * @pf_q: the PF queue to configure
4797  * @enable: start or stop the queue
4798  *
4799  * This function enables or disables a single queue. Note that
4800  * any delay required after the operation is expected to be
4801  * handled by the caller of this function.
4802  **/
4803 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4804 {
4805 	struct i40e_hw *hw = &pf->hw;
4806 	u32 rx_reg;
4807 	int i;
4808 
4809 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4810 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4811 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4812 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4813 			break;
4814 		usleep_range(1000, 2000);
4815 	}
4816 
4817 	/* Skip if the queue is already in the requested state */
4818 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4819 		return;
4820 
4821 	/* turn on/off the queue */
4822 	if (enable)
4823 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4824 	else
4825 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4826 
4827 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4828 }
4829 
4830 /**
4831  * i40e_control_wait_rx_q
4832  * @pf: the PF structure
4833  * @pf_q: queue being configured
4834  * @enable: start or stop the rings
4835  *
4836  * This function enables or disables a single queue along with waiting
4837  * for the change to finish. The caller of this function should handle
4838  * the delays needed in the case of disabling queues.
4839  **/
4840 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4841 {
4842 	int ret = 0;
4843 
4844 	i40e_control_rx_q(pf, pf_q, enable);
4845 
4846 	/* wait for the change to finish */
4847 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4848 	if (ret)
4849 		return ret;
4850 
4851 	return ret;
4852 }
4853 
4854 /**
4855  * i40e_vsi_enable_rx - Start a VSI's rings
4856  * @vsi: the VSI being configured
4857  **/
4858 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4859 {
4860 	struct i40e_pf *pf = vsi->back;
4861 	int i, pf_q, ret = 0;
4862 
4863 	pf_q = vsi->base_queue;
4864 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4865 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4866 		if (ret) {
4867 			dev_info(&pf->pdev->dev,
4868 				 "VSI seid %d Rx ring %d enable timeout\n",
4869 				 vsi->seid, pf_q);
4870 			break;
4871 		}
4872 	}
4873 
4874 	return ret;
4875 }
4876 
4877 /**
4878  * i40e_vsi_start_rings - Start a VSI's rings
4879  * @vsi: the VSI being configured
4880  **/
4881 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4882 {
4883 	int ret = 0;
4884 
4885 	/* do rx first for enable and last for disable */
4886 	ret = i40e_vsi_enable_rx(vsi);
4887 	if (ret)
4888 		return ret;
4889 	ret = i40e_vsi_enable_tx(vsi);
4890 
4891 	return ret;
4892 }
4893 
4894 #define I40E_DISABLE_TX_GAP_MSEC	50
4895 
4896 /**
4897  * i40e_vsi_stop_rings - Stop a VSI's rings
4898  * @vsi: the VSI being configured
4899  **/
4900 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4901 {
4902 	struct i40e_pf *pf = vsi->back;
4903 	int pf_q, err, q_end;
4904 
4905 	/* When port TX is suspended, don't wait */
4906 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4907 		return i40e_vsi_stop_rings_no_wait(vsi);
4908 
4909 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4910 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4911 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4912 
4913 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4914 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4915 		if (err)
4916 			dev_info(&pf->pdev->dev,
4917 				 "VSI seid %d Rx ring %d disable timeout\n",
4918 				 vsi->seid, pf_q);
4919 	}
4920 
4921 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4922 	pf_q = vsi->base_queue;
4923 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4924 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4925 
4926 	i40e_vsi_wait_queues_disabled(vsi);
4927 }
4928 
4929 /**
4930  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4931  * @vsi: the VSI being shutdown
4932  *
4933  * This function stops all the rings for a VSI but does not delay to verify
4934  * that rings have been disabled. It is expected that the caller is shutting
4935  * down multiple VSIs at once and will delay together for all the VSIs after
4936  * initiating the shutdown. This is particularly useful for shutting down lots
4937  * of VFs together. Otherwise, a large delay can be incurred while configuring
4938  * each VSI in serial.
4939  **/
4940 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4941 {
4942 	struct i40e_pf *pf = vsi->back;
4943 	int i, pf_q;
4944 
4945 	pf_q = vsi->base_queue;
4946 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4947 		i40e_control_tx_q(pf, pf_q, false);
4948 		i40e_control_rx_q(pf, pf_q, false);
4949 	}
4950 }
4951 
4952 /**
4953  * i40e_vsi_free_irq - Free the irq association with the OS
4954  * @vsi: the VSI being configured
4955  **/
4956 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4957 {
4958 	struct i40e_pf *pf = vsi->back;
4959 	struct i40e_hw *hw = &pf->hw;
4960 	int base = vsi->base_vector;
4961 	u32 val, qp;
4962 	int i;
4963 
4964 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4965 		if (!vsi->q_vectors)
4966 			return;
4967 
4968 		if (!vsi->irqs_ready)
4969 			return;
4970 
4971 		vsi->irqs_ready = false;
4972 		for (i = 0; i < vsi->num_q_vectors; i++) {
4973 			int irq_num;
4974 			u16 vector;
4975 
4976 			vector = i + base;
4977 			irq_num = pf->msix_entries[vector].vector;
4978 
4979 			/* free only the irqs that were actually requested */
4980 			if (!vsi->q_vectors[i] ||
4981 			    !vsi->q_vectors[i]->num_ringpairs)
4982 				continue;
4983 
4984 			/* clear the affinity notifier in the IRQ descriptor */
4985 			irq_set_affinity_notifier(irq_num, NULL);
4986 			/* remove our suggested affinity mask for this IRQ */
4987 			irq_update_affinity_hint(irq_num, NULL);
4988 			free_irq(irq_num, vsi->q_vectors[i]);
4989 
4990 			/* Tear down the interrupt queue link list
4991 			 *
4992 			 * We know that they come in pairs and always
4993 			 * the Rx first, then the Tx.  To clear the
4994 			 * link list, stick the EOL value into the
4995 			 * next_q field of the registers.
4996 			 */
4997 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4998 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4999 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5000 			val |= I40E_QUEUE_END_OF_LIST
5001 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5002 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
5003 
5004 			while (qp != I40E_QUEUE_END_OF_LIST) {
5005 				u32 next;
5006 
5007 				val = rd32(hw, I40E_QINT_RQCTL(qp));
5008 
5009 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5010 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5011 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5012 					 I40E_QINT_RQCTL_INTEVENT_MASK);
5013 
5014 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5015 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5016 
5017 				wr32(hw, I40E_QINT_RQCTL(qp), val);
5018 
5019 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5020 
5021 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5022 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5023 
5024 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5025 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5026 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5027 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5028 
5029 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5030 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5031 
5032 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5033 				qp = next;
5034 			}
5035 		}
5036 	} else {
5037 		free_irq(pf->pdev->irq, pf);
5038 
5039 		val = rd32(hw, I40E_PFINT_LNKLST0);
5040 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5041 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5042 		val |= I40E_QUEUE_END_OF_LIST
5043 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5044 		wr32(hw, I40E_PFINT_LNKLST0, val);
5045 
5046 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5047 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5048 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5049 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5050 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5051 
5052 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5053 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5054 
5055 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5056 
5057 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5058 
5059 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5060 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5061 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5062 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5063 
5064 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5065 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5066 
5067 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5068 	}
5069 }
5070 
5071 /**
5072  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5073  * @vsi: the VSI being configured
5074  * @v_idx: Index of vector to be freed
5075  *
5076  * This function frees the memory allocated to the q_vector.  In addition if
5077  * NAPI is enabled it will delete any references to the NAPI struct prior
5078  * to freeing the q_vector.
5079  **/
5080 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5081 {
5082 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5083 	struct i40e_ring *ring;
5084 
5085 	if (!q_vector)
5086 		return;
5087 
5088 	/* disassociate q_vector from rings */
5089 	i40e_for_each_ring(ring, q_vector->tx)
5090 		ring->q_vector = NULL;
5091 
5092 	i40e_for_each_ring(ring, q_vector->rx)
5093 		ring->q_vector = NULL;
5094 
5095 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5096 	if (vsi->netdev)
5097 		netif_napi_del(&q_vector->napi);
5098 
5099 	vsi->q_vectors[v_idx] = NULL;
5100 
5101 	kfree_rcu(q_vector, rcu);
5102 }
5103 
5104 /**
5105  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5106  * @vsi: the VSI being un-configured
5107  *
5108  * This frees the memory allocated to the q_vectors and
5109  * deletes references to the NAPI struct.
5110  **/
5111 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5112 {
5113 	int v_idx;
5114 
5115 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5116 		i40e_free_q_vector(vsi, v_idx);
5117 }
5118 
5119 /**
5120  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5121  * @pf: board private structure
5122  **/
5123 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5124 {
5125 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5126 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5127 		pci_disable_msix(pf->pdev);
5128 		kfree(pf->msix_entries);
5129 		pf->msix_entries = NULL;
5130 		kfree(pf->irq_pile);
5131 		pf->irq_pile = NULL;
5132 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5133 		pci_disable_msi(pf->pdev);
5134 	}
5135 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5136 }
5137 
5138 /**
5139  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5140  * @pf: board private structure
5141  *
5142  * We go through and clear interrupt specific resources and reset the structure
5143  * to pre-load conditions
5144  **/
5145 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5146 {
5147 	int i;
5148 
5149 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5150 		i40e_free_misc_vector(pf);
5151 
5152 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5153 		      I40E_IWARP_IRQ_PILE_ID);
5154 
5155 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5156 	for (i = 0; i < pf->num_alloc_vsi; i++)
5157 		if (pf->vsi[i])
5158 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5159 	i40e_reset_interrupt_capability(pf);
5160 }
5161 
5162 /**
5163  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5164  * @vsi: the VSI being configured
5165  **/
5166 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5167 {
5168 	int q_idx;
5169 
5170 	if (!vsi->netdev)
5171 		return;
5172 
5173 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5174 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5175 
5176 		if (q_vector->rx.ring || q_vector->tx.ring)
5177 			napi_enable(&q_vector->napi);
5178 	}
5179 }
5180 
5181 /**
5182  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5183  * @vsi: the VSI being configured
5184  **/
5185 static void i40e_napi_disable_all(struct i40e_vsi *vsi)
5186 {
5187 	int q_idx;
5188 
5189 	if (!vsi->netdev)
5190 		return;
5191 
5192 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5193 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5194 
5195 		if (q_vector->rx.ring || q_vector->tx.ring)
5196 			napi_disable(&q_vector->napi);
5197 	}
5198 }
5199 
5200 /**
5201  * i40e_vsi_close - Shut down a VSI
5202  * @vsi: the vsi to be quelled
5203  **/
5204 static void i40e_vsi_close(struct i40e_vsi *vsi)
5205 {
5206 	struct i40e_pf *pf = vsi->back;
5207 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5208 		i40e_down(vsi);
5209 	i40e_vsi_free_irq(vsi);
5210 	i40e_vsi_free_tx_resources(vsi);
5211 	i40e_vsi_free_rx_resources(vsi);
5212 	vsi->current_netdev_flags = 0;
5213 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5214 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5215 		set_bit(__I40E_CLIENT_RESET, pf->state);
5216 }
5217 
5218 /**
5219  * i40e_quiesce_vsi - Pause a given VSI
5220  * @vsi: the VSI being paused
5221  **/
5222 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5223 {
5224 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5225 		return;
5226 
5227 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5228 	if (vsi->netdev && netif_running(vsi->netdev))
5229 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5230 	else
5231 		i40e_vsi_close(vsi);
5232 }
5233 
5234 /**
5235  * i40e_unquiesce_vsi - Resume a given VSI
5236  * @vsi: the VSI being resumed
5237  **/
5238 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5239 {
5240 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5241 		return;
5242 
5243 	if (vsi->netdev && netif_running(vsi->netdev))
5244 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5245 	else
5246 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5247 }
5248 
5249 /**
5250  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5251  * @pf: the PF
5252  **/
5253 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5254 {
5255 	int v;
5256 
5257 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5258 		if (pf->vsi[v])
5259 			i40e_quiesce_vsi(pf->vsi[v]);
5260 	}
5261 }
5262 
5263 /**
5264  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5265  * @pf: the PF
5266  **/
5267 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5268 {
5269 	int v;
5270 
5271 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5272 		if (pf->vsi[v])
5273 			i40e_unquiesce_vsi(pf->vsi[v]);
5274 	}
5275 }
5276 
5277 /**
5278  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5279  * @vsi: the VSI being configured
5280  *
5281  * Wait until all queues on a given VSI have been disabled.
5282  **/
5283 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5284 {
5285 	struct i40e_pf *pf = vsi->back;
5286 	int i, pf_q, ret;
5287 
5288 	pf_q = vsi->base_queue;
5289 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5290 		/* Check and wait for the Tx queue */
5291 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5292 		if (ret) {
5293 			dev_info(&pf->pdev->dev,
5294 				 "VSI seid %d Tx ring %d disable timeout\n",
5295 				 vsi->seid, pf_q);
5296 			return ret;
5297 		}
5298 
5299 		if (!i40e_enabled_xdp_vsi(vsi))
5300 			goto wait_rx;
5301 
5302 		/* Check and wait for the XDP Tx queue */
5303 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5304 				       false);
5305 		if (ret) {
5306 			dev_info(&pf->pdev->dev,
5307 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5308 				 vsi->seid, pf_q);
5309 			return ret;
5310 		}
5311 wait_rx:
5312 		/* Check and wait for the Rx queue */
5313 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5314 		if (ret) {
5315 			dev_info(&pf->pdev->dev,
5316 				 "VSI seid %d Rx ring %d disable timeout\n",
5317 				 vsi->seid, pf_q);
5318 			return ret;
5319 		}
5320 	}
5321 
5322 	return 0;
5323 }
5324 
5325 #ifdef CONFIG_I40E_DCB
5326 /**
5327  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5328  * @pf: the PF
5329  *
5330  * This function waits for the queues to be in disabled state for all the
5331  * VSIs that are managed by this PF.
5332  **/
5333 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5334 {
5335 	int v, ret = 0;
5336 
5337 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5338 		if (pf->vsi[v]) {
5339 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5340 			if (ret)
5341 				break;
5342 		}
5343 	}
5344 
5345 	return ret;
5346 }
5347 
5348 #endif
5349 
5350 /**
5351  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5352  * @pf: pointer to PF
5353  *
5354  * Get TC map for ISCSI PF type that will include iSCSI TC
5355  * and LAN TC.
5356  **/
5357 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5358 {
5359 	struct i40e_dcb_app_priority_table app;
5360 	struct i40e_hw *hw = &pf->hw;
5361 	u8 enabled_tc = 1; /* TC0 is always enabled */
5362 	u8 tc, i;
5363 	/* Get the iSCSI APP TLV */
5364 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5365 
5366 	for (i = 0; i < dcbcfg->numapps; i++) {
5367 		app = dcbcfg->app[i];
5368 		if (app.selector == I40E_APP_SEL_TCPIP &&
5369 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5370 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5371 			enabled_tc |= BIT(tc);
5372 			break;
5373 		}
5374 	}
5375 
5376 	return enabled_tc;
5377 }
5378 
5379 /**
5380  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5381  * @dcbcfg: the corresponding DCBx configuration structure
5382  *
5383  * Return the number of TCs from given DCBx configuration
5384  **/
5385 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5386 {
5387 	int i, tc_unused = 0;
5388 	u8 num_tc = 0;
5389 	u8 ret = 0;
5390 
5391 	/* Scan the ETS Config Priority Table to find
5392 	 * traffic class enabled for a given priority
5393 	 * and create a bitmask of enabled TCs
5394 	 */
5395 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5396 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5397 
5398 	/* Now scan the bitmask to check for
5399 	 * contiguous TCs starting with TC0
5400 	 */
5401 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5402 		if (num_tc & BIT(i)) {
5403 			if (!tc_unused) {
5404 				ret++;
5405 			} else {
5406 				pr_err("Non-contiguous TC - Disabling DCB\n");
5407 				return 1;
5408 			}
5409 		} else {
5410 			tc_unused = 1;
5411 		}
5412 	}
5413 
5414 	/* There is always at least TC0 */
5415 	if (!ret)
5416 		ret = 1;
5417 
5418 	return ret;
5419 }
5420 
5421 /**
5422  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5423  * @dcbcfg: the corresponding DCBx configuration structure
5424  *
5425  * Query the current DCB configuration and return the number of
5426  * traffic classes enabled from the given DCBX config
5427  **/
5428 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5429 {
5430 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5431 	u8 enabled_tc = 1;
5432 	u8 i;
5433 
5434 	for (i = 0; i < num_tc; i++)
5435 		enabled_tc |= BIT(i);
5436 
5437 	return enabled_tc;
5438 }
5439 
5440 /**
5441  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5442  * @pf: PF being queried
5443  *
5444  * Query the current MQPRIO configuration and return the number of
5445  * traffic classes enabled.
5446  **/
5447 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5448 {
5449 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5450 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5451 	u8 enabled_tc = 1, i;
5452 
5453 	for (i = 1; i < num_tc; i++)
5454 		enabled_tc |= BIT(i);
5455 	return enabled_tc;
5456 }
5457 
5458 /**
5459  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5460  * @pf: PF being queried
5461  *
5462  * Return number of traffic classes enabled for the given PF
5463  **/
5464 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5465 {
5466 	struct i40e_hw *hw = &pf->hw;
5467 	u8 i, enabled_tc = 1;
5468 	u8 num_tc = 0;
5469 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5470 
5471 	if (i40e_is_tc_mqprio_enabled(pf))
5472 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5473 
5474 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5475 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5476 		return 1;
5477 
5478 	/* SFP mode will be enabled for all TCs on port */
5479 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5480 		return i40e_dcb_get_num_tc(dcbcfg);
5481 
5482 	/* MFP mode return count of enabled TCs for this PF */
5483 	if (pf->hw.func_caps.iscsi)
5484 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5485 	else
5486 		return 1; /* Only TC0 */
5487 
5488 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5489 		if (enabled_tc & BIT(i))
5490 			num_tc++;
5491 	}
5492 	return num_tc;
5493 }
5494 
5495 /**
5496  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5497  * @pf: PF being queried
5498  *
5499  * Return a bitmap for enabled traffic classes for this PF.
5500  **/
5501 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5502 {
5503 	if (i40e_is_tc_mqprio_enabled(pf))
5504 		return i40e_mqprio_get_enabled_tc(pf);
5505 
5506 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5507 	 * default TC
5508 	 */
5509 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5510 		return I40E_DEFAULT_TRAFFIC_CLASS;
5511 
5512 	/* SFP mode we want PF to be enabled for all TCs */
5513 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5514 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5515 
5516 	/* MFP enabled and iSCSI PF type */
5517 	if (pf->hw.func_caps.iscsi)
5518 		return i40e_get_iscsi_tc_map(pf);
5519 	else
5520 		return I40E_DEFAULT_TRAFFIC_CLASS;
5521 }
5522 
5523 /**
5524  * i40e_vsi_get_bw_info - Query VSI BW Information
5525  * @vsi: the VSI being queried
5526  *
5527  * Returns 0 on success, negative value on failure
5528  **/
5529 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5530 {
5531 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5532 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5533 	struct i40e_pf *pf = vsi->back;
5534 	struct i40e_hw *hw = &pf->hw;
5535 	u32 tc_bw_max;
5536 	int ret;
5537 	int i;
5538 
5539 	/* Get the VSI level BW configuration */
5540 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5541 	if (ret) {
5542 		dev_info(&pf->pdev->dev,
5543 			 "couldn't get PF vsi bw config, err %pe aq_err %s\n",
5544 			 ERR_PTR(ret),
5545 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5546 		return -EINVAL;
5547 	}
5548 
5549 	/* Get the VSI level BW configuration per TC */
5550 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5551 					       NULL);
5552 	if (ret) {
5553 		dev_info(&pf->pdev->dev,
5554 			 "couldn't get PF vsi ets bw config, err %pe aq_err %s\n",
5555 			 ERR_PTR(ret),
5556 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5557 		return -EINVAL;
5558 	}
5559 
5560 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5561 		dev_info(&pf->pdev->dev,
5562 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5563 			 bw_config.tc_valid_bits,
5564 			 bw_ets_config.tc_valid_bits);
5565 		/* Still continuing */
5566 	}
5567 
5568 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5569 	vsi->bw_max_quanta = bw_config.max_bw;
5570 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5571 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5572 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5573 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5574 		vsi->bw_ets_limit_credits[i] =
5575 					le16_to_cpu(bw_ets_config.credits[i]);
5576 		/* 3 bits out of 4 for each TC */
5577 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5578 	}
5579 
5580 	return 0;
5581 }
5582 
5583 /**
5584  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5585  * @vsi: the VSI being configured
5586  * @enabled_tc: TC bitmap
5587  * @bw_share: BW shared credits per TC
5588  *
5589  * Returns 0 on success, negative value on failure
5590  **/
5591 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5592 				       u8 *bw_share)
5593 {
5594 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5595 	struct i40e_pf *pf = vsi->back;
5596 	int ret;
5597 	int i;
5598 
5599 	/* There is no need to reset BW when mqprio mode is on.  */
5600 	if (i40e_is_tc_mqprio_enabled(pf))
5601 		return 0;
5602 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5603 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5604 		if (ret)
5605 			dev_info(&pf->pdev->dev,
5606 				 "Failed to reset tx rate for vsi->seid %u\n",
5607 				 vsi->seid);
5608 		return ret;
5609 	}
5610 	memset(&bw_data, 0, sizeof(bw_data));
5611 	bw_data.tc_valid_bits = enabled_tc;
5612 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5613 		bw_data.tc_bw_credits[i] = bw_share[i];
5614 
5615 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5616 	if (ret) {
5617 		dev_info(&pf->pdev->dev,
5618 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5619 			 pf->hw.aq.asq_last_status);
5620 		return -EINVAL;
5621 	}
5622 
5623 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5624 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5625 
5626 	return 0;
5627 }
5628 
5629 /**
5630  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5631  * @vsi: the VSI being configured
5632  * @enabled_tc: TC map to be enabled
5633  *
5634  **/
5635 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5636 {
5637 	struct net_device *netdev = vsi->netdev;
5638 	struct i40e_pf *pf = vsi->back;
5639 	struct i40e_hw *hw = &pf->hw;
5640 	u8 netdev_tc = 0;
5641 	int i;
5642 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5643 
5644 	if (!netdev)
5645 		return;
5646 
5647 	if (!enabled_tc) {
5648 		netdev_reset_tc(netdev);
5649 		return;
5650 	}
5651 
5652 	/* Set up actual enabled TCs on the VSI */
5653 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5654 		return;
5655 
5656 	/* set per TC queues for the VSI */
5657 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5658 		/* Only set TC queues for enabled tcs
5659 		 *
5660 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5661 		 * enabled_tc bitmap would be 0x00001001; the driver
5662 		 * will set the numtc for netdev as 2 that will be
5663 		 * referenced by the netdev layer as TC 0 and 1.
5664 		 */
5665 		if (vsi->tc_config.enabled_tc & BIT(i))
5666 			netdev_set_tc_queue(netdev,
5667 					vsi->tc_config.tc_info[i].netdev_tc,
5668 					vsi->tc_config.tc_info[i].qcount,
5669 					vsi->tc_config.tc_info[i].qoffset);
5670 	}
5671 
5672 	if (i40e_is_tc_mqprio_enabled(pf))
5673 		return;
5674 
5675 	/* Assign UP2TC map for the VSI */
5676 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5677 		/* Get the actual TC# for the UP */
5678 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5679 		/* Get the mapped netdev TC# for the UP */
5680 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5681 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5682 	}
5683 }
5684 
5685 /**
5686  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5687  * @vsi: the VSI being configured
5688  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5689  **/
5690 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5691 				      struct i40e_vsi_context *ctxt)
5692 {
5693 	/* copy just the sections touched not the entire info
5694 	 * since not all sections are valid as returned by
5695 	 * update vsi params
5696 	 */
5697 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5698 	memcpy(&vsi->info.queue_mapping,
5699 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5700 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5701 	       sizeof(vsi->info.tc_mapping));
5702 }
5703 
5704 /**
5705  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5706  * @vsi: the VSI being reconfigured
5707  * @vsi_offset: offset from main VF VSI
5708  */
5709 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5710 {
5711 	struct i40e_vsi_context ctxt = {};
5712 	struct i40e_pf *pf;
5713 	struct i40e_hw *hw;
5714 	int ret;
5715 
5716 	if (!vsi)
5717 		return I40E_ERR_PARAM;
5718 	pf = vsi->back;
5719 	hw = &pf->hw;
5720 
5721 	ctxt.seid = vsi->seid;
5722 	ctxt.pf_num = hw->pf_id;
5723 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5724 	ctxt.uplink_seid = vsi->uplink_seid;
5725 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5726 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5727 	ctxt.info = vsi->info;
5728 
5729 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5730 				 false);
5731 	if (vsi->reconfig_rss) {
5732 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5733 				      vsi->num_queue_pairs);
5734 		ret = i40e_vsi_config_rss(vsi);
5735 		if (ret) {
5736 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5737 			return ret;
5738 		}
5739 		vsi->reconfig_rss = false;
5740 	}
5741 
5742 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5743 	if (ret) {
5744 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %pe aq_err %s\n",
5745 			 ERR_PTR(ret),
5746 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5747 		return ret;
5748 	}
5749 	/* update the local VSI info with updated queue map */
5750 	i40e_vsi_update_queue_map(vsi, &ctxt);
5751 	vsi->info.valid_sections = 0;
5752 
5753 	return ret;
5754 }
5755 
5756 /**
5757  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5758  * @vsi: VSI to be configured
5759  * @enabled_tc: TC bitmap
5760  *
5761  * This configures a particular VSI for TCs that are mapped to the
5762  * given TC bitmap. It uses default bandwidth share for TCs across
5763  * VSIs to configure TC for a particular VSI.
5764  *
5765  * NOTE:
5766  * It is expected that the VSI queues have been quisced before calling
5767  * this function.
5768  **/
5769 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5770 {
5771 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5772 	struct i40e_pf *pf = vsi->back;
5773 	struct i40e_hw *hw = &pf->hw;
5774 	struct i40e_vsi_context ctxt;
5775 	int ret = 0;
5776 	int i;
5777 
5778 	/* Check if enabled_tc is same as existing or new TCs */
5779 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5780 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5781 		return ret;
5782 
5783 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5784 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5785 		if (enabled_tc & BIT(i))
5786 			bw_share[i] = 1;
5787 	}
5788 
5789 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5790 	if (ret) {
5791 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5792 
5793 		dev_info(&pf->pdev->dev,
5794 			 "Failed configuring TC map %d for VSI %d\n",
5795 			 enabled_tc, vsi->seid);
5796 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5797 						  &bw_config, NULL);
5798 		if (ret) {
5799 			dev_info(&pf->pdev->dev,
5800 				 "Failed querying vsi bw info, err %pe aq_err %s\n",
5801 				 ERR_PTR(ret),
5802 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5803 			goto out;
5804 		}
5805 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5806 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5807 
5808 			if (!valid_tc)
5809 				valid_tc = bw_config.tc_valid_bits;
5810 			/* Always enable TC0, no matter what */
5811 			valid_tc |= 1;
5812 			dev_info(&pf->pdev->dev,
5813 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5814 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5815 			enabled_tc = valid_tc;
5816 		}
5817 
5818 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5819 		if (ret) {
5820 			dev_err(&pf->pdev->dev,
5821 				"Unable to  configure TC map %d for VSI %d\n",
5822 				enabled_tc, vsi->seid);
5823 			goto out;
5824 		}
5825 	}
5826 
5827 	/* Update Queue Pairs Mapping for currently enabled UPs */
5828 	ctxt.seid = vsi->seid;
5829 	ctxt.pf_num = vsi->back->hw.pf_id;
5830 	ctxt.vf_num = 0;
5831 	ctxt.uplink_seid = vsi->uplink_seid;
5832 	ctxt.info = vsi->info;
5833 	if (i40e_is_tc_mqprio_enabled(pf)) {
5834 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5835 		if (ret)
5836 			goto out;
5837 	} else {
5838 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5839 	}
5840 
5841 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5842 	 * queues changed.
5843 	 */
5844 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5845 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5846 				      vsi->num_queue_pairs);
5847 		ret = i40e_vsi_config_rss(vsi);
5848 		if (ret) {
5849 			dev_info(&vsi->back->pdev->dev,
5850 				 "Failed to reconfig rss for num_queues\n");
5851 			return ret;
5852 		}
5853 		vsi->reconfig_rss = false;
5854 	}
5855 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5856 		ctxt.info.valid_sections |=
5857 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5858 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5859 	}
5860 
5861 	/* Update the VSI after updating the VSI queue-mapping
5862 	 * information
5863 	 */
5864 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5865 	if (ret) {
5866 		dev_info(&pf->pdev->dev,
5867 			 "Update vsi tc config failed, err %pe aq_err %s\n",
5868 			 ERR_PTR(ret),
5869 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5870 		goto out;
5871 	}
5872 	/* update the local VSI info with updated queue map */
5873 	i40e_vsi_update_queue_map(vsi, &ctxt);
5874 	vsi->info.valid_sections = 0;
5875 
5876 	/* Update current VSI BW information */
5877 	ret = i40e_vsi_get_bw_info(vsi);
5878 	if (ret) {
5879 		dev_info(&pf->pdev->dev,
5880 			 "Failed updating vsi bw info, err %pe aq_err %s\n",
5881 			 ERR_PTR(ret),
5882 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5883 		goto out;
5884 	}
5885 
5886 	/* Update the netdev TC setup */
5887 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5888 out:
5889 	return ret;
5890 }
5891 
5892 /**
5893  * i40e_get_link_speed - Returns link speed for the interface
5894  * @vsi: VSI to be configured
5895  *
5896  **/
5897 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5898 {
5899 	struct i40e_pf *pf = vsi->back;
5900 
5901 	switch (pf->hw.phy.link_info.link_speed) {
5902 	case I40E_LINK_SPEED_40GB:
5903 		return 40000;
5904 	case I40E_LINK_SPEED_25GB:
5905 		return 25000;
5906 	case I40E_LINK_SPEED_20GB:
5907 		return 20000;
5908 	case I40E_LINK_SPEED_10GB:
5909 		return 10000;
5910 	case I40E_LINK_SPEED_1GB:
5911 		return 1000;
5912 	default:
5913 		return -EINVAL;
5914 	}
5915 }
5916 
5917 /**
5918  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5919  * @vsi: Pointer to vsi structure
5920  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5921  *
5922  * Helper function to convert units before send to set BW limit
5923  **/
5924 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5925 {
5926 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5927 		dev_warn(&vsi->back->pdev->dev,
5928 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5929 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5930 	} else {
5931 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5932 	}
5933 
5934 	return max_tx_rate;
5935 }
5936 
5937 /**
5938  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5939  * @vsi: VSI to be configured
5940  * @seid: seid of the channel/VSI
5941  * @max_tx_rate: max TX rate to be configured as BW limit
5942  *
5943  * Helper function to set BW limit for a given VSI
5944  **/
5945 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5946 {
5947 	struct i40e_pf *pf = vsi->back;
5948 	u64 credits = 0;
5949 	int speed = 0;
5950 	int ret = 0;
5951 
5952 	speed = i40e_get_link_speed(vsi);
5953 	if (max_tx_rate > speed) {
5954 		dev_err(&pf->pdev->dev,
5955 			"Invalid max tx rate %llu specified for VSI seid %d.",
5956 			max_tx_rate, seid);
5957 		return -EINVAL;
5958 	}
5959 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5960 		dev_warn(&pf->pdev->dev,
5961 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5962 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5963 	}
5964 
5965 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5966 	credits = max_tx_rate;
5967 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5968 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5969 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5970 	if (ret)
5971 		dev_err(&pf->pdev->dev,
5972 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %pe aq_err %s\n",
5973 			max_tx_rate, seid, ERR_PTR(ret),
5974 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5975 	return ret;
5976 }
5977 
5978 /**
5979  * i40e_remove_queue_channels - Remove queue channels for the TCs
5980  * @vsi: VSI to be configured
5981  *
5982  * Remove queue channels for the TCs
5983  **/
5984 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5985 {
5986 	enum i40e_admin_queue_err last_aq_status;
5987 	struct i40e_cloud_filter *cfilter;
5988 	struct i40e_channel *ch, *ch_tmp;
5989 	struct i40e_pf *pf = vsi->back;
5990 	struct hlist_node *node;
5991 	int ret, i;
5992 
5993 	/* Reset rss size that was stored when reconfiguring rss for
5994 	 * channel VSIs with non-power-of-2 queue count.
5995 	 */
5996 	vsi->current_rss_size = 0;
5997 
5998 	/* perform cleanup for channels if they exist */
5999 	if (list_empty(&vsi->ch_list))
6000 		return;
6001 
6002 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6003 		struct i40e_vsi *p_vsi;
6004 
6005 		list_del(&ch->list);
6006 		p_vsi = ch->parent_vsi;
6007 		if (!p_vsi || !ch->initialized) {
6008 			kfree(ch);
6009 			continue;
6010 		}
6011 		/* Reset queue contexts */
6012 		for (i = 0; i < ch->num_queue_pairs; i++) {
6013 			struct i40e_ring *tx_ring, *rx_ring;
6014 			u16 pf_q;
6015 
6016 			pf_q = ch->base_queue + i;
6017 			tx_ring = vsi->tx_rings[pf_q];
6018 			tx_ring->ch = NULL;
6019 
6020 			rx_ring = vsi->rx_rings[pf_q];
6021 			rx_ring->ch = NULL;
6022 		}
6023 
6024 		/* Reset BW configured for this VSI via mqprio */
6025 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6026 		if (ret)
6027 			dev_info(&vsi->back->pdev->dev,
6028 				 "Failed to reset tx rate for ch->seid %u\n",
6029 				 ch->seid);
6030 
6031 		/* delete cloud filters associated with this channel */
6032 		hlist_for_each_entry_safe(cfilter, node,
6033 					  &pf->cloud_filter_list, cloud_node) {
6034 			if (cfilter->seid != ch->seid)
6035 				continue;
6036 
6037 			hash_del(&cfilter->cloud_node);
6038 			if (cfilter->dst_port)
6039 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6040 									cfilter,
6041 									false);
6042 			else
6043 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6044 								false);
6045 			last_aq_status = pf->hw.aq.asq_last_status;
6046 			if (ret)
6047 				dev_info(&pf->pdev->dev,
6048 					 "Failed to delete cloud filter, err %pe aq_err %s\n",
6049 					 ERR_PTR(ret),
6050 					 i40e_aq_str(&pf->hw, last_aq_status));
6051 			kfree(cfilter);
6052 		}
6053 
6054 		/* delete VSI from FW */
6055 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6056 					     NULL);
6057 		if (ret)
6058 			dev_err(&vsi->back->pdev->dev,
6059 				"unable to remove channel (%d) for parent VSI(%d)\n",
6060 				ch->seid, p_vsi->seid);
6061 		kfree(ch);
6062 	}
6063 	INIT_LIST_HEAD(&vsi->ch_list);
6064 }
6065 
6066 /**
6067  * i40e_get_max_queues_for_channel
6068  * @vsi: ptr to VSI to which channels are associated with
6069  *
6070  * Helper function which returns max value among the queue counts set on the
6071  * channels/TCs created.
6072  **/
6073 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6074 {
6075 	struct i40e_channel *ch, *ch_tmp;
6076 	int max = 0;
6077 
6078 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6079 		if (!ch->initialized)
6080 			continue;
6081 		if (ch->num_queue_pairs > max)
6082 			max = ch->num_queue_pairs;
6083 	}
6084 
6085 	return max;
6086 }
6087 
6088 /**
6089  * i40e_validate_num_queues - validate num_queues w.r.t channel
6090  * @pf: ptr to PF device
6091  * @num_queues: number of queues
6092  * @vsi: the parent VSI
6093  * @reconfig_rss: indicates should the RSS be reconfigured or not
6094  *
6095  * This function validates number of queues in the context of new channel
6096  * which is being established and determines if RSS should be reconfigured
6097  * or not for parent VSI.
6098  **/
6099 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6100 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6101 {
6102 	int max_ch_queues;
6103 
6104 	if (!reconfig_rss)
6105 		return -EINVAL;
6106 
6107 	*reconfig_rss = false;
6108 	if (vsi->current_rss_size) {
6109 		if (num_queues > vsi->current_rss_size) {
6110 			dev_dbg(&pf->pdev->dev,
6111 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6112 				num_queues, vsi->current_rss_size);
6113 			return -EINVAL;
6114 		} else if ((num_queues < vsi->current_rss_size) &&
6115 			   (!is_power_of_2(num_queues))) {
6116 			dev_dbg(&pf->pdev->dev,
6117 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6118 				num_queues, vsi->current_rss_size);
6119 			return -EINVAL;
6120 		}
6121 	}
6122 
6123 	if (!is_power_of_2(num_queues)) {
6124 		/* Find the max num_queues configured for channel if channel
6125 		 * exist.
6126 		 * if channel exist, then enforce 'num_queues' to be more than
6127 		 * max ever queues configured for channel.
6128 		 */
6129 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6130 		if (num_queues < max_ch_queues) {
6131 			dev_dbg(&pf->pdev->dev,
6132 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6133 				num_queues, max_ch_queues);
6134 			return -EINVAL;
6135 		}
6136 		*reconfig_rss = true;
6137 	}
6138 
6139 	return 0;
6140 }
6141 
6142 /**
6143  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6144  * @vsi: the VSI being setup
6145  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6146  *
6147  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6148  **/
6149 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6150 {
6151 	struct i40e_pf *pf = vsi->back;
6152 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6153 	struct i40e_hw *hw = &pf->hw;
6154 	int local_rss_size;
6155 	u8 *lut;
6156 	int ret;
6157 
6158 	if (!vsi->rss_size)
6159 		return -EINVAL;
6160 
6161 	if (rss_size > vsi->rss_size)
6162 		return -EINVAL;
6163 
6164 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6165 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6166 	if (!lut)
6167 		return -ENOMEM;
6168 
6169 	/* Ignoring user configured lut if there is one */
6170 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6171 
6172 	/* Use user configured hash key if there is one, otherwise
6173 	 * use default.
6174 	 */
6175 	if (vsi->rss_hkey_user)
6176 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6177 	else
6178 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6179 
6180 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6181 	if (ret) {
6182 		dev_info(&pf->pdev->dev,
6183 			 "Cannot set RSS lut, err %pe aq_err %s\n",
6184 			 ERR_PTR(ret),
6185 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6186 		kfree(lut);
6187 		return ret;
6188 	}
6189 	kfree(lut);
6190 
6191 	/* Do the update w.r.t. storing rss_size */
6192 	if (!vsi->orig_rss_size)
6193 		vsi->orig_rss_size = vsi->rss_size;
6194 	vsi->current_rss_size = local_rss_size;
6195 
6196 	return ret;
6197 }
6198 
6199 /**
6200  * i40e_channel_setup_queue_map - Setup a channel queue map
6201  * @pf: ptr to PF device
6202  * @ctxt: VSI context structure
6203  * @ch: ptr to channel structure
6204  *
6205  * Setup queue map for a specific channel
6206  **/
6207 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6208 					 struct i40e_vsi_context *ctxt,
6209 					 struct i40e_channel *ch)
6210 {
6211 	u16 qcount, qmap, sections = 0;
6212 	u8 offset = 0;
6213 	int pow;
6214 
6215 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6216 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6217 
6218 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6219 	ch->num_queue_pairs = qcount;
6220 
6221 	/* find the next higher power-of-2 of num queue pairs */
6222 	pow = ilog2(qcount);
6223 	if (!is_power_of_2(qcount))
6224 		pow++;
6225 
6226 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6227 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6228 
6229 	/* Setup queue TC[0].qmap for given VSI context */
6230 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6231 
6232 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6233 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6234 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6235 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6236 }
6237 
6238 /**
6239  * i40e_add_channel - add a channel by adding VSI
6240  * @pf: ptr to PF device
6241  * @uplink_seid: underlying HW switching element (VEB) ID
6242  * @ch: ptr to channel structure
6243  *
6244  * Add a channel (VSI) using add_vsi and queue_map
6245  **/
6246 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6247 			    struct i40e_channel *ch)
6248 {
6249 	struct i40e_hw *hw = &pf->hw;
6250 	struct i40e_vsi_context ctxt;
6251 	u8 enabled_tc = 0x1; /* TC0 enabled */
6252 	int ret;
6253 
6254 	if (ch->type != I40E_VSI_VMDQ2) {
6255 		dev_info(&pf->pdev->dev,
6256 			 "add new vsi failed, ch->type %d\n", ch->type);
6257 		return -EINVAL;
6258 	}
6259 
6260 	memset(&ctxt, 0, sizeof(ctxt));
6261 	ctxt.pf_num = hw->pf_id;
6262 	ctxt.vf_num = 0;
6263 	ctxt.uplink_seid = uplink_seid;
6264 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6265 	if (ch->type == I40E_VSI_VMDQ2)
6266 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6267 
6268 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6269 		ctxt.info.valid_sections |=
6270 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6271 		ctxt.info.switch_id =
6272 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6273 	}
6274 
6275 	/* Set queue map for a given VSI context */
6276 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6277 
6278 	/* Now time to create VSI */
6279 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6280 	if (ret) {
6281 		dev_info(&pf->pdev->dev,
6282 			 "add new vsi failed, err %pe aq_err %s\n",
6283 			 ERR_PTR(ret),
6284 			 i40e_aq_str(&pf->hw,
6285 				     pf->hw.aq.asq_last_status));
6286 		return -ENOENT;
6287 	}
6288 
6289 	/* Success, update channel, set enabled_tc only if the channel
6290 	 * is not a macvlan
6291 	 */
6292 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6293 	ch->seid = ctxt.seid;
6294 	ch->vsi_number = ctxt.vsi_number;
6295 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6296 
6297 	/* copy just the sections touched not the entire info
6298 	 * since not all sections are valid as returned by
6299 	 * update vsi params
6300 	 */
6301 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6302 	memcpy(&ch->info.queue_mapping,
6303 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6304 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6305 	       sizeof(ctxt.info.tc_mapping));
6306 
6307 	return 0;
6308 }
6309 
6310 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6311 				  u8 *bw_share)
6312 {
6313 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6314 	int ret;
6315 	int i;
6316 
6317 	memset(&bw_data, 0, sizeof(bw_data));
6318 	bw_data.tc_valid_bits = ch->enabled_tc;
6319 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6320 		bw_data.tc_bw_credits[i] = bw_share[i];
6321 
6322 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6323 				       &bw_data, NULL);
6324 	if (ret) {
6325 		dev_info(&vsi->back->pdev->dev,
6326 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6327 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6328 		return -EINVAL;
6329 	}
6330 
6331 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6332 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6333 
6334 	return 0;
6335 }
6336 
6337 /**
6338  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6339  * @pf: ptr to PF device
6340  * @vsi: the VSI being setup
6341  * @ch: ptr to channel structure
6342  *
6343  * Configure TX rings associated with channel (VSI) since queues are being
6344  * from parent VSI.
6345  **/
6346 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6347 				       struct i40e_vsi *vsi,
6348 				       struct i40e_channel *ch)
6349 {
6350 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6351 	int ret;
6352 	int i;
6353 
6354 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6355 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6356 		if (ch->enabled_tc & BIT(i))
6357 			bw_share[i] = 1;
6358 	}
6359 
6360 	/* configure BW for new VSI */
6361 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6362 	if (ret) {
6363 		dev_info(&vsi->back->pdev->dev,
6364 			 "Failed configuring TC map %d for channel (seid %u)\n",
6365 			 ch->enabled_tc, ch->seid);
6366 		return ret;
6367 	}
6368 
6369 	for (i = 0; i < ch->num_queue_pairs; i++) {
6370 		struct i40e_ring *tx_ring, *rx_ring;
6371 		u16 pf_q;
6372 
6373 		pf_q = ch->base_queue + i;
6374 
6375 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6376 		 * context
6377 		 */
6378 		tx_ring = vsi->tx_rings[pf_q];
6379 		tx_ring->ch = ch;
6380 
6381 		/* Get the RX ring ptr */
6382 		rx_ring = vsi->rx_rings[pf_q];
6383 		rx_ring->ch = ch;
6384 	}
6385 
6386 	return 0;
6387 }
6388 
6389 /**
6390  * i40e_setup_hw_channel - setup new channel
6391  * @pf: ptr to PF device
6392  * @vsi: the VSI being setup
6393  * @ch: ptr to channel structure
6394  * @uplink_seid: underlying HW switching element (VEB) ID
6395  * @type: type of channel to be created (VMDq2/VF)
6396  *
6397  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6398  * and configures TX rings accordingly
6399  **/
6400 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6401 					struct i40e_vsi *vsi,
6402 					struct i40e_channel *ch,
6403 					u16 uplink_seid, u8 type)
6404 {
6405 	int ret;
6406 
6407 	ch->initialized = false;
6408 	ch->base_queue = vsi->next_base_queue;
6409 	ch->type = type;
6410 
6411 	/* Proceed with creation of channel (VMDq2) VSI */
6412 	ret = i40e_add_channel(pf, uplink_seid, ch);
6413 	if (ret) {
6414 		dev_info(&pf->pdev->dev,
6415 			 "failed to add_channel using uplink_seid %u\n",
6416 			 uplink_seid);
6417 		return ret;
6418 	}
6419 
6420 	/* Mark the successful creation of channel */
6421 	ch->initialized = true;
6422 
6423 	/* Reconfigure TX queues using QTX_CTL register */
6424 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6425 	if (ret) {
6426 		dev_info(&pf->pdev->dev,
6427 			 "failed to configure TX rings for channel %u\n",
6428 			 ch->seid);
6429 		return ret;
6430 	}
6431 
6432 	/* update 'next_base_queue' */
6433 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6434 	dev_dbg(&pf->pdev->dev,
6435 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6436 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6437 		ch->num_queue_pairs,
6438 		vsi->next_base_queue);
6439 	return ret;
6440 }
6441 
6442 /**
6443  * i40e_setup_channel - setup new channel using uplink element
6444  * @pf: ptr to PF device
6445  * @vsi: pointer to the VSI to set up the channel within
6446  * @ch: ptr to channel structure
6447  *
6448  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6449  * and uplink switching element (uplink_seid)
6450  **/
6451 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6452 			       struct i40e_channel *ch)
6453 {
6454 	u8 vsi_type;
6455 	u16 seid;
6456 	int ret;
6457 
6458 	if (vsi->type == I40E_VSI_MAIN) {
6459 		vsi_type = I40E_VSI_VMDQ2;
6460 	} else {
6461 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6462 			vsi->type);
6463 		return false;
6464 	}
6465 
6466 	/* underlying switching element */
6467 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6468 
6469 	/* create channel (VSI), configure TX rings */
6470 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6471 	if (ret) {
6472 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6473 		return false;
6474 	}
6475 
6476 	return ch->initialized ? true : false;
6477 }
6478 
6479 /**
6480  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6481  * @vsi: ptr to VSI which has PF backing
6482  *
6483  * Sets up switch mode correctly if it needs to be changed and perform
6484  * what are allowed modes.
6485  **/
6486 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6487 {
6488 	u8 mode;
6489 	struct i40e_pf *pf = vsi->back;
6490 	struct i40e_hw *hw = &pf->hw;
6491 	int ret;
6492 
6493 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6494 	if (ret)
6495 		return -EINVAL;
6496 
6497 	if (hw->dev_caps.switch_mode) {
6498 		/* if switch mode is set, support mode2 (non-tunneled for
6499 		 * cloud filter) for now
6500 		 */
6501 		u32 switch_mode = hw->dev_caps.switch_mode &
6502 				  I40E_SWITCH_MODE_MASK;
6503 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6504 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6505 				return 0;
6506 			dev_err(&pf->pdev->dev,
6507 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6508 				hw->dev_caps.switch_mode);
6509 			return -EINVAL;
6510 		}
6511 	}
6512 
6513 	/* Set Bit 7 to be valid */
6514 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6515 
6516 	/* Set L4type for TCP support */
6517 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6518 
6519 	/* Set cloud filter mode */
6520 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6521 
6522 	/* Prep mode field for set_switch_config */
6523 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6524 					pf->last_sw_conf_valid_flags,
6525 					mode, NULL);
6526 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6527 		dev_err(&pf->pdev->dev,
6528 			"couldn't set switch config bits, err %pe aq_err %s\n",
6529 			ERR_PTR(ret),
6530 			i40e_aq_str(hw,
6531 				    hw->aq.asq_last_status));
6532 
6533 	return ret;
6534 }
6535 
6536 /**
6537  * i40e_create_queue_channel - function to create channel
6538  * @vsi: VSI to be configured
6539  * @ch: ptr to channel (it contains channel specific params)
6540  *
6541  * This function creates channel (VSI) using num_queues specified by user,
6542  * reconfigs RSS if needed.
6543  **/
6544 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6545 			      struct i40e_channel *ch)
6546 {
6547 	struct i40e_pf *pf = vsi->back;
6548 	bool reconfig_rss;
6549 	int err;
6550 
6551 	if (!ch)
6552 		return -EINVAL;
6553 
6554 	if (!ch->num_queue_pairs) {
6555 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6556 			ch->num_queue_pairs);
6557 		return -EINVAL;
6558 	}
6559 
6560 	/* validate user requested num_queues for channel */
6561 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6562 				       &reconfig_rss);
6563 	if (err) {
6564 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6565 			 ch->num_queue_pairs);
6566 		return -EINVAL;
6567 	}
6568 
6569 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6570 	 * VSI to be added switch to VEB mode.
6571 	 */
6572 
6573 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6574 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6575 
6576 		if (vsi->type == I40E_VSI_MAIN) {
6577 			if (i40e_is_tc_mqprio_enabled(pf))
6578 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6579 			else
6580 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6581 		}
6582 		/* now onwards for main VSI, number of queues will be value
6583 		 * of TC0's queue count
6584 		 */
6585 	}
6586 
6587 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6588 	 * it should be more than num_queues
6589 	 */
6590 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6591 		dev_dbg(&pf->pdev->dev,
6592 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6593 			vsi->cnt_q_avail, ch->num_queue_pairs);
6594 		return -EINVAL;
6595 	}
6596 
6597 	/* reconfig_rss only if vsi type is MAIN_VSI */
6598 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6599 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6600 		if (err) {
6601 			dev_info(&pf->pdev->dev,
6602 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6603 				 ch->num_queue_pairs);
6604 			return -EINVAL;
6605 		}
6606 	}
6607 
6608 	if (!i40e_setup_channel(pf, vsi, ch)) {
6609 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6610 		return -EINVAL;
6611 	}
6612 
6613 	dev_info(&pf->pdev->dev,
6614 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6615 		 ch->seid, ch->num_queue_pairs);
6616 
6617 	/* configure VSI for BW limit */
6618 	if (ch->max_tx_rate) {
6619 		u64 credits = ch->max_tx_rate;
6620 
6621 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6622 			return -EINVAL;
6623 
6624 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6625 		dev_dbg(&pf->pdev->dev,
6626 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6627 			ch->max_tx_rate,
6628 			credits,
6629 			ch->seid);
6630 	}
6631 
6632 	/* in case of VF, this will be main SRIOV VSI */
6633 	ch->parent_vsi = vsi;
6634 
6635 	/* and update main_vsi's count for queue_available to use */
6636 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6637 
6638 	return 0;
6639 }
6640 
6641 /**
6642  * i40e_configure_queue_channels - Add queue channel for the given TCs
6643  * @vsi: VSI to be configured
6644  *
6645  * Configures queue channel mapping to the given TCs
6646  **/
6647 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6648 {
6649 	struct i40e_channel *ch;
6650 	u64 max_rate = 0;
6651 	int ret = 0, i;
6652 
6653 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6654 	vsi->tc_seid_map[0] = vsi->seid;
6655 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6656 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6657 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6658 			if (!ch) {
6659 				ret = -ENOMEM;
6660 				goto err_free;
6661 			}
6662 
6663 			INIT_LIST_HEAD(&ch->list);
6664 			ch->num_queue_pairs =
6665 				vsi->tc_config.tc_info[i].qcount;
6666 			ch->base_queue =
6667 				vsi->tc_config.tc_info[i].qoffset;
6668 
6669 			/* Bandwidth limit through tc interface is in bytes/s,
6670 			 * change to Mbit/s
6671 			 */
6672 			max_rate = vsi->mqprio_qopt.max_rate[i];
6673 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6674 			ch->max_tx_rate = max_rate;
6675 
6676 			list_add_tail(&ch->list, &vsi->ch_list);
6677 
6678 			ret = i40e_create_queue_channel(vsi, ch);
6679 			if (ret) {
6680 				dev_err(&vsi->back->pdev->dev,
6681 					"Failed creating queue channel with TC%d: queues %d\n",
6682 					i, ch->num_queue_pairs);
6683 				goto err_free;
6684 			}
6685 			vsi->tc_seid_map[i] = ch->seid;
6686 		}
6687 	}
6688 
6689 	/* reset to reconfigure TX queue contexts */
6690 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6691 	return ret;
6692 
6693 err_free:
6694 	i40e_remove_queue_channels(vsi);
6695 	return ret;
6696 }
6697 
6698 /**
6699  * i40e_veb_config_tc - Configure TCs for given VEB
6700  * @veb: given VEB
6701  * @enabled_tc: TC bitmap
6702  *
6703  * Configures given TC bitmap for VEB (switching) element
6704  **/
6705 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6706 {
6707 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6708 	struct i40e_pf *pf = veb->pf;
6709 	int ret = 0;
6710 	int i;
6711 
6712 	/* No TCs or already enabled TCs just return */
6713 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6714 		return ret;
6715 
6716 	bw_data.tc_valid_bits = enabled_tc;
6717 	/* bw_data.absolute_credits is not set (relative) */
6718 
6719 	/* Enable ETS TCs with equal BW Share for now */
6720 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6721 		if (enabled_tc & BIT(i))
6722 			bw_data.tc_bw_share_credits[i] = 1;
6723 	}
6724 
6725 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6726 						   &bw_data, NULL);
6727 	if (ret) {
6728 		dev_info(&pf->pdev->dev,
6729 			 "VEB bw config failed, err %pe aq_err %s\n",
6730 			 ERR_PTR(ret),
6731 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6732 		goto out;
6733 	}
6734 
6735 	/* Update the BW information */
6736 	ret = i40e_veb_get_bw_info(veb);
6737 	if (ret) {
6738 		dev_info(&pf->pdev->dev,
6739 			 "Failed getting veb bw config, err %pe aq_err %s\n",
6740 			 ERR_PTR(ret),
6741 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6742 	}
6743 
6744 out:
6745 	return ret;
6746 }
6747 
6748 #ifdef CONFIG_I40E_DCB
6749 /**
6750  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6751  * @pf: PF struct
6752  *
6753  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6754  * the caller would've quiesce all the VSIs before calling
6755  * this function
6756  **/
6757 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6758 {
6759 	u8 tc_map = 0;
6760 	int ret;
6761 	u8 v;
6762 
6763 	/* Enable the TCs available on PF to all VEBs */
6764 	tc_map = i40e_pf_get_tc_map(pf);
6765 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6766 		return;
6767 
6768 	for (v = 0; v < I40E_MAX_VEB; v++) {
6769 		if (!pf->veb[v])
6770 			continue;
6771 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6772 		if (ret) {
6773 			dev_info(&pf->pdev->dev,
6774 				 "Failed configuring TC for VEB seid=%d\n",
6775 				 pf->veb[v]->seid);
6776 			/* Will try to configure as many components */
6777 		}
6778 	}
6779 
6780 	/* Update each VSI */
6781 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6782 		if (!pf->vsi[v])
6783 			continue;
6784 
6785 		/* - Enable all TCs for the LAN VSI
6786 		 * - For all others keep them at TC0 for now
6787 		 */
6788 		if (v == pf->lan_vsi)
6789 			tc_map = i40e_pf_get_tc_map(pf);
6790 		else
6791 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6792 
6793 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6794 		if (ret) {
6795 			dev_info(&pf->pdev->dev,
6796 				 "Failed configuring TC for VSI seid=%d\n",
6797 				 pf->vsi[v]->seid);
6798 			/* Will try to configure as many components */
6799 		} else {
6800 			/* Re-configure VSI vectors based on updated TC map */
6801 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6802 			if (pf->vsi[v]->netdev)
6803 				i40e_dcbnl_set_all(pf->vsi[v]);
6804 		}
6805 	}
6806 }
6807 
6808 /**
6809  * i40e_resume_port_tx - Resume port Tx
6810  * @pf: PF struct
6811  *
6812  * Resume a port's Tx and issue a PF reset in case of failure to
6813  * resume.
6814  **/
6815 static int i40e_resume_port_tx(struct i40e_pf *pf)
6816 {
6817 	struct i40e_hw *hw = &pf->hw;
6818 	int ret;
6819 
6820 	ret = i40e_aq_resume_port_tx(hw, NULL);
6821 	if (ret) {
6822 		dev_info(&pf->pdev->dev,
6823 			 "Resume Port Tx failed, err %pe aq_err %s\n",
6824 			  ERR_PTR(ret),
6825 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6826 		/* Schedule PF reset to recover */
6827 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6828 		i40e_service_event_schedule(pf);
6829 	}
6830 
6831 	return ret;
6832 }
6833 
6834 /**
6835  * i40e_suspend_port_tx - Suspend port Tx
6836  * @pf: PF struct
6837  *
6838  * Suspend a port's Tx and issue a PF reset in case of failure.
6839  **/
6840 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6841 {
6842 	struct i40e_hw *hw = &pf->hw;
6843 	int ret;
6844 
6845 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6846 	if (ret) {
6847 		dev_info(&pf->pdev->dev,
6848 			 "Suspend Port Tx failed, err %pe aq_err %s\n",
6849 			 ERR_PTR(ret),
6850 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6851 		/* Schedule PF reset to recover */
6852 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6853 		i40e_service_event_schedule(pf);
6854 	}
6855 
6856 	return ret;
6857 }
6858 
6859 /**
6860  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6861  * @pf: PF being configured
6862  * @new_cfg: New DCBX configuration
6863  *
6864  * Program DCB settings into HW and reconfigure VEB/VSIs on
6865  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6866  **/
6867 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6868 				  struct i40e_dcbx_config *new_cfg)
6869 {
6870 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6871 	int ret;
6872 
6873 	/* Check if need reconfiguration */
6874 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6875 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6876 		return 0;
6877 	}
6878 
6879 	/* Config change disable all VSIs */
6880 	i40e_pf_quiesce_all_vsi(pf);
6881 
6882 	/* Copy the new config to the current config */
6883 	*old_cfg = *new_cfg;
6884 	old_cfg->etsrec = old_cfg->etscfg;
6885 	ret = i40e_set_dcb_config(&pf->hw);
6886 	if (ret) {
6887 		dev_info(&pf->pdev->dev,
6888 			 "Set DCB Config failed, err %pe aq_err %s\n",
6889 			 ERR_PTR(ret),
6890 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6891 		goto out;
6892 	}
6893 
6894 	/* Changes in configuration update VEB/VSI */
6895 	i40e_dcb_reconfigure(pf);
6896 out:
6897 	/* In case of reset do not try to resume anything */
6898 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6899 		/* Re-start the VSIs if disabled */
6900 		ret = i40e_resume_port_tx(pf);
6901 		/* In case of error no point in resuming VSIs */
6902 		if (ret)
6903 			goto err;
6904 		i40e_pf_unquiesce_all_vsi(pf);
6905 	}
6906 err:
6907 	return ret;
6908 }
6909 
6910 /**
6911  * i40e_hw_dcb_config - Program new DCBX settings into HW
6912  * @pf: PF being configured
6913  * @new_cfg: New DCBX configuration
6914  *
6915  * Program DCB settings into HW and reconfigure VEB/VSIs on
6916  * given PF
6917  **/
6918 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6919 {
6920 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6921 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6922 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6923 	struct i40e_dcbx_config *old_cfg;
6924 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6925 	struct i40e_rx_pb_config pb_cfg;
6926 	struct i40e_hw *hw = &pf->hw;
6927 	u8 num_ports = hw->num_ports;
6928 	bool need_reconfig;
6929 	int ret = -EINVAL;
6930 	u8 lltc_map = 0;
6931 	u8 tc_map = 0;
6932 	u8 new_numtc;
6933 	u8 i;
6934 
6935 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6936 	/* Un-pack information to Program ETS HW via shared API
6937 	 * numtc, tcmap
6938 	 * LLTC map
6939 	 * ETS/NON-ETS arbiter mode
6940 	 * max exponent (credit refills)
6941 	 * Total number of ports
6942 	 * PFC priority bit-map
6943 	 * Priority Table
6944 	 * BW % per TC
6945 	 * Arbiter mode between UPs sharing same TC
6946 	 * TSA table (ETS or non-ETS)
6947 	 * EEE enabled or not
6948 	 * MFS TC table
6949 	 */
6950 
6951 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6952 
6953 	memset(&ets_data, 0, sizeof(ets_data));
6954 	for (i = 0; i < new_numtc; i++) {
6955 		tc_map |= BIT(i);
6956 		switch (new_cfg->etscfg.tsatable[i]) {
6957 		case I40E_IEEE_TSA_ETS:
6958 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6959 			ets_data.tc_bw_share_credits[i] =
6960 					new_cfg->etscfg.tcbwtable[i];
6961 			break;
6962 		case I40E_IEEE_TSA_STRICT:
6963 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6964 			lltc_map |= BIT(i);
6965 			ets_data.tc_bw_share_credits[i] =
6966 					I40E_DCB_STRICT_PRIO_CREDITS;
6967 			break;
6968 		default:
6969 			/* Invalid TSA type */
6970 			need_reconfig = false;
6971 			goto out;
6972 		}
6973 	}
6974 
6975 	old_cfg = &hw->local_dcbx_config;
6976 	/* Check if need reconfiguration */
6977 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6978 
6979 	/* If needed, enable/disable frame tagging, disable all VSIs
6980 	 * and suspend port tx
6981 	 */
6982 	if (need_reconfig) {
6983 		/* Enable DCB tagging only when more than one TC */
6984 		if (new_numtc > 1)
6985 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6986 		else
6987 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6988 
6989 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6990 		/* Reconfiguration needed quiesce all VSIs */
6991 		i40e_pf_quiesce_all_vsi(pf);
6992 		ret = i40e_suspend_port_tx(pf);
6993 		if (ret)
6994 			goto err;
6995 	}
6996 
6997 	/* Configure Port ETS Tx Scheduler */
6998 	ets_data.tc_valid_bits = tc_map;
6999 	ets_data.tc_strict_priority_flags = lltc_map;
7000 	ret = i40e_aq_config_switch_comp_ets
7001 		(hw, pf->mac_seid, &ets_data,
7002 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
7003 	if (ret) {
7004 		dev_info(&pf->pdev->dev,
7005 			 "Modify Port ETS failed, err %pe aq_err %s\n",
7006 			 ERR_PTR(ret),
7007 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7008 		goto out;
7009 	}
7010 
7011 	/* Configure Rx ETS HW */
7012 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
7013 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
7014 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
7015 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
7016 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
7017 				   lltc_map);
7018 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7019 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7020 				     prio_type);
7021 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7022 			       new_cfg->etscfg.prioritytable);
7023 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7024 
7025 	/* Configure Rx Packet Buffers in HW */
7026 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7027 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7028 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7029 	}
7030 
7031 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7032 					 false, new_cfg->pfc.pfcenable,
7033 					 mfs_tc, &pb_cfg);
7034 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7035 
7036 	/* Update the local Rx Packet buffer config */
7037 	pf->pb_cfg = pb_cfg;
7038 
7039 	/* Inform the FW about changes to DCB configuration */
7040 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7041 	if (ret) {
7042 		dev_info(&pf->pdev->dev,
7043 			 "DCB Updated failed, err %pe aq_err %s\n",
7044 			 ERR_PTR(ret),
7045 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7046 		goto out;
7047 	}
7048 
7049 	/* Update the port DCBx configuration */
7050 	*old_cfg = *new_cfg;
7051 
7052 	/* Changes in configuration update VEB/VSI */
7053 	i40e_dcb_reconfigure(pf);
7054 out:
7055 	/* Re-start the VSIs if disabled */
7056 	if (need_reconfig) {
7057 		ret = i40e_resume_port_tx(pf);
7058 
7059 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7060 		/* In case of error no point in resuming VSIs */
7061 		if (ret)
7062 			goto err;
7063 
7064 		/* Wait for the PF's queues to be disabled */
7065 		ret = i40e_pf_wait_queues_disabled(pf);
7066 		if (ret) {
7067 			/* Schedule PF reset to recover */
7068 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7069 			i40e_service_event_schedule(pf);
7070 			goto err;
7071 		} else {
7072 			i40e_pf_unquiesce_all_vsi(pf);
7073 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7074 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7075 		}
7076 		/* registers are set, lets apply */
7077 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7078 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7079 	}
7080 
7081 err:
7082 	return ret;
7083 }
7084 
7085 /**
7086  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7087  * @pf: PF being queried
7088  *
7089  * Set default DCB configuration in case DCB is to be done in SW.
7090  **/
7091 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7092 {
7093 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7094 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7095 	struct i40e_hw *hw = &pf->hw;
7096 	int err;
7097 
7098 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7099 		/* Update the local cached instance with TC0 ETS */
7100 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7101 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7102 		pf->tmp_cfg.etscfg.maxtcs = 0;
7103 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7104 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7105 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7106 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7107 		/* FW needs one App to configure HW */
7108 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7109 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7110 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7111 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7112 
7113 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7114 	}
7115 
7116 	memset(&ets_data, 0, sizeof(ets_data));
7117 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7118 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7119 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7120 
7121 	/* Enable ETS on the Physical port */
7122 	err = i40e_aq_config_switch_comp_ets
7123 		(hw, pf->mac_seid, &ets_data,
7124 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7125 	if (err) {
7126 		dev_info(&pf->pdev->dev,
7127 			 "Enable Port ETS failed, err %pe aq_err %s\n",
7128 			 ERR_PTR(err),
7129 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7130 		err = -ENOENT;
7131 		goto out;
7132 	}
7133 
7134 	/* Update the local cached instance with TC0 ETS */
7135 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7136 	dcb_cfg->etscfg.cbs = 0;
7137 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7138 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7139 
7140 out:
7141 	return err;
7142 }
7143 
7144 /**
7145  * i40e_init_pf_dcb - Initialize DCB configuration
7146  * @pf: PF being configured
7147  *
7148  * Query the current DCB configuration and cache it
7149  * in the hardware structure
7150  **/
7151 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7152 {
7153 	struct i40e_hw *hw = &pf->hw;
7154 	int err;
7155 
7156 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7157 	 * Also do not enable DCBx if FW LLDP agent is disabled
7158 	 */
7159 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7160 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7161 		err = I40E_NOT_SUPPORTED;
7162 		goto out;
7163 	}
7164 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7165 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7166 		err = i40e_dcb_sw_default_config(pf);
7167 		if (err) {
7168 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7169 			goto out;
7170 		}
7171 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7172 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7173 			       DCB_CAP_DCBX_VER_IEEE;
7174 		/* at init capable but disabled */
7175 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7176 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7177 		goto out;
7178 	}
7179 	err = i40e_init_dcb(hw, true);
7180 	if (!err) {
7181 		/* Device/Function is not DCBX capable */
7182 		if ((!hw->func_caps.dcb) ||
7183 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7184 			dev_info(&pf->pdev->dev,
7185 				 "DCBX offload is not supported or is disabled for this PF.\n");
7186 		} else {
7187 			/* When status is not DISABLED then DCBX in FW */
7188 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7189 				       DCB_CAP_DCBX_VER_IEEE;
7190 
7191 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7192 			/* Enable DCB tagging only when more than one TC
7193 			 * or explicitly disable if only one TC
7194 			 */
7195 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7196 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7197 			else
7198 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7199 			dev_dbg(&pf->pdev->dev,
7200 				"DCBX offload is supported for this PF.\n");
7201 		}
7202 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7203 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7204 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7205 	} else {
7206 		dev_info(&pf->pdev->dev,
7207 			 "Query for DCB configuration failed, err %pe aq_err %s\n",
7208 			 ERR_PTR(err),
7209 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7210 	}
7211 
7212 out:
7213 	return err;
7214 }
7215 #endif /* CONFIG_I40E_DCB */
7216 
7217 /**
7218  * i40e_print_link_message - print link up or down
7219  * @vsi: the VSI for which link needs a message
7220  * @isup: true of link is up, false otherwise
7221  */
7222 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7223 {
7224 	enum i40e_aq_link_speed new_speed;
7225 	struct i40e_pf *pf = vsi->back;
7226 	char *speed = "Unknown";
7227 	char *fc = "Unknown";
7228 	char *fec = "";
7229 	char *req_fec = "";
7230 	char *an = "";
7231 
7232 	if (isup)
7233 		new_speed = pf->hw.phy.link_info.link_speed;
7234 	else
7235 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7236 
7237 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7238 		return;
7239 	vsi->current_isup = isup;
7240 	vsi->current_speed = new_speed;
7241 	if (!isup) {
7242 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7243 		return;
7244 	}
7245 
7246 	/* Warn user if link speed on NPAR enabled partition is not at
7247 	 * least 10GB
7248 	 */
7249 	if (pf->hw.func_caps.npar_enable &&
7250 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7251 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7252 		netdev_warn(vsi->netdev,
7253 			    "The partition detected link speed that is less than 10Gbps\n");
7254 
7255 	switch (pf->hw.phy.link_info.link_speed) {
7256 	case I40E_LINK_SPEED_40GB:
7257 		speed = "40 G";
7258 		break;
7259 	case I40E_LINK_SPEED_20GB:
7260 		speed = "20 G";
7261 		break;
7262 	case I40E_LINK_SPEED_25GB:
7263 		speed = "25 G";
7264 		break;
7265 	case I40E_LINK_SPEED_10GB:
7266 		speed = "10 G";
7267 		break;
7268 	case I40E_LINK_SPEED_5GB:
7269 		speed = "5 G";
7270 		break;
7271 	case I40E_LINK_SPEED_2_5GB:
7272 		speed = "2.5 G";
7273 		break;
7274 	case I40E_LINK_SPEED_1GB:
7275 		speed = "1000 M";
7276 		break;
7277 	case I40E_LINK_SPEED_100MB:
7278 		speed = "100 M";
7279 		break;
7280 	default:
7281 		break;
7282 	}
7283 
7284 	switch (pf->hw.fc.current_mode) {
7285 	case I40E_FC_FULL:
7286 		fc = "RX/TX";
7287 		break;
7288 	case I40E_FC_TX_PAUSE:
7289 		fc = "TX";
7290 		break;
7291 	case I40E_FC_RX_PAUSE:
7292 		fc = "RX";
7293 		break;
7294 	default:
7295 		fc = "None";
7296 		break;
7297 	}
7298 
7299 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7300 		req_fec = "None";
7301 		fec = "None";
7302 		an = "False";
7303 
7304 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7305 			an = "True";
7306 
7307 		if (pf->hw.phy.link_info.fec_info &
7308 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7309 			fec = "CL74 FC-FEC/BASE-R";
7310 		else if (pf->hw.phy.link_info.fec_info &
7311 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7312 			fec = "CL108 RS-FEC";
7313 
7314 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7315 		 * both RS and FC are requested
7316 		 */
7317 		if (vsi->back->hw.phy.link_info.req_fec_info &
7318 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7319 			if (vsi->back->hw.phy.link_info.req_fec_info &
7320 			    I40E_AQ_REQUEST_FEC_RS)
7321 				req_fec = "CL108 RS-FEC";
7322 			else
7323 				req_fec = "CL74 FC-FEC/BASE-R";
7324 		}
7325 		netdev_info(vsi->netdev,
7326 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7327 			    speed, req_fec, fec, an, fc);
7328 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7329 		req_fec = "None";
7330 		fec = "None";
7331 		an = "False";
7332 
7333 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7334 			an = "True";
7335 
7336 		if (pf->hw.phy.link_info.fec_info &
7337 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7338 			fec = "CL74 FC-FEC/BASE-R";
7339 
7340 		if (pf->hw.phy.link_info.req_fec_info &
7341 		    I40E_AQ_REQUEST_FEC_KR)
7342 			req_fec = "CL74 FC-FEC/BASE-R";
7343 
7344 		netdev_info(vsi->netdev,
7345 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7346 			    speed, req_fec, fec, an, fc);
7347 	} else {
7348 		netdev_info(vsi->netdev,
7349 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7350 			    speed, fc);
7351 	}
7352 
7353 }
7354 
7355 /**
7356  * i40e_up_complete - Finish the last steps of bringing up a connection
7357  * @vsi: the VSI being configured
7358  **/
7359 static int i40e_up_complete(struct i40e_vsi *vsi)
7360 {
7361 	struct i40e_pf *pf = vsi->back;
7362 	int err;
7363 
7364 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7365 		i40e_vsi_configure_msix(vsi);
7366 	else
7367 		i40e_configure_msi_and_legacy(vsi);
7368 
7369 	/* start rings */
7370 	err = i40e_vsi_start_rings(vsi);
7371 	if (err)
7372 		return err;
7373 
7374 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7375 	i40e_napi_enable_all(vsi);
7376 	i40e_vsi_enable_irq(vsi);
7377 
7378 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7379 	    (vsi->netdev)) {
7380 		i40e_print_link_message(vsi, true);
7381 		netif_tx_start_all_queues(vsi->netdev);
7382 		netif_carrier_on(vsi->netdev);
7383 	}
7384 
7385 	/* replay FDIR SB filters */
7386 	if (vsi->type == I40E_VSI_FDIR) {
7387 		/* reset fd counters */
7388 		pf->fd_add_err = 0;
7389 		pf->fd_atr_cnt = 0;
7390 		i40e_fdir_filter_restore(vsi);
7391 	}
7392 
7393 	/* On the next run of the service_task, notify any clients of the new
7394 	 * opened netdev
7395 	 */
7396 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7397 	i40e_service_event_schedule(pf);
7398 
7399 	return 0;
7400 }
7401 
7402 /**
7403  * i40e_vsi_reinit_locked - Reset the VSI
7404  * @vsi: the VSI being configured
7405  *
7406  * Rebuild the ring structs after some configuration
7407  * has changed, e.g. MTU size.
7408  **/
7409 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7410 {
7411 	struct i40e_pf *pf = vsi->back;
7412 
7413 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7414 		usleep_range(1000, 2000);
7415 	i40e_down(vsi);
7416 
7417 	i40e_up(vsi);
7418 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7419 }
7420 
7421 /**
7422  * i40e_force_link_state - Force the link status
7423  * @pf: board private structure
7424  * @is_up: whether the link state should be forced up or down
7425  **/
7426 static int i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7427 {
7428 	struct i40e_aq_get_phy_abilities_resp abilities;
7429 	struct i40e_aq_set_phy_config config = {0};
7430 	bool non_zero_phy_type = is_up;
7431 	struct i40e_hw *hw = &pf->hw;
7432 	u64 mask;
7433 	u8 speed;
7434 	int err;
7435 
7436 	/* Card might've been put in an unstable state by other drivers
7437 	 * and applications, which causes incorrect speed values being
7438 	 * set on startup. In order to clear speed registers, we call
7439 	 * get_phy_capabilities twice, once to get initial state of
7440 	 * available speeds, and once to get current PHY config.
7441 	 */
7442 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7443 					   NULL);
7444 	if (err) {
7445 		dev_err(&pf->pdev->dev,
7446 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7447 			ERR_PTR(err),
7448 			i40e_aq_str(hw, hw->aq.asq_last_status));
7449 		return err;
7450 	}
7451 	speed = abilities.link_speed;
7452 
7453 	/* Get the current phy config */
7454 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7455 					   NULL);
7456 	if (err) {
7457 		dev_err(&pf->pdev->dev,
7458 			"failed to get phy cap., ret =  %pe last_status =  %s\n",
7459 			ERR_PTR(err),
7460 			i40e_aq_str(hw, hw->aq.asq_last_status));
7461 		return err;
7462 	}
7463 
7464 	/* If link needs to go up, but was not forced to go down,
7465 	 * and its speed values are OK, no need for a flap
7466 	 * if non_zero_phy_type was set, still need to force up
7467 	 */
7468 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7469 		non_zero_phy_type = true;
7470 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7471 		return I40E_SUCCESS;
7472 
7473 	/* To force link we need to set bits for all supported PHY types,
7474 	 * but there are now more than 32, so we need to split the bitmap
7475 	 * across two fields.
7476 	 */
7477 	mask = I40E_PHY_TYPES_BITMASK;
7478 	config.phy_type =
7479 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7480 	config.phy_type_ext =
7481 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7482 	/* Copy the old settings, except of phy_type */
7483 	config.abilities = abilities.abilities;
7484 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7485 		if (is_up)
7486 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7487 		else
7488 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7489 	}
7490 	if (abilities.link_speed != 0)
7491 		config.link_speed = abilities.link_speed;
7492 	else
7493 		config.link_speed = speed;
7494 	config.eee_capability = abilities.eee_capability;
7495 	config.eeer = abilities.eeer_val;
7496 	config.low_power_ctrl = abilities.d3_lpan;
7497 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7498 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7499 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7500 
7501 	if (err) {
7502 		dev_err(&pf->pdev->dev,
7503 			"set phy config ret =  %pe last_status =  %s\n",
7504 			ERR_PTR(err),
7505 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7506 		return err;
7507 	}
7508 
7509 	/* Update the link info */
7510 	err = i40e_update_link_info(hw);
7511 	if (err) {
7512 		/* Wait a little bit (on 40G cards it sometimes takes a really
7513 		 * long time for link to come back from the atomic reset)
7514 		 * and try once more
7515 		 */
7516 		msleep(1000);
7517 		i40e_update_link_info(hw);
7518 	}
7519 
7520 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7521 
7522 	return I40E_SUCCESS;
7523 }
7524 
7525 /**
7526  * i40e_up - Bring the connection back up after being down
7527  * @vsi: the VSI being configured
7528  **/
7529 int i40e_up(struct i40e_vsi *vsi)
7530 {
7531 	int err;
7532 
7533 	if (vsi->type == I40E_VSI_MAIN &&
7534 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7535 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7536 		i40e_force_link_state(vsi->back, true);
7537 
7538 	err = i40e_vsi_configure(vsi);
7539 	if (!err)
7540 		err = i40e_up_complete(vsi);
7541 
7542 	return err;
7543 }
7544 
7545 /**
7546  * i40e_down - Shutdown the connection processing
7547  * @vsi: the VSI being stopped
7548  **/
7549 void i40e_down(struct i40e_vsi *vsi)
7550 {
7551 	int i;
7552 
7553 	/* It is assumed that the caller of this function
7554 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7555 	 */
7556 	if (vsi->netdev) {
7557 		netif_carrier_off(vsi->netdev);
7558 		netif_tx_disable(vsi->netdev);
7559 	}
7560 	i40e_vsi_disable_irq(vsi);
7561 	i40e_vsi_stop_rings(vsi);
7562 	if (vsi->type == I40E_VSI_MAIN &&
7563 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7564 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7565 		i40e_force_link_state(vsi->back, false);
7566 	i40e_napi_disable_all(vsi);
7567 
7568 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7569 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7570 		if (i40e_enabled_xdp_vsi(vsi)) {
7571 			/* Make sure that in-progress ndo_xdp_xmit and
7572 			 * ndo_xsk_wakeup calls are completed.
7573 			 */
7574 			synchronize_rcu();
7575 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7576 		}
7577 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7578 	}
7579 
7580 }
7581 
7582 /**
7583  * i40e_validate_mqprio_qopt- validate queue mapping info
7584  * @vsi: the VSI being configured
7585  * @mqprio_qopt: queue parametrs
7586  **/
7587 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7588 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7589 {
7590 	u64 sum_max_rate = 0;
7591 	u64 max_rate = 0;
7592 	int i;
7593 
7594 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7595 	    mqprio_qopt->qopt.num_tc < 1 ||
7596 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7597 		return -EINVAL;
7598 	for (i = 0; ; i++) {
7599 		if (!mqprio_qopt->qopt.count[i])
7600 			return -EINVAL;
7601 		if (mqprio_qopt->min_rate[i]) {
7602 			dev_err(&vsi->back->pdev->dev,
7603 				"Invalid min tx rate (greater than 0) specified\n");
7604 			return -EINVAL;
7605 		}
7606 		max_rate = mqprio_qopt->max_rate[i];
7607 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7608 		sum_max_rate += max_rate;
7609 
7610 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7611 			break;
7612 		if (mqprio_qopt->qopt.offset[i + 1] !=
7613 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7614 			return -EINVAL;
7615 	}
7616 	if (vsi->num_queue_pairs <
7617 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7618 		dev_err(&vsi->back->pdev->dev,
7619 			"Failed to create traffic channel, insufficient number of queues.\n");
7620 		return -EINVAL;
7621 	}
7622 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7623 		dev_err(&vsi->back->pdev->dev,
7624 			"Invalid max tx rate specified\n");
7625 		return -EINVAL;
7626 	}
7627 	return 0;
7628 }
7629 
7630 /**
7631  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7632  * @vsi: the VSI being configured
7633  **/
7634 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7635 {
7636 	u16 qcount;
7637 	int i;
7638 
7639 	/* Only TC0 is enabled */
7640 	vsi->tc_config.numtc = 1;
7641 	vsi->tc_config.enabled_tc = 1;
7642 	qcount = min_t(int, vsi->alloc_queue_pairs,
7643 		       i40e_pf_get_max_q_per_tc(vsi->back));
7644 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7645 		/* For the TC that is not enabled set the offset to default
7646 		 * queue and allocate one queue for the given TC.
7647 		 */
7648 		vsi->tc_config.tc_info[i].qoffset = 0;
7649 		if (i == 0)
7650 			vsi->tc_config.tc_info[i].qcount = qcount;
7651 		else
7652 			vsi->tc_config.tc_info[i].qcount = 1;
7653 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7654 	}
7655 }
7656 
7657 /**
7658  * i40e_del_macvlan_filter
7659  * @hw: pointer to the HW structure
7660  * @seid: seid of the channel VSI
7661  * @macaddr: the mac address to apply as a filter
7662  * @aq_err: store the admin Q error
7663  *
7664  * This function deletes a mac filter on the channel VSI which serves as the
7665  * macvlan. Returns 0 on success.
7666  **/
7667 static int i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7668 				   const u8 *macaddr, int *aq_err)
7669 {
7670 	struct i40e_aqc_remove_macvlan_element_data element;
7671 	int status;
7672 
7673 	memset(&element, 0, sizeof(element));
7674 	ether_addr_copy(element.mac_addr, macaddr);
7675 	element.vlan_tag = 0;
7676 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7677 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7678 	*aq_err = hw->aq.asq_last_status;
7679 
7680 	return status;
7681 }
7682 
7683 /**
7684  * i40e_add_macvlan_filter
7685  * @hw: pointer to the HW structure
7686  * @seid: seid of the channel VSI
7687  * @macaddr: the mac address to apply as a filter
7688  * @aq_err: store the admin Q error
7689  *
7690  * This function adds a mac filter on the channel VSI which serves as the
7691  * macvlan. Returns 0 on success.
7692  **/
7693 static int i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7694 				   const u8 *macaddr, int *aq_err)
7695 {
7696 	struct i40e_aqc_add_macvlan_element_data element;
7697 	u16 cmd_flags = 0;
7698 	int status;
7699 
7700 	ether_addr_copy(element.mac_addr, macaddr);
7701 	element.vlan_tag = 0;
7702 	element.queue_number = 0;
7703 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7704 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7705 	element.flags = cpu_to_le16(cmd_flags);
7706 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7707 	*aq_err = hw->aq.asq_last_status;
7708 
7709 	return status;
7710 }
7711 
7712 /**
7713  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7714  * @vsi: the VSI we want to access
7715  * @ch: the channel we want to access
7716  */
7717 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7718 {
7719 	struct i40e_ring *tx_ring, *rx_ring;
7720 	u16 pf_q;
7721 	int i;
7722 
7723 	for (i = 0; i < ch->num_queue_pairs; i++) {
7724 		pf_q = ch->base_queue + i;
7725 		tx_ring = vsi->tx_rings[pf_q];
7726 		tx_ring->ch = NULL;
7727 		rx_ring = vsi->rx_rings[pf_q];
7728 		rx_ring->ch = NULL;
7729 	}
7730 }
7731 
7732 /**
7733  * i40e_free_macvlan_channels
7734  * @vsi: the VSI we want to access
7735  *
7736  * This function frees the Qs of the channel VSI from
7737  * the stack and also deletes the channel VSIs which
7738  * serve as macvlans.
7739  */
7740 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7741 {
7742 	struct i40e_channel *ch, *ch_tmp;
7743 	int ret;
7744 
7745 	if (list_empty(&vsi->macvlan_list))
7746 		return;
7747 
7748 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7749 		struct i40e_vsi *parent_vsi;
7750 
7751 		if (i40e_is_channel_macvlan(ch)) {
7752 			i40e_reset_ch_rings(vsi, ch);
7753 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7754 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7755 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7756 			kfree(ch->fwd);
7757 			ch->fwd = NULL;
7758 		}
7759 
7760 		list_del(&ch->list);
7761 		parent_vsi = ch->parent_vsi;
7762 		if (!parent_vsi || !ch->initialized) {
7763 			kfree(ch);
7764 			continue;
7765 		}
7766 
7767 		/* remove the VSI */
7768 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7769 					     NULL);
7770 		if (ret)
7771 			dev_err(&vsi->back->pdev->dev,
7772 				"unable to remove channel (%d) for parent VSI(%d)\n",
7773 				ch->seid, parent_vsi->seid);
7774 		kfree(ch);
7775 	}
7776 	vsi->macvlan_cnt = 0;
7777 }
7778 
7779 /**
7780  * i40e_fwd_ring_up - bring the macvlan device up
7781  * @vsi: the VSI we want to access
7782  * @vdev: macvlan netdevice
7783  * @fwd: the private fwd structure
7784  */
7785 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7786 			    struct i40e_fwd_adapter *fwd)
7787 {
7788 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7789 	int ret = 0, num_tc = 1,  i, aq_err;
7790 	struct i40e_pf *pf = vsi->back;
7791 	struct i40e_hw *hw = &pf->hw;
7792 
7793 	/* Go through the list and find an available channel */
7794 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7795 		if (!i40e_is_channel_macvlan(iter)) {
7796 			iter->fwd = fwd;
7797 			/* record configuration for macvlan interface in vdev */
7798 			for (i = 0; i < num_tc; i++)
7799 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7800 							     i,
7801 							     iter->num_queue_pairs,
7802 							     iter->base_queue);
7803 			for (i = 0; i < iter->num_queue_pairs; i++) {
7804 				struct i40e_ring *tx_ring, *rx_ring;
7805 				u16 pf_q;
7806 
7807 				pf_q = iter->base_queue + i;
7808 
7809 				/* Get to TX ring ptr */
7810 				tx_ring = vsi->tx_rings[pf_q];
7811 				tx_ring->ch = iter;
7812 
7813 				/* Get the RX ring ptr */
7814 				rx_ring = vsi->rx_rings[pf_q];
7815 				rx_ring->ch = iter;
7816 			}
7817 			ch = iter;
7818 			break;
7819 		}
7820 	}
7821 
7822 	if (!ch)
7823 		return -EINVAL;
7824 
7825 	/* Guarantee all rings are updated before we update the
7826 	 * MAC address filter.
7827 	 */
7828 	wmb();
7829 
7830 	/* Add a mac filter */
7831 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7832 	if (ret) {
7833 		/* if we cannot add the MAC rule then disable the offload */
7834 		macvlan_release_l2fw_offload(vdev);
7835 		for (i = 0; i < ch->num_queue_pairs; i++) {
7836 			struct i40e_ring *rx_ring;
7837 			u16 pf_q;
7838 
7839 			pf_q = ch->base_queue + i;
7840 			rx_ring = vsi->rx_rings[pf_q];
7841 			rx_ring->netdev = NULL;
7842 		}
7843 		dev_info(&pf->pdev->dev,
7844 			 "Error adding mac filter on macvlan err %pe, aq_err %s\n",
7845 			  ERR_PTR(ret),
7846 			  i40e_aq_str(hw, aq_err));
7847 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7848 	}
7849 
7850 	return ret;
7851 }
7852 
7853 /**
7854  * i40e_setup_macvlans - create the channels which will be macvlans
7855  * @vsi: the VSI we want to access
7856  * @macvlan_cnt: no. of macvlans to be setup
7857  * @qcnt: no. of Qs per macvlan
7858  * @vdev: macvlan netdevice
7859  */
7860 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7861 			       struct net_device *vdev)
7862 {
7863 	struct i40e_pf *pf = vsi->back;
7864 	struct i40e_hw *hw = &pf->hw;
7865 	struct i40e_vsi_context ctxt;
7866 	u16 sections, qmap, num_qps;
7867 	struct i40e_channel *ch;
7868 	int i, pow, ret = 0;
7869 	u8 offset = 0;
7870 
7871 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7872 		return -EINVAL;
7873 
7874 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7875 
7876 	/* find the next higher power-of-2 of num queue pairs */
7877 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7878 
7879 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7880 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7881 
7882 	/* Setup context bits for the main VSI */
7883 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7884 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7885 	memset(&ctxt, 0, sizeof(ctxt));
7886 	ctxt.seid = vsi->seid;
7887 	ctxt.pf_num = vsi->back->hw.pf_id;
7888 	ctxt.vf_num = 0;
7889 	ctxt.uplink_seid = vsi->uplink_seid;
7890 	ctxt.info = vsi->info;
7891 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7892 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7893 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7894 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7895 
7896 	/* Reconfigure RSS for main VSI with new max queue count */
7897 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7898 	ret = i40e_vsi_config_rss(vsi);
7899 	if (ret) {
7900 		dev_info(&pf->pdev->dev,
7901 			 "Failed to reconfig RSS for num_queues (%u)\n",
7902 			 vsi->rss_size);
7903 		return ret;
7904 	}
7905 	vsi->reconfig_rss = true;
7906 	dev_dbg(&vsi->back->pdev->dev,
7907 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7908 	vsi->next_base_queue = num_qps;
7909 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7910 
7911 	/* Update the VSI after updating the VSI queue-mapping
7912 	 * information
7913 	 */
7914 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7915 	if (ret) {
7916 		dev_info(&pf->pdev->dev,
7917 			 "Update vsi tc config failed, err %pe aq_err %s\n",
7918 			 ERR_PTR(ret),
7919 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7920 		return ret;
7921 	}
7922 	/* update the local VSI info with updated queue map */
7923 	i40e_vsi_update_queue_map(vsi, &ctxt);
7924 	vsi->info.valid_sections = 0;
7925 
7926 	/* Create channels for macvlans */
7927 	INIT_LIST_HEAD(&vsi->macvlan_list);
7928 	for (i = 0; i < macvlan_cnt; i++) {
7929 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7930 		if (!ch) {
7931 			ret = -ENOMEM;
7932 			goto err_free;
7933 		}
7934 		INIT_LIST_HEAD(&ch->list);
7935 		ch->num_queue_pairs = qcnt;
7936 		if (!i40e_setup_channel(pf, vsi, ch)) {
7937 			ret = -EINVAL;
7938 			kfree(ch);
7939 			goto err_free;
7940 		}
7941 		ch->parent_vsi = vsi;
7942 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7943 		vsi->macvlan_cnt++;
7944 		list_add_tail(&ch->list, &vsi->macvlan_list);
7945 	}
7946 
7947 	return ret;
7948 
7949 err_free:
7950 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7951 	i40e_free_macvlan_channels(vsi);
7952 
7953 	return ret;
7954 }
7955 
7956 /**
7957  * i40e_fwd_add - configure macvlans
7958  * @netdev: net device to configure
7959  * @vdev: macvlan netdevice
7960  **/
7961 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7962 {
7963 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7964 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7965 	struct i40e_vsi *vsi = np->vsi;
7966 	struct i40e_pf *pf = vsi->back;
7967 	struct i40e_fwd_adapter *fwd;
7968 	int avail_macvlan, ret;
7969 
7970 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7971 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7972 		return ERR_PTR(-EINVAL);
7973 	}
7974 	if (i40e_is_tc_mqprio_enabled(pf)) {
7975 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7976 		return ERR_PTR(-EINVAL);
7977 	}
7978 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7979 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7980 		return ERR_PTR(-EINVAL);
7981 	}
7982 
7983 	/* The macvlan device has to be a single Q device so that the
7984 	 * tc_to_txq field can be reused to pick the tx queue.
7985 	 */
7986 	if (netif_is_multiqueue(vdev))
7987 		return ERR_PTR(-ERANGE);
7988 
7989 	if (!vsi->macvlan_cnt) {
7990 		/* reserve bit 0 for the pf device */
7991 		set_bit(0, vsi->fwd_bitmask);
7992 
7993 		/* Try to reserve as many queues as possible for macvlans. First
7994 		 * reserve 3/4th of max vectors, then half, then quarter and
7995 		 * calculate Qs per macvlan as you go
7996 		 */
7997 		vectors = pf->num_lan_msix;
7998 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7999 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
8000 			q_per_macvlan = 4;
8001 			macvlan_cnt = (vectors - 32) / 4;
8002 		} else if (vectors <= 64 && vectors > 32) {
8003 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
8004 			q_per_macvlan = 2;
8005 			macvlan_cnt = (vectors - 16) / 2;
8006 		} else if (vectors <= 32 && vectors > 16) {
8007 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
8008 			q_per_macvlan = 1;
8009 			macvlan_cnt = vectors - 16;
8010 		} else if (vectors <= 16 && vectors > 8) {
8011 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
8012 			q_per_macvlan = 1;
8013 			macvlan_cnt = vectors - 8;
8014 		} else {
8015 			/* allocate 1 Q per macvlan and 1 Q to the PF */
8016 			q_per_macvlan = 1;
8017 			macvlan_cnt = vectors - 1;
8018 		}
8019 
8020 		if (macvlan_cnt == 0)
8021 			return ERR_PTR(-EBUSY);
8022 
8023 		/* Quiesce VSI queues */
8024 		i40e_quiesce_vsi(vsi);
8025 
8026 		/* sets up the macvlans but does not "enable" them */
8027 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8028 					  vdev);
8029 		if (ret)
8030 			return ERR_PTR(ret);
8031 
8032 		/* Unquiesce VSI */
8033 		i40e_unquiesce_vsi(vsi);
8034 	}
8035 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8036 					    vsi->macvlan_cnt);
8037 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8038 		return ERR_PTR(-EBUSY);
8039 
8040 	/* create the fwd struct */
8041 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8042 	if (!fwd)
8043 		return ERR_PTR(-ENOMEM);
8044 
8045 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8046 	fwd->bit_no = avail_macvlan;
8047 	netdev_set_sb_channel(vdev, avail_macvlan);
8048 	fwd->netdev = vdev;
8049 
8050 	if (!netif_running(netdev))
8051 		return fwd;
8052 
8053 	/* Set fwd ring up */
8054 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8055 	if (ret) {
8056 		/* unbind the queues and drop the subordinate channel config */
8057 		netdev_unbind_sb_channel(netdev, vdev);
8058 		netdev_set_sb_channel(vdev, 0);
8059 
8060 		kfree(fwd);
8061 		return ERR_PTR(-EINVAL);
8062 	}
8063 
8064 	return fwd;
8065 }
8066 
8067 /**
8068  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8069  * @vsi: the VSI we want to access
8070  */
8071 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8072 {
8073 	struct i40e_channel *ch, *ch_tmp;
8074 	struct i40e_pf *pf = vsi->back;
8075 	struct i40e_hw *hw = &pf->hw;
8076 	int aq_err, ret = 0;
8077 
8078 	if (list_empty(&vsi->macvlan_list))
8079 		return;
8080 
8081 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8082 		if (i40e_is_channel_macvlan(ch)) {
8083 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8084 						      i40e_channel_mac(ch),
8085 						      &aq_err);
8086 			if (!ret) {
8087 				/* Reset queue contexts */
8088 				i40e_reset_ch_rings(vsi, ch);
8089 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8090 				netdev_unbind_sb_channel(vsi->netdev,
8091 							 ch->fwd->netdev);
8092 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8093 				kfree(ch->fwd);
8094 				ch->fwd = NULL;
8095 			}
8096 		}
8097 	}
8098 }
8099 
8100 /**
8101  * i40e_fwd_del - delete macvlan interfaces
8102  * @netdev: net device to configure
8103  * @vdev: macvlan netdevice
8104  */
8105 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8106 {
8107 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8108 	struct i40e_fwd_adapter *fwd = vdev;
8109 	struct i40e_channel *ch, *ch_tmp;
8110 	struct i40e_vsi *vsi = np->vsi;
8111 	struct i40e_pf *pf = vsi->back;
8112 	struct i40e_hw *hw = &pf->hw;
8113 	int aq_err, ret = 0;
8114 
8115 	/* Find the channel associated with the macvlan and del mac filter */
8116 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8117 		if (i40e_is_channel_macvlan(ch) &&
8118 		    ether_addr_equal(i40e_channel_mac(ch),
8119 				     fwd->netdev->dev_addr)) {
8120 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8121 						      i40e_channel_mac(ch),
8122 						      &aq_err);
8123 			if (!ret) {
8124 				/* Reset queue contexts */
8125 				i40e_reset_ch_rings(vsi, ch);
8126 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8127 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8128 				netdev_set_sb_channel(fwd->netdev, 0);
8129 				kfree(ch->fwd);
8130 				ch->fwd = NULL;
8131 			} else {
8132 				dev_info(&pf->pdev->dev,
8133 					 "Error deleting mac filter on macvlan err %pe, aq_err %s\n",
8134 					  ERR_PTR(ret),
8135 					  i40e_aq_str(hw, aq_err));
8136 			}
8137 			break;
8138 		}
8139 	}
8140 }
8141 
8142 /**
8143  * i40e_setup_tc - configure multiple traffic classes
8144  * @netdev: net device to configure
8145  * @type_data: tc offload data
8146  **/
8147 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8148 {
8149 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8150 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8151 	struct i40e_vsi *vsi = np->vsi;
8152 	struct i40e_pf *pf = vsi->back;
8153 	u8 enabled_tc = 0, num_tc, hw;
8154 	bool need_reset = false;
8155 	int old_queue_pairs;
8156 	int ret = -EINVAL;
8157 	u16 mode;
8158 	int i;
8159 
8160 	old_queue_pairs = vsi->num_queue_pairs;
8161 	num_tc = mqprio_qopt->qopt.num_tc;
8162 	hw = mqprio_qopt->qopt.hw;
8163 	mode = mqprio_qopt->mode;
8164 	if (!hw) {
8165 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8166 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8167 		goto config_tc;
8168 	}
8169 
8170 	/* Check if MFP enabled */
8171 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8172 		netdev_info(netdev,
8173 			    "Configuring TC not supported in MFP mode\n");
8174 		return ret;
8175 	}
8176 	switch (mode) {
8177 	case TC_MQPRIO_MODE_DCB:
8178 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8179 
8180 		/* Check if DCB enabled to continue */
8181 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8182 			netdev_info(netdev,
8183 				    "DCB is not enabled for adapter\n");
8184 			return ret;
8185 		}
8186 
8187 		/* Check whether tc count is within enabled limit */
8188 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8189 			netdev_info(netdev,
8190 				    "TC count greater than enabled on link for adapter\n");
8191 			return ret;
8192 		}
8193 		break;
8194 	case TC_MQPRIO_MODE_CHANNEL:
8195 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8196 			netdev_info(netdev,
8197 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8198 			return ret;
8199 		}
8200 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8201 			return ret;
8202 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8203 		if (ret)
8204 			return ret;
8205 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8206 		       sizeof(*mqprio_qopt));
8207 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8208 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8209 		break;
8210 	default:
8211 		return -EINVAL;
8212 	}
8213 
8214 config_tc:
8215 	/* Generate TC map for number of tc requested */
8216 	for (i = 0; i < num_tc; i++)
8217 		enabled_tc |= BIT(i);
8218 
8219 	/* Requesting same TC configuration as already enabled */
8220 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8221 	    mode != TC_MQPRIO_MODE_CHANNEL)
8222 		return 0;
8223 
8224 	/* Quiesce VSI queues */
8225 	i40e_quiesce_vsi(vsi);
8226 
8227 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8228 		i40e_remove_queue_channels(vsi);
8229 
8230 	/* Configure VSI for enabled TCs */
8231 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8232 	if (ret) {
8233 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8234 			    vsi->seid);
8235 		need_reset = true;
8236 		goto exit;
8237 	} else if (enabled_tc &&
8238 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8239 		netdev_info(netdev,
8240 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8241 			    vsi->tc_config.tc_info[0].qcount);
8242 		ret = -EINVAL;
8243 		need_reset = true;
8244 		goto exit;
8245 	}
8246 
8247 	dev_info(&vsi->back->pdev->dev,
8248 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8249 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8250 
8251 	if (i40e_is_tc_mqprio_enabled(pf)) {
8252 		if (vsi->mqprio_qopt.max_rate[0]) {
8253 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8254 						  vsi->mqprio_qopt.max_rate[0]);
8255 
8256 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8257 			if (!ret) {
8258 				u64 credits = max_tx_rate;
8259 
8260 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8261 				dev_dbg(&vsi->back->pdev->dev,
8262 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8263 					max_tx_rate,
8264 					credits,
8265 					vsi->seid);
8266 			} else {
8267 				need_reset = true;
8268 				goto exit;
8269 			}
8270 		}
8271 		ret = i40e_configure_queue_channels(vsi);
8272 		if (ret) {
8273 			vsi->num_queue_pairs = old_queue_pairs;
8274 			netdev_info(netdev,
8275 				    "Failed configuring queue channels\n");
8276 			need_reset = true;
8277 			goto exit;
8278 		}
8279 	}
8280 
8281 exit:
8282 	/* Reset the configuration data to defaults, only TC0 is enabled */
8283 	if (need_reset) {
8284 		i40e_vsi_set_default_tc_config(vsi);
8285 		need_reset = false;
8286 	}
8287 
8288 	/* Unquiesce VSI */
8289 	i40e_unquiesce_vsi(vsi);
8290 	return ret;
8291 }
8292 
8293 /**
8294  * i40e_set_cld_element - sets cloud filter element data
8295  * @filter: cloud filter rule
8296  * @cld: ptr to cloud filter element data
8297  *
8298  * This is helper function to copy data into cloud filter element
8299  **/
8300 static inline void
8301 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8302 		     struct i40e_aqc_cloud_filters_element_data *cld)
8303 {
8304 	u32 ipa;
8305 	int i;
8306 
8307 	memset(cld, 0, sizeof(*cld));
8308 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8309 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8310 
8311 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8312 		return;
8313 
8314 	if (filter->n_proto == ETH_P_IPV6) {
8315 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8316 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8317 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8318 
8319 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8320 		}
8321 	} else {
8322 		ipa = be32_to_cpu(filter->dst_ipv4);
8323 
8324 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8325 	}
8326 
8327 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8328 
8329 	/* tenant_id is not supported by FW now, once the support is enabled
8330 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8331 	 */
8332 	if (filter->tenant_id)
8333 		return;
8334 }
8335 
8336 /**
8337  * i40e_add_del_cloud_filter - Add/del cloud filter
8338  * @vsi: pointer to VSI
8339  * @filter: cloud filter rule
8340  * @add: if true, add, if false, delete
8341  *
8342  * Add or delete a cloud filter for a specific flow spec.
8343  * Returns 0 if the filter were successfully added.
8344  **/
8345 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8346 			      struct i40e_cloud_filter *filter, bool add)
8347 {
8348 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8349 	struct i40e_pf *pf = vsi->back;
8350 	int ret;
8351 	static const u16 flag_table[128] = {
8352 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8353 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8354 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8355 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8356 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8357 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8358 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8359 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8360 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8361 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8362 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8363 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8364 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8365 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8366 	};
8367 
8368 	if (filter->flags >= ARRAY_SIZE(flag_table))
8369 		return I40E_ERR_CONFIG;
8370 
8371 	memset(&cld_filter, 0, sizeof(cld_filter));
8372 
8373 	/* copy element needed to add cloud filter from filter */
8374 	i40e_set_cld_element(filter, &cld_filter);
8375 
8376 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8377 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8378 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8379 
8380 	if (filter->n_proto == ETH_P_IPV6)
8381 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8382 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8383 	else
8384 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8385 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8386 
8387 	if (add)
8388 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8389 						&cld_filter, 1);
8390 	else
8391 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8392 						&cld_filter, 1);
8393 	if (ret)
8394 		dev_dbg(&pf->pdev->dev,
8395 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8396 			add ? "add" : "delete", filter->dst_port, ret,
8397 			pf->hw.aq.asq_last_status);
8398 	else
8399 		dev_info(&pf->pdev->dev,
8400 			 "%s cloud filter for VSI: %d\n",
8401 			 add ? "Added" : "Deleted", filter->seid);
8402 	return ret;
8403 }
8404 
8405 /**
8406  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8407  * @vsi: pointer to VSI
8408  * @filter: cloud filter rule
8409  * @add: if true, add, if false, delete
8410  *
8411  * Add or delete a cloud filter for a specific flow spec using big buffer.
8412  * Returns 0 if the filter were successfully added.
8413  **/
8414 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8415 				      struct i40e_cloud_filter *filter,
8416 				      bool add)
8417 {
8418 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8419 	struct i40e_pf *pf = vsi->back;
8420 	int ret;
8421 
8422 	/* Both (src/dst) valid mac_addr are not supported */
8423 	if ((is_valid_ether_addr(filter->dst_mac) &&
8424 	     is_valid_ether_addr(filter->src_mac)) ||
8425 	    (is_multicast_ether_addr(filter->dst_mac) &&
8426 	     is_multicast_ether_addr(filter->src_mac)))
8427 		return -EOPNOTSUPP;
8428 
8429 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8430 	 * ports are not supported via big buffer now.
8431 	 */
8432 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8433 		return -EOPNOTSUPP;
8434 
8435 	/* adding filter using src_port/src_ip is not supported at this stage */
8436 	if (filter->src_port ||
8437 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8438 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8439 		return -EOPNOTSUPP;
8440 
8441 	memset(&cld_filter, 0, sizeof(cld_filter));
8442 
8443 	/* copy element needed to add cloud filter from filter */
8444 	i40e_set_cld_element(filter, &cld_filter.element);
8445 
8446 	if (is_valid_ether_addr(filter->dst_mac) ||
8447 	    is_valid_ether_addr(filter->src_mac) ||
8448 	    is_multicast_ether_addr(filter->dst_mac) ||
8449 	    is_multicast_ether_addr(filter->src_mac)) {
8450 		/* MAC + IP : unsupported mode */
8451 		if (filter->dst_ipv4)
8452 			return -EOPNOTSUPP;
8453 
8454 		/* since we validated that L4 port must be valid before
8455 		 * we get here, start with respective "flags" value
8456 		 * and update if vlan is present or not
8457 		 */
8458 		cld_filter.element.flags =
8459 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8460 
8461 		if (filter->vlan_id) {
8462 			cld_filter.element.flags =
8463 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8464 		}
8465 
8466 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8467 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8468 		cld_filter.element.flags =
8469 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8470 		if (filter->n_proto == ETH_P_IPV6)
8471 			cld_filter.element.flags |=
8472 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8473 		else
8474 			cld_filter.element.flags |=
8475 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8476 	} else {
8477 		dev_err(&pf->pdev->dev,
8478 			"either mac or ip has to be valid for cloud filter\n");
8479 		return -EINVAL;
8480 	}
8481 
8482 	/* Now copy L4 port in Byte 6..7 in general fields */
8483 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8484 						be16_to_cpu(filter->dst_port);
8485 
8486 	if (add) {
8487 		/* Validate current device switch mode, change if necessary */
8488 		ret = i40e_validate_and_set_switch_mode(vsi);
8489 		if (ret) {
8490 			dev_err(&pf->pdev->dev,
8491 				"failed to set switch mode, ret %d\n",
8492 				ret);
8493 			return ret;
8494 		}
8495 
8496 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8497 						   &cld_filter, 1);
8498 	} else {
8499 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8500 						   &cld_filter, 1);
8501 	}
8502 
8503 	if (ret)
8504 		dev_dbg(&pf->pdev->dev,
8505 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8506 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8507 	else
8508 		dev_info(&pf->pdev->dev,
8509 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8510 			 add ? "add" : "delete", filter->seid,
8511 			 ntohs(filter->dst_port));
8512 	return ret;
8513 }
8514 
8515 /**
8516  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8517  * @vsi: Pointer to VSI
8518  * @f: Pointer to struct flow_cls_offload
8519  * @filter: Pointer to cloud filter structure
8520  *
8521  **/
8522 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8523 				 struct flow_cls_offload *f,
8524 				 struct i40e_cloud_filter *filter)
8525 {
8526 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8527 	struct flow_dissector *dissector = rule->match.dissector;
8528 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8529 	struct i40e_pf *pf = vsi->back;
8530 	u8 field_flags = 0;
8531 
8532 	if (dissector->used_keys &
8533 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8534 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8535 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8536 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8537 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8538 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8539 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8540 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8541 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8542 			dissector->used_keys);
8543 		return -EOPNOTSUPP;
8544 	}
8545 
8546 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8547 		struct flow_match_enc_keyid match;
8548 
8549 		flow_rule_match_enc_keyid(rule, &match);
8550 		if (match.mask->keyid != 0)
8551 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8552 
8553 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8554 	}
8555 
8556 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8557 		struct flow_match_basic match;
8558 
8559 		flow_rule_match_basic(rule, &match);
8560 		n_proto_key = ntohs(match.key->n_proto);
8561 		n_proto_mask = ntohs(match.mask->n_proto);
8562 
8563 		if (n_proto_key == ETH_P_ALL) {
8564 			n_proto_key = 0;
8565 			n_proto_mask = 0;
8566 		}
8567 		filter->n_proto = n_proto_key & n_proto_mask;
8568 		filter->ip_proto = match.key->ip_proto;
8569 	}
8570 
8571 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8572 		struct flow_match_eth_addrs match;
8573 
8574 		flow_rule_match_eth_addrs(rule, &match);
8575 
8576 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8577 		if (!is_zero_ether_addr(match.mask->dst)) {
8578 			if (is_broadcast_ether_addr(match.mask->dst)) {
8579 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8580 			} else {
8581 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8582 					match.mask->dst);
8583 				return I40E_ERR_CONFIG;
8584 			}
8585 		}
8586 
8587 		if (!is_zero_ether_addr(match.mask->src)) {
8588 			if (is_broadcast_ether_addr(match.mask->src)) {
8589 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8590 			} else {
8591 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8592 					match.mask->src);
8593 				return I40E_ERR_CONFIG;
8594 			}
8595 		}
8596 		ether_addr_copy(filter->dst_mac, match.key->dst);
8597 		ether_addr_copy(filter->src_mac, match.key->src);
8598 	}
8599 
8600 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8601 		struct flow_match_vlan match;
8602 
8603 		flow_rule_match_vlan(rule, &match);
8604 		if (match.mask->vlan_id) {
8605 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8606 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8607 
8608 			} else {
8609 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8610 					match.mask->vlan_id);
8611 				return I40E_ERR_CONFIG;
8612 			}
8613 		}
8614 
8615 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8616 	}
8617 
8618 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8619 		struct flow_match_control match;
8620 
8621 		flow_rule_match_control(rule, &match);
8622 		addr_type = match.key->addr_type;
8623 	}
8624 
8625 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8626 		struct flow_match_ipv4_addrs match;
8627 
8628 		flow_rule_match_ipv4_addrs(rule, &match);
8629 		if (match.mask->dst) {
8630 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8631 				field_flags |= I40E_CLOUD_FIELD_IIP;
8632 			} else {
8633 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8634 					&match.mask->dst);
8635 				return I40E_ERR_CONFIG;
8636 			}
8637 		}
8638 
8639 		if (match.mask->src) {
8640 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8641 				field_flags |= I40E_CLOUD_FIELD_IIP;
8642 			} else {
8643 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8644 					&match.mask->src);
8645 				return I40E_ERR_CONFIG;
8646 			}
8647 		}
8648 
8649 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8650 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8651 			return I40E_ERR_CONFIG;
8652 		}
8653 		filter->dst_ipv4 = match.key->dst;
8654 		filter->src_ipv4 = match.key->src;
8655 	}
8656 
8657 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8658 		struct flow_match_ipv6_addrs match;
8659 
8660 		flow_rule_match_ipv6_addrs(rule, &match);
8661 
8662 		/* src and dest IPV6 address should not be LOOPBACK
8663 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8664 		 */
8665 		if (ipv6_addr_loopback(&match.key->dst) ||
8666 		    ipv6_addr_loopback(&match.key->src)) {
8667 			dev_err(&pf->pdev->dev,
8668 				"Bad ipv6, addr is LOOPBACK\n");
8669 			return I40E_ERR_CONFIG;
8670 		}
8671 		if (!ipv6_addr_any(&match.mask->dst) ||
8672 		    !ipv6_addr_any(&match.mask->src))
8673 			field_flags |= I40E_CLOUD_FIELD_IIP;
8674 
8675 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8676 		       sizeof(filter->src_ipv6));
8677 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8678 		       sizeof(filter->dst_ipv6));
8679 	}
8680 
8681 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8682 		struct flow_match_ports match;
8683 
8684 		flow_rule_match_ports(rule, &match);
8685 		if (match.mask->src) {
8686 			if (match.mask->src == cpu_to_be16(0xffff)) {
8687 				field_flags |= I40E_CLOUD_FIELD_IIP;
8688 			} else {
8689 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8690 					be16_to_cpu(match.mask->src));
8691 				return I40E_ERR_CONFIG;
8692 			}
8693 		}
8694 
8695 		if (match.mask->dst) {
8696 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8697 				field_flags |= I40E_CLOUD_FIELD_IIP;
8698 			} else {
8699 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8700 					be16_to_cpu(match.mask->dst));
8701 				return I40E_ERR_CONFIG;
8702 			}
8703 		}
8704 
8705 		filter->dst_port = match.key->dst;
8706 		filter->src_port = match.key->src;
8707 
8708 		switch (filter->ip_proto) {
8709 		case IPPROTO_TCP:
8710 		case IPPROTO_UDP:
8711 			break;
8712 		default:
8713 			dev_err(&pf->pdev->dev,
8714 				"Only UDP and TCP transport are supported\n");
8715 			return -EINVAL;
8716 		}
8717 	}
8718 	filter->flags = field_flags;
8719 	return 0;
8720 }
8721 
8722 /**
8723  * i40e_handle_tclass: Forward to a traffic class on the device
8724  * @vsi: Pointer to VSI
8725  * @tc: traffic class index on the device
8726  * @filter: Pointer to cloud filter structure
8727  *
8728  **/
8729 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8730 			      struct i40e_cloud_filter *filter)
8731 {
8732 	struct i40e_channel *ch, *ch_tmp;
8733 
8734 	/* direct to a traffic class on the same device */
8735 	if (tc == 0) {
8736 		filter->seid = vsi->seid;
8737 		return 0;
8738 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8739 		if (!filter->dst_port) {
8740 			dev_err(&vsi->back->pdev->dev,
8741 				"Specify destination port to direct to traffic class that is not default\n");
8742 			return -EINVAL;
8743 		}
8744 		if (list_empty(&vsi->ch_list))
8745 			return -EINVAL;
8746 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8747 					 list) {
8748 			if (ch->seid == vsi->tc_seid_map[tc])
8749 				filter->seid = ch->seid;
8750 		}
8751 		return 0;
8752 	}
8753 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8754 	return -EINVAL;
8755 }
8756 
8757 /**
8758  * i40e_configure_clsflower - Configure tc flower filters
8759  * @vsi: Pointer to VSI
8760  * @cls_flower: Pointer to struct flow_cls_offload
8761  *
8762  **/
8763 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8764 				    struct flow_cls_offload *cls_flower)
8765 {
8766 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8767 	struct i40e_cloud_filter *filter = NULL;
8768 	struct i40e_pf *pf = vsi->back;
8769 	int err = 0;
8770 
8771 	if (tc < 0) {
8772 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8773 		return -EOPNOTSUPP;
8774 	}
8775 
8776 	if (!tc) {
8777 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8778 		return -EINVAL;
8779 	}
8780 
8781 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8782 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8783 		return -EBUSY;
8784 
8785 	if (pf->fdir_pf_active_filters ||
8786 	    (!hlist_empty(&pf->fdir_filter_list))) {
8787 		dev_err(&vsi->back->pdev->dev,
8788 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8789 		return -EINVAL;
8790 	}
8791 
8792 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8793 		dev_err(&vsi->back->pdev->dev,
8794 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8795 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8796 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8797 	}
8798 
8799 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8800 	if (!filter)
8801 		return -ENOMEM;
8802 
8803 	filter->cookie = cls_flower->cookie;
8804 
8805 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8806 	if (err < 0)
8807 		goto err;
8808 
8809 	err = i40e_handle_tclass(vsi, tc, filter);
8810 	if (err < 0)
8811 		goto err;
8812 
8813 	/* Add cloud filter */
8814 	if (filter->dst_port)
8815 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8816 	else
8817 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8818 
8819 	if (err) {
8820 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8821 			err);
8822 		goto err;
8823 	}
8824 
8825 	/* add filter to the ordered list */
8826 	INIT_HLIST_NODE(&filter->cloud_node);
8827 
8828 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8829 
8830 	pf->num_cloud_filters++;
8831 
8832 	return err;
8833 err:
8834 	kfree(filter);
8835 	return err;
8836 }
8837 
8838 /**
8839  * i40e_find_cloud_filter - Find the could filter in the list
8840  * @vsi: Pointer to VSI
8841  * @cookie: filter specific cookie
8842  *
8843  **/
8844 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8845 							unsigned long *cookie)
8846 {
8847 	struct i40e_cloud_filter *filter = NULL;
8848 	struct hlist_node *node2;
8849 
8850 	hlist_for_each_entry_safe(filter, node2,
8851 				  &vsi->back->cloud_filter_list, cloud_node)
8852 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8853 			return filter;
8854 	return NULL;
8855 }
8856 
8857 /**
8858  * i40e_delete_clsflower - Remove tc flower filters
8859  * @vsi: Pointer to VSI
8860  * @cls_flower: Pointer to struct flow_cls_offload
8861  *
8862  **/
8863 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8864 				 struct flow_cls_offload *cls_flower)
8865 {
8866 	struct i40e_cloud_filter *filter = NULL;
8867 	struct i40e_pf *pf = vsi->back;
8868 	int err = 0;
8869 
8870 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8871 
8872 	if (!filter)
8873 		return -EINVAL;
8874 
8875 	hash_del(&filter->cloud_node);
8876 
8877 	if (filter->dst_port)
8878 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8879 	else
8880 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8881 
8882 	kfree(filter);
8883 	if (err) {
8884 		dev_err(&pf->pdev->dev,
8885 			"Failed to delete cloud filter, err %pe\n",
8886 			ERR_PTR(err));
8887 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8888 	}
8889 
8890 	pf->num_cloud_filters--;
8891 	if (!pf->num_cloud_filters)
8892 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8893 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8894 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8895 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8896 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8897 		}
8898 	return 0;
8899 }
8900 
8901 /**
8902  * i40e_setup_tc_cls_flower - flower classifier offloads
8903  * @np: net device to configure
8904  * @cls_flower: offload data
8905  **/
8906 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8907 				    struct flow_cls_offload *cls_flower)
8908 {
8909 	struct i40e_vsi *vsi = np->vsi;
8910 
8911 	switch (cls_flower->command) {
8912 	case FLOW_CLS_REPLACE:
8913 		return i40e_configure_clsflower(vsi, cls_flower);
8914 	case FLOW_CLS_DESTROY:
8915 		return i40e_delete_clsflower(vsi, cls_flower);
8916 	case FLOW_CLS_STATS:
8917 		return -EOPNOTSUPP;
8918 	default:
8919 		return -EOPNOTSUPP;
8920 	}
8921 }
8922 
8923 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8924 				  void *cb_priv)
8925 {
8926 	struct i40e_netdev_priv *np = cb_priv;
8927 
8928 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8929 		return -EOPNOTSUPP;
8930 
8931 	switch (type) {
8932 	case TC_SETUP_CLSFLOWER:
8933 		return i40e_setup_tc_cls_flower(np, type_data);
8934 
8935 	default:
8936 		return -EOPNOTSUPP;
8937 	}
8938 }
8939 
8940 static LIST_HEAD(i40e_block_cb_list);
8941 
8942 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8943 			   void *type_data)
8944 {
8945 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8946 
8947 	switch (type) {
8948 	case TC_SETUP_QDISC_MQPRIO:
8949 		return i40e_setup_tc(netdev, type_data);
8950 	case TC_SETUP_BLOCK:
8951 		return flow_block_cb_setup_simple(type_data,
8952 						  &i40e_block_cb_list,
8953 						  i40e_setup_tc_block_cb,
8954 						  np, np, true);
8955 	default:
8956 		return -EOPNOTSUPP;
8957 	}
8958 }
8959 
8960 /**
8961  * i40e_open - Called when a network interface is made active
8962  * @netdev: network interface device structure
8963  *
8964  * The open entry point is called when a network interface is made
8965  * active by the system (IFF_UP).  At this point all resources needed
8966  * for transmit and receive operations are allocated, the interrupt
8967  * handler is registered with the OS, the netdev watchdog subtask is
8968  * enabled, and the stack is notified that the interface is ready.
8969  *
8970  * Returns 0 on success, negative value on failure
8971  **/
8972 int i40e_open(struct net_device *netdev)
8973 {
8974 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8975 	struct i40e_vsi *vsi = np->vsi;
8976 	struct i40e_pf *pf = vsi->back;
8977 	int err;
8978 
8979 	/* disallow open during test or if eeprom is broken */
8980 	if (test_bit(__I40E_TESTING, pf->state) ||
8981 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8982 		return -EBUSY;
8983 
8984 	netif_carrier_off(netdev);
8985 
8986 	if (i40e_force_link_state(pf, true))
8987 		return -EAGAIN;
8988 
8989 	err = i40e_vsi_open(vsi);
8990 	if (err)
8991 		return err;
8992 
8993 	/* configure global TSO hardware offload settings */
8994 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8995 						       TCP_FLAG_FIN) >> 16);
8996 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8997 						       TCP_FLAG_FIN |
8998 						       TCP_FLAG_CWR) >> 16);
8999 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
9000 	udp_tunnel_get_rx_info(netdev);
9001 
9002 	return 0;
9003 }
9004 
9005 /**
9006  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
9007  * @vsi: vsi structure
9008  *
9009  * This updates netdev's number of tx/rx queues
9010  *
9011  * Returns status of setting tx/rx queues
9012  **/
9013 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
9014 {
9015 	int ret;
9016 
9017 	ret = netif_set_real_num_rx_queues(vsi->netdev,
9018 					   vsi->num_queue_pairs);
9019 	if (ret)
9020 		return ret;
9021 
9022 	return netif_set_real_num_tx_queues(vsi->netdev,
9023 					    vsi->num_queue_pairs);
9024 }
9025 
9026 /**
9027  * i40e_vsi_open -
9028  * @vsi: the VSI to open
9029  *
9030  * Finish initialization of the VSI.
9031  *
9032  * Returns 0 on success, negative value on failure
9033  *
9034  * Note: expects to be called while under rtnl_lock()
9035  **/
9036 int i40e_vsi_open(struct i40e_vsi *vsi)
9037 {
9038 	struct i40e_pf *pf = vsi->back;
9039 	char int_name[I40E_INT_NAME_STR_LEN];
9040 	int err;
9041 
9042 	/* allocate descriptors */
9043 	err = i40e_vsi_setup_tx_resources(vsi);
9044 	if (err)
9045 		goto err_setup_tx;
9046 	err = i40e_vsi_setup_rx_resources(vsi);
9047 	if (err)
9048 		goto err_setup_rx;
9049 
9050 	err = i40e_vsi_configure(vsi);
9051 	if (err)
9052 		goto err_setup_rx;
9053 
9054 	if (vsi->netdev) {
9055 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9056 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9057 		err = i40e_vsi_request_irq(vsi, int_name);
9058 		if (err)
9059 			goto err_setup_rx;
9060 
9061 		/* Notify the stack of the actual queue counts. */
9062 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9063 		if (err)
9064 			goto err_set_queues;
9065 
9066 	} else if (vsi->type == I40E_VSI_FDIR) {
9067 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9068 			 dev_driver_string(&pf->pdev->dev),
9069 			 dev_name(&pf->pdev->dev));
9070 		err = i40e_vsi_request_irq(vsi, int_name);
9071 		if (err)
9072 			goto err_setup_rx;
9073 
9074 	} else {
9075 		err = -EINVAL;
9076 		goto err_setup_rx;
9077 	}
9078 
9079 	err = i40e_up_complete(vsi);
9080 	if (err)
9081 		goto err_up_complete;
9082 
9083 	return 0;
9084 
9085 err_up_complete:
9086 	i40e_down(vsi);
9087 err_set_queues:
9088 	i40e_vsi_free_irq(vsi);
9089 err_setup_rx:
9090 	i40e_vsi_free_rx_resources(vsi);
9091 err_setup_tx:
9092 	i40e_vsi_free_tx_resources(vsi);
9093 	if (vsi == pf->vsi[pf->lan_vsi])
9094 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9095 
9096 	return err;
9097 }
9098 
9099 /**
9100  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9101  * @pf: Pointer to PF
9102  *
9103  * This function destroys the hlist where all the Flow Director
9104  * filters were saved.
9105  **/
9106 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9107 {
9108 	struct i40e_fdir_filter *filter;
9109 	struct i40e_flex_pit *pit_entry, *tmp;
9110 	struct hlist_node *node2;
9111 
9112 	hlist_for_each_entry_safe(filter, node2,
9113 				  &pf->fdir_filter_list, fdir_node) {
9114 		hlist_del(&filter->fdir_node);
9115 		kfree(filter);
9116 	}
9117 
9118 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9119 		list_del(&pit_entry->list);
9120 		kfree(pit_entry);
9121 	}
9122 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9123 
9124 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9125 		list_del(&pit_entry->list);
9126 		kfree(pit_entry);
9127 	}
9128 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9129 
9130 	pf->fdir_pf_active_filters = 0;
9131 	i40e_reset_fdir_filter_cnt(pf);
9132 
9133 	/* Reprogram the default input set for TCP/IPv4 */
9134 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9135 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9136 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9137 
9138 	/* Reprogram the default input set for TCP/IPv6 */
9139 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9140 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9141 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9142 
9143 	/* Reprogram the default input set for UDP/IPv4 */
9144 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9145 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9146 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9147 
9148 	/* Reprogram the default input set for UDP/IPv6 */
9149 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9150 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9151 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9152 
9153 	/* Reprogram the default input set for SCTP/IPv4 */
9154 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9155 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9156 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9157 
9158 	/* Reprogram the default input set for SCTP/IPv6 */
9159 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9160 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9161 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9162 
9163 	/* Reprogram the default input set for Other/IPv4 */
9164 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9165 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9166 
9167 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9168 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9169 
9170 	/* Reprogram the default input set for Other/IPv6 */
9171 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9172 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9173 
9174 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9175 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9176 }
9177 
9178 /**
9179  * i40e_cloud_filter_exit - Cleans up the cloud filters
9180  * @pf: Pointer to PF
9181  *
9182  * This function destroys the hlist where all the cloud filters
9183  * were saved.
9184  **/
9185 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9186 {
9187 	struct i40e_cloud_filter *cfilter;
9188 	struct hlist_node *node;
9189 
9190 	hlist_for_each_entry_safe(cfilter, node,
9191 				  &pf->cloud_filter_list, cloud_node) {
9192 		hlist_del(&cfilter->cloud_node);
9193 		kfree(cfilter);
9194 	}
9195 	pf->num_cloud_filters = 0;
9196 
9197 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9198 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9199 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9200 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9201 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9202 	}
9203 }
9204 
9205 /**
9206  * i40e_close - Disables a network interface
9207  * @netdev: network interface device structure
9208  *
9209  * The close entry point is called when an interface is de-activated
9210  * by the OS.  The hardware is still under the driver's control, but
9211  * this netdev interface is disabled.
9212  *
9213  * Returns 0, this is not allowed to fail
9214  **/
9215 int i40e_close(struct net_device *netdev)
9216 {
9217 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9218 	struct i40e_vsi *vsi = np->vsi;
9219 
9220 	i40e_vsi_close(vsi);
9221 
9222 	return 0;
9223 }
9224 
9225 /**
9226  * i40e_do_reset - Start a PF or Core Reset sequence
9227  * @pf: board private structure
9228  * @reset_flags: which reset is requested
9229  * @lock_acquired: indicates whether or not the lock has been acquired
9230  * before this function was called.
9231  *
9232  * The essential difference in resets is that the PF Reset
9233  * doesn't clear the packet buffers, doesn't reset the PE
9234  * firmware, and doesn't bother the other PFs on the chip.
9235  **/
9236 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9237 {
9238 	u32 val;
9239 
9240 	/* do the biggest reset indicated */
9241 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9242 
9243 		/* Request a Global Reset
9244 		 *
9245 		 * This will start the chip's countdown to the actual full
9246 		 * chip reset event, and a warning interrupt to be sent
9247 		 * to all PFs, including the requestor.  Our handler
9248 		 * for the warning interrupt will deal with the shutdown
9249 		 * and recovery of the switch setup.
9250 		 */
9251 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9252 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9253 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9254 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9255 
9256 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9257 
9258 		/* Request a Core Reset
9259 		 *
9260 		 * Same as Global Reset, except does *not* include the MAC/PHY
9261 		 */
9262 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9263 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9264 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9265 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9266 		i40e_flush(&pf->hw);
9267 
9268 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9269 
9270 		/* Request a PF Reset
9271 		 *
9272 		 * Resets only the PF-specific registers
9273 		 *
9274 		 * This goes directly to the tear-down and rebuild of
9275 		 * the switch, since we need to do all the recovery as
9276 		 * for the Core Reset.
9277 		 */
9278 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9279 		i40e_handle_reset_warning(pf, lock_acquired);
9280 
9281 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9282 		/* Request a PF Reset
9283 		 *
9284 		 * Resets PF and reinitializes PFs VSI.
9285 		 */
9286 		i40e_prep_for_reset(pf);
9287 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9288 		dev_info(&pf->pdev->dev,
9289 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9290 			 "FW LLDP is disabled\n" :
9291 			 "FW LLDP is enabled\n");
9292 
9293 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9294 		int v;
9295 
9296 		/* Find the VSI(s) that requested a re-init */
9297 		dev_info(&pf->pdev->dev,
9298 			 "VSI reinit requested\n");
9299 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9300 			struct i40e_vsi *vsi = pf->vsi[v];
9301 
9302 			if (vsi != NULL &&
9303 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9304 					       vsi->state))
9305 				i40e_vsi_reinit_locked(pf->vsi[v]);
9306 		}
9307 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9308 		int v;
9309 
9310 		/* Find the VSI(s) that needs to be brought down */
9311 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9312 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9313 			struct i40e_vsi *vsi = pf->vsi[v];
9314 
9315 			if (vsi != NULL &&
9316 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9317 					       vsi->state)) {
9318 				set_bit(__I40E_VSI_DOWN, vsi->state);
9319 				i40e_down(vsi);
9320 			}
9321 		}
9322 	} else {
9323 		dev_info(&pf->pdev->dev,
9324 			 "bad reset request 0x%08x\n", reset_flags);
9325 	}
9326 }
9327 
9328 #ifdef CONFIG_I40E_DCB
9329 /**
9330  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9331  * @pf: board private structure
9332  * @old_cfg: current DCB config
9333  * @new_cfg: new DCB config
9334  **/
9335 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9336 			    struct i40e_dcbx_config *old_cfg,
9337 			    struct i40e_dcbx_config *new_cfg)
9338 {
9339 	bool need_reconfig = false;
9340 
9341 	/* Check if ETS configuration has changed */
9342 	if (memcmp(&new_cfg->etscfg,
9343 		   &old_cfg->etscfg,
9344 		   sizeof(new_cfg->etscfg))) {
9345 		/* If Priority Table has changed reconfig is needed */
9346 		if (memcmp(&new_cfg->etscfg.prioritytable,
9347 			   &old_cfg->etscfg.prioritytable,
9348 			   sizeof(new_cfg->etscfg.prioritytable))) {
9349 			need_reconfig = true;
9350 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9351 		}
9352 
9353 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9354 			   &old_cfg->etscfg.tcbwtable,
9355 			   sizeof(new_cfg->etscfg.tcbwtable)))
9356 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9357 
9358 		if (memcmp(&new_cfg->etscfg.tsatable,
9359 			   &old_cfg->etscfg.tsatable,
9360 			   sizeof(new_cfg->etscfg.tsatable)))
9361 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9362 	}
9363 
9364 	/* Check if PFC configuration has changed */
9365 	if (memcmp(&new_cfg->pfc,
9366 		   &old_cfg->pfc,
9367 		   sizeof(new_cfg->pfc))) {
9368 		need_reconfig = true;
9369 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9370 	}
9371 
9372 	/* Check if APP Table has changed */
9373 	if (memcmp(&new_cfg->app,
9374 		   &old_cfg->app,
9375 		   sizeof(new_cfg->app))) {
9376 		need_reconfig = true;
9377 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9378 	}
9379 
9380 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9381 	return need_reconfig;
9382 }
9383 
9384 /**
9385  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9386  * @pf: board private structure
9387  * @e: event info posted on ARQ
9388  **/
9389 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9390 				  struct i40e_arq_event_info *e)
9391 {
9392 	struct i40e_aqc_lldp_get_mib *mib =
9393 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9394 	struct i40e_hw *hw = &pf->hw;
9395 	struct i40e_dcbx_config tmp_dcbx_cfg;
9396 	bool need_reconfig = false;
9397 	int ret = 0;
9398 	u8 type;
9399 
9400 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9401 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9402 	    (hw->phy.link_info.link_speed &
9403 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9404 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9405 		/* let firmware decide if the DCB should be disabled */
9406 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9407 
9408 	/* Not DCB capable or capability disabled */
9409 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9410 		return ret;
9411 
9412 	/* Ignore if event is not for Nearest Bridge */
9413 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9414 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9415 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9416 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9417 		return ret;
9418 
9419 	/* Check MIB Type and return if event for Remote MIB update */
9420 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9421 	dev_dbg(&pf->pdev->dev,
9422 		"LLDP event mib type %s\n", type ? "remote" : "local");
9423 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9424 		/* Update the remote cached instance and return */
9425 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9426 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9427 				&hw->remote_dcbx_config);
9428 		goto exit;
9429 	}
9430 
9431 	/* Store the old configuration */
9432 	tmp_dcbx_cfg = hw->local_dcbx_config;
9433 
9434 	/* Reset the old DCBx configuration data */
9435 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9436 	/* Get updated DCBX data from firmware */
9437 	ret = i40e_get_dcb_config(&pf->hw);
9438 	if (ret) {
9439 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9440 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9441 		    (hw->phy.link_info.link_speed &
9442 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9443 			dev_warn(&pf->pdev->dev,
9444 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9445 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9446 		} else {
9447 			dev_info(&pf->pdev->dev,
9448 				 "Failed querying DCB configuration data from firmware, err %pe aq_err %s\n",
9449 				 ERR_PTR(ret),
9450 				 i40e_aq_str(&pf->hw,
9451 					     pf->hw.aq.asq_last_status));
9452 		}
9453 		goto exit;
9454 	}
9455 
9456 	/* No change detected in DCBX configs */
9457 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9458 		    sizeof(tmp_dcbx_cfg))) {
9459 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9460 		goto exit;
9461 	}
9462 
9463 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9464 					       &hw->local_dcbx_config);
9465 
9466 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9467 
9468 	if (!need_reconfig)
9469 		goto exit;
9470 
9471 	/* Enable DCB tagging only when more than one TC */
9472 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9473 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9474 	else
9475 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9476 
9477 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9478 	/* Reconfiguration needed quiesce all VSIs */
9479 	i40e_pf_quiesce_all_vsi(pf);
9480 
9481 	/* Changes in configuration update VEB/VSI */
9482 	i40e_dcb_reconfigure(pf);
9483 
9484 	ret = i40e_resume_port_tx(pf);
9485 
9486 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9487 	/* In case of error no point in resuming VSIs */
9488 	if (ret)
9489 		goto exit;
9490 
9491 	/* Wait for the PF's queues to be disabled */
9492 	ret = i40e_pf_wait_queues_disabled(pf);
9493 	if (ret) {
9494 		/* Schedule PF reset to recover */
9495 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9496 		i40e_service_event_schedule(pf);
9497 	} else {
9498 		i40e_pf_unquiesce_all_vsi(pf);
9499 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9500 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9501 	}
9502 
9503 exit:
9504 	return ret;
9505 }
9506 #endif /* CONFIG_I40E_DCB */
9507 
9508 /**
9509  * i40e_do_reset_safe - Protected reset path for userland calls.
9510  * @pf: board private structure
9511  * @reset_flags: which reset is requested
9512  *
9513  **/
9514 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9515 {
9516 	rtnl_lock();
9517 	i40e_do_reset(pf, reset_flags, true);
9518 	rtnl_unlock();
9519 }
9520 
9521 /**
9522  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9523  * @pf: board private structure
9524  * @e: event info posted on ARQ
9525  *
9526  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9527  * and VF queues
9528  **/
9529 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9530 					   struct i40e_arq_event_info *e)
9531 {
9532 	struct i40e_aqc_lan_overflow *data =
9533 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9534 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9535 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9536 	struct i40e_hw *hw = &pf->hw;
9537 	struct i40e_vf *vf;
9538 	u16 vf_id;
9539 
9540 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9541 		queue, qtx_ctl);
9542 
9543 	/* Queue belongs to VF, find the VF and issue VF reset */
9544 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9545 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9546 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9547 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9548 		vf_id -= hw->func_caps.vf_base_id;
9549 		vf = &pf->vf[vf_id];
9550 		i40e_vc_notify_vf_reset(vf);
9551 		/* Allow VF to process pending reset notification */
9552 		msleep(20);
9553 		i40e_reset_vf(vf, false);
9554 	}
9555 }
9556 
9557 /**
9558  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9559  * @pf: board private structure
9560  **/
9561 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9562 {
9563 	u32 val, fcnt_prog;
9564 
9565 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9566 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9567 	return fcnt_prog;
9568 }
9569 
9570 /**
9571  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9572  * @pf: board private structure
9573  **/
9574 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9575 {
9576 	u32 val, fcnt_prog;
9577 
9578 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9579 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9580 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9581 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9582 	return fcnt_prog;
9583 }
9584 
9585 /**
9586  * i40e_get_global_fd_count - Get total FD filters programmed on device
9587  * @pf: board private structure
9588  **/
9589 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9590 {
9591 	u32 val, fcnt_prog;
9592 
9593 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9594 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9595 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9596 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9597 	return fcnt_prog;
9598 }
9599 
9600 /**
9601  * i40e_reenable_fdir_sb - Restore FDir SB capability
9602  * @pf: board private structure
9603  **/
9604 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9605 {
9606 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9607 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9608 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9609 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9610 }
9611 
9612 /**
9613  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9614  * @pf: board private structure
9615  **/
9616 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9617 {
9618 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9619 		/* ATR uses the same filtering logic as SB rules. It only
9620 		 * functions properly if the input set mask is at the default
9621 		 * settings. It is safe to restore the default input set
9622 		 * because there are no active TCPv4 filter rules.
9623 		 */
9624 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9625 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9626 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9627 
9628 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9629 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9630 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9631 	}
9632 }
9633 
9634 /**
9635  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9636  * @pf: board private structure
9637  * @filter: FDir filter to remove
9638  */
9639 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9640 				       struct i40e_fdir_filter *filter)
9641 {
9642 	/* Update counters */
9643 	pf->fdir_pf_active_filters--;
9644 	pf->fd_inv = 0;
9645 
9646 	switch (filter->flow_type) {
9647 	case TCP_V4_FLOW:
9648 		pf->fd_tcp4_filter_cnt--;
9649 		break;
9650 	case UDP_V4_FLOW:
9651 		pf->fd_udp4_filter_cnt--;
9652 		break;
9653 	case SCTP_V4_FLOW:
9654 		pf->fd_sctp4_filter_cnt--;
9655 		break;
9656 	case TCP_V6_FLOW:
9657 		pf->fd_tcp6_filter_cnt--;
9658 		break;
9659 	case UDP_V6_FLOW:
9660 		pf->fd_udp6_filter_cnt--;
9661 		break;
9662 	case SCTP_V6_FLOW:
9663 		pf->fd_udp6_filter_cnt--;
9664 		break;
9665 	case IP_USER_FLOW:
9666 		switch (filter->ipl4_proto) {
9667 		case IPPROTO_TCP:
9668 			pf->fd_tcp4_filter_cnt--;
9669 			break;
9670 		case IPPROTO_UDP:
9671 			pf->fd_udp4_filter_cnt--;
9672 			break;
9673 		case IPPROTO_SCTP:
9674 			pf->fd_sctp4_filter_cnt--;
9675 			break;
9676 		case IPPROTO_IP:
9677 			pf->fd_ip4_filter_cnt--;
9678 			break;
9679 		}
9680 		break;
9681 	case IPV6_USER_FLOW:
9682 		switch (filter->ipl4_proto) {
9683 		case IPPROTO_TCP:
9684 			pf->fd_tcp6_filter_cnt--;
9685 			break;
9686 		case IPPROTO_UDP:
9687 			pf->fd_udp6_filter_cnt--;
9688 			break;
9689 		case IPPROTO_SCTP:
9690 			pf->fd_sctp6_filter_cnt--;
9691 			break;
9692 		case IPPROTO_IP:
9693 			pf->fd_ip6_filter_cnt--;
9694 			break;
9695 		}
9696 		break;
9697 	}
9698 
9699 	/* Remove the filter from the list and free memory */
9700 	hlist_del(&filter->fdir_node);
9701 	kfree(filter);
9702 }
9703 
9704 /**
9705  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9706  * @pf: board private structure
9707  **/
9708 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9709 {
9710 	struct i40e_fdir_filter *filter;
9711 	u32 fcnt_prog, fcnt_avail;
9712 	struct hlist_node *node;
9713 
9714 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9715 		return;
9716 
9717 	/* Check if we have enough room to re-enable FDir SB capability. */
9718 	fcnt_prog = i40e_get_global_fd_count(pf);
9719 	fcnt_avail = pf->fdir_pf_filter_count;
9720 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9721 	    (pf->fd_add_err == 0) ||
9722 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9723 		i40e_reenable_fdir_sb(pf);
9724 
9725 	/* We should wait for even more space before re-enabling ATR.
9726 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9727 	 * rules active.
9728 	 */
9729 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9730 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9731 		i40e_reenable_fdir_atr(pf);
9732 
9733 	/* if hw had a problem adding a filter, delete it */
9734 	if (pf->fd_inv > 0) {
9735 		hlist_for_each_entry_safe(filter, node,
9736 					  &pf->fdir_filter_list, fdir_node)
9737 			if (filter->fd_id == pf->fd_inv)
9738 				i40e_delete_invalid_filter(pf, filter);
9739 	}
9740 }
9741 
9742 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9743 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9744 /**
9745  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9746  * @pf: board private structure
9747  **/
9748 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9749 {
9750 	unsigned long min_flush_time;
9751 	int flush_wait_retry = 50;
9752 	bool disable_atr = false;
9753 	int fd_room;
9754 	int reg;
9755 
9756 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9757 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9758 		return;
9759 
9760 	/* If the flush is happening too quick and we have mostly SB rules we
9761 	 * should not re-enable ATR for some time.
9762 	 */
9763 	min_flush_time = pf->fd_flush_timestamp +
9764 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9765 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9766 
9767 	if (!(time_after(jiffies, min_flush_time)) &&
9768 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9769 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9770 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9771 		disable_atr = true;
9772 	}
9773 
9774 	pf->fd_flush_timestamp = jiffies;
9775 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9776 	/* flush all filters */
9777 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9778 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9779 	i40e_flush(&pf->hw);
9780 	pf->fd_flush_cnt++;
9781 	pf->fd_add_err = 0;
9782 	do {
9783 		/* Check FD flush status every 5-6msec */
9784 		usleep_range(5000, 6000);
9785 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9786 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9787 			break;
9788 	} while (flush_wait_retry--);
9789 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9790 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9791 	} else {
9792 		/* replay sideband filters */
9793 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9794 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9795 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9796 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9797 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9798 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9799 	}
9800 }
9801 
9802 /**
9803  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9804  * @pf: board private structure
9805  **/
9806 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9807 {
9808 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9809 }
9810 
9811 /**
9812  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9813  * @pf: board private structure
9814  **/
9815 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9816 {
9817 
9818 	/* if interface is down do nothing */
9819 	if (test_bit(__I40E_DOWN, pf->state))
9820 		return;
9821 
9822 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9823 		i40e_fdir_flush_and_replay(pf);
9824 
9825 	i40e_fdir_check_and_reenable(pf);
9826 
9827 }
9828 
9829 /**
9830  * i40e_vsi_link_event - notify VSI of a link event
9831  * @vsi: vsi to be notified
9832  * @link_up: link up or down
9833  **/
9834 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9835 {
9836 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9837 		return;
9838 
9839 	switch (vsi->type) {
9840 	case I40E_VSI_MAIN:
9841 		if (!vsi->netdev || !vsi->netdev_registered)
9842 			break;
9843 
9844 		if (link_up) {
9845 			netif_carrier_on(vsi->netdev);
9846 			netif_tx_wake_all_queues(vsi->netdev);
9847 		} else {
9848 			netif_carrier_off(vsi->netdev);
9849 			netif_tx_stop_all_queues(vsi->netdev);
9850 		}
9851 		break;
9852 
9853 	case I40E_VSI_SRIOV:
9854 	case I40E_VSI_VMDQ2:
9855 	case I40E_VSI_CTRL:
9856 	case I40E_VSI_IWARP:
9857 	case I40E_VSI_MIRROR:
9858 	default:
9859 		/* there is no notification for other VSIs */
9860 		break;
9861 	}
9862 }
9863 
9864 /**
9865  * i40e_veb_link_event - notify elements on the veb of a link event
9866  * @veb: veb to be notified
9867  * @link_up: link up or down
9868  **/
9869 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9870 {
9871 	struct i40e_pf *pf;
9872 	int i;
9873 
9874 	if (!veb || !veb->pf)
9875 		return;
9876 	pf = veb->pf;
9877 
9878 	/* depth first... */
9879 	for (i = 0; i < I40E_MAX_VEB; i++)
9880 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9881 			i40e_veb_link_event(pf->veb[i], link_up);
9882 
9883 	/* ... now the local VSIs */
9884 	for (i = 0; i < pf->num_alloc_vsi; i++)
9885 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9886 			i40e_vsi_link_event(pf->vsi[i], link_up);
9887 }
9888 
9889 /**
9890  * i40e_link_event - Update netif_carrier status
9891  * @pf: board private structure
9892  **/
9893 static void i40e_link_event(struct i40e_pf *pf)
9894 {
9895 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9896 	u8 new_link_speed, old_link_speed;
9897 	bool new_link, old_link;
9898 	int status;
9899 #ifdef CONFIG_I40E_DCB
9900 	int err;
9901 #endif /* CONFIG_I40E_DCB */
9902 
9903 	/* set this to force the get_link_status call to refresh state */
9904 	pf->hw.phy.get_link_info = true;
9905 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9906 	status = i40e_get_link_status(&pf->hw, &new_link);
9907 
9908 	/* On success, disable temp link polling */
9909 	if (status == I40E_SUCCESS) {
9910 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9911 	} else {
9912 		/* Enable link polling temporarily until i40e_get_link_status
9913 		 * returns I40E_SUCCESS
9914 		 */
9915 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9916 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9917 			status);
9918 		return;
9919 	}
9920 
9921 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9922 	new_link_speed = pf->hw.phy.link_info.link_speed;
9923 
9924 	if (new_link == old_link &&
9925 	    new_link_speed == old_link_speed &&
9926 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9927 	     new_link == netif_carrier_ok(vsi->netdev)))
9928 		return;
9929 
9930 	i40e_print_link_message(vsi, new_link);
9931 
9932 	/* Notify the base of the switch tree connected to
9933 	 * the link.  Floating VEBs are not notified.
9934 	 */
9935 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9936 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9937 	else
9938 		i40e_vsi_link_event(vsi, new_link);
9939 
9940 	if (pf->vf)
9941 		i40e_vc_notify_link_state(pf);
9942 
9943 	if (pf->flags & I40E_FLAG_PTP)
9944 		i40e_ptp_set_increment(pf);
9945 #ifdef CONFIG_I40E_DCB
9946 	if (new_link == old_link)
9947 		return;
9948 	/* Not SW DCB so firmware will take care of default settings */
9949 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9950 		return;
9951 
9952 	/* We cover here only link down, as after link up in case of SW DCB
9953 	 * SW LLDP agent will take care of setting it up
9954 	 */
9955 	if (!new_link) {
9956 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9957 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9958 		err = i40e_dcb_sw_default_config(pf);
9959 		if (err) {
9960 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9961 				       I40E_FLAG_DCB_ENABLED);
9962 		} else {
9963 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9964 				       DCB_CAP_DCBX_VER_IEEE;
9965 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9966 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9967 		}
9968 	}
9969 #endif /* CONFIG_I40E_DCB */
9970 }
9971 
9972 /**
9973  * i40e_watchdog_subtask - periodic checks not using event driven response
9974  * @pf: board private structure
9975  **/
9976 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9977 {
9978 	int i;
9979 
9980 	/* if interface is down do nothing */
9981 	if (test_bit(__I40E_DOWN, pf->state) ||
9982 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9983 		return;
9984 
9985 	/* make sure we don't do these things too often */
9986 	if (time_before(jiffies, (pf->service_timer_previous +
9987 				  pf->service_timer_period)))
9988 		return;
9989 	pf->service_timer_previous = jiffies;
9990 
9991 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9992 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9993 		i40e_link_event(pf);
9994 
9995 	/* Update the stats for active netdevs so the network stack
9996 	 * can look at updated numbers whenever it cares to
9997 	 */
9998 	for (i = 0; i < pf->num_alloc_vsi; i++)
9999 		if (pf->vsi[i] && pf->vsi[i]->netdev)
10000 			i40e_update_stats(pf->vsi[i]);
10001 
10002 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
10003 		/* Update the stats for the active switching components */
10004 		for (i = 0; i < I40E_MAX_VEB; i++)
10005 			if (pf->veb[i])
10006 				i40e_update_veb_stats(pf->veb[i]);
10007 	}
10008 
10009 	i40e_ptp_rx_hang(pf);
10010 	i40e_ptp_tx_hang(pf);
10011 }
10012 
10013 /**
10014  * i40e_reset_subtask - Set up for resetting the device and driver
10015  * @pf: board private structure
10016  **/
10017 static void i40e_reset_subtask(struct i40e_pf *pf)
10018 {
10019 	u32 reset_flags = 0;
10020 
10021 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10022 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10023 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10024 	}
10025 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10026 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10027 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10028 	}
10029 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10030 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10031 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10032 	}
10033 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10034 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10035 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10036 	}
10037 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10038 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10039 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10040 	}
10041 
10042 	/* If there's a recovery already waiting, it takes
10043 	 * precedence before starting a new reset sequence.
10044 	 */
10045 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10046 		i40e_prep_for_reset(pf);
10047 		i40e_reset(pf);
10048 		i40e_rebuild(pf, false, false);
10049 	}
10050 
10051 	/* If we're already down or resetting, just bail */
10052 	if (reset_flags &&
10053 	    !test_bit(__I40E_DOWN, pf->state) &&
10054 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10055 		i40e_do_reset(pf, reset_flags, false);
10056 	}
10057 }
10058 
10059 /**
10060  * i40e_handle_link_event - Handle link event
10061  * @pf: board private structure
10062  * @e: event info posted on ARQ
10063  **/
10064 static void i40e_handle_link_event(struct i40e_pf *pf,
10065 				   struct i40e_arq_event_info *e)
10066 {
10067 	struct i40e_aqc_get_link_status *status =
10068 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10069 
10070 	/* Do a new status request to re-enable LSE reporting
10071 	 * and load new status information into the hw struct
10072 	 * This completely ignores any state information
10073 	 * in the ARQ event info, instead choosing to always
10074 	 * issue the AQ update link status command.
10075 	 */
10076 	i40e_link_event(pf);
10077 
10078 	/* Check if module meets thermal requirements */
10079 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10080 		dev_err(&pf->pdev->dev,
10081 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10082 		dev_err(&pf->pdev->dev,
10083 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10084 	} else {
10085 		/* check for unqualified module, if link is down, suppress
10086 		 * the message if link was forced to be down.
10087 		 */
10088 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10089 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10090 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10091 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10092 			dev_err(&pf->pdev->dev,
10093 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10094 			dev_err(&pf->pdev->dev,
10095 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10096 		}
10097 	}
10098 }
10099 
10100 /**
10101  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10102  * @pf: board private structure
10103  **/
10104 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10105 {
10106 	struct i40e_arq_event_info event;
10107 	struct i40e_hw *hw = &pf->hw;
10108 	u16 pending, i = 0;
10109 	u16 opcode;
10110 	u32 oldval;
10111 	int ret;
10112 	u32 val;
10113 
10114 	/* Do not run clean AQ when PF reset fails */
10115 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10116 		return;
10117 
10118 	/* check for error indications */
10119 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10120 	oldval = val;
10121 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10122 		if (hw->debug_mask & I40E_DEBUG_AQ)
10123 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10124 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10125 	}
10126 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10127 		if (hw->debug_mask & I40E_DEBUG_AQ)
10128 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10129 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10130 		pf->arq_overflows++;
10131 	}
10132 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10133 		if (hw->debug_mask & I40E_DEBUG_AQ)
10134 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10135 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10136 	}
10137 	if (oldval != val)
10138 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10139 
10140 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10141 	oldval = val;
10142 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10143 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10144 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10145 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10146 	}
10147 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10148 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10149 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10150 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10151 	}
10152 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10153 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10154 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10155 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10156 	}
10157 	if (oldval != val)
10158 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10159 
10160 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10161 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10162 	if (!event.msg_buf)
10163 		return;
10164 
10165 	do {
10166 		ret = i40e_clean_arq_element(hw, &event, &pending);
10167 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10168 			break;
10169 		else if (ret) {
10170 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10171 			break;
10172 		}
10173 
10174 		opcode = le16_to_cpu(event.desc.opcode);
10175 		switch (opcode) {
10176 
10177 		case i40e_aqc_opc_get_link_status:
10178 			rtnl_lock();
10179 			i40e_handle_link_event(pf, &event);
10180 			rtnl_unlock();
10181 			break;
10182 		case i40e_aqc_opc_send_msg_to_pf:
10183 			ret = i40e_vc_process_vf_msg(pf,
10184 					le16_to_cpu(event.desc.retval),
10185 					le32_to_cpu(event.desc.cookie_high),
10186 					le32_to_cpu(event.desc.cookie_low),
10187 					event.msg_buf,
10188 					event.msg_len);
10189 			break;
10190 		case i40e_aqc_opc_lldp_update_mib:
10191 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10192 #ifdef CONFIG_I40E_DCB
10193 			rtnl_lock();
10194 			i40e_handle_lldp_event(pf, &event);
10195 			rtnl_unlock();
10196 #endif /* CONFIG_I40E_DCB */
10197 			break;
10198 		case i40e_aqc_opc_event_lan_overflow:
10199 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10200 			i40e_handle_lan_overflow_event(pf, &event);
10201 			break;
10202 		case i40e_aqc_opc_send_msg_to_peer:
10203 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10204 			break;
10205 		case i40e_aqc_opc_nvm_erase:
10206 		case i40e_aqc_opc_nvm_update:
10207 		case i40e_aqc_opc_oem_post_update:
10208 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10209 				   "ARQ NVM operation 0x%04x completed\n",
10210 				   opcode);
10211 			break;
10212 		default:
10213 			dev_info(&pf->pdev->dev,
10214 				 "ARQ: Unknown event 0x%04x ignored\n",
10215 				 opcode);
10216 			break;
10217 		}
10218 	} while (i++ < pf->adminq_work_limit);
10219 
10220 	if (i < pf->adminq_work_limit)
10221 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10222 
10223 	/* re-enable Admin queue interrupt cause */
10224 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10225 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10226 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10227 	i40e_flush(hw);
10228 
10229 	kfree(event.msg_buf);
10230 }
10231 
10232 /**
10233  * i40e_verify_eeprom - make sure eeprom is good to use
10234  * @pf: board private structure
10235  **/
10236 static void i40e_verify_eeprom(struct i40e_pf *pf)
10237 {
10238 	int err;
10239 
10240 	err = i40e_diag_eeprom_test(&pf->hw);
10241 	if (err) {
10242 		/* retry in case of garbage read */
10243 		err = i40e_diag_eeprom_test(&pf->hw);
10244 		if (err) {
10245 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10246 				 err);
10247 			set_bit(__I40E_BAD_EEPROM, pf->state);
10248 		}
10249 	}
10250 
10251 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10252 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10253 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10254 	}
10255 }
10256 
10257 /**
10258  * i40e_enable_pf_switch_lb
10259  * @pf: pointer to the PF structure
10260  *
10261  * enable switch loop back or die - no point in a return value
10262  **/
10263 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10264 {
10265 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10266 	struct i40e_vsi_context ctxt;
10267 	int ret;
10268 
10269 	ctxt.seid = pf->main_vsi_seid;
10270 	ctxt.pf_num = pf->hw.pf_id;
10271 	ctxt.vf_num = 0;
10272 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10273 	if (ret) {
10274 		dev_info(&pf->pdev->dev,
10275 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10276 			 ERR_PTR(ret),
10277 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10278 		return;
10279 	}
10280 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10281 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10282 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10283 
10284 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10285 	if (ret) {
10286 		dev_info(&pf->pdev->dev,
10287 			 "update vsi switch failed, err %pe aq_err %s\n",
10288 			 ERR_PTR(ret),
10289 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10290 	}
10291 }
10292 
10293 /**
10294  * i40e_disable_pf_switch_lb
10295  * @pf: pointer to the PF structure
10296  *
10297  * disable switch loop back or die - no point in a return value
10298  **/
10299 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10300 {
10301 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10302 	struct i40e_vsi_context ctxt;
10303 	int ret;
10304 
10305 	ctxt.seid = pf->main_vsi_seid;
10306 	ctxt.pf_num = pf->hw.pf_id;
10307 	ctxt.vf_num = 0;
10308 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10309 	if (ret) {
10310 		dev_info(&pf->pdev->dev,
10311 			 "couldn't get PF vsi config, err %pe aq_err %s\n",
10312 			 ERR_PTR(ret),
10313 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10314 		return;
10315 	}
10316 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10317 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10318 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10319 
10320 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10321 	if (ret) {
10322 		dev_info(&pf->pdev->dev,
10323 			 "update vsi switch failed, err %pe aq_err %s\n",
10324 			 ERR_PTR(ret),
10325 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10326 	}
10327 }
10328 
10329 /**
10330  * i40e_config_bridge_mode - Configure the HW bridge mode
10331  * @veb: pointer to the bridge instance
10332  *
10333  * Configure the loop back mode for the LAN VSI that is downlink to the
10334  * specified HW bridge instance. It is expected this function is called
10335  * when a new HW bridge is instantiated.
10336  **/
10337 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10338 {
10339 	struct i40e_pf *pf = veb->pf;
10340 
10341 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10342 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10343 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10344 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10345 		i40e_disable_pf_switch_lb(pf);
10346 	else
10347 		i40e_enable_pf_switch_lb(pf);
10348 }
10349 
10350 /**
10351  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10352  * @veb: pointer to the VEB instance
10353  *
10354  * This is a recursive function that first builds the attached VSIs then
10355  * recurses in to build the next layer of VEB.  We track the connections
10356  * through our own index numbers because the seid's from the HW could
10357  * change across the reset.
10358  **/
10359 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10360 {
10361 	struct i40e_vsi *ctl_vsi = NULL;
10362 	struct i40e_pf *pf = veb->pf;
10363 	int v, veb_idx;
10364 	int ret;
10365 
10366 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10367 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10368 		if (pf->vsi[v] &&
10369 		    pf->vsi[v]->veb_idx == veb->idx &&
10370 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10371 			ctl_vsi = pf->vsi[v];
10372 			break;
10373 		}
10374 	}
10375 	if (!ctl_vsi) {
10376 		dev_info(&pf->pdev->dev,
10377 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10378 		ret = -ENOENT;
10379 		goto end_reconstitute;
10380 	}
10381 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10382 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10383 	ret = i40e_add_vsi(ctl_vsi);
10384 	if (ret) {
10385 		dev_info(&pf->pdev->dev,
10386 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10387 			 veb->idx, ret);
10388 		goto end_reconstitute;
10389 	}
10390 	i40e_vsi_reset_stats(ctl_vsi);
10391 
10392 	/* create the VEB in the switch and move the VSI onto the VEB */
10393 	ret = i40e_add_veb(veb, ctl_vsi);
10394 	if (ret)
10395 		goto end_reconstitute;
10396 
10397 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10398 		veb->bridge_mode = BRIDGE_MODE_VEB;
10399 	else
10400 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10401 	i40e_config_bridge_mode(veb);
10402 
10403 	/* create the remaining VSIs attached to this VEB */
10404 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10405 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10406 			continue;
10407 
10408 		if (pf->vsi[v]->veb_idx == veb->idx) {
10409 			struct i40e_vsi *vsi = pf->vsi[v];
10410 
10411 			vsi->uplink_seid = veb->seid;
10412 			ret = i40e_add_vsi(vsi);
10413 			if (ret) {
10414 				dev_info(&pf->pdev->dev,
10415 					 "rebuild of vsi_idx %d failed: %d\n",
10416 					 v, ret);
10417 				goto end_reconstitute;
10418 			}
10419 			i40e_vsi_reset_stats(vsi);
10420 		}
10421 	}
10422 
10423 	/* create any VEBs attached to this VEB - RECURSION */
10424 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10425 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10426 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10427 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10428 			if (ret)
10429 				break;
10430 		}
10431 	}
10432 
10433 end_reconstitute:
10434 	return ret;
10435 }
10436 
10437 /**
10438  * i40e_get_capabilities - get info about the HW
10439  * @pf: the PF struct
10440  * @list_type: AQ capability to be queried
10441  **/
10442 static int i40e_get_capabilities(struct i40e_pf *pf,
10443 				 enum i40e_admin_queue_opc list_type)
10444 {
10445 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10446 	u16 data_size;
10447 	int buf_len;
10448 	int err;
10449 
10450 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10451 	do {
10452 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10453 		if (!cap_buf)
10454 			return -ENOMEM;
10455 
10456 		/* this loads the data into the hw struct for us */
10457 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10458 						    &data_size, list_type,
10459 						    NULL);
10460 		/* data loaded, buffer no longer needed */
10461 		kfree(cap_buf);
10462 
10463 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10464 			/* retry with a larger buffer */
10465 			buf_len = data_size;
10466 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10467 			dev_info(&pf->pdev->dev,
10468 				 "capability discovery failed, err %pe aq_err %s\n",
10469 				 ERR_PTR(err),
10470 				 i40e_aq_str(&pf->hw,
10471 					     pf->hw.aq.asq_last_status));
10472 			return -ENODEV;
10473 		}
10474 	} while (err);
10475 
10476 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10477 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10478 			dev_info(&pf->pdev->dev,
10479 				 "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",
10480 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10481 				 pf->hw.func_caps.num_msix_vectors,
10482 				 pf->hw.func_caps.num_msix_vectors_vf,
10483 				 pf->hw.func_caps.fd_filters_guaranteed,
10484 				 pf->hw.func_caps.fd_filters_best_effort,
10485 				 pf->hw.func_caps.num_tx_qp,
10486 				 pf->hw.func_caps.num_vsis);
10487 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10488 			dev_info(&pf->pdev->dev,
10489 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10490 				 pf->hw.dev_caps.switch_mode,
10491 				 pf->hw.dev_caps.valid_functions);
10492 			dev_info(&pf->pdev->dev,
10493 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10494 				 pf->hw.dev_caps.sr_iov_1_1,
10495 				 pf->hw.dev_caps.num_vfs);
10496 			dev_info(&pf->pdev->dev,
10497 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10498 				 pf->hw.dev_caps.num_vsis,
10499 				 pf->hw.dev_caps.num_rx_qp,
10500 				 pf->hw.dev_caps.num_tx_qp);
10501 		}
10502 	}
10503 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10504 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10505 		       + pf->hw.func_caps.num_vfs)
10506 		if (pf->hw.revision_id == 0 &&
10507 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10508 			dev_info(&pf->pdev->dev,
10509 				 "got num_vsis %d, setting num_vsis to %d\n",
10510 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10511 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10512 		}
10513 	}
10514 	return 0;
10515 }
10516 
10517 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10518 
10519 /**
10520  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10521  * @pf: board private structure
10522  **/
10523 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10524 {
10525 	struct i40e_vsi *vsi;
10526 
10527 	/* quick workaround for an NVM issue that leaves a critical register
10528 	 * uninitialized
10529 	 */
10530 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10531 		static const u32 hkey[] = {
10532 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10533 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10534 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10535 			0x95b3a76d};
10536 		int i;
10537 
10538 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10539 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10540 	}
10541 
10542 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10543 		return;
10544 
10545 	/* find existing VSI and see if it needs configuring */
10546 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10547 
10548 	/* create a new VSI if none exists */
10549 	if (!vsi) {
10550 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10551 				     pf->vsi[pf->lan_vsi]->seid, 0);
10552 		if (!vsi) {
10553 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10554 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10555 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10556 			return;
10557 		}
10558 	}
10559 
10560 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10561 }
10562 
10563 /**
10564  * i40e_fdir_teardown - release the Flow Director resources
10565  * @pf: board private structure
10566  **/
10567 static void i40e_fdir_teardown(struct i40e_pf *pf)
10568 {
10569 	struct i40e_vsi *vsi;
10570 
10571 	i40e_fdir_filter_exit(pf);
10572 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10573 	if (vsi)
10574 		i40e_vsi_release(vsi);
10575 }
10576 
10577 /**
10578  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10579  * @vsi: PF main vsi
10580  * @seid: seid of main or channel VSIs
10581  *
10582  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10583  * existed before reset
10584  **/
10585 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10586 {
10587 	struct i40e_cloud_filter *cfilter;
10588 	struct i40e_pf *pf = vsi->back;
10589 	struct hlist_node *node;
10590 	int ret;
10591 
10592 	/* Add cloud filters back if they exist */
10593 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10594 				  cloud_node) {
10595 		if (cfilter->seid != seid)
10596 			continue;
10597 
10598 		if (cfilter->dst_port)
10599 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10600 								true);
10601 		else
10602 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10603 
10604 		if (ret) {
10605 			dev_dbg(&pf->pdev->dev,
10606 				"Failed to rebuild cloud filter, err %pe aq_err %s\n",
10607 				ERR_PTR(ret),
10608 				i40e_aq_str(&pf->hw,
10609 					    pf->hw.aq.asq_last_status));
10610 			return ret;
10611 		}
10612 	}
10613 	return 0;
10614 }
10615 
10616 /**
10617  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10618  * @vsi: PF main vsi
10619  *
10620  * Rebuilds channel VSIs if they existed before reset
10621  **/
10622 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10623 {
10624 	struct i40e_channel *ch, *ch_tmp;
10625 	int ret;
10626 
10627 	if (list_empty(&vsi->ch_list))
10628 		return 0;
10629 
10630 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10631 		if (!ch->initialized)
10632 			break;
10633 		/* Proceed with creation of channel (VMDq2) VSI */
10634 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10635 		if (ret) {
10636 			dev_info(&vsi->back->pdev->dev,
10637 				 "failed to rebuild channels using uplink_seid %u\n",
10638 				 vsi->uplink_seid);
10639 			return ret;
10640 		}
10641 		/* Reconfigure TX queues using QTX_CTL register */
10642 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10643 		if (ret) {
10644 			dev_info(&vsi->back->pdev->dev,
10645 				 "failed to configure TX rings for channel %u\n",
10646 				 ch->seid);
10647 			return ret;
10648 		}
10649 		/* update 'next_base_queue' */
10650 		vsi->next_base_queue = vsi->next_base_queue +
10651 							ch->num_queue_pairs;
10652 		if (ch->max_tx_rate) {
10653 			u64 credits = ch->max_tx_rate;
10654 
10655 			if (i40e_set_bw_limit(vsi, ch->seid,
10656 					      ch->max_tx_rate))
10657 				return -EINVAL;
10658 
10659 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10660 			dev_dbg(&vsi->back->pdev->dev,
10661 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10662 				ch->max_tx_rate,
10663 				credits,
10664 				ch->seid);
10665 		}
10666 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10667 		if (ret) {
10668 			dev_dbg(&vsi->back->pdev->dev,
10669 				"Failed to rebuild cloud filters for channel VSI %u\n",
10670 				ch->seid);
10671 			return ret;
10672 		}
10673 	}
10674 	return 0;
10675 }
10676 
10677 /**
10678  * i40e_clean_xps_state - clean xps state for every tx_ring
10679  * @vsi: ptr to the VSI
10680  **/
10681 static void i40e_clean_xps_state(struct i40e_vsi *vsi)
10682 {
10683 	int i;
10684 
10685 	if (vsi->tx_rings)
10686 		for (i = 0; i < vsi->num_queue_pairs; i++)
10687 			if (vsi->tx_rings[i])
10688 				clear_bit(__I40E_TX_XPS_INIT_DONE,
10689 					  vsi->tx_rings[i]->state);
10690 }
10691 
10692 /**
10693  * i40e_prep_for_reset - prep for the core to reset
10694  * @pf: board private structure
10695  *
10696  * Close up the VFs and other things in prep for PF Reset.
10697   **/
10698 static void i40e_prep_for_reset(struct i40e_pf *pf)
10699 {
10700 	struct i40e_hw *hw = &pf->hw;
10701 	int ret = 0;
10702 	u32 v;
10703 
10704 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10705 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10706 		return;
10707 	if (i40e_check_asq_alive(&pf->hw))
10708 		i40e_vc_notify_reset(pf);
10709 
10710 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10711 
10712 	/* quiesce the VSIs and their queues that are not already DOWN */
10713 	i40e_pf_quiesce_all_vsi(pf);
10714 
10715 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10716 		if (pf->vsi[v]) {
10717 			i40e_clean_xps_state(pf->vsi[v]);
10718 			pf->vsi[v]->seid = 0;
10719 		}
10720 	}
10721 
10722 	i40e_shutdown_adminq(&pf->hw);
10723 
10724 	/* call shutdown HMC */
10725 	if (hw->hmc.hmc_obj) {
10726 		ret = i40e_shutdown_lan_hmc(hw);
10727 		if (ret)
10728 			dev_warn(&pf->pdev->dev,
10729 				 "shutdown_lan_hmc failed: %d\n", ret);
10730 	}
10731 
10732 	/* Save the current PTP time so that we can restore the time after the
10733 	 * reset completes.
10734 	 */
10735 	i40e_ptp_save_hw_time(pf);
10736 }
10737 
10738 /**
10739  * i40e_send_version - update firmware with driver version
10740  * @pf: PF struct
10741  */
10742 static void i40e_send_version(struct i40e_pf *pf)
10743 {
10744 	struct i40e_driver_version dv;
10745 
10746 	dv.major_version = 0xff;
10747 	dv.minor_version = 0xff;
10748 	dv.build_version = 0xff;
10749 	dv.subbuild_version = 0;
10750 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10751 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10752 }
10753 
10754 /**
10755  * i40e_get_oem_version - get OEM specific version information
10756  * @hw: pointer to the hardware structure
10757  **/
10758 static void i40e_get_oem_version(struct i40e_hw *hw)
10759 {
10760 	u16 block_offset = 0xffff;
10761 	u16 block_length = 0;
10762 	u16 capabilities = 0;
10763 	u16 gen_snap = 0;
10764 	u16 release = 0;
10765 
10766 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10767 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10768 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10769 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10770 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10771 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10772 #define I40E_NVM_OEM_LENGTH			3
10773 
10774 	/* Check if pointer to OEM version block is valid. */
10775 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10776 	if (block_offset == 0xffff)
10777 		return;
10778 
10779 	/* Check if OEM version block has correct length. */
10780 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10781 			   &block_length);
10782 	if (block_length < I40E_NVM_OEM_LENGTH)
10783 		return;
10784 
10785 	/* Check if OEM version format is as expected. */
10786 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10787 			   &capabilities);
10788 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10789 		return;
10790 
10791 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10792 			   &gen_snap);
10793 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10794 			   &release);
10795 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10796 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10797 }
10798 
10799 /**
10800  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10801  * @pf: board private structure
10802  **/
10803 static int i40e_reset(struct i40e_pf *pf)
10804 {
10805 	struct i40e_hw *hw = &pf->hw;
10806 	int ret;
10807 
10808 	ret = i40e_pf_reset(hw);
10809 	if (ret) {
10810 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10811 		set_bit(__I40E_RESET_FAILED, pf->state);
10812 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10813 	} else {
10814 		pf->pfr_count++;
10815 	}
10816 	return ret;
10817 }
10818 
10819 /**
10820  * i40e_rebuild - rebuild using a saved config
10821  * @pf: board private structure
10822  * @reinit: if the Main VSI needs to re-initialized.
10823  * @lock_acquired: indicates whether or not the lock has been acquired
10824  * before this function was called.
10825  **/
10826 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10827 {
10828 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10829 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10830 	struct i40e_hw *hw = &pf->hw;
10831 	int ret;
10832 	u32 val;
10833 	int v;
10834 
10835 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10836 	    is_recovery_mode_reported)
10837 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10838 
10839 	if (test_bit(__I40E_DOWN, pf->state) &&
10840 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10841 		goto clear_recovery;
10842 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10843 
10844 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10845 	ret = i40e_init_adminq(&pf->hw);
10846 	if (ret) {
10847 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %pe aq_err %s\n",
10848 			 ERR_PTR(ret),
10849 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10850 		goto clear_recovery;
10851 	}
10852 	i40e_get_oem_version(&pf->hw);
10853 
10854 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10855 		/* The following delay is necessary for firmware update. */
10856 		mdelay(1000);
10857 	}
10858 
10859 	/* re-verify the eeprom if we just had an EMP reset */
10860 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10861 		i40e_verify_eeprom(pf);
10862 
10863 	/* if we are going out of or into recovery mode we have to act
10864 	 * accordingly with regard to resources initialization
10865 	 * and deinitialization
10866 	 */
10867 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10868 		if (i40e_get_capabilities(pf,
10869 					  i40e_aqc_opc_list_func_capabilities))
10870 			goto end_unlock;
10871 
10872 		if (is_recovery_mode_reported) {
10873 			/* we're staying in recovery mode so we'll reinitialize
10874 			 * misc vector here
10875 			 */
10876 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10877 				goto end_unlock;
10878 		} else {
10879 			if (!lock_acquired)
10880 				rtnl_lock();
10881 			/* we're going out of recovery mode so we'll free
10882 			 * the IRQ allocated specifically for recovery mode
10883 			 * and restore the interrupt scheme
10884 			 */
10885 			free_irq(pf->pdev->irq, pf);
10886 			i40e_clear_interrupt_scheme(pf);
10887 			if (i40e_restore_interrupt_scheme(pf))
10888 				goto end_unlock;
10889 		}
10890 
10891 		/* tell the firmware that we're starting */
10892 		i40e_send_version(pf);
10893 
10894 		/* bail out in case recovery mode was detected, as there is
10895 		 * no need for further configuration.
10896 		 */
10897 		goto end_unlock;
10898 	}
10899 
10900 	i40e_clear_pxe_mode(hw);
10901 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10902 	if (ret)
10903 		goto end_core_reset;
10904 
10905 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10906 				hw->func_caps.num_rx_qp, 0, 0);
10907 	if (ret) {
10908 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10909 		goto end_core_reset;
10910 	}
10911 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10912 	if (ret) {
10913 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10914 		goto end_core_reset;
10915 	}
10916 
10917 #ifdef CONFIG_I40E_DCB
10918 	/* Enable FW to write a default DCB config on link-up
10919 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10920 	 * is not supported with new link speed
10921 	 */
10922 	if (i40e_is_tc_mqprio_enabled(pf)) {
10923 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10924 	} else {
10925 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10926 		    (hw->phy.link_info.link_speed &
10927 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10928 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10929 			dev_warn(&pf->pdev->dev,
10930 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10931 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10932 		} else {
10933 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10934 			ret = i40e_init_pf_dcb(pf);
10935 			if (ret) {
10936 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10937 					 ret);
10938 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10939 				/* Continue without DCB enabled */
10940 			}
10941 		}
10942 	}
10943 
10944 #endif /* CONFIG_I40E_DCB */
10945 	if (!lock_acquired)
10946 		rtnl_lock();
10947 	ret = i40e_setup_pf_switch(pf, reinit, true);
10948 	if (ret)
10949 		goto end_unlock;
10950 
10951 	/* The driver only wants link up/down and module qualification
10952 	 * reports from firmware.  Note the negative logic.
10953 	 */
10954 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10955 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10956 					 I40E_AQ_EVENT_MEDIA_NA |
10957 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10958 	if (ret)
10959 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
10960 			 ERR_PTR(ret),
10961 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10962 
10963 	/* Rebuild the VSIs and VEBs that existed before reset.
10964 	 * They are still in our local switch element arrays, so only
10965 	 * need to rebuild the switch model in the HW.
10966 	 *
10967 	 * If there were VEBs but the reconstitution failed, we'll try
10968 	 * to recover minimal use by getting the basic PF VSI working.
10969 	 */
10970 	if (vsi->uplink_seid != pf->mac_seid) {
10971 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10972 		/* find the one VEB connected to the MAC, and find orphans */
10973 		for (v = 0; v < I40E_MAX_VEB; v++) {
10974 			if (!pf->veb[v])
10975 				continue;
10976 
10977 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10978 			    pf->veb[v]->uplink_seid == 0) {
10979 				ret = i40e_reconstitute_veb(pf->veb[v]);
10980 
10981 				if (!ret)
10982 					continue;
10983 
10984 				/* If Main VEB failed, we're in deep doodoo,
10985 				 * so give up rebuilding the switch and set up
10986 				 * for minimal rebuild of PF VSI.
10987 				 * If orphan failed, we'll report the error
10988 				 * but try to keep going.
10989 				 */
10990 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10991 					dev_info(&pf->pdev->dev,
10992 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10993 						 ret);
10994 					vsi->uplink_seid = pf->mac_seid;
10995 					break;
10996 				} else if (pf->veb[v]->uplink_seid == 0) {
10997 					dev_info(&pf->pdev->dev,
10998 						 "rebuild of orphan VEB failed: %d\n",
10999 						 ret);
11000 				}
11001 			}
11002 		}
11003 	}
11004 
11005 	if (vsi->uplink_seid == pf->mac_seid) {
11006 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
11007 		/* no VEB, so rebuild only the Main VSI */
11008 		ret = i40e_add_vsi(vsi);
11009 		if (ret) {
11010 			dev_info(&pf->pdev->dev,
11011 				 "rebuild of Main VSI failed: %d\n", ret);
11012 			goto end_unlock;
11013 		}
11014 	}
11015 
11016 	if (vsi->mqprio_qopt.max_rate[0]) {
11017 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
11018 						  vsi->mqprio_qopt.max_rate[0]);
11019 		u64 credits = 0;
11020 
11021 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
11022 		if (ret)
11023 			goto end_unlock;
11024 
11025 		credits = max_tx_rate;
11026 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
11027 		dev_dbg(&vsi->back->pdev->dev,
11028 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
11029 			max_tx_rate,
11030 			credits,
11031 			vsi->seid);
11032 	}
11033 
11034 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
11035 	if (ret)
11036 		goto end_unlock;
11037 
11038 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11039 	 * for this main VSI if they exist
11040 	 */
11041 	ret = i40e_rebuild_channels(vsi);
11042 	if (ret)
11043 		goto end_unlock;
11044 
11045 	/* Reconfigure hardware for allowing smaller MSS in the case
11046 	 * of TSO, so that we avoid the MDD being fired and causing
11047 	 * a reset in the case of small MSS+TSO.
11048 	 */
11049 #define I40E_REG_MSS          0x000E64DC
11050 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11051 #define I40E_64BYTE_MSS       0x400000
11052 	val = rd32(hw, I40E_REG_MSS);
11053 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11054 		val &= ~I40E_REG_MSS_MIN_MASK;
11055 		val |= I40E_64BYTE_MSS;
11056 		wr32(hw, I40E_REG_MSS, val);
11057 	}
11058 
11059 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11060 		msleep(75);
11061 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11062 		if (ret)
11063 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
11064 				 ERR_PTR(ret),
11065 				 i40e_aq_str(&pf->hw,
11066 					     pf->hw.aq.asq_last_status));
11067 	}
11068 	/* reinit the misc interrupt */
11069 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11070 		ret = i40e_setup_misc_vector(pf);
11071 		if (ret)
11072 			goto end_unlock;
11073 	}
11074 
11075 	/* Add a filter to drop all Flow control frames from any VSI from being
11076 	 * transmitted. By doing so we stop a malicious VF from sending out
11077 	 * PAUSE or PFC frames and potentially controlling traffic for other
11078 	 * PF/VF VSIs.
11079 	 * The FW can still send Flow control frames if enabled.
11080 	 */
11081 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11082 						       pf->main_vsi_seid);
11083 
11084 	/* restart the VSIs that were rebuilt and running before the reset */
11085 	i40e_pf_unquiesce_all_vsi(pf);
11086 
11087 	/* Release the RTNL lock before we start resetting VFs */
11088 	if (!lock_acquired)
11089 		rtnl_unlock();
11090 
11091 	/* Restore promiscuous settings */
11092 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11093 	if (ret)
11094 		dev_warn(&pf->pdev->dev,
11095 			 "Failed to restore promiscuous setting: %s, err %pe aq_err %s\n",
11096 			 pf->cur_promisc ? "on" : "off",
11097 			 ERR_PTR(ret),
11098 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11099 
11100 	i40e_reset_all_vfs(pf, true);
11101 
11102 	/* tell the firmware that we're starting */
11103 	i40e_send_version(pf);
11104 
11105 	/* We've already released the lock, so don't do it again */
11106 	goto end_core_reset;
11107 
11108 end_unlock:
11109 	if (!lock_acquired)
11110 		rtnl_unlock();
11111 end_core_reset:
11112 	clear_bit(__I40E_RESET_FAILED, pf->state);
11113 clear_recovery:
11114 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11115 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11116 }
11117 
11118 /**
11119  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11120  * @pf: board private structure
11121  * @reinit: if the Main VSI needs to re-initialized.
11122  * @lock_acquired: indicates whether or not the lock has been acquired
11123  * before this function was called.
11124  **/
11125 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11126 				   bool lock_acquired)
11127 {
11128 	int ret;
11129 
11130 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11131 		return;
11132 	/* Now we wait for GRST to settle out.
11133 	 * We don't have to delete the VEBs or VSIs from the hw switch
11134 	 * because the reset will make them disappear.
11135 	 */
11136 	ret = i40e_reset(pf);
11137 	if (!ret)
11138 		i40e_rebuild(pf, reinit, lock_acquired);
11139 }
11140 
11141 /**
11142  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11143  * @pf: board private structure
11144  *
11145  * Close up the VFs and other things in prep for a Core Reset,
11146  * then get ready to rebuild the world.
11147  * @lock_acquired: indicates whether or not the lock has been acquired
11148  * before this function was called.
11149  **/
11150 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11151 {
11152 	i40e_prep_for_reset(pf);
11153 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11154 }
11155 
11156 /**
11157  * i40e_handle_mdd_event
11158  * @pf: pointer to the PF structure
11159  *
11160  * Called from the MDD irq handler to identify possibly malicious vfs
11161  **/
11162 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11163 {
11164 	struct i40e_hw *hw = &pf->hw;
11165 	bool mdd_detected = false;
11166 	struct i40e_vf *vf;
11167 	u32 reg;
11168 	int i;
11169 
11170 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11171 		return;
11172 
11173 	/* find what triggered the MDD event */
11174 	reg = rd32(hw, I40E_GL_MDET_TX);
11175 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11176 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11177 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11178 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11179 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11180 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11181 				I40E_GL_MDET_TX_EVENT_SHIFT;
11182 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11183 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11184 				pf->hw.func_caps.base_queue;
11185 		if (netif_msg_tx_err(pf))
11186 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11187 				 event, queue, pf_num, vf_num);
11188 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11189 		mdd_detected = true;
11190 	}
11191 	reg = rd32(hw, I40E_GL_MDET_RX);
11192 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11193 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11194 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11195 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11196 				I40E_GL_MDET_RX_EVENT_SHIFT;
11197 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11198 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11199 				pf->hw.func_caps.base_queue;
11200 		if (netif_msg_rx_err(pf))
11201 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11202 				 event, queue, func);
11203 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11204 		mdd_detected = true;
11205 	}
11206 
11207 	if (mdd_detected) {
11208 		reg = rd32(hw, I40E_PF_MDET_TX);
11209 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11210 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11211 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11212 		}
11213 		reg = rd32(hw, I40E_PF_MDET_RX);
11214 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11215 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11216 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11217 		}
11218 	}
11219 
11220 	/* see if one of the VFs needs its hand slapped */
11221 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11222 		vf = &(pf->vf[i]);
11223 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11224 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11225 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11226 			vf->num_mdd_events++;
11227 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11228 				 i);
11229 			dev_info(&pf->pdev->dev,
11230 				 "Use PF Control I/F to re-enable the VF\n");
11231 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11232 		}
11233 
11234 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11235 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11236 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11237 			vf->num_mdd_events++;
11238 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11239 				 i);
11240 			dev_info(&pf->pdev->dev,
11241 				 "Use PF Control I/F to re-enable the VF\n");
11242 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11243 		}
11244 	}
11245 
11246 	/* re-enable mdd interrupt cause */
11247 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11248 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11249 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11250 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11251 	i40e_flush(hw);
11252 }
11253 
11254 /**
11255  * i40e_service_task - Run the driver's async subtasks
11256  * @work: pointer to work_struct containing our data
11257  **/
11258 static void i40e_service_task(struct work_struct *work)
11259 {
11260 	struct i40e_pf *pf = container_of(work,
11261 					  struct i40e_pf,
11262 					  service_task);
11263 	unsigned long start_time = jiffies;
11264 
11265 	/* don't bother with service tasks if a reset is in progress */
11266 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11267 	    test_bit(__I40E_SUSPENDED, pf->state))
11268 		return;
11269 
11270 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11271 		return;
11272 
11273 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11274 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11275 		i40e_sync_filters_subtask(pf);
11276 		i40e_reset_subtask(pf);
11277 		i40e_handle_mdd_event(pf);
11278 		i40e_vc_process_vflr_event(pf);
11279 		i40e_watchdog_subtask(pf);
11280 		i40e_fdir_reinit_subtask(pf);
11281 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11282 			/* Client subtask will reopen next time through. */
11283 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11284 							   true);
11285 		} else {
11286 			i40e_client_subtask(pf);
11287 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11288 					       pf->state))
11289 				i40e_notify_client_of_l2_param_changes(
11290 								pf->vsi[pf->lan_vsi]);
11291 		}
11292 		i40e_sync_filters_subtask(pf);
11293 	} else {
11294 		i40e_reset_subtask(pf);
11295 	}
11296 
11297 	i40e_clean_adminq_subtask(pf);
11298 
11299 	/* flush memory to make sure state is correct before next watchdog */
11300 	smp_mb__before_atomic();
11301 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11302 
11303 	/* If the tasks have taken longer than one timer cycle or there
11304 	 * is more work to be done, reschedule the service task now
11305 	 * rather than wait for the timer to tick again.
11306 	 */
11307 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11308 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11309 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11310 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11311 		i40e_service_event_schedule(pf);
11312 }
11313 
11314 /**
11315  * i40e_service_timer - timer callback
11316  * @t: timer list pointer
11317  **/
11318 static void i40e_service_timer(struct timer_list *t)
11319 {
11320 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11321 
11322 	mod_timer(&pf->service_timer,
11323 		  round_jiffies(jiffies + pf->service_timer_period));
11324 	i40e_service_event_schedule(pf);
11325 }
11326 
11327 /**
11328  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11329  * @vsi: the VSI being configured
11330  **/
11331 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11332 {
11333 	struct i40e_pf *pf = vsi->back;
11334 
11335 	switch (vsi->type) {
11336 	case I40E_VSI_MAIN:
11337 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11338 		if (!vsi->num_tx_desc)
11339 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11340 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11341 		if (!vsi->num_rx_desc)
11342 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11343 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11344 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11345 			vsi->num_q_vectors = pf->num_lan_msix;
11346 		else
11347 			vsi->num_q_vectors = 1;
11348 
11349 		break;
11350 
11351 	case I40E_VSI_FDIR:
11352 		vsi->alloc_queue_pairs = 1;
11353 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11354 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11355 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11356 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11357 		vsi->num_q_vectors = pf->num_fdsb_msix;
11358 		break;
11359 
11360 	case I40E_VSI_VMDQ2:
11361 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11362 		if (!vsi->num_tx_desc)
11363 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11364 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11365 		if (!vsi->num_rx_desc)
11366 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11367 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11368 		vsi->num_q_vectors = pf->num_vmdq_msix;
11369 		break;
11370 
11371 	case I40E_VSI_SRIOV:
11372 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11373 		if (!vsi->num_tx_desc)
11374 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11375 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11376 		if (!vsi->num_rx_desc)
11377 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11378 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11379 		break;
11380 
11381 	default:
11382 		WARN_ON(1);
11383 		return -ENODATA;
11384 	}
11385 
11386 	if (is_kdump_kernel()) {
11387 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11388 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11389 	}
11390 
11391 	return 0;
11392 }
11393 
11394 /**
11395  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11396  * @vsi: VSI pointer
11397  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11398  *
11399  * On error: returns error code (negative)
11400  * On success: returns 0
11401  **/
11402 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11403 {
11404 	struct i40e_ring **next_rings;
11405 	int size;
11406 	int ret = 0;
11407 
11408 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11409 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11410 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11411 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11412 	if (!vsi->tx_rings)
11413 		return -ENOMEM;
11414 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11415 	if (i40e_enabled_xdp_vsi(vsi)) {
11416 		vsi->xdp_rings = next_rings;
11417 		next_rings += vsi->alloc_queue_pairs;
11418 	}
11419 	vsi->rx_rings = next_rings;
11420 
11421 	if (alloc_qvectors) {
11422 		/* allocate memory for q_vector pointers */
11423 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11424 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11425 		if (!vsi->q_vectors) {
11426 			ret = -ENOMEM;
11427 			goto err_vectors;
11428 		}
11429 	}
11430 	return ret;
11431 
11432 err_vectors:
11433 	kfree(vsi->tx_rings);
11434 	return ret;
11435 }
11436 
11437 /**
11438  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11439  * @pf: board private structure
11440  * @type: type of VSI
11441  *
11442  * On error: returns error code (negative)
11443  * On success: returns vsi index in PF (positive)
11444  **/
11445 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11446 {
11447 	int ret = -ENODEV;
11448 	struct i40e_vsi *vsi;
11449 	int vsi_idx;
11450 	int i;
11451 
11452 	/* Need to protect the allocation of the VSIs at the PF level */
11453 	mutex_lock(&pf->switch_mutex);
11454 
11455 	/* VSI list may be fragmented if VSI creation/destruction has
11456 	 * been happening.  We can afford to do a quick scan to look
11457 	 * for any free VSIs in the list.
11458 	 *
11459 	 * find next empty vsi slot, looping back around if necessary
11460 	 */
11461 	i = pf->next_vsi;
11462 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11463 		i++;
11464 	if (i >= pf->num_alloc_vsi) {
11465 		i = 0;
11466 		while (i < pf->next_vsi && pf->vsi[i])
11467 			i++;
11468 	}
11469 
11470 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11471 		vsi_idx = i;             /* Found one! */
11472 	} else {
11473 		ret = -ENODEV;
11474 		goto unlock_pf;  /* out of VSI slots! */
11475 	}
11476 	pf->next_vsi = ++i;
11477 
11478 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11479 	if (!vsi) {
11480 		ret = -ENOMEM;
11481 		goto unlock_pf;
11482 	}
11483 	vsi->type = type;
11484 	vsi->back = pf;
11485 	set_bit(__I40E_VSI_DOWN, vsi->state);
11486 	vsi->flags = 0;
11487 	vsi->idx = vsi_idx;
11488 	vsi->int_rate_limit = 0;
11489 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11490 				pf->rss_table_size : 64;
11491 	vsi->netdev_registered = false;
11492 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11493 	hash_init(vsi->mac_filter_hash);
11494 	vsi->irqs_ready = false;
11495 
11496 	if (type == I40E_VSI_MAIN) {
11497 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11498 		if (!vsi->af_xdp_zc_qps)
11499 			goto err_rings;
11500 	}
11501 
11502 	ret = i40e_set_num_rings_in_vsi(vsi);
11503 	if (ret)
11504 		goto err_rings;
11505 
11506 	ret = i40e_vsi_alloc_arrays(vsi, true);
11507 	if (ret)
11508 		goto err_rings;
11509 
11510 	/* Setup default MSIX irq handler for VSI */
11511 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11512 
11513 	/* Initialize VSI lock */
11514 	spin_lock_init(&vsi->mac_filter_hash_lock);
11515 	pf->vsi[vsi_idx] = vsi;
11516 	ret = vsi_idx;
11517 	goto unlock_pf;
11518 
11519 err_rings:
11520 	bitmap_free(vsi->af_xdp_zc_qps);
11521 	pf->next_vsi = i - 1;
11522 	kfree(vsi);
11523 unlock_pf:
11524 	mutex_unlock(&pf->switch_mutex);
11525 	return ret;
11526 }
11527 
11528 /**
11529  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11530  * @vsi: VSI pointer
11531  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11532  *
11533  * On error: returns error code (negative)
11534  * On success: returns 0
11535  **/
11536 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11537 {
11538 	/* free the ring and vector containers */
11539 	if (free_qvectors) {
11540 		kfree(vsi->q_vectors);
11541 		vsi->q_vectors = NULL;
11542 	}
11543 	kfree(vsi->tx_rings);
11544 	vsi->tx_rings = NULL;
11545 	vsi->rx_rings = NULL;
11546 	vsi->xdp_rings = NULL;
11547 }
11548 
11549 /**
11550  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11551  * and lookup table
11552  * @vsi: Pointer to VSI structure
11553  */
11554 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11555 {
11556 	if (!vsi)
11557 		return;
11558 
11559 	kfree(vsi->rss_hkey_user);
11560 	vsi->rss_hkey_user = NULL;
11561 
11562 	kfree(vsi->rss_lut_user);
11563 	vsi->rss_lut_user = NULL;
11564 }
11565 
11566 /**
11567  * i40e_vsi_clear - Deallocate the VSI provided
11568  * @vsi: the VSI being un-configured
11569  **/
11570 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11571 {
11572 	struct i40e_pf *pf;
11573 
11574 	if (!vsi)
11575 		return 0;
11576 
11577 	if (!vsi->back)
11578 		goto free_vsi;
11579 	pf = vsi->back;
11580 
11581 	mutex_lock(&pf->switch_mutex);
11582 	if (!pf->vsi[vsi->idx]) {
11583 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11584 			vsi->idx, vsi->idx, vsi->type);
11585 		goto unlock_vsi;
11586 	}
11587 
11588 	if (pf->vsi[vsi->idx] != vsi) {
11589 		dev_err(&pf->pdev->dev,
11590 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11591 			pf->vsi[vsi->idx]->idx,
11592 			pf->vsi[vsi->idx]->type,
11593 			vsi->idx, vsi->type);
11594 		goto unlock_vsi;
11595 	}
11596 
11597 	/* updates the PF for this cleared vsi */
11598 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11599 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11600 
11601 	bitmap_free(vsi->af_xdp_zc_qps);
11602 	i40e_vsi_free_arrays(vsi, true);
11603 	i40e_clear_rss_config_user(vsi);
11604 
11605 	pf->vsi[vsi->idx] = NULL;
11606 	if (vsi->idx < pf->next_vsi)
11607 		pf->next_vsi = vsi->idx;
11608 
11609 unlock_vsi:
11610 	mutex_unlock(&pf->switch_mutex);
11611 free_vsi:
11612 	kfree(vsi);
11613 
11614 	return 0;
11615 }
11616 
11617 /**
11618  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11619  * @vsi: the VSI being cleaned
11620  **/
11621 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11622 {
11623 	int i;
11624 
11625 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11626 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11627 			kfree_rcu(vsi->tx_rings[i], rcu);
11628 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11629 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11630 			if (vsi->xdp_rings)
11631 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11632 		}
11633 	}
11634 }
11635 
11636 /**
11637  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11638  * @vsi: the VSI being configured
11639  **/
11640 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11641 {
11642 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11643 	struct i40e_pf *pf = vsi->back;
11644 	struct i40e_ring *ring;
11645 
11646 	/* Set basic values in the rings to be used later during open() */
11647 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11648 		/* allocate space for both Tx and Rx in one shot */
11649 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11650 		if (!ring)
11651 			goto err_out;
11652 
11653 		ring->queue_index = i;
11654 		ring->reg_idx = vsi->base_queue + i;
11655 		ring->ring_active = false;
11656 		ring->vsi = vsi;
11657 		ring->netdev = vsi->netdev;
11658 		ring->dev = &pf->pdev->dev;
11659 		ring->count = vsi->num_tx_desc;
11660 		ring->size = 0;
11661 		ring->dcb_tc = 0;
11662 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11663 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11664 		ring->itr_setting = pf->tx_itr_default;
11665 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11666 
11667 		if (!i40e_enabled_xdp_vsi(vsi))
11668 			goto setup_rx;
11669 
11670 		ring->queue_index = vsi->alloc_queue_pairs + i;
11671 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11672 		ring->ring_active = false;
11673 		ring->vsi = vsi;
11674 		ring->netdev = NULL;
11675 		ring->dev = &pf->pdev->dev;
11676 		ring->count = vsi->num_tx_desc;
11677 		ring->size = 0;
11678 		ring->dcb_tc = 0;
11679 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11680 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11681 		set_ring_xdp(ring);
11682 		ring->itr_setting = pf->tx_itr_default;
11683 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11684 
11685 setup_rx:
11686 		ring->queue_index = i;
11687 		ring->reg_idx = vsi->base_queue + i;
11688 		ring->ring_active = false;
11689 		ring->vsi = vsi;
11690 		ring->netdev = vsi->netdev;
11691 		ring->dev = &pf->pdev->dev;
11692 		ring->count = vsi->num_rx_desc;
11693 		ring->size = 0;
11694 		ring->dcb_tc = 0;
11695 		ring->itr_setting = pf->rx_itr_default;
11696 		WRITE_ONCE(vsi->rx_rings[i], ring);
11697 	}
11698 
11699 	return 0;
11700 
11701 err_out:
11702 	i40e_vsi_clear_rings(vsi);
11703 	return -ENOMEM;
11704 }
11705 
11706 /**
11707  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11708  * @pf: board private structure
11709  * @vectors: the number of MSI-X vectors to request
11710  *
11711  * Returns the number of vectors reserved, or error
11712  **/
11713 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11714 {
11715 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11716 					I40E_MIN_MSIX, vectors);
11717 	if (vectors < 0) {
11718 		dev_info(&pf->pdev->dev,
11719 			 "MSI-X vector reservation failed: %d\n", vectors);
11720 		vectors = 0;
11721 	}
11722 
11723 	return vectors;
11724 }
11725 
11726 /**
11727  * i40e_init_msix - Setup the MSIX capability
11728  * @pf: board private structure
11729  *
11730  * Work with the OS to set up the MSIX vectors needed.
11731  *
11732  * Returns the number of vectors reserved or negative on failure
11733  **/
11734 static int i40e_init_msix(struct i40e_pf *pf)
11735 {
11736 	struct i40e_hw *hw = &pf->hw;
11737 	int cpus, extra_vectors;
11738 	int vectors_left;
11739 	int v_budget, i;
11740 	int v_actual;
11741 	int iwarp_requested = 0;
11742 
11743 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11744 		return -ENODEV;
11745 
11746 	/* The number of vectors we'll request will be comprised of:
11747 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11748 	 *   - The number of LAN queue pairs
11749 	 *	- Queues being used for RSS.
11750 	 *		We don't need as many as max_rss_size vectors.
11751 	 *		use rss_size instead in the calculation since that
11752 	 *		is governed by number of cpus in the system.
11753 	 *	- assumes symmetric Tx/Rx pairing
11754 	 *   - The number of VMDq pairs
11755 	 *   - The CPU count within the NUMA node if iWARP is enabled
11756 	 * Once we count this up, try the request.
11757 	 *
11758 	 * If we can't get what we want, we'll simplify to nearly nothing
11759 	 * and try again.  If that still fails, we punt.
11760 	 */
11761 	vectors_left = hw->func_caps.num_msix_vectors;
11762 	v_budget = 0;
11763 
11764 	/* reserve one vector for miscellaneous handler */
11765 	if (vectors_left) {
11766 		v_budget++;
11767 		vectors_left--;
11768 	}
11769 
11770 	/* reserve some vectors for the main PF traffic queues. Initially we
11771 	 * only reserve at most 50% of the available vectors, in the case that
11772 	 * the number of online CPUs is large. This ensures that we can enable
11773 	 * extra features as well. Once we've enabled the other features, we
11774 	 * will use any remaining vectors to reach as close as we can to the
11775 	 * number of online CPUs.
11776 	 */
11777 	cpus = num_online_cpus();
11778 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11779 	vectors_left -= pf->num_lan_msix;
11780 
11781 	/* reserve one vector for sideband flow director */
11782 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11783 		if (vectors_left) {
11784 			pf->num_fdsb_msix = 1;
11785 			v_budget++;
11786 			vectors_left--;
11787 		} else {
11788 			pf->num_fdsb_msix = 0;
11789 		}
11790 	}
11791 
11792 	/* can we reserve enough for iWARP? */
11793 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11794 		iwarp_requested = pf->num_iwarp_msix;
11795 
11796 		if (!vectors_left)
11797 			pf->num_iwarp_msix = 0;
11798 		else if (vectors_left < pf->num_iwarp_msix)
11799 			pf->num_iwarp_msix = 1;
11800 		v_budget += pf->num_iwarp_msix;
11801 		vectors_left -= pf->num_iwarp_msix;
11802 	}
11803 
11804 	/* any vectors left over go for VMDq support */
11805 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11806 		if (!vectors_left) {
11807 			pf->num_vmdq_msix = 0;
11808 			pf->num_vmdq_qps = 0;
11809 		} else {
11810 			int vmdq_vecs_wanted =
11811 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11812 			int vmdq_vecs =
11813 				min_t(int, vectors_left, vmdq_vecs_wanted);
11814 
11815 			/* if we're short on vectors for what's desired, we limit
11816 			 * the queues per vmdq.  If this is still more than are
11817 			 * available, the user will need to change the number of
11818 			 * queues/vectors used by the PF later with the ethtool
11819 			 * channels command
11820 			 */
11821 			if (vectors_left < vmdq_vecs_wanted) {
11822 				pf->num_vmdq_qps = 1;
11823 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11824 				vmdq_vecs = min_t(int,
11825 						  vectors_left,
11826 						  vmdq_vecs_wanted);
11827 			}
11828 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11829 
11830 			v_budget += vmdq_vecs;
11831 			vectors_left -= vmdq_vecs;
11832 		}
11833 	}
11834 
11835 	/* On systems with a large number of SMP cores, we previously limited
11836 	 * the number of vectors for num_lan_msix to be at most 50% of the
11837 	 * available vectors, to allow for other features. Now, we add back
11838 	 * the remaining vectors. However, we ensure that the total
11839 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11840 	 * calculate the number of vectors we can add without going over the
11841 	 * cap of CPUs. For systems with a small number of CPUs this will be
11842 	 * zero.
11843 	 */
11844 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11845 	pf->num_lan_msix += extra_vectors;
11846 	vectors_left -= extra_vectors;
11847 
11848 	WARN(vectors_left < 0,
11849 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11850 
11851 	v_budget += pf->num_lan_msix;
11852 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11853 				   GFP_KERNEL);
11854 	if (!pf->msix_entries)
11855 		return -ENOMEM;
11856 
11857 	for (i = 0; i < v_budget; i++)
11858 		pf->msix_entries[i].entry = i;
11859 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11860 
11861 	if (v_actual < I40E_MIN_MSIX) {
11862 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11863 		kfree(pf->msix_entries);
11864 		pf->msix_entries = NULL;
11865 		pci_disable_msix(pf->pdev);
11866 		return -ENODEV;
11867 
11868 	} else if (v_actual == I40E_MIN_MSIX) {
11869 		/* Adjust for minimal MSIX use */
11870 		pf->num_vmdq_vsis = 0;
11871 		pf->num_vmdq_qps = 0;
11872 		pf->num_lan_qps = 1;
11873 		pf->num_lan_msix = 1;
11874 
11875 	} else if (v_actual != v_budget) {
11876 		/* If we have limited resources, we will start with no vectors
11877 		 * for the special features and then allocate vectors to some
11878 		 * of these features based on the policy and at the end disable
11879 		 * the features that did not get any vectors.
11880 		 */
11881 		int vec;
11882 
11883 		dev_info(&pf->pdev->dev,
11884 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11885 			 v_actual, v_budget);
11886 		/* reserve the misc vector */
11887 		vec = v_actual - 1;
11888 
11889 		/* Scale vector usage down */
11890 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11891 		pf->num_vmdq_vsis = 1;
11892 		pf->num_vmdq_qps = 1;
11893 
11894 		/* partition out the remaining vectors */
11895 		switch (vec) {
11896 		case 2:
11897 			pf->num_lan_msix = 1;
11898 			break;
11899 		case 3:
11900 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11901 				pf->num_lan_msix = 1;
11902 				pf->num_iwarp_msix = 1;
11903 			} else {
11904 				pf->num_lan_msix = 2;
11905 			}
11906 			break;
11907 		default:
11908 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11909 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11910 						 iwarp_requested);
11911 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11912 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11913 			} else {
11914 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11915 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11916 			}
11917 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11918 				pf->num_fdsb_msix = 1;
11919 				vec--;
11920 			}
11921 			pf->num_lan_msix = min_t(int,
11922 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11923 							      pf->num_lan_msix);
11924 			pf->num_lan_qps = pf->num_lan_msix;
11925 			break;
11926 		}
11927 	}
11928 
11929 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11930 	    (pf->num_fdsb_msix == 0)) {
11931 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11932 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11933 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11934 	}
11935 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11936 	    (pf->num_vmdq_msix == 0)) {
11937 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11938 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11939 	}
11940 
11941 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11942 	    (pf->num_iwarp_msix == 0)) {
11943 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11944 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11945 	}
11946 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11947 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11948 		   pf->num_lan_msix,
11949 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11950 		   pf->num_fdsb_msix,
11951 		   pf->num_iwarp_msix);
11952 
11953 	return v_actual;
11954 }
11955 
11956 /**
11957  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11958  * @vsi: the VSI being configured
11959  * @v_idx: index of the vector in the vsi struct
11960  *
11961  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11962  **/
11963 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11964 {
11965 	struct i40e_q_vector *q_vector;
11966 
11967 	/* allocate q_vector */
11968 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11969 	if (!q_vector)
11970 		return -ENOMEM;
11971 
11972 	q_vector->vsi = vsi;
11973 	q_vector->v_idx = v_idx;
11974 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11975 
11976 	if (vsi->netdev)
11977 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11978 
11979 	/* tie q_vector and vsi together */
11980 	vsi->q_vectors[v_idx] = q_vector;
11981 
11982 	return 0;
11983 }
11984 
11985 /**
11986  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11987  * @vsi: the VSI being configured
11988  *
11989  * We allocate one q_vector per queue interrupt.  If allocation fails we
11990  * return -ENOMEM.
11991  **/
11992 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11993 {
11994 	struct i40e_pf *pf = vsi->back;
11995 	int err, v_idx, num_q_vectors;
11996 
11997 	/* if not MSIX, give the one vector only to the LAN VSI */
11998 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11999 		num_q_vectors = vsi->num_q_vectors;
12000 	else if (vsi == pf->vsi[pf->lan_vsi])
12001 		num_q_vectors = 1;
12002 	else
12003 		return -EINVAL;
12004 
12005 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
12006 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
12007 		if (err)
12008 			goto err_out;
12009 	}
12010 
12011 	return 0;
12012 
12013 err_out:
12014 	while (v_idx--)
12015 		i40e_free_q_vector(vsi, v_idx);
12016 
12017 	return err;
12018 }
12019 
12020 /**
12021  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
12022  * @pf: board private structure to initialize
12023  **/
12024 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
12025 {
12026 	int vectors = 0;
12027 	ssize_t size;
12028 
12029 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12030 		vectors = i40e_init_msix(pf);
12031 		if (vectors < 0) {
12032 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
12033 				       I40E_FLAG_IWARP_ENABLED	|
12034 				       I40E_FLAG_RSS_ENABLED	|
12035 				       I40E_FLAG_DCB_CAPABLE	|
12036 				       I40E_FLAG_DCB_ENABLED	|
12037 				       I40E_FLAG_SRIOV_ENABLED	|
12038 				       I40E_FLAG_FD_SB_ENABLED	|
12039 				       I40E_FLAG_FD_ATR_ENABLED	|
12040 				       I40E_FLAG_VMDQ_ENABLED);
12041 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12042 
12043 			/* rework the queue expectations without MSIX */
12044 			i40e_determine_queue_usage(pf);
12045 		}
12046 	}
12047 
12048 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12049 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12050 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12051 		vectors = pci_enable_msi(pf->pdev);
12052 		if (vectors < 0) {
12053 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12054 				 vectors);
12055 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12056 		}
12057 		vectors = 1;  /* one MSI or Legacy vector */
12058 	}
12059 
12060 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12061 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12062 
12063 	/* set up vector assignment tracking */
12064 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12065 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12066 	if (!pf->irq_pile)
12067 		return -ENOMEM;
12068 
12069 	pf->irq_pile->num_entries = vectors;
12070 
12071 	/* track first vector for misc interrupts, ignore return */
12072 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12073 
12074 	return 0;
12075 }
12076 
12077 /**
12078  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12079  * @pf: private board data structure
12080  *
12081  * Restore the interrupt scheme that was cleared when we suspended the
12082  * device. This should be called during resume to re-allocate the q_vectors
12083  * and reacquire IRQs.
12084  */
12085 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12086 {
12087 	int err, i;
12088 
12089 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12090 	 * scheme. We need to re-enabled them here in order to attempt to
12091 	 * re-acquire the MSI or MSI-X vectors
12092 	 */
12093 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12094 
12095 	err = i40e_init_interrupt_scheme(pf);
12096 	if (err)
12097 		return err;
12098 
12099 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12100 	 * rings together again.
12101 	 */
12102 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12103 		if (pf->vsi[i]) {
12104 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12105 			if (err)
12106 				goto err_unwind;
12107 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12108 		}
12109 	}
12110 
12111 	err = i40e_setup_misc_vector(pf);
12112 	if (err)
12113 		goto err_unwind;
12114 
12115 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12116 		i40e_client_update_msix_info(pf);
12117 
12118 	return 0;
12119 
12120 err_unwind:
12121 	while (i--) {
12122 		if (pf->vsi[i])
12123 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12124 	}
12125 
12126 	return err;
12127 }
12128 
12129 /**
12130  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12131  * non queue events in recovery mode
12132  * @pf: board private structure
12133  *
12134  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12135  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12136  * This is handled differently than in recovery mode since no Tx/Rx resources
12137  * are being allocated.
12138  **/
12139 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12140 {
12141 	int err;
12142 
12143 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12144 		err = i40e_setup_misc_vector(pf);
12145 
12146 		if (err) {
12147 			dev_info(&pf->pdev->dev,
12148 				 "MSI-X misc vector request failed, error %d\n",
12149 				 err);
12150 			return err;
12151 		}
12152 	} else {
12153 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12154 
12155 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12156 				  pf->int_name, pf);
12157 
12158 		if (err) {
12159 			dev_info(&pf->pdev->dev,
12160 				 "MSI/legacy misc vector request failed, error %d\n",
12161 				 err);
12162 			return err;
12163 		}
12164 		i40e_enable_misc_int_causes(pf);
12165 		i40e_irq_dynamic_enable_icr0(pf);
12166 	}
12167 
12168 	return 0;
12169 }
12170 
12171 /**
12172  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12173  * @pf: board private structure
12174  *
12175  * This sets up the handler for MSIX 0, which is used to manage the
12176  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12177  * when in MSI or Legacy interrupt mode.
12178  **/
12179 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12180 {
12181 	struct i40e_hw *hw = &pf->hw;
12182 	int err = 0;
12183 
12184 	/* Only request the IRQ once, the first time through. */
12185 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12186 		err = request_irq(pf->msix_entries[0].vector,
12187 				  i40e_intr, 0, pf->int_name, pf);
12188 		if (err) {
12189 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12190 			dev_info(&pf->pdev->dev,
12191 				 "request_irq for %s failed: %d\n",
12192 				 pf->int_name, err);
12193 			return -EFAULT;
12194 		}
12195 	}
12196 
12197 	i40e_enable_misc_int_causes(pf);
12198 
12199 	/* associate no queues to the misc vector */
12200 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12201 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12202 
12203 	i40e_flush(hw);
12204 
12205 	i40e_irq_dynamic_enable_icr0(pf);
12206 
12207 	return err;
12208 }
12209 
12210 /**
12211  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12212  * @vsi: Pointer to vsi structure
12213  * @seed: Buffter to store the hash keys
12214  * @lut: Buffer to store the lookup table entries
12215  * @lut_size: Size of buffer to store the lookup table entries
12216  *
12217  * Return 0 on success, negative on failure
12218  */
12219 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12220 			   u8 *lut, u16 lut_size)
12221 {
12222 	struct i40e_pf *pf = vsi->back;
12223 	struct i40e_hw *hw = &pf->hw;
12224 	int ret = 0;
12225 
12226 	if (seed) {
12227 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12228 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12229 		if (ret) {
12230 			dev_info(&pf->pdev->dev,
12231 				 "Cannot get RSS key, err %pe aq_err %s\n",
12232 				 ERR_PTR(ret),
12233 				 i40e_aq_str(&pf->hw,
12234 					     pf->hw.aq.asq_last_status));
12235 			return ret;
12236 		}
12237 	}
12238 
12239 	if (lut) {
12240 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12241 
12242 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12243 		if (ret) {
12244 			dev_info(&pf->pdev->dev,
12245 				 "Cannot get RSS lut, err %pe aq_err %s\n",
12246 				 ERR_PTR(ret),
12247 				 i40e_aq_str(&pf->hw,
12248 					     pf->hw.aq.asq_last_status));
12249 			return ret;
12250 		}
12251 	}
12252 
12253 	return ret;
12254 }
12255 
12256 /**
12257  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12258  * @vsi: Pointer to vsi structure
12259  * @seed: RSS hash seed
12260  * @lut: Lookup table
12261  * @lut_size: Lookup table size
12262  *
12263  * Returns 0 on success, negative on failure
12264  **/
12265 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12266 			       const u8 *lut, u16 lut_size)
12267 {
12268 	struct i40e_pf *pf = vsi->back;
12269 	struct i40e_hw *hw = &pf->hw;
12270 	u16 vf_id = vsi->vf_id;
12271 	u8 i;
12272 
12273 	/* Fill out hash function seed */
12274 	if (seed) {
12275 		u32 *seed_dw = (u32 *)seed;
12276 
12277 		if (vsi->type == I40E_VSI_MAIN) {
12278 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12279 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12280 		} else if (vsi->type == I40E_VSI_SRIOV) {
12281 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12282 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12283 		} else {
12284 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12285 		}
12286 	}
12287 
12288 	if (lut) {
12289 		u32 *lut_dw = (u32 *)lut;
12290 
12291 		if (vsi->type == I40E_VSI_MAIN) {
12292 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12293 				return -EINVAL;
12294 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12295 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12296 		} else if (vsi->type == I40E_VSI_SRIOV) {
12297 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12298 				return -EINVAL;
12299 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12300 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12301 		} else {
12302 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12303 		}
12304 	}
12305 	i40e_flush(hw);
12306 
12307 	return 0;
12308 }
12309 
12310 /**
12311  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12312  * @vsi: Pointer to VSI structure
12313  * @seed: Buffer to store the keys
12314  * @lut: Buffer to store the lookup table entries
12315  * @lut_size: Size of buffer to store the lookup table entries
12316  *
12317  * Returns 0 on success, negative on failure
12318  */
12319 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12320 			    u8 *lut, u16 lut_size)
12321 {
12322 	struct i40e_pf *pf = vsi->back;
12323 	struct i40e_hw *hw = &pf->hw;
12324 	u16 i;
12325 
12326 	if (seed) {
12327 		u32 *seed_dw = (u32 *)seed;
12328 
12329 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12330 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12331 	}
12332 	if (lut) {
12333 		u32 *lut_dw = (u32 *)lut;
12334 
12335 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12336 			return -EINVAL;
12337 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12338 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12339 	}
12340 
12341 	return 0;
12342 }
12343 
12344 /**
12345  * i40e_config_rss - Configure RSS keys and lut
12346  * @vsi: Pointer to VSI structure
12347  * @seed: RSS hash seed
12348  * @lut: Lookup table
12349  * @lut_size: Lookup table size
12350  *
12351  * Returns 0 on success, negative on failure
12352  */
12353 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12354 {
12355 	struct i40e_pf *pf = vsi->back;
12356 
12357 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12358 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12359 	else
12360 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12361 }
12362 
12363 /**
12364  * i40e_get_rss - Get RSS keys and lut
12365  * @vsi: Pointer to VSI structure
12366  * @seed: Buffer to store the keys
12367  * @lut: Buffer to store the lookup table entries
12368  * @lut_size: Size of buffer to store the lookup table entries
12369  *
12370  * Returns 0 on success, negative on failure
12371  */
12372 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12373 {
12374 	struct i40e_pf *pf = vsi->back;
12375 
12376 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12377 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12378 	else
12379 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12380 }
12381 
12382 /**
12383  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12384  * @pf: Pointer to board private structure
12385  * @lut: Lookup table
12386  * @rss_table_size: Lookup table size
12387  * @rss_size: Range of queue number for hashing
12388  */
12389 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12390 		       u16 rss_table_size, u16 rss_size)
12391 {
12392 	u16 i;
12393 
12394 	for (i = 0; i < rss_table_size; i++)
12395 		lut[i] = i % rss_size;
12396 }
12397 
12398 /**
12399  * i40e_pf_config_rss - Prepare for RSS if used
12400  * @pf: board private structure
12401  **/
12402 static int i40e_pf_config_rss(struct i40e_pf *pf)
12403 {
12404 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12405 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12406 	u8 *lut;
12407 	struct i40e_hw *hw = &pf->hw;
12408 	u32 reg_val;
12409 	u64 hena;
12410 	int ret;
12411 
12412 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12413 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12414 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12415 	hena |= i40e_pf_get_default_rss_hena(pf);
12416 
12417 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12418 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12419 
12420 	/* Determine the RSS table size based on the hardware capabilities */
12421 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12422 	reg_val = (pf->rss_table_size == 512) ?
12423 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12424 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12425 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12426 
12427 	/* Determine the RSS size of the VSI */
12428 	if (!vsi->rss_size) {
12429 		u16 qcount;
12430 		/* If the firmware does something weird during VSI init, we
12431 		 * could end up with zero TCs. Check for that to avoid
12432 		 * divide-by-zero. It probably won't pass traffic, but it also
12433 		 * won't panic.
12434 		 */
12435 		qcount = vsi->num_queue_pairs /
12436 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12437 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12438 	}
12439 	if (!vsi->rss_size)
12440 		return -EINVAL;
12441 
12442 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12443 	if (!lut)
12444 		return -ENOMEM;
12445 
12446 	/* Use user configured lut if there is one, otherwise use default */
12447 	if (vsi->rss_lut_user)
12448 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12449 	else
12450 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12451 
12452 	/* Use user configured hash key if there is one, otherwise
12453 	 * use default.
12454 	 */
12455 	if (vsi->rss_hkey_user)
12456 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12457 	else
12458 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12459 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12460 	kfree(lut);
12461 
12462 	return ret;
12463 }
12464 
12465 /**
12466  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12467  * @pf: board private structure
12468  * @queue_count: the requested queue count for rss.
12469  *
12470  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12471  * count which may be different from the requested queue count.
12472  * Note: expects to be called while under rtnl_lock()
12473  **/
12474 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12475 {
12476 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12477 	int new_rss_size;
12478 
12479 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12480 		return 0;
12481 
12482 	queue_count = min_t(int, queue_count, num_online_cpus());
12483 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12484 
12485 	if (queue_count != vsi->num_queue_pairs) {
12486 		u16 qcount;
12487 
12488 		vsi->req_queue_pairs = queue_count;
12489 		i40e_prep_for_reset(pf);
12490 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12491 			return pf->alloc_rss_size;
12492 
12493 		pf->alloc_rss_size = new_rss_size;
12494 
12495 		i40e_reset_and_rebuild(pf, true, true);
12496 
12497 		/* Discard the user configured hash keys and lut, if less
12498 		 * queues are enabled.
12499 		 */
12500 		if (queue_count < vsi->rss_size) {
12501 			i40e_clear_rss_config_user(vsi);
12502 			dev_dbg(&pf->pdev->dev,
12503 				"discard user configured hash keys and lut\n");
12504 		}
12505 
12506 		/* Reset vsi->rss_size, as number of enabled queues changed */
12507 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12508 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12509 
12510 		i40e_pf_config_rss(pf);
12511 	}
12512 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12513 		 vsi->req_queue_pairs, pf->rss_size_max);
12514 	return pf->alloc_rss_size;
12515 }
12516 
12517 /**
12518  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12519  * @pf: board private structure
12520  **/
12521 int i40e_get_partition_bw_setting(struct i40e_pf *pf)
12522 {
12523 	bool min_valid, max_valid;
12524 	u32 max_bw, min_bw;
12525 	int status;
12526 
12527 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12528 					   &min_valid, &max_valid);
12529 
12530 	if (!status) {
12531 		if (min_valid)
12532 			pf->min_bw = min_bw;
12533 		if (max_valid)
12534 			pf->max_bw = max_bw;
12535 	}
12536 
12537 	return status;
12538 }
12539 
12540 /**
12541  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12542  * @pf: board private structure
12543  **/
12544 int i40e_set_partition_bw_setting(struct i40e_pf *pf)
12545 {
12546 	struct i40e_aqc_configure_partition_bw_data bw_data;
12547 	int status;
12548 
12549 	memset(&bw_data, 0, sizeof(bw_data));
12550 
12551 	/* Set the valid bit for this PF */
12552 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12553 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12554 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12555 
12556 	/* Set the new bandwidths */
12557 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12558 
12559 	return status;
12560 }
12561 
12562 /**
12563  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12564  * @pf: board private structure
12565  **/
12566 int i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12567 {
12568 	/* Commit temporary BW setting to permanent NVM image */
12569 	enum i40e_admin_queue_err last_aq_status;
12570 	u16 nvm_word;
12571 	int ret;
12572 
12573 	if (pf->hw.partition_id != 1) {
12574 		dev_info(&pf->pdev->dev,
12575 			 "Commit BW only works on partition 1! This is partition %d",
12576 			 pf->hw.partition_id);
12577 		ret = I40E_NOT_SUPPORTED;
12578 		goto bw_commit_out;
12579 	}
12580 
12581 	/* Acquire NVM for read access */
12582 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12583 	last_aq_status = pf->hw.aq.asq_last_status;
12584 	if (ret) {
12585 		dev_info(&pf->pdev->dev,
12586 			 "Cannot acquire NVM for read access, err %pe aq_err %s\n",
12587 			 ERR_PTR(ret),
12588 			 i40e_aq_str(&pf->hw, last_aq_status));
12589 		goto bw_commit_out;
12590 	}
12591 
12592 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12593 	ret = i40e_aq_read_nvm(&pf->hw,
12594 			       I40E_SR_NVM_CONTROL_WORD,
12595 			       0x10, sizeof(nvm_word), &nvm_word,
12596 			       false, NULL);
12597 	/* Save off last admin queue command status before releasing
12598 	 * the NVM
12599 	 */
12600 	last_aq_status = pf->hw.aq.asq_last_status;
12601 	i40e_release_nvm(&pf->hw);
12602 	if (ret) {
12603 		dev_info(&pf->pdev->dev, "NVM read error, err %pe aq_err %s\n",
12604 			 ERR_PTR(ret),
12605 			 i40e_aq_str(&pf->hw, last_aq_status));
12606 		goto bw_commit_out;
12607 	}
12608 
12609 	/* Wait a bit for NVM release to complete */
12610 	msleep(50);
12611 
12612 	/* Acquire NVM for write access */
12613 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12614 	last_aq_status = pf->hw.aq.asq_last_status;
12615 	if (ret) {
12616 		dev_info(&pf->pdev->dev,
12617 			 "Cannot acquire NVM for write access, err %pe aq_err %s\n",
12618 			 ERR_PTR(ret),
12619 			 i40e_aq_str(&pf->hw, last_aq_status));
12620 		goto bw_commit_out;
12621 	}
12622 	/* Write it back out unchanged to initiate update NVM,
12623 	 * which will force a write of the shadow (alt) RAM to
12624 	 * the NVM - thus storing the bandwidth values permanently.
12625 	 */
12626 	ret = i40e_aq_update_nvm(&pf->hw,
12627 				 I40E_SR_NVM_CONTROL_WORD,
12628 				 0x10, sizeof(nvm_word),
12629 				 &nvm_word, true, 0, NULL);
12630 	/* Save off last admin queue command status before releasing
12631 	 * the NVM
12632 	 */
12633 	last_aq_status = pf->hw.aq.asq_last_status;
12634 	i40e_release_nvm(&pf->hw);
12635 	if (ret)
12636 		dev_info(&pf->pdev->dev,
12637 			 "BW settings NOT SAVED, err %pe aq_err %s\n",
12638 			 ERR_PTR(ret),
12639 			 i40e_aq_str(&pf->hw, last_aq_status));
12640 bw_commit_out:
12641 
12642 	return ret;
12643 }
12644 
12645 /**
12646  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12647  * if total port shutdown feature is enabled for this PF
12648  * @pf: board private structure
12649  **/
12650 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12651 {
12652 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12653 #define I40E_FEATURES_ENABLE_PTR		0x2A
12654 #define I40E_CURRENT_SETTING_PTR		0x2B
12655 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12656 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12657 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12658 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12659 	int read_status = I40E_SUCCESS;
12660 	u16 sr_emp_sr_settings_ptr = 0;
12661 	u16 features_enable = 0;
12662 	u16 link_behavior = 0;
12663 	bool ret = false;
12664 
12665 	read_status = i40e_read_nvm_word(&pf->hw,
12666 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12667 					 &sr_emp_sr_settings_ptr);
12668 	if (read_status)
12669 		goto err_nvm;
12670 	read_status = i40e_read_nvm_word(&pf->hw,
12671 					 sr_emp_sr_settings_ptr +
12672 					 I40E_FEATURES_ENABLE_PTR,
12673 					 &features_enable);
12674 	if (read_status)
12675 		goto err_nvm;
12676 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12677 		read_status = i40e_read_nvm_module_data(&pf->hw,
12678 							I40E_SR_EMP_SR_SETTINGS_PTR,
12679 							I40E_CURRENT_SETTING_PTR,
12680 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12681 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12682 							&link_behavior);
12683 		if (read_status)
12684 			goto err_nvm;
12685 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12686 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12687 	}
12688 	return ret;
12689 
12690 err_nvm:
12691 	dev_warn(&pf->pdev->dev,
12692 		 "total-port-shutdown feature is off due to read nvm error: %pe\n",
12693 		 ERR_PTR(read_status));
12694 	return ret;
12695 }
12696 
12697 /**
12698  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12699  * @pf: board private structure to initialize
12700  *
12701  * i40e_sw_init initializes the Adapter private data structure.
12702  * Fields are initialized based on PCI device information and
12703  * OS network device settings (MTU size).
12704  **/
12705 static int i40e_sw_init(struct i40e_pf *pf)
12706 {
12707 	int err = 0;
12708 	int size;
12709 	u16 pow;
12710 
12711 	/* Set default capability flags */
12712 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12713 		    I40E_FLAG_MSI_ENABLED     |
12714 		    I40E_FLAG_MSIX_ENABLED;
12715 
12716 	/* Set default ITR */
12717 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12718 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12719 
12720 	/* Depending on PF configurations, it is possible that the RSS
12721 	 * maximum might end up larger than the available queues
12722 	 */
12723 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12724 	pf->alloc_rss_size = 1;
12725 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12726 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12727 				 pf->hw.func_caps.num_tx_qp);
12728 
12729 	/* find the next higher power-of-2 of num cpus */
12730 	pow = roundup_pow_of_two(num_online_cpus());
12731 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12732 
12733 	if (pf->hw.func_caps.rss) {
12734 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12735 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12736 					   num_online_cpus());
12737 	}
12738 
12739 	/* MFP mode enabled */
12740 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12741 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12742 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12743 		if (i40e_get_partition_bw_setting(pf)) {
12744 			dev_warn(&pf->pdev->dev,
12745 				 "Could not get partition bw settings\n");
12746 		} else {
12747 			dev_info(&pf->pdev->dev,
12748 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12749 				 pf->min_bw, pf->max_bw);
12750 
12751 			/* nudge the Tx scheduler */
12752 			i40e_set_partition_bw_setting(pf);
12753 		}
12754 	}
12755 
12756 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12757 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12758 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12759 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12760 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12761 		    pf->hw.num_partitions > 1)
12762 			dev_info(&pf->pdev->dev,
12763 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12764 		else
12765 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12766 		pf->fdir_pf_filter_count =
12767 				 pf->hw.func_caps.fd_filters_guaranteed;
12768 		pf->hw.fdir_shared_filter_count =
12769 				 pf->hw.func_caps.fd_filters_best_effort;
12770 	}
12771 
12772 	if (pf->hw.mac.type == I40E_MAC_X722) {
12773 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12774 				    I40E_HW_128_QP_RSS_CAPABLE |
12775 				    I40E_HW_ATR_EVICT_CAPABLE |
12776 				    I40E_HW_WB_ON_ITR_CAPABLE |
12777 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12778 				    I40E_HW_NO_PCI_LINK_CHECK |
12779 				    I40E_HW_USE_SET_LLDP_MIB |
12780 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12781 				    I40E_HW_PTP_L4_CAPABLE |
12782 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12783 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12784 
12785 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12786 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12787 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12788 			dev_warn(&pf->pdev->dev,
12789 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12790 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12791 		}
12792 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12793 		   ((pf->hw.aq.api_maj_ver == 1) &&
12794 		    (pf->hw.aq.api_min_ver > 4))) {
12795 		/* Supported in FW API version higher than 1.4 */
12796 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12797 	}
12798 
12799 	/* Enable HW ATR eviction if possible */
12800 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12801 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12802 
12803 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12804 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12805 	    (pf->hw.aq.fw_maj_ver < 4))) {
12806 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12807 		/* No DCB support  for FW < v4.33 */
12808 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12809 	}
12810 
12811 	/* Disable FW LLDP if FW < v4.3 */
12812 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12813 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12814 	    (pf->hw.aq.fw_maj_ver < 4)))
12815 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12816 
12817 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12818 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12819 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12820 	    (pf->hw.aq.fw_maj_ver >= 5)))
12821 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12822 
12823 	/* Enable PTP L4 if FW > v6.0 */
12824 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12825 	    pf->hw.aq.fw_maj_ver >= 6)
12826 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12827 
12828 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12829 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12830 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12831 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12832 	}
12833 
12834 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12835 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12836 		/* IWARP needs one extra vector for CQP just like MISC.*/
12837 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12838 	}
12839 	/* Stopping FW LLDP engine is supported on XL710 and X722
12840 	 * starting from FW versions determined in i40e_init_adminq.
12841 	 * Stopping the FW LLDP engine is not supported on XL710
12842 	 * if NPAR is functioning so unset this hw flag in this case.
12843 	 */
12844 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12845 	    pf->hw.func_caps.npar_enable &&
12846 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12847 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12848 
12849 #ifdef CONFIG_PCI_IOV
12850 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12851 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12852 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12853 		pf->num_req_vfs = min_t(int,
12854 					pf->hw.func_caps.num_vfs,
12855 					I40E_MAX_VF_COUNT);
12856 	}
12857 #endif /* CONFIG_PCI_IOV */
12858 	pf->eeprom_version = 0xDEAD;
12859 	pf->lan_veb = I40E_NO_VEB;
12860 	pf->lan_vsi = I40E_NO_VSI;
12861 
12862 	/* By default FW has this off for performance reasons */
12863 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12864 
12865 	/* set up queue assignment tracking */
12866 	size = sizeof(struct i40e_lump_tracking)
12867 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12868 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12869 	if (!pf->qp_pile) {
12870 		err = -ENOMEM;
12871 		goto sw_init_done;
12872 	}
12873 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12874 
12875 	pf->tx_timeout_recovery_level = 1;
12876 
12877 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12878 	    i40e_is_total_port_shutdown_enabled(pf)) {
12879 		/* Link down on close must be on when total port shutdown
12880 		 * is enabled for a given port
12881 		 */
12882 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12883 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12884 		dev_info(&pf->pdev->dev,
12885 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12886 	}
12887 	mutex_init(&pf->switch_mutex);
12888 
12889 sw_init_done:
12890 	return err;
12891 }
12892 
12893 /**
12894  * i40e_set_ntuple - set the ntuple feature flag and take action
12895  * @pf: board private structure to initialize
12896  * @features: the feature set that the stack is suggesting
12897  *
12898  * returns a bool to indicate if reset needs to happen
12899  **/
12900 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12901 {
12902 	bool need_reset = false;
12903 
12904 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12905 	 * the state changed, we need to reset.
12906 	 */
12907 	if (features & NETIF_F_NTUPLE) {
12908 		/* Enable filters and mark for reset */
12909 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12910 			need_reset = true;
12911 		/* enable FD_SB only if there is MSI-X vector and no cloud
12912 		 * filters exist
12913 		 */
12914 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12915 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12916 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12917 		}
12918 	} else {
12919 		/* turn off filters, mark for reset and clear SW filter list */
12920 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12921 			need_reset = true;
12922 			i40e_fdir_filter_exit(pf);
12923 		}
12924 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12925 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12926 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12927 
12928 		/* reset fd counters */
12929 		pf->fd_add_err = 0;
12930 		pf->fd_atr_cnt = 0;
12931 		/* if ATR was auto disabled it can be re-enabled. */
12932 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12933 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12934 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12935 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12936 	}
12937 	return need_reset;
12938 }
12939 
12940 /**
12941  * i40e_clear_rss_lut - clear the rx hash lookup table
12942  * @vsi: the VSI being configured
12943  **/
12944 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12945 {
12946 	struct i40e_pf *pf = vsi->back;
12947 	struct i40e_hw *hw = &pf->hw;
12948 	u16 vf_id = vsi->vf_id;
12949 	u8 i;
12950 
12951 	if (vsi->type == I40E_VSI_MAIN) {
12952 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12953 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12954 	} else if (vsi->type == I40E_VSI_SRIOV) {
12955 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12956 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12957 	} else {
12958 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12959 	}
12960 }
12961 
12962 /**
12963  * i40e_set_loopback - turn on/off loopback mode on underlying PF
12964  * @vsi: ptr to VSI
12965  * @ena: flag to indicate the on/off setting
12966  */
12967 static int i40e_set_loopback(struct i40e_vsi *vsi, bool ena)
12968 {
12969 	bool if_running = netif_running(vsi->netdev) &&
12970 			  !test_and_set_bit(__I40E_VSI_DOWN, vsi->state);
12971 	int ret;
12972 
12973 	if (if_running)
12974 		i40e_down(vsi);
12975 
12976 	ret = i40e_aq_set_mac_loopback(&vsi->back->hw, ena, NULL);
12977 	if (ret)
12978 		netdev_err(vsi->netdev, "Failed to toggle loopback state\n");
12979 	if (if_running)
12980 		i40e_up(vsi);
12981 
12982 	return ret;
12983 }
12984 
12985 /**
12986  * i40e_set_features - set the netdev feature flags
12987  * @netdev: ptr to the netdev being adjusted
12988  * @features: the feature set that the stack is suggesting
12989  * Note: expects to be called while under rtnl_lock()
12990  **/
12991 static int i40e_set_features(struct net_device *netdev,
12992 			     netdev_features_t features)
12993 {
12994 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12995 	struct i40e_vsi *vsi = np->vsi;
12996 	struct i40e_pf *pf = vsi->back;
12997 	bool need_reset;
12998 
12999 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
13000 		i40e_pf_config_rss(pf);
13001 	else if (!(features & NETIF_F_RXHASH) &&
13002 		 netdev->features & NETIF_F_RXHASH)
13003 		i40e_clear_rss_lut(vsi);
13004 
13005 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
13006 		i40e_vlan_stripping_enable(vsi);
13007 	else
13008 		i40e_vlan_stripping_disable(vsi);
13009 
13010 	if (!(features & NETIF_F_HW_TC) &&
13011 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
13012 		dev_err(&pf->pdev->dev,
13013 			"Offloaded tc filters active, can't turn hw_tc_offload off");
13014 		return -EINVAL;
13015 	}
13016 
13017 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
13018 		i40e_del_all_macvlans(vsi);
13019 
13020 	need_reset = i40e_set_ntuple(pf, features);
13021 
13022 	if (need_reset)
13023 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13024 
13025 	if ((features ^ netdev->features) & NETIF_F_LOOPBACK)
13026 		return i40e_set_loopback(vsi, !!(features & NETIF_F_LOOPBACK));
13027 
13028 	return 0;
13029 }
13030 
13031 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
13032 				    unsigned int table, unsigned int idx,
13033 				    struct udp_tunnel_info *ti)
13034 {
13035 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13036 	struct i40e_hw *hw = &np->vsi->back->hw;
13037 	u8 type, filter_index;
13038 	int ret;
13039 
13040 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
13041 						   I40E_AQC_TUNNEL_TYPE_NGE;
13042 
13043 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
13044 				     NULL);
13045 	if (ret) {
13046 		netdev_info(netdev, "add UDP port failed, err %pe aq_err %s\n",
13047 			    ERR_PTR(ret),
13048 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13049 		return -EIO;
13050 	}
13051 
13052 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
13053 	return 0;
13054 }
13055 
13056 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
13057 				      unsigned int table, unsigned int idx,
13058 				      struct udp_tunnel_info *ti)
13059 {
13060 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13061 	struct i40e_hw *hw = &np->vsi->back->hw;
13062 	int ret;
13063 
13064 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13065 	if (ret) {
13066 		netdev_info(netdev, "delete UDP port failed, err %pe aq_err %s\n",
13067 			    ERR_PTR(ret),
13068 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13069 		return -EIO;
13070 	}
13071 
13072 	return 0;
13073 }
13074 
13075 static int i40e_get_phys_port_id(struct net_device *netdev,
13076 				 struct netdev_phys_item_id *ppid)
13077 {
13078 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13079 	struct i40e_pf *pf = np->vsi->back;
13080 	struct i40e_hw *hw = &pf->hw;
13081 
13082 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13083 		return -EOPNOTSUPP;
13084 
13085 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13086 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13087 
13088 	return 0;
13089 }
13090 
13091 /**
13092  * i40e_ndo_fdb_add - add an entry to the hardware database
13093  * @ndm: the input from the stack
13094  * @tb: pointer to array of nladdr (unused)
13095  * @dev: the net device pointer
13096  * @addr: the MAC address entry being added
13097  * @vid: VLAN ID
13098  * @flags: instructions from stack about fdb operation
13099  * @extack: netlink extended ack, unused currently
13100  */
13101 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13102 			    struct net_device *dev,
13103 			    const unsigned char *addr, u16 vid,
13104 			    u16 flags,
13105 			    struct netlink_ext_ack *extack)
13106 {
13107 	struct i40e_netdev_priv *np = netdev_priv(dev);
13108 	struct i40e_pf *pf = np->vsi->back;
13109 	int err = 0;
13110 
13111 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13112 		return -EOPNOTSUPP;
13113 
13114 	if (vid) {
13115 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13116 		return -EINVAL;
13117 	}
13118 
13119 	/* Hardware does not support aging addresses so if a
13120 	 * ndm_state is given only allow permanent addresses
13121 	 */
13122 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13123 		netdev_info(dev, "FDB only supports static addresses\n");
13124 		return -EINVAL;
13125 	}
13126 
13127 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13128 		err = dev_uc_add_excl(dev, addr);
13129 	else if (is_multicast_ether_addr(addr))
13130 		err = dev_mc_add_excl(dev, addr);
13131 	else
13132 		err = -EINVAL;
13133 
13134 	/* Only return duplicate errors if NLM_F_EXCL is set */
13135 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13136 		err = 0;
13137 
13138 	return err;
13139 }
13140 
13141 /**
13142  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13143  * @dev: the netdev being configured
13144  * @nlh: RTNL message
13145  * @flags: bridge flags
13146  * @extack: netlink extended ack
13147  *
13148  * Inserts a new hardware bridge if not already created and
13149  * enables the bridging mode requested (VEB or VEPA). If the
13150  * hardware bridge has already been inserted and the request
13151  * is to change the mode then that requires a PF reset to
13152  * allow rebuild of the components with required hardware
13153  * bridge mode enabled.
13154  *
13155  * Note: expects to be called while under rtnl_lock()
13156  **/
13157 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13158 				   struct nlmsghdr *nlh,
13159 				   u16 flags,
13160 				   struct netlink_ext_ack *extack)
13161 {
13162 	struct i40e_netdev_priv *np = netdev_priv(dev);
13163 	struct i40e_vsi *vsi = np->vsi;
13164 	struct i40e_pf *pf = vsi->back;
13165 	struct i40e_veb *veb = NULL;
13166 	struct nlattr *attr, *br_spec;
13167 	int i, rem;
13168 
13169 	/* Only for PF VSI for now */
13170 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13171 		return -EOPNOTSUPP;
13172 
13173 	/* Find the HW bridge for PF VSI */
13174 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13175 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13176 			veb = pf->veb[i];
13177 	}
13178 
13179 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13180 	if (!br_spec)
13181 		return -EINVAL;
13182 
13183 	nla_for_each_nested(attr, br_spec, rem) {
13184 		__u16 mode;
13185 
13186 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13187 			continue;
13188 
13189 		mode = nla_get_u16(attr);
13190 		if ((mode != BRIDGE_MODE_VEPA) &&
13191 		    (mode != BRIDGE_MODE_VEB))
13192 			return -EINVAL;
13193 
13194 		/* Insert a new HW bridge */
13195 		if (!veb) {
13196 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13197 					     vsi->tc_config.enabled_tc);
13198 			if (veb) {
13199 				veb->bridge_mode = mode;
13200 				i40e_config_bridge_mode(veb);
13201 			} else {
13202 				/* No Bridge HW offload available */
13203 				return -ENOENT;
13204 			}
13205 			break;
13206 		} else if (mode != veb->bridge_mode) {
13207 			/* Existing HW bridge but different mode needs reset */
13208 			veb->bridge_mode = mode;
13209 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13210 			if (mode == BRIDGE_MODE_VEB)
13211 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13212 			else
13213 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13214 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13215 			break;
13216 		}
13217 	}
13218 
13219 	return 0;
13220 }
13221 
13222 /**
13223  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13224  * @skb: skb buff
13225  * @pid: process id
13226  * @seq: RTNL message seq #
13227  * @dev: the netdev being configured
13228  * @filter_mask: unused
13229  * @nlflags: netlink flags passed in
13230  *
13231  * Return the mode in which the hardware bridge is operating in
13232  * i.e VEB or VEPA.
13233  **/
13234 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13235 				   struct net_device *dev,
13236 				   u32 __always_unused filter_mask,
13237 				   int nlflags)
13238 {
13239 	struct i40e_netdev_priv *np = netdev_priv(dev);
13240 	struct i40e_vsi *vsi = np->vsi;
13241 	struct i40e_pf *pf = vsi->back;
13242 	struct i40e_veb *veb = NULL;
13243 	int i;
13244 
13245 	/* Only for PF VSI for now */
13246 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13247 		return -EOPNOTSUPP;
13248 
13249 	/* Find the HW bridge for the PF VSI */
13250 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13251 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13252 			veb = pf->veb[i];
13253 	}
13254 
13255 	if (!veb)
13256 		return 0;
13257 
13258 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13259 				       0, 0, nlflags, filter_mask, NULL);
13260 }
13261 
13262 /**
13263  * i40e_features_check - Validate encapsulated packet conforms to limits
13264  * @skb: skb buff
13265  * @dev: This physical port's netdev
13266  * @features: Offload features that the stack believes apply
13267  **/
13268 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13269 					     struct net_device *dev,
13270 					     netdev_features_t features)
13271 {
13272 	size_t len;
13273 
13274 	/* No point in doing any of this if neither checksum nor GSO are
13275 	 * being requested for this frame.  We can rule out both by just
13276 	 * checking for CHECKSUM_PARTIAL
13277 	 */
13278 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13279 		return features;
13280 
13281 	/* We cannot support GSO if the MSS is going to be less than
13282 	 * 64 bytes.  If it is then we need to drop support for GSO.
13283 	 */
13284 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13285 		features &= ~NETIF_F_GSO_MASK;
13286 
13287 	/* MACLEN can support at most 63 words */
13288 	len = skb_network_header(skb) - skb->data;
13289 	if (len & ~(63 * 2))
13290 		goto out_err;
13291 
13292 	/* IPLEN and EIPLEN can support at most 127 dwords */
13293 	len = skb_transport_header(skb) - skb_network_header(skb);
13294 	if (len & ~(127 * 4))
13295 		goto out_err;
13296 
13297 	if (skb->encapsulation) {
13298 		/* L4TUNLEN can support 127 words */
13299 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13300 		if (len & ~(127 * 2))
13301 			goto out_err;
13302 
13303 		/* IPLEN can support at most 127 dwords */
13304 		len = skb_inner_transport_header(skb) -
13305 		      skb_inner_network_header(skb);
13306 		if (len & ~(127 * 4))
13307 			goto out_err;
13308 	}
13309 
13310 	/* No need to validate L4LEN as TCP is the only protocol with a
13311 	 * flexible value and we support all possible values supported
13312 	 * by TCP, which is at most 15 dwords
13313 	 */
13314 
13315 	return features;
13316 out_err:
13317 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13318 }
13319 
13320 /**
13321  * i40e_xdp_setup - add/remove an XDP program
13322  * @vsi: VSI to changed
13323  * @prog: XDP program
13324  * @extack: netlink extended ack
13325  **/
13326 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13327 			  struct netlink_ext_ack *extack)
13328 {
13329 	int frame_size = i40e_max_vsi_frame_size(vsi, prog);
13330 	struct i40e_pf *pf = vsi->back;
13331 	struct bpf_prog *old_prog;
13332 	bool need_reset;
13333 	int i;
13334 
13335 	/* Don't allow frames that span over multiple buffers */
13336 	if (vsi->netdev->mtu > frame_size - I40E_PACKET_HDR_PAD) {
13337 		NL_SET_ERR_MSG_MOD(extack, "MTU too large for linear frames and XDP prog does not support frags");
13338 		return -EINVAL;
13339 	}
13340 
13341 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13342 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13343 
13344 	if (need_reset)
13345 		i40e_prep_for_reset(pf);
13346 
13347 	/* VSI shall be deleted in a moment, just return EINVAL */
13348 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13349 		return -EINVAL;
13350 
13351 	old_prog = xchg(&vsi->xdp_prog, prog);
13352 
13353 	if (need_reset) {
13354 		if (!prog) {
13355 			xdp_features_clear_redirect_target(vsi->netdev);
13356 			/* Wait until ndo_xsk_wakeup completes. */
13357 			synchronize_rcu();
13358 		}
13359 		i40e_reset_and_rebuild(pf, true, true);
13360 	}
13361 
13362 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13363 		if (i40e_realloc_rx_bi_zc(vsi, true))
13364 			return -ENOMEM;
13365 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13366 		if (i40e_realloc_rx_bi_zc(vsi, false))
13367 			return -ENOMEM;
13368 	}
13369 
13370 	for (i = 0; i < vsi->num_queue_pairs; i++)
13371 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13372 
13373 	if (old_prog)
13374 		bpf_prog_put(old_prog);
13375 
13376 	/* Kick start the NAPI context if there is an AF_XDP socket open
13377 	 * on that queue id. This so that receiving will start.
13378 	 */
13379 	if (need_reset && prog) {
13380 		for (i = 0; i < vsi->num_queue_pairs; i++)
13381 			if (vsi->xdp_rings[i]->xsk_pool)
13382 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13383 						      XDP_WAKEUP_RX);
13384 		xdp_features_set_redirect_target(vsi->netdev, true);
13385 	}
13386 
13387 	return 0;
13388 }
13389 
13390 /**
13391  * i40e_enter_busy_conf - Enters busy config state
13392  * @vsi: vsi
13393  *
13394  * Returns 0 on success, <0 for failure.
13395  **/
13396 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13397 {
13398 	struct i40e_pf *pf = vsi->back;
13399 	int timeout = 50;
13400 
13401 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13402 		timeout--;
13403 		if (!timeout)
13404 			return -EBUSY;
13405 		usleep_range(1000, 2000);
13406 	}
13407 
13408 	return 0;
13409 }
13410 
13411 /**
13412  * i40e_exit_busy_conf - Exits busy config state
13413  * @vsi: vsi
13414  **/
13415 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13416 {
13417 	struct i40e_pf *pf = vsi->back;
13418 
13419 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13420 }
13421 
13422 /**
13423  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13424  * @vsi: vsi
13425  * @queue_pair: queue pair
13426  **/
13427 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13428 {
13429 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13430 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13431 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13432 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13433 	if (i40e_enabled_xdp_vsi(vsi)) {
13434 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13435 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13436 	}
13437 }
13438 
13439 /**
13440  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13441  * @vsi: vsi
13442  * @queue_pair: queue pair
13443  **/
13444 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13445 {
13446 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13447 	if (i40e_enabled_xdp_vsi(vsi)) {
13448 		/* Make sure that in-progress ndo_xdp_xmit calls are
13449 		 * completed.
13450 		 */
13451 		synchronize_rcu();
13452 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13453 	}
13454 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13455 }
13456 
13457 /**
13458  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13459  * @vsi: vsi
13460  * @queue_pair: queue pair
13461  * @enable: true for enable, false for disable
13462  **/
13463 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13464 					bool enable)
13465 {
13466 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13467 	struct i40e_q_vector *q_vector = rxr->q_vector;
13468 
13469 	if (!vsi->netdev)
13470 		return;
13471 
13472 	/* All rings in a qp belong to the same qvector. */
13473 	if (q_vector->rx.ring || q_vector->tx.ring) {
13474 		if (enable)
13475 			napi_enable(&q_vector->napi);
13476 		else
13477 			napi_disable(&q_vector->napi);
13478 	}
13479 }
13480 
13481 /**
13482  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13483  * @vsi: vsi
13484  * @queue_pair: queue pair
13485  * @enable: true for enable, false for disable
13486  *
13487  * Returns 0 on success, <0 on failure.
13488  **/
13489 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13490 					bool enable)
13491 {
13492 	struct i40e_pf *pf = vsi->back;
13493 	int pf_q, ret = 0;
13494 
13495 	pf_q = vsi->base_queue + queue_pair;
13496 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13497 				     false /*is xdp*/, enable);
13498 	if (ret) {
13499 		dev_info(&pf->pdev->dev,
13500 			 "VSI seid %d Tx ring %d %sable timeout\n",
13501 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13502 		return ret;
13503 	}
13504 
13505 	i40e_control_rx_q(pf, pf_q, enable);
13506 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13507 	if (ret) {
13508 		dev_info(&pf->pdev->dev,
13509 			 "VSI seid %d Rx ring %d %sable timeout\n",
13510 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13511 		return ret;
13512 	}
13513 
13514 	/* Due to HW errata, on Rx disable only, the register can
13515 	 * indicate done before it really is. Needs 50ms to be sure
13516 	 */
13517 	if (!enable)
13518 		mdelay(50);
13519 
13520 	if (!i40e_enabled_xdp_vsi(vsi))
13521 		return ret;
13522 
13523 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13524 				     pf_q + vsi->alloc_queue_pairs,
13525 				     true /*is xdp*/, enable);
13526 	if (ret) {
13527 		dev_info(&pf->pdev->dev,
13528 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13529 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13530 	}
13531 
13532 	return ret;
13533 }
13534 
13535 /**
13536  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13537  * @vsi: vsi
13538  * @queue_pair: queue_pair
13539  **/
13540 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13541 {
13542 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13543 	struct i40e_pf *pf = vsi->back;
13544 	struct i40e_hw *hw = &pf->hw;
13545 
13546 	/* All rings in a qp belong to the same qvector. */
13547 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13548 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13549 	else
13550 		i40e_irq_dynamic_enable_icr0(pf);
13551 
13552 	i40e_flush(hw);
13553 }
13554 
13555 /**
13556  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13557  * @vsi: vsi
13558  * @queue_pair: queue_pair
13559  **/
13560 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13561 {
13562 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13563 	struct i40e_pf *pf = vsi->back;
13564 	struct i40e_hw *hw = &pf->hw;
13565 
13566 	/* For simplicity, instead of removing the qp interrupt causes
13567 	 * from the interrupt linked list, we simply disable the interrupt, and
13568 	 * leave the list intact.
13569 	 *
13570 	 * All rings in a qp belong to the same qvector.
13571 	 */
13572 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13573 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13574 
13575 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13576 		i40e_flush(hw);
13577 		synchronize_irq(pf->msix_entries[intpf].vector);
13578 	} else {
13579 		/* Legacy and MSI mode - this stops all interrupt handling */
13580 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13581 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13582 		i40e_flush(hw);
13583 		synchronize_irq(pf->pdev->irq);
13584 	}
13585 }
13586 
13587 /**
13588  * i40e_queue_pair_disable - Disables a queue pair
13589  * @vsi: vsi
13590  * @queue_pair: queue pair
13591  *
13592  * Returns 0 on success, <0 on failure.
13593  **/
13594 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13595 {
13596 	int err;
13597 
13598 	err = i40e_enter_busy_conf(vsi);
13599 	if (err)
13600 		return err;
13601 
13602 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13603 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13604 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13605 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13606 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13607 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13608 
13609 	return err;
13610 }
13611 
13612 /**
13613  * i40e_queue_pair_enable - Enables a queue pair
13614  * @vsi: vsi
13615  * @queue_pair: queue pair
13616  *
13617  * Returns 0 on success, <0 on failure.
13618  **/
13619 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13620 {
13621 	int err;
13622 
13623 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13624 	if (err)
13625 		return err;
13626 
13627 	if (i40e_enabled_xdp_vsi(vsi)) {
13628 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13629 		if (err)
13630 			return err;
13631 	}
13632 
13633 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13634 	if (err)
13635 		return err;
13636 
13637 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13638 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13639 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13640 
13641 	i40e_exit_busy_conf(vsi);
13642 
13643 	return err;
13644 }
13645 
13646 /**
13647  * i40e_xdp - implements ndo_bpf for i40e
13648  * @dev: netdevice
13649  * @xdp: XDP command
13650  **/
13651 static int i40e_xdp(struct net_device *dev,
13652 		    struct netdev_bpf *xdp)
13653 {
13654 	struct i40e_netdev_priv *np = netdev_priv(dev);
13655 	struct i40e_vsi *vsi = np->vsi;
13656 
13657 	if (vsi->type != I40E_VSI_MAIN)
13658 		return -EINVAL;
13659 
13660 	switch (xdp->command) {
13661 	case XDP_SETUP_PROG:
13662 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13663 	case XDP_SETUP_XSK_POOL:
13664 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13665 					   xdp->xsk.queue_id);
13666 	default:
13667 		return -EINVAL;
13668 	}
13669 }
13670 
13671 static const struct net_device_ops i40e_netdev_ops = {
13672 	.ndo_open		= i40e_open,
13673 	.ndo_stop		= i40e_close,
13674 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13675 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13676 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13677 	.ndo_validate_addr	= eth_validate_addr,
13678 	.ndo_set_mac_address	= i40e_set_mac,
13679 	.ndo_change_mtu		= i40e_change_mtu,
13680 	.ndo_eth_ioctl		= i40e_ioctl,
13681 	.ndo_tx_timeout		= i40e_tx_timeout,
13682 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13683 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13684 #ifdef CONFIG_NET_POLL_CONTROLLER
13685 	.ndo_poll_controller	= i40e_netpoll,
13686 #endif
13687 	.ndo_setup_tc		= __i40e_setup_tc,
13688 	.ndo_select_queue	= i40e_lan_select_queue,
13689 	.ndo_set_features	= i40e_set_features,
13690 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13691 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13692 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13693 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13694 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13695 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13696 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13697 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13698 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13699 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13700 	.ndo_features_check	= i40e_features_check,
13701 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13702 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13703 	.ndo_bpf		= i40e_xdp,
13704 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13705 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13706 	.ndo_dfwd_add_station	= i40e_fwd_add,
13707 	.ndo_dfwd_del_station	= i40e_fwd_del,
13708 };
13709 
13710 /**
13711  * i40e_config_netdev - Setup the netdev flags
13712  * @vsi: the VSI being configured
13713  *
13714  * Returns 0 on success, negative value on failure
13715  **/
13716 static int i40e_config_netdev(struct i40e_vsi *vsi)
13717 {
13718 	struct i40e_pf *pf = vsi->back;
13719 	struct i40e_hw *hw = &pf->hw;
13720 	struct i40e_netdev_priv *np;
13721 	struct net_device *netdev;
13722 	u8 broadcast[ETH_ALEN];
13723 	u8 mac_addr[ETH_ALEN];
13724 	int etherdev_size;
13725 	netdev_features_t hw_enc_features;
13726 	netdev_features_t hw_features;
13727 
13728 	etherdev_size = sizeof(struct i40e_netdev_priv);
13729 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13730 	if (!netdev)
13731 		return -ENOMEM;
13732 
13733 	vsi->netdev = netdev;
13734 	np = netdev_priv(netdev);
13735 	np->vsi = vsi;
13736 
13737 	hw_enc_features = NETIF_F_SG			|
13738 			  NETIF_F_HW_CSUM		|
13739 			  NETIF_F_HIGHDMA		|
13740 			  NETIF_F_SOFT_FEATURES		|
13741 			  NETIF_F_TSO			|
13742 			  NETIF_F_TSO_ECN		|
13743 			  NETIF_F_TSO6			|
13744 			  NETIF_F_GSO_GRE		|
13745 			  NETIF_F_GSO_GRE_CSUM		|
13746 			  NETIF_F_GSO_PARTIAL		|
13747 			  NETIF_F_GSO_IPXIP4		|
13748 			  NETIF_F_GSO_IPXIP6		|
13749 			  NETIF_F_GSO_UDP_TUNNEL	|
13750 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13751 			  NETIF_F_GSO_UDP_L4		|
13752 			  NETIF_F_SCTP_CRC		|
13753 			  NETIF_F_RXHASH		|
13754 			  NETIF_F_RXCSUM		|
13755 			  0;
13756 
13757 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13758 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13759 
13760 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13761 
13762 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13763 
13764 	netdev->hw_enc_features |= hw_enc_features;
13765 
13766 	/* record features VLANs can make use of */
13767 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13768 
13769 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13770 				   NETIF_F_GSO_GRE_CSUM |	\
13771 				   NETIF_F_GSO_IPXIP4 |		\
13772 				   NETIF_F_GSO_IPXIP6 |		\
13773 				   NETIF_F_GSO_UDP_TUNNEL |	\
13774 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13775 
13776 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13777 	netdev->features |= NETIF_F_GSO_PARTIAL |
13778 			    I40E_GSO_PARTIAL_FEATURES;
13779 
13780 	netdev->mpls_features |= NETIF_F_SG;
13781 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13782 	netdev->mpls_features |= NETIF_F_TSO;
13783 	netdev->mpls_features |= NETIF_F_TSO6;
13784 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13785 
13786 	/* enable macvlan offloads */
13787 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13788 
13789 	hw_features = hw_enc_features		|
13790 		      NETIF_F_HW_VLAN_CTAG_TX	|
13791 		      NETIF_F_HW_VLAN_CTAG_RX;
13792 
13793 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13794 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13795 
13796 	netdev->hw_features |= hw_features | NETIF_F_LOOPBACK;
13797 
13798 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13799 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13800 
13801 	netdev->features &= ~NETIF_F_HW_TC;
13802 
13803 	if (vsi->type == I40E_VSI_MAIN) {
13804 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13805 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13806 		/* The following steps are necessary for two reasons. First,
13807 		 * some older NVM configurations load a default MAC-VLAN
13808 		 * filter that will accept any tagged packet, and we want to
13809 		 * replace this with a normal filter. Additionally, it is
13810 		 * possible our MAC address was provided by the platform using
13811 		 * Open Firmware or similar.
13812 		 *
13813 		 * Thus, we need to remove the default filter and install one
13814 		 * specific to the MAC address.
13815 		 */
13816 		i40e_rm_default_mac_filter(vsi, mac_addr);
13817 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13818 		i40e_add_mac_filter(vsi, mac_addr);
13819 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13820 
13821 		netdev->xdp_features = NETDEV_XDP_ACT_BASIC |
13822 				       NETDEV_XDP_ACT_REDIRECT |
13823 				       NETDEV_XDP_ACT_XSK_ZEROCOPY |
13824 				       NETDEV_XDP_ACT_RX_SG;
13825 	} else {
13826 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13827 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13828 		 * the end, which is 4 bytes long, so force truncation of the
13829 		 * original name by IFNAMSIZ - 4
13830 		 */
13831 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13832 			 IFNAMSIZ - 4,
13833 			 pf->vsi[pf->lan_vsi]->netdev->name);
13834 		eth_random_addr(mac_addr);
13835 
13836 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13837 		i40e_add_mac_filter(vsi, mac_addr);
13838 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13839 	}
13840 
13841 	/* Add the broadcast filter so that we initially will receive
13842 	 * broadcast packets. Note that when a new VLAN is first added the
13843 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13844 	 * specific filters as part of transitioning into "vlan" operation.
13845 	 * When more VLANs are added, the driver will copy each existing MAC
13846 	 * filter and add it for the new VLAN.
13847 	 *
13848 	 * Broadcast filters are handled specially by
13849 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13850 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13851 	 * filter. The subtask will update the correct broadcast promiscuous
13852 	 * bits as VLANs become active or inactive.
13853 	 */
13854 	eth_broadcast_addr(broadcast);
13855 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13856 	i40e_add_mac_filter(vsi, broadcast);
13857 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13858 
13859 	eth_hw_addr_set(netdev, mac_addr);
13860 	ether_addr_copy(netdev->perm_addr, mac_addr);
13861 
13862 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13863 	netdev->neigh_priv_len = sizeof(u32) * 4;
13864 
13865 	netdev->priv_flags |= IFF_UNICAST_FLT;
13866 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13867 	/* Setup netdev TC information */
13868 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13869 
13870 	netdev->netdev_ops = &i40e_netdev_ops;
13871 	netdev->watchdog_timeo = 5 * HZ;
13872 	i40e_set_ethtool_ops(netdev);
13873 
13874 	/* MTU range: 68 - 9706 */
13875 	netdev->min_mtu = ETH_MIN_MTU;
13876 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13877 
13878 	return 0;
13879 }
13880 
13881 /**
13882  * i40e_vsi_delete - Delete a VSI from the switch
13883  * @vsi: the VSI being removed
13884  *
13885  * Returns 0 on success, negative value on failure
13886  **/
13887 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13888 {
13889 	/* remove default VSI is not allowed */
13890 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13891 		return;
13892 
13893 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13894 }
13895 
13896 /**
13897  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13898  * @vsi: the VSI being queried
13899  *
13900  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13901  **/
13902 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13903 {
13904 	struct i40e_veb *veb;
13905 	struct i40e_pf *pf = vsi->back;
13906 
13907 	/* Uplink is not a bridge so default to VEB */
13908 	if (vsi->veb_idx >= I40E_MAX_VEB)
13909 		return 1;
13910 
13911 	veb = pf->veb[vsi->veb_idx];
13912 	if (!veb) {
13913 		dev_info(&pf->pdev->dev,
13914 			 "There is no veb associated with the bridge\n");
13915 		return -ENOENT;
13916 	}
13917 
13918 	/* Uplink is a bridge in VEPA mode */
13919 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13920 		return 0;
13921 	} else {
13922 		/* Uplink is a bridge in VEB mode */
13923 		return 1;
13924 	}
13925 
13926 	/* VEPA is now default bridge, so return 0 */
13927 	return 0;
13928 }
13929 
13930 /**
13931  * i40e_add_vsi - Add a VSI to the switch
13932  * @vsi: the VSI being configured
13933  *
13934  * This initializes a VSI context depending on the VSI type to be added and
13935  * passes it down to the add_vsi aq command.
13936  **/
13937 static int i40e_add_vsi(struct i40e_vsi *vsi)
13938 {
13939 	int ret = -ENODEV;
13940 	struct i40e_pf *pf = vsi->back;
13941 	struct i40e_hw *hw = &pf->hw;
13942 	struct i40e_vsi_context ctxt;
13943 	struct i40e_mac_filter *f;
13944 	struct hlist_node *h;
13945 	int bkt;
13946 
13947 	u8 enabled_tc = 0x1; /* TC0 enabled */
13948 	int f_count = 0;
13949 
13950 	memset(&ctxt, 0, sizeof(ctxt));
13951 	switch (vsi->type) {
13952 	case I40E_VSI_MAIN:
13953 		/* The PF's main VSI is already setup as part of the
13954 		 * device initialization, so we'll not bother with
13955 		 * the add_vsi call, but we will retrieve the current
13956 		 * VSI context.
13957 		 */
13958 		ctxt.seid = pf->main_vsi_seid;
13959 		ctxt.pf_num = pf->hw.pf_id;
13960 		ctxt.vf_num = 0;
13961 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13962 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13963 		if (ret) {
13964 			dev_info(&pf->pdev->dev,
13965 				 "couldn't get PF vsi config, err %pe aq_err %s\n",
13966 				 ERR_PTR(ret),
13967 				 i40e_aq_str(&pf->hw,
13968 					     pf->hw.aq.asq_last_status));
13969 			return -ENOENT;
13970 		}
13971 		vsi->info = ctxt.info;
13972 		vsi->info.valid_sections = 0;
13973 
13974 		vsi->seid = ctxt.seid;
13975 		vsi->id = ctxt.vsi_number;
13976 
13977 		enabled_tc = i40e_pf_get_tc_map(pf);
13978 
13979 		/* Source pruning is enabled by default, so the flag is
13980 		 * negative logic - if it's set, we need to fiddle with
13981 		 * the VSI to disable source pruning.
13982 		 */
13983 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13984 			memset(&ctxt, 0, sizeof(ctxt));
13985 			ctxt.seid = pf->main_vsi_seid;
13986 			ctxt.pf_num = pf->hw.pf_id;
13987 			ctxt.vf_num = 0;
13988 			ctxt.info.valid_sections |=
13989 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13990 			ctxt.info.switch_id =
13991 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13992 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13993 			if (ret) {
13994 				dev_info(&pf->pdev->dev,
13995 					 "update vsi failed, err %d aq_err %s\n",
13996 					 ret,
13997 					 i40e_aq_str(&pf->hw,
13998 						     pf->hw.aq.asq_last_status));
13999 				ret = -ENOENT;
14000 				goto err;
14001 			}
14002 		}
14003 
14004 		/* MFP mode setup queue map and update VSI */
14005 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
14006 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
14007 			memset(&ctxt, 0, sizeof(ctxt));
14008 			ctxt.seid = pf->main_vsi_seid;
14009 			ctxt.pf_num = pf->hw.pf_id;
14010 			ctxt.vf_num = 0;
14011 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
14012 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
14013 			if (ret) {
14014 				dev_info(&pf->pdev->dev,
14015 					 "update vsi failed, err %pe aq_err %s\n",
14016 					 ERR_PTR(ret),
14017 					 i40e_aq_str(&pf->hw,
14018 						    pf->hw.aq.asq_last_status));
14019 				ret = -ENOENT;
14020 				goto err;
14021 			}
14022 			/* update the local VSI info queue map */
14023 			i40e_vsi_update_queue_map(vsi, &ctxt);
14024 			vsi->info.valid_sections = 0;
14025 		} else {
14026 			/* Default/Main VSI is only enabled for TC0
14027 			 * reconfigure it to enable all TCs that are
14028 			 * available on the port in SFP mode.
14029 			 * For MFP case the iSCSI PF would use this
14030 			 * flow to enable LAN+iSCSI TC.
14031 			 */
14032 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
14033 			if (ret) {
14034 				/* Single TC condition is not fatal,
14035 				 * message and continue
14036 				 */
14037 				dev_info(&pf->pdev->dev,
14038 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %pe aq_err %s\n",
14039 					 enabled_tc,
14040 					 ERR_PTR(ret),
14041 					 i40e_aq_str(&pf->hw,
14042 						    pf->hw.aq.asq_last_status));
14043 			}
14044 		}
14045 		break;
14046 
14047 	case I40E_VSI_FDIR:
14048 		ctxt.pf_num = hw->pf_id;
14049 		ctxt.vf_num = 0;
14050 		ctxt.uplink_seid = vsi->uplink_seid;
14051 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14052 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
14053 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
14054 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
14055 			ctxt.info.valid_sections |=
14056 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14057 			ctxt.info.switch_id =
14058 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14059 		}
14060 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14061 		break;
14062 
14063 	case I40E_VSI_VMDQ2:
14064 		ctxt.pf_num = hw->pf_id;
14065 		ctxt.vf_num = 0;
14066 		ctxt.uplink_seid = vsi->uplink_seid;
14067 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14068 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
14069 
14070 		/* This VSI is connected to VEB so the switch_id
14071 		 * should be set to zero by default.
14072 		 */
14073 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14074 			ctxt.info.valid_sections |=
14075 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14076 			ctxt.info.switch_id =
14077 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14078 		}
14079 
14080 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14081 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14082 		break;
14083 
14084 	case I40E_VSI_SRIOV:
14085 		ctxt.pf_num = hw->pf_id;
14086 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14087 		ctxt.uplink_seid = vsi->uplink_seid;
14088 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14089 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14090 
14091 		/* This VSI is connected to VEB so the switch_id
14092 		 * should be set to zero by default.
14093 		 */
14094 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14095 			ctxt.info.valid_sections |=
14096 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14097 			ctxt.info.switch_id =
14098 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14099 		}
14100 
14101 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14102 			ctxt.info.valid_sections |=
14103 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14104 			ctxt.info.queueing_opt_flags |=
14105 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14106 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14107 		}
14108 
14109 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14110 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14111 		if (pf->vf[vsi->vf_id].spoofchk) {
14112 			ctxt.info.valid_sections |=
14113 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14114 			ctxt.info.sec_flags |=
14115 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14116 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14117 		}
14118 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14119 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14120 		break;
14121 
14122 	case I40E_VSI_IWARP:
14123 		/* send down message to iWARP */
14124 		break;
14125 
14126 	default:
14127 		return -ENODEV;
14128 	}
14129 
14130 	if (vsi->type != I40E_VSI_MAIN) {
14131 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14132 		if (ret) {
14133 			dev_info(&vsi->back->pdev->dev,
14134 				 "add vsi failed, err %pe aq_err %s\n",
14135 				 ERR_PTR(ret),
14136 				 i40e_aq_str(&pf->hw,
14137 					     pf->hw.aq.asq_last_status));
14138 			ret = -ENOENT;
14139 			goto err;
14140 		}
14141 		vsi->info = ctxt.info;
14142 		vsi->info.valid_sections = 0;
14143 		vsi->seid = ctxt.seid;
14144 		vsi->id = ctxt.vsi_number;
14145 	}
14146 
14147 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14148 	vsi->active_filters = 0;
14149 	/* If macvlan filters already exist, force them to get loaded */
14150 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14151 		f->state = I40E_FILTER_NEW;
14152 		f_count++;
14153 	}
14154 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14155 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14156 
14157 	if (f_count) {
14158 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14159 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14160 	}
14161 
14162 	/* Update VSI BW information */
14163 	ret = i40e_vsi_get_bw_info(vsi);
14164 	if (ret) {
14165 		dev_info(&pf->pdev->dev,
14166 			 "couldn't get vsi bw info, err %pe aq_err %s\n",
14167 			 ERR_PTR(ret),
14168 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14169 		/* VSI is already added so not tearing that up */
14170 		ret = 0;
14171 	}
14172 
14173 err:
14174 	return ret;
14175 }
14176 
14177 /**
14178  * i40e_vsi_release - Delete a VSI and free its resources
14179  * @vsi: the VSI being removed
14180  *
14181  * Returns 0 on success or < 0 on error
14182  **/
14183 int i40e_vsi_release(struct i40e_vsi *vsi)
14184 {
14185 	struct i40e_mac_filter *f;
14186 	struct hlist_node *h;
14187 	struct i40e_veb *veb = NULL;
14188 	struct i40e_pf *pf;
14189 	u16 uplink_seid;
14190 	int i, n, bkt;
14191 
14192 	pf = vsi->back;
14193 
14194 	/* release of a VEB-owner or last VSI is not allowed */
14195 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14196 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14197 			 vsi->seid, vsi->uplink_seid);
14198 		return -ENODEV;
14199 	}
14200 	if (vsi == pf->vsi[pf->lan_vsi] &&
14201 	    !test_bit(__I40E_DOWN, pf->state)) {
14202 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14203 		return -ENODEV;
14204 	}
14205 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14206 	uplink_seid = vsi->uplink_seid;
14207 	if (vsi->type != I40E_VSI_SRIOV) {
14208 		if (vsi->netdev_registered) {
14209 			vsi->netdev_registered = false;
14210 			if (vsi->netdev) {
14211 				/* results in a call to i40e_close() */
14212 				unregister_netdev(vsi->netdev);
14213 			}
14214 		} else {
14215 			i40e_vsi_close(vsi);
14216 		}
14217 		i40e_vsi_disable_irq(vsi);
14218 	}
14219 
14220 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14221 
14222 	/* clear the sync flag on all filters */
14223 	if (vsi->netdev) {
14224 		__dev_uc_unsync(vsi->netdev, NULL);
14225 		__dev_mc_unsync(vsi->netdev, NULL);
14226 	}
14227 
14228 	/* make sure any remaining filters are marked for deletion */
14229 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14230 		__i40e_del_filter(vsi, f);
14231 
14232 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14233 
14234 	i40e_sync_vsi_filters(vsi);
14235 
14236 	i40e_vsi_delete(vsi);
14237 	i40e_vsi_free_q_vectors(vsi);
14238 	if (vsi->netdev) {
14239 		free_netdev(vsi->netdev);
14240 		vsi->netdev = NULL;
14241 	}
14242 	i40e_vsi_clear_rings(vsi);
14243 	i40e_vsi_clear(vsi);
14244 
14245 	/* If this was the last thing on the VEB, except for the
14246 	 * controlling VSI, remove the VEB, which puts the controlling
14247 	 * VSI onto the next level down in the switch.
14248 	 *
14249 	 * Well, okay, there's one more exception here: don't remove
14250 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14251 	 * from up the network stack.
14252 	 */
14253 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14254 		if (pf->vsi[i] &&
14255 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14256 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14257 			n++;      /* count the VSIs */
14258 		}
14259 	}
14260 	for (i = 0; i < I40E_MAX_VEB; i++) {
14261 		if (!pf->veb[i])
14262 			continue;
14263 		if (pf->veb[i]->uplink_seid == uplink_seid)
14264 			n++;     /* count the VEBs */
14265 		if (pf->veb[i]->seid == uplink_seid)
14266 			veb = pf->veb[i];
14267 	}
14268 	if (n == 0 && veb && veb->uplink_seid != 0)
14269 		i40e_veb_release(veb);
14270 
14271 	return 0;
14272 }
14273 
14274 /**
14275  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14276  * @vsi: ptr to the VSI
14277  *
14278  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14279  * corresponding SW VSI structure and initializes num_queue_pairs for the
14280  * newly allocated VSI.
14281  *
14282  * Returns 0 on success or negative on failure
14283  **/
14284 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14285 {
14286 	int ret = -ENOENT;
14287 	struct i40e_pf *pf = vsi->back;
14288 
14289 	if (vsi->q_vectors[0]) {
14290 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14291 			 vsi->seid);
14292 		return -EEXIST;
14293 	}
14294 
14295 	if (vsi->base_vector) {
14296 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14297 			 vsi->seid, vsi->base_vector);
14298 		return -EEXIST;
14299 	}
14300 
14301 	ret = i40e_vsi_alloc_q_vectors(vsi);
14302 	if (ret) {
14303 		dev_info(&pf->pdev->dev,
14304 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14305 			 vsi->num_q_vectors, vsi->seid, ret);
14306 		vsi->num_q_vectors = 0;
14307 		goto vector_setup_out;
14308 	}
14309 
14310 	/* In Legacy mode, we do not have to get any other vector since we
14311 	 * piggyback on the misc/ICR0 for queue interrupts.
14312 	*/
14313 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14314 		return ret;
14315 	if (vsi->num_q_vectors)
14316 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14317 						 vsi->num_q_vectors, vsi->idx);
14318 	if (vsi->base_vector < 0) {
14319 		dev_info(&pf->pdev->dev,
14320 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14321 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14322 		i40e_vsi_free_q_vectors(vsi);
14323 		ret = -ENOENT;
14324 		goto vector_setup_out;
14325 	}
14326 
14327 vector_setup_out:
14328 	return ret;
14329 }
14330 
14331 /**
14332  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14333  * @vsi: pointer to the vsi.
14334  *
14335  * This re-allocates a vsi's queue resources.
14336  *
14337  * Returns pointer to the successfully allocated and configured VSI sw struct
14338  * on success, otherwise returns NULL on failure.
14339  **/
14340 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14341 {
14342 	u16 alloc_queue_pairs;
14343 	struct i40e_pf *pf;
14344 	u8 enabled_tc;
14345 	int ret;
14346 
14347 	if (!vsi)
14348 		return NULL;
14349 
14350 	pf = vsi->back;
14351 
14352 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14353 	i40e_vsi_clear_rings(vsi);
14354 
14355 	i40e_vsi_free_arrays(vsi, false);
14356 	i40e_set_num_rings_in_vsi(vsi);
14357 	ret = i40e_vsi_alloc_arrays(vsi, false);
14358 	if (ret)
14359 		goto err_vsi;
14360 
14361 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14362 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14363 
14364 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14365 	if (ret < 0) {
14366 		dev_info(&pf->pdev->dev,
14367 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14368 			 alloc_queue_pairs, vsi->seid, ret);
14369 		goto err_vsi;
14370 	}
14371 	vsi->base_queue = ret;
14372 
14373 	/* Update the FW view of the VSI. Force a reset of TC and queue
14374 	 * layout configurations.
14375 	 */
14376 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14377 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14378 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14379 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14380 	if (vsi->type == I40E_VSI_MAIN)
14381 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14382 
14383 	/* assign it some queues */
14384 	ret = i40e_alloc_rings(vsi);
14385 	if (ret)
14386 		goto err_rings;
14387 
14388 	/* map all of the rings to the q_vectors */
14389 	i40e_vsi_map_rings_to_vectors(vsi);
14390 	return vsi;
14391 
14392 err_rings:
14393 	i40e_vsi_free_q_vectors(vsi);
14394 	if (vsi->netdev_registered) {
14395 		vsi->netdev_registered = false;
14396 		unregister_netdev(vsi->netdev);
14397 		free_netdev(vsi->netdev);
14398 		vsi->netdev = NULL;
14399 	}
14400 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14401 err_vsi:
14402 	i40e_vsi_clear(vsi);
14403 	return NULL;
14404 }
14405 
14406 /**
14407  * i40e_vsi_setup - Set up a VSI by a given type
14408  * @pf: board private structure
14409  * @type: VSI type
14410  * @uplink_seid: the switch element to link to
14411  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14412  *
14413  * This allocates the sw VSI structure and its queue resources, then add a VSI
14414  * to the identified VEB.
14415  *
14416  * Returns pointer to the successfully allocated and configure VSI sw struct on
14417  * success, otherwise returns NULL on failure.
14418  **/
14419 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14420 				u16 uplink_seid, u32 param1)
14421 {
14422 	struct i40e_vsi *vsi = NULL;
14423 	struct i40e_veb *veb = NULL;
14424 	u16 alloc_queue_pairs;
14425 	int ret, i;
14426 	int v_idx;
14427 
14428 	/* The requested uplink_seid must be either
14429 	 *     - the PF's port seid
14430 	 *              no VEB is needed because this is the PF
14431 	 *              or this is a Flow Director special case VSI
14432 	 *     - seid of an existing VEB
14433 	 *     - seid of a VSI that owns an existing VEB
14434 	 *     - seid of a VSI that doesn't own a VEB
14435 	 *              a new VEB is created and the VSI becomes the owner
14436 	 *     - seid of the PF VSI, which is what creates the first VEB
14437 	 *              this is a special case of the previous
14438 	 *
14439 	 * Find which uplink_seid we were given and create a new VEB if needed
14440 	 */
14441 	for (i = 0; i < I40E_MAX_VEB; i++) {
14442 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14443 			veb = pf->veb[i];
14444 			break;
14445 		}
14446 	}
14447 
14448 	if (!veb && uplink_seid != pf->mac_seid) {
14449 
14450 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14451 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14452 				vsi = pf->vsi[i];
14453 				break;
14454 			}
14455 		}
14456 		if (!vsi) {
14457 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14458 				 uplink_seid);
14459 			return NULL;
14460 		}
14461 
14462 		if (vsi->uplink_seid == pf->mac_seid)
14463 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14464 					     vsi->tc_config.enabled_tc);
14465 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14466 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14467 					     vsi->tc_config.enabled_tc);
14468 		if (veb) {
14469 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14470 				dev_info(&vsi->back->pdev->dev,
14471 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14472 				return NULL;
14473 			}
14474 			/* We come up by default in VEPA mode if SRIOV is not
14475 			 * already enabled, in which case we can't force VEPA
14476 			 * mode.
14477 			 */
14478 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14479 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14480 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14481 			}
14482 			i40e_config_bridge_mode(veb);
14483 		}
14484 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14485 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14486 				veb = pf->veb[i];
14487 		}
14488 		if (!veb) {
14489 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14490 			return NULL;
14491 		}
14492 
14493 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14494 		uplink_seid = veb->seid;
14495 	}
14496 
14497 	/* get vsi sw struct */
14498 	v_idx = i40e_vsi_mem_alloc(pf, type);
14499 	if (v_idx < 0)
14500 		goto err_alloc;
14501 	vsi = pf->vsi[v_idx];
14502 	if (!vsi)
14503 		goto err_alloc;
14504 	vsi->type = type;
14505 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14506 
14507 	if (type == I40E_VSI_MAIN)
14508 		pf->lan_vsi = v_idx;
14509 	else if (type == I40E_VSI_SRIOV)
14510 		vsi->vf_id = param1;
14511 	/* assign it some queues */
14512 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14513 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14514 
14515 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14516 	if (ret < 0) {
14517 		dev_info(&pf->pdev->dev,
14518 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14519 			 alloc_queue_pairs, vsi->seid, ret);
14520 		goto err_vsi;
14521 	}
14522 	vsi->base_queue = ret;
14523 
14524 	/* get a VSI from the hardware */
14525 	vsi->uplink_seid = uplink_seid;
14526 	ret = i40e_add_vsi(vsi);
14527 	if (ret)
14528 		goto err_vsi;
14529 
14530 	switch (vsi->type) {
14531 	/* setup the netdev if needed */
14532 	case I40E_VSI_MAIN:
14533 	case I40E_VSI_VMDQ2:
14534 		ret = i40e_config_netdev(vsi);
14535 		if (ret)
14536 			goto err_netdev;
14537 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14538 		if (ret)
14539 			goto err_netdev;
14540 		ret = register_netdev(vsi->netdev);
14541 		if (ret)
14542 			goto err_netdev;
14543 		vsi->netdev_registered = true;
14544 		netif_carrier_off(vsi->netdev);
14545 #ifdef CONFIG_I40E_DCB
14546 		/* Setup DCB netlink interface */
14547 		i40e_dcbnl_setup(vsi);
14548 #endif /* CONFIG_I40E_DCB */
14549 		fallthrough;
14550 	case I40E_VSI_FDIR:
14551 		/* set up vectors and rings if needed */
14552 		ret = i40e_vsi_setup_vectors(vsi);
14553 		if (ret)
14554 			goto err_msix;
14555 
14556 		ret = i40e_alloc_rings(vsi);
14557 		if (ret)
14558 			goto err_rings;
14559 
14560 		/* map all of the rings to the q_vectors */
14561 		i40e_vsi_map_rings_to_vectors(vsi);
14562 
14563 		i40e_vsi_reset_stats(vsi);
14564 		break;
14565 	default:
14566 		/* no netdev or rings for the other VSI types */
14567 		break;
14568 	}
14569 
14570 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14571 	    (vsi->type == I40E_VSI_VMDQ2)) {
14572 		ret = i40e_vsi_config_rss(vsi);
14573 	}
14574 	return vsi;
14575 
14576 err_rings:
14577 	i40e_vsi_free_q_vectors(vsi);
14578 err_msix:
14579 	if (vsi->netdev_registered) {
14580 		vsi->netdev_registered = false;
14581 		unregister_netdev(vsi->netdev);
14582 		free_netdev(vsi->netdev);
14583 		vsi->netdev = NULL;
14584 	}
14585 err_netdev:
14586 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14587 err_vsi:
14588 	i40e_vsi_clear(vsi);
14589 err_alloc:
14590 	return NULL;
14591 }
14592 
14593 /**
14594  * i40e_veb_get_bw_info - Query VEB BW information
14595  * @veb: the veb to query
14596  *
14597  * Query the Tx scheduler BW configuration data for given VEB
14598  **/
14599 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14600 {
14601 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14602 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14603 	struct i40e_pf *pf = veb->pf;
14604 	struct i40e_hw *hw = &pf->hw;
14605 	u32 tc_bw_max;
14606 	int ret = 0;
14607 	int i;
14608 
14609 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14610 						  &bw_data, NULL);
14611 	if (ret) {
14612 		dev_info(&pf->pdev->dev,
14613 			 "query veb bw config failed, err %pe aq_err %s\n",
14614 			 ERR_PTR(ret),
14615 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14616 		goto out;
14617 	}
14618 
14619 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14620 						   &ets_data, NULL);
14621 	if (ret) {
14622 		dev_info(&pf->pdev->dev,
14623 			 "query veb bw ets config failed, err %pe aq_err %s\n",
14624 			 ERR_PTR(ret),
14625 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14626 		goto out;
14627 	}
14628 
14629 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14630 	veb->bw_max_quanta = ets_data.tc_bw_max;
14631 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14632 	veb->enabled_tc = ets_data.tc_valid_bits;
14633 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14634 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14635 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14636 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14637 		veb->bw_tc_limit_credits[i] =
14638 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14639 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14640 	}
14641 
14642 out:
14643 	return ret;
14644 }
14645 
14646 /**
14647  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14648  * @pf: board private structure
14649  *
14650  * On error: returns error code (negative)
14651  * On success: returns vsi index in PF (positive)
14652  **/
14653 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14654 {
14655 	int ret = -ENOENT;
14656 	struct i40e_veb *veb;
14657 	int i;
14658 
14659 	/* Need to protect the allocation of switch elements at the PF level */
14660 	mutex_lock(&pf->switch_mutex);
14661 
14662 	/* VEB list may be fragmented if VEB creation/destruction has
14663 	 * been happening.  We can afford to do a quick scan to look
14664 	 * for any free slots in the list.
14665 	 *
14666 	 * find next empty veb slot, looping back around if necessary
14667 	 */
14668 	i = 0;
14669 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14670 		i++;
14671 	if (i >= I40E_MAX_VEB) {
14672 		ret = -ENOMEM;
14673 		goto err_alloc_veb;  /* out of VEB slots! */
14674 	}
14675 
14676 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14677 	if (!veb) {
14678 		ret = -ENOMEM;
14679 		goto err_alloc_veb;
14680 	}
14681 	veb->pf = pf;
14682 	veb->idx = i;
14683 	veb->enabled_tc = 1;
14684 
14685 	pf->veb[i] = veb;
14686 	ret = i;
14687 err_alloc_veb:
14688 	mutex_unlock(&pf->switch_mutex);
14689 	return ret;
14690 }
14691 
14692 /**
14693  * i40e_switch_branch_release - Delete a branch of the switch tree
14694  * @branch: where to start deleting
14695  *
14696  * This uses recursion to find the tips of the branch to be
14697  * removed, deleting until we get back to and can delete this VEB.
14698  **/
14699 static void i40e_switch_branch_release(struct i40e_veb *branch)
14700 {
14701 	struct i40e_pf *pf = branch->pf;
14702 	u16 branch_seid = branch->seid;
14703 	u16 veb_idx = branch->idx;
14704 	int i;
14705 
14706 	/* release any VEBs on this VEB - RECURSION */
14707 	for (i = 0; i < I40E_MAX_VEB; i++) {
14708 		if (!pf->veb[i])
14709 			continue;
14710 		if (pf->veb[i]->uplink_seid == branch->seid)
14711 			i40e_switch_branch_release(pf->veb[i]);
14712 	}
14713 
14714 	/* Release the VSIs on this VEB, but not the owner VSI.
14715 	 *
14716 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14717 	 *       the VEB itself, so don't use (*branch) after this loop.
14718 	 */
14719 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14720 		if (!pf->vsi[i])
14721 			continue;
14722 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14723 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14724 			i40e_vsi_release(pf->vsi[i]);
14725 		}
14726 	}
14727 
14728 	/* There's one corner case where the VEB might not have been
14729 	 * removed, so double check it here and remove it if needed.
14730 	 * This case happens if the veb was created from the debugfs
14731 	 * commands and no VSIs were added to it.
14732 	 */
14733 	if (pf->veb[veb_idx])
14734 		i40e_veb_release(pf->veb[veb_idx]);
14735 }
14736 
14737 /**
14738  * i40e_veb_clear - remove veb struct
14739  * @veb: the veb to remove
14740  **/
14741 static void i40e_veb_clear(struct i40e_veb *veb)
14742 {
14743 	if (!veb)
14744 		return;
14745 
14746 	if (veb->pf) {
14747 		struct i40e_pf *pf = veb->pf;
14748 
14749 		mutex_lock(&pf->switch_mutex);
14750 		if (pf->veb[veb->idx] == veb)
14751 			pf->veb[veb->idx] = NULL;
14752 		mutex_unlock(&pf->switch_mutex);
14753 	}
14754 
14755 	kfree(veb);
14756 }
14757 
14758 /**
14759  * i40e_veb_release - Delete a VEB and free its resources
14760  * @veb: the VEB being removed
14761  **/
14762 void i40e_veb_release(struct i40e_veb *veb)
14763 {
14764 	struct i40e_vsi *vsi = NULL;
14765 	struct i40e_pf *pf;
14766 	int i, n = 0;
14767 
14768 	pf = veb->pf;
14769 
14770 	/* find the remaining VSI and check for extras */
14771 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14772 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14773 			n++;
14774 			vsi = pf->vsi[i];
14775 		}
14776 	}
14777 	if (n != 1) {
14778 		dev_info(&pf->pdev->dev,
14779 			 "can't remove VEB %d with %d VSIs left\n",
14780 			 veb->seid, n);
14781 		return;
14782 	}
14783 
14784 	/* move the remaining VSI to uplink veb */
14785 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14786 	if (veb->uplink_seid) {
14787 		vsi->uplink_seid = veb->uplink_seid;
14788 		if (veb->uplink_seid == pf->mac_seid)
14789 			vsi->veb_idx = I40E_NO_VEB;
14790 		else
14791 			vsi->veb_idx = veb->veb_idx;
14792 	} else {
14793 		/* floating VEB */
14794 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14795 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14796 	}
14797 
14798 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14799 	i40e_veb_clear(veb);
14800 }
14801 
14802 /**
14803  * i40e_add_veb - create the VEB in the switch
14804  * @veb: the VEB to be instantiated
14805  * @vsi: the controlling VSI
14806  **/
14807 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14808 {
14809 	struct i40e_pf *pf = veb->pf;
14810 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14811 	int ret;
14812 
14813 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14814 			      veb->enabled_tc, false,
14815 			      &veb->seid, enable_stats, NULL);
14816 
14817 	/* get a VEB from the hardware */
14818 	if (ret) {
14819 		dev_info(&pf->pdev->dev,
14820 			 "couldn't add VEB, err %pe aq_err %s\n",
14821 			 ERR_PTR(ret),
14822 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14823 		return -EPERM;
14824 	}
14825 
14826 	/* get statistics counter */
14827 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14828 					 &veb->stats_idx, NULL, NULL, NULL);
14829 	if (ret) {
14830 		dev_info(&pf->pdev->dev,
14831 			 "couldn't get VEB statistics idx, err %pe aq_err %s\n",
14832 			 ERR_PTR(ret),
14833 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14834 		return -EPERM;
14835 	}
14836 	ret = i40e_veb_get_bw_info(veb);
14837 	if (ret) {
14838 		dev_info(&pf->pdev->dev,
14839 			 "couldn't get VEB bw info, err %pe aq_err %s\n",
14840 			 ERR_PTR(ret),
14841 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14842 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14843 		return -ENOENT;
14844 	}
14845 
14846 	vsi->uplink_seid = veb->seid;
14847 	vsi->veb_idx = veb->idx;
14848 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14849 
14850 	return 0;
14851 }
14852 
14853 /**
14854  * i40e_veb_setup - Set up a VEB
14855  * @pf: board private structure
14856  * @flags: VEB setup flags
14857  * @uplink_seid: the switch element to link to
14858  * @vsi_seid: the initial VSI seid
14859  * @enabled_tc: Enabled TC bit-map
14860  *
14861  * This allocates the sw VEB structure and links it into the switch
14862  * It is possible and legal for this to be a duplicate of an already
14863  * existing VEB.  It is also possible for both uplink and vsi seids
14864  * to be zero, in order to create a floating VEB.
14865  *
14866  * Returns pointer to the successfully allocated VEB sw struct on
14867  * success, otherwise returns NULL on failure.
14868  **/
14869 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14870 				u16 uplink_seid, u16 vsi_seid,
14871 				u8 enabled_tc)
14872 {
14873 	struct i40e_veb *veb, *uplink_veb = NULL;
14874 	int vsi_idx, veb_idx;
14875 	int ret;
14876 
14877 	/* if one seid is 0, the other must be 0 to create a floating relay */
14878 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14879 	    (uplink_seid + vsi_seid != 0)) {
14880 		dev_info(&pf->pdev->dev,
14881 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14882 			 uplink_seid, vsi_seid);
14883 		return NULL;
14884 	}
14885 
14886 	/* make sure there is such a vsi and uplink */
14887 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14888 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14889 			break;
14890 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14891 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14892 			 vsi_seid);
14893 		return NULL;
14894 	}
14895 
14896 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14897 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14898 			if (pf->veb[veb_idx] &&
14899 			    pf->veb[veb_idx]->seid == uplink_seid) {
14900 				uplink_veb = pf->veb[veb_idx];
14901 				break;
14902 			}
14903 		}
14904 		if (!uplink_veb) {
14905 			dev_info(&pf->pdev->dev,
14906 				 "uplink seid %d not found\n", uplink_seid);
14907 			return NULL;
14908 		}
14909 	}
14910 
14911 	/* get veb sw struct */
14912 	veb_idx = i40e_veb_mem_alloc(pf);
14913 	if (veb_idx < 0)
14914 		goto err_alloc;
14915 	veb = pf->veb[veb_idx];
14916 	veb->flags = flags;
14917 	veb->uplink_seid = uplink_seid;
14918 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14919 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14920 
14921 	/* create the VEB in the switch */
14922 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14923 	if (ret)
14924 		goto err_veb;
14925 	if (vsi_idx == pf->lan_vsi)
14926 		pf->lan_veb = veb->idx;
14927 
14928 	return veb;
14929 
14930 err_veb:
14931 	i40e_veb_clear(veb);
14932 err_alloc:
14933 	return NULL;
14934 }
14935 
14936 /**
14937  * i40e_setup_pf_switch_element - set PF vars based on switch type
14938  * @pf: board private structure
14939  * @ele: element we are building info from
14940  * @num_reported: total number of elements
14941  * @printconfig: should we print the contents
14942  *
14943  * helper function to assist in extracting a few useful SEID values.
14944  **/
14945 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14946 				struct i40e_aqc_switch_config_element_resp *ele,
14947 				u16 num_reported, bool printconfig)
14948 {
14949 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14950 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14951 	u8 element_type = ele->element_type;
14952 	u16 seid = le16_to_cpu(ele->seid);
14953 
14954 	if (printconfig)
14955 		dev_info(&pf->pdev->dev,
14956 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14957 			 element_type, seid, uplink_seid, downlink_seid);
14958 
14959 	switch (element_type) {
14960 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14961 		pf->mac_seid = seid;
14962 		break;
14963 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14964 		/* Main VEB? */
14965 		if (uplink_seid != pf->mac_seid)
14966 			break;
14967 		if (pf->lan_veb >= I40E_MAX_VEB) {
14968 			int v;
14969 
14970 			/* find existing or else empty VEB */
14971 			for (v = 0; v < I40E_MAX_VEB; v++) {
14972 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14973 					pf->lan_veb = v;
14974 					break;
14975 				}
14976 			}
14977 			if (pf->lan_veb >= I40E_MAX_VEB) {
14978 				v = i40e_veb_mem_alloc(pf);
14979 				if (v < 0)
14980 					break;
14981 				pf->lan_veb = v;
14982 			}
14983 		}
14984 		if (pf->lan_veb >= I40E_MAX_VEB)
14985 			break;
14986 
14987 		pf->veb[pf->lan_veb]->seid = seid;
14988 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14989 		pf->veb[pf->lan_veb]->pf = pf;
14990 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14991 		break;
14992 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14993 		if (num_reported != 1)
14994 			break;
14995 		/* This is immediately after a reset so we can assume this is
14996 		 * the PF's VSI
14997 		 */
14998 		pf->mac_seid = uplink_seid;
14999 		pf->pf_seid = downlink_seid;
15000 		pf->main_vsi_seid = seid;
15001 		if (printconfig)
15002 			dev_info(&pf->pdev->dev,
15003 				 "pf_seid=%d main_vsi_seid=%d\n",
15004 				 pf->pf_seid, pf->main_vsi_seid);
15005 		break;
15006 	case I40E_SWITCH_ELEMENT_TYPE_PF:
15007 	case I40E_SWITCH_ELEMENT_TYPE_VF:
15008 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
15009 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
15010 	case I40E_SWITCH_ELEMENT_TYPE_PE:
15011 	case I40E_SWITCH_ELEMENT_TYPE_PA:
15012 		/* ignore these for now */
15013 		break;
15014 	default:
15015 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
15016 			 element_type, seid);
15017 		break;
15018 	}
15019 }
15020 
15021 /**
15022  * i40e_fetch_switch_configuration - Get switch config from firmware
15023  * @pf: board private structure
15024  * @printconfig: should we print the contents
15025  *
15026  * Get the current switch configuration from the device and
15027  * extract a few useful SEID values.
15028  **/
15029 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
15030 {
15031 	struct i40e_aqc_get_switch_config_resp *sw_config;
15032 	u16 next_seid = 0;
15033 	int ret = 0;
15034 	u8 *aq_buf;
15035 	int i;
15036 
15037 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
15038 	if (!aq_buf)
15039 		return -ENOMEM;
15040 
15041 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
15042 	do {
15043 		u16 num_reported, num_total;
15044 
15045 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
15046 						I40E_AQ_LARGE_BUF,
15047 						&next_seid, NULL);
15048 		if (ret) {
15049 			dev_info(&pf->pdev->dev,
15050 				 "get switch config failed err %d aq_err %s\n",
15051 				 ret,
15052 				 i40e_aq_str(&pf->hw,
15053 					     pf->hw.aq.asq_last_status));
15054 			kfree(aq_buf);
15055 			return -ENOENT;
15056 		}
15057 
15058 		num_reported = le16_to_cpu(sw_config->header.num_reported);
15059 		num_total = le16_to_cpu(sw_config->header.num_total);
15060 
15061 		if (printconfig)
15062 			dev_info(&pf->pdev->dev,
15063 				 "header: %d reported %d total\n",
15064 				 num_reported, num_total);
15065 
15066 		for (i = 0; i < num_reported; i++) {
15067 			struct i40e_aqc_switch_config_element_resp *ele =
15068 				&sw_config->element[i];
15069 
15070 			i40e_setup_pf_switch_element(pf, ele, num_reported,
15071 						     printconfig);
15072 		}
15073 	} while (next_seid != 0);
15074 
15075 	kfree(aq_buf);
15076 	return ret;
15077 }
15078 
15079 /**
15080  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15081  * @pf: board private structure
15082  * @reinit: if the Main VSI needs to re-initialized.
15083  * @lock_acquired: indicates whether or not the lock has been acquired
15084  *
15085  * Returns 0 on success, negative value on failure
15086  **/
15087 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15088 {
15089 	u16 flags = 0;
15090 	int ret;
15091 
15092 	/* find out what's out there already */
15093 	ret = i40e_fetch_switch_configuration(pf, false);
15094 	if (ret) {
15095 		dev_info(&pf->pdev->dev,
15096 			 "couldn't fetch switch config, err %pe aq_err %s\n",
15097 			 ERR_PTR(ret),
15098 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15099 		return ret;
15100 	}
15101 	i40e_pf_reset_stats(pf);
15102 
15103 	/* set the switch config bit for the whole device to
15104 	 * support limited promisc or true promisc
15105 	 * when user requests promisc. The default is limited
15106 	 * promisc.
15107 	*/
15108 
15109 	if ((pf->hw.pf_id == 0) &&
15110 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15111 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15112 		pf->last_sw_conf_flags = flags;
15113 	}
15114 
15115 	if (pf->hw.pf_id == 0) {
15116 		u16 valid_flags;
15117 
15118 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15119 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15120 						NULL);
15121 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15122 			dev_info(&pf->pdev->dev,
15123 				 "couldn't set switch config bits, err %pe aq_err %s\n",
15124 				 ERR_PTR(ret),
15125 				 i40e_aq_str(&pf->hw,
15126 					     pf->hw.aq.asq_last_status));
15127 			/* not a fatal problem, just keep going */
15128 		}
15129 		pf->last_sw_conf_valid_flags = valid_flags;
15130 	}
15131 
15132 	/* first time setup */
15133 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15134 		struct i40e_vsi *vsi = NULL;
15135 		u16 uplink_seid;
15136 
15137 		/* Set up the PF VSI associated with the PF's main VSI
15138 		 * that is already in the HW switch
15139 		 */
15140 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15141 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15142 		else
15143 			uplink_seid = pf->mac_seid;
15144 		if (pf->lan_vsi == I40E_NO_VSI)
15145 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15146 		else if (reinit)
15147 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15148 		if (!vsi) {
15149 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15150 			i40e_cloud_filter_exit(pf);
15151 			i40e_fdir_teardown(pf);
15152 			return -EAGAIN;
15153 		}
15154 	} else {
15155 		/* force a reset of TC and queue layout configurations */
15156 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15157 
15158 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15159 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15160 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15161 	}
15162 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15163 
15164 	i40e_fdir_sb_setup(pf);
15165 
15166 	/* Setup static PF queue filter control settings */
15167 	ret = i40e_setup_pf_filter_control(pf);
15168 	if (ret) {
15169 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15170 			 ret);
15171 		/* Failure here should not stop continuing other steps */
15172 	}
15173 
15174 	/* enable RSS in the HW, even for only one queue, as the stack can use
15175 	 * the hash
15176 	 */
15177 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15178 		i40e_pf_config_rss(pf);
15179 
15180 	/* fill in link information and enable LSE reporting */
15181 	i40e_link_event(pf);
15182 
15183 	/* Initialize user-specific link properties */
15184 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15185 				  I40E_AQ_AN_COMPLETED) ? true : false);
15186 
15187 	i40e_ptp_init(pf);
15188 
15189 	if (!lock_acquired)
15190 		rtnl_lock();
15191 
15192 	/* repopulate tunnel port filters */
15193 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15194 
15195 	if (!lock_acquired)
15196 		rtnl_unlock();
15197 
15198 	return ret;
15199 }
15200 
15201 /**
15202  * i40e_determine_queue_usage - Work out queue distribution
15203  * @pf: board private structure
15204  **/
15205 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15206 {
15207 	int queues_left;
15208 	int q_max;
15209 
15210 	pf->num_lan_qps = 0;
15211 
15212 	/* Find the max queues to be put into basic use.  We'll always be
15213 	 * using TC0, whether or not DCB is running, and TC0 will get the
15214 	 * big RSS set.
15215 	 */
15216 	queues_left = pf->hw.func_caps.num_tx_qp;
15217 
15218 	if ((queues_left == 1) ||
15219 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15220 		/* one qp for PF, no queues for anything else */
15221 		queues_left = 0;
15222 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15223 
15224 		/* make sure all the fancies are disabled */
15225 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15226 			       I40E_FLAG_IWARP_ENABLED	|
15227 			       I40E_FLAG_FD_SB_ENABLED	|
15228 			       I40E_FLAG_FD_ATR_ENABLED	|
15229 			       I40E_FLAG_DCB_CAPABLE	|
15230 			       I40E_FLAG_DCB_ENABLED	|
15231 			       I40E_FLAG_SRIOV_ENABLED	|
15232 			       I40E_FLAG_VMDQ_ENABLED);
15233 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15234 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15235 				  I40E_FLAG_FD_SB_ENABLED |
15236 				  I40E_FLAG_FD_ATR_ENABLED |
15237 				  I40E_FLAG_DCB_CAPABLE))) {
15238 		/* one qp for PF */
15239 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15240 		queues_left -= pf->num_lan_qps;
15241 
15242 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15243 			       I40E_FLAG_IWARP_ENABLED	|
15244 			       I40E_FLAG_FD_SB_ENABLED	|
15245 			       I40E_FLAG_FD_ATR_ENABLED	|
15246 			       I40E_FLAG_DCB_ENABLED	|
15247 			       I40E_FLAG_VMDQ_ENABLED);
15248 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15249 	} else {
15250 		/* Not enough queues for all TCs */
15251 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15252 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15253 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15254 					I40E_FLAG_DCB_ENABLED);
15255 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15256 		}
15257 
15258 		/* limit lan qps to the smaller of qps, cpus or msix */
15259 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15260 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15261 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15262 		pf->num_lan_qps = q_max;
15263 
15264 		queues_left -= pf->num_lan_qps;
15265 	}
15266 
15267 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15268 		if (queues_left > 1) {
15269 			queues_left -= 1; /* save 1 queue for FD */
15270 		} else {
15271 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15272 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15273 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15274 		}
15275 	}
15276 
15277 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15278 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15279 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15280 					(queues_left / pf->num_vf_qps));
15281 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15282 	}
15283 
15284 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15285 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15286 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15287 					  (queues_left / pf->num_vmdq_qps));
15288 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15289 	}
15290 
15291 	pf->queues_left = queues_left;
15292 	dev_dbg(&pf->pdev->dev,
15293 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15294 		pf->hw.func_caps.num_tx_qp,
15295 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15296 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15297 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15298 		queues_left);
15299 }
15300 
15301 /**
15302  * i40e_setup_pf_filter_control - Setup PF static filter control
15303  * @pf: PF to be setup
15304  *
15305  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15306  * settings. If PE/FCoE are enabled then it will also set the per PF
15307  * based filter sizes required for them. It also enables Flow director,
15308  * ethertype and macvlan type filter settings for the pf.
15309  *
15310  * Returns 0 on success, negative on failure
15311  **/
15312 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15313 {
15314 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15315 
15316 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15317 
15318 	/* Flow Director is enabled */
15319 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15320 		settings->enable_fdir = true;
15321 
15322 	/* Ethtype and MACVLAN filters enabled for PF */
15323 	settings->enable_ethtype = true;
15324 	settings->enable_macvlan = true;
15325 
15326 	if (i40e_set_filter_control(&pf->hw, settings))
15327 		return -ENOENT;
15328 
15329 	return 0;
15330 }
15331 
15332 #define INFO_STRING_LEN 255
15333 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15334 static void i40e_print_features(struct i40e_pf *pf)
15335 {
15336 	struct i40e_hw *hw = &pf->hw;
15337 	char *buf;
15338 	int i;
15339 
15340 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15341 	if (!buf)
15342 		return;
15343 
15344 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15345 #ifdef CONFIG_PCI_IOV
15346 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15347 #endif
15348 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15349 		      pf->hw.func_caps.num_vsis,
15350 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15351 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15352 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15353 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15354 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15355 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15356 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15357 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15358 	}
15359 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15360 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15361 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15362 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15363 	if (pf->flags & I40E_FLAG_PTP)
15364 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15365 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15366 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15367 	else
15368 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15369 
15370 	dev_info(&pf->pdev->dev, "%s\n", buf);
15371 	kfree(buf);
15372 	WARN_ON(i > INFO_STRING_LEN);
15373 }
15374 
15375 /**
15376  * i40e_get_platform_mac_addr - get platform-specific MAC address
15377  * @pdev: PCI device information struct
15378  * @pf: board private structure
15379  *
15380  * Look up the MAC address for the device. First we'll try
15381  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15382  * specific fallback. Otherwise, we'll default to the stored value in
15383  * firmware.
15384  **/
15385 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15386 {
15387 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15388 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15389 }
15390 
15391 /**
15392  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15393  * @fec_cfg: FEC option to set in flags
15394  * @flags: ptr to flags in which we set FEC option
15395  **/
15396 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15397 {
15398 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15399 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15400 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15401 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15402 		*flags |= I40E_FLAG_RS_FEC;
15403 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15404 	}
15405 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15406 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15407 		*flags |= I40E_FLAG_BASE_R_FEC;
15408 		*flags &= ~I40E_FLAG_RS_FEC;
15409 	}
15410 	if (fec_cfg == 0)
15411 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15412 }
15413 
15414 /**
15415  * i40e_check_recovery_mode - check if we are running transition firmware
15416  * @pf: board private structure
15417  *
15418  * Check registers indicating the firmware runs in recovery mode. Sets the
15419  * appropriate driver state.
15420  *
15421  * Returns true if the recovery mode was detected, false otherwise
15422  **/
15423 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15424 {
15425 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15426 
15427 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15428 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15429 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15430 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15431 
15432 		return true;
15433 	}
15434 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15435 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15436 
15437 	return false;
15438 }
15439 
15440 /**
15441  * i40e_pf_loop_reset - perform reset in a loop.
15442  * @pf: board private structure
15443  *
15444  * This function is useful when a NIC is about to enter recovery mode.
15445  * When a NIC's internal data structures are corrupted the NIC's
15446  * firmware is going to enter recovery mode.
15447  * Right after a POR it takes about 7 minutes for firmware to enter
15448  * recovery mode. Until that time a NIC is in some kind of intermediate
15449  * state. After that time period the NIC almost surely enters
15450  * recovery mode. The only way for a driver to detect intermediate
15451  * state is to issue a series of pf-resets and check a return value.
15452  * If a PF reset returns success then the firmware could be in recovery
15453  * mode so the caller of this code needs to check for recovery mode
15454  * if this function returns success. There is a little chance that
15455  * firmware will hang in intermediate state forever.
15456  * Since waiting 7 minutes is quite a lot of time this function waits
15457  * 10 seconds and then gives up by returning an error.
15458  *
15459  * Return 0 on success, negative on failure.
15460  **/
15461 static int i40e_pf_loop_reset(struct i40e_pf *pf)
15462 {
15463 	/* wait max 10 seconds for PF reset to succeed */
15464 	const unsigned long time_end = jiffies + 10 * HZ;
15465 	struct i40e_hw *hw = &pf->hw;
15466 	int ret;
15467 
15468 	ret = i40e_pf_reset(hw);
15469 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15470 		usleep_range(10000, 20000);
15471 		ret = i40e_pf_reset(hw);
15472 	}
15473 
15474 	if (ret == I40E_SUCCESS)
15475 		pf->pfr_count++;
15476 	else
15477 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15478 
15479 	return ret;
15480 }
15481 
15482 /**
15483  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15484  * @pf: board private structure
15485  *
15486  * Check FW registers to determine if FW issued unexpected EMP Reset.
15487  * Every time when unexpected EMP Reset occurs the FW increments
15488  * a counter of unexpected EMP Resets. When the counter reaches 10
15489  * the FW should enter the Recovery mode
15490  *
15491  * Returns true if FW issued unexpected EMP Reset
15492  **/
15493 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15494 {
15495 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15496 			   I40E_GL_FWSTS_FWS1B_MASK;
15497 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15498 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15499 }
15500 
15501 /**
15502  * i40e_handle_resets - handle EMP resets and PF resets
15503  * @pf: board private structure
15504  *
15505  * Handle both EMP resets and PF resets and conclude whether there are
15506  * any issues regarding these resets. If there are any issues then
15507  * generate log entry.
15508  *
15509  * Return 0 if NIC is healthy or negative value when there are issues
15510  * with resets
15511  **/
15512 static int i40e_handle_resets(struct i40e_pf *pf)
15513 {
15514 	const int pfr = i40e_pf_loop_reset(pf);
15515 	const bool is_empr = i40e_check_fw_empr(pf);
15516 
15517 	if (is_empr || pfr != I40E_SUCCESS)
15518 		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");
15519 
15520 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15521 }
15522 
15523 /**
15524  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15525  * @pf: board private structure
15526  * @hw: ptr to the hardware info
15527  *
15528  * This function does a minimal setup of all subsystems needed for running
15529  * recovery mode.
15530  *
15531  * Returns 0 on success, negative on failure
15532  **/
15533 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15534 {
15535 	struct i40e_vsi *vsi;
15536 	int err;
15537 	int v_idx;
15538 
15539 	pci_set_drvdata(pf->pdev, pf);
15540 	pci_save_state(pf->pdev);
15541 
15542 	/* set up periodic task facility */
15543 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15544 	pf->service_timer_period = HZ;
15545 
15546 	INIT_WORK(&pf->service_task, i40e_service_task);
15547 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15548 
15549 	err = i40e_init_interrupt_scheme(pf);
15550 	if (err)
15551 		goto err_switch_setup;
15552 
15553 	/* The number of VSIs reported by the FW is the minimum guaranteed
15554 	 * to us; HW supports far more and we share the remaining pool with
15555 	 * the other PFs. We allocate space for more than the guarantee with
15556 	 * the understanding that we might not get them all later.
15557 	 */
15558 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15559 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15560 	else
15561 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15562 
15563 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15564 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15565 			  GFP_KERNEL);
15566 	if (!pf->vsi) {
15567 		err = -ENOMEM;
15568 		goto err_switch_setup;
15569 	}
15570 
15571 	/* We allocate one VSI which is needed as absolute minimum
15572 	 * in order to register the netdev
15573 	 */
15574 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15575 	if (v_idx < 0) {
15576 		err = v_idx;
15577 		goto err_switch_setup;
15578 	}
15579 	pf->lan_vsi = v_idx;
15580 	vsi = pf->vsi[v_idx];
15581 	if (!vsi) {
15582 		err = -EFAULT;
15583 		goto err_switch_setup;
15584 	}
15585 	vsi->alloc_queue_pairs = 1;
15586 	err = i40e_config_netdev(vsi);
15587 	if (err)
15588 		goto err_switch_setup;
15589 	err = register_netdev(vsi->netdev);
15590 	if (err)
15591 		goto err_switch_setup;
15592 	vsi->netdev_registered = true;
15593 	i40e_dbg_pf_init(pf);
15594 
15595 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15596 	if (err)
15597 		goto err_switch_setup;
15598 
15599 	/* tell the firmware that we're starting */
15600 	i40e_send_version(pf);
15601 
15602 	/* since everything's happy, start the service_task timer */
15603 	mod_timer(&pf->service_timer,
15604 		  round_jiffies(jiffies + pf->service_timer_period));
15605 
15606 	return 0;
15607 
15608 err_switch_setup:
15609 	i40e_reset_interrupt_capability(pf);
15610 	timer_shutdown_sync(&pf->service_timer);
15611 	i40e_shutdown_adminq(hw);
15612 	iounmap(hw->hw_addr);
15613 	pci_release_mem_regions(pf->pdev);
15614 	pci_disable_device(pf->pdev);
15615 	kfree(pf);
15616 
15617 	return err;
15618 }
15619 
15620 /**
15621  * i40e_set_subsystem_device_id - set subsystem device id
15622  * @hw: pointer to the hardware info
15623  *
15624  * Set PCI subsystem device id either from a pci_dev structure or
15625  * a specific FW register.
15626  **/
15627 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15628 {
15629 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15630 
15631 	hw->subsystem_device_id = pdev->subsystem_device ?
15632 		pdev->subsystem_device :
15633 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15634 }
15635 
15636 /**
15637  * i40e_probe - Device initialization routine
15638  * @pdev: PCI device information struct
15639  * @ent: entry in i40e_pci_tbl
15640  *
15641  * i40e_probe initializes a PF identified by a pci_dev structure.
15642  * The OS initialization, configuring of the PF private structure,
15643  * and a hardware reset occur.
15644  *
15645  * Returns 0 on success, negative on failure
15646  **/
15647 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15648 {
15649 	struct i40e_aq_get_phy_abilities_resp abilities;
15650 #ifdef CONFIG_I40E_DCB
15651 	enum i40e_get_fw_lldp_status_resp lldp_status;
15652 #endif /* CONFIG_I40E_DCB */
15653 	struct i40e_pf *pf;
15654 	struct i40e_hw *hw;
15655 	static u16 pfs_found;
15656 	u16 wol_nvm_bits;
15657 	u16 link_status;
15658 #ifdef CONFIG_I40E_DCB
15659 	int status;
15660 #endif /* CONFIG_I40E_DCB */
15661 	int err;
15662 	u32 val;
15663 	u32 i;
15664 
15665 	err = pci_enable_device_mem(pdev);
15666 	if (err)
15667 		return err;
15668 
15669 	/* set up for high or low dma */
15670 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15671 	if (err) {
15672 		dev_err(&pdev->dev,
15673 			"DMA configuration failed: 0x%x\n", err);
15674 		goto err_dma;
15675 	}
15676 
15677 	/* set up pci connections */
15678 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15679 	if (err) {
15680 		dev_info(&pdev->dev,
15681 			 "pci_request_selected_regions failed %d\n", err);
15682 		goto err_pci_reg;
15683 	}
15684 
15685 	pci_set_master(pdev);
15686 
15687 	/* Now that we have a PCI connection, we need to do the
15688 	 * low level device setup.  This is primarily setting up
15689 	 * the Admin Queue structures and then querying for the
15690 	 * device's current profile information.
15691 	 */
15692 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15693 	if (!pf) {
15694 		err = -ENOMEM;
15695 		goto err_pf_alloc;
15696 	}
15697 	pf->next_vsi = 0;
15698 	pf->pdev = pdev;
15699 	set_bit(__I40E_DOWN, pf->state);
15700 
15701 	hw = &pf->hw;
15702 	hw->back = pf;
15703 
15704 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15705 				I40E_MAX_CSR_SPACE);
15706 	/* We believe that the highest register to read is
15707 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15708 	 * is not less than that before mapping to prevent a
15709 	 * kernel panic.
15710 	 */
15711 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15712 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15713 			pf->ioremap_len);
15714 		err = -ENOMEM;
15715 		goto err_ioremap;
15716 	}
15717 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15718 	if (!hw->hw_addr) {
15719 		err = -EIO;
15720 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15721 			 (unsigned int)pci_resource_start(pdev, 0),
15722 			 pf->ioremap_len, err);
15723 		goto err_ioremap;
15724 	}
15725 	hw->vendor_id = pdev->vendor;
15726 	hw->device_id = pdev->device;
15727 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15728 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15729 	i40e_set_subsystem_device_id(hw);
15730 	hw->bus.device = PCI_SLOT(pdev->devfn);
15731 	hw->bus.func = PCI_FUNC(pdev->devfn);
15732 	hw->bus.bus_id = pdev->bus->number;
15733 	pf->instance = pfs_found;
15734 
15735 	/* Select something other than the 802.1ad ethertype for the
15736 	 * switch to use internally and drop on ingress.
15737 	 */
15738 	hw->switch_tag = 0xffff;
15739 	hw->first_tag = ETH_P_8021AD;
15740 	hw->second_tag = ETH_P_8021Q;
15741 
15742 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15743 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15744 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15745 
15746 	/* set up the locks for the AQ, do this only once in probe
15747 	 * and destroy them only once in remove
15748 	 */
15749 	mutex_init(&hw->aq.asq_mutex);
15750 	mutex_init(&hw->aq.arq_mutex);
15751 
15752 	pf->msg_enable = netif_msg_init(debug,
15753 					NETIF_MSG_DRV |
15754 					NETIF_MSG_PROBE |
15755 					NETIF_MSG_LINK);
15756 	if (debug < -1)
15757 		pf->hw.debug_mask = debug;
15758 
15759 	/* do a special CORER for clearing PXE mode once at init */
15760 	if (hw->revision_id == 0 &&
15761 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15762 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15763 		i40e_flush(hw);
15764 		msleep(200);
15765 		pf->corer_count++;
15766 
15767 		i40e_clear_pxe_mode(hw);
15768 	}
15769 
15770 	/* Reset here to make sure all is clean and to define PF 'n' */
15771 	i40e_clear_hw(hw);
15772 
15773 	err = i40e_set_mac_type(hw);
15774 	if (err) {
15775 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15776 			 err);
15777 		goto err_pf_reset;
15778 	}
15779 
15780 	err = i40e_handle_resets(pf);
15781 	if (err)
15782 		goto err_pf_reset;
15783 
15784 	i40e_check_recovery_mode(pf);
15785 
15786 	if (is_kdump_kernel()) {
15787 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15788 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15789 	} else {
15790 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15791 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15792 	}
15793 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15794 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15795 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15796 
15797 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15798 		 "%s-%s:misc",
15799 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15800 
15801 	err = i40e_init_shared_code(hw);
15802 	if (err) {
15803 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15804 			 err);
15805 		goto err_pf_reset;
15806 	}
15807 
15808 	/* set up a default setting for link flow control */
15809 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15810 
15811 	err = i40e_init_adminq(hw);
15812 	if (err) {
15813 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15814 			dev_info(&pdev->dev,
15815 				 "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",
15816 				 hw->aq.api_maj_ver,
15817 				 hw->aq.api_min_ver,
15818 				 I40E_FW_API_VERSION_MAJOR,
15819 				 I40E_FW_MINOR_VERSION(hw));
15820 		else
15821 			dev_info(&pdev->dev,
15822 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15823 
15824 		goto err_pf_reset;
15825 	}
15826 	i40e_get_oem_version(hw);
15827 
15828 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15829 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15830 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15831 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15832 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15833 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15834 
15835 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15836 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15837 		dev_dbg(&pdev->dev,
15838 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15839 			 hw->aq.api_maj_ver,
15840 			 hw->aq.api_min_ver,
15841 			 I40E_FW_API_VERSION_MAJOR,
15842 			 I40E_FW_MINOR_VERSION(hw));
15843 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15844 		dev_info(&pdev->dev,
15845 			 "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",
15846 			 hw->aq.api_maj_ver,
15847 			 hw->aq.api_min_ver,
15848 			 I40E_FW_API_VERSION_MAJOR,
15849 			 I40E_FW_MINOR_VERSION(hw));
15850 
15851 	i40e_verify_eeprom(pf);
15852 
15853 	/* Rev 0 hardware was never productized */
15854 	if (hw->revision_id < 1)
15855 		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");
15856 
15857 	i40e_clear_pxe_mode(hw);
15858 
15859 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15860 	if (err)
15861 		goto err_adminq_setup;
15862 
15863 	err = i40e_sw_init(pf);
15864 	if (err) {
15865 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15866 		goto err_sw_init;
15867 	}
15868 
15869 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15870 		return i40e_init_recovery_mode(pf, hw);
15871 
15872 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15873 				hw->func_caps.num_rx_qp, 0, 0);
15874 	if (err) {
15875 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15876 		goto err_init_lan_hmc;
15877 	}
15878 
15879 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15880 	if (err) {
15881 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15882 		err = -ENOENT;
15883 		goto err_configure_lan_hmc;
15884 	}
15885 
15886 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15887 	 * Ignore error return codes because if it was already disabled via
15888 	 * hardware settings this will fail
15889 	 */
15890 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15891 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15892 		i40e_aq_stop_lldp(hw, true, false, NULL);
15893 	}
15894 
15895 	/* allow a platform config to override the HW addr */
15896 	i40e_get_platform_mac_addr(pdev, pf);
15897 
15898 	if (!is_valid_ether_addr(hw->mac.addr)) {
15899 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15900 		err = -EIO;
15901 		goto err_mac_addr;
15902 	}
15903 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15904 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15905 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15906 	if (is_valid_ether_addr(hw->mac.port_addr))
15907 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15908 
15909 	i40e_ptp_alloc_pins(pf);
15910 	pci_set_drvdata(pdev, pf);
15911 	pci_save_state(pdev);
15912 
15913 #ifdef CONFIG_I40E_DCB
15914 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15915 	(!status &&
15916 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15917 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15918 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15919 	dev_info(&pdev->dev,
15920 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15921 			"FW LLDP is disabled\n" :
15922 			"FW LLDP is enabled\n");
15923 
15924 	/* Enable FW to write default DCB config on link-up */
15925 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15926 
15927 	err = i40e_init_pf_dcb(pf);
15928 	if (err) {
15929 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15930 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15931 		/* Continue without DCB enabled */
15932 	}
15933 #endif /* CONFIG_I40E_DCB */
15934 
15935 	/* set up periodic task facility */
15936 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15937 	pf->service_timer_period = HZ;
15938 
15939 	INIT_WORK(&pf->service_task, i40e_service_task);
15940 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15941 
15942 	/* NVM bit on means WoL disabled for the port */
15943 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15944 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15945 		pf->wol_en = false;
15946 	else
15947 		pf->wol_en = true;
15948 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15949 
15950 	/* set up the main switch operations */
15951 	i40e_determine_queue_usage(pf);
15952 	err = i40e_init_interrupt_scheme(pf);
15953 	if (err)
15954 		goto err_switch_setup;
15955 
15956 	/* Reduce Tx and Rx pairs for kdump
15957 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15958 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15959 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15960 	 */
15961 	if (is_kdump_kernel())
15962 		pf->num_lan_msix = 1;
15963 
15964 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15965 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15966 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15967 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15968 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15969 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15970 						    UDP_TUNNEL_TYPE_GENEVE;
15971 
15972 	/* The number of VSIs reported by the FW is the minimum guaranteed
15973 	 * to us; HW supports far more and we share the remaining pool with
15974 	 * the other PFs. We allocate space for more than the guarantee with
15975 	 * the understanding that we might not get them all later.
15976 	 */
15977 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15978 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15979 	else
15980 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15981 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15982 		dev_warn(&pf->pdev->dev,
15983 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15984 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15985 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15986 	}
15987 
15988 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15989 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15990 			  GFP_KERNEL);
15991 	if (!pf->vsi) {
15992 		err = -ENOMEM;
15993 		goto err_switch_setup;
15994 	}
15995 
15996 #ifdef CONFIG_PCI_IOV
15997 	/* prep for VF support */
15998 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15999 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16000 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16001 		if (pci_num_vf(pdev))
16002 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
16003 	}
16004 #endif
16005 	err = i40e_setup_pf_switch(pf, false, false);
16006 	if (err) {
16007 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
16008 		goto err_vsis;
16009 	}
16010 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
16011 
16012 	/* if FDIR VSI was set up, start it now */
16013 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16014 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
16015 			i40e_vsi_open(pf->vsi[i]);
16016 			break;
16017 		}
16018 	}
16019 
16020 	/* The driver only wants link up/down and module qualification
16021 	 * reports from firmware.  Note the negative logic.
16022 	 */
16023 	err = i40e_aq_set_phy_int_mask(&pf->hw,
16024 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
16025 					 I40E_AQ_EVENT_MEDIA_NA |
16026 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
16027 	if (err)
16028 		dev_info(&pf->pdev->dev, "set phy mask fail, err %pe aq_err %s\n",
16029 			 ERR_PTR(err),
16030 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16031 
16032 	/* Reconfigure hardware for allowing smaller MSS in the case
16033 	 * of TSO, so that we avoid the MDD being fired and causing
16034 	 * a reset in the case of small MSS+TSO.
16035 	 */
16036 	val = rd32(hw, I40E_REG_MSS);
16037 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
16038 		val &= ~I40E_REG_MSS_MIN_MASK;
16039 		val |= I40E_64BYTE_MSS;
16040 		wr32(hw, I40E_REG_MSS, val);
16041 	}
16042 
16043 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
16044 		msleep(75);
16045 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
16046 		if (err)
16047 			dev_info(&pf->pdev->dev, "link restart failed, err %pe aq_err %s\n",
16048 				 ERR_PTR(err),
16049 				 i40e_aq_str(&pf->hw,
16050 					     pf->hw.aq.asq_last_status));
16051 	}
16052 	/* The main driver is (mostly) up and happy. We need to set this state
16053 	 * before setting up the misc vector or we get a race and the vector
16054 	 * ends up disabled forever.
16055 	 */
16056 	clear_bit(__I40E_DOWN, pf->state);
16057 
16058 	/* In case of MSIX we are going to setup the misc vector right here
16059 	 * to handle admin queue events etc. In case of legacy and MSI
16060 	 * the misc functionality and queue processing is combined in
16061 	 * the same vector and that gets setup at open.
16062 	 */
16063 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
16064 		err = i40e_setup_misc_vector(pf);
16065 		if (err) {
16066 			dev_info(&pdev->dev,
16067 				 "setup of misc vector failed: %d\n", err);
16068 			i40e_cloud_filter_exit(pf);
16069 			i40e_fdir_teardown(pf);
16070 			goto err_vsis;
16071 		}
16072 	}
16073 
16074 #ifdef CONFIG_PCI_IOV
16075 	/* prep for VF support */
16076 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16077 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16078 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16079 		/* disable link interrupts for VFs */
16080 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16081 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16082 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16083 		i40e_flush(hw);
16084 
16085 		if (pci_num_vf(pdev)) {
16086 			dev_info(&pdev->dev,
16087 				 "Active VFs found, allocating resources.\n");
16088 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16089 			if (err)
16090 				dev_info(&pdev->dev,
16091 					 "Error %d allocating resources for existing VFs\n",
16092 					 err);
16093 		}
16094 	}
16095 #endif /* CONFIG_PCI_IOV */
16096 
16097 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16098 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16099 						      pf->num_iwarp_msix,
16100 						      I40E_IWARP_IRQ_PILE_ID);
16101 		if (pf->iwarp_base_vector < 0) {
16102 			dev_info(&pdev->dev,
16103 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16104 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16105 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16106 		}
16107 	}
16108 
16109 	i40e_dbg_pf_init(pf);
16110 
16111 	/* tell the firmware that we're starting */
16112 	i40e_send_version(pf);
16113 
16114 	/* since everything's happy, start the service_task timer */
16115 	mod_timer(&pf->service_timer,
16116 		  round_jiffies(jiffies + pf->service_timer_period));
16117 
16118 	/* add this PF to client device list and launch a client service task */
16119 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16120 		err = i40e_lan_add_device(pf);
16121 		if (err)
16122 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16123 				 err);
16124 	}
16125 
16126 #define PCI_SPEED_SIZE 8
16127 #define PCI_WIDTH_SIZE 8
16128 	/* Devices on the IOSF bus do not have this information
16129 	 * and will report PCI Gen 1 x 1 by default so don't bother
16130 	 * checking them.
16131 	 */
16132 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16133 		char speed[PCI_SPEED_SIZE] = "Unknown";
16134 		char width[PCI_WIDTH_SIZE] = "Unknown";
16135 
16136 		/* Get the negotiated link width and speed from PCI config
16137 		 * space
16138 		 */
16139 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16140 					  &link_status);
16141 
16142 		i40e_set_pci_config_data(hw, link_status);
16143 
16144 		switch (hw->bus.speed) {
16145 		case i40e_bus_speed_8000:
16146 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16147 		case i40e_bus_speed_5000:
16148 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16149 		case i40e_bus_speed_2500:
16150 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16151 		default:
16152 			break;
16153 		}
16154 		switch (hw->bus.width) {
16155 		case i40e_bus_width_pcie_x8:
16156 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16157 		case i40e_bus_width_pcie_x4:
16158 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16159 		case i40e_bus_width_pcie_x2:
16160 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16161 		case i40e_bus_width_pcie_x1:
16162 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16163 		default:
16164 			break;
16165 		}
16166 
16167 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16168 			 speed, width);
16169 
16170 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16171 		    hw->bus.speed < i40e_bus_speed_8000) {
16172 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16173 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16174 		}
16175 	}
16176 
16177 	/* get the requested speeds from the fw */
16178 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16179 	if (err)
16180 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %pe last_status =  %s\n",
16181 			ERR_PTR(err),
16182 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16183 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16184 
16185 	/* set the FEC config due to the board capabilities */
16186 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16187 
16188 	/* get the supported phy types from the fw */
16189 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16190 	if (err)
16191 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %pe last_status =  %s\n",
16192 			ERR_PTR(err),
16193 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16194 
16195 	/* make sure the MFS hasn't been set lower than the default */
16196 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16197 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16198 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16199 	if (val < MAX_FRAME_SIZE_DEFAULT)
16200 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16201 			 i, val);
16202 
16203 	/* Add a filter to drop all Flow control frames from any VSI from being
16204 	 * transmitted. By doing so we stop a malicious VF from sending out
16205 	 * PAUSE or PFC frames and potentially controlling traffic for other
16206 	 * PF/VF VSIs.
16207 	 * The FW can still send Flow control frames if enabled.
16208 	 */
16209 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16210 						       pf->main_vsi_seid);
16211 
16212 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16213 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16214 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16215 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16216 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16217 	/* print a string summarizing features */
16218 	i40e_print_features(pf);
16219 
16220 	return 0;
16221 
16222 	/* Unwind what we've done if something failed in the setup */
16223 err_vsis:
16224 	set_bit(__I40E_DOWN, pf->state);
16225 	i40e_clear_interrupt_scheme(pf);
16226 	kfree(pf->vsi);
16227 err_switch_setup:
16228 	i40e_reset_interrupt_capability(pf);
16229 	timer_shutdown_sync(&pf->service_timer);
16230 err_mac_addr:
16231 err_configure_lan_hmc:
16232 	(void)i40e_shutdown_lan_hmc(hw);
16233 err_init_lan_hmc:
16234 	kfree(pf->qp_pile);
16235 err_sw_init:
16236 err_adminq_setup:
16237 err_pf_reset:
16238 	iounmap(hw->hw_addr);
16239 err_ioremap:
16240 	kfree(pf);
16241 err_pf_alloc:
16242 	pci_release_mem_regions(pdev);
16243 err_pci_reg:
16244 err_dma:
16245 	pci_disable_device(pdev);
16246 	return err;
16247 }
16248 
16249 /**
16250  * i40e_remove - Device removal routine
16251  * @pdev: PCI device information struct
16252  *
16253  * i40e_remove is called by the PCI subsystem to alert the driver
16254  * that is should release a PCI device.  This could be caused by a
16255  * Hot-Plug event, or because the driver is going to be removed from
16256  * memory.
16257  **/
16258 static void i40e_remove(struct pci_dev *pdev)
16259 {
16260 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16261 	struct i40e_hw *hw = &pf->hw;
16262 	int ret_code;
16263 	int i;
16264 
16265 	i40e_dbg_pf_exit(pf);
16266 
16267 	i40e_ptp_stop(pf);
16268 
16269 	/* Disable RSS in hw */
16270 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16271 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16272 
16273 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16274 	 * flags, once they are set, i40e_rebuild should not be called as
16275 	 * i40e_prep_for_reset always returns early.
16276 	 */
16277 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16278 		usleep_range(1000, 2000);
16279 	set_bit(__I40E_IN_REMOVE, pf->state);
16280 
16281 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16282 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16283 		i40e_free_vfs(pf);
16284 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16285 	}
16286 	/* no more scheduling of any task */
16287 	set_bit(__I40E_SUSPENDED, pf->state);
16288 	set_bit(__I40E_DOWN, pf->state);
16289 	if (pf->service_timer.function)
16290 		timer_shutdown_sync(&pf->service_timer);
16291 	if (pf->service_task.func)
16292 		cancel_work_sync(&pf->service_task);
16293 
16294 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16295 		struct i40e_vsi *vsi = pf->vsi[0];
16296 
16297 		/* We know that we have allocated only one vsi for this PF,
16298 		 * it was just for registering netdevice, so the interface
16299 		 * could be visible in the 'ifconfig' output
16300 		 */
16301 		unregister_netdev(vsi->netdev);
16302 		free_netdev(vsi->netdev);
16303 
16304 		goto unmap;
16305 	}
16306 
16307 	/* Client close must be called explicitly here because the timer
16308 	 * has been stopped.
16309 	 */
16310 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16311 
16312 	i40e_fdir_teardown(pf);
16313 
16314 	/* If there is a switch structure or any orphans, remove them.
16315 	 * This will leave only the PF's VSI remaining.
16316 	 */
16317 	for (i = 0; i < I40E_MAX_VEB; i++) {
16318 		if (!pf->veb[i])
16319 			continue;
16320 
16321 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16322 		    pf->veb[i]->uplink_seid == 0)
16323 			i40e_switch_branch_release(pf->veb[i]);
16324 	}
16325 
16326 	/* Now we can shutdown the PF's VSI, just before we kill
16327 	 * adminq and hmc.
16328 	 */
16329 	if (pf->vsi[pf->lan_vsi])
16330 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16331 
16332 	i40e_cloud_filter_exit(pf);
16333 
16334 	/* remove attached clients */
16335 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16336 		ret_code = i40e_lan_del_device(pf);
16337 		if (ret_code)
16338 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16339 				 ret_code);
16340 	}
16341 
16342 	/* shutdown and destroy the HMC */
16343 	if (hw->hmc.hmc_obj) {
16344 		ret_code = i40e_shutdown_lan_hmc(hw);
16345 		if (ret_code)
16346 			dev_warn(&pdev->dev,
16347 				 "Failed to destroy the HMC resources: %d\n",
16348 				 ret_code);
16349 	}
16350 
16351 unmap:
16352 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16353 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16354 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16355 		free_irq(pf->pdev->irq, pf);
16356 
16357 	/* shutdown the adminq */
16358 	i40e_shutdown_adminq(hw);
16359 
16360 	/* destroy the locks only once, here */
16361 	mutex_destroy(&hw->aq.arq_mutex);
16362 	mutex_destroy(&hw->aq.asq_mutex);
16363 
16364 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16365 	rtnl_lock();
16366 	i40e_clear_interrupt_scheme(pf);
16367 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16368 		if (pf->vsi[i]) {
16369 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16370 				i40e_vsi_clear_rings(pf->vsi[i]);
16371 			i40e_vsi_clear(pf->vsi[i]);
16372 			pf->vsi[i] = NULL;
16373 		}
16374 	}
16375 	rtnl_unlock();
16376 
16377 	for (i = 0; i < I40E_MAX_VEB; i++) {
16378 		kfree(pf->veb[i]);
16379 		pf->veb[i] = NULL;
16380 	}
16381 
16382 	kfree(pf->qp_pile);
16383 	kfree(pf->vsi);
16384 
16385 	iounmap(hw->hw_addr);
16386 	kfree(pf);
16387 	pci_release_mem_regions(pdev);
16388 
16389 	pci_disable_device(pdev);
16390 }
16391 
16392 /**
16393  * i40e_pci_error_detected - warning that something funky happened in PCI land
16394  * @pdev: PCI device information struct
16395  * @error: the type of PCI error
16396  *
16397  * Called to warn that something happened and the error handling steps
16398  * are in progress.  Allows the driver to quiesce things, be ready for
16399  * remediation.
16400  **/
16401 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16402 						pci_channel_state_t error)
16403 {
16404 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16405 
16406 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16407 
16408 	if (!pf) {
16409 		dev_info(&pdev->dev,
16410 			 "Cannot recover - error happened during device probe\n");
16411 		return PCI_ERS_RESULT_DISCONNECT;
16412 	}
16413 
16414 	/* shutdown all operations */
16415 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16416 		i40e_prep_for_reset(pf);
16417 
16418 	/* Request a slot reset */
16419 	return PCI_ERS_RESULT_NEED_RESET;
16420 }
16421 
16422 /**
16423  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16424  * @pdev: PCI device information struct
16425  *
16426  * Called to find if the driver can work with the device now that
16427  * the pci slot has been reset.  If a basic connection seems good
16428  * (registers are readable and have sane content) then return a
16429  * happy little PCI_ERS_RESULT_xxx.
16430  **/
16431 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16432 {
16433 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16434 	pci_ers_result_t result;
16435 	u32 reg;
16436 
16437 	dev_dbg(&pdev->dev, "%s\n", __func__);
16438 	if (pci_enable_device_mem(pdev)) {
16439 		dev_info(&pdev->dev,
16440 			 "Cannot re-enable PCI device after reset.\n");
16441 		result = PCI_ERS_RESULT_DISCONNECT;
16442 	} else {
16443 		pci_set_master(pdev);
16444 		pci_restore_state(pdev);
16445 		pci_save_state(pdev);
16446 		pci_wake_from_d3(pdev, false);
16447 
16448 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16449 		if (reg == 0)
16450 			result = PCI_ERS_RESULT_RECOVERED;
16451 		else
16452 			result = PCI_ERS_RESULT_DISCONNECT;
16453 	}
16454 
16455 	return result;
16456 }
16457 
16458 /**
16459  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16460  * @pdev: PCI device information struct
16461  */
16462 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16463 {
16464 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16465 
16466 	i40e_prep_for_reset(pf);
16467 }
16468 
16469 /**
16470  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16471  * @pdev: PCI device information struct
16472  */
16473 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16474 {
16475 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16476 
16477 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16478 		return;
16479 
16480 	i40e_reset_and_rebuild(pf, false, false);
16481 }
16482 
16483 /**
16484  * i40e_pci_error_resume - restart operations after PCI error recovery
16485  * @pdev: PCI device information struct
16486  *
16487  * Called to allow the driver to bring things back up after PCI error
16488  * and/or reset recovery has finished.
16489  **/
16490 static void i40e_pci_error_resume(struct pci_dev *pdev)
16491 {
16492 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16493 
16494 	dev_dbg(&pdev->dev, "%s\n", __func__);
16495 	if (test_bit(__I40E_SUSPENDED, pf->state))
16496 		return;
16497 
16498 	i40e_handle_reset_warning(pf, false);
16499 }
16500 
16501 /**
16502  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16503  * using the mac_address_write admin q function
16504  * @pf: pointer to i40e_pf struct
16505  **/
16506 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16507 {
16508 	struct i40e_hw *hw = &pf->hw;
16509 	u8 mac_addr[6];
16510 	u16 flags = 0;
16511 	int ret;
16512 
16513 	/* Get current MAC address in case it's an LAA */
16514 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16515 		ether_addr_copy(mac_addr,
16516 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16517 	} else {
16518 		dev_err(&pf->pdev->dev,
16519 			"Failed to retrieve MAC address; using default\n");
16520 		ether_addr_copy(mac_addr, hw->mac.addr);
16521 	}
16522 
16523 	/* The FW expects the mac address write cmd to first be called with
16524 	 * one of these flags before calling it again with the multicast
16525 	 * enable flags.
16526 	 */
16527 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16528 
16529 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16530 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16531 
16532 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16533 	if (ret) {
16534 		dev_err(&pf->pdev->dev,
16535 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16536 		return;
16537 	}
16538 
16539 	flags = I40E_AQC_MC_MAG_EN
16540 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16541 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16542 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16543 	if (ret)
16544 		dev_err(&pf->pdev->dev,
16545 			"Failed to enable Multicast Magic Packet wake up\n");
16546 }
16547 
16548 /**
16549  * i40e_shutdown - PCI callback for shutting down
16550  * @pdev: PCI device information struct
16551  **/
16552 static void i40e_shutdown(struct pci_dev *pdev)
16553 {
16554 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16555 	struct i40e_hw *hw = &pf->hw;
16556 
16557 	set_bit(__I40E_SUSPENDED, pf->state);
16558 	set_bit(__I40E_DOWN, pf->state);
16559 
16560 	del_timer_sync(&pf->service_timer);
16561 	cancel_work_sync(&pf->service_task);
16562 	i40e_cloud_filter_exit(pf);
16563 	i40e_fdir_teardown(pf);
16564 
16565 	/* Client close must be called explicitly here because the timer
16566 	 * has been stopped.
16567 	 */
16568 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16569 
16570 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16571 		i40e_enable_mc_magic_wake(pf);
16572 
16573 	i40e_prep_for_reset(pf);
16574 
16575 	wr32(hw, I40E_PFPM_APM,
16576 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16577 	wr32(hw, I40E_PFPM_WUFC,
16578 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16579 
16580 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16581 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16582 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16583 		free_irq(pf->pdev->irq, pf);
16584 
16585 	/* Since we're going to destroy queues during the
16586 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16587 	 * whole section
16588 	 */
16589 	rtnl_lock();
16590 	i40e_clear_interrupt_scheme(pf);
16591 	rtnl_unlock();
16592 
16593 	if (system_state == SYSTEM_POWER_OFF) {
16594 		pci_wake_from_d3(pdev, pf->wol_en);
16595 		pci_set_power_state(pdev, PCI_D3hot);
16596 	}
16597 }
16598 
16599 /**
16600  * i40e_suspend - PM callback for moving to D3
16601  * @dev: generic device information structure
16602  **/
16603 static int __maybe_unused i40e_suspend(struct device *dev)
16604 {
16605 	struct i40e_pf *pf = dev_get_drvdata(dev);
16606 	struct i40e_hw *hw = &pf->hw;
16607 
16608 	/* If we're already suspended, then there is nothing to do */
16609 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16610 		return 0;
16611 
16612 	set_bit(__I40E_DOWN, pf->state);
16613 
16614 	/* Ensure service task will not be running */
16615 	del_timer_sync(&pf->service_timer);
16616 	cancel_work_sync(&pf->service_task);
16617 
16618 	/* Client close must be called explicitly here because the timer
16619 	 * has been stopped.
16620 	 */
16621 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16622 
16623 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16624 		i40e_enable_mc_magic_wake(pf);
16625 
16626 	/* Since we're going to destroy queues during the
16627 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16628 	 * whole section
16629 	 */
16630 	rtnl_lock();
16631 
16632 	i40e_prep_for_reset(pf);
16633 
16634 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16635 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16636 
16637 	/* Clear the interrupt scheme and release our IRQs so that the system
16638 	 * can safely hibernate even when there are a large number of CPUs.
16639 	 * Otherwise hibernation might fail when mapping all the vectors back
16640 	 * to CPU0.
16641 	 */
16642 	i40e_clear_interrupt_scheme(pf);
16643 
16644 	rtnl_unlock();
16645 
16646 	return 0;
16647 }
16648 
16649 /**
16650  * i40e_resume - PM callback for waking up from D3
16651  * @dev: generic device information structure
16652  **/
16653 static int __maybe_unused i40e_resume(struct device *dev)
16654 {
16655 	struct i40e_pf *pf = dev_get_drvdata(dev);
16656 	int err;
16657 
16658 	/* If we're not suspended, then there is nothing to do */
16659 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16660 		return 0;
16661 
16662 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16663 	 * since we're going to be restoring queues
16664 	 */
16665 	rtnl_lock();
16666 
16667 	/* We cleared the interrupt scheme when we suspended, so we need to
16668 	 * restore it now to resume device functionality.
16669 	 */
16670 	err = i40e_restore_interrupt_scheme(pf);
16671 	if (err) {
16672 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16673 			err);
16674 	}
16675 
16676 	clear_bit(__I40E_DOWN, pf->state);
16677 	i40e_reset_and_rebuild(pf, false, true);
16678 
16679 	rtnl_unlock();
16680 
16681 	/* Clear suspended state last after everything is recovered */
16682 	clear_bit(__I40E_SUSPENDED, pf->state);
16683 
16684 	/* Restart the service task */
16685 	mod_timer(&pf->service_timer,
16686 		  round_jiffies(jiffies + pf->service_timer_period));
16687 
16688 	return 0;
16689 }
16690 
16691 static const struct pci_error_handlers i40e_err_handler = {
16692 	.error_detected = i40e_pci_error_detected,
16693 	.slot_reset = i40e_pci_error_slot_reset,
16694 	.reset_prepare = i40e_pci_error_reset_prepare,
16695 	.reset_done = i40e_pci_error_reset_done,
16696 	.resume = i40e_pci_error_resume,
16697 };
16698 
16699 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16700 
16701 static struct pci_driver i40e_driver = {
16702 	.name     = i40e_driver_name,
16703 	.id_table = i40e_pci_tbl,
16704 	.probe    = i40e_probe,
16705 	.remove   = i40e_remove,
16706 	.driver   = {
16707 		.pm = &i40e_pm_ops,
16708 	},
16709 	.shutdown = i40e_shutdown,
16710 	.err_handler = &i40e_err_handler,
16711 	.sriov_configure = i40e_pci_sriov_configure,
16712 };
16713 
16714 /**
16715  * i40e_init_module - Driver registration routine
16716  *
16717  * i40e_init_module is the first routine called when the driver is
16718  * loaded. All it does is register with the PCI subsystem.
16719  **/
16720 static int __init i40e_init_module(void)
16721 {
16722 	int err;
16723 
16724 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16725 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16726 
16727 	/* There is no need to throttle the number of active tasks because
16728 	 * each device limits its own task using a state bit for scheduling
16729 	 * the service task, and the device tasks do not interfere with each
16730 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16731 	 * since we need to be able to guarantee forward progress even under
16732 	 * memory pressure.
16733 	 */
16734 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16735 	if (!i40e_wq) {
16736 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16737 		return -ENOMEM;
16738 	}
16739 
16740 	i40e_dbg_init();
16741 	err = pci_register_driver(&i40e_driver);
16742 	if (err) {
16743 		destroy_workqueue(i40e_wq);
16744 		i40e_dbg_exit();
16745 		return err;
16746 	}
16747 
16748 	return 0;
16749 }
16750 module_init(i40e_init_module);
16751 
16752 /**
16753  * i40e_exit_module - Driver exit cleanup routine
16754  *
16755  * i40e_exit_module is called just before the driver is removed
16756  * from memory.
16757  **/
16758 static void __exit i40e_exit_module(void)
16759 {
16760 	pci_unregister_driver(&i40e_driver);
16761 	destroy_workqueue(i40e_wq);
16762 	ida_destroy(&i40e_client_ida);
16763 	i40e_dbg_exit();
16764 }
16765 module_exit(i40e_exit_module);
16766