xref: /openbmc/linux/drivers/net/ethernet/intel/i40e/i40e_main.c (revision e1f4ecab19338ec7079830e8700e4869f991fd45)
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 (ether_addr_equal(netdev->dev_addr, addr->sa_data)) {
1792 		netdev_info(netdev, "already using mac address %pM\n",
1793 			    addr->sa_data);
1794 		return 0;
1795 	}
1796 
1797 	if (test_bit(__I40E_DOWN, pf->state) ||
1798 	    test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
1799 		return -EADDRNOTAVAIL;
1800 
1801 	if (ether_addr_equal(hw->mac.addr, addr->sa_data))
1802 		netdev_info(netdev, "returning to hw mac address %pM\n",
1803 			    hw->mac.addr);
1804 	else
1805 		netdev_info(netdev, "set new mac address %pM\n", addr->sa_data);
1806 
1807 	/* Copy the address first, so that we avoid a possible race with
1808 	 * .set_rx_mode().
1809 	 * - Remove old address from MAC filter
1810 	 * - Copy new address
1811 	 * - Add new address to MAC filter
1812 	 */
1813 	spin_lock_bh(&vsi->mac_filter_hash_lock);
1814 	i40e_del_mac_filter(vsi, netdev->dev_addr);
1815 	eth_hw_addr_set(netdev, addr->sa_data);
1816 	i40e_add_mac_filter(vsi, netdev->dev_addr);
1817 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
1818 
1819 	if (vsi->type == I40E_VSI_MAIN) {
1820 		i40e_status ret;
1821 
1822 		ret = i40e_aq_mac_address_write(hw, I40E_AQC_WRITE_TYPE_LAA_WOL,
1823 						addr->sa_data, NULL);
1824 		if (ret)
1825 			netdev_info(netdev, "Ignoring error from firmware on LAA update, status %s, AQ ret %s\n",
1826 				    i40e_stat_str(hw, ret),
1827 				    i40e_aq_str(hw, hw->aq.asq_last_status));
1828 	}
1829 
1830 	/* schedule our worker thread which will take care of
1831 	 * applying the new filter changes
1832 	 */
1833 	i40e_service_event_schedule(pf);
1834 	return 0;
1835 }
1836 
1837 /**
1838  * i40e_config_rss_aq - Prepare for RSS using AQ commands
1839  * @vsi: vsi structure
1840  * @seed: RSS hash seed
1841  * @lut: pointer to lookup table of lut_size
1842  * @lut_size: size of the lookup table
1843  **/
1844 static int i40e_config_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
1845 			      u8 *lut, u16 lut_size)
1846 {
1847 	struct i40e_pf *pf = vsi->back;
1848 	struct i40e_hw *hw = &pf->hw;
1849 	int ret = 0;
1850 
1851 	if (seed) {
1852 		struct i40e_aqc_get_set_rss_key_data *seed_dw =
1853 			(struct i40e_aqc_get_set_rss_key_data *)seed;
1854 		ret = i40e_aq_set_rss_key(hw, vsi->id, seed_dw);
1855 		if (ret) {
1856 			dev_info(&pf->pdev->dev,
1857 				 "Cannot set RSS key, err %s aq_err %s\n",
1858 				 i40e_stat_str(hw, ret),
1859 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1860 			return ret;
1861 		}
1862 	}
1863 	if (lut) {
1864 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
1865 
1866 		ret = i40e_aq_set_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
1867 		if (ret) {
1868 			dev_info(&pf->pdev->dev,
1869 				 "Cannot set RSS lut, err %s aq_err %s\n",
1870 				 i40e_stat_str(hw, ret),
1871 				 i40e_aq_str(hw, hw->aq.asq_last_status));
1872 			return ret;
1873 		}
1874 	}
1875 	return ret;
1876 }
1877 
1878 /**
1879  * i40e_vsi_config_rss - Prepare for VSI(VMDq) RSS if used
1880  * @vsi: VSI structure
1881  **/
1882 static int i40e_vsi_config_rss(struct i40e_vsi *vsi)
1883 {
1884 	struct i40e_pf *pf = vsi->back;
1885 	u8 seed[I40E_HKEY_ARRAY_SIZE];
1886 	u8 *lut;
1887 	int ret;
1888 
1889 	if (!(pf->hw_features & I40E_HW_RSS_AQ_CAPABLE))
1890 		return 0;
1891 	if (!vsi->rss_size)
1892 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
1893 				      vsi->num_queue_pairs);
1894 	if (!vsi->rss_size)
1895 		return -EINVAL;
1896 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
1897 	if (!lut)
1898 		return -ENOMEM;
1899 
1900 	/* Use the user configured hash keys and lookup table if there is one,
1901 	 * otherwise use default
1902 	 */
1903 	if (vsi->rss_lut_user)
1904 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
1905 	else
1906 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
1907 	if (vsi->rss_hkey_user)
1908 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
1909 	else
1910 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
1911 	ret = i40e_config_rss_aq(vsi, seed, lut, vsi->rss_table_size);
1912 	kfree(lut);
1913 	return ret;
1914 }
1915 
1916 /**
1917  * i40e_vsi_setup_queue_map_mqprio - Prepares mqprio based tc_config
1918  * @vsi: the VSI being configured,
1919  * @ctxt: VSI context structure
1920  * @enabled_tc: number of traffic classes to enable
1921  *
1922  * Prepares VSI tc_config to have queue configurations based on MQPRIO options.
1923  **/
1924 static int i40e_vsi_setup_queue_map_mqprio(struct i40e_vsi *vsi,
1925 					   struct i40e_vsi_context *ctxt,
1926 					   u8 enabled_tc)
1927 {
1928 	u16 qcount = 0, max_qcount, qmap, sections = 0;
1929 	int i, override_q, pow, num_qps, ret;
1930 	u8 netdev_tc = 0, offset = 0;
1931 
1932 	if (vsi->type != I40E_VSI_MAIN)
1933 		return -EINVAL;
1934 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
1935 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
1936 	vsi->tc_config.numtc = vsi->mqprio_qopt.qopt.num_tc;
1937 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
1938 	num_qps = vsi->mqprio_qopt.qopt.count[0];
1939 
1940 	/* find the next higher power-of-2 of num queue pairs */
1941 	pow = ilog2(num_qps);
1942 	if (!is_power_of_2(num_qps))
1943 		pow++;
1944 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
1945 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
1946 
1947 	/* Setup queue offset/count for all TCs for given VSI */
1948 	max_qcount = vsi->mqprio_qopt.qopt.count[0];
1949 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
1950 		/* See if the given TC is enabled for the given VSI */
1951 		if (vsi->tc_config.enabled_tc & BIT(i)) {
1952 			offset = vsi->mqprio_qopt.qopt.offset[i];
1953 			qcount = vsi->mqprio_qopt.qopt.count[i];
1954 			if (qcount > max_qcount)
1955 				max_qcount = qcount;
1956 			vsi->tc_config.tc_info[i].qoffset = offset;
1957 			vsi->tc_config.tc_info[i].qcount = qcount;
1958 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
1959 		} else {
1960 			/* TC is not enabled so set the offset to
1961 			 * default queue and allocate one queue
1962 			 * for the given TC.
1963 			 */
1964 			vsi->tc_config.tc_info[i].qoffset = 0;
1965 			vsi->tc_config.tc_info[i].qcount = 1;
1966 			vsi->tc_config.tc_info[i].netdev_tc = 0;
1967 		}
1968 	}
1969 
1970 	/* Set actual Tx/Rx queue pairs */
1971 	vsi->num_queue_pairs = offset + qcount;
1972 
1973 	/* Setup queue TC[0].qmap for given VSI context */
1974 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
1975 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
1976 	ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
1977 	ctxt->info.valid_sections |= cpu_to_le16(sections);
1978 
1979 	/* Reconfigure RSS for main VSI with max queue count */
1980 	vsi->rss_size = max_qcount;
1981 	ret = i40e_vsi_config_rss(vsi);
1982 	if (ret) {
1983 		dev_info(&vsi->back->pdev->dev,
1984 			 "Failed to reconfig rss for num_queues (%u)\n",
1985 			 max_qcount);
1986 		return ret;
1987 	}
1988 	vsi->reconfig_rss = true;
1989 	dev_dbg(&vsi->back->pdev->dev,
1990 		"Reconfigured rss with num_queues (%u)\n", max_qcount);
1991 
1992 	/* Find queue count available for channel VSIs and starting offset
1993 	 * for channel VSIs
1994 	 */
1995 	override_q = vsi->mqprio_qopt.qopt.count[0];
1996 	if (override_q && override_q < vsi->num_queue_pairs) {
1997 		vsi->cnt_q_avail = vsi->num_queue_pairs - override_q;
1998 		vsi->next_base_queue = override_q;
1999 	}
2000 	return 0;
2001 }
2002 
2003 /**
2004  * i40e_vsi_setup_queue_map - Setup a VSI queue map based on enabled_tc
2005  * @vsi: the VSI being setup
2006  * @ctxt: VSI context structure
2007  * @enabled_tc: Enabled TCs bitmap
2008  * @is_add: True if called before Add VSI
2009  *
2010  * Setup VSI queue mapping for enabled traffic classes.
2011  **/
2012 static void i40e_vsi_setup_queue_map(struct i40e_vsi *vsi,
2013 				     struct i40e_vsi_context *ctxt,
2014 				     u8 enabled_tc,
2015 				     bool is_add)
2016 {
2017 	struct i40e_pf *pf = vsi->back;
2018 	u16 num_tc_qps = 0;
2019 	u16 sections = 0;
2020 	u8 netdev_tc = 0;
2021 	u16 numtc = 1;
2022 	u16 qcount;
2023 	u8 offset;
2024 	u16 qmap;
2025 	int i;
2026 
2027 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
2028 	offset = 0;
2029 	/* zero out queue mapping, it will get updated on the end of the function */
2030 	memset(ctxt->info.queue_mapping, 0, sizeof(ctxt->info.queue_mapping));
2031 
2032 	if (vsi->type == I40E_VSI_MAIN) {
2033 		/* This code helps add more queue to the VSI if we have
2034 		 * more cores than RSS can support, the higher cores will
2035 		 * be served by ATR or other filters. Furthermore, the
2036 		 * non-zero req_queue_pairs says that user requested a new
2037 		 * queue count via ethtool's set_channels, so use this
2038 		 * value for queues distribution across traffic classes
2039 		 * We need at least one queue pair for the interface
2040 		 * to be usable as we see in else statement.
2041 		 */
2042 		if (vsi->req_queue_pairs > 0)
2043 			vsi->num_queue_pairs = vsi->req_queue_pairs;
2044 		else if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2045 			vsi->num_queue_pairs = pf->num_lan_msix;
2046 		else
2047 			vsi->num_queue_pairs = 1;
2048 	}
2049 
2050 	/* Number of queues per enabled TC */
2051 	if (vsi->type == I40E_VSI_MAIN ||
2052 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs != 0))
2053 		num_tc_qps = vsi->num_queue_pairs;
2054 	else
2055 		num_tc_qps = vsi->alloc_queue_pairs;
2056 
2057 	if (enabled_tc && (vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
2058 		/* Find numtc from enabled TC bitmap */
2059 		for (i = 0, numtc = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2060 			if (enabled_tc & BIT(i)) /* TC is enabled */
2061 				numtc++;
2062 		}
2063 		if (!numtc) {
2064 			dev_warn(&pf->pdev->dev, "DCB is enabled but no TC enabled, forcing TC0\n");
2065 			numtc = 1;
2066 		}
2067 		num_tc_qps = num_tc_qps / numtc;
2068 		num_tc_qps = min_t(int, num_tc_qps,
2069 				   i40e_pf_get_max_q_per_tc(pf));
2070 	}
2071 
2072 	vsi->tc_config.numtc = numtc;
2073 	vsi->tc_config.enabled_tc = enabled_tc ? enabled_tc : 1;
2074 
2075 	/* Do not allow use more TC queue pairs than MSI-X vectors exist */
2076 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
2077 		num_tc_qps = min_t(int, num_tc_qps, pf->num_lan_msix);
2078 
2079 	/* Setup queue offset/count for all TCs for given VSI */
2080 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
2081 		/* See if the given TC is enabled for the given VSI */
2082 		if (vsi->tc_config.enabled_tc & BIT(i)) {
2083 			/* TC is enabled */
2084 			int pow, num_qps;
2085 
2086 			switch (vsi->type) {
2087 			case I40E_VSI_MAIN:
2088 				if (!(pf->flags & (I40E_FLAG_FD_SB_ENABLED |
2089 				    I40E_FLAG_FD_ATR_ENABLED)) ||
2090 				    vsi->tc_config.enabled_tc != 1) {
2091 					qcount = min_t(int, pf->alloc_rss_size,
2092 						       num_tc_qps);
2093 					break;
2094 				}
2095 				fallthrough;
2096 			case I40E_VSI_FDIR:
2097 			case I40E_VSI_SRIOV:
2098 			case I40E_VSI_VMDQ2:
2099 			default:
2100 				qcount = num_tc_qps;
2101 				WARN_ON(i != 0);
2102 				break;
2103 			}
2104 			vsi->tc_config.tc_info[i].qoffset = offset;
2105 			vsi->tc_config.tc_info[i].qcount = qcount;
2106 
2107 			/* find the next higher power-of-2 of num queue pairs */
2108 			num_qps = qcount;
2109 			pow = 0;
2110 			while (num_qps && (BIT_ULL(pow) < qcount)) {
2111 				pow++;
2112 				num_qps >>= 1;
2113 			}
2114 
2115 			vsi->tc_config.tc_info[i].netdev_tc = netdev_tc++;
2116 			qmap =
2117 			    (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
2118 			    (pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
2119 
2120 			offset += qcount;
2121 		} else {
2122 			/* TC is not enabled so set the offset to
2123 			 * default queue and allocate one queue
2124 			 * for the given TC.
2125 			 */
2126 			vsi->tc_config.tc_info[i].qoffset = 0;
2127 			vsi->tc_config.tc_info[i].qcount = 1;
2128 			vsi->tc_config.tc_info[i].netdev_tc = 0;
2129 
2130 			qmap = 0;
2131 		}
2132 		ctxt->info.tc_mapping[i] = cpu_to_le16(qmap);
2133 	}
2134 	/* Do not change previously set num_queue_pairs for PFs and VFs*/
2135 	if ((vsi->type == I40E_VSI_MAIN && numtc != 1) ||
2136 	    (vsi->type == I40E_VSI_SRIOV && vsi->num_queue_pairs == 0) ||
2137 	    (vsi->type != I40E_VSI_MAIN && vsi->type != I40E_VSI_SRIOV))
2138 		vsi->num_queue_pairs = offset;
2139 
2140 	/* Scheduler section valid can only be set for ADD VSI */
2141 	if (is_add) {
2142 		sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
2143 
2144 		ctxt->info.up_enable_bits = enabled_tc;
2145 	}
2146 	if (vsi->type == I40E_VSI_SRIOV) {
2147 		ctxt->info.mapping_flags |=
2148 				     cpu_to_le16(I40E_AQ_VSI_QUE_MAP_NONCONTIG);
2149 		for (i = 0; i < vsi->num_queue_pairs; i++)
2150 			ctxt->info.queue_mapping[i] =
2151 					       cpu_to_le16(vsi->base_queue + i);
2152 	} else {
2153 		ctxt->info.mapping_flags |=
2154 					cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
2155 		ctxt->info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
2156 	}
2157 	ctxt->info.valid_sections |= cpu_to_le16(sections);
2158 }
2159 
2160 /**
2161  * i40e_addr_sync - Callback for dev_(mc|uc)_sync to add address
2162  * @netdev: the netdevice
2163  * @addr: address to add
2164  *
2165  * Called by __dev_(mc|uc)_sync when an address needs to be added. We call
2166  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2167  */
2168 static int i40e_addr_sync(struct net_device *netdev, const u8 *addr)
2169 {
2170 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2171 	struct i40e_vsi *vsi = np->vsi;
2172 
2173 	if (i40e_add_mac_filter(vsi, addr))
2174 		return 0;
2175 	else
2176 		return -ENOMEM;
2177 }
2178 
2179 /**
2180  * i40e_addr_unsync - Callback for dev_(mc|uc)_sync to remove address
2181  * @netdev: the netdevice
2182  * @addr: address to add
2183  *
2184  * Called by __dev_(mc|uc)_sync when an address needs to be removed. We call
2185  * __dev_(uc|mc)_sync from .set_rx_mode and guarantee to hold the hash lock.
2186  */
2187 static int i40e_addr_unsync(struct net_device *netdev, const u8 *addr)
2188 {
2189 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2190 	struct i40e_vsi *vsi = np->vsi;
2191 
2192 	/* Under some circumstances, we might receive a request to delete
2193 	 * our own device address from our uc list. Because we store the
2194 	 * device address in the VSI's MAC/VLAN filter list, we need to ignore
2195 	 * such requests and not delete our device address from this list.
2196 	 */
2197 	if (ether_addr_equal(addr, netdev->dev_addr))
2198 		return 0;
2199 
2200 	i40e_del_mac_filter(vsi, addr);
2201 
2202 	return 0;
2203 }
2204 
2205 /**
2206  * i40e_set_rx_mode - NDO callback to set the netdev filters
2207  * @netdev: network interface device structure
2208  **/
2209 static void i40e_set_rx_mode(struct net_device *netdev)
2210 {
2211 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2212 	struct i40e_vsi *vsi = np->vsi;
2213 
2214 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2215 
2216 	__dev_uc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2217 	__dev_mc_sync(netdev, i40e_addr_sync, i40e_addr_unsync);
2218 
2219 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2220 
2221 	/* check for other flag changes */
2222 	if (vsi->current_netdev_flags != vsi->netdev->flags) {
2223 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2224 		set_bit(__I40E_MACVLAN_SYNC_PENDING, vsi->back->state);
2225 	}
2226 }
2227 
2228 /**
2229  * i40e_undo_del_filter_entries - Undo the changes made to MAC filter entries
2230  * @vsi: Pointer to VSI struct
2231  * @from: Pointer to list which contains MAC filter entries - changes to
2232  *        those entries needs to be undone.
2233  *
2234  * MAC filter entries from this list were slated for deletion.
2235  **/
2236 static void i40e_undo_del_filter_entries(struct i40e_vsi *vsi,
2237 					 struct hlist_head *from)
2238 {
2239 	struct i40e_mac_filter *f;
2240 	struct hlist_node *h;
2241 
2242 	hlist_for_each_entry_safe(f, h, from, hlist) {
2243 		u64 key = i40e_addr_to_hkey(f->macaddr);
2244 
2245 		/* Move the element back into MAC filter list*/
2246 		hlist_del(&f->hlist);
2247 		hash_add(vsi->mac_filter_hash, &f->hlist, key);
2248 	}
2249 }
2250 
2251 /**
2252  * i40e_undo_add_filter_entries - Undo the changes made to MAC filter entries
2253  * @vsi: Pointer to vsi struct
2254  * @from: Pointer to list which contains MAC filter entries - changes to
2255  *        those entries needs to be undone.
2256  *
2257  * MAC filter entries from this list were slated for addition.
2258  **/
2259 static void i40e_undo_add_filter_entries(struct i40e_vsi *vsi,
2260 					 struct hlist_head *from)
2261 {
2262 	struct i40e_new_mac_filter *new;
2263 	struct hlist_node *h;
2264 
2265 	hlist_for_each_entry_safe(new, h, from, hlist) {
2266 		/* We can simply free the wrapper structure */
2267 		hlist_del(&new->hlist);
2268 		netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2269 		kfree(new);
2270 	}
2271 }
2272 
2273 /**
2274  * i40e_next_filter - Get the next non-broadcast filter from a list
2275  * @next: pointer to filter in list
2276  *
2277  * Returns the next non-broadcast filter in the list. Required so that we
2278  * ignore broadcast filters within the list, since these are not handled via
2279  * the normal firmware update path.
2280  */
2281 static
2282 struct i40e_new_mac_filter *i40e_next_filter(struct i40e_new_mac_filter *next)
2283 {
2284 	hlist_for_each_entry_continue(next, hlist) {
2285 		if (!is_broadcast_ether_addr(next->f->macaddr))
2286 			return next;
2287 	}
2288 
2289 	return NULL;
2290 }
2291 
2292 /**
2293  * i40e_update_filter_state - Update filter state based on return data
2294  * from firmware
2295  * @count: Number of filters added
2296  * @add_list: return data from fw
2297  * @add_head: pointer to first filter in current batch
2298  *
2299  * MAC filter entries from list were slated to be added to device. Returns
2300  * number of successful filters. Note that 0 does NOT mean success!
2301  **/
2302 static int
2303 i40e_update_filter_state(int count,
2304 			 struct i40e_aqc_add_macvlan_element_data *add_list,
2305 			 struct i40e_new_mac_filter *add_head)
2306 {
2307 	int retval = 0;
2308 	int i;
2309 
2310 	for (i = 0; i < count; i++) {
2311 		/* Always check status of each filter. We don't need to check
2312 		 * the firmware return status because we pre-set the filter
2313 		 * status to I40E_AQC_MM_ERR_NO_RES when sending the filter
2314 		 * request to the adminq. Thus, if it no longer matches then
2315 		 * we know the filter is active.
2316 		 */
2317 		if (add_list[i].match_method == I40E_AQC_MM_ERR_NO_RES) {
2318 			add_head->state = I40E_FILTER_FAILED;
2319 		} else {
2320 			add_head->state = I40E_FILTER_ACTIVE;
2321 			retval++;
2322 		}
2323 
2324 		add_head = i40e_next_filter(add_head);
2325 		if (!add_head)
2326 			break;
2327 	}
2328 
2329 	return retval;
2330 }
2331 
2332 /**
2333  * i40e_aqc_del_filters - Request firmware to delete a set of filters
2334  * @vsi: ptr to the VSI
2335  * @vsi_name: name to display in messages
2336  * @list: the list of filters to send to firmware
2337  * @num_del: the number of filters to delete
2338  * @retval: Set to -EIO on failure to delete
2339  *
2340  * Send a request to firmware via AdminQ to delete a set of filters. Uses
2341  * *retval instead of a return value so that success does not force ret_val to
2342  * be set to 0. This ensures that a sequence of calls to this function
2343  * preserve the previous value of *retval on successful delete.
2344  */
2345 static
2346 void i40e_aqc_del_filters(struct i40e_vsi *vsi, const char *vsi_name,
2347 			  struct i40e_aqc_remove_macvlan_element_data *list,
2348 			  int num_del, int *retval)
2349 {
2350 	struct i40e_hw *hw = &vsi->back->hw;
2351 	enum i40e_admin_queue_err aq_status;
2352 	i40e_status aq_ret;
2353 
2354 	aq_ret = i40e_aq_remove_macvlan_v2(hw, vsi->seid, list, num_del, NULL,
2355 					   &aq_status);
2356 
2357 	/* Explicitly ignore and do not report when firmware returns ENOENT */
2358 	if (aq_ret && !(aq_status == I40E_AQ_RC_ENOENT)) {
2359 		*retval = -EIO;
2360 		dev_info(&vsi->back->pdev->dev,
2361 			 "ignoring delete macvlan error on %s, err %s, aq_err %s\n",
2362 			 vsi_name, i40e_stat_str(hw, aq_ret),
2363 			 i40e_aq_str(hw, aq_status));
2364 	}
2365 }
2366 
2367 /**
2368  * i40e_aqc_add_filters - Request firmware to add a set of filters
2369  * @vsi: ptr to the VSI
2370  * @vsi_name: name to display in messages
2371  * @list: the list of filters to send to firmware
2372  * @add_head: Position in the add hlist
2373  * @num_add: the number of filters to add
2374  *
2375  * Send a request to firmware via AdminQ to add a chunk of filters. Will set
2376  * __I40E_VSI_OVERFLOW_PROMISC bit in vsi->state if the firmware has run out of
2377  * space for more filters.
2378  */
2379 static
2380 void i40e_aqc_add_filters(struct i40e_vsi *vsi, const char *vsi_name,
2381 			  struct i40e_aqc_add_macvlan_element_data *list,
2382 			  struct i40e_new_mac_filter *add_head,
2383 			  int num_add)
2384 {
2385 	struct i40e_hw *hw = &vsi->back->hw;
2386 	enum i40e_admin_queue_err aq_status;
2387 	int fcnt;
2388 
2389 	i40e_aq_add_macvlan_v2(hw, vsi->seid, list, num_add, NULL, &aq_status);
2390 	fcnt = i40e_update_filter_state(num_add, list, add_head);
2391 
2392 	if (fcnt != num_add) {
2393 		if (vsi->type == I40E_VSI_MAIN) {
2394 			set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2395 			dev_warn(&vsi->back->pdev->dev,
2396 				 "Error %s adding RX filters on %s, promiscuous mode forced on\n",
2397 				 i40e_aq_str(hw, aq_status), vsi_name);
2398 		} else if (vsi->type == I40E_VSI_SRIOV ||
2399 			   vsi->type == I40E_VSI_VMDQ1 ||
2400 			   vsi->type == I40E_VSI_VMDQ2) {
2401 			dev_warn(&vsi->back->pdev->dev,
2402 				 "Error %s adding RX filters on %s, please set promiscuous on manually for %s\n",
2403 				 i40e_aq_str(hw, aq_status), vsi_name,
2404 					     vsi_name);
2405 		} else {
2406 			dev_warn(&vsi->back->pdev->dev,
2407 				 "Error %s adding RX filters on %s, incorrect VSI type: %i.\n",
2408 				 i40e_aq_str(hw, aq_status), vsi_name,
2409 					     vsi->type);
2410 		}
2411 	}
2412 }
2413 
2414 /**
2415  * i40e_aqc_broadcast_filter - Set promiscuous broadcast flags
2416  * @vsi: pointer to the VSI
2417  * @vsi_name: the VSI name
2418  * @f: filter data
2419  *
2420  * This function sets or clears the promiscuous broadcast flags for VLAN
2421  * filters in order to properly receive broadcast frames. Assumes that only
2422  * broadcast filters are passed.
2423  *
2424  * Returns status indicating success or failure;
2425  **/
2426 static i40e_status
2427 i40e_aqc_broadcast_filter(struct i40e_vsi *vsi, const char *vsi_name,
2428 			  struct i40e_mac_filter *f)
2429 {
2430 	bool enable = f->state == I40E_FILTER_NEW;
2431 	struct i40e_hw *hw = &vsi->back->hw;
2432 	i40e_status aq_ret;
2433 
2434 	if (f->vlan == I40E_VLAN_ANY) {
2435 		aq_ret = i40e_aq_set_vsi_broadcast(hw,
2436 						   vsi->seid,
2437 						   enable,
2438 						   NULL);
2439 	} else {
2440 		aq_ret = i40e_aq_set_vsi_bc_promisc_on_vlan(hw,
2441 							    vsi->seid,
2442 							    enable,
2443 							    f->vlan,
2444 							    NULL);
2445 	}
2446 
2447 	if (aq_ret) {
2448 		set_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2449 		dev_warn(&vsi->back->pdev->dev,
2450 			 "Error %s, forcing overflow promiscuous on %s\n",
2451 			 i40e_aq_str(hw, hw->aq.asq_last_status),
2452 			 vsi_name);
2453 	}
2454 
2455 	return aq_ret;
2456 }
2457 
2458 /**
2459  * i40e_set_promiscuous - set promiscuous mode
2460  * @pf: board private structure
2461  * @promisc: promisc on or off
2462  *
2463  * There are different ways of setting promiscuous mode on a PF depending on
2464  * what state/environment we're in.  This identifies and sets it appropriately.
2465  * Returns 0 on success.
2466  **/
2467 static int i40e_set_promiscuous(struct i40e_pf *pf, bool promisc)
2468 {
2469 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
2470 	struct i40e_hw *hw = &pf->hw;
2471 	i40e_status aq_ret;
2472 
2473 	if (vsi->type == I40E_VSI_MAIN &&
2474 	    pf->lan_veb != I40E_NO_VEB &&
2475 	    !(pf->flags & I40E_FLAG_MFP_ENABLED)) {
2476 		/* set defport ON for Main VSI instead of true promisc
2477 		 * this way we will get all unicast/multicast and VLAN
2478 		 * promisc behavior but will not get VF or VMDq traffic
2479 		 * replicated on the Main VSI.
2480 		 */
2481 		if (promisc)
2482 			aq_ret = i40e_aq_set_default_vsi(hw,
2483 							 vsi->seid,
2484 							 NULL);
2485 		else
2486 			aq_ret = i40e_aq_clear_default_vsi(hw,
2487 							   vsi->seid,
2488 							   NULL);
2489 		if (aq_ret) {
2490 			dev_info(&pf->pdev->dev,
2491 				 "Set default VSI failed, err %s, aq_err %s\n",
2492 				 i40e_stat_str(hw, aq_ret),
2493 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2494 		}
2495 	} else {
2496 		aq_ret = i40e_aq_set_vsi_unicast_promiscuous(
2497 						  hw,
2498 						  vsi->seid,
2499 						  promisc, NULL,
2500 						  true);
2501 		if (aq_ret) {
2502 			dev_info(&pf->pdev->dev,
2503 				 "set unicast promisc failed, err %s, aq_err %s\n",
2504 				 i40e_stat_str(hw, aq_ret),
2505 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2506 		}
2507 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(
2508 						  hw,
2509 						  vsi->seid,
2510 						  promisc, NULL);
2511 		if (aq_ret) {
2512 			dev_info(&pf->pdev->dev,
2513 				 "set multicast promisc failed, err %s, aq_err %s\n",
2514 				 i40e_stat_str(hw, aq_ret),
2515 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2516 		}
2517 	}
2518 
2519 	if (!aq_ret)
2520 		pf->cur_promisc = promisc;
2521 
2522 	return aq_ret;
2523 }
2524 
2525 /**
2526  * i40e_sync_vsi_filters - Update the VSI filter list to the HW
2527  * @vsi: ptr to the VSI
2528  *
2529  * Push any outstanding VSI filter changes through the AdminQ.
2530  *
2531  * Returns 0 or error value
2532  **/
2533 int i40e_sync_vsi_filters(struct i40e_vsi *vsi)
2534 {
2535 	struct hlist_head tmp_add_list, tmp_del_list;
2536 	struct i40e_mac_filter *f;
2537 	struct i40e_new_mac_filter *new, *add_head = NULL;
2538 	struct i40e_hw *hw = &vsi->back->hw;
2539 	bool old_overflow, new_overflow;
2540 	unsigned int failed_filters = 0;
2541 	unsigned int vlan_filters = 0;
2542 	char vsi_name[16] = "PF";
2543 	int filter_list_len = 0;
2544 	i40e_status aq_ret = 0;
2545 	u32 changed_flags = 0;
2546 	struct hlist_node *h;
2547 	struct i40e_pf *pf;
2548 	int num_add = 0;
2549 	int num_del = 0;
2550 	int retval = 0;
2551 	u16 cmd_flags;
2552 	int list_size;
2553 	int bkt;
2554 
2555 	/* empty array typed pointers, kcalloc later */
2556 	struct i40e_aqc_add_macvlan_element_data *add_list;
2557 	struct i40e_aqc_remove_macvlan_element_data *del_list;
2558 
2559 	while (test_and_set_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state))
2560 		usleep_range(1000, 2000);
2561 	pf = vsi->back;
2562 
2563 	old_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2564 
2565 	if (vsi->netdev) {
2566 		changed_flags = vsi->current_netdev_flags ^ vsi->netdev->flags;
2567 		vsi->current_netdev_flags = vsi->netdev->flags;
2568 	}
2569 
2570 	INIT_HLIST_HEAD(&tmp_add_list);
2571 	INIT_HLIST_HEAD(&tmp_del_list);
2572 
2573 	if (vsi->type == I40E_VSI_SRIOV)
2574 		snprintf(vsi_name, sizeof(vsi_name) - 1, "VF %d", vsi->vf_id);
2575 	else if (vsi->type != I40E_VSI_MAIN)
2576 		snprintf(vsi_name, sizeof(vsi_name) - 1, "vsi %d", vsi->seid);
2577 
2578 	if (vsi->flags & I40E_VSI_FLAG_FILTER_CHANGED) {
2579 		vsi->flags &= ~I40E_VSI_FLAG_FILTER_CHANGED;
2580 
2581 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2582 		/* Create a list of filters to delete. */
2583 		hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
2584 			if (f->state == I40E_FILTER_REMOVE) {
2585 				/* Move the element into temporary del_list */
2586 				hash_del(&f->hlist);
2587 				hlist_add_head(&f->hlist, &tmp_del_list);
2588 
2589 				/* Avoid counting removed filters */
2590 				continue;
2591 			}
2592 			if (f->state == I40E_FILTER_NEW) {
2593 				/* Create a temporary i40e_new_mac_filter */
2594 				new = kzalloc(sizeof(*new), GFP_ATOMIC);
2595 				if (!new)
2596 					goto err_no_memory_locked;
2597 
2598 				/* Store pointer to the real filter */
2599 				new->f = f;
2600 				new->state = f->state;
2601 
2602 				/* Add it to the hash list */
2603 				hlist_add_head(&new->hlist, &tmp_add_list);
2604 			}
2605 
2606 			/* Count the number of active (current and new) VLAN
2607 			 * filters we have now. Does not count filters which
2608 			 * are marked for deletion.
2609 			 */
2610 			if (f->vlan > 0)
2611 				vlan_filters++;
2612 		}
2613 
2614 		if (vsi->type != I40E_VSI_SRIOV)
2615 			retval = i40e_correct_mac_vlan_filters
2616 				(vsi, &tmp_add_list, &tmp_del_list,
2617 				 vlan_filters);
2618 		else
2619 			retval = i40e_correct_vf_mac_vlan_filters
2620 				(vsi, &tmp_add_list, &tmp_del_list,
2621 				 vlan_filters, pf->vf[vsi->vf_id].trusted);
2622 
2623 		hlist_for_each_entry(new, &tmp_add_list, hlist)
2624 			netdev_hw_addr_refcnt(new->f, vsi->netdev, 1);
2625 
2626 		if (retval)
2627 			goto err_no_memory_locked;
2628 
2629 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2630 	}
2631 
2632 	/* Now process 'del_list' outside the lock */
2633 	if (!hlist_empty(&tmp_del_list)) {
2634 		filter_list_len = hw->aq.asq_buf_size /
2635 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2636 		list_size = filter_list_len *
2637 			    sizeof(struct i40e_aqc_remove_macvlan_element_data);
2638 		del_list = kzalloc(list_size, GFP_ATOMIC);
2639 		if (!del_list)
2640 			goto err_no_memory;
2641 
2642 		hlist_for_each_entry_safe(f, h, &tmp_del_list, hlist) {
2643 			cmd_flags = 0;
2644 
2645 			/* handle broadcast filters by updating the broadcast
2646 			 * promiscuous flag and release filter list.
2647 			 */
2648 			if (is_broadcast_ether_addr(f->macaddr)) {
2649 				i40e_aqc_broadcast_filter(vsi, vsi_name, f);
2650 
2651 				hlist_del(&f->hlist);
2652 				kfree(f);
2653 				continue;
2654 			}
2655 
2656 			/* add to delete list */
2657 			ether_addr_copy(del_list[num_del].mac_addr, f->macaddr);
2658 			if (f->vlan == I40E_VLAN_ANY) {
2659 				del_list[num_del].vlan_tag = 0;
2660 				cmd_flags |= I40E_AQC_MACVLAN_DEL_IGNORE_VLAN;
2661 			} else {
2662 				del_list[num_del].vlan_tag =
2663 					cpu_to_le16((u16)(f->vlan));
2664 			}
2665 
2666 			cmd_flags |= I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
2667 			del_list[num_del].flags = cmd_flags;
2668 			num_del++;
2669 
2670 			/* flush a full buffer */
2671 			if (num_del == filter_list_len) {
2672 				i40e_aqc_del_filters(vsi, vsi_name, del_list,
2673 						     num_del, &retval);
2674 				memset(del_list, 0, list_size);
2675 				num_del = 0;
2676 			}
2677 			/* Release memory for MAC filter entries which were
2678 			 * synced up with HW.
2679 			 */
2680 			hlist_del(&f->hlist);
2681 			kfree(f);
2682 		}
2683 
2684 		if (num_del) {
2685 			i40e_aqc_del_filters(vsi, vsi_name, del_list,
2686 					     num_del, &retval);
2687 		}
2688 
2689 		kfree(del_list);
2690 		del_list = NULL;
2691 	}
2692 
2693 	if (!hlist_empty(&tmp_add_list)) {
2694 		/* Do all the adds now. */
2695 		filter_list_len = hw->aq.asq_buf_size /
2696 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2697 		list_size = filter_list_len *
2698 			       sizeof(struct i40e_aqc_add_macvlan_element_data);
2699 		add_list = kzalloc(list_size, GFP_ATOMIC);
2700 		if (!add_list)
2701 			goto err_no_memory;
2702 
2703 		num_add = 0;
2704 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2705 			/* handle broadcast filters by updating the broadcast
2706 			 * promiscuous flag instead of adding a MAC filter.
2707 			 */
2708 			if (is_broadcast_ether_addr(new->f->macaddr)) {
2709 				if (i40e_aqc_broadcast_filter(vsi, vsi_name,
2710 							      new->f))
2711 					new->state = I40E_FILTER_FAILED;
2712 				else
2713 					new->state = I40E_FILTER_ACTIVE;
2714 				continue;
2715 			}
2716 
2717 			/* add to add array */
2718 			if (num_add == 0)
2719 				add_head = new;
2720 			cmd_flags = 0;
2721 			ether_addr_copy(add_list[num_add].mac_addr,
2722 					new->f->macaddr);
2723 			if (new->f->vlan == I40E_VLAN_ANY) {
2724 				add_list[num_add].vlan_tag = 0;
2725 				cmd_flags |= I40E_AQC_MACVLAN_ADD_IGNORE_VLAN;
2726 			} else {
2727 				add_list[num_add].vlan_tag =
2728 					cpu_to_le16((u16)(new->f->vlan));
2729 			}
2730 			add_list[num_add].queue_number = 0;
2731 			/* set invalid match method for later detection */
2732 			add_list[num_add].match_method = I40E_AQC_MM_ERR_NO_RES;
2733 			cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
2734 			add_list[num_add].flags = cpu_to_le16(cmd_flags);
2735 			num_add++;
2736 
2737 			/* flush a full buffer */
2738 			if (num_add == filter_list_len) {
2739 				i40e_aqc_add_filters(vsi, vsi_name, add_list,
2740 						     add_head, num_add);
2741 				memset(add_list, 0, list_size);
2742 				num_add = 0;
2743 			}
2744 		}
2745 		if (num_add) {
2746 			i40e_aqc_add_filters(vsi, vsi_name, add_list, add_head,
2747 					     num_add);
2748 		}
2749 		/* Now move all of the filters from the temp add list back to
2750 		 * the VSI's list.
2751 		 */
2752 		spin_lock_bh(&vsi->mac_filter_hash_lock);
2753 		hlist_for_each_entry_safe(new, h, &tmp_add_list, hlist) {
2754 			/* Only update the state if we're still NEW */
2755 			if (new->f->state == I40E_FILTER_NEW)
2756 				new->f->state = new->state;
2757 			hlist_del(&new->hlist);
2758 			netdev_hw_addr_refcnt(new->f, vsi->netdev, -1);
2759 			kfree(new);
2760 		}
2761 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
2762 		kfree(add_list);
2763 		add_list = NULL;
2764 	}
2765 
2766 	/* Determine the number of active and failed filters. */
2767 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2768 	vsi->active_filters = 0;
2769 	hash_for_each(vsi->mac_filter_hash, bkt, f, hlist) {
2770 		if (f->state == I40E_FILTER_ACTIVE)
2771 			vsi->active_filters++;
2772 		else if (f->state == I40E_FILTER_FAILED)
2773 			failed_filters++;
2774 	}
2775 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2776 
2777 	/* Check if we are able to exit overflow promiscuous mode. We can
2778 	 * safely exit if we didn't just enter, we no longer have any failed
2779 	 * filters, and we have reduced filters below the threshold value.
2780 	 */
2781 	if (old_overflow && !failed_filters &&
2782 	    vsi->active_filters < vsi->promisc_threshold) {
2783 		dev_info(&pf->pdev->dev,
2784 			 "filter logjam cleared on %s, leaving overflow promiscuous mode\n",
2785 			 vsi_name);
2786 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2787 		vsi->promisc_threshold = 0;
2788 	}
2789 
2790 	/* if the VF is not trusted do not do promisc */
2791 	if ((vsi->type == I40E_VSI_SRIOV) && !pf->vf[vsi->vf_id].trusted) {
2792 		clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2793 		goto out;
2794 	}
2795 
2796 	new_overflow = test_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
2797 
2798 	/* If we are entering overflow promiscuous, we need to calculate a new
2799 	 * threshold for when we are safe to exit
2800 	 */
2801 	if (!old_overflow && new_overflow)
2802 		vsi->promisc_threshold = (vsi->active_filters * 3) / 4;
2803 
2804 	/* check for changes in promiscuous modes */
2805 	if (changed_flags & IFF_ALLMULTI) {
2806 		bool cur_multipromisc;
2807 
2808 		cur_multipromisc = !!(vsi->current_netdev_flags & IFF_ALLMULTI);
2809 		aq_ret = i40e_aq_set_vsi_multicast_promiscuous(&vsi->back->hw,
2810 							       vsi->seid,
2811 							       cur_multipromisc,
2812 							       NULL);
2813 		if (aq_ret) {
2814 			retval = i40e_aq_rc_to_posix(aq_ret,
2815 						     hw->aq.asq_last_status);
2816 			dev_info(&pf->pdev->dev,
2817 				 "set multi promisc failed on %s, err %s aq_err %s\n",
2818 				 vsi_name,
2819 				 i40e_stat_str(hw, aq_ret),
2820 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2821 		} else {
2822 			dev_info(&pf->pdev->dev, "%s allmulti mode.\n",
2823 				 cur_multipromisc ? "entering" : "leaving");
2824 		}
2825 	}
2826 
2827 	if ((changed_flags & IFF_PROMISC) || old_overflow != new_overflow) {
2828 		bool cur_promisc;
2829 
2830 		cur_promisc = (!!(vsi->current_netdev_flags & IFF_PROMISC) ||
2831 			       new_overflow);
2832 		aq_ret = i40e_set_promiscuous(pf, cur_promisc);
2833 		if (aq_ret) {
2834 			retval = i40e_aq_rc_to_posix(aq_ret,
2835 						     hw->aq.asq_last_status);
2836 			dev_info(&pf->pdev->dev,
2837 				 "Setting promiscuous %s failed on %s, err %s aq_err %s\n",
2838 				 cur_promisc ? "on" : "off",
2839 				 vsi_name,
2840 				 i40e_stat_str(hw, aq_ret),
2841 				 i40e_aq_str(hw, hw->aq.asq_last_status));
2842 		}
2843 	}
2844 out:
2845 	/* if something went wrong then set the changed flag so we try again */
2846 	if (retval)
2847 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2848 
2849 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2850 	return retval;
2851 
2852 err_no_memory:
2853 	/* Restore elements on the temporary add and delete lists */
2854 	spin_lock_bh(&vsi->mac_filter_hash_lock);
2855 err_no_memory_locked:
2856 	i40e_undo_del_filter_entries(vsi, &tmp_del_list);
2857 	i40e_undo_add_filter_entries(vsi, &tmp_add_list);
2858 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
2859 
2860 	vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
2861 	clear_bit(__I40E_VSI_SYNCING_FILTERS, vsi->state);
2862 	return -ENOMEM;
2863 }
2864 
2865 /**
2866  * i40e_sync_filters_subtask - Sync the VSI filter list with HW
2867  * @pf: board private structure
2868  **/
2869 static void i40e_sync_filters_subtask(struct i40e_pf *pf)
2870 {
2871 	int v;
2872 
2873 	if (!pf)
2874 		return;
2875 	if (!test_and_clear_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state))
2876 		return;
2877 	if (test_bit(__I40E_VF_DISABLE, pf->state)) {
2878 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
2879 		return;
2880 	}
2881 
2882 	for (v = 0; v < pf->num_alloc_vsi; v++) {
2883 		if (pf->vsi[v] &&
2884 		    (pf->vsi[v]->flags & I40E_VSI_FLAG_FILTER_CHANGED) &&
2885 		    !test_bit(__I40E_VSI_RELEASING, pf->vsi[v]->state)) {
2886 			int ret = i40e_sync_vsi_filters(pf->vsi[v]);
2887 
2888 			if (ret) {
2889 				/* come back and try again later */
2890 				set_bit(__I40E_MACVLAN_SYNC_PENDING,
2891 					pf->state);
2892 				break;
2893 			}
2894 		}
2895 	}
2896 }
2897 
2898 /**
2899  * i40e_max_xdp_frame_size - returns the maximum allowed frame size for XDP
2900  * @vsi: the vsi
2901  **/
2902 static int i40e_max_xdp_frame_size(struct i40e_vsi *vsi)
2903 {
2904 	if (PAGE_SIZE >= 8192 || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
2905 		return I40E_RXBUFFER_2048;
2906 	else
2907 		return I40E_RXBUFFER_3072;
2908 }
2909 
2910 /**
2911  * i40e_change_mtu - NDO callback to change the Maximum Transfer Unit
2912  * @netdev: network interface device structure
2913  * @new_mtu: new value for maximum frame size
2914  *
2915  * Returns 0 on success, negative on failure
2916  **/
2917 static int i40e_change_mtu(struct net_device *netdev, int new_mtu)
2918 {
2919 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2920 	struct i40e_vsi *vsi = np->vsi;
2921 	struct i40e_pf *pf = vsi->back;
2922 
2923 	if (i40e_enabled_xdp_vsi(vsi)) {
2924 		int frame_size = new_mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
2925 
2926 		if (frame_size > i40e_max_xdp_frame_size(vsi))
2927 			return -EINVAL;
2928 	}
2929 
2930 	netdev_dbg(netdev, "changing MTU from %d to %d\n",
2931 		   netdev->mtu, new_mtu);
2932 	netdev->mtu = new_mtu;
2933 	if (netif_running(netdev))
2934 		i40e_vsi_reinit_locked(vsi);
2935 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
2936 	set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
2937 	return 0;
2938 }
2939 
2940 /**
2941  * i40e_ioctl - Access the hwtstamp interface
2942  * @netdev: network interface device structure
2943  * @ifr: interface request data
2944  * @cmd: ioctl command
2945  **/
2946 int i40e_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
2947 {
2948 	struct i40e_netdev_priv *np = netdev_priv(netdev);
2949 	struct i40e_pf *pf = np->vsi->back;
2950 
2951 	switch (cmd) {
2952 	case SIOCGHWTSTAMP:
2953 		return i40e_ptp_get_ts_config(pf, ifr);
2954 	case SIOCSHWTSTAMP:
2955 		return i40e_ptp_set_ts_config(pf, ifr);
2956 	default:
2957 		return -EOPNOTSUPP;
2958 	}
2959 }
2960 
2961 /**
2962  * i40e_vlan_stripping_enable - Turn on vlan stripping for the VSI
2963  * @vsi: the vsi being adjusted
2964  **/
2965 void i40e_vlan_stripping_enable(struct i40e_vsi *vsi)
2966 {
2967 	struct i40e_vsi_context ctxt;
2968 	i40e_status ret;
2969 
2970 	/* Don't modify stripping options if a port VLAN is active */
2971 	if (vsi->info.pvid)
2972 		return;
2973 
2974 	if ((vsi->info.valid_sections &
2975 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
2976 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_MODE_MASK) == 0))
2977 		return;  /* already enabled */
2978 
2979 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
2980 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
2981 				    I40E_AQ_VSI_PVLAN_EMOD_STR_BOTH;
2982 
2983 	ctxt.seid = vsi->seid;
2984 	ctxt.info = vsi->info;
2985 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
2986 	if (ret) {
2987 		dev_info(&vsi->back->pdev->dev,
2988 			 "update vlan stripping failed, err %s aq_err %s\n",
2989 			 i40e_stat_str(&vsi->back->hw, ret),
2990 			 i40e_aq_str(&vsi->back->hw,
2991 				     vsi->back->hw.aq.asq_last_status));
2992 	}
2993 }
2994 
2995 /**
2996  * i40e_vlan_stripping_disable - Turn off vlan stripping for the VSI
2997  * @vsi: the vsi being adjusted
2998  **/
2999 void i40e_vlan_stripping_disable(struct i40e_vsi *vsi)
3000 {
3001 	struct i40e_vsi_context ctxt;
3002 	i40e_status ret;
3003 
3004 	/* Don't modify stripping options if a port VLAN is active */
3005 	if (vsi->info.pvid)
3006 		return;
3007 
3008 	if ((vsi->info.valid_sections &
3009 	     cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID)) &&
3010 	    ((vsi->info.port_vlan_flags & I40E_AQ_VSI_PVLAN_EMOD_MASK) ==
3011 	     I40E_AQ_VSI_PVLAN_EMOD_MASK))
3012 		return;  /* already disabled */
3013 
3014 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3015 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_ALL |
3016 				    I40E_AQ_VSI_PVLAN_EMOD_NOTHING;
3017 
3018 	ctxt.seid = vsi->seid;
3019 	ctxt.info = vsi->info;
3020 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3021 	if (ret) {
3022 		dev_info(&vsi->back->pdev->dev,
3023 			 "update vlan stripping failed, err %s aq_err %s\n",
3024 			 i40e_stat_str(&vsi->back->hw, ret),
3025 			 i40e_aq_str(&vsi->back->hw,
3026 				     vsi->back->hw.aq.asq_last_status));
3027 	}
3028 }
3029 
3030 /**
3031  * i40e_add_vlan_all_mac - Add a MAC/VLAN filter for each existing MAC address
3032  * @vsi: the vsi being configured
3033  * @vid: vlan id to be added (0 = untagged only , -1 = any)
3034  *
3035  * This is a helper function for adding a new MAC/VLAN filter with the
3036  * specified VLAN for each existing MAC address already in the hash table.
3037  * This function does *not* perform any accounting to update filters based on
3038  * VLAN mode.
3039  *
3040  * NOTE: this function expects to be called while under the
3041  * mac_filter_hash_lock
3042  **/
3043 int i40e_add_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3044 {
3045 	struct i40e_mac_filter *f, *add_f;
3046 	struct hlist_node *h;
3047 	int bkt;
3048 
3049 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3050 		/* If we're asked to add a filter that has been marked for
3051 		 * removal, it is safe to simply restore it to active state.
3052 		 * __i40e_del_filter will have simply deleted any filters which
3053 		 * were previously marked NEW or FAILED, so if it is currently
3054 		 * marked REMOVE it must have previously been ACTIVE. Since we
3055 		 * haven't yet run the sync filters task, just restore this
3056 		 * filter to the ACTIVE state so that the sync task leaves it
3057 		 * in place.
3058 		 */
3059 		if (f->state == I40E_FILTER_REMOVE && f->vlan == vid) {
3060 			f->state = I40E_FILTER_ACTIVE;
3061 			continue;
3062 		} else if (f->state == I40E_FILTER_REMOVE) {
3063 			continue;
3064 		}
3065 		add_f = i40e_add_filter(vsi, f->macaddr, vid);
3066 		if (!add_f) {
3067 			dev_info(&vsi->back->pdev->dev,
3068 				 "Could not add vlan filter %d for %pM\n",
3069 				 vid, f->macaddr);
3070 			return -ENOMEM;
3071 		}
3072 	}
3073 
3074 	return 0;
3075 }
3076 
3077 /**
3078  * i40e_vsi_add_vlan - Add VSI membership for given VLAN
3079  * @vsi: the VSI being configured
3080  * @vid: VLAN id to be added
3081  **/
3082 int i40e_vsi_add_vlan(struct i40e_vsi *vsi, u16 vid)
3083 {
3084 	int err;
3085 
3086 	if (vsi->info.pvid)
3087 		return -EINVAL;
3088 
3089 	/* The network stack will attempt to add VID=0, with the intention to
3090 	 * receive priority tagged packets with a VLAN of 0. Our HW receives
3091 	 * these packets by default when configured to receive untagged
3092 	 * packets, so we don't need to add a filter for this case.
3093 	 * Additionally, HW interprets adding a VID=0 filter as meaning to
3094 	 * receive *only* tagged traffic and stops receiving untagged traffic.
3095 	 * Thus, we do not want to actually add a filter for VID=0
3096 	 */
3097 	if (!vid)
3098 		return 0;
3099 
3100 	/* Locked once because all functions invoked below iterates list*/
3101 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3102 	err = i40e_add_vlan_all_mac(vsi, vid);
3103 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3104 	if (err)
3105 		return err;
3106 
3107 	/* schedule our worker thread which will take care of
3108 	 * applying the new filter changes
3109 	 */
3110 	i40e_service_event_schedule(vsi->back);
3111 	return 0;
3112 }
3113 
3114 /**
3115  * i40e_rm_vlan_all_mac - Remove MAC/VLAN pair for all MAC with the given VLAN
3116  * @vsi: the vsi being configured
3117  * @vid: vlan id to be removed (0 = untagged only , -1 = any)
3118  *
3119  * This function should be used to remove all VLAN filters which match the
3120  * given VID. It does not schedule the service event and does not take the
3121  * mac_filter_hash_lock so it may be combined with other operations under
3122  * a single invocation of the mac_filter_hash_lock.
3123  *
3124  * NOTE: this function expects to be called while under the
3125  * mac_filter_hash_lock
3126  */
3127 void i40e_rm_vlan_all_mac(struct i40e_vsi *vsi, s16 vid)
3128 {
3129 	struct i40e_mac_filter *f;
3130 	struct hlist_node *h;
3131 	int bkt;
3132 
3133 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
3134 		if (f->vlan == vid)
3135 			__i40e_del_filter(vsi, f);
3136 	}
3137 }
3138 
3139 /**
3140  * i40e_vsi_kill_vlan - Remove VSI membership for given VLAN
3141  * @vsi: the VSI being configured
3142  * @vid: VLAN id to be removed
3143  **/
3144 void i40e_vsi_kill_vlan(struct i40e_vsi *vsi, u16 vid)
3145 {
3146 	if (!vid || vsi->info.pvid)
3147 		return;
3148 
3149 	spin_lock_bh(&vsi->mac_filter_hash_lock);
3150 	i40e_rm_vlan_all_mac(vsi, vid);
3151 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
3152 
3153 	/* schedule our worker thread which will take care of
3154 	 * applying the new filter changes
3155 	 */
3156 	i40e_service_event_schedule(vsi->back);
3157 }
3158 
3159 /**
3160  * i40e_vlan_rx_add_vid - Add a vlan id filter to HW offload
3161  * @netdev: network interface to be adjusted
3162  * @proto: unused protocol value
3163  * @vid: vlan id to be added
3164  *
3165  * net_device_ops implementation for adding vlan ids
3166  **/
3167 static int i40e_vlan_rx_add_vid(struct net_device *netdev,
3168 				__always_unused __be16 proto, u16 vid)
3169 {
3170 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3171 	struct i40e_vsi *vsi = np->vsi;
3172 	int ret = 0;
3173 
3174 	if (vid >= VLAN_N_VID)
3175 		return -EINVAL;
3176 
3177 	ret = i40e_vsi_add_vlan(vsi, vid);
3178 	if (!ret)
3179 		set_bit(vid, vsi->active_vlans);
3180 
3181 	return ret;
3182 }
3183 
3184 /**
3185  * i40e_vlan_rx_add_vid_up - Add a vlan id filter to HW offload in UP path
3186  * @netdev: network interface to be adjusted
3187  * @proto: unused protocol value
3188  * @vid: vlan id to be added
3189  **/
3190 static void i40e_vlan_rx_add_vid_up(struct net_device *netdev,
3191 				    __always_unused __be16 proto, u16 vid)
3192 {
3193 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3194 	struct i40e_vsi *vsi = np->vsi;
3195 
3196 	if (vid >= VLAN_N_VID)
3197 		return;
3198 	set_bit(vid, vsi->active_vlans);
3199 }
3200 
3201 /**
3202  * i40e_vlan_rx_kill_vid - Remove a vlan id filter from HW offload
3203  * @netdev: network interface to be adjusted
3204  * @proto: unused protocol value
3205  * @vid: vlan id to be removed
3206  *
3207  * net_device_ops implementation for removing vlan ids
3208  **/
3209 static int i40e_vlan_rx_kill_vid(struct net_device *netdev,
3210 				 __always_unused __be16 proto, u16 vid)
3211 {
3212 	struct i40e_netdev_priv *np = netdev_priv(netdev);
3213 	struct i40e_vsi *vsi = np->vsi;
3214 
3215 	/* return code is ignored as there is nothing a user
3216 	 * can do about failure to remove and a log message was
3217 	 * already printed from the other function
3218 	 */
3219 	i40e_vsi_kill_vlan(vsi, vid);
3220 
3221 	clear_bit(vid, vsi->active_vlans);
3222 
3223 	return 0;
3224 }
3225 
3226 /**
3227  * i40e_restore_vlan - Reinstate vlans when vsi/netdev comes back up
3228  * @vsi: the vsi being brought back up
3229  **/
3230 static void i40e_restore_vlan(struct i40e_vsi *vsi)
3231 {
3232 	u16 vid;
3233 
3234 	if (!vsi->netdev)
3235 		return;
3236 
3237 	if (vsi->netdev->features & NETIF_F_HW_VLAN_CTAG_RX)
3238 		i40e_vlan_stripping_enable(vsi);
3239 	else
3240 		i40e_vlan_stripping_disable(vsi);
3241 
3242 	for_each_set_bit(vid, vsi->active_vlans, VLAN_N_VID)
3243 		i40e_vlan_rx_add_vid_up(vsi->netdev, htons(ETH_P_8021Q),
3244 					vid);
3245 }
3246 
3247 /**
3248  * i40e_vsi_add_pvid - Add pvid for the VSI
3249  * @vsi: the vsi being adjusted
3250  * @vid: the vlan id to set as a PVID
3251  **/
3252 int i40e_vsi_add_pvid(struct i40e_vsi *vsi, u16 vid)
3253 {
3254 	struct i40e_vsi_context ctxt;
3255 	i40e_status ret;
3256 
3257 	vsi->info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
3258 	vsi->info.pvid = cpu_to_le16(vid);
3259 	vsi->info.port_vlan_flags = I40E_AQ_VSI_PVLAN_MODE_TAGGED |
3260 				    I40E_AQ_VSI_PVLAN_INSERT_PVID |
3261 				    I40E_AQ_VSI_PVLAN_EMOD_STR;
3262 
3263 	ctxt.seid = vsi->seid;
3264 	ctxt.info = vsi->info;
3265 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
3266 	if (ret) {
3267 		dev_info(&vsi->back->pdev->dev,
3268 			 "add pvid failed, err %s aq_err %s\n",
3269 			 i40e_stat_str(&vsi->back->hw, ret),
3270 			 i40e_aq_str(&vsi->back->hw,
3271 				     vsi->back->hw.aq.asq_last_status));
3272 		return -ENOENT;
3273 	}
3274 
3275 	return 0;
3276 }
3277 
3278 /**
3279  * i40e_vsi_remove_pvid - Remove the pvid from the VSI
3280  * @vsi: the vsi being adjusted
3281  *
3282  * Just use the vlan_rx_register() service to put it back to normal
3283  **/
3284 void i40e_vsi_remove_pvid(struct i40e_vsi *vsi)
3285 {
3286 	vsi->info.pvid = 0;
3287 
3288 	i40e_vlan_stripping_disable(vsi);
3289 }
3290 
3291 /**
3292  * i40e_vsi_setup_tx_resources - Allocate VSI Tx queue resources
3293  * @vsi: ptr to the VSI
3294  *
3295  * If this function returns with an error, then it's possible one or
3296  * more of the rings is populated (while the rest are not).  It is the
3297  * callers duty to clean those orphaned rings.
3298  *
3299  * Return 0 on success, negative on failure
3300  **/
3301 static int i40e_vsi_setup_tx_resources(struct i40e_vsi *vsi)
3302 {
3303 	int i, err = 0;
3304 
3305 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3306 		err = i40e_setup_tx_descriptors(vsi->tx_rings[i]);
3307 
3308 	if (!i40e_enabled_xdp_vsi(vsi))
3309 		return err;
3310 
3311 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3312 		err = i40e_setup_tx_descriptors(vsi->xdp_rings[i]);
3313 
3314 	return err;
3315 }
3316 
3317 /**
3318  * i40e_vsi_free_tx_resources - Free Tx resources for VSI queues
3319  * @vsi: ptr to the VSI
3320  *
3321  * Free VSI's transmit software resources
3322  **/
3323 static void i40e_vsi_free_tx_resources(struct i40e_vsi *vsi)
3324 {
3325 	int i;
3326 
3327 	if (vsi->tx_rings) {
3328 		for (i = 0; i < vsi->num_queue_pairs; i++)
3329 			if (vsi->tx_rings[i] && vsi->tx_rings[i]->desc)
3330 				i40e_free_tx_resources(vsi->tx_rings[i]);
3331 	}
3332 
3333 	if (vsi->xdp_rings) {
3334 		for (i = 0; i < vsi->num_queue_pairs; i++)
3335 			if (vsi->xdp_rings[i] && vsi->xdp_rings[i]->desc)
3336 				i40e_free_tx_resources(vsi->xdp_rings[i]);
3337 	}
3338 }
3339 
3340 /**
3341  * i40e_vsi_setup_rx_resources - Allocate VSI queues Rx resources
3342  * @vsi: ptr to the VSI
3343  *
3344  * If this function returns with an error, then it's possible one or
3345  * more of the rings is populated (while the rest are not).  It is the
3346  * callers duty to clean those orphaned rings.
3347  *
3348  * Return 0 on success, negative on failure
3349  **/
3350 static int i40e_vsi_setup_rx_resources(struct i40e_vsi *vsi)
3351 {
3352 	int i, err = 0;
3353 
3354 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3355 		err = i40e_setup_rx_descriptors(vsi->rx_rings[i]);
3356 	return err;
3357 }
3358 
3359 /**
3360  * i40e_vsi_free_rx_resources - Free Rx Resources for VSI queues
3361  * @vsi: ptr to the VSI
3362  *
3363  * Free all receive software resources
3364  **/
3365 static void i40e_vsi_free_rx_resources(struct i40e_vsi *vsi)
3366 {
3367 	int i;
3368 
3369 	if (!vsi->rx_rings)
3370 		return;
3371 
3372 	for (i = 0; i < vsi->num_queue_pairs; i++)
3373 		if (vsi->rx_rings[i] && vsi->rx_rings[i]->desc)
3374 			i40e_free_rx_resources(vsi->rx_rings[i]);
3375 }
3376 
3377 /**
3378  * i40e_config_xps_tx_ring - Configure XPS for a Tx ring
3379  * @ring: The Tx ring to configure
3380  *
3381  * This enables/disables XPS for a given Tx descriptor ring
3382  * based on the TCs enabled for the VSI that ring belongs to.
3383  **/
3384 static void i40e_config_xps_tx_ring(struct i40e_ring *ring)
3385 {
3386 	int cpu;
3387 
3388 	if (!ring->q_vector || !ring->netdev || ring->ch)
3389 		return;
3390 
3391 	/* We only initialize XPS once, so as not to overwrite user settings */
3392 	if (test_and_set_bit(__I40E_TX_XPS_INIT_DONE, ring->state))
3393 		return;
3394 
3395 	cpu = cpumask_local_spread(ring->q_vector->v_idx, -1);
3396 	netif_set_xps_queue(ring->netdev, get_cpu_mask(cpu),
3397 			    ring->queue_index);
3398 }
3399 
3400 /**
3401  * i40e_xsk_pool - Retrieve the AF_XDP buffer pool if XDP and ZC is enabled
3402  * @ring: The Tx or Rx ring
3403  *
3404  * Returns the AF_XDP buffer pool or NULL.
3405  **/
3406 static struct xsk_buff_pool *i40e_xsk_pool(struct i40e_ring *ring)
3407 {
3408 	bool xdp_on = i40e_enabled_xdp_vsi(ring->vsi);
3409 	int qid = ring->queue_index;
3410 
3411 	if (ring_is_xdp(ring))
3412 		qid -= ring->vsi->alloc_queue_pairs;
3413 
3414 	if (!xdp_on || !test_bit(qid, ring->vsi->af_xdp_zc_qps))
3415 		return NULL;
3416 
3417 	return xsk_get_pool_from_qid(ring->vsi->netdev, qid);
3418 }
3419 
3420 /**
3421  * i40e_configure_tx_ring - Configure a transmit ring context and rest
3422  * @ring: The Tx ring to configure
3423  *
3424  * Configure the Tx descriptor ring in the HMC context.
3425  **/
3426 static int i40e_configure_tx_ring(struct i40e_ring *ring)
3427 {
3428 	struct i40e_vsi *vsi = ring->vsi;
3429 	u16 pf_q = vsi->base_queue + ring->queue_index;
3430 	struct i40e_hw *hw = &vsi->back->hw;
3431 	struct i40e_hmc_obj_txq tx_ctx;
3432 	i40e_status err = 0;
3433 	u32 qtx_ctl = 0;
3434 
3435 	if (ring_is_xdp(ring))
3436 		ring->xsk_pool = i40e_xsk_pool(ring);
3437 
3438 	/* some ATR related tx ring init */
3439 	if (vsi->back->flags & I40E_FLAG_FD_ATR_ENABLED) {
3440 		ring->atr_sample_rate = vsi->back->atr_sample_rate;
3441 		ring->atr_count = 0;
3442 	} else {
3443 		ring->atr_sample_rate = 0;
3444 	}
3445 
3446 	/* configure XPS */
3447 	i40e_config_xps_tx_ring(ring);
3448 
3449 	/* clear the context structure first */
3450 	memset(&tx_ctx, 0, sizeof(tx_ctx));
3451 
3452 	tx_ctx.new_context = 1;
3453 	tx_ctx.base = (ring->dma / 128);
3454 	tx_ctx.qlen = ring->count;
3455 	tx_ctx.fd_ena = !!(vsi->back->flags & (I40E_FLAG_FD_SB_ENABLED |
3456 					       I40E_FLAG_FD_ATR_ENABLED));
3457 	tx_ctx.timesync_ena = !!(vsi->back->flags & I40E_FLAG_PTP);
3458 	/* FDIR VSI tx ring can still use RS bit and writebacks */
3459 	if (vsi->type != I40E_VSI_FDIR)
3460 		tx_ctx.head_wb_ena = 1;
3461 	tx_ctx.head_wb_addr = ring->dma +
3462 			      (ring->count * sizeof(struct i40e_tx_desc));
3463 
3464 	/* As part of VSI creation/update, FW allocates certain
3465 	 * Tx arbitration queue sets for each TC enabled for
3466 	 * the VSI. The FW returns the handles to these queue
3467 	 * sets as part of the response buffer to Add VSI,
3468 	 * Update VSI, etc. AQ commands. It is expected that
3469 	 * these queue set handles be associated with the Tx
3470 	 * queues by the driver as part of the TX queue context
3471 	 * initialization. This has to be done regardless of
3472 	 * DCB as by default everything is mapped to TC0.
3473 	 */
3474 
3475 	if (ring->ch)
3476 		tx_ctx.rdylist =
3477 			le16_to_cpu(ring->ch->info.qs_handle[ring->dcb_tc]);
3478 
3479 	else
3480 		tx_ctx.rdylist = le16_to_cpu(vsi->info.qs_handle[ring->dcb_tc]);
3481 
3482 	tx_ctx.rdylist_act = 0;
3483 
3484 	/* clear the context in the HMC */
3485 	err = i40e_clear_lan_tx_queue_context(hw, pf_q);
3486 	if (err) {
3487 		dev_info(&vsi->back->pdev->dev,
3488 			 "Failed to clear LAN Tx queue context on Tx ring %d (pf_q %d), error: %d\n",
3489 			 ring->queue_index, pf_q, err);
3490 		return -ENOMEM;
3491 	}
3492 
3493 	/* set the context in the HMC */
3494 	err = i40e_set_lan_tx_queue_context(hw, pf_q, &tx_ctx);
3495 	if (err) {
3496 		dev_info(&vsi->back->pdev->dev,
3497 			 "Failed to set LAN Tx queue context on Tx ring %d (pf_q %d, error: %d\n",
3498 			 ring->queue_index, pf_q, err);
3499 		return -ENOMEM;
3500 	}
3501 
3502 	/* Now associate this queue with this PCI function */
3503 	if (ring->ch) {
3504 		if (ring->ch->type == I40E_VSI_VMDQ2)
3505 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3506 		else
3507 			return -EINVAL;
3508 
3509 		qtx_ctl |= (ring->ch->vsi_number <<
3510 			    I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3511 			    I40E_QTX_CTL_VFVM_INDX_MASK;
3512 	} else {
3513 		if (vsi->type == I40E_VSI_VMDQ2) {
3514 			qtx_ctl = I40E_QTX_CTL_VM_QUEUE;
3515 			qtx_ctl |= ((vsi->id) << I40E_QTX_CTL_VFVM_INDX_SHIFT) &
3516 				    I40E_QTX_CTL_VFVM_INDX_MASK;
3517 		} else {
3518 			qtx_ctl = I40E_QTX_CTL_PF_QUEUE;
3519 		}
3520 	}
3521 
3522 	qtx_ctl |= ((hw->pf_id << I40E_QTX_CTL_PF_INDX_SHIFT) &
3523 		    I40E_QTX_CTL_PF_INDX_MASK);
3524 	wr32(hw, I40E_QTX_CTL(pf_q), qtx_ctl);
3525 	i40e_flush(hw);
3526 
3527 	/* cache tail off for easier writes later */
3528 	ring->tail = hw->hw_addr + I40E_QTX_TAIL(pf_q);
3529 
3530 	return 0;
3531 }
3532 
3533 /**
3534  * i40e_rx_offset - Return expected offset into page to access data
3535  * @rx_ring: Ring we are requesting offset of
3536  *
3537  * Returns the offset value for ring into the data buffer.
3538  */
3539 static unsigned int i40e_rx_offset(struct i40e_ring *rx_ring)
3540 {
3541 	return ring_uses_build_skb(rx_ring) ? I40E_SKB_PAD : 0;
3542 }
3543 
3544 /**
3545  * i40e_configure_rx_ring - Configure a receive ring context
3546  * @ring: The Rx ring to configure
3547  *
3548  * Configure the Rx descriptor ring in the HMC context.
3549  **/
3550 static int i40e_configure_rx_ring(struct i40e_ring *ring)
3551 {
3552 	struct i40e_vsi *vsi = ring->vsi;
3553 	u32 chain_len = vsi->back->hw.func_caps.rx_buf_chain_len;
3554 	u16 pf_q = vsi->base_queue + ring->queue_index;
3555 	struct i40e_hw *hw = &vsi->back->hw;
3556 	struct i40e_hmc_obj_rxq rx_ctx;
3557 	i40e_status err = 0;
3558 	bool ok;
3559 	int ret;
3560 
3561 	bitmap_zero(ring->state, __I40E_RING_STATE_NBITS);
3562 
3563 	/* clear the context structure first */
3564 	memset(&rx_ctx, 0, sizeof(rx_ctx));
3565 
3566 	if (ring->vsi->type == I40E_VSI_MAIN)
3567 		xdp_rxq_info_unreg_mem_model(&ring->xdp_rxq);
3568 
3569 	ring->xsk_pool = i40e_xsk_pool(ring);
3570 	if (ring->xsk_pool) {
3571 		ring->rx_buf_len =
3572 		  xsk_pool_get_rx_frame_size(ring->xsk_pool);
3573 		/* For AF_XDP ZC, we disallow packets to span on
3574 		 * multiple buffers, thus letting us skip that
3575 		 * handling in the fast-path.
3576 		 */
3577 		chain_len = 1;
3578 		ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3579 						 MEM_TYPE_XSK_BUFF_POOL,
3580 						 NULL);
3581 		if (ret)
3582 			return ret;
3583 		dev_info(&vsi->back->pdev->dev,
3584 			 "Registered XDP mem model MEM_TYPE_XSK_BUFF_POOL on Rx ring %d\n",
3585 			 ring->queue_index);
3586 
3587 	} else {
3588 		ring->rx_buf_len = vsi->rx_buf_len;
3589 		if (ring->vsi->type == I40E_VSI_MAIN) {
3590 			ret = xdp_rxq_info_reg_mem_model(&ring->xdp_rxq,
3591 							 MEM_TYPE_PAGE_SHARED,
3592 							 NULL);
3593 			if (ret)
3594 				return ret;
3595 		}
3596 	}
3597 
3598 	rx_ctx.dbuff = DIV_ROUND_UP(ring->rx_buf_len,
3599 				    BIT_ULL(I40E_RXQ_CTX_DBUFF_SHIFT));
3600 
3601 	rx_ctx.base = (ring->dma / 128);
3602 	rx_ctx.qlen = ring->count;
3603 
3604 	/* use 16 byte descriptors */
3605 	rx_ctx.dsize = 0;
3606 
3607 	/* descriptor type is always zero
3608 	 * rx_ctx.dtype = 0;
3609 	 */
3610 	rx_ctx.hsplit_0 = 0;
3611 
3612 	rx_ctx.rxmax = min_t(u16, vsi->max_frame, chain_len * ring->rx_buf_len);
3613 	if (hw->revision_id == 0)
3614 		rx_ctx.lrxqthresh = 0;
3615 	else
3616 		rx_ctx.lrxqthresh = 1;
3617 	rx_ctx.crcstrip = 1;
3618 	rx_ctx.l2tsel = 1;
3619 	/* this controls whether VLAN is stripped from inner headers */
3620 	rx_ctx.showiv = 0;
3621 	/* set the prefena field to 1 because the manual says to */
3622 	rx_ctx.prefena = 1;
3623 
3624 	/* clear the context in the HMC */
3625 	err = i40e_clear_lan_rx_queue_context(hw, pf_q);
3626 	if (err) {
3627 		dev_info(&vsi->back->pdev->dev,
3628 			 "Failed to clear LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3629 			 ring->queue_index, pf_q, err);
3630 		return -ENOMEM;
3631 	}
3632 
3633 	/* set the context in the HMC */
3634 	err = i40e_set_lan_rx_queue_context(hw, pf_q, &rx_ctx);
3635 	if (err) {
3636 		dev_info(&vsi->back->pdev->dev,
3637 			 "Failed to set LAN Rx queue context on Rx ring %d (pf_q %d), error: %d\n",
3638 			 ring->queue_index, pf_q, err);
3639 		return -ENOMEM;
3640 	}
3641 
3642 	/* configure Rx buffer alignment */
3643 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX))
3644 		clear_ring_build_skb_enabled(ring);
3645 	else
3646 		set_ring_build_skb_enabled(ring);
3647 
3648 	ring->rx_offset = i40e_rx_offset(ring);
3649 
3650 	/* cache tail for quicker writes, and clear the reg before use */
3651 	ring->tail = hw->hw_addr + I40E_QRX_TAIL(pf_q);
3652 	writel(0, ring->tail);
3653 
3654 	if (ring->xsk_pool) {
3655 		xsk_pool_set_rxq_info(ring->xsk_pool, &ring->xdp_rxq);
3656 		ok = i40e_alloc_rx_buffers_zc(ring, I40E_DESC_UNUSED(ring));
3657 	} else {
3658 		ok = !i40e_alloc_rx_buffers(ring, I40E_DESC_UNUSED(ring));
3659 	}
3660 	if (!ok) {
3661 		/* Log this in case the user has forgotten to give the kernel
3662 		 * any buffers, even later in the application.
3663 		 */
3664 		dev_info(&vsi->back->pdev->dev,
3665 			 "Failed to allocate some buffers on %sRx ring %d (pf_q %d)\n",
3666 			 ring->xsk_pool ? "AF_XDP ZC enabled " : "",
3667 			 ring->queue_index, pf_q);
3668 	}
3669 
3670 	return 0;
3671 }
3672 
3673 /**
3674  * i40e_vsi_configure_tx - Configure the VSI for Tx
3675  * @vsi: VSI structure describing this set of rings and resources
3676  *
3677  * Configure the Tx VSI for operation.
3678  **/
3679 static int i40e_vsi_configure_tx(struct i40e_vsi *vsi)
3680 {
3681 	int err = 0;
3682 	u16 i;
3683 
3684 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3685 		err = i40e_configure_tx_ring(vsi->tx_rings[i]);
3686 
3687 	if (err || !i40e_enabled_xdp_vsi(vsi))
3688 		return err;
3689 
3690 	for (i = 0; (i < vsi->num_queue_pairs) && !err; i++)
3691 		err = i40e_configure_tx_ring(vsi->xdp_rings[i]);
3692 
3693 	return err;
3694 }
3695 
3696 /**
3697  * i40e_vsi_configure_rx - Configure the VSI for Rx
3698  * @vsi: the VSI being configured
3699  *
3700  * Configure the Rx VSI for operation.
3701  **/
3702 static int i40e_vsi_configure_rx(struct i40e_vsi *vsi)
3703 {
3704 	int err = 0;
3705 	u16 i;
3706 
3707 	if (!vsi->netdev || (vsi->back->flags & I40E_FLAG_LEGACY_RX)) {
3708 		vsi->max_frame = I40E_MAX_RXBUFFER;
3709 		vsi->rx_buf_len = I40E_RXBUFFER_2048;
3710 #if (PAGE_SIZE < 8192)
3711 	} else if (!I40E_2K_TOO_SMALL_WITH_PADDING &&
3712 		   (vsi->netdev->mtu <= ETH_DATA_LEN)) {
3713 		vsi->max_frame = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3714 		vsi->rx_buf_len = I40E_RXBUFFER_1536 - NET_IP_ALIGN;
3715 #endif
3716 	} else {
3717 		vsi->max_frame = I40E_MAX_RXBUFFER;
3718 		vsi->rx_buf_len = (PAGE_SIZE < 8192) ? I40E_RXBUFFER_3072 :
3719 						       I40E_RXBUFFER_2048;
3720 	}
3721 
3722 	/* set up individual rings */
3723 	for (i = 0; i < vsi->num_queue_pairs && !err; i++)
3724 		err = i40e_configure_rx_ring(vsi->rx_rings[i]);
3725 
3726 	return err;
3727 }
3728 
3729 /**
3730  * i40e_vsi_config_dcb_rings - Update rings to reflect DCB TC
3731  * @vsi: ptr to the VSI
3732  **/
3733 static void i40e_vsi_config_dcb_rings(struct i40e_vsi *vsi)
3734 {
3735 	struct i40e_ring *tx_ring, *rx_ring;
3736 	u16 qoffset, qcount;
3737 	int i, n;
3738 
3739 	if (!(vsi->back->flags & I40E_FLAG_DCB_ENABLED)) {
3740 		/* Reset the TC information */
3741 		for (i = 0; i < vsi->num_queue_pairs; i++) {
3742 			rx_ring = vsi->rx_rings[i];
3743 			tx_ring = vsi->tx_rings[i];
3744 			rx_ring->dcb_tc = 0;
3745 			tx_ring->dcb_tc = 0;
3746 		}
3747 		return;
3748 	}
3749 
3750 	for (n = 0; n < I40E_MAX_TRAFFIC_CLASS; n++) {
3751 		if (!(vsi->tc_config.enabled_tc & BIT_ULL(n)))
3752 			continue;
3753 
3754 		qoffset = vsi->tc_config.tc_info[n].qoffset;
3755 		qcount = vsi->tc_config.tc_info[n].qcount;
3756 		for (i = qoffset; i < (qoffset + qcount); i++) {
3757 			rx_ring = vsi->rx_rings[i];
3758 			tx_ring = vsi->tx_rings[i];
3759 			rx_ring->dcb_tc = n;
3760 			tx_ring->dcb_tc = n;
3761 		}
3762 	}
3763 }
3764 
3765 /**
3766  * i40e_set_vsi_rx_mode - Call set_rx_mode on a VSI
3767  * @vsi: ptr to the VSI
3768  **/
3769 static void i40e_set_vsi_rx_mode(struct i40e_vsi *vsi)
3770 {
3771 	if (vsi->netdev)
3772 		i40e_set_rx_mode(vsi->netdev);
3773 }
3774 
3775 /**
3776  * i40e_reset_fdir_filter_cnt - Reset flow director filter counters
3777  * @pf: Pointer to the targeted PF
3778  *
3779  * Set all flow director counters to 0.
3780  */
3781 static void i40e_reset_fdir_filter_cnt(struct i40e_pf *pf)
3782 {
3783 	pf->fd_tcp4_filter_cnt = 0;
3784 	pf->fd_udp4_filter_cnt = 0;
3785 	pf->fd_sctp4_filter_cnt = 0;
3786 	pf->fd_ip4_filter_cnt = 0;
3787 	pf->fd_tcp6_filter_cnt = 0;
3788 	pf->fd_udp6_filter_cnt = 0;
3789 	pf->fd_sctp6_filter_cnt = 0;
3790 	pf->fd_ip6_filter_cnt = 0;
3791 }
3792 
3793 /**
3794  * i40e_fdir_filter_restore - Restore the Sideband Flow Director filters
3795  * @vsi: Pointer to the targeted VSI
3796  *
3797  * This function replays the hlist on the hw where all the SB Flow Director
3798  * filters were saved.
3799  **/
3800 static void i40e_fdir_filter_restore(struct i40e_vsi *vsi)
3801 {
3802 	struct i40e_fdir_filter *filter;
3803 	struct i40e_pf *pf = vsi->back;
3804 	struct hlist_node *node;
3805 
3806 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
3807 		return;
3808 
3809 	/* Reset FDir counters as we're replaying all existing filters */
3810 	i40e_reset_fdir_filter_cnt(pf);
3811 
3812 	hlist_for_each_entry_safe(filter, node,
3813 				  &pf->fdir_filter_list, fdir_node) {
3814 		i40e_add_del_fdir(vsi, filter, true);
3815 	}
3816 }
3817 
3818 /**
3819  * i40e_vsi_configure - Set up the VSI for action
3820  * @vsi: the VSI being configured
3821  **/
3822 static int i40e_vsi_configure(struct i40e_vsi *vsi)
3823 {
3824 	int err;
3825 
3826 	i40e_set_vsi_rx_mode(vsi);
3827 	i40e_restore_vlan(vsi);
3828 	i40e_vsi_config_dcb_rings(vsi);
3829 	err = i40e_vsi_configure_tx(vsi);
3830 	if (!err)
3831 		err = i40e_vsi_configure_rx(vsi);
3832 
3833 	return err;
3834 }
3835 
3836 /**
3837  * i40e_vsi_configure_msix - MSIX mode Interrupt Config in the HW
3838  * @vsi: the VSI being configured
3839  **/
3840 static void i40e_vsi_configure_msix(struct i40e_vsi *vsi)
3841 {
3842 	bool has_xdp = i40e_enabled_xdp_vsi(vsi);
3843 	struct i40e_pf *pf = vsi->back;
3844 	struct i40e_hw *hw = &pf->hw;
3845 	u16 vector;
3846 	int i, q;
3847 	u32 qp;
3848 
3849 	/* The interrupt indexing is offset by 1 in the PFINT_ITRn
3850 	 * and PFINT_LNKLSTn registers, e.g.:
3851 	 *   PFINT_ITRn[0..n-1] gets msix-1..msix-n  (qpair interrupts)
3852 	 */
3853 	qp = vsi->base_queue;
3854 	vector = vsi->base_vector;
3855 	for (i = 0; i < vsi->num_q_vectors; i++, vector++) {
3856 		struct i40e_q_vector *q_vector = vsi->q_vectors[i];
3857 
3858 		q_vector->rx.next_update = jiffies + 1;
3859 		q_vector->rx.target_itr =
3860 			ITR_TO_REG(vsi->rx_rings[i]->itr_setting);
3861 		wr32(hw, I40E_PFINT_ITRN(I40E_RX_ITR, vector - 1),
3862 		     q_vector->rx.target_itr >> 1);
3863 		q_vector->rx.current_itr = q_vector->rx.target_itr;
3864 
3865 		q_vector->tx.next_update = jiffies + 1;
3866 		q_vector->tx.target_itr =
3867 			ITR_TO_REG(vsi->tx_rings[i]->itr_setting);
3868 		wr32(hw, I40E_PFINT_ITRN(I40E_TX_ITR, vector - 1),
3869 		     q_vector->tx.target_itr >> 1);
3870 		q_vector->tx.current_itr = q_vector->tx.target_itr;
3871 
3872 		wr32(hw, I40E_PFINT_RATEN(vector - 1),
3873 		     i40e_intrl_usec_to_reg(vsi->int_rate_limit));
3874 
3875 		/* begin of linked list for RX queue assigned to this vector */
3876 		wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), qp);
3877 		for (q = 0; q < q_vector->num_ringpairs; q++) {
3878 			u32 nextqp = has_xdp ? qp + vsi->alloc_queue_pairs : qp;
3879 			u32 val;
3880 
3881 			val = I40E_QINT_RQCTL_CAUSE_ENA_MASK |
3882 			      (I40E_RX_ITR << I40E_QINT_RQCTL_ITR_INDX_SHIFT) |
3883 			      (vector << I40E_QINT_RQCTL_MSIX_INDX_SHIFT) |
3884 			      (nextqp << I40E_QINT_RQCTL_NEXTQ_INDX_SHIFT) |
3885 			      (I40E_QUEUE_TYPE_TX <<
3886 			       I40E_QINT_RQCTL_NEXTQ_TYPE_SHIFT);
3887 
3888 			wr32(hw, I40E_QINT_RQCTL(qp), val);
3889 
3890 			if (has_xdp) {
3891 				/* TX queue with next queue set to TX */
3892 				val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3893 				      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3894 				      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3895 				      (qp << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3896 				      (I40E_QUEUE_TYPE_TX <<
3897 				       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3898 
3899 				wr32(hw, I40E_QINT_TQCTL(nextqp), val);
3900 			}
3901 			/* TX queue with next RX or end of linked list */
3902 			val = I40E_QINT_TQCTL_CAUSE_ENA_MASK |
3903 			      (I40E_TX_ITR << I40E_QINT_TQCTL_ITR_INDX_SHIFT) |
3904 			      (vector << I40E_QINT_TQCTL_MSIX_INDX_SHIFT) |
3905 			      ((qp + 1) << I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT) |
3906 			      (I40E_QUEUE_TYPE_RX <<
3907 			       I40E_QINT_TQCTL_NEXTQ_TYPE_SHIFT);
3908 
3909 			/* Terminate the linked list */
3910 			if (q == (q_vector->num_ringpairs - 1))
3911 				val |= (I40E_QUEUE_END_OF_LIST <<
3912 					I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT);
3913 
3914 			wr32(hw, I40E_QINT_TQCTL(qp), val);
3915 			qp++;
3916 		}
3917 	}
3918 
3919 	i40e_flush(hw);
3920 }
3921 
3922 /**
3923  * i40e_enable_misc_int_causes - enable the non-queue interrupts
3924  * @pf: pointer to private device data structure
3925  **/
3926 static void i40e_enable_misc_int_causes(struct i40e_pf *pf)
3927 {
3928 	struct i40e_hw *hw = &pf->hw;
3929 	u32 val;
3930 
3931 	/* clear things first */
3932 	wr32(hw, I40E_PFINT_ICR0_ENA, 0);  /* disable all */
3933 	rd32(hw, I40E_PFINT_ICR0);         /* read to clear */
3934 
3935 	val = I40E_PFINT_ICR0_ENA_ECC_ERR_MASK       |
3936 	      I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK    |
3937 	      I40E_PFINT_ICR0_ENA_GRST_MASK          |
3938 	      I40E_PFINT_ICR0_ENA_PCI_EXCEPTION_MASK |
3939 	      I40E_PFINT_ICR0_ENA_GPIO_MASK          |
3940 	      I40E_PFINT_ICR0_ENA_HMC_ERR_MASK       |
3941 	      I40E_PFINT_ICR0_ENA_VFLR_MASK          |
3942 	      I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
3943 
3944 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
3945 		val |= I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
3946 
3947 	if (pf->flags & I40E_FLAG_PTP)
3948 		val |= I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
3949 
3950 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
3951 
3952 	/* SW_ITR_IDX = 0, but don't change INTENA */
3953 	wr32(hw, I40E_PFINT_DYN_CTL0, I40E_PFINT_DYN_CTL0_SW_ITR_INDX_MASK |
3954 					I40E_PFINT_DYN_CTL0_INTENA_MSK_MASK);
3955 
3956 	/* OTHER_ITR_IDX = 0 */
3957 	wr32(hw, I40E_PFINT_STAT_CTL0, 0);
3958 }
3959 
3960 /**
3961  * i40e_configure_msi_and_legacy - Legacy mode interrupt config in the HW
3962  * @vsi: the VSI being configured
3963  **/
3964 static void i40e_configure_msi_and_legacy(struct i40e_vsi *vsi)
3965 {
3966 	u32 nextqp = i40e_enabled_xdp_vsi(vsi) ? vsi->alloc_queue_pairs : 0;
3967 	struct i40e_q_vector *q_vector = vsi->q_vectors[0];
3968 	struct i40e_pf *pf = vsi->back;
3969 	struct i40e_hw *hw = &pf->hw;
3970 
3971 	/* set the ITR configuration */
3972 	q_vector->rx.next_update = jiffies + 1;
3973 	q_vector->rx.target_itr = ITR_TO_REG(vsi->rx_rings[0]->itr_setting);
3974 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), q_vector->rx.target_itr >> 1);
3975 	q_vector->rx.current_itr = q_vector->rx.target_itr;
3976 	q_vector->tx.next_update = jiffies + 1;
3977 	q_vector->tx.target_itr = ITR_TO_REG(vsi->tx_rings[0]->itr_setting);
3978 	wr32(hw, I40E_PFINT_ITR0(I40E_TX_ITR), q_vector->tx.target_itr >> 1);
3979 	q_vector->tx.current_itr = q_vector->tx.target_itr;
3980 
3981 	i40e_enable_misc_int_causes(pf);
3982 
3983 	/* FIRSTQ_INDX = 0, FIRSTQ_TYPE = 0 (rx) */
3984 	wr32(hw, I40E_PFINT_LNKLST0, 0);
3985 
3986 	/* Associate the queue pair to the vector and enable the queue
3987 	 * interrupt RX queue in linked list with next queue set to TX
3988 	 */
3989 	wr32(hw, I40E_QINT_RQCTL(0), I40E_QINT_RQCTL_VAL(nextqp, 0, TX));
3990 
3991 	if (i40e_enabled_xdp_vsi(vsi)) {
3992 		/* TX queue in linked list with next queue set to TX */
3993 		wr32(hw, I40E_QINT_TQCTL(nextqp),
3994 		     I40E_QINT_TQCTL_VAL(nextqp, 0, TX));
3995 	}
3996 
3997 	/* last TX queue so the next RX queue doesn't matter */
3998 	wr32(hw, I40E_QINT_TQCTL(0),
3999 	     I40E_QINT_TQCTL_VAL(I40E_QUEUE_END_OF_LIST, 0, RX));
4000 	i40e_flush(hw);
4001 }
4002 
4003 /**
4004  * i40e_irq_dynamic_disable_icr0 - Disable default interrupt generation for icr0
4005  * @pf: board private structure
4006  **/
4007 void i40e_irq_dynamic_disable_icr0(struct i40e_pf *pf)
4008 {
4009 	struct i40e_hw *hw = &pf->hw;
4010 
4011 	wr32(hw, I40E_PFINT_DYN_CTL0,
4012 	     I40E_ITR_NONE << I40E_PFINT_DYN_CTLN_ITR_INDX_SHIFT);
4013 	i40e_flush(hw);
4014 }
4015 
4016 /**
4017  * i40e_irq_dynamic_enable_icr0 - Enable default interrupt generation for icr0
4018  * @pf: board private structure
4019  **/
4020 void i40e_irq_dynamic_enable_icr0(struct i40e_pf *pf)
4021 {
4022 	struct i40e_hw *hw = &pf->hw;
4023 	u32 val;
4024 
4025 	val = I40E_PFINT_DYN_CTL0_INTENA_MASK   |
4026 	      I40E_PFINT_DYN_CTL0_CLEARPBA_MASK |
4027 	      (I40E_ITR_NONE << I40E_PFINT_DYN_CTL0_ITR_INDX_SHIFT);
4028 
4029 	wr32(hw, I40E_PFINT_DYN_CTL0, val);
4030 	i40e_flush(hw);
4031 }
4032 
4033 /**
4034  * i40e_msix_clean_rings - MSIX mode Interrupt Handler
4035  * @irq: interrupt number
4036  * @data: pointer to a q_vector
4037  **/
4038 static irqreturn_t i40e_msix_clean_rings(int irq, void *data)
4039 {
4040 	struct i40e_q_vector *q_vector = data;
4041 
4042 	if (!q_vector->tx.ring && !q_vector->rx.ring)
4043 		return IRQ_HANDLED;
4044 
4045 	napi_schedule_irqoff(&q_vector->napi);
4046 
4047 	return IRQ_HANDLED;
4048 }
4049 
4050 /**
4051  * i40e_irq_affinity_notify - Callback for affinity changes
4052  * @notify: context as to what irq was changed
4053  * @mask: the new affinity mask
4054  *
4055  * This is a callback function used by the irq_set_affinity_notifier function
4056  * so that we may register to receive changes to the irq affinity masks.
4057  **/
4058 static void i40e_irq_affinity_notify(struct irq_affinity_notify *notify,
4059 				     const cpumask_t *mask)
4060 {
4061 	struct i40e_q_vector *q_vector =
4062 		container_of(notify, struct i40e_q_vector, affinity_notify);
4063 
4064 	cpumask_copy(&q_vector->affinity_mask, mask);
4065 }
4066 
4067 /**
4068  * i40e_irq_affinity_release - Callback for affinity notifier release
4069  * @ref: internal core kernel usage
4070  *
4071  * This is a callback function used by the irq_set_affinity_notifier function
4072  * to inform the current notification subscriber that they will no longer
4073  * receive notifications.
4074  **/
4075 static void i40e_irq_affinity_release(struct kref *ref) {}
4076 
4077 /**
4078  * i40e_vsi_request_irq_msix - Initialize MSI-X interrupts
4079  * @vsi: the VSI being configured
4080  * @basename: name for the vector
4081  *
4082  * Allocates MSI-X vectors and requests interrupts from the kernel.
4083  **/
4084 static int i40e_vsi_request_irq_msix(struct i40e_vsi *vsi, char *basename)
4085 {
4086 	int q_vectors = vsi->num_q_vectors;
4087 	struct i40e_pf *pf = vsi->back;
4088 	int base = vsi->base_vector;
4089 	int rx_int_idx = 0;
4090 	int tx_int_idx = 0;
4091 	int vector, err;
4092 	int irq_num;
4093 	int cpu;
4094 
4095 	for (vector = 0; vector < q_vectors; vector++) {
4096 		struct i40e_q_vector *q_vector = vsi->q_vectors[vector];
4097 
4098 		irq_num = pf->msix_entries[base + vector].vector;
4099 
4100 		if (q_vector->tx.ring && q_vector->rx.ring) {
4101 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4102 				 "%s-%s-%d", basename, "TxRx", rx_int_idx++);
4103 			tx_int_idx++;
4104 		} else if (q_vector->rx.ring) {
4105 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4106 				 "%s-%s-%d", basename, "rx", rx_int_idx++);
4107 		} else if (q_vector->tx.ring) {
4108 			snprintf(q_vector->name, sizeof(q_vector->name) - 1,
4109 				 "%s-%s-%d", basename, "tx", tx_int_idx++);
4110 		} else {
4111 			/* skip this unused q_vector */
4112 			continue;
4113 		}
4114 		err = request_irq(irq_num,
4115 				  vsi->irq_handler,
4116 				  0,
4117 				  q_vector->name,
4118 				  q_vector);
4119 		if (err) {
4120 			dev_info(&pf->pdev->dev,
4121 				 "MSIX request_irq failed, error: %d\n", err);
4122 			goto free_queue_irqs;
4123 		}
4124 
4125 		/* register for affinity change notifications */
4126 		q_vector->irq_num = irq_num;
4127 		q_vector->affinity_notify.notify = i40e_irq_affinity_notify;
4128 		q_vector->affinity_notify.release = i40e_irq_affinity_release;
4129 		irq_set_affinity_notifier(irq_num, &q_vector->affinity_notify);
4130 		/* Spread affinity hints out across online CPUs.
4131 		 *
4132 		 * get_cpu_mask returns a static constant mask with
4133 		 * a permanent lifetime so it's ok to pass to
4134 		 * irq_update_affinity_hint without making a copy.
4135 		 */
4136 		cpu = cpumask_local_spread(q_vector->v_idx, -1);
4137 		irq_update_affinity_hint(irq_num, get_cpu_mask(cpu));
4138 	}
4139 
4140 	vsi->irqs_ready = true;
4141 	return 0;
4142 
4143 free_queue_irqs:
4144 	while (vector) {
4145 		vector--;
4146 		irq_num = pf->msix_entries[base + vector].vector;
4147 		irq_set_affinity_notifier(irq_num, NULL);
4148 		irq_update_affinity_hint(irq_num, NULL);
4149 		free_irq(irq_num, &vsi->q_vectors[vector]);
4150 	}
4151 	return err;
4152 }
4153 
4154 /**
4155  * i40e_vsi_disable_irq - Mask off queue interrupt generation on the VSI
4156  * @vsi: the VSI being un-configured
4157  **/
4158 static void i40e_vsi_disable_irq(struct i40e_vsi *vsi)
4159 {
4160 	struct i40e_pf *pf = vsi->back;
4161 	struct i40e_hw *hw = &pf->hw;
4162 	int base = vsi->base_vector;
4163 	int i;
4164 
4165 	/* disable interrupt causation from each queue */
4166 	for (i = 0; i < vsi->num_queue_pairs; i++) {
4167 		u32 val;
4168 
4169 		val = rd32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx));
4170 		val &= ~I40E_QINT_TQCTL_CAUSE_ENA_MASK;
4171 		wr32(hw, I40E_QINT_TQCTL(vsi->tx_rings[i]->reg_idx), val);
4172 
4173 		val = rd32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx));
4174 		val &= ~I40E_QINT_RQCTL_CAUSE_ENA_MASK;
4175 		wr32(hw, I40E_QINT_RQCTL(vsi->rx_rings[i]->reg_idx), val);
4176 
4177 		if (!i40e_enabled_xdp_vsi(vsi))
4178 			continue;
4179 		wr32(hw, I40E_QINT_TQCTL(vsi->xdp_rings[i]->reg_idx), 0);
4180 	}
4181 
4182 	/* disable each interrupt */
4183 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4184 		for (i = vsi->base_vector;
4185 		     i < (vsi->num_q_vectors + vsi->base_vector); i++)
4186 			wr32(hw, I40E_PFINT_DYN_CTLN(i - 1), 0);
4187 
4188 		i40e_flush(hw);
4189 		for (i = 0; i < vsi->num_q_vectors; i++)
4190 			synchronize_irq(pf->msix_entries[i + base].vector);
4191 	} else {
4192 		/* Legacy and MSI mode - this stops all interrupt handling */
4193 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
4194 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
4195 		i40e_flush(hw);
4196 		synchronize_irq(pf->pdev->irq);
4197 	}
4198 }
4199 
4200 /**
4201  * i40e_vsi_enable_irq - Enable IRQ for the given VSI
4202  * @vsi: the VSI being configured
4203  **/
4204 static int i40e_vsi_enable_irq(struct i40e_vsi *vsi)
4205 {
4206 	struct i40e_pf *pf = vsi->back;
4207 	int i;
4208 
4209 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4210 		for (i = 0; i < vsi->num_q_vectors; i++)
4211 			i40e_irq_dynamic_enable(vsi, i);
4212 	} else {
4213 		i40e_irq_dynamic_enable_icr0(pf);
4214 	}
4215 
4216 	i40e_flush(&pf->hw);
4217 	return 0;
4218 }
4219 
4220 /**
4221  * i40e_free_misc_vector - Free the vector that handles non-queue events
4222  * @pf: board private structure
4223  **/
4224 static void i40e_free_misc_vector(struct i40e_pf *pf)
4225 {
4226 	/* Disable ICR 0 */
4227 	wr32(&pf->hw, I40E_PFINT_ICR0_ENA, 0);
4228 	i40e_flush(&pf->hw);
4229 
4230 	if (pf->flags & I40E_FLAG_MSIX_ENABLED && pf->msix_entries) {
4231 		free_irq(pf->msix_entries[0].vector, pf);
4232 		clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
4233 	}
4234 }
4235 
4236 /**
4237  * i40e_intr - MSI/Legacy and non-queue interrupt handler
4238  * @irq: interrupt number
4239  * @data: pointer to a q_vector
4240  *
4241  * This is the handler used for all MSI/Legacy interrupts, and deals
4242  * with both queue and non-queue interrupts.  This is also used in
4243  * MSIX mode to handle the non-queue interrupts.
4244  **/
4245 static irqreturn_t i40e_intr(int irq, void *data)
4246 {
4247 	struct i40e_pf *pf = (struct i40e_pf *)data;
4248 	struct i40e_hw *hw = &pf->hw;
4249 	irqreturn_t ret = IRQ_NONE;
4250 	u32 icr0, icr0_remaining;
4251 	u32 val, ena_mask;
4252 
4253 	icr0 = rd32(hw, I40E_PFINT_ICR0);
4254 	ena_mask = rd32(hw, I40E_PFINT_ICR0_ENA);
4255 
4256 	/* if sharing a legacy IRQ, we might get called w/o an intr pending */
4257 	if ((icr0 & I40E_PFINT_ICR0_INTEVENT_MASK) == 0)
4258 		goto enable_intr;
4259 
4260 	/* if interrupt but no bits showing, must be SWINT */
4261 	if (((icr0 & ~I40E_PFINT_ICR0_INTEVENT_MASK) == 0) ||
4262 	    (icr0 & I40E_PFINT_ICR0_SWINT_MASK))
4263 		pf->sw_int_count++;
4264 
4265 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
4266 	    (icr0 & I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK)) {
4267 		ena_mask &= ~I40E_PFINT_ICR0_ENA_PE_CRITERR_MASK;
4268 		dev_dbg(&pf->pdev->dev, "cleared PE_CRITERR\n");
4269 		set_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
4270 	}
4271 
4272 	/* only q0 is used in MSI/Legacy mode, and none are used in MSIX */
4273 	if (icr0 & I40E_PFINT_ICR0_QUEUE_0_MASK) {
4274 		struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
4275 		struct i40e_q_vector *q_vector = vsi->q_vectors[0];
4276 
4277 		/* We do not have a way to disarm Queue causes while leaving
4278 		 * interrupt enabled for all other causes, ideally
4279 		 * interrupt should be disabled while we are in NAPI but
4280 		 * this is not a performance path and napi_schedule()
4281 		 * can deal with rescheduling.
4282 		 */
4283 		if (!test_bit(__I40E_DOWN, pf->state))
4284 			napi_schedule_irqoff(&q_vector->napi);
4285 	}
4286 
4287 	if (icr0 & I40E_PFINT_ICR0_ADMINQ_MASK) {
4288 		ena_mask &= ~I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
4289 		set_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
4290 		i40e_debug(&pf->hw, I40E_DEBUG_NVM, "AdminQ event\n");
4291 	}
4292 
4293 	if (icr0 & I40E_PFINT_ICR0_MAL_DETECT_MASK) {
4294 		ena_mask &= ~I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
4295 		set_bit(__I40E_MDD_EVENT_PENDING, pf->state);
4296 	}
4297 
4298 	if (icr0 & I40E_PFINT_ICR0_VFLR_MASK) {
4299 		/* disable any further VFLR event notifications */
4300 		if (test_bit(__I40E_VF_RESETS_DISABLED, pf->state)) {
4301 			u32 reg = rd32(hw, I40E_PFINT_ICR0_ENA);
4302 
4303 			reg &= ~I40E_PFINT_ICR0_VFLR_MASK;
4304 			wr32(hw, I40E_PFINT_ICR0_ENA, reg);
4305 		} else {
4306 			ena_mask &= ~I40E_PFINT_ICR0_ENA_VFLR_MASK;
4307 			set_bit(__I40E_VFLR_EVENT_PENDING, pf->state);
4308 		}
4309 	}
4310 
4311 	if (icr0 & I40E_PFINT_ICR0_GRST_MASK) {
4312 		if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
4313 			set_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
4314 		ena_mask &= ~I40E_PFINT_ICR0_ENA_GRST_MASK;
4315 		val = rd32(hw, I40E_GLGEN_RSTAT);
4316 		val = (val & I40E_GLGEN_RSTAT_RESET_TYPE_MASK)
4317 		       >> I40E_GLGEN_RSTAT_RESET_TYPE_SHIFT;
4318 		if (val == I40E_RESET_CORER) {
4319 			pf->corer_count++;
4320 		} else if (val == I40E_RESET_GLOBR) {
4321 			pf->globr_count++;
4322 		} else if (val == I40E_RESET_EMPR) {
4323 			pf->empr_count++;
4324 			set_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state);
4325 		}
4326 	}
4327 
4328 	if (icr0 & I40E_PFINT_ICR0_HMC_ERR_MASK) {
4329 		icr0 &= ~I40E_PFINT_ICR0_HMC_ERR_MASK;
4330 		dev_info(&pf->pdev->dev, "HMC error interrupt\n");
4331 		dev_info(&pf->pdev->dev, "HMC error info 0x%x, HMC error data 0x%x\n",
4332 			 rd32(hw, I40E_PFHMC_ERRORINFO),
4333 			 rd32(hw, I40E_PFHMC_ERRORDATA));
4334 	}
4335 
4336 	if (icr0 & I40E_PFINT_ICR0_TIMESYNC_MASK) {
4337 		u32 prttsyn_stat = rd32(hw, I40E_PRTTSYN_STAT_0);
4338 
4339 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_EVENT0_MASK)
4340 			schedule_work(&pf->ptp_extts0_work);
4341 
4342 		if (prttsyn_stat & I40E_PRTTSYN_STAT_0_TXTIME_MASK)
4343 			i40e_ptp_tx_hwtstamp(pf);
4344 
4345 		icr0 &= ~I40E_PFINT_ICR0_ENA_TIMESYNC_MASK;
4346 	}
4347 
4348 	/* If a critical error is pending we have no choice but to reset the
4349 	 * device.
4350 	 * Report and mask out any remaining unexpected interrupts.
4351 	 */
4352 	icr0_remaining = icr0 & ena_mask;
4353 	if (icr0_remaining) {
4354 		dev_info(&pf->pdev->dev, "unhandled interrupt icr0=0x%08x\n",
4355 			 icr0_remaining);
4356 		if ((icr0_remaining & I40E_PFINT_ICR0_PE_CRITERR_MASK) ||
4357 		    (icr0_remaining & I40E_PFINT_ICR0_PCI_EXCEPTION_MASK) ||
4358 		    (icr0_remaining & I40E_PFINT_ICR0_ECC_ERR_MASK)) {
4359 			dev_info(&pf->pdev->dev, "device will be reset\n");
4360 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
4361 			i40e_service_event_schedule(pf);
4362 		}
4363 		ena_mask &= ~icr0_remaining;
4364 	}
4365 	ret = IRQ_HANDLED;
4366 
4367 enable_intr:
4368 	/* re-enable interrupt causes */
4369 	wr32(hw, I40E_PFINT_ICR0_ENA, ena_mask);
4370 	if (!test_bit(__I40E_DOWN, pf->state) ||
4371 	    test_bit(__I40E_RECOVERY_MODE, pf->state)) {
4372 		i40e_service_event_schedule(pf);
4373 		i40e_irq_dynamic_enable_icr0(pf);
4374 	}
4375 
4376 	return ret;
4377 }
4378 
4379 /**
4380  * i40e_clean_fdir_tx_irq - Reclaim resources after transmit completes
4381  * @tx_ring:  tx ring to clean
4382  * @budget:   how many cleans we're allowed
4383  *
4384  * Returns true if there's any budget left (e.g. the clean is finished)
4385  **/
4386 static bool i40e_clean_fdir_tx_irq(struct i40e_ring *tx_ring, int budget)
4387 {
4388 	struct i40e_vsi *vsi = tx_ring->vsi;
4389 	u16 i = tx_ring->next_to_clean;
4390 	struct i40e_tx_buffer *tx_buf;
4391 	struct i40e_tx_desc *tx_desc;
4392 
4393 	tx_buf = &tx_ring->tx_bi[i];
4394 	tx_desc = I40E_TX_DESC(tx_ring, i);
4395 	i -= tx_ring->count;
4396 
4397 	do {
4398 		struct i40e_tx_desc *eop_desc = tx_buf->next_to_watch;
4399 
4400 		/* if next_to_watch is not set then there is no work pending */
4401 		if (!eop_desc)
4402 			break;
4403 
4404 		/* prevent any other reads prior to eop_desc */
4405 		smp_rmb();
4406 
4407 		/* if the descriptor isn't done, no work yet to do */
4408 		if (!(eop_desc->cmd_type_offset_bsz &
4409 		      cpu_to_le64(I40E_TX_DESC_DTYPE_DESC_DONE)))
4410 			break;
4411 
4412 		/* clear next_to_watch to prevent false hangs */
4413 		tx_buf->next_to_watch = NULL;
4414 
4415 		tx_desc->buffer_addr = 0;
4416 		tx_desc->cmd_type_offset_bsz = 0;
4417 		/* move past filter desc */
4418 		tx_buf++;
4419 		tx_desc++;
4420 		i++;
4421 		if (unlikely(!i)) {
4422 			i -= tx_ring->count;
4423 			tx_buf = tx_ring->tx_bi;
4424 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4425 		}
4426 		/* unmap skb header data */
4427 		dma_unmap_single(tx_ring->dev,
4428 				 dma_unmap_addr(tx_buf, dma),
4429 				 dma_unmap_len(tx_buf, len),
4430 				 DMA_TO_DEVICE);
4431 		if (tx_buf->tx_flags & I40E_TX_FLAGS_FD_SB)
4432 			kfree(tx_buf->raw_buf);
4433 
4434 		tx_buf->raw_buf = NULL;
4435 		tx_buf->tx_flags = 0;
4436 		tx_buf->next_to_watch = NULL;
4437 		dma_unmap_len_set(tx_buf, len, 0);
4438 		tx_desc->buffer_addr = 0;
4439 		tx_desc->cmd_type_offset_bsz = 0;
4440 
4441 		/* move us past the eop_desc for start of next FD desc */
4442 		tx_buf++;
4443 		tx_desc++;
4444 		i++;
4445 		if (unlikely(!i)) {
4446 			i -= tx_ring->count;
4447 			tx_buf = tx_ring->tx_bi;
4448 			tx_desc = I40E_TX_DESC(tx_ring, 0);
4449 		}
4450 
4451 		/* update budget accounting */
4452 		budget--;
4453 	} while (likely(budget));
4454 
4455 	i += tx_ring->count;
4456 	tx_ring->next_to_clean = i;
4457 
4458 	if (vsi->back->flags & I40E_FLAG_MSIX_ENABLED)
4459 		i40e_irq_dynamic_enable(vsi, tx_ring->q_vector->v_idx);
4460 
4461 	return budget > 0;
4462 }
4463 
4464 /**
4465  * i40e_fdir_clean_ring - Interrupt Handler for FDIR SB ring
4466  * @irq: interrupt number
4467  * @data: pointer to a q_vector
4468  **/
4469 static irqreturn_t i40e_fdir_clean_ring(int irq, void *data)
4470 {
4471 	struct i40e_q_vector *q_vector = data;
4472 	struct i40e_vsi *vsi;
4473 
4474 	if (!q_vector->tx.ring)
4475 		return IRQ_HANDLED;
4476 
4477 	vsi = q_vector->tx.ring->vsi;
4478 	i40e_clean_fdir_tx_irq(q_vector->tx.ring, vsi->work_limit);
4479 
4480 	return IRQ_HANDLED;
4481 }
4482 
4483 /**
4484  * i40e_map_vector_to_qp - Assigns the queue pair to the vector
4485  * @vsi: the VSI being configured
4486  * @v_idx: vector index
4487  * @qp_idx: queue pair index
4488  **/
4489 static void i40e_map_vector_to_qp(struct i40e_vsi *vsi, int v_idx, int qp_idx)
4490 {
4491 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
4492 	struct i40e_ring *tx_ring = vsi->tx_rings[qp_idx];
4493 	struct i40e_ring *rx_ring = vsi->rx_rings[qp_idx];
4494 
4495 	tx_ring->q_vector = q_vector;
4496 	tx_ring->next = q_vector->tx.ring;
4497 	q_vector->tx.ring = tx_ring;
4498 	q_vector->tx.count++;
4499 
4500 	/* Place XDP Tx ring in the same q_vector ring list as regular Tx */
4501 	if (i40e_enabled_xdp_vsi(vsi)) {
4502 		struct i40e_ring *xdp_ring = vsi->xdp_rings[qp_idx];
4503 
4504 		xdp_ring->q_vector = q_vector;
4505 		xdp_ring->next = q_vector->tx.ring;
4506 		q_vector->tx.ring = xdp_ring;
4507 		q_vector->tx.count++;
4508 	}
4509 
4510 	rx_ring->q_vector = q_vector;
4511 	rx_ring->next = q_vector->rx.ring;
4512 	q_vector->rx.ring = rx_ring;
4513 	q_vector->rx.count++;
4514 }
4515 
4516 /**
4517  * i40e_vsi_map_rings_to_vectors - Maps descriptor rings to vectors
4518  * @vsi: the VSI being configured
4519  *
4520  * This function maps descriptor rings to the queue-specific vectors
4521  * we were allotted through the MSI-X enabling code.  Ideally, we'd have
4522  * one vector per queue pair, but on a constrained vector budget, we
4523  * group the queue pairs as "efficiently" as possible.
4524  **/
4525 static void i40e_vsi_map_rings_to_vectors(struct i40e_vsi *vsi)
4526 {
4527 	int qp_remaining = vsi->num_queue_pairs;
4528 	int q_vectors = vsi->num_q_vectors;
4529 	int num_ringpairs;
4530 	int v_start = 0;
4531 	int qp_idx = 0;
4532 
4533 	/* If we don't have enough vectors for a 1-to-1 mapping, we'll have to
4534 	 * group them so there are multiple queues per vector.
4535 	 * It is also important to go through all the vectors available to be
4536 	 * sure that if we don't use all the vectors, that the remaining vectors
4537 	 * are cleared. This is especially important when decreasing the
4538 	 * number of queues in use.
4539 	 */
4540 	for (; v_start < q_vectors; v_start++) {
4541 		struct i40e_q_vector *q_vector = vsi->q_vectors[v_start];
4542 
4543 		num_ringpairs = DIV_ROUND_UP(qp_remaining, q_vectors - v_start);
4544 
4545 		q_vector->num_ringpairs = num_ringpairs;
4546 		q_vector->reg_idx = q_vector->v_idx + vsi->base_vector - 1;
4547 
4548 		q_vector->rx.count = 0;
4549 		q_vector->tx.count = 0;
4550 		q_vector->rx.ring = NULL;
4551 		q_vector->tx.ring = NULL;
4552 
4553 		while (num_ringpairs--) {
4554 			i40e_map_vector_to_qp(vsi, v_start, qp_idx);
4555 			qp_idx++;
4556 			qp_remaining--;
4557 		}
4558 	}
4559 }
4560 
4561 /**
4562  * i40e_vsi_request_irq - Request IRQ from the OS
4563  * @vsi: the VSI being configured
4564  * @basename: name for the vector
4565  **/
4566 static int i40e_vsi_request_irq(struct i40e_vsi *vsi, char *basename)
4567 {
4568 	struct i40e_pf *pf = vsi->back;
4569 	int err;
4570 
4571 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
4572 		err = i40e_vsi_request_irq_msix(vsi, basename);
4573 	else if (pf->flags & I40E_FLAG_MSI_ENABLED)
4574 		err = request_irq(pf->pdev->irq, i40e_intr, 0,
4575 				  pf->int_name, pf);
4576 	else
4577 		err = request_irq(pf->pdev->irq, i40e_intr, IRQF_SHARED,
4578 				  pf->int_name, pf);
4579 
4580 	if (err)
4581 		dev_info(&pf->pdev->dev, "request_irq failed, Error %d\n", err);
4582 
4583 	return err;
4584 }
4585 
4586 #ifdef CONFIG_NET_POLL_CONTROLLER
4587 /**
4588  * i40e_netpoll - A Polling 'interrupt' handler
4589  * @netdev: network interface device structure
4590  *
4591  * This is used by netconsole to send skbs without having to re-enable
4592  * interrupts.  It's not called while the normal interrupt routine is executing.
4593  **/
4594 static void i40e_netpoll(struct net_device *netdev)
4595 {
4596 	struct i40e_netdev_priv *np = netdev_priv(netdev);
4597 	struct i40e_vsi *vsi = np->vsi;
4598 	struct i40e_pf *pf = vsi->back;
4599 	int i;
4600 
4601 	/* if interface is down do nothing */
4602 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
4603 		return;
4604 
4605 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4606 		for (i = 0; i < vsi->num_q_vectors; i++)
4607 			i40e_msix_clean_rings(0, vsi->q_vectors[i]);
4608 	} else {
4609 		i40e_intr(pf->pdev->irq, netdev);
4610 	}
4611 }
4612 #endif
4613 
4614 #define I40E_QTX_ENA_WAIT_COUNT 50
4615 
4616 /**
4617  * i40e_pf_txq_wait - Wait for a PF's Tx queue to be enabled or disabled
4618  * @pf: the PF being configured
4619  * @pf_q: the PF queue
4620  * @enable: enable or disable state of the queue
4621  *
4622  * This routine will wait for the given Tx queue of the PF to reach the
4623  * enabled or disabled state.
4624  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4625  * multiple retries; else will return 0 in case of success.
4626  **/
4627 static int i40e_pf_txq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4628 {
4629 	int i;
4630 	u32 tx_reg;
4631 
4632 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4633 		tx_reg = rd32(&pf->hw, I40E_QTX_ENA(pf_q));
4634 		if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4635 			break;
4636 
4637 		usleep_range(10, 20);
4638 	}
4639 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4640 		return -ETIMEDOUT;
4641 
4642 	return 0;
4643 }
4644 
4645 /**
4646  * i40e_control_tx_q - Start or stop a particular Tx queue
4647  * @pf: the PF structure
4648  * @pf_q: the PF queue to configure
4649  * @enable: start or stop the queue
4650  *
4651  * This function enables or disables a single queue. Note that any delay
4652  * required after the operation is expected to be handled by the caller of
4653  * this function.
4654  **/
4655 static void i40e_control_tx_q(struct i40e_pf *pf, int pf_q, bool enable)
4656 {
4657 	struct i40e_hw *hw = &pf->hw;
4658 	u32 tx_reg;
4659 	int i;
4660 
4661 	/* warn the TX unit of coming changes */
4662 	i40e_pre_tx_queue_cfg(&pf->hw, pf_q, enable);
4663 	if (!enable)
4664 		usleep_range(10, 20);
4665 
4666 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4667 		tx_reg = rd32(hw, I40E_QTX_ENA(pf_q));
4668 		if (((tx_reg >> I40E_QTX_ENA_QENA_REQ_SHIFT) & 1) ==
4669 		    ((tx_reg >> I40E_QTX_ENA_QENA_STAT_SHIFT) & 1))
4670 			break;
4671 		usleep_range(1000, 2000);
4672 	}
4673 
4674 	/* Skip if the queue is already in the requested state */
4675 	if (enable == !!(tx_reg & I40E_QTX_ENA_QENA_STAT_MASK))
4676 		return;
4677 
4678 	/* turn on/off the queue */
4679 	if (enable) {
4680 		wr32(hw, I40E_QTX_HEAD(pf_q), 0);
4681 		tx_reg |= I40E_QTX_ENA_QENA_REQ_MASK;
4682 	} else {
4683 		tx_reg &= ~I40E_QTX_ENA_QENA_REQ_MASK;
4684 	}
4685 
4686 	wr32(hw, I40E_QTX_ENA(pf_q), tx_reg);
4687 }
4688 
4689 /**
4690  * i40e_control_wait_tx_q - Start/stop Tx queue and wait for completion
4691  * @seid: VSI SEID
4692  * @pf: the PF structure
4693  * @pf_q: the PF queue to configure
4694  * @is_xdp: true if the queue is used for XDP
4695  * @enable: start or stop the queue
4696  **/
4697 int i40e_control_wait_tx_q(int seid, struct i40e_pf *pf, int pf_q,
4698 			   bool is_xdp, bool enable)
4699 {
4700 	int ret;
4701 
4702 	i40e_control_tx_q(pf, pf_q, enable);
4703 
4704 	/* wait for the change to finish */
4705 	ret = i40e_pf_txq_wait(pf, pf_q, enable);
4706 	if (ret) {
4707 		dev_info(&pf->pdev->dev,
4708 			 "VSI seid %d %sTx ring %d %sable timeout\n",
4709 			 seid, (is_xdp ? "XDP " : ""), pf_q,
4710 			 (enable ? "en" : "dis"));
4711 	}
4712 
4713 	return ret;
4714 }
4715 
4716 /**
4717  * i40e_vsi_enable_tx - Start a VSI's rings
4718  * @vsi: the VSI being configured
4719  **/
4720 static int i40e_vsi_enable_tx(struct i40e_vsi *vsi)
4721 {
4722 	struct i40e_pf *pf = vsi->back;
4723 	int i, pf_q, ret = 0;
4724 
4725 	pf_q = vsi->base_queue;
4726 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4727 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4728 					     pf_q,
4729 					     false /*is xdp*/, true);
4730 		if (ret)
4731 			break;
4732 
4733 		if (!i40e_enabled_xdp_vsi(vsi))
4734 			continue;
4735 
4736 		ret = i40e_control_wait_tx_q(vsi->seid, pf,
4737 					     pf_q + vsi->alloc_queue_pairs,
4738 					     true /*is xdp*/, true);
4739 		if (ret)
4740 			break;
4741 	}
4742 	return ret;
4743 }
4744 
4745 /**
4746  * i40e_pf_rxq_wait - Wait for a PF's Rx queue to be enabled or disabled
4747  * @pf: the PF being configured
4748  * @pf_q: the PF queue
4749  * @enable: enable or disable state of the queue
4750  *
4751  * This routine will wait for the given Rx queue of the PF to reach the
4752  * enabled or disabled state.
4753  * Returns -ETIMEDOUT in case of failing to reach the requested state after
4754  * multiple retries; else will return 0 in case of success.
4755  **/
4756 static int i40e_pf_rxq_wait(struct i40e_pf *pf, int pf_q, bool enable)
4757 {
4758 	int i;
4759 	u32 rx_reg;
4760 
4761 	for (i = 0; i < I40E_QUEUE_WAIT_RETRY_LIMIT; i++) {
4762 		rx_reg = rd32(&pf->hw, I40E_QRX_ENA(pf_q));
4763 		if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4764 			break;
4765 
4766 		usleep_range(10, 20);
4767 	}
4768 	if (i >= I40E_QUEUE_WAIT_RETRY_LIMIT)
4769 		return -ETIMEDOUT;
4770 
4771 	return 0;
4772 }
4773 
4774 /**
4775  * i40e_control_rx_q - Start or stop a particular Rx queue
4776  * @pf: the PF structure
4777  * @pf_q: the PF queue to configure
4778  * @enable: start or stop the queue
4779  *
4780  * This function enables or disables a single queue. Note that
4781  * any delay required after the operation is expected to be
4782  * handled by the caller of this function.
4783  **/
4784 static void i40e_control_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4785 {
4786 	struct i40e_hw *hw = &pf->hw;
4787 	u32 rx_reg;
4788 	int i;
4789 
4790 	for (i = 0; i < I40E_QTX_ENA_WAIT_COUNT; i++) {
4791 		rx_reg = rd32(hw, I40E_QRX_ENA(pf_q));
4792 		if (((rx_reg >> I40E_QRX_ENA_QENA_REQ_SHIFT) & 1) ==
4793 		    ((rx_reg >> I40E_QRX_ENA_QENA_STAT_SHIFT) & 1))
4794 			break;
4795 		usleep_range(1000, 2000);
4796 	}
4797 
4798 	/* Skip if the queue is already in the requested state */
4799 	if (enable == !!(rx_reg & I40E_QRX_ENA_QENA_STAT_MASK))
4800 		return;
4801 
4802 	/* turn on/off the queue */
4803 	if (enable)
4804 		rx_reg |= I40E_QRX_ENA_QENA_REQ_MASK;
4805 	else
4806 		rx_reg &= ~I40E_QRX_ENA_QENA_REQ_MASK;
4807 
4808 	wr32(hw, I40E_QRX_ENA(pf_q), rx_reg);
4809 }
4810 
4811 /**
4812  * i40e_control_wait_rx_q
4813  * @pf: the PF structure
4814  * @pf_q: queue being configured
4815  * @enable: start or stop the rings
4816  *
4817  * This function enables or disables a single queue along with waiting
4818  * for the change to finish. The caller of this function should handle
4819  * the delays needed in the case of disabling queues.
4820  **/
4821 int i40e_control_wait_rx_q(struct i40e_pf *pf, int pf_q, bool enable)
4822 {
4823 	int ret = 0;
4824 
4825 	i40e_control_rx_q(pf, pf_q, enable);
4826 
4827 	/* wait for the change to finish */
4828 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
4829 	if (ret)
4830 		return ret;
4831 
4832 	return ret;
4833 }
4834 
4835 /**
4836  * i40e_vsi_enable_rx - Start a VSI's rings
4837  * @vsi: the VSI being configured
4838  **/
4839 static int i40e_vsi_enable_rx(struct i40e_vsi *vsi)
4840 {
4841 	struct i40e_pf *pf = vsi->back;
4842 	int i, pf_q, ret = 0;
4843 
4844 	pf_q = vsi->base_queue;
4845 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4846 		ret = i40e_control_wait_rx_q(pf, pf_q, true);
4847 		if (ret) {
4848 			dev_info(&pf->pdev->dev,
4849 				 "VSI seid %d Rx ring %d enable timeout\n",
4850 				 vsi->seid, pf_q);
4851 			break;
4852 		}
4853 	}
4854 
4855 	return ret;
4856 }
4857 
4858 /**
4859  * i40e_vsi_start_rings - Start a VSI's rings
4860  * @vsi: the VSI being configured
4861  **/
4862 int i40e_vsi_start_rings(struct i40e_vsi *vsi)
4863 {
4864 	int ret = 0;
4865 
4866 	/* do rx first for enable and last for disable */
4867 	ret = i40e_vsi_enable_rx(vsi);
4868 	if (ret)
4869 		return ret;
4870 	ret = i40e_vsi_enable_tx(vsi);
4871 
4872 	return ret;
4873 }
4874 
4875 #define I40E_DISABLE_TX_GAP_MSEC	50
4876 
4877 /**
4878  * i40e_vsi_stop_rings - Stop a VSI's rings
4879  * @vsi: the VSI being configured
4880  **/
4881 void i40e_vsi_stop_rings(struct i40e_vsi *vsi)
4882 {
4883 	struct i40e_pf *pf = vsi->back;
4884 	int pf_q, err, q_end;
4885 
4886 	/* When port TX is suspended, don't wait */
4887 	if (test_bit(__I40E_PORT_SUSPENDED, vsi->back->state))
4888 		return i40e_vsi_stop_rings_no_wait(vsi);
4889 
4890 	q_end = vsi->base_queue + vsi->num_queue_pairs;
4891 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4892 		i40e_pre_tx_queue_cfg(&pf->hw, (u32)pf_q, false);
4893 
4894 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++) {
4895 		err = i40e_control_wait_rx_q(pf, pf_q, false);
4896 		if (err)
4897 			dev_info(&pf->pdev->dev,
4898 				 "VSI seid %d Rx ring %d disable timeout\n",
4899 				 vsi->seid, pf_q);
4900 	}
4901 
4902 	msleep(I40E_DISABLE_TX_GAP_MSEC);
4903 	pf_q = vsi->base_queue;
4904 	for (pf_q = vsi->base_queue; pf_q < q_end; pf_q++)
4905 		wr32(&pf->hw, I40E_QTX_ENA(pf_q), 0);
4906 
4907 	i40e_vsi_wait_queues_disabled(vsi);
4908 }
4909 
4910 /**
4911  * i40e_vsi_stop_rings_no_wait - Stop a VSI's rings and do not delay
4912  * @vsi: the VSI being shutdown
4913  *
4914  * This function stops all the rings for a VSI but does not delay to verify
4915  * that rings have been disabled. It is expected that the caller is shutting
4916  * down multiple VSIs at once and will delay together for all the VSIs after
4917  * initiating the shutdown. This is particularly useful for shutting down lots
4918  * of VFs together. Otherwise, a large delay can be incurred while configuring
4919  * each VSI in serial.
4920  **/
4921 void i40e_vsi_stop_rings_no_wait(struct i40e_vsi *vsi)
4922 {
4923 	struct i40e_pf *pf = vsi->back;
4924 	int i, pf_q;
4925 
4926 	pf_q = vsi->base_queue;
4927 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
4928 		i40e_control_tx_q(pf, pf_q, false);
4929 		i40e_control_rx_q(pf, pf_q, false);
4930 	}
4931 }
4932 
4933 /**
4934  * i40e_vsi_free_irq - Free the irq association with the OS
4935  * @vsi: the VSI being configured
4936  **/
4937 static void i40e_vsi_free_irq(struct i40e_vsi *vsi)
4938 {
4939 	struct i40e_pf *pf = vsi->back;
4940 	struct i40e_hw *hw = &pf->hw;
4941 	int base = vsi->base_vector;
4942 	u32 val, qp;
4943 	int i;
4944 
4945 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
4946 		if (!vsi->q_vectors)
4947 			return;
4948 
4949 		if (!vsi->irqs_ready)
4950 			return;
4951 
4952 		vsi->irqs_ready = false;
4953 		for (i = 0; i < vsi->num_q_vectors; i++) {
4954 			int irq_num;
4955 			u16 vector;
4956 
4957 			vector = i + base;
4958 			irq_num = pf->msix_entries[vector].vector;
4959 
4960 			/* free only the irqs that were actually requested */
4961 			if (!vsi->q_vectors[i] ||
4962 			    !vsi->q_vectors[i]->num_ringpairs)
4963 				continue;
4964 
4965 			/* clear the affinity notifier in the IRQ descriptor */
4966 			irq_set_affinity_notifier(irq_num, NULL);
4967 			/* remove our suggested affinity mask for this IRQ */
4968 			irq_update_affinity_hint(irq_num, NULL);
4969 			free_irq(irq_num, vsi->q_vectors[i]);
4970 
4971 			/* Tear down the interrupt queue link list
4972 			 *
4973 			 * We know that they come in pairs and always
4974 			 * the Rx first, then the Tx.  To clear the
4975 			 * link list, stick the EOL value into the
4976 			 * next_q field of the registers.
4977 			 */
4978 			val = rd32(hw, I40E_PFINT_LNKLSTN(vector - 1));
4979 			qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
4980 				>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4981 			val |= I40E_QUEUE_END_OF_LIST
4982 				<< I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
4983 			wr32(hw, I40E_PFINT_LNKLSTN(vector - 1), val);
4984 
4985 			while (qp != I40E_QUEUE_END_OF_LIST) {
4986 				u32 next;
4987 
4988 				val = rd32(hw, I40E_QINT_RQCTL(qp));
4989 
4990 				val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
4991 					 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
4992 					 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
4993 					 I40E_QINT_RQCTL_INTEVENT_MASK);
4994 
4995 				val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
4996 					 I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
4997 
4998 				wr32(hw, I40E_QINT_RQCTL(qp), val);
4999 
5000 				val = rd32(hw, I40E_QINT_TQCTL(qp));
5001 
5002 				next = (val & I40E_QINT_TQCTL_NEXTQ_INDX_MASK)
5003 					>> I40E_QINT_TQCTL_NEXTQ_INDX_SHIFT;
5004 
5005 				val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5006 					 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5007 					 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5008 					 I40E_QINT_TQCTL_INTEVENT_MASK);
5009 
5010 				val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5011 					 I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5012 
5013 				wr32(hw, I40E_QINT_TQCTL(qp), val);
5014 				qp = next;
5015 			}
5016 		}
5017 	} else {
5018 		free_irq(pf->pdev->irq, pf);
5019 
5020 		val = rd32(hw, I40E_PFINT_LNKLST0);
5021 		qp = (val & I40E_PFINT_LNKLSTN_FIRSTQ_INDX_MASK)
5022 			>> I40E_PFINT_LNKLSTN_FIRSTQ_INDX_SHIFT;
5023 		val |= I40E_QUEUE_END_OF_LIST
5024 			<< I40E_PFINT_LNKLST0_FIRSTQ_INDX_SHIFT;
5025 		wr32(hw, I40E_PFINT_LNKLST0, val);
5026 
5027 		val = rd32(hw, I40E_QINT_RQCTL(qp));
5028 		val &= ~(I40E_QINT_RQCTL_MSIX_INDX_MASK  |
5029 			 I40E_QINT_RQCTL_MSIX0_INDX_MASK |
5030 			 I40E_QINT_RQCTL_CAUSE_ENA_MASK  |
5031 			 I40E_QINT_RQCTL_INTEVENT_MASK);
5032 
5033 		val |= (I40E_QINT_RQCTL_ITR_INDX_MASK |
5034 			I40E_QINT_RQCTL_NEXTQ_INDX_MASK);
5035 
5036 		wr32(hw, I40E_QINT_RQCTL(qp), val);
5037 
5038 		val = rd32(hw, I40E_QINT_TQCTL(qp));
5039 
5040 		val &= ~(I40E_QINT_TQCTL_MSIX_INDX_MASK  |
5041 			 I40E_QINT_TQCTL_MSIX0_INDX_MASK |
5042 			 I40E_QINT_TQCTL_CAUSE_ENA_MASK  |
5043 			 I40E_QINT_TQCTL_INTEVENT_MASK);
5044 
5045 		val |= (I40E_QINT_TQCTL_ITR_INDX_MASK |
5046 			I40E_QINT_TQCTL_NEXTQ_INDX_MASK);
5047 
5048 		wr32(hw, I40E_QINT_TQCTL(qp), val);
5049 	}
5050 }
5051 
5052 /**
5053  * i40e_free_q_vector - Free memory allocated for specific interrupt vector
5054  * @vsi: the VSI being configured
5055  * @v_idx: Index of vector to be freed
5056  *
5057  * This function frees the memory allocated to the q_vector.  In addition if
5058  * NAPI is enabled it will delete any references to the NAPI struct prior
5059  * to freeing the q_vector.
5060  **/
5061 static void i40e_free_q_vector(struct i40e_vsi *vsi, int v_idx)
5062 {
5063 	struct i40e_q_vector *q_vector = vsi->q_vectors[v_idx];
5064 	struct i40e_ring *ring;
5065 
5066 	if (!q_vector)
5067 		return;
5068 
5069 	/* disassociate q_vector from rings */
5070 	i40e_for_each_ring(ring, q_vector->tx)
5071 		ring->q_vector = NULL;
5072 
5073 	i40e_for_each_ring(ring, q_vector->rx)
5074 		ring->q_vector = NULL;
5075 
5076 	/* only VSI w/ an associated netdev is set up w/ NAPI */
5077 	if (vsi->netdev)
5078 		netif_napi_del(&q_vector->napi);
5079 
5080 	vsi->q_vectors[v_idx] = NULL;
5081 
5082 	kfree_rcu(q_vector, rcu);
5083 }
5084 
5085 /**
5086  * i40e_vsi_free_q_vectors - Free memory allocated for interrupt vectors
5087  * @vsi: the VSI being un-configured
5088  *
5089  * This frees the memory allocated to the q_vectors and
5090  * deletes references to the NAPI struct.
5091  **/
5092 static void i40e_vsi_free_q_vectors(struct i40e_vsi *vsi)
5093 {
5094 	int v_idx;
5095 
5096 	for (v_idx = 0; v_idx < vsi->num_q_vectors; v_idx++)
5097 		i40e_free_q_vector(vsi, v_idx);
5098 }
5099 
5100 /**
5101  * i40e_reset_interrupt_capability - Disable interrupt setup in OS
5102  * @pf: board private structure
5103  **/
5104 static void i40e_reset_interrupt_capability(struct i40e_pf *pf)
5105 {
5106 	/* If we're in Legacy mode, the interrupt was cleaned in vsi_close */
5107 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
5108 		pci_disable_msix(pf->pdev);
5109 		kfree(pf->msix_entries);
5110 		pf->msix_entries = NULL;
5111 		kfree(pf->irq_pile);
5112 		pf->irq_pile = NULL;
5113 	} else if (pf->flags & I40E_FLAG_MSI_ENABLED) {
5114 		pci_disable_msi(pf->pdev);
5115 	}
5116 	pf->flags &= ~(I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
5117 }
5118 
5119 /**
5120  * i40e_clear_interrupt_scheme - Clear the current interrupt scheme settings
5121  * @pf: board private structure
5122  *
5123  * We go through and clear interrupt specific resources and reset the structure
5124  * to pre-load conditions
5125  **/
5126 static void i40e_clear_interrupt_scheme(struct i40e_pf *pf)
5127 {
5128 	int i;
5129 
5130 	if (test_bit(__I40E_MISC_IRQ_REQUESTED, pf->state))
5131 		i40e_free_misc_vector(pf);
5132 
5133 	i40e_put_lump(pf->irq_pile, pf->iwarp_base_vector,
5134 		      I40E_IWARP_IRQ_PILE_ID);
5135 
5136 	i40e_put_lump(pf->irq_pile, 0, I40E_PILE_VALID_BIT-1);
5137 	for (i = 0; i < pf->num_alloc_vsi; i++)
5138 		if (pf->vsi[i])
5139 			i40e_vsi_free_q_vectors(pf->vsi[i]);
5140 	i40e_reset_interrupt_capability(pf);
5141 }
5142 
5143 /**
5144  * i40e_napi_enable_all - Enable NAPI for all q_vectors in the VSI
5145  * @vsi: the VSI being configured
5146  **/
5147 static void i40e_napi_enable_all(struct i40e_vsi *vsi)
5148 {
5149 	int q_idx;
5150 
5151 	if (!vsi->netdev)
5152 		return;
5153 
5154 	for (q_idx = 0; q_idx < vsi->num_q_vectors; q_idx++) {
5155 		struct i40e_q_vector *q_vector = vsi->q_vectors[q_idx];
5156 
5157 		if (q_vector->rx.ring || q_vector->tx.ring)
5158 			napi_enable(&q_vector->napi);
5159 	}
5160 }
5161 
5162 /**
5163  * i40e_napi_disable_all - Disable NAPI for all q_vectors in the VSI
5164  * @vsi: the VSI being configured
5165  **/
5166 static void i40e_napi_disable_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_disable(&q_vector->napi);
5178 	}
5179 }
5180 
5181 /**
5182  * i40e_vsi_close - Shut down a VSI
5183  * @vsi: the vsi to be quelled
5184  **/
5185 static void i40e_vsi_close(struct i40e_vsi *vsi)
5186 {
5187 	struct i40e_pf *pf = vsi->back;
5188 	if (!test_and_set_bit(__I40E_VSI_DOWN, vsi->state))
5189 		i40e_down(vsi);
5190 	i40e_vsi_free_irq(vsi);
5191 	i40e_vsi_free_tx_resources(vsi);
5192 	i40e_vsi_free_rx_resources(vsi);
5193 	vsi->current_netdev_flags = 0;
5194 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
5195 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
5196 		set_bit(__I40E_CLIENT_RESET, pf->state);
5197 }
5198 
5199 /**
5200  * i40e_quiesce_vsi - Pause a given VSI
5201  * @vsi: the VSI being paused
5202  **/
5203 static void i40e_quiesce_vsi(struct i40e_vsi *vsi)
5204 {
5205 	if (test_bit(__I40E_VSI_DOWN, vsi->state))
5206 		return;
5207 
5208 	set_bit(__I40E_VSI_NEEDS_RESTART, vsi->state);
5209 	if (vsi->netdev && netif_running(vsi->netdev))
5210 		vsi->netdev->netdev_ops->ndo_stop(vsi->netdev);
5211 	else
5212 		i40e_vsi_close(vsi);
5213 }
5214 
5215 /**
5216  * i40e_unquiesce_vsi - Resume a given VSI
5217  * @vsi: the VSI being resumed
5218  **/
5219 static void i40e_unquiesce_vsi(struct i40e_vsi *vsi)
5220 {
5221 	if (!test_and_clear_bit(__I40E_VSI_NEEDS_RESTART, vsi->state))
5222 		return;
5223 
5224 	if (vsi->netdev && netif_running(vsi->netdev))
5225 		vsi->netdev->netdev_ops->ndo_open(vsi->netdev);
5226 	else
5227 		i40e_vsi_open(vsi);   /* this clears the DOWN bit */
5228 }
5229 
5230 /**
5231  * i40e_pf_quiesce_all_vsi - Pause all VSIs on a PF
5232  * @pf: the PF
5233  **/
5234 static void i40e_pf_quiesce_all_vsi(struct i40e_pf *pf)
5235 {
5236 	int v;
5237 
5238 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5239 		if (pf->vsi[v])
5240 			i40e_quiesce_vsi(pf->vsi[v]);
5241 	}
5242 }
5243 
5244 /**
5245  * i40e_pf_unquiesce_all_vsi - Resume all VSIs on a PF
5246  * @pf: the PF
5247  **/
5248 static void i40e_pf_unquiesce_all_vsi(struct i40e_pf *pf)
5249 {
5250 	int v;
5251 
5252 	for (v = 0; v < pf->num_alloc_vsi; v++) {
5253 		if (pf->vsi[v])
5254 			i40e_unquiesce_vsi(pf->vsi[v]);
5255 	}
5256 }
5257 
5258 /**
5259  * i40e_vsi_wait_queues_disabled - Wait for VSI's queues to be disabled
5260  * @vsi: the VSI being configured
5261  *
5262  * Wait until all queues on a given VSI have been disabled.
5263  **/
5264 int i40e_vsi_wait_queues_disabled(struct i40e_vsi *vsi)
5265 {
5266 	struct i40e_pf *pf = vsi->back;
5267 	int i, pf_q, ret;
5268 
5269 	pf_q = vsi->base_queue;
5270 	for (i = 0; i < vsi->num_queue_pairs; i++, pf_q++) {
5271 		/* Check and wait for the Tx queue */
5272 		ret = i40e_pf_txq_wait(pf, pf_q, false);
5273 		if (ret) {
5274 			dev_info(&pf->pdev->dev,
5275 				 "VSI seid %d Tx ring %d disable timeout\n",
5276 				 vsi->seid, pf_q);
5277 			return ret;
5278 		}
5279 
5280 		if (!i40e_enabled_xdp_vsi(vsi))
5281 			goto wait_rx;
5282 
5283 		/* Check and wait for the XDP Tx queue */
5284 		ret = i40e_pf_txq_wait(pf, pf_q + vsi->alloc_queue_pairs,
5285 				       false);
5286 		if (ret) {
5287 			dev_info(&pf->pdev->dev,
5288 				 "VSI seid %d XDP Tx ring %d disable timeout\n",
5289 				 vsi->seid, pf_q);
5290 			return ret;
5291 		}
5292 wait_rx:
5293 		/* Check and wait for the Rx queue */
5294 		ret = i40e_pf_rxq_wait(pf, pf_q, false);
5295 		if (ret) {
5296 			dev_info(&pf->pdev->dev,
5297 				 "VSI seid %d Rx ring %d disable timeout\n",
5298 				 vsi->seid, pf_q);
5299 			return ret;
5300 		}
5301 	}
5302 
5303 	return 0;
5304 }
5305 
5306 #ifdef CONFIG_I40E_DCB
5307 /**
5308  * i40e_pf_wait_queues_disabled - Wait for all queues of PF VSIs to be disabled
5309  * @pf: the PF
5310  *
5311  * This function waits for the queues to be in disabled state for all the
5312  * VSIs that are managed by this PF.
5313  **/
5314 static int i40e_pf_wait_queues_disabled(struct i40e_pf *pf)
5315 {
5316 	int v, ret = 0;
5317 
5318 	for (v = 0; v < pf->hw.func_caps.num_vsis; v++) {
5319 		if (pf->vsi[v]) {
5320 			ret = i40e_vsi_wait_queues_disabled(pf->vsi[v]);
5321 			if (ret)
5322 				break;
5323 		}
5324 	}
5325 
5326 	return ret;
5327 }
5328 
5329 #endif
5330 
5331 /**
5332  * i40e_get_iscsi_tc_map - Return TC map for iSCSI APP
5333  * @pf: pointer to PF
5334  *
5335  * Get TC map for ISCSI PF type that will include iSCSI TC
5336  * and LAN TC.
5337  **/
5338 static u8 i40e_get_iscsi_tc_map(struct i40e_pf *pf)
5339 {
5340 	struct i40e_dcb_app_priority_table app;
5341 	struct i40e_hw *hw = &pf->hw;
5342 	u8 enabled_tc = 1; /* TC0 is always enabled */
5343 	u8 tc, i;
5344 	/* Get the iSCSI APP TLV */
5345 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5346 
5347 	for (i = 0; i < dcbcfg->numapps; i++) {
5348 		app = dcbcfg->app[i];
5349 		if (app.selector == I40E_APP_SEL_TCPIP &&
5350 		    app.protocolid == I40E_APP_PROTOID_ISCSI) {
5351 			tc = dcbcfg->etscfg.prioritytable[app.priority];
5352 			enabled_tc |= BIT(tc);
5353 			break;
5354 		}
5355 	}
5356 
5357 	return enabled_tc;
5358 }
5359 
5360 /**
5361  * i40e_dcb_get_num_tc -  Get the number of TCs from DCBx config
5362  * @dcbcfg: the corresponding DCBx configuration structure
5363  *
5364  * Return the number of TCs from given DCBx configuration
5365  **/
5366 static u8 i40e_dcb_get_num_tc(struct i40e_dcbx_config *dcbcfg)
5367 {
5368 	int i, tc_unused = 0;
5369 	u8 num_tc = 0;
5370 	u8 ret = 0;
5371 
5372 	/* Scan the ETS Config Priority Table to find
5373 	 * traffic class enabled for a given priority
5374 	 * and create a bitmask of enabled TCs
5375 	 */
5376 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++)
5377 		num_tc |= BIT(dcbcfg->etscfg.prioritytable[i]);
5378 
5379 	/* Now scan the bitmask to check for
5380 	 * contiguous TCs starting with TC0
5381 	 */
5382 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5383 		if (num_tc & BIT(i)) {
5384 			if (!tc_unused) {
5385 				ret++;
5386 			} else {
5387 				pr_err("Non-contiguous TC - Disabling DCB\n");
5388 				return 1;
5389 			}
5390 		} else {
5391 			tc_unused = 1;
5392 		}
5393 	}
5394 
5395 	/* There is always at least TC0 */
5396 	if (!ret)
5397 		ret = 1;
5398 
5399 	return ret;
5400 }
5401 
5402 /**
5403  * i40e_dcb_get_enabled_tc - Get enabled traffic classes
5404  * @dcbcfg: the corresponding DCBx configuration structure
5405  *
5406  * Query the current DCB configuration and return the number of
5407  * traffic classes enabled from the given DCBX config
5408  **/
5409 static u8 i40e_dcb_get_enabled_tc(struct i40e_dcbx_config *dcbcfg)
5410 {
5411 	u8 num_tc = i40e_dcb_get_num_tc(dcbcfg);
5412 	u8 enabled_tc = 1;
5413 	u8 i;
5414 
5415 	for (i = 0; i < num_tc; i++)
5416 		enabled_tc |= BIT(i);
5417 
5418 	return enabled_tc;
5419 }
5420 
5421 /**
5422  * i40e_mqprio_get_enabled_tc - Get enabled traffic classes
5423  * @pf: PF being queried
5424  *
5425  * Query the current MQPRIO configuration and return the number of
5426  * traffic classes enabled.
5427  **/
5428 static u8 i40e_mqprio_get_enabled_tc(struct i40e_pf *pf)
5429 {
5430 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
5431 	u8 num_tc = vsi->mqprio_qopt.qopt.num_tc;
5432 	u8 enabled_tc = 1, i;
5433 
5434 	for (i = 1; i < num_tc; i++)
5435 		enabled_tc |= BIT(i);
5436 	return enabled_tc;
5437 }
5438 
5439 /**
5440  * i40e_pf_get_num_tc - Get enabled traffic classes for PF
5441  * @pf: PF being queried
5442  *
5443  * Return number of traffic classes enabled for the given PF
5444  **/
5445 static u8 i40e_pf_get_num_tc(struct i40e_pf *pf)
5446 {
5447 	struct i40e_hw *hw = &pf->hw;
5448 	u8 i, enabled_tc = 1;
5449 	u8 num_tc = 0;
5450 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5451 
5452 	if (i40e_is_tc_mqprio_enabled(pf))
5453 		return pf->vsi[pf->lan_vsi]->mqprio_qopt.qopt.num_tc;
5454 
5455 	/* If neither MQPRIO nor DCB is enabled, then always use single TC */
5456 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5457 		return 1;
5458 
5459 	/* SFP mode will be enabled for all TCs on port */
5460 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5461 		return i40e_dcb_get_num_tc(dcbcfg);
5462 
5463 	/* MFP mode return count of enabled TCs for this PF */
5464 	if (pf->hw.func_caps.iscsi)
5465 		enabled_tc =  i40e_get_iscsi_tc_map(pf);
5466 	else
5467 		return 1; /* Only TC0 */
5468 
5469 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5470 		if (enabled_tc & BIT(i))
5471 			num_tc++;
5472 	}
5473 	return num_tc;
5474 }
5475 
5476 /**
5477  * i40e_pf_get_tc_map - Get bitmap for enabled traffic classes
5478  * @pf: PF being queried
5479  *
5480  * Return a bitmap for enabled traffic classes for this PF.
5481  **/
5482 static u8 i40e_pf_get_tc_map(struct i40e_pf *pf)
5483 {
5484 	if (i40e_is_tc_mqprio_enabled(pf))
5485 		return i40e_mqprio_get_enabled_tc(pf);
5486 
5487 	/* If neither MQPRIO nor DCB is enabled for this PF then just return
5488 	 * default TC
5489 	 */
5490 	if (!(pf->flags & I40E_FLAG_DCB_ENABLED))
5491 		return I40E_DEFAULT_TRAFFIC_CLASS;
5492 
5493 	/* SFP mode we want PF to be enabled for all TCs */
5494 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
5495 		return i40e_dcb_get_enabled_tc(&pf->hw.local_dcbx_config);
5496 
5497 	/* MFP enabled and iSCSI PF type */
5498 	if (pf->hw.func_caps.iscsi)
5499 		return i40e_get_iscsi_tc_map(pf);
5500 	else
5501 		return I40E_DEFAULT_TRAFFIC_CLASS;
5502 }
5503 
5504 /**
5505  * i40e_vsi_get_bw_info - Query VSI BW Information
5506  * @vsi: the VSI being queried
5507  *
5508  * Returns 0 on success, negative value on failure
5509  **/
5510 static int i40e_vsi_get_bw_info(struct i40e_vsi *vsi)
5511 {
5512 	struct i40e_aqc_query_vsi_ets_sla_config_resp bw_ets_config = {0};
5513 	struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5514 	struct i40e_pf *pf = vsi->back;
5515 	struct i40e_hw *hw = &pf->hw;
5516 	i40e_status ret;
5517 	u32 tc_bw_max;
5518 	int i;
5519 
5520 	/* Get the VSI level BW configuration */
5521 	ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid, &bw_config, NULL);
5522 	if (ret) {
5523 		dev_info(&pf->pdev->dev,
5524 			 "couldn't get PF vsi bw config, err %s aq_err %s\n",
5525 			 i40e_stat_str(&pf->hw, ret),
5526 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5527 		return -EINVAL;
5528 	}
5529 
5530 	/* Get the VSI level BW configuration per TC */
5531 	ret = i40e_aq_query_vsi_ets_sla_config(hw, vsi->seid, &bw_ets_config,
5532 					       NULL);
5533 	if (ret) {
5534 		dev_info(&pf->pdev->dev,
5535 			 "couldn't get PF vsi ets bw config, err %s aq_err %s\n",
5536 			 i40e_stat_str(&pf->hw, ret),
5537 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5538 		return -EINVAL;
5539 	}
5540 
5541 	if (bw_config.tc_valid_bits != bw_ets_config.tc_valid_bits) {
5542 		dev_info(&pf->pdev->dev,
5543 			 "Enabled TCs mismatch from querying VSI BW info 0x%08x 0x%08x\n",
5544 			 bw_config.tc_valid_bits,
5545 			 bw_ets_config.tc_valid_bits);
5546 		/* Still continuing */
5547 	}
5548 
5549 	vsi->bw_limit = le16_to_cpu(bw_config.port_bw_limit);
5550 	vsi->bw_max_quanta = bw_config.max_bw;
5551 	tc_bw_max = le16_to_cpu(bw_ets_config.tc_bw_max[0]) |
5552 		    (le16_to_cpu(bw_ets_config.tc_bw_max[1]) << 16);
5553 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5554 		vsi->bw_ets_share_credits[i] = bw_ets_config.share_credits[i];
5555 		vsi->bw_ets_limit_credits[i] =
5556 					le16_to_cpu(bw_ets_config.credits[i]);
5557 		/* 3 bits out of 4 for each TC */
5558 		vsi->bw_ets_max_quanta[i] = (u8)((tc_bw_max >> (i*4)) & 0x7);
5559 	}
5560 
5561 	return 0;
5562 }
5563 
5564 /**
5565  * i40e_vsi_configure_bw_alloc - Configure VSI BW allocation per TC
5566  * @vsi: the VSI being configured
5567  * @enabled_tc: TC bitmap
5568  * @bw_share: BW shared credits per TC
5569  *
5570  * Returns 0 on success, negative value on failure
5571  **/
5572 static int i40e_vsi_configure_bw_alloc(struct i40e_vsi *vsi, u8 enabled_tc,
5573 				       u8 *bw_share)
5574 {
5575 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
5576 	struct i40e_pf *pf = vsi->back;
5577 	i40e_status ret;
5578 	int i;
5579 
5580 	/* There is no need to reset BW when mqprio mode is on.  */
5581 	if (i40e_is_tc_mqprio_enabled(pf))
5582 		return 0;
5583 	if (!vsi->mqprio_qopt.qopt.hw && !(pf->flags & I40E_FLAG_DCB_ENABLED)) {
5584 		ret = i40e_set_bw_limit(vsi, vsi->seid, 0);
5585 		if (ret)
5586 			dev_info(&pf->pdev->dev,
5587 				 "Failed to reset tx rate for vsi->seid %u\n",
5588 				 vsi->seid);
5589 		return ret;
5590 	}
5591 	memset(&bw_data, 0, sizeof(bw_data));
5592 	bw_data.tc_valid_bits = enabled_tc;
5593 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5594 		bw_data.tc_bw_credits[i] = bw_share[i];
5595 
5596 	ret = i40e_aq_config_vsi_tc_bw(&pf->hw, vsi->seid, &bw_data, NULL);
5597 	if (ret) {
5598 		dev_info(&pf->pdev->dev,
5599 			 "AQ command Config VSI BW allocation per TC failed = %d\n",
5600 			 pf->hw.aq.asq_last_status);
5601 		return -EINVAL;
5602 	}
5603 
5604 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
5605 		vsi->info.qs_handle[i] = bw_data.qs_handles[i];
5606 
5607 	return 0;
5608 }
5609 
5610 /**
5611  * i40e_vsi_config_netdev_tc - Setup the netdev TC configuration
5612  * @vsi: the VSI being configured
5613  * @enabled_tc: TC map to be enabled
5614  *
5615  **/
5616 static void i40e_vsi_config_netdev_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5617 {
5618 	struct net_device *netdev = vsi->netdev;
5619 	struct i40e_pf *pf = vsi->back;
5620 	struct i40e_hw *hw = &pf->hw;
5621 	u8 netdev_tc = 0;
5622 	int i;
5623 	struct i40e_dcbx_config *dcbcfg = &hw->local_dcbx_config;
5624 
5625 	if (!netdev)
5626 		return;
5627 
5628 	if (!enabled_tc) {
5629 		netdev_reset_tc(netdev);
5630 		return;
5631 	}
5632 
5633 	/* Set up actual enabled TCs on the VSI */
5634 	if (netdev_set_num_tc(netdev, vsi->tc_config.numtc))
5635 		return;
5636 
5637 	/* set per TC queues for the VSI */
5638 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5639 		/* Only set TC queues for enabled tcs
5640 		 *
5641 		 * e.g. For a VSI that has TC0 and TC3 enabled the
5642 		 * enabled_tc bitmap would be 0x00001001; the driver
5643 		 * will set the numtc for netdev as 2 that will be
5644 		 * referenced by the netdev layer as TC 0 and 1.
5645 		 */
5646 		if (vsi->tc_config.enabled_tc & BIT(i))
5647 			netdev_set_tc_queue(netdev,
5648 					vsi->tc_config.tc_info[i].netdev_tc,
5649 					vsi->tc_config.tc_info[i].qcount,
5650 					vsi->tc_config.tc_info[i].qoffset);
5651 	}
5652 
5653 	if (i40e_is_tc_mqprio_enabled(pf))
5654 		return;
5655 
5656 	/* Assign UP2TC map for the VSI */
5657 	for (i = 0; i < I40E_MAX_USER_PRIORITY; i++) {
5658 		/* Get the actual TC# for the UP */
5659 		u8 ets_tc = dcbcfg->etscfg.prioritytable[i];
5660 		/* Get the mapped netdev TC# for the UP */
5661 		netdev_tc =  vsi->tc_config.tc_info[ets_tc].netdev_tc;
5662 		netdev_set_prio_tc_map(netdev, i, netdev_tc);
5663 	}
5664 }
5665 
5666 /**
5667  * i40e_vsi_update_queue_map - Update our copy of VSi info with new queue map
5668  * @vsi: the VSI being configured
5669  * @ctxt: the ctxt buffer returned from AQ VSI update param command
5670  **/
5671 static void i40e_vsi_update_queue_map(struct i40e_vsi *vsi,
5672 				      struct i40e_vsi_context *ctxt)
5673 {
5674 	/* copy just the sections touched not the entire info
5675 	 * since not all sections are valid as returned by
5676 	 * update vsi params
5677 	 */
5678 	vsi->info.mapping_flags = ctxt->info.mapping_flags;
5679 	memcpy(&vsi->info.queue_mapping,
5680 	       &ctxt->info.queue_mapping, sizeof(vsi->info.queue_mapping));
5681 	memcpy(&vsi->info.tc_mapping, ctxt->info.tc_mapping,
5682 	       sizeof(vsi->info.tc_mapping));
5683 }
5684 
5685 /**
5686  * i40e_update_adq_vsi_queues - update queue mapping for ADq VSI
5687  * @vsi: the VSI being reconfigured
5688  * @vsi_offset: offset from main VF VSI
5689  */
5690 int i40e_update_adq_vsi_queues(struct i40e_vsi *vsi, int vsi_offset)
5691 {
5692 	struct i40e_vsi_context ctxt = {};
5693 	struct i40e_pf *pf;
5694 	struct i40e_hw *hw;
5695 	int ret;
5696 
5697 	if (!vsi)
5698 		return I40E_ERR_PARAM;
5699 	pf = vsi->back;
5700 	hw = &pf->hw;
5701 
5702 	ctxt.seid = vsi->seid;
5703 	ctxt.pf_num = hw->pf_id;
5704 	ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id + vsi_offset;
5705 	ctxt.uplink_seid = vsi->uplink_seid;
5706 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
5707 	ctxt.flags = I40E_AQ_VSI_TYPE_VF;
5708 	ctxt.info = vsi->info;
5709 
5710 	i40e_vsi_setup_queue_map(vsi, &ctxt, vsi->tc_config.enabled_tc,
5711 				 false);
5712 	if (vsi->reconfig_rss) {
5713 		vsi->rss_size = min_t(int, pf->alloc_rss_size,
5714 				      vsi->num_queue_pairs);
5715 		ret = i40e_vsi_config_rss(vsi);
5716 		if (ret) {
5717 			dev_info(&pf->pdev->dev, "Failed to reconfig rss for num_queues\n");
5718 			return ret;
5719 		}
5720 		vsi->reconfig_rss = false;
5721 	}
5722 
5723 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5724 	if (ret) {
5725 		dev_info(&pf->pdev->dev, "Update vsi config failed, err %s aq_err %s\n",
5726 			 i40e_stat_str(hw, ret),
5727 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5728 		return ret;
5729 	}
5730 	/* update the local VSI info with updated queue map */
5731 	i40e_vsi_update_queue_map(vsi, &ctxt);
5732 	vsi->info.valid_sections = 0;
5733 
5734 	return ret;
5735 }
5736 
5737 /**
5738  * i40e_vsi_config_tc - Configure VSI Tx Scheduler for given TC map
5739  * @vsi: VSI to be configured
5740  * @enabled_tc: TC bitmap
5741  *
5742  * This configures a particular VSI for TCs that are mapped to the
5743  * given TC bitmap. It uses default bandwidth share for TCs across
5744  * VSIs to configure TC for a particular VSI.
5745  *
5746  * NOTE:
5747  * It is expected that the VSI queues have been quisced before calling
5748  * this function.
5749  **/
5750 static int i40e_vsi_config_tc(struct i40e_vsi *vsi, u8 enabled_tc)
5751 {
5752 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
5753 	struct i40e_pf *pf = vsi->back;
5754 	struct i40e_hw *hw = &pf->hw;
5755 	struct i40e_vsi_context ctxt;
5756 	int ret = 0;
5757 	int i;
5758 
5759 	/* Check if enabled_tc is same as existing or new TCs */
5760 	if (vsi->tc_config.enabled_tc == enabled_tc &&
5761 	    vsi->mqprio_qopt.mode != TC_MQPRIO_MODE_CHANNEL)
5762 		return ret;
5763 
5764 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
5765 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
5766 		if (enabled_tc & BIT(i))
5767 			bw_share[i] = 1;
5768 	}
5769 
5770 	ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5771 	if (ret) {
5772 		struct i40e_aqc_query_vsi_bw_config_resp bw_config = {0};
5773 
5774 		dev_info(&pf->pdev->dev,
5775 			 "Failed configuring TC map %d for VSI %d\n",
5776 			 enabled_tc, vsi->seid);
5777 		ret = i40e_aq_query_vsi_bw_config(hw, vsi->seid,
5778 						  &bw_config, NULL);
5779 		if (ret) {
5780 			dev_info(&pf->pdev->dev,
5781 				 "Failed querying vsi bw info, err %s aq_err %s\n",
5782 				 i40e_stat_str(hw, ret),
5783 				 i40e_aq_str(hw, hw->aq.asq_last_status));
5784 			goto out;
5785 		}
5786 		if ((bw_config.tc_valid_bits & enabled_tc) != enabled_tc) {
5787 			u8 valid_tc = bw_config.tc_valid_bits & enabled_tc;
5788 
5789 			if (!valid_tc)
5790 				valid_tc = bw_config.tc_valid_bits;
5791 			/* Always enable TC0, no matter what */
5792 			valid_tc |= 1;
5793 			dev_info(&pf->pdev->dev,
5794 				 "Requested tc 0x%x, but FW reports 0x%x as valid. Attempting to use 0x%x.\n",
5795 				 enabled_tc, bw_config.tc_valid_bits, valid_tc);
5796 			enabled_tc = valid_tc;
5797 		}
5798 
5799 		ret = i40e_vsi_configure_bw_alloc(vsi, enabled_tc, bw_share);
5800 		if (ret) {
5801 			dev_err(&pf->pdev->dev,
5802 				"Unable to  configure TC map %d for VSI %d\n",
5803 				enabled_tc, vsi->seid);
5804 			goto out;
5805 		}
5806 	}
5807 
5808 	/* Update Queue Pairs Mapping for currently enabled UPs */
5809 	ctxt.seid = vsi->seid;
5810 	ctxt.pf_num = vsi->back->hw.pf_id;
5811 	ctxt.vf_num = 0;
5812 	ctxt.uplink_seid = vsi->uplink_seid;
5813 	ctxt.info = vsi->info;
5814 	if (i40e_is_tc_mqprio_enabled(pf)) {
5815 		ret = i40e_vsi_setup_queue_map_mqprio(vsi, &ctxt, enabled_tc);
5816 		if (ret)
5817 			goto out;
5818 	} else {
5819 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
5820 	}
5821 
5822 	/* On destroying the qdisc, reset vsi->rss_size, as number of enabled
5823 	 * queues changed.
5824 	 */
5825 	if (!vsi->mqprio_qopt.qopt.hw && vsi->reconfig_rss) {
5826 		vsi->rss_size = min_t(int, vsi->back->alloc_rss_size,
5827 				      vsi->num_queue_pairs);
5828 		ret = i40e_vsi_config_rss(vsi);
5829 		if (ret) {
5830 			dev_info(&vsi->back->pdev->dev,
5831 				 "Failed to reconfig rss for num_queues\n");
5832 			return ret;
5833 		}
5834 		vsi->reconfig_rss = false;
5835 	}
5836 	if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
5837 		ctxt.info.valid_sections |=
5838 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
5839 		ctxt.info.queueing_opt_flags |= I40E_AQ_VSI_QUE_OPT_TCP_ENA;
5840 	}
5841 
5842 	/* Update the VSI after updating the VSI queue-mapping
5843 	 * information
5844 	 */
5845 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
5846 	if (ret) {
5847 		dev_info(&pf->pdev->dev,
5848 			 "Update vsi tc config failed, err %s aq_err %s\n",
5849 			 i40e_stat_str(hw, ret),
5850 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5851 		goto out;
5852 	}
5853 	/* update the local VSI info with updated queue map */
5854 	i40e_vsi_update_queue_map(vsi, &ctxt);
5855 	vsi->info.valid_sections = 0;
5856 
5857 	/* Update current VSI BW information */
5858 	ret = i40e_vsi_get_bw_info(vsi);
5859 	if (ret) {
5860 		dev_info(&pf->pdev->dev,
5861 			 "Failed updating vsi bw info, err %s aq_err %s\n",
5862 			 i40e_stat_str(hw, ret),
5863 			 i40e_aq_str(hw, hw->aq.asq_last_status));
5864 		goto out;
5865 	}
5866 
5867 	/* Update the netdev TC setup */
5868 	i40e_vsi_config_netdev_tc(vsi, enabled_tc);
5869 out:
5870 	return ret;
5871 }
5872 
5873 /**
5874  * i40e_get_link_speed - Returns link speed for the interface
5875  * @vsi: VSI to be configured
5876  *
5877  **/
5878 static int i40e_get_link_speed(struct i40e_vsi *vsi)
5879 {
5880 	struct i40e_pf *pf = vsi->back;
5881 
5882 	switch (pf->hw.phy.link_info.link_speed) {
5883 	case I40E_LINK_SPEED_40GB:
5884 		return 40000;
5885 	case I40E_LINK_SPEED_25GB:
5886 		return 25000;
5887 	case I40E_LINK_SPEED_20GB:
5888 		return 20000;
5889 	case I40E_LINK_SPEED_10GB:
5890 		return 10000;
5891 	case I40E_LINK_SPEED_1GB:
5892 		return 1000;
5893 	default:
5894 		return -EINVAL;
5895 	}
5896 }
5897 
5898 /**
5899  * i40e_bw_bytes_to_mbits - Convert max_tx_rate from bytes to mbits
5900  * @vsi: Pointer to vsi structure
5901  * @max_tx_rate: max TX rate in bytes to be converted into Mbits
5902  *
5903  * Helper function to convert units before send to set BW limit
5904  **/
5905 static u64 i40e_bw_bytes_to_mbits(struct i40e_vsi *vsi, u64 max_tx_rate)
5906 {
5907 	if (max_tx_rate < I40E_BW_MBPS_DIVISOR) {
5908 		dev_warn(&vsi->back->pdev->dev,
5909 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5910 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5911 	} else {
5912 		do_div(max_tx_rate, I40E_BW_MBPS_DIVISOR);
5913 	}
5914 
5915 	return max_tx_rate;
5916 }
5917 
5918 /**
5919  * i40e_set_bw_limit - setup BW limit for Tx traffic based on max_tx_rate
5920  * @vsi: VSI to be configured
5921  * @seid: seid of the channel/VSI
5922  * @max_tx_rate: max TX rate to be configured as BW limit
5923  *
5924  * Helper function to set BW limit for a given VSI
5925  **/
5926 int i40e_set_bw_limit(struct i40e_vsi *vsi, u16 seid, u64 max_tx_rate)
5927 {
5928 	struct i40e_pf *pf = vsi->back;
5929 	u64 credits = 0;
5930 	int speed = 0;
5931 	int ret = 0;
5932 
5933 	speed = i40e_get_link_speed(vsi);
5934 	if (max_tx_rate > speed) {
5935 		dev_err(&pf->pdev->dev,
5936 			"Invalid max tx rate %llu specified for VSI seid %d.",
5937 			max_tx_rate, seid);
5938 		return -EINVAL;
5939 	}
5940 	if (max_tx_rate && max_tx_rate < I40E_BW_CREDIT_DIVISOR) {
5941 		dev_warn(&pf->pdev->dev,
5942 			 "Setting max tx rate to minimum usable value of 50Mbps.\n");
5943 		max_tx_rate = I40E_BW_CREDIT_DIVISOR;
5944 	}
5945 
5946 	/* Tx rate credits are in values of 50Mbps, 0 is disabled */
5947 	credits = max_tx_rate;
5948 	do_div(credits, I40E_BW_CREDIT_DIVISOR);
5949 	ret = i40e_aq_config_vsi_bw_limit(&pf->hw, seid, credits,
5950 					  I40E_MAX_BW_INACTIVE_ACCUM, NULL);
5951 	if (ret)
5952 		dev_err(&pf->pdev->dev,
5953 			"Failed set tx rate (%llu Mbps) for vsi->seid %u, err %s aq_err %s\n",
5954 			max_tx_rate, seid, i40e_stat_str(&pf->hw, ret),
5955 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
5956 	return ret;
5957 }
5958 
5959 /**
5960  * i40e_remove_queue_channels - Remove queue channels for the TCs
5961  * @vsi: VSI to be configured
5962  *
5963  * Remove queue channels for the TCs
5964  **/
5965 static void i40e_remove_queue_channels(struct i40e_vsi *vsi)
5966 {
5967 	enum i40e_admin_queue_err last_aq_status;
5968 	struct i40e_cloud_filter *cfilter;
5969 	struct i40e_channel *ch, *ch_tmp;
5970 	struct i40e_pf *pf = vsi->back;
5971 	struct hlist_node *node;
5972 	int ret, i;
5973 
5974 	/* Reset rss size that was stored when reconfiguring rss for
5975 	 * channel VSIs with non-power-of-2 queue count.
5976 	 */
5977 	vsi->current_rss_size = 0;
5978 
5979 	/* perform cleanup for channels if they exist */
5980 	if (list_empty(&vsi->ch_list))
5981 		return;
5982 
5983 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
5984 		struct i40e_vsi *p_vsi;
5985 
5986 		list_del(&ch->list);
5987 		p_vsi = ch->parent_vsi;
5988 		if (!p_vsi || !ch->initialized) {
5989 			kfree(ch);
5990 			continue;
5991 		}
5992 		/* Reset queue contexts */
5993 		for (i = 0; i < ch->num_queue_pairs; i++) {
5994 			struct i40e_ring *tx_ring, *rx_ring;
5995 			u16 pf_q;
5996 
5997 			pf_q = ch->base_queue + i;
5998 			tx_ring = vsi->tx_rings[pf_q];
5999 			tx_ring->ch = NULL;
6000 
6001 			rx_ring = vsi->rx_rings[pf_q];
6002 			rx_ring->ch = NULL;
6003 		}
6004 
6005 		/* Reset BW configured for this VSI via mqprio */
6006 		ret = i40e_set_bw_limit(vsi, ch->seid, 0);
6007 		if (ret)
6008 			dev_info(&vsi->back->pdev->dev,
6009 				 "Failed to reset tx rate for ch->seid %u\n",
6010 				 ch->seid);
6011 
6012 		/* delete cloud filters associated with this channel */
6013 		hlist_for_each_entry_safe(cfilter, node,
6014 					  &pf->cloud_filter_list, cloud_node) {
6015 			if (cfilter->seid != ch->seid)
6016 				continue;
6017 
6018 			hash_del(&cfilter->cloud_node);
6019 			if (cfilter->dst_port)
6020 				ret = i40e_add_del_cloud_filter_big_buf(vsi,
6021 									cfilter,
6022 									false);
6023 			else
6024 				ret = i40e_add_del_cloud_filter(vsi, cfilter,
6025 								false);
6026 			last_aq_status = pf->hw.aq.asq_last_status;
6027 			if (ret)
6028 				dev_info(&pf->pdev->dev,
6029 					 "Failed to delete cloud filter, err %s aq_err %s\n",
6030 					 i40e_stat_str(&pf->hw, ret),
6031 					 i40e_aq_str(&pf->hw, last_aq_status));
6032 			kfree(cfilter);
6033 		}
6034 
6035 		/* delete VSI from FW */
6036 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
6037 					     NULL);
6038 		if (ret)
6039 			dev_err(&vsi->back->pdev->dev,
6040 				"unable to remove channel (%d) for parent VSI(%d)\n",
6041 				ch->seid, p_vsi->seid);
6042 		kfree(ch);
6043 	}
6044 	INIT_LIST_HEAD(&vsi->ch_list);
6045 }
6046 
6047 /**
6048  * i40e_get_max_queues_for_channel
6049  * @vsi: ptr to VSI to which channels are associated with
6050  *
6051  * Helper function which returns max value among the queue counts set on the
6052  * channels/TCs created.
6053  **/
6054 static int i40e_get_max_queues_for_channel(struct i40e_vsi *vsi)
6055 {
6056 	struct i40e_channel *ch, *ch_tmp;
6057 	int max = 0;
6058 
6059 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
6060 		if (!ch->initialized)
6061 			continue;
6062 		if (ch->num_queue_pairs > max)
6063 			max = ch->num_queue_pairs;
6064 	}
6065 
6066 	return max;
6067 }
6068 
6069 /**
6070  * i40e_validate_num_queues - validate num_queues w.r.t channel
6071  * @pf: ptr to PF device
6072  * @num_queues: number of queues
6073  * @vsi: the parent VSI
6074  * @reconfig_rss: indicates should the RSS be reconfigured or not
6075  *
6076  * This function validates number of queues in the context of new channel
6077  * which is being established and determines if RSS should be reconfigured
6078  * or not for parent VSI.
6079  **/
6080 static int i40e_validate_num_queues(struct i40e_pf *pf, int num_queues,
6081 				    struct i40e_vsi *vsi, bool *reconfig_rss)
6082 {
6083 	int max_ch_queues;
6084 
6085 	if (!reconfig_rss)
6086 		return -EINVAL;
6087 
6088 	*reconfig_rss = false;
6089 	if (vsi->current_rss_size) {
6090 		if (num_queues > vsi->current_rss_size) {
6091 			dev_dbg(&pf->pdev->dev,
6092 				"Error: num_queues (%d) > vsi's current_size(%d)\n",
6093 				num_queues, vsi->current_rss_size);
6094 			return -EINVAL;
6095 		} else if ((num_queues < vsi->current_rss_size) &&
6096 			   (!is_power_of_2(num_queues))) {
6097 			dev_dbg(&pf->pdev->dev,
6098 				"Error: num_queues (%d) < vsi's current_size(%d), but not power of 2\n",
6099 				num_queues, vsi->current_rss_size);
6100 			return -EINVAL;
6101 		}
6102 	}
6103 
6104 	if (!is_power_of_2(num_queues)) {
6105 		/* Find the max num_queues configured for channel if channel
6106 		 * exist.
6107 		 * if channel exist, then enforce 'num_queues' to be more than
6108 		 * max ever queues configured for channel.
6109 		 */
6110 		max_ch_queues = i40e_get_max_queues_for_channel(vsi);
6111 		if (num_queues < max_ch_queues) {
6112 			dev_dbg(&pf->pdev->dev,
6113 				"Error: num_queues (%d) < max queues configured for channel(%d)\n",
6114 				num_queues, max_ch_queues);
6115 			return -EINVAL;
6116 		}
6117 		*reconfig_rss = true;
6118 	}
6119 
6120 	return 0;
6121 }
6122 
6123 /**
6124  * i40e_vsi_reconfig_rss - reconfig RSS based on specified rss_size
6125  * @vsi: the VSI being setup
6126  * @rss_size: size of RSS, accordingly LUT gets reprogrammed
6127  *
6128  * This function reconfigures RSS by reprogramming LUTs using 'rss_size'
6129  **/
6130 static int i40e_vsi_reconfig_rss(struct i40e_vsi *vsi, u16 rss_size)
6131 {
6132 	struct i40e_pf *pf = vsi->back;
6133 	u8 seed[I40E_HKEY_ARRAY_SIZE];
6134 	struct i40e_hw *hw = &pf->hw;
6135 	int local_rss_size;
6136 	u8 *lut;
6137 	int ret;
6138 
6139 	if (!vsi->rss_size)
6140 		return -EINVAL;
6141 
6142 	if (rss_size > vsi->rss_size)
6143 		return -EINVAL;
6144 
6145 	local_rss_size = min_t(int, vsi->rss_size, rss_size);
6146 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
6147 	if (!lut)
6148 		return -ENOMEM;
6149 
6150 	/* Ignoring user configured lut if there is one */
6151 	i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, local_rss_size);
6152 
6153 	/* Use user configured hash key if there is one, otherwise
6154 	 * use default.
6155 	 */
6156 	if (vsi->rss_hkey_user)
6157 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
6158 	else
6159 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
6160 
6161 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
6162 	if (ret) {
6163 		dev_info(&pf->pdev->dev,
6164 			 "Cannot set RSS lut, err %s aq_err %s\n",
6165 			 i40e_stat_str(hw, ret),
6166 			 i40e_aq_str(hw, hw->aq.asq_last_status));
6167 		kfree(lut);
6168 		return ret;
6169 	}
6170 	kfree(lut);
6171 
6172 	/* Do the update w.r.t. storing rss_size */
6173 	if (!vsi->orig_rss_size)
6174 		vsi->orig_rss_size = vsi->rss_size;
6175 	vsi->current_rss_size = local_rss_size;
6176 
6177 	return ret;
6178 }
6179 
6180 /**
6181  * i40e_channel_setup_queue_map - Setup a channel queue map
6182  * @pf: ptr to PF device
6183  * @ctxt: VSI context structure
6184  * @ch: ptr to channel structure
6185  *
6186  * Setup queue map for a specific channel
6187  **/
6188 static void i40e_channel_setup_queue_map(struct i40e_pf *pf,
6189 					 struct i40e_vsi_context *ctxt,
6190 					 struct i40e_channel *ch)
6191 {
6192 	u16 qcount, qmap, sections = 0;
6193 	u8 offset = 0;
6194 	int pow;
6195 
6196 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
6197 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
6198 
6199 	qcount = min_t(int, ch->num_queue_pairs, pf->num_lan_msix);
6200 	ch->num_queue_pairs = qcount;
6201 
6202 	/* find the next higher power-of-2 of num queue pairs */
6203 	pow = ilog2(qcount);
6204 	if (!is_power_of_2(qcount))
6205 		pow++;
6206 
6207 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
6208 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
6209 
6210 	/* Setup queue TC[0].qmap for given VSI context */
6211 	ctxt->info.tc_mapping[0] = cpu_to_le16(qmap);
6212 
6213 	ctxt->info.up_enable_bits = 0x1; /* TC0 enabled */
6214 	ctxt->info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
6215 	ctxt->info.queue_mapping[0] = cpu_to_le16(ch->base_queue);
6216 	ctxt->info.valid_sections |= cpu_to_le16(sections);
6217 }
6218 
6219 /**
6220  * i40e_add_channel - add a channel by adding VSI
6221  * @pf: ptr to PF device
6222  * @uplink_seid: underlying HW switching element (VEB) ID
6223  * @ch: ptr to channel structure
6224  *
6225  * Add a channel (VSI) using add_vsi and queue_map
6226  **/
6227 static int i40e_add_channel(struct i40e_pf *pf, u16 uplink_seid,
6228 			    struct i40e_channel *ch)
6229 {
6230 	struct i40e_hw *hw = &pf->hw;
6231 	struct i40e_vsi_context ctxt;
6232 	u8 enabled_tc = 0x1; /* TC0 enabled */
6233 	int ret;
6234 
6235 	if (ch->type != I40E_VSI_VMDQ2) {
6236 		dev_info(&pf->pdev->dev,
6237 			 "add new vsi failed, ch->type %d\n", ch->type);
6238 		return -EINVAL;
6239 	}
6240 
6241 	memset(&ctxt, 0, sizeof(ctxt));
6242 	ctxt.pf_num = hw->pf_id;
6243 	ctxt.vf_num = 0;
6244 	ctxt.uplink_seid = uplink_seid;
6245 	ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
6246 	if (ch->type == I40E_VSI_VMDQ2)
6247 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
6248 
6249 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED) {
6250 		ctxt.info.valid_sections |=
6251 		     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
6252 		ctxt.info.switch_id =
6253 		   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
6254 	}
6255 
6256 	/* Set queue map for a given VSI context */
6257 	i40e_channel_setup_queue_map(pf, &ctxt, ch);
6258 
6259 	/* Now time to create VSI */
6260 	ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
6261 	if (ret) {
6262 		dev_info(&pf->pdev->dev,
6263 			 "add new vsi failed, err %s aq_err %s\n",
6264 			 i40e_stat_str(&pf->hw, ret),
6265 			 i40e_aq_str(&pf->hw,
6266 				     pf->hw.aq.asq_last_status));
6267 		return -ENOENT;
6268 	}
6269 
6270 	/* Success, update channel, set enabled_tc only if the channel
6271 	 * is not a macvlan
6272 	 */
6273 	ch->enabled_tc = !i40e_is_channel_macvlan(ch) && enabled_tc;
6274 	ch->seid = ctxt.seid;
6275 	ch->vsi_number = ctxt.vsi_number;
6276 	ch->stat_counter_idx = le16_to_cpu(ctxt.info.stat_counter_idx);
6277 
6278 	/* copy just the sections touched not the entire info
6279 	 * since not all sections are valid as returned by
6280 	 * update vsi params
6281 	 */
6282 	ch->info.mapping_flags = ctxt.info.mapping_flags;
6283 	memcpy(&ch->info.queue_mapping,
6284 	       &ctxt.info.queue_mapping, sizeof(ctxt.info.queue_mapping));
6285 	memcpy(&ch->info.tc_mapping, ctxt.info.tc_mapping,
6286 	       sizeof(ctxt.info.tc_mapping));
6287 
6288 	return 0;
6289 }
6290 
6291 static int i40e_channel_config_bw(struct i40e_vsi *vsi, struct i40e_channel *ch,
6292 				  u8 *bw_share)
6293 {
6294 	struct i40e_aqc_configure_vsi_tc_bw_data bw_data;
6295 	i40e_status ret;
6296 	int i;
6297 
6298 	memset(&bw_data, 0, sizeof(bw_data));
6299 	bw_data.tc_valid_bits = ch->enabled_tc;
6300 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6301 		bw_data.tc_bw_credits[i] = bw_share[i];
6302 
6303 	ret = i40e_aq_config_vsi_tc_bw(&vsi->back->hw, ch->seid,
6304 				       &bw_data, NULL);
6305 	if (ret) {
6306 		dev_info(&vsi->back->pdev->dev,
6307 			 "Config VSI BW allocation per TC failed, aq_err: %d for new_vsi->seid %u\n",
6308 			 vsi->back->hw.aq.asq_last_status, ch->seid);
6309 		return -EINVAL;
6310 	}
6311 
6312 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++)
6313 		ch->info.qs_handle[i] = bw_data.qs_handles[i];
6314 
6315 	return 0;
6316 }
6317 
6318 /**
6319  * i40e_channel_config_tx_ring - config TX ring associated with new channel
6320  * @pf: ptr to PF device
6321  * @vsi: the VSI being setup
6322  * @ch: ptr to channel structure
6323  *
6324  * Configure TX rings associated with channel (VSI) since queues are being
6325  * from parent VSI.
6326  **/
6327 static int i40e_channel_config_tx_ring(struct i40e_pf *pf,
6328 				       struct i40e_vsi *vsi,
6329 				       struct i40e_channel *ch)
6330 {
6331 	i40e_status ret;
6332 	int i;
6333 	u8 bw_share[I40E_MAX_TRAFFIC_CLASS] = {0};
6334 
6335 	/* Enable ETS TCs with equal BW Share for now across all VSIs */
6336 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6337 		if (ch->enabled_tc & BIT(i))
6338 			bw_share[i] = 1;
6339 	}
6340 
6341 	/* configure BW for new VSI */
6342 	ret = i40e_channel_config_bw(vsi, ch, bw_share);
6343 	if (ret) {
6344 		dev_info(&vsi->back->pdev->dev,
6345 			 "Failed configuring TC map %d for channel (seid %u)\n",
6346 			 ch->enabled_tc, ch->seid);
6347 		return ret;
6348 	}
6349 
6350 	for (i = 0; i < ch->num_queue_pairs; i++) {
6351 		struct i40e_ring *tx_ring, *rx_ring;
6352 		u16 pf_q;
6353 
6354 		pf_q = ch->base_queue + i;
6355 
6356 		/* Get to TX ring ptr of main VSI, for re-setup TX queue
6357 		 * context
6358 		 */
6359 		tx_ring = vsi->tx_rings[pf_q];
6360 		tx_ring->ch = ch;
6361 
6362 		/* Get the RX ring ptr */
6363 		rx_ring = vsi->rx_rings[pf_q];
6364 		rx_ring->ch = ch;
6365 	}
6366 
6367 	return 0;
6368 }
6369 
6370 /**
6371  * i40e_setup_hw_channel - setup new channel
6372  * @pf: ptr to PF device
6373  * @vsi: the VSI being setup
6374  * @ch: ptr to channel structure
6375  * @uplink_seid: underlying HW switching element (VEB) ID
6376  * @type: type of channel to be created (VMDq2/VF)
6377  *
6378  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6379  * and configures TX rings accordingly
6380  **/
6381 static inline int i40e_setup_hw_channel(struct i40e_pf *pf,
6382 					struct i40e_vsi *vsi,
6383 					struct i40e_channel *ch,
6384 					u16 uplink_seid, u8 type)
6385 {
6386 	int ret;
6387 
6388 	ch->initialized = false;
6389 	ch->base_queue = vsi->next_base_queue;
6390 	ch->type = type;
6391 
6392 	/* Proceed with creation of channel (VMDq2) VSI */
6393 	ret = i40e_add_channel(pf, uplink_seid, ch);
6394 	if (ret) {
6395 		dev_info(&pf->pdev->dev,
6396 			 "failed to add_channel using uplink_seid %u\n",
6397 			 uplink_seid);
6398 		return ret;
6399 	}
6400 
6401 	/* Mark the successful creation of channel */
6402 	ch->initialized = true;
6403 
6404 	/* Reconfigure TX queues using QTX_CTL register */
6405 	ret = i40e_channel_config_tx_ring(pf, vsi, ch);
6406 	if (ret) {
6407 		dev_info(&pf->pdev->dev,
6408 			 "failed to configure TX rings for channel %u\n",
6409 			 ch->seid);
6410 		return ret;
6411 	}
6412 
6413 	/* update 'next_base_queue' */
6414 	vsi->next_base_queue = vsi->next_base_queue + ch->num_queue_pairs;
6415 	dev_dbg(&pf->pdev->dev,
6416 		"Added channel: vsi_seid %u, vsi_number %u, stat_counter_idx %u, num_queue_pairs %u, pf->next_base_queue %d\n",
6417 		ch->seid, ch->vsi_number, ch->stat_counter_idx,
6418 		ch->num_queue_pairs,
6419 		vsi->next_base_queue);
6420 	return ret;
6421 }
6422 
6423 /**
6424  * i40e_setup_channel - setup new channel using uplink element
6425  * @pf: ptr to PF device
6426  * @vsi: pointer to the VSI to set up the channel within
6427  * @ch: ptr to channel structure
6428  *
6429  * Setup new channel (VSI) based on specified type (VMDq2/VF)
6430  * and uplink switching element (uplink_seid)
6431  **/
6432 static bool i40e_setup_channel(struct i40e_pf *pf, struct i40e_vsi *vsi,
6433 			       struct i40e_channel *ch)
6434 {
6435 	u8 vsi_type;
6436 	u16 seid;
6437 	int ret;
6438 
6439 	if (vsi->type == I40E_VSI_MAIN) {
6440 		vsi_type = I40E_VSI_VMDQ2;
6441 	} else {
6442 		dev_err(&pf->pdev->dev, "unsupported parent vsi type(%d)\n",
6443 			vsi->type);
6444 		return false;
6445 	}
6446 
6447 	/* underlying switching element */
6448 	seid = pf->vsi[pf->lan_vsi]->uplink_seid;
6449 
6450 	/* create channel (VSI), configure TX rings */
6451 	ret = i40e_setup_hw_channel(pf, vsi, ch, seid, vsi_type);
6452 	if (ret) {
6453 		dev_err(&pf->pdev->dev, "failed to setup hw_channel\n");
6454 		return false;
6455 	}
6456 
6457 	return ch->initialized ? true : false;
6458 }
6459 
6460 /**
6461  * i40e_validate_and_set_switch_mode - sets up switch mode correctly
6462  * @vsi: ptr to VSI which has PF backing
6463  *
6464  * Sets up switch mode correctly if it needs to be changed and perform
6465  * what are allowed modes.
6466  **/
6467 static int i40e_validate_and_set_switch_mode(struct i40e_vsi *vsi)
6468 {
6469 	u8 mode;
6470 	struct i40e_pf *pf = vsi->back;
6471 	struct i40e_hw *hw = &pf->hw;
6472 	int ret;
6473 
6474 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_dev_capabilities);
6475 	if (ret)
6476 		return -EINVAL;
6477 
6478 	if (hw->dev_caps.switch_mode) {
6479 		/* if switch mode is set, support mode2 (non-tunneled for
6480 		 * cloud filter) for now
6481 		 */
6482 		u32 switch_mode = hw->dev_caps.switch_mode &
6483 				  I40E_SWITCH_MODE_MASK;
6484 		if (switch_mode >= I40E_CLOUD_FILTER_MODE1) {
6485 			if (switch_mode == I40E_CLOUD_FILTER_MODE2)
6486 				return 0;
6487 			dev_err(&pf->pdev->dev,
6488 				"Invalid switch_mode (%d), only non-tunneled mode for cloud filter is supported\n",
6489 				hw->dev_caps.switch_mode);
6490 			return -EINVAL;
6491 		}
6492 	}
6493 
6494 	/* Set Bit 7 to be valid */
6495 	mode = I40E_AQ_SET_SWITCH_BIT7_VALID;
6496 
6497 	/* Set L4type for TCP support */
6498 	mode |= I40E_AQ_SET_SWITCH_L4_TYPE_TCP;
6499 
6500 	/* Set cloud filter mode */
6501 	mode |= I40E_AQ_SET_SWITCH_MODE_NON_TUNNEL;
6502 
6503 	/* Prep mode field for set_switch_config */
6504 	ret = i40e_aq_set_switch_config(hw, pf->last_sw_conf_flags,
6505 					pf->last_sw_conf_valid_flags,
6506 					mode, NULL);
6507 	if (ret && hw->aq.asq_last_status != I40E_AQ_RC_ESRCH)
6508 		dev_err(&pf->pdev->dev,
6509 			"couldn't set switch config bits, err %s aq_err %s\n",
6510 			i40e_stat_str(hw, ret),
6511 			i40e_aq_str(hw,
6512 				    hw->aq.asq_last_status));
6513 
6514 	return ret;
6515 }
6516 
6517 /**
6518  * i40e_create_queue_channel - function to create channel
6519  * @vsi: VSI to be configured
6520  * @ch: ptr to channel (it contains channel specific params)
6521  *
6522  * This function creates channel (VSI) using num_queues specified by user,
6523  * reconfigs RSS if needed.
6524  **/
6525 int i40e_create_queue_channel(struct i40e_vsi *vsi,
6526 			      struct i40e_channel *ch)
6527 {
6528 	struct i40e_pf *pf = vsi->back;
6529 	bool reconfig_rss;
6530 	int err;
6531 
6532 	if (!ch)
6533 		return -EINVAL;
6534 
6535 	if (!ch->num_queue_pairs) {
6536 		dev_err(&pf->pdev->dev, "Invalid num_queues requested: %d\n",
6537 			ch->num_queue_pairs);
6538 		return -EINVAL;
6539 	}
6540 
6541 	/* validate user requested num_queues for channel */
6542 	err = i40e_validate_num_queues(pf, ch->num_queue_pairs, vsi,
6543 				       &reconfig_rss);
6544 	if (err) {
6545 		dev_info(&pf->pdev->dev, "Failed to validate num_queues (%d)\n",
6546 			 ch->num_queue_pairs);
6547 		return -EINVAL;
6548 	}
6549 
6550 	/* By default we are in VEPA mode, if this is the first VF/VMDq
6551 	 * VSI to be added switch to VEB mode.
6552 	 */
6553 
6554 	if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
6555 		pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
6556 
6557 		if (vsi->type == I40E_VSI_MAIN) {
6558 			if (i40e_is_tc_mqprio_enabled(pf))
6559 				i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
6560 			else
6561 				i40e_do_reset_safe(pf, I40E_PF_RESET_FLAG);
6562 		}
6563 		/* now onwards for main VSI, number of queues will be value
6564 		 * of TC0's queue count
6565 		 */
6566 	}
6567 
6568 	/* By this time, vsi->cnt_q_avail shall be set to non-zero and
6569 	 * it should be more than num_queues
6570 	 */
6571 	if (!vsi->cnt_q_avail || vsi->cnt_q_avail < ch->num_queue_pairs) {
6572 		dev_dbg(&pf->pdev->dev,
6573 			"Error: cnt_q_avail (%u) less than num_queues %d\n",
6574 			vsi->cnt_q_avail, ch->num_queue_pairs);
6575 		return -EINVAL;
6576 	}
6577 
6578 	/* reconfig_rss only if vsi type is MAIN_VSI */
6579 	if (reconfig_rss && (vsi->type == I40E_VSI_MAIN)) {
6580 		err = i40e_vsi_reconfig_rss(vsi, ch->num_queue_pairs);
6581 		if (err) {
6582 			dev_info(&pf->pdev->dev,
6583 				 "Error: unable to reconfig rss for num_queues (%u)\n",
6584 				 ch->num_queue_pairs);
6585 			return -EINVAL;
6586 		}
6587 	}
6588 
6589 	if (!i40e_setup_channel(pf, vsi, ch)) {
6590 		dev_info(&pf->pdev->dev, "Failed to setup channel\n");
6591 		return -EINVAL;
6592 	}
6593 
6594 	dev_info(&pf->pdev->dev,
6595 		 "Setup channel (id:%u) utilizing num_queues %d\n",
6596 		 ch->seid, ch->num_queue_pairs);
6597 
6598 	/* configure VSI for BW limit */
6599 	if (ch->max_tx_rate) {
6600 		u64 credits = ch->max_tx_rate;
6601 
6602 		if (i40e_set_bw_limit(vsi, ch->seid, ch->max_tx_rate))
6603 			return -EINVAL;
6604 
6605 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
6606 		dev_dbg(&pf->pdev->dev,
6607 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
6608 			ch->max_tx_rate,
6609 			credits,
6610 			ch->seid);
6611 	}
6612 
6613 	/* in case of VF, this will be main SRIOV VSI */
6614 	ch->parent_vsi = vsi;
6615 
6616 	/* and update main_vsi's count for queue_available to use */
6617 	vsi->cnt_q_avail -= ch->num_queue_pairs;
6618 
6619 	return 0;
6620 }
6621 
6622 /**
6623  * i40e_configure_queue_channels - Add queue channel for the given TCs
6624  * @vsi: VSI to be configured
6625  *
6626  * Configures queue channel mapping to the given TCs
6627  **/
6628 static int i40e_configure_queue_channels(struct i40e_vsi *vsi)
6629 {
6630 	struct i40e_channel *ch;
6631 	u64 max_rate = 0;
6632 	int ret = 0, i;
6633 
6634 	/* Create app vsi with the TCs. Main VSI with TC0 is already set up */
6635 	vsi->tc_seid_map[0] = vsi->seid;
6636 	for (i = 1; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6637 		if (vsi->tc_config.enabled_tc & BIT(i)) {
6638 			ch = kzalloc(sizeof(*ch), GFP_KERNEL);
6639 			if (!ch) {
6640 				ret = -ENOMEM;
6641 				goto err_free;
6642 			}
6643 
6644 			INIT_LIST_HEAD(&ch->list);
6645 			ch->num_queue_pairs =
6646 				vsi->tc_config.tc_info[i].qcount;
6647 			ch->base_queue =
6648 				vsi->tc_config.tc_info[i].qoffset;
6649 
6650 			/* Bandwidth limit through tc interface is in bytes/s,
6651 			 * change to Mbit/s
6652 			 */
6653 			max_rate = vsi->mqprio_qopt.max_rate[i];
6654 			do_div(max_rate, I40E_BW_MBPS_DIVISOR);
6655 			ch->max_tx_rate = max_rate;
6656 
6657 			list_add_tail(&ch->list, &vsi->ch_list);
6658 
6659 			ret = i40e_create_queue_channel(vsi, ch);
6660 			if (ret) {
6661 				dev_err(&vsi->back->pdev->dev,
6662 					"Failed creating queue channel with TC%d: queues %d\n",
6663 					i, ch->num_queue_pairs);
6664 				goto err_free;
6665 			}
6666 			vsi->tc_seid_map[i] = ch->seid;
6667 		}
6668 	}
6669 
6670 	/* reset to reconfigure TX queue contexts */
6671 	i40e_do_reset(vsi->back, I40E_PF_RESET_FLAG, true);
6672 	return ret;
6673 
6674 err_free:
6675 	i40e_remove_queue_channels(vsi);
6676 	return ret;
6677 }
6678 
6679 /**
6680  * i40e_veb_config_tc - Configure TCs for given VEB
6681  * @veb: given VEB
6682  * @enabled_tc: TC bitmap
6683  *
6684  * Configures given TC bitmap for VEB (switching) element
6685  **/
6686 int i40e_veb_config_tc(struct i40e_veb *veb, u8 enabled_tc)
6687 {
6688 	struct i40e_aqc_configure_switching_comp_bw_config_data bw_data = {0};
6689 	struct i40e_pf *pf = veb->pf;
6690 	int ret = 0;
6691 	int i;
6692 
6693 	/* No TCs or already enabled TCs just return */
6694 	if (!enabled_tc || veb->enabled_tc == enabled_tc)
6695 		return ret;
6696 
6697 	bw_data.tc_valid_bits = enabled_tc;
6698 	/* bw_data.absolute_credits is not set (relative) */
6699 
6700 	/* Enable ETS TCs with equal BW Share for now */
6701 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
6702 		if (enabled_tc & BIT(i))
6703 			bw_data.tc_bw_share_credits[i] = 1;
6704 	}
6705 
6706 	ret = i40e_aq_config_switch_comp_bw_config(&pf->hw, veb->seid,
6707 						   &bw_data, NULL);
6708 	if (ret) {
6709 		dev_info(&pf->pdev->dev,
6710 			 "VEB bw config failed, err %s aq_err %s\n",
6711 			 i40e_stat_str(&pf->hw, ret),
6712 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6713 		goto out;
6714 	}
6715 
6716 	/* Update the BW information */
6717 	ret = i40e_veb_get_bw_info(veb);
6718 	if (ret) {
6719 		dev_info(&pf->pdev->dev,
6720 			 "Failed getting veb bw config, err %s aq_err %s\n",
6721 			 i40e_stat_str(&pf->hw, ret),
6722 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6723 	}
6724 
6725 out:
6726 	return ret;
6727 }
6728 
6729 #ifdef CONFIG_I40E_DCB
6730 /**
6731  * i40e_dcb_reconfigure - Reconfigure all VEBs and VSIs
6732  * @pf: PF struct
6733  *
6734  * Reconfigure VEB/VSIs on a given PF; it is assumed that
6735  * the caller would've quiesce all the VSIs before calling
6736  * this function
6737  **/
6738 static void i40e_dcb_reconfigure(struct i40e_pf *pf)
6739 {
6740 	u8 tc_map = 0;
6741 	int ret;
6742 	u8 v;
6743 
6744 	/* Enable the TCs available on PF to all VEBs */
6745 	tc_map = i40e_pf_get_tc_map(pf);
6746 	if (tc_map == I40E_DEFAULT_TRAFFIC_CLASS)
6747 		return;
6748 
6749 	for (v = 0; v < I40E_MAX_VEB; v++) {
6750 		if (!pf->veb[v])
6751 			continue;
6752 		ret = i40e_veb_config_tc(pf->veb[v], tc_map);
6753 		if (ret) {
6754 			dev_info(&pf->pdev->dev,
6755 				 "Failed configuring TC for VEB seid=%d\n",
6756 				 pf->veb[v]->seid);
6757 			/* Will try to configure as many components */
6758 		}
6759 	}
6760 
6761 	/* Update each VSI */
6762 	for (v = 0; v < pf->num_alloc_vsi; v++) {
6763 		if (!pf->vsi[v])
6764 			continue;
6765 
6766 		/* - Enable all TCs for the LAN VSI
6767 		 * - For all others keep them at TC0 for now
6768 		 */
6769 		if (v == pf->lan_vsi)
6770 			tc_map = i40e_pf_get_tc_map(pf);
6771 		else
6772 			tc_map = I40E_DEFAULT_TRAFFIC_CLASS;
6773 
6774 		ret = i40e_vsi_config_tc(pf->vsi[v], tc_map);
6775 		if (ret) {
6776 			dev_info(&pf->pdev->dev,
6777 				 "Failed configuring TC for VSI seid=%d\n",
6778 				 pf->vsi[v]->seid);
6779 			/* Will try to configure as many components */
6780 		} else {
6781 			/* Re-configure VSI vectors based on updated TC map */
6782 			i40e_vsi_map_rings_to_vectors(pf->vsi[v]);
6783 			if (pf->vsi[v]->netdev)
6784 				i40e_dcbnl_set_all(pf->vsi[v]);
6785 		}
6786 	}
6787 }
6788 
6789 /**
6790  * i40e_resume_port_tx - Resume port Tx
6791  * @pf: PF struct
6792  *
6793  * Resume a port's Tx and issue a PF reset in case of failure to
6794  * resume.
6795  **/
6796 static int i40e_resume_port_tx(struct i40e_pf *pf)
6797 {
6798 	struct i40e_hw *hw = &pf->hw;
6799 	int ret;
6800 
6801 	ret = i40e_aq_resume_port_tx(hw, NULL);
6802 	if (ret) {
6803 		dev_info(&pf->pdev->dev,
6804 			 "Resume Port Tx failed, err %s aq_err %s\n",
6805 			  i40e_stat_str(&pf->hw, ret),
6806 			  i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6807 		/* Schedule PF reset to recover */
6808 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6809 		i40e_service_event_schedule(pf);
6810 	}
6811 
6812 	return ret;
6813 }
6814 
6815 /**
6816  * i40e_suspend_port_tx - Suspend port Tx
6817  * @pf: PF struct
6818  *
6819  * Suspend a port's Tx and issue a PF reset in case of failure.
6820  **/
6821 static int i40e_suspend_port_tx(struct i40e_pf *pf)
6822 {
6823 	struct i40e_hw *hw = &pf->hw;
6824 	int ret;
6825 
6826 	ret = i40e_aq_suspend_port_tx(hw, pf->mac_seid, NULL);
6827 	if (ret) {
6828 		dev_info(&pf->pdev->dev,
6829 			 "Suspend Port Tx failed, err %s aq_err %s\n",
6830 			 i40e_stat_str(&pf->hw, ret),
6831 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6832 		/* Schedule PF reset to recover */
6833 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
6834 		i40e_service_event_schedule(pf);
6835 	}
6836 
6837 	return ret;
6838 }
6839 
6840 /**
6841  * i40e_hw_set_dcb_config - Program new DCBX settings into HW
6842  * @pf: PF being configured
6843  * @new_cfg: New DCBX configuration
6844  *
6845  * Program DCB settings into HW and reconfigure VEB/VSIs on
6846  * given PF. Uses "Set LLDP MIB" AQC to program the hardware.
6847  **/
6848 static int i40e_hw_set_dcb_config(struct i40e_pf *pf,
6849 				  struct i40e_dcbx_config *new_cfg)
6850 {
6851 	struct i40e_dcbx_config *old_cfg = &pf->hw.local_dcbx_config;
6852 	int ret;
6853 
6854 	/* Check if need reconfiguration */
6855 	if (!memcmp(&new_cfg, &old_cfg, sizeof(new_cfg))) {
6856 		dev_dbg(&pf->pdev->dev, "No Change in DCB Config required.\n");
6857 		return 0;
6858 	}
6859 
6860 	/* Config change disable all VSIs */
6861 	i40e_pf_quiesce_all_vsi(pf);
6862 
6863 	/* Copy the new config to the current config */
6864 	*old_cfg = *new_cfg;
6865 	old_cfg->etsrec = old_cfg->etscfg;
6866 	ret = i40e_set_dcb_config(&pf->hw);
6867 	if (ret) {
6868 		dev_info(&pf->pdev->dev,
6869 			 "Set DCB Config failed, err %s aq_err %s\n",
6870 			 i40e_stat_str(&pf->hw, ret),
6871 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6872 		goto out;
6873 	}
6874 
6875 	/* Changes in configuration update VEB/VSI */
6876 	i40e_dcb_reconfigure(pf);
6877 out:
6878 	/* In case of reset do not try to resume anything */
6879 	if (!test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state)) {
6880 		/* Re-start the VSIs if disabled */
6881 		ret = i40e_resume_port_tx(pf);
6882 		/* In case of error no point in resuming VSIs */
6883 		if (ret)
6884 			goto err;
6885 		i40e_pf_unquiesce_all_vsi(pf);
6886 	}
6887 err:
6888 	return ret;
6889 }
6890 
6891 /**
6892  * i40e_hw_dcb_config - Program new DCBX settings into HW
6893  * @pf: PF being configured
6894  * @new_cfg: New DCBX configuration
6895  *
6896  * Program DCB settings into HW and reconfigure VEB/VSIs on
6897  * given PF
6898  **/
6899 int i40e_hw_dcb_config(struct i40e_pf *pf, struct i40e_dcbx_config *new_cfg)
6900 {
6901 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
6902 	u8 prio_type[I40E_MAX_TRAFFIC_CLASS] = {0};
6903 	u32 mfs_tc[I40E_MAX_TRAFFIC_CLASS];
6904 	struct i40e_dcbx_config *old_cfg;
6905 	u8 mode[I40E_MAX_TRAFFIC_CLASS];
6906 	struct i40e_rx_pb_config pb_cfg;
6907 	struct i40e_hw *hw = &pf->hw;
6908 	u8 num_ports = hw->num_ports;
6909 	bool need_reconfig;
6910 	int ret = -EINVAL;
6911 	u8 lltc_map = 0;
6912 	u8 tc_map = 0;
6913 	u8 new_numtc;
6914 	u8 i;
6915 
6916 	dev_dbg(&pf->pdev->dev, "Configuring DCB registers directly\n");
6917 	/* Un-pack information to Program ETS HW via shared API
6918 	 * numtc, tcmap
6919 	 * LLTC map
6920 	 * ETS/NON-ETS arbiter mode
6921 	 * max exponent (credit refills)
6922 	 * Total number of ports
6923 	 * PFC priority bit-map
6924 	 * Priority Table
6925 	 * BW % per TC
6926 	 * Arbiter mode between UPs sharing same TC
6927 	 * TSA table (ETS or non-ETS)
6928 	 * EEE enabled or not
6929 	 * MFS TC table
6930 	 */
6931 
6932 	new_numtc = i40e_dcb_get_num_tc(new_cfg);
6933 
6934 	memset(&ets_data, 0, sizeof(ets_data));
6935 	for (i = 0; i < new_numtc; i++) {
6936 		tc_map |= BIT(i);
6937 		switch (new_cfg->etscfg.tsatable[i]) {
6938 		case I40E_IEEE_TSA_ETS:
6939 			prio_type[i] = I40E_DCB_PRIO_TYPE_ETS;
6940 			ets_data.tc_bw_share_credits[i] =
6941 					new_cfg->etscfg.tcbwtable[i];
6942 			break;
6943 		case I40E_IEEE_TSA_STRICT:
6944 			prio_type[i] = I40E_DCB_PRIO_TYPE_STRICT;
6945 			lltc_map |= BIT(i);
6946 			ets_data.tc_bw_share_credits[i] =
6947 					I40E_DCB_STRICT_PRIO_CREDITS;
6948 			break;
6949 		default:
6950 			/* Invalid TSA type */
6951 			need_reconfig = false;
6952 			goto out;
6953 		}
6954 	}
6955 
6956 	old_cfg = &hw->local_dcbx_config;
6957 	/* Check if need reconfiguration */
6958 	need_reconfig = i40e_dcb_need_reconfig(pf, old_cfg, new_cfg);
6959 
6960 	/* If needed, enable/disable frame tagging, disable all VSIs
6961 	 * and suspend port tx
6962 	 */
6963 	if (need_reconfig) {
6964 		/* Enable DCB tagging only when more than one TC */
6965 		if (new_numtc > 1)
6966 			pf->flags |= I40E_FLAG_DCB_ENABLED;
6967 		else
6968 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
6969 
6970 		set_bit(__I40E_PORT_SUSPENDED, pf->state);
6971 		/* Reconfiguration needed quiesce all VSIs */
6972 		i40e_pf_quiesce_all_vsi(pf);
6973 		ret = i40e_suspend_port_tx(pf);
6974 		if (ret)
6975 			goto err;
6976 	}
6977 
6978 	/* Configure Port ETS Tx Scheduler */
6979 	ets_data.tc_valid_bits = tc_map;
6980 	ets_data.tc_strict_priority_flags = lltc_map;
6981 	ret = i40e_aq_config_switch_comp_ets
6982 		(hw, pf->mac_seid, &ets_data,
6983 		 i40e_aqc_opc_modify_switching_comp_ets, NULL);
6984 	if (ret) {
6985 		dev_info(&pf->pdev->dev,
6986 			 "Modify Port ETS failed, err %s aq_err %s\n",
6987 			 i40e_stat_str(&pf->hw, ret),
6988 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
6989 		goto out;
6990 	}
6991 
6992 	/* Configure Rx ETS HW */
6993 	memset(&mode, I40E_DCB_ARB_MODE_ROUND_ROBIN, sizeof(mode));
6994 	i40e_dcb_hw_set_num_tc(hw, new_numtc);
6995 	i40e_dcb_hw_rx_fifo_config(hw, I40E_DCB_ARB_MODE_ROUND_ROBIN,
6996 				   I40E_DCB_ARB_MODE_STRICT_PRIORITY,
6997 				   I40E_DCB_DEFAULT_MAX_EXPONENT,
6998 				   lltc_map);
6999 	i40e_dcb_hw_rx_cmd_monitor_config(hw, new_numtc, num_ports);
7000 	i40e_dcb_hw_rx_ets_bw_config(hw, new_cfg->etscfg.tcbwtable, mode,
7001 				     prio_type);
7002 	i40e_dcb_hw_pfc_config(hw, new_cfg->pfc.pfcenable,
7003 			       new_cfg->etscfg.prioritytable);
7004 	i40e_dcb_hw_rx_up2tc_config(hw, new_cfg->etscfg.prioritytable);
7005 
7006 	/* Configure Rx Packet Buffers in HW */
7007 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7008 		mfs_tc[i] = pf->vsi[pf->lan_vsi]->netdev->mtu;
7009 		mfs_tc[i] += I40E_PACKET_HDR_PAD;
7010 	}
7011 
7012 	i40e_dcb_hw_calculate_pool_sizes(hw, num_ports,
7013 					 false, new_cfg->pfc.pfcenable,
7014 					 mfs_tc, &pb_cfg);
7015 	i40e_dcb_hw_rx_pb_config(hw, &pf->pb_cfg, &pb_cfg);
7016 
7017 	/* Update the local Rx Packet buffer config */
7018 	pf->pb_cfg = pb_cfg;
7019 
7020 	/* Inform the FW about changes to DCB configuration */
7021 	ret = i40e_aq_dcb_updated(&pf->hw, NULL);
7022 	if (ret) {
7023 		dev_info(&pf->pdev->dev,
7024 			 "DCB Updated failed, err %s aq_err %s\n",
7025 			 i40e_stat_str(&pf->hw, ret),
7026 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7027 		goto out;
7028 	}
7029 
7030 	/* Update the port DCBx configuration */
7031 	*old_cfg = *new_cfg;
7032 
7033 	/* Changes in configuration update VEB/VSI */
7034 	i40e_dcb_reconfigure(pf);
7035 out:
7036 	/* Re-start the VSIs if disabled */
7037 	if (need_reconfig) {
7038 		ret = i40e_resume_port_tx(pf);
7039 
7040 		clear_bit(__I40E_PORT_SUSPENDED, pf->state);
7041 		/* In case of error no point in resuming VSIs */
7042 		if (ret)
7043 			goto err;
7044 
7045 		/* Wait for the PF's queues to be disabled */
7046 		ret = i40e_pf_wait_queues_disabled(pf);
7047 		if (ret) {
7048 			/* Schedule PF reset to recover */
7049 			set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
7050 			i40e_service_event_schedule(pf);
7051 			goto err;
7052 		} else {
7053 			i40e_pf_unquiesce_all_vsi(pf);
7054 			set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7055 			set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
7056 		}
7057 		/* registers are set, lets apply */
7058 		if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB)
7059 			ret = i40e_hw_set_dcb_config(pf, new_cfg);
7060 	}
7061 
7062 err:
7063 	return ret;
7064 }
7065 
7066 /**
7067  * i40e_dcb_sw_default_config - Set default DCB configuration when DCB in SW
7068  * @pf: PF being queried
7069  *
7070  * Set default DCB configuration in case DCB is to be done in SW.
7071  **/
7072 int i40e_dcb_sw_default_config(struct i40e_pf *pf)
7073 {
7074 	struct i40e_dcbx_config *dcb_cfg = &pf->hw.local_dcbx_config;
7075 	struct i40e_aqc_configure_switching_comp_ets_data ets_data;
7076 	struct i40e_hw *hw = &pf->hw;
7077 	int err;
7078 
7079 	if (pf->hw_features & I40E_HW_USE_SET_LLDP_MIB) {
7080 		/* Update the local cached instance with TC0 ETS */
7081 		memset(&pf->tmp_cfg, 0, sizeof(struct i40e_dcbx_config));
7082 		pf->tmp_cfg.etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7083 		pf->tmp_cfg.etscfg.maxtcs = 0;
7084 		pf->tmp_cfg.etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7085 		pf->tmp_cfg.etscfg.tsatable[0] = I40E_IEEE_TSA_ETS;
7086 		pf->tmp_cfg.pfc.willing = I40E_IEEE_DEFAULT_PFC_WILLING;
7087 		pf->tmp_cfg.pfc.pfccap = I40E_MAX_TRAFFIC_CLASS;
7088 		/* FW needs one App to configure HW */
7089 		pf->tmp_cfg.numapps = I40E_IEEE_DEFAULT_NUM_APPS;
7090 		pf->tmp_cfg.app[0].selector = I40E_APP_SEL_ETHTYPE;
7091 		pf->tmp_cfg.app[0].priority = I40E_IEEE_DEFAULT_APP_PRIO;
7092 		pf->tmp_cfg.app[0].protocolid = I40E_APP_PROTOID_FCOE;
7093 
7094 		return i40e_hw_set_dcb_config(pf, &pf->tmp_cfg);
7095 	}
7096 
7097 	memset(&ets_data, 0, sizeof(ets_data));
7098 	ets_data.tc_valid_bits = I40E_DEFAULT_TRAFFIC_CLASS; /* TC0 only */
7099 	ets_data.tc_strict_priority_flags = 0; /* ETS */
7100 	ets_data.tc_bw_share_credits[0] = I40E_IEEE_DEFAULT_ETS_TCBW; /* 100% to TC0 */
7101 
7102 	/* Enable ETS on the Physical port */
7103 	err = i40e_aq_config_switch_comp_ets
7104 		(hw, pf->mac_seid, &ets_data,
7105 		 i40e_aqc_opc_enable_switching_comp_ets, NULL);
7106 	if (err) {
7107 		dev_info(&pf->pdev->dev,
7108 			 "Enable Port ETS failed, err %s aq_err %s\n",
7109 			 i40e_stat_str(&pf->hw, err),
7110 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7111 		err = -ENOENT;
7112 		goto out;
7113 	}
7114 
7115 	/* Update the local cached instance with TC0 ETS */
7116 	dcb_cfg->etscfg.willing = I40E_IEEE_DEFAULT_ETS_WILLING;
7117 	dcb_cfg->etscfg.cbs = 0;
7118 	dcb_cfg->etscfg.maxtcs = I40E_MAX_TRAFFIC_CLASS;
7119 	dcb_cfg->etscfg.tcbwtable[0] = I40E_IEEE_DEFAULT_ETS_TCBW;
7120 
7121 out:
7122 	return err;
7123 }
7124 
7125 /**
7126  * i40e_init_pf_dcb - Initialize DCB configuration
7127  * @pf: PF being configured
7128  *
7129  * Query the current DCB configuration and cache it
7130  * in the hardware structure
7131  **/
7132 static int i40e_init_pf_dcb(struct i40e_pf *pf)
7133 {
7134 	struct i40e_hw *hw = &pf->hw;
7135 	int err;
7136 
7137 	/* Do not enable DCB for SW1 and SW2 images even if the FW is capable
7138 	 * Also do not enable DCBx if FW LLDP agent is disabled
7139 	 */
7140 	if (pf->hw_features & I40E_HW_NO_DCB_SUPPORT) {
7141 		dev_info(&pf->pdev->dev, "DCB is not supported.\n");
7142 		err = I40E_NOT_SUPPORTED;
7143 		goto out;
7144 	}
7145 	if (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) {
7146 		dev_info(&pf->pdev->dev, "FW LLDP is disabled, attempting SW DCB\n");
7147 		err = i40e_dcb_sw_default_config(pf);
7148 		if (err) {
7149 			dev_info(&pf->pdev->dev, "Could not initialize SW DCB\n");
7150 			goto out;
7151 		}
7152 		dev_info(&pf->pdev->dev, "SW DCB initialization succeeded.\n");
7153 		pf->dcbx_cap = DCB_CAP_DCBX_HOST |
7154 			       DCB_CAP_DCBX_VER_IEEE;
7155 		/* at init capable but disabled */
7156 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
7157 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7158 		goto out;
7159 	}
7160 	err = i40e_init_dcb(hw, true);
7161 	if (!err) {
7162 		/* Device/Function is not DCBX capable */
7163 		if ((!hw->func_caps.dcb) ||
7164 		    (hw->dcbx_status == I40E_DCBX_STATUS_DISABLED)) {
7165 			dev_info(&pf->pdev->dev,
7166 				 "DCBX offload is not supported or is disabled for this PF.\n");
7167 		} else {
7168 			/* When status is not DISABLED then DCBX in FW */
7169 			pf->dcbx_cap = DCB_CAP_DCBX_LLD_MANAGED |
7170 				       DCB_CAP_DCBX_VER_IEEE;
7171 
7172 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
7173 			/* Enable DCB tagging only when more than one TC
7174 			 * or explicitly disable if only one TC
7175 			 */
7176 			if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
7177 				pf->flags |= I40E_FLAG_DCB_ENABLED;
7178 			else
7179 				pf->flags &= ~I40E_FLAG_DCB_ENABLED;
7180 			dev_dbg(&pf->pdev->dev,
7181 				"DCBX offload is supported for this PF.\n");
7182 		}
7183 	} else if (pf->hw.aq.asq_last_status == I40E_AQ_RC_EPERM) {
7184 		dev_info(&pf->pdev->dev, "FW LLDP disabled for this PF.\n");
7185 		pf->flags |= I40E_FLAG_DISABLE_FW_LLDP;
7186 	} else {
7187 		dev_info(&pf->pdev->dev,
7188 			 "Query for DCB configuration failed, err %s aq_err %s\n",
7189 			 i40e_stat_str(&pf->hw, err),
7190 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7191 	}
7192 
7193 out:
7194 	return err;
7195 }
7196 #endif /* CONFIG_I40E_DCB */
7197 
7198 /**
7199  * i40e_print_link_message - print link up or down
7200  * @vsi: the VSI for which link needs a message
7201  * @isup: true of link is up, false otherwise
7202  */
7203 void i40e_print_link_message(struct i40e_vsi *vsi, bool isup)
7204 {
7205 	enum i40e_aq_link_speed new_speed;
7206 	struct i40e_pf *pf = vsi->back;
7207 	char *speed = "Unknown";
7208 	char *fc = "Unknown";
7209 	char *fec = "";
7210 	char *req_fec = "";
7211 	char *an = "";
7212 
7213 	if (isup)
7214 		new_speed = pf->hw.phy.link_info.link_speed;
7215 	else
7216 		new_speed = I40E_LINK_SPEED_UNKNOWN;
7217 
7218 	if ((vsi->current_isup == isup) && (vsi->current_speed == new_speed))
7219 		return;
7220 	vsi->current_isup = isup;
7221 	vsi->current_speed = new_speed;
7222 	if (!isup) {
7223 		netdev_info(vsi->netdev, "NIC Link is Down\n");
7224 		return;
7225 	}
7226 
7227 	/* Warn user if link speed on NPAR enabled partition is not at
7228 	 * least 10GB
7229 	 */
7230 	if (pf->hw.func_caps.npar_enable &&
7231 	    (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_1GB ||
7232 	     pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_100MB))
7233 		netdev_warn(vsi->netdev,
7234 			    "The partition detected link speed that is less than 10Gbps\n");
7235 
7236 	switch (pf->hw.phy.link_info.link_speed) {
7237 	case I40E_LINK_SPEED_40GB:
7238 		speed = "40 G";
7239 		break;
7240 	case I40E_LINK_SPEED_20GB:
7241 		speed = "20 G";
7242 		break;
7243 	case I40E_LINK_SPEED_25GB:
7244 		speed = "25 G";
7245 		break;
7246 	case I40E_LINK_SPEED_10GB:
7247 		speed = "10 G";
7248 		break;
7249 	case I40E_LINK_SPEED_5GB:
7250 		speed = "5 G";
7251 		break;
7252 	case I40E_LINK_SPEED_2_5GB:
7253 		speed = "2.5 G";
7254 		break;
7255 	case I40E_LINK_SPEED_1GB:
7256 		speed = "1000 M";
7257 		break;
7258 	case I40E_LINK_SPEED_100MB:
7259 		speed = "100 M";
7260 		break;
7261 	default:
7262 		break;
7263 	}
7264 
7265 	switch (pf->hw.fc.current_mode) {
7266 	case I40E_FC_FULL:
7267 		fc = "RX/TX";
7268 		break;
7269 	case I40E_FC_TX_PAUSE:
7270 		fc = "TX";
7271 		break;
7272 	case I40E_FC_RX_PAUSE:
7273 		fc = "RX";
7274 		break;
7275 	default:
7276 		fc = "None";
7277 		break;
7278 	}
7279 
7280 	if (pf->hw.phy.link_info.link_speed == I40E_LINK_SPEED_25GB) {
7281 		req_fec = "None";
7282 		fec = "None";
7283 		an = "False";
7284 
7285 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7286 			an = "True";
7287 
7288 		if (pf->hw.phy.link_info.fec_info &
7289 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7290 			fec = "CL74 FC-FEC/BASE-R";
7291 		else if (pf->hw.phy.link_info.fec_info &
7292 			 I40E_AQ_CONFIG_FEC_RS_ENA)
7293 			fec = "CL108 RS-FEC";
7294 
7295 		/* 'CL108 RS-FEC' should be displayed when RS is requested, or
7296 		 * both RS and FC are requested
7297 		 */
7298 		if (vsi->back->hw.phy.link_info.req_fec_info &
7299 		    (I40E_AQ_REQUEST_FEC_KR | I40E_AQ_REQUEST_FEC_RS)) {
7300 			if (vsi->back->hw.phy.link_info.req_fec_info &
7301 			    I40E_AQ_REQUEST_FEC_RS)
7302 				req_fec = "CL108 RS-FEC";
7303 			else
7304 				req_fec = "CL74 FC-FEC/BASE-R";
7305 		}
7306 		netdev_info(vsi->netdev,
7307 			    "NIC Link is Up, %sbps Full Duplex, Requested FEC: %s, Negotiated FEC: %s, Autoneg: %s, Flow Control: %s\n",
7308 			    speed, req_fec, fec, an, fc);
7309 	} else if (pf->hw.device_id == I40E_DEV_ID_KX_X722) {
7310 		req_fec = "None";
7311 		fec = "None";
7312 		an = "False";
7313 
7314 		if (pf->hw.phy.link_info.an_info & I40E_AQ_AN_COMPLETED)
7315 			an = "True";
7316 
7317 		if (pf->hw.phy.link_info.fec_info &
7318 		    I40E_AQ_CONFIG_FEC_KR_ENA)
7319 			fec = "CL74 FC-FEC/BASE-R";
7320 
7321 		if (pf->hw.phy.link_info.req_fec_info &
7322 		    I40E_AQ_REQUEST_FEC_KR)
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 {
7329 		netdev_info(vsi->netdev,
7330 			    "NIC Link is Up, %sbps Full Duplex, Flow Control: %s\n",
7331 			    speed, fc);
7332 	}
7333 
7334 }
7335 
7336 /**
7337  * i40e_up_complete - Finish the last steps of bringing up a connection
7338  * @vsi: the VSI being configured
7339  **/
7340 static int i40e_up_complete(struct i40e_vsi *vsi)
7341 {
7342 	struct i40e_pf *pf = vsi->back;
7343 	int err;
7344 
7345 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
7346 		i40e_vsi_configure_msix(vsi);
7347 	else
7348 		i40e_configure_msi_and_legacy(vsi);
7349 
7350 	/* start rings */
7351 	err = i40e_vsi_start_rings(vsi);
7352 	if (err)
7353 		return err;
7354 
7355 	clear_bit(__I40E_VSI_DOWN, vsi->state);
7356 	i40e_napi_enable_all(vsi);
7357 	i40e_vsi_enable_irq(vsi);
7358 
7359 	if ((pf->hw.phy.link_info.link_info & I40E_AQ_LINK_UP) &&
7360 	    (vsi->netdev)) {
7361 		i40e_print_link_message(vsi, true);
7362 		netif_tx_start_all_queues(vsi->netdev);
7363 		netif_carrier_on(vsi->netdev);
7364 	}
7365 
7366 	/* replay FDIR SB filters */
7367 	if (vsi->type == I40E_VSI_FDIR) {
7368 		/* reset fd counters */
7369 		pf->fd_add_err = 0;
7370 		pf->fd_atr_cnt = 0;
7371 		i40e_fdir_filter_restore(vsi);
7372 	}
7373 
7374 	/* On the next run of the service_task, notify any clients of the new
7375 	 * opened netdev
7376 	 */
7377 	set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
7378 	i40e_service_event_schedule(pf);
7379 
7380 	return 0;
7381 }
7382 
7383 /**
7384  * i40e_vsi_reinit_locked - Reset the VSI
7385  * @vsi: the VSI being configured
7386  *
7387  * Rebuild the ring structs after some configuration
7388  * has changed, e.g. MTU size.
7389  **/
7390 static void i40e_vsi_reinit_locked(struct i40e_vsi *vsi)
7391 {
7392 	struct i40e_pf *pf = vsi->back;
7393 
7394 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state))
7395 		usleep_range(1000, 2000);
7396 	i40e_down(vsi);
7397 
7398 	i40e_up(vsi);
7399 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
7400 }
7401 
7402 /**
7403  * i40e_force_link_state - Force the link status
7404  * @pf: board private structure
7405  * @is_up: whether the link state should be forced up or down
7406  **/
7407 static i40e_status i40e_force_link_state(struct i40e_pf *pf, bool is_up)
7408 {
7409 	struct i40e_aq_get_phy_abilities_resp abilities;
7410 	struct i40e_aq_set_phy_config config = {0};
7411 	bool non_zero_phy_type = is_up;
7412 	struct i40e_hw *hw = &pf->hw;
7413 	i40e_status err;
7414 	u64 mask;
7415 	u8 speed;
7416 
7417 	/* Card might've been put in an unstable state by other drivers
7418 	 * and applications, which causes incorrect speed values being
7419 	 * set on startup. In order to clear speed registers, we call
7420 	 * get_phy_capabilities twice, once to get initial state of
7421 	 * available speeds, and once to get current PHY config.
7422 	 */
7423 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities,
7424 					   NULL);
7425 	if (err) {
7426 		dev_err(&pf->pdev->dev,
7427 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7428 			i40e_stat_str(hw, err),
7429 			i40e_aq_str(hw, hw->aq.asq_last_status));
7430 		return err;
7431 	}
7432 	speed = abilities.link_speed;
7433 
7434 	/* Get the current phy config */
7435 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities,
7436 					   NULL);
7437 	if (err) {
7438 		dev_err(&pf->pdev->dev,
7439 			"failed to get phy cap., ret =  %s last_status =  %s\n",
7440 			i40e_stat_str(hw, err),
7441 			i40e_aq_str(hw, hw->aq.asq_last_status));
7442 		return err;
7443 	}
7444 
7445 	/* If link needs to go up, but was not forced to go down,
7446 	 * and its speed values are OK, no need for a flap
7447 	 * if non_zero_phy_type was set, still need to force up
7448 	 */
7449 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED)
7450 		non_zero_phy_type = true;
7451 	else if (is_up && abilities.phy_type != 0 && abilities.link_speed != 0)
7452 		return I40E_SUCCESS;
7453 
7454 	/* To force link we need to set bits for all supported PHY types,
7455 	 * but there are now more than 32, so we need to split the bitmap
7456 	 * across two fields.
7457 	 */
7458 	mask = I40E_PHY_TYPES_BITMASK;
7459 	config.phy_type =
7460 		non_zero_phy_type ? cpu_to_le32((u32)(mask & 0xffffffff)) : 0;
7461 	config.phy_type_ext =
7462 		non_zero_phy_type ? (u8)((mask >> 32) & 0xff) : 0;
7463 	/* Copy the old settings, except of phy_type */
7464 	config.abilities = abilities.abilities;
7465 	if (pf->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED) {
7466 		if (is_up)
7467 			config.abilities |= I40E_AQ_PHY_ENABLE_LINK;
7468 		else
7469 			config.abilities &= ~(I40E_AQ_PHY_ENABLE_LINK);
7470 	}
7471 	if (abilities.link_speed != 0)
7472 		config.link_speed = abilities.link_speed;
7473 	else
7474 		config.link_speed = speed;
7475 	config.eee_capability = abilities.eee_capability;
7476 	config.eeer = abilities.eeer_val;
7477 	config.low_power_ctrl = abilities.d3_lpan;
7478 	config.fec_config = abilities.fec_cfg_curr_mod_ext_info &
7479 			    I40E_AQ_PHY_FEC_CONFIG_MASK;
7480 	err = i40e_aq_set_phy_config(hw, &config, NULL);
7481 
7482 	if (err) {
7483 		dev_err(&pf->pdev->dev,
7484 			"set phy config ret =  %s last_status =  %s\n",
7485 			i40e_stat_str(&pf->hw, err),
7486 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
7487 		return err;
7488 	}
7489 
7490 	/* Update the link info */
7491 	err = i40e_update_link_info(hw);
7492 	if (err) {
7493 		/* Wait a little bit (on 40G cards it sometimes takes a really
7494 		 * long time for link to come back from the atomic reset)
7495 		 * and try once more
7496 		 */
7497 		msleep(1000);
7498 		i40e_update_link_info(hw);
7499 	}
7500 
7501 	i40e_aq_set_link_restart_an(hw, is_up, NULL);
7502 
7503 	return I40E_SUCCESS;
7504 }
7505 
7506 /**
7507  * i40e_up - Bring the connection back up after being down
7508  * @vsi: the VSI being configured
7509  **/
7510 int i40e_up(struct i40e_vsi *vsi)
7511 {
7512 	int err;
7513 
7514 	if (vsi->type == I40E_VSI_MAIN &&
7515 	    (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7516 	     vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7517 		i40e_force_link_state(vsi->back, true);
7518 
7519 	err = i40e_vsi_configure(vsi);
7520 	if (!err)
7521 		err = i40e_up_complete(vsi);
7522 
7523 	return err;
7524 }
7525 
7526 /**
7527  * i40e_down - Shutdown the connection processing
7528  * @vsi: the VSI being stopped
7529  **/
7530 void i40e_down(struct i40e_vsi *vsi)
7531 {
7532 	int i;
7533 
7534 	/* It is assumed that the caller of this function
7535 	 * sets the vsi->state __I40E_VSI_DOWN bit.
7536 	 */
7537 	if (vsi->netdev) {
7538 		netif_carrier_off(vsi->netdev);
7539 		netif_tx_disable(vsi->netdev);
7540 	}
7541 	i40e_vsi_disable_irq(vsi);
7542 	i40e_vsi_stop_rings(vsi);
7543 	if (vsi->type == I40E_VSI_MAIN &&
7544 	   (vsi->back->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED ||
7545 	    vsi->back->flags & I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED))
7546 		i40e_force_link_state(vsi->back, false);
7547 	i40e_napi_disable_all(vsi);
7548 
7549 	for (i = 0; i < vsi->num_queue_pairs; i++) {
7550 		i40e_clean_tx_ring(vsi->tx_rings[i]);
7551 		if (i40e_enabled_xdp_vsi(vsi)) {
7552 			/* Make sure that in-progress ndo_xdp_xmit and
7553 			 * ndo_xsk_wakeup calls are completed.
7554 			 */
7555 			synchronize_rcu();
7556 			i40e_clean_tx_ring(vsi->xdp_rings[i]);
7557 		}
7558 		i40e_clean_rx_ring(vsi->rx_rings[i]);
7559 	}
7560 
7561 }
7562 
7563 /**
7564  * i40e_validate_mqprio_qopt- validate queue mapping info
7565  * @vsi: the VSI being configured
7566  * @mqprio_qopt: queue parametrs
7567  **/
7568 static int i40e_validate_mqprio_qopt(struct i40e_vsi *vsi,
7569 				     struct tc_mqprio_qopt_offload *mqprio_qopt)
7570 {
7571 	u64 sum_max_rate = 0;
7572 	u64 max_rate = 0;
7573 	int i;
7574 
7575 	if (mqprio_qopt->qopt.offset[0] != 0 ||
7576 	    mqprio_qopt->qopt.num_tc < 1 ||
7577 	    mqprio_qopt->qopt.num_tc > I40E_MAX_TRAFFIC_CLASS)
7578 		return -EINVAL;
7579 	for (i = 0; ; i++) {
7580 		if (!mqprio_qopt->qopt.count[i])
7581 			return -EINVAL;
7582 		if (mqprio_qopt->min_rate[i]) {
7583 			dev_err(&vsi->back->pdev->dev,
7584 				"Invalid min tx rate (greater than 0) specified\n");
7585 			return -EINVAL;
7586 		}
7587 		max_rate = mqprio_qopt->max_rate[i];
7588 		do_div(max_rate, I40E_BW_MBPS_DIVISOR);
7589 		sum_max_rate += max_rate;
7590 
7591 		if (i >= mqprio_qopt->qopt.num_tc - 1)
7592 			break;
7593 		if (mqprio_qopt->qopt.offset[i + 1] !=
7594 		    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i]))
7595 			return -EINVAL;
7596 	}
7597 	if (vsi->num_queue_pairs <
7598 	    (mqprio_qopt->qopt.offset[i] + mqprio_qopt->qopt.count[i])) {
7599 		dev_err(&vsi->back->pdev->dev,
7600 			"Failed to create traffic channel, insufficient number of queues.\n");
7601 		return -EINVAL;
7602 	}
7603 	if (sum_max_rate > i40e_get_link_speed(vsi)) {
7604 		dev_err(&vsi->back->pdev->dev,
7605 			"Invalid max tx rate specified\n");
7606 		return -EINVAL;
7607 	}
7608 	return 0;
7609 }
7610 
7611 /**
7612  * i40e_vsi_set_default_tc_config - set default values for tc configuration
7613  * @vsi: the VSI being configured
7614  **/
7615 static void i40e_vsi_set_default_tc_config(struct i40e_vsi *vsi)
7616 {
7617 	u16 qcount;
7618 	int i;
7619 
7620 	/* Only TC0 is enabled */
7621 	vsi->tc_config.numtc = 1;
7622 	vsi->tc_config.enabled_tc = 1;
7623 	qcount = min_t(int, vsi->alloc_queue_pairs,
7624 		       i40e_pf_get_max_q_per_tc(vsi->back));
7625 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
7626 		/* For the TC that is not enabled set the offset to default
7627 		 * queue and allocate one queue for the given TC.
7628 		 */
7629 		vsi->tc_config.tc_info[i].qoffset = 0;
7630 		if (i == 0)
7631 			vsi->tc_config.tc_info[i].qcount = qcount;
7632 		else
7633 			vsi->tc_config.tc_info[i].qcount = 1;
7634 		vsi->tc_config.tc_info[i].netdev_tc = 0;
7635 	}
7636 }
7637 
7638 /**
7639  * i40e_del_macvlan_filter
7640  * @hw: pointer to the HW structure
7641  * @seid: seid of the channel VSI
7642  * @macaddr: the mac address to apply as a filter
7643  * @aq_err: store the admin Q error
7644  *
7645  * This function deletes a mac filter on the channel VSI which serves as the
7646  * macvlan. Returns 0 on success.
7647  **/
7648 static i40e_status i40e_del_macvlan_filter(struct i40e_hw *hw, u16 seid,
7649 					   const u8 *macaddr, int *aq_err)
7650 {
7651 	struct i40e_aqc_remove_macvlan_element_data element;
7652 	i40e_status status;
7653 
7654 	memset(&element, 0, sizeof(element));
7655 	ether_addr_copy(element.mac_addr, macaddr);
7656 	element.vlan_tag = 0;
7657 	element.flags = I40E_AQC_MACVLAN_DEL_PERFECT_MATCH;
7658 	status = i40e_aq_remove_macvlan(hw, seid, &element, 1, NULL);
7659 	*aq_err = hw->aq.asq_last_status;
7660 
7661 	return status;
7662 }
7663 
7664 /**
7665  * i40e_add_macvlan_filter
7666  * @hw: pointer to the HW structure
7667  * @seid: seid of the channel VSI
7668  * @macaddr: the mac address to apply as a filter
7669  * @aq_err: store the admin Q error
7670  *
7671  * This function adds a mac filter on the channel VSI which serves as the
7672  * macvlan. Returns 0 on success.
7673  **/
7674 static i40e_status i40e_add_macvlan_filter(struct i40e_hw *hw, u16 seid,
7675 					   const u8 *macaddr, int *aq_err)
7676 {
7677 	struct i40e_aqc_add_macvlan_element_data element;
7678 	i40e_status status;
7679 	u16 cmd_flags = 0;
7680 
7681 	ether_addr_copy(element.mac_addr, macaddr);
7682 	element.vlan_tag = 0;
7683 	element.queue_number = 0;
7684 	element.match_method = I40E_AQC_MM_ERR_NO_RES;
7685 	cmd_flags |= I40E_AQC_MACVLAN_ADD_PERFECT_MATCH;
7686 	element.flags = cpu_to_le16(cmd_flags);
7687 	status = i40e_aq_add_macvlan(hw, seid, &element, 1, NULL);
7688 	*aq_err = hw->aq.asq_last_status;
7689 
7690 	return status;
7691 }
7692 
7693 /**
7694  * i40e_reset_ch_rings - Reset the queue contexts in a channel
7695  * @vsi: the VSI we want to access
7696  * @ch: the channel we want to access
7697  */
7698 static void i40e_reset_ch_rings(struct i40e_vsi *vsi, struct i40e_channel *ch)
7699 {
7700 	struct i40e_ring *tx_ring, *rx_ring;
7701 	u16 pf_q;
7702 	int i;
7703 
7704 	for (i = 0; i < ch->num_queue_pairs; i++) {
7705 		pf_q = ch->base_queue + i;
7706 		tx_ring = vsi->tx_rings[pf_q];
7707 		tx_ring->ch = NULL;
7708 		rx_ring = vsi->rx_rings[pf_q];
7709 		rx_ring->ch = NULL;
7710 	}
7711 }
7712 
7713 /**
7714  * i40e_free_macvlan_channels
7715  * @vsi: the VSI we want to access
7716  *
7717  * This function frees the Qs of the channel VSI from
7718  * the stack and also deletes the channel VSIs which
7719  * serve as macvlans.
7720  */
7721 static void i40e_free_macvlan_channels(struct i40e_vsi *vsi)
7722 {
7723 	struct i40e_channel *ch, *ch_tmp;
7724 	int ret;
7725 
7726 	if (list_empty(&vsi->macvlan_list))
7727 		return;
7728 
7729 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
7730 		struct i40e_vsi *parent_vsi;
7731 
7732 		if (i40e_is_channel_macvlan(ch)) {
7733 			i40e_reset_ch_rings(vsi, ch);
7734 			clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
7735 			netdev_unbind_sb_channel(vsi->netdev, ch->fwd->netdev);
7736 			netdev_set_sb_channel(ch->fwd->netdev, 0);
7737 			kfree(ch->fwd);
7738 			ch->fwd = NULL;
7739 		}
7740 
7741 		list_del(&ch->list);
7742 		parent_vsi = ch->parent_vsi;
7743 		if (!parent_vsi || !ch->initialized) {
7744 			kfree(ch);
7745 			continue;
7746 		}
7747 
7748 		/* remove the VSI */
7749 		ret = i40e_aq_delete_element(&vsi->back->hw, ch->seid,
7750 					     NULL);
7751 		if (ret)
7752 			dev_err(&vsi->back->pdev->dev,
7753 				"unable to remove channel (%d) for parent VSI(%d)\n",
7754 				ch->seid, parent_vsi->seid);
7755 		kfree(ch);
7756 	}
7757 	vsi->macvlan_cnt = 0;
7758 }
7759 
7760 /**
7761  * i40e_fwd_ring_up - bring the macvlan device up
7762  * @vsi: the VSI we want to access
7763  * @vdev: macvlan netdevice
7764  * @fwd: the private fwd structure
7765  */
7766 static int i40e_fwd_ring_up(struct i40e_vsi *vsi, struct net_device *vdev,
7767 			    struct i40e_fwd_adapter *fwd)
7768 {
7769 	struct i40e_channel *ch = NULL, *ch_tmp, *iter;
7770 	int ret = 0, num_tc = 1,  i, aq_err;
7771 	struct i40e_pf *pf = vsi->back;
7772 	struct i40e_hw *hw = &pf->hw;
7773 
7774 	/* Go through the list and find an available channel */
7775 	list_for_each_entry_safe(iter, ch_tmp, &vsi->macvlan_list, list) {
7776 		if (!i40e_is_channel_macvlan(iter)) {
7777 			iter->fwd = fwd;
7778 			/* record configuration for macvlan interface in vdev */
7779 			for (i = 0; i < num_tc; i++)
7780 				netdev_bind_sb_channel_queue(vsi->netdev, vdev,
7781 							     i,
7782 							     iter->num_queue_pairs,
7783 							     iter->base_queue);
7784 			for (i = 0; i < iter->num_queue_pairs; i++) {
7785 				struct i40e_ring *tx_ring, *rx_ring;
7786 				u16 pf_q;
7787 
7788 				pf_q = iter->base_queue + i;
7789 
7790 				/* Get to TX ring ptr */
7791 				tx_ring = vsi->tx_rings[pf_q];
7792 				tx_ring->ch = iter;
7793 
7794 				/* Get the RX ring ptr */
7795 				rx_ring = vsi->rx_rings[pf_q];
7796 				rx_ring->ch = iter;
7797 			}
7798 			ch = iter;
7799 			break;
7800 		}
7801 	}
7802 
7803 	if (!ch)
7804 		return -EINVAL;
7805 
7806 	/* Guarantee all rings are updated before we update the
7807 	 * MAC address filter.
7808 	 */
7809 	wmb();
7810 
7811 	/* Add a mac filter */
7812 	ret = i40e_add_macvlan_filter(hw, ch->seid, vdev->dev_addr, &aq_err);
7813 	if (ret) {
7814 		/* if we cannot add the MAC rule then disable the offload */
7815 		macvlan_release_l2fw_offload(vdev);
7816 		for (i = 0; i < ch->num_queue_pairs; i++) {
7817 			struct i40e_ring *rx_ring;
7818 			u16 pf_q;
7819 
7820 			pf_q = ch->base_queue + i;
7821 			rx_ring = vsi->rx_rings[pf_q];
7822 			rx_ring->netdev = NULL;
7823 		}
7824 		dev_info(&pf->pdev->dev,
7825 			 "Error adding mac filter on macvlan err %s, aq_err %s\n",
7826 			  i40e_stat_str(hw, ret),
7827 			  i40e_aq_str(hw, aq_err));
7828 		netdev_err(vdev, "L2fwd offload disabled to L2 filter error\n");
7829 	}
7830 
7831 	return ret;
7832 }
7833 
7834 /**
7835  * i40e_setup_macvlans - create the channels which will be macvlans
7836  * @vsi: the VSI we want to access
7837  * @macvlan_cnt: no. of macvlans to be setup
7838  * @qcnt: no. of Qs per macvlan
7839  * @vdev: macvlan netdevice
7840  */
7841 static int i40e_setup_macvlans(struct i40e_vsi *vsi, u16 macvlan_cnt, u16 qcnt,
7842 			       struct net_device *vdev)
7843 {
7844 	struct i40e_pf *pf = vsi->back;
7845 	struct i40e_hw *hw = &pf->hw;
7846 	struct i40e_vsi_context ctxt;
7847 	u16 sections, qmap, num_qps;
7848 	struct i40e_channel *ch;
7849 	int i, pow, ret = 0;
7850 	u8 offset = 0;
7851 
7852 	if (vsi->type != I40E_VSI_MAIN || !macvlan_cnt)
7853 		return -EINVAL;
7854 
7855 	num_qps = vsi->num_queue_pairs - (macvlan_cnt * qcnt);
7856 
7857 	/* find the next higher power-of-2 of num queue pairs */
7858 	pow = fls(roundup_pow_of_two(num_qps) - 1);
7859 
7860 	qmap = (offset << I40E_AQ_VSI_TC_QUE_OFFSET_SHIFT) |
7861 		(pow << I40E_AQ_VSI_TC_QUE_NUMBER_SHIFT);
7862 
7863 	/* Setup context bits for the main VSI */
7864 	sections = I40E_AQ_VSI_PROP_QUEUE_MAP_VALID;
7865 	sections |= I40E_AQ_VSI_PROP_SCHED_VALID;
7866 	memset(&ctxt, 0, sizeof(ctxt));
7867 	ctxt.seid = vsi->seid;
7868 	ctxt.pf_num = vsi->back->hw.pf_id;
7869 	ctxt.vf_num = 0;
7870 	ctxt.uplink_seid = vsi->uplink_seid;
7871 	ctxt.info = vsi->info;
7872 	ctxt.info.tc_mapping[0] = cpu_to_le16(qmap);
7873 	ctxt.info.mapping_flags |= cpu_to_le16(I40E_AQ_VSI_QUE_MAP_CONTIG);
7874 	ctxt.info.queue_mapping[0] = cpu_to_le16(vsi->base_queue);
7875 	ctxt.info.valid_sections |= cpu_to_le16(sections);
7876 
7877 	/* Reconfigure RSS for main VSI with new max queue count */
7878 	vsi->rss_size = max_t(u16, num_qps, qcnt);
7879 	ret = i40e_vsi_config_rss(vsi);
7880 	if (ret) {
7881 		dev_info(&pf->pdev->dev,
7882 			 "Failed to reconfig RSS for num_queues (%u)\n",
7883 			 vsi->rss_size);
7884 		return ret;
7885 	}
7886 	vsi->reconfig_rss = true;
7887 	dev_dbg(&vsi->back->pdev->dev,
7888 		"Reconfigured RSS with num_queues (%u)\n", vsi->rss_size);
7889 	vsi->next_base_queue = num_qps;
7890 	vsi->cnt_q_avail = vsi->num_queue_pairs - num_qps;
7891 
7892 	/* Update the VSI after updating the VSI queue-mapping
7893 	 * information
7894 	 */
7895 	ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
7896 	if (ret) {
7897 		dev_info(&pf->pdev->dev,
7898 			 "Update vsi tc config failed, err %s aq_err %s\n",
7899 			 i40e_stat_str(hw, ret),
7900 			 i40e_aq_str(hw, hw->aq.asq_last_status));
7901 		return ret;
7902 	}
7903 	/* update the local VSI info with updated queue map */
7904 	i40e_vsi_update_queue_map(vsi, &ctxt);
7905 	vsi->info.valid_sections = 0;
7906 
7907 	/* Create channels for macvlans */
7908 	INIT_LIST_HEAD(&vsi->macvlan_list);
7909 	for (i = 0; i < macvlan_cnt; i++) {
7910 		ch = kzalloc(sizeof(*ch), GFP_KERNEL);
7911 		if (!ch) {
7912 			ret = -ENOMEM;
7913 			goto err_free;
7914 		}
7915 		INIT_LIST_HEAD(&ch->list);
7916 		ch->num_queue_pairs = qcnt;
7917 		if (!i40e_setup_channel(pf, vsi, ch)) {
7918 			ret = -EINVAL;
7919 			kfree(ch);
7920 			goto err_free;
7921 		}
7922 		ch->parent_vsi = vsi;
7923 		vsi->cnt_q_avail -= ch->num_queue_pairs;
7924 		vsi->macvlan_cnt++;
7925 		list_add_tail(&ch->list, &vsi->macvlan_list);
7926 	}
7927 
7928 	return ret;
7929 
7930 err_free:
7931 	dev_info(&pf->pdev->dev, "Failed to setup macvlans\n");
7932 	i40e_free_macvlan_channels(vsi);
7933 
7934 	return ret;
7935 }
7936 
7937 /**
7938  * i40e_fwd_add - configure macvlans
7939  * @netdev: net device to configure
7940  * @vdev: macvlan netdevice
7941  **/
7942 static void *i40e_fwd_add(struct net_device *netdev, struct net_device *vdev)
7943 {
7944 	struct i40e_netdev_priv *np = netdev_priv(netdev);
7945 	u16 q_per_macvlan = 0, macvlan_cnt = 0, vectors;
7946 	struct i40e_vsi *vsi = np->vsi;
7947 	struct i40e_pf *pf = vsi->back;
7948 	struct i40e_fwd_adapter *fwd;
7949 	int avail_macvlan, ret;
7950 
7951 	if ((pf->flags & I40E_FLAG_DCB_ENABLED)) {
7952 		netdev_info(netdev, "Macvlans are not supported when DCB is enabled\n");
7953 		return ERR_PTR(-EINVAL);
7954 	}
7955 	if (i40e_is_tc_mqprio_enabled(pf)) {
7956 		netdev_info(netdev, "Macvlans are not supported when HW TC offload is on\n");
7957 		return ERR_PTR(-EINVAL);
7958 	}
7959 	if (pf->num_lan_msix < I40E_MIN_MACVLAN_VECTORS) {
7960 		netdev_info(netdev, "Not enough vectors available to support macvlans\n");
7961 		return ERR_PTR(-EINVAL);
7962 	}
7963 
7964 	/* The macvlan device has to be a single Q device so that the
7965 	 * tc_to_txq field can be reused to pick the tx queue.
7966 	 */
7967 	if (netif_is_multiqueue(vdev))
7968 		return ERR_PTR(-ERANGE);
7969 
7970 	if (!vsi->macvlan_cnt) {
7971 		/* reserve bit 0 for the pf device */
7972 		set_bit(0, vsi->fwd_bitmask);
7973 
7974 		/* Try to reserve as many queues as possible for macvlans. First
7975 		 * reserve 3/4th of max vectors, then half, then quarter and
7976 		 * calculate Qs per macvlan as you go
7977 		 */
7978 		vectors = pf->num_lan_msix;
7979 		if (vectors <= I40E_MAX_MACVLANS && vectors > 64) {
7980 			/* allocate 4 Qs per macvlan and 32 Qs to the PF*/
7981 			q_per_macvlan = 4;
7982 			macvlan_cnt = (vectors - 32) / 4;
7983 		} else if (vectors <= 64 && vectors > 32) {
7984 			/* allocate 2 Qs per macvlan and 16 Qs to the PF*/
7985 			q_per_macvlan = 2;
7986 			macvlan_cnt = (vectors - 16) / 2;
7987 		} else if (vectors <= 32 && vectors > 16) {
7988 			/* allocate 1 Q per macvlan and 16 Qs to the PF*/
7989 			q_per_macvlan = 1;
7990 			macvlan_cnt = vectors - 16;
7991 		} else if (vectors <= 16 && vectors > 8) {
7992 			/* allocate 1 Q per macvlan and 8 Qs to the PF */
7993 			q_per_macvlan = 1;
7994 			macvlan_cnt = vectors - 8;
7995 		} else {
7996 			/* allocate 1 Q per macvlan and 1 Q to the PF */
7997 			q_per_macvlan = 1;
7998 			macvlan_cnt = vectors - 1;
7999 		}
8000 
8001 		if (macvlan_cnt == 0)
8002 			return ERR_PTR(-EBUSY);
8003 
8004 		/* Quiesce VSI queues */
8005 		i40e_quiesce_vsi(vsi);
8006 
8007 		/* sets up the macvlans but does not "enable" them */
8008 		ret = i40e_setup_macvlans(vsi, macvlan_cnt, q_per_macvlan,
8009 					  vdev);
8010 		if (ret)
8011 			return ERR_PTR(ret);
8012 
8013 		/* Unquiesce VSI */
8014 		i40e_unquiesce_vsi(vsi);
8015 	}
8016 	avail_macvlan = find_first_zero_bit(vsi->fwd_bitmask,
8017 					    vsi->macvlan_cnt);
8018 	if (avail_macvlan >= I40E_MAX_MACVLANS)
8019 		return ERR_PTR(-EBUSY);
8020 
8021 	/* create the fwd struct */
8022 	fwd = kzalloc(sizeof(*fwd), GFP_KERNEL);
8023 	if (!fwd)
8024 		return ERR_PTR(-ENOMEM);
8025 
8026 	set_bit(avail_macvlan, vsi->fwd_bitmask);
8027 	fwd->bit_no = avail_macvlan;
8028 	netdev_set_sb_channel(vdev, avail_macvlan);
8029 	fwd->netdev = vdev;
8030 
8031 	if (!netif_running(netdev))
8032 		return fwd;
8033 
8034 	/* Set fwd ring up */
8035 	ret = i40e_fwd_ring_up(vsi, vdev, fwd);
8036 	if (ret) {
8037 		/* unbind the queues and drop the subordinate channel config */
8038 		netdev_unbind_sb_channel(netdev, vdev);
8039 		netdev_set_sb_channel(vdev, 0);
8040 
8041 		kfree(fwd);
8042 		return ERR_PTR(-EINVAL);
8043 	}
8044 
8045 	return fwd;
8046 }
8047 
8048 /**
8049  * i40e_del_all_macvlans - Delete all the mac filters on the channels
8050  * @vsi: the VSI we want to access
8051  */
8052 static void i40e_del_all_macvlans(struct i40e_vsi *vsi)
8053 {
8054 	struct i40e_channel *ch, *ch_tmp;
8055 	struct i40e_pf *pf = vsi->back;
8056 	struct i40e_hw *hw = &pf->hw;
8057 	int aq_err, ret = 0;
8058 
8059 	if (list_empty(&vsi->macvlan_list))
8060 		return;
8061 
8062 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8063 		if (i40e_is_channel_macvlan(ch)) {
8064 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8065 						      i40e_channel_mac(ch),
8066 						      &aq_err);
8067 			if (!ret) {
8068 				/* Reset queue contexts */
8069 				i40e_reset_ch_rings(vsi, ch);
8070 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8071 				netdev_unbind_sb_channel(vsi->netdev,
8072 							 ch->fwd->netdev);
8073 				netdev_set_sb_channel(ch->fwd->netdev, 0);
8074 				kfree(ch->fwd);
8075 				ch->fwd = NULL;
8076 			}
8077 		}
8078 	}
8079 }
8080 
8081 /**
8082  * i40e_fwd_del - delete macvlan interfaces
8083  * @netdev: net device to configure
8084  * @vdev: macvlan netdevice
8085  */
8086 static void i40e_fwd_del(struct net_device *netdev, void *vdev)
8087 {
8088 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8089 	struct i40e_fwd_adapter *fwd = vdev;
8090 	struct i40e_channel *ch, *ch_tmp;
8091 	struct i40e_vsi *vsi = np->vsi;
8092 	struct i40e_pf *pf = vsi->back;
8093 	struct i40e_hw *hw = &pf->hw;
8094 	int aq_err, ret = 0;
8095 
8096 	/* Find the channel associated with the macvlan and del mac filter */
8097 	list_for_each_entry_safe(ch, ch_tmp, &vsi->macvlan_list, list) {
8098 		if (i40e_is_channel_macvlan(ch) &&
8099 		    ether_addr_equal(i40e_channel_mac(ch),
8100 				     fwd->netdev->dev_addr)) {
8101 			ret = i40e_del_macvlan_filter(hw, ch->seid,
8102 						      i40e_channel_mac(ch),
8103 						      &aq_err);
8104 			if (!ret) {
8105 				/* Reset queue contexts */
8106 				i40e_reset_ch_rings(vsi, ch);
8107 				clear_bit(ch->fwd->bit_no, vsi->fwd_bitmask);
8108 				netdev_unbind_sb_channel(netdev, fwd->netdev);
8109 				netdev_set_sb_channel(fwd->netdev, 0);
8110 				kfree(ch->fwd);
8111 				ch->fwd = NULL;
8112 			} else {
8113 				dev_info(&pf->pdev->dev,
8114 					 "Error deleting mac filter on macvlan err %s, aq_err %s\n",
8115 					  i40e_stat_str(hw, ret),
8116 					  i40e_aq_str(hw, aq_err));
8117 			}
8118 			break;
8119 		}
8120 	}
8121 }
8122 
8123 /**
8124  * i40e_setup_tc - configure multiple traffic classes
8125  * @netdev: net device to configure
8126  * @type_data: tc offload data
8127  **/
8128 static int i40e_setup_tc(struct net_device *netdev, void *type_data)
8129 {
8130 	struct tc_mqprio_qopt_offload *mqprio_qopt = type_data;
8131 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8132 	struct i40e_vsi *vsi = np->vsi;
8133 	struct i40e_pf *pf = vsi->back;
8134 	u8 enabled_tc = 0, num_tc, hw;
8135 	bool need_reset = false;
8136 	int old_queue_pairs;
8137 	int ret = -EINVAL;
8138 	u16 mode;
8139 	int i;
8140 
8141 	old_queue_pairs = vsi->num_queue_pairs;
8142 	num_tc = mqprio_qopt->qopt.num_tc;
8143 	hw = mqprio_qopt->qopt.hw;
8144 	mode = mqprio_qopt->mode;
8145 	if (!hw) {
8146 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8147 		memcpy(&vsi->mqprio_qopt, mqprio_qopt, sizeof(*mqprio_qopt));
8148 		goto config_tc;
8149 	}
8150 
8151 	/* Check if MFP enabled */
8152 	if (pf->flags & I40E_FLAG_MFP_ENABLED) {
8153 		netdev_info(netdev,
8154 			    "Configuring TC not supported in MFP mode\n");
8155 		return ret;
8156 	}
8157 	switch (mode) {
8158 	case TC_MQPRIO_MODE_DCB:
8159 		pf->flags &= ~I40E_FLAG_TC_MQPRIO;
8160 
8161 		/* Check if DCB enabled to continue */
8162 		if (!(pf->flags & I40E_FLAG_DCB_ENABLED)) {
8163 			netdev_info(netdev,
8164 				    "DCB is not enabled for adapter\n");
8165 			return ret;
8166 		}
8167 
8168 		/* Check whether tc count is within enabled limit */
8169 		if (num_tc > i40e_pf_get_num_tc(pf)) {
8170 			netdev_info(netdev,
8171 				    "TC count greater than enabled on link for adapter\n");
8172 			return ret;
8173 		}
8174 		break;
8175 	case TC_MQPRIO_MODE_CHANNEL:
8176 		if (pf->flags & I40E_FLAG_DCB_ENABLED) {
8177 			netdev_info(netdev,
8178 				    "Full offload of TC Mqprio options is not supported when DCB is enabled\n");
8179 			return ret;
8180 		}
8181 		if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
8182 			return ret;
8183 		ret = i40e_validate_mqprio_qopt(vsi, mqprio_qopt);
8184 		if (ret)
8185 			return ret;
8186 		memcpy(&vsi->mqprio_qopt, mqprio_qopt,
8187 		       sizeof(*mqprio_qopt));
8188 		pf->flags |= I40E_FLAG_TC_MQPRIO;
8189 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
8190 		break;
8191 	default:
8192 		return -EINVAL;
8193 	}
8194 
8195 config_tc:
8196 	/* Generate TC map for number of tc requested */
8197 	for (i = 0; i < num_tc; i++)
8198 		enabled_tc |= BIT(i);
8199 
8200 	/* Requesting same TC configuration as already enabled */
8201 	if (enabled_tc == vsi->tc_config.enabled_tc &&
8202 	    mode != TC_MQPRIO_MODE_CHANNEL)
8203 		return 0;
8204 
8205 	/* Quiesce VSI queues */
8206 	i40e_quiesce_vsi(vsi);
8207 
8208 	if (!hw && !i40e_is_tc_mqprio_enabled(pf))
8209 		i40e_remove_queue_channels(vsi);
8210 
8211 	/* Configure VSI for enabled TCs */
8212 	ret = i40e_vsi_config_tc(vsi, enabled_tc);
8213 	if (ret) {
8214 		netdev_info(netdev, "Failed configuring TC for VSI seid=%d\n",
8215 			    vsi->seid);
8216 		need_reset = true;
8217 		goto exit;
8218 	} else if (enabled_tc &&
8219 		   (!is_power_of_2(vsi->tc_config.tc_info[0].qcount))) {
8220 		netdev_info(netdev,
8221 			    "Failed to create channel. Override queues (%u) not power of 2\n",
8222 			    vsi->tc_config.tc_info[0].qcount);
8223 		ret = -EINVAL;
8224 		need_reset = true;
8225 		goto exit;
8226 	}
8227 
8228 	dev_info(&vsi->back->pdev->dev,
8229 		 "Setup channel (id:%u) utilizing num_queues %d\n",
8230 		 vsi->seid, vsi->tc_config.tc_info[0].qcount);
8231 
8232 	if (i40e_is_tc_mqprio_enabled(pf)) {
8233 		if (vsi->mqprio_qopt.max_rate[0]) {
8234 			u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
8235 						  vsi->mqprio_qopt.max_rate[0]);
8236 
8237 			ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
8238 			if (!ret) {
8239 				u64 credits = max_tx_rate;
8240 
8241 				do_div(credits, I40E_BW_CREDIT_DIVISOR);
8242 				dev_dbg(&vsi->back->pdev->dev,
8243 					"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
8244 					max_tx_rate,
8245 					credits,
8246 					vsi->seid);
8247 			} else {
8248 				need_reset = true;
8249 				goto exit;
8250 			}
8251 		}
8252 		ret = i40e_configure_queue_channels(vsi);
8253 		if (ret) {
8254 			vsi->num_queue_pairs = old_queue_pairs;
8255 			netdev_info(netdev,
8256 				    "Failed configuring queue channels\n");
8257 			need_reset = true;
8258 			goto exit;
8259 		}
8260 	}
8261 
8262 exit:
8263 	/* Reset the configuration data to defaults, only TC0 is enabled */
8264 	if (need_reset) {
8265 		i40e_vsi_set_default_tc_config(vsi);
8266 		need_reset = false;
8267 	}
8268 
8269 	/* Unquiesce VSI */
8270 	i40e_unquiesce_vsi(vsi);
8271 	return ret;
8272 }
8273 
8274 /**
8275  * i40e_set_cld_element - sets cloud filter element data
8276  * @filter: cloud filter rule
8277  * @cld: ptr to cloud filter element data
8278  *
8279  * This is helper function to copy data into cloud filter element
8280  **/
8281 static inline void
8282 i40e_set_cld_element(struct i40e_cloud_filter *filter,
8283 		     struct i40e_aqc_cloud_filters_element_data *cld)
8284 {
8285 	u32 ipa;
8286 	int i;
8287 
8288 	memset(cld, 0, sizeof(*cld));
8289 	ether_addr_copy(cld->outer_mac, filter->dst_mac);
8290 	ether_addr_copy(cld->inner_mac, filter->src_mac);
8291 
8292 	if (filter->n_proto != ETH_P_IP && filter->n_proto != ETH_P_IPV6)
8293 		return;
8294 
8295 	if (filter->n_proto == ETH_P_IPV6) {
8296 #define IPV6_MAX_INDEX	(ARRAY_SIZE(filter->dst_ipv6) - 1)
8297 		for (i = 0; i < ARRAY_SIZE(filter->dst_ipv6); i++) {
8298 			ipa = be32_to_cpu(filter->dst_ipv6[IPV6_MAX_INDEX - i]);
8299 
8300 			*(__le32 *)&cld->ipaddr.raw_v6.data[i * 2] = cpu_to_le32(ipa);
8301 		}
8302 	} else {
8303 		ipa = be32_to_cpu(filter->dst_ipv4);
8304 
8305 		memcpy(&cld->ipaddr.v4.data, &ipa, sizeof(ipa));
8306 	}
8307 
8308 	cld->inner_vlan = cpu_to_le16(ntohs(filter->vlan_id));
8309 
8310 	/* tenant_id is not supported by FW now, once the support is enabled
8311 	 * fill the cld->tenant_id with cpu_to_le32(filter->tenant_id)
8312 	 */
8313 	if (filter->tenant_id)
8314 		return;
8315 }
8316 
8317 /**
8318  * i40e_add_del_cloud_filter - Add/del cloud filter
8319  * @vsi: pointer to VSI
8320  * @filter: cloud filter rule
8321  * @add: if true, add, if false, delete
8322  *
8323  * Add or delete a cloud filter for a specific flow spec.
8324  * Returns 0 if the filter were successfully added.
8325  **/
8326 int i40e_add_del_cloud_filter(struct i40e_vsi *vsi,
8327 			      struct i40e_cloud_filter *filter, bool add)
8328 {
8329 	struct i40e_aqc_cloud_filters_element_data cld_filter;
8330 	struct i40e_pf *pf = vsi->back;
8331 	int ret;
8332 	static const u16 flag_table[128] = {
8333 		[I40E_CLOUD_FILTER_FLAGS_OMAC]  =
8334 			I40E_AQC_ADD_CLOUD_FILTER_OMAC,
8335 		[I40E_CLOUD_FILTER_FLAGS_IMAC]  =
8336 			I40E_AQC_ADD_CLOUD_FILTER_IMAC,
8337 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN]  =
8338 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN,
8339 		[I40E_CLOUD_FILTER_FLAGS_IMAC_TEN_ID] =
8340 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_TEN_ID,
8341 		[I40E_CLOUD_FILTER_FLAGS_OMAC_TEN_ID_IMAC] =
8342 			I40E_AQC_ADD_CLOUD_FILTER_OMAC_TEN_ID_IMAC,
8343 		[I40E_CLOUD_FILTER_FLAGS_IMAC_IVLAN_TEN_ID] =
8344 			I40E_AQC_ADD_CLOUD_FILTER_IMAC_IVLAN_TEN_ID,
8345 		[I40E_CLOUD_FILTER_FLAGS_IIP] =
8346 			I40E_AQC_ADD_CLOUD_FILTER_IIP,
8347 	};
8348 
8349 	if (filter->flags >= ARRAY_SIZE(flag_table))
8350 		return I40E_ERR_CONFIG;
8351 
8352 	memset(&cld_filter, 0, sizeof(cld_filter));
8353 
8354 	/* copy element needed to add cloud filter from filter */
8355 	i40e_set_cld_element(filter, &cld_filter);
8356 
8357 	if (filter->tunnel_type != I40E_CLOUD_TNL_TYPE_NONE)
8358 		cld_filter.flags = cpu_to_le16(filter->tunnel_type <<
8359 					     I40E_AQC_ADD_CLOUD_TNL_TYPE_SHIFT);
8360 
8361 	if (filter->n_proto == ETH_P_IPV6)
8362 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8363 						I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8364 	else
8365 		cld_filter.flags |= cpu_to_le16(flag_table[filter->flags] |
8366 						I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8367 
8368 	if (add)
8369 		ret = i40e_aq_add_cloud_filters(&pf->hw, filter->seid,
8370 						&cld_filter, 1);
8371 	else
8372 		ret = i40e_aq_rem_cloud_filters(&pf->hw, filter->seid,
8373 						&cld_filter, 1);
8374 	if (ret)
8375 		dev_dbg(&pf->pdev->dev,
8376 			"Failed to %s cloud filter using l4 port %u, err %d aq_err %d\n",
8377 			add ? "add" : "delete", filter->dst_port, ret,
8378 			pf->hw.aq.asq_last_status);
8379 	else
8380 		dev_info(&pf->pdev->dev,
8381 			 "%s cloud filter for VSI: %d\n",
8382 			 add ? "Added" : "Deleted", filter->seid);
8383 	return ret;
8384 }
8385 
8386 /**
8387  * i40e_add_del_cloud_filter_big_buf - Add/del cloud filter using big_buf
8388  * @vsi: pointer to VSI
8389  * @filter: cloud filter rule
8390  * @add: if true, add, if false, delete
8391  *
8392  * Add or delete a cloud filter for a specific flow spec using big buffer.
8393  * Returns 0 if the filter were successfully added.
8394  **/
8395 int i40e_add_del_cloud_filter_big_buf(struct i40e_vsi *vsi,
8396 				      struct i40e_cloud_filter *filter,
8397 				      bool add)
8398 {
8399 	struct i40e_aqc_cloud_filters_element_bb cld_filter;
8400 	struct i40e_pf *pf = vsi->back;
8401 	int ret;
8402 
8403 	/* Both (src/dst) valid mac_addr are not supported */
8404 	if ((is_valid_ether_addr(filter->dst_mac) &&
8405 	     is_valid_ether_addr(filter->src_mac)) ||
8406 	    (is_multicast_ether_addr(filter->dst_mac) &&
8407 	     is_multicast_ether_addr(filter->src_mac)))
8408 		return -EOPNOTSUPP;
8409 
8410 	/* Big buffer cloud filter needs 'L4 port' to be non-zero. Also, UDP
8411 	 * ports are not supported via big buffer now.
8412 	 */
8413 	if (!filter->dst_port || filter->ip_proto == IPPROTO_UDP)
8414 		return -EOPNOTSUPP;
8415 
8416 	/* adding filter using src_port/src_ip is not supported at this stage */
8417 	if (filter->src_port ||
8418 	    (filter->src_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8419 	    !ipv6_addr_any(&filter->ip.v6.src_ip6))
8420 		return -EOPNOTSUPP;
8421 
8422 	memset(&cld_filter, 0, sizeof(cld_filter));
8423 
8424 	/* copy element needed to add cloud filter from filter */
8425 	i40e_set_cld_element(filter, &cld_filter.element);
8426 
8427 	if (is_valid_ether_addr(filter->dst_mac) ||
8428 	    is_valid_ether_addr(filter->src_mac) ||
8429 	    is_multicast_ether_addr(filter->dst_mac) ||
8430 	    is_multicast_ether_addr(filter->src_mac)) {
8431 		/* MAC + IP : unsupported mode */
8432 		if (filter->dst_ipv4)
8433 			return -EOPNOTSUPP;
8434 
8435 		/* since we validated that L4 port must be valid before
8436 		 * we get here, start with respective "flags" value
8437 		 * and update if vlan is present or not
8438 		 */
8439 		cld_filter.element.flags =
8440 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_PORT);
8441 
8442 		if (filter->vlan_id) {
8443 			cld_filter.element.flags =
8444 			cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_MAC_VLAN_PORT);
8445 		}
8446 
8447 	} else if ((filter->dst_ipv4 && filter->n_proto != ETH_P_IPV6) ||
8448 		   !ipv6_addr_any(&filter->ip.v6.dst_ip6)) {
8449 		cld_filter.element.flags =
8450 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FILTER_IP_PORT);
8451 		if (filter->n_proto == ETH_P_IPV6)
8452 			cld_filter.element.flags |=
8453 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV6);
8454 		else
8455 			cld_filter.element.flags |=
8456 				cpu_to_le16(I40E_AQC_ADD_CLOUD_FLAGS_IPV4);
8457 	} else {
8458 		dev_err(&pf->pdev->dev,
8459 			"either mac or ip has to be valid for cloud filter\n");
8460 		return -EINVAL;
8461 	}
8462 
8463 	/* Now copy L4 port in Byte 6..7 in general fields */
8464 	cld_filter.general_fields[I40E_AQC_ADD_CLOUD_FV_FLU_0X16_WORD0] =
8465 						be16_to_cpu(filter->dst_port);
8466 
8467 	if (add) {
8468 		/* Validate current device switch mode, change if necessary */
8469 		ret = i40e_validate_and_set_switch_mode(vsi);
8470 		if (ret) {
8471 			dev_err(&pf->pdev->dev,
8472 				"failed to set switch mode, ret %d\n",
8473 				ret);
8474 			return ret;
8475 		}
8476 
8477 		ret = i40e_aq_add_cloud_filters_bb(&pf->hw, filter->seid,
8478 						   &cld_filter, 1);
8479 	} else {
8480 		ret = i40e_aq_rem_cloud_filters_bb(&pf->hw, filter->seid,
8481 						   &cld_filter, 1);
8482 	}
8483 
8484 	if (ret)
8485 		dev_dbg(&pf->pdev->dev,
8486 			"Failed to %s cloud filter(big buffer) err %d aq_err %d\n",
8487 			add ? "add" : "delete", ret, pf->hw.aq.asq_last_status);
8488 	else
8489 		dev_info(&pf->pdev->dev,
8490 			 "%s cloud filter for VSI: %d, L4 port: %d\n",
8491 			 add ? "add" : "delete", filter->seid,
8492 			 ntohs(filter->dst_port));
8493 	return ret;
8494 }
8495 
8496 /**
8497  * i40e_parse_cls_flower - Parse tc flower filters provided by kernel
8498  * @vsi: Pointer to VSI
8499  * @f: Pointer to struct flow_cls_offload
8500  * @filter: Pointer to cloud filter structure
8501  *
8502  **/
8503 static int i40e_parse_cls_flower(struct i40e_vsi *vsi,
8504 				 struct flow_cls_offload *f,
8505 				 struct i40e_cloud_filter *filter)
8506 {
8507 	struct flow_rule *rule = flow_cls_offload_flow_rule(f);
8508 	struct flow_dissector *dissector = rule->match.dissector;
8509 	u16 n_proto_mask = 0, n_proto_key = 0, addr_type = 0;
8510 	struct i40e_pf *pf = vsi->back;
8511 	u8 field_flags = 0;
8512 
8513 	if (dissector->used_keys &
8514 	    ~(BIT(FLOW_DISSECTOR_KEY_CONTROL) |
8515 	      BIT(FLOW_DISSECTOR_KEY_BASIC) |
8516 	      BIT(FLOW_DISSECTOR_KEY_ETH_ADDRS) |
8517 	      BIT(FLOW_DISSECTOR_KEY_VLAN) |
8518 	      BIT(FLOW_DISSECTOR_KEY_IPV4_ADDRS) |
8519 	      BIT(FLOW_DISSECTOR_KEY_IPV6_ADDRS) |
8520 	      BIT(FLOW_DISSECTOR_KEY_PORTS) |
8521 	      BIT(FLOW_DISSECTOR_KEY_ENC_KEYID))) {
8522 		dev_err(&pf->pdev->dev, "Unsupported key used: 0x%x\n",
8523 			dissector->used_keys);
8524 		return -EOPNOTSUPP;
8525 	}
8526 
8527 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ENC_KEYID)) {
8528 		struct flow_match_enc_keyid match;
8529 
8530 		flow_rule_match_enc_keyid(rule, &match);
8531 		if (match.mask->keyid != 0)
8532 			field_flags |= I40E_CLOUD_FIELD_TEN_ID;
8533 
8534 		filter->tenant_id = be32_to_cpu(match.key->keyid);
8535 	}
8536 
8537 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_BASIC)) {
8538 		struct flow_match_basic match;
8539 
8540 		flow_rule_match_basic(rule, &match);
8541 		n_proto_key = ntohs(match.key->n_proto);
8542 		n_proto_mask = ntohs(match.mask->n_proto);
8543 
8544 		if (n_proto_key == ETH_P_ALL) {
8545 			n_proto_key = 0;
8546 			n_proto_mask = 0;
8547 		}
8548 		filter->n_proto = n_proto_key & n_proto_mask;
8549 		filter->ip_proto = match.key->ip_proto;
8550 	}
8551 
8552 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_ETH_ADDRS)) {
8553 		struct flow_match_eth_addrs match;
8554 
8555 		flow_rule_match_eth_addrs(rule, &match);
8556 
8557 		/* use is_broadcast and is_zero to check for all 0xf or 0 */
8558 		if (!is_zero_ether_addr(match.mask->dst)) {
8559 			if (is_broadcast_ether_addr(match.mask->dst)) {
8560 				field_flags |= I40E_CLOUD_FIELD_OMAC;
8561 			} else {
8562 				dev_err(&pf->pdev->dev, "Bad ether dest mask %pM\n",
8563 					match.mask->dst);
8564 				return I40E_ERR_CONFIG;
8565 			}
8566 		}
8567 
8568 		if (!is_zero_ether_addr(match.mask->src)) {
8569 			if (is_broadcast_ether_addr(match.mask->src)) {
8570 				field_flags |= I40E_CLOUD_FIELD_IMAC;
8571 			} else {
8572 				dev_err(&pf->pdev->dev, "Bad ether src mask %pM\n",
8573 					match.mask->src);
8574 				return I40E_ERR_CONFIG;
8575 			}
8576 		}
8577 		ether_addr_copy(filter->dst_mac, match.key->dst);
8578 		ether_addr_copy(filter->src_mac, match.key->src);
8579 	}
8580 
8581 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_VLAN)) {
8582 		struct flow_match_vlan match;
8583 
8584 		flow_rule_match_vlan(rule, &match);
8585 		if (match.mask->vlan_id) {
8586 			if (match.mask->vlan_id == VLAN_VID_MASK) {
8587 				field_flags |= I40E_CLOUD_FIELD_IVLAN;
8588 
8589 			} else {
8590 				dev_err(&pf->pdev->dev, "Bad vlan mask 0x%04x\n",
8591 					match.mask->vlan_id);
8592 				return I40E_ERR_CONFIG;
8593 			}
8594 		}
8595 
8596 		filter->vlan_id = cpu_to_be16(match.key->vlan_id);
8597 	}
8598 
8599 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_CONTROL)) {
8600 		struct flow_match_control match;
8601 
8602 		flow_rule_match_control(rule, &match);
8603 		addr_type = match.key->addr_type;
8604 	}
8605 
8606 	if (addr_type == FLOW_DISSECTOR_KEY_IPV4_ADDRS) {
8607 		struct flow_match_ipv4_addrs match;
8608 
8609 		flow_rule_match_ipv4_addrs(rule, &match);
8610 		if (match.mask->dst) {
8611 			if (match.mask->dst == cpu_to_be32(0xffffffff)) {
8612 				field_flags |= I40E_CLOUD_FIELD_IIP;
8613 			} else {
8614 				dev_err(&pf->pdev->dev, "Bad ip dst mask %pI4b\n",
8615 					&match.mask->dst);
8616 				return I40E_ERR_CONFIG;
8617 			}
8618 		}
8619 
8620 		if (match.mask->src) {
8621 			if (match.mask->src == cpu_to_be32(0xffffffff)) {
8622 				field_flags |= I40E_CLOUD_FIELD_IIP;
8623 			} else {
8624 				dev_err(&pf->pdev->dev, "Bad ip src mask %pI4b\n",
8625 					&match.mask->src);
8626 				return I40E_ERR_CONFIG;
8627 			}
8628 		}
8629 
8630 		if (field_flags & I40E_CLOUD_FIELD_TEN_ID) {
8631 			dev_err(&pf->pdev->dev, "Tenant id not allowed for ip filter\n");
8632 			return I40E_ERR_CONFIG;
8633 		}
8634 		filter->dst_ipv4 = match.key->dst;
8635 		filter->src_ipv4 = match.key->src;
8636 	}
8637 
8638 	if (addr_type == FLOW_DISSECTOR_KEY_IPV6_ADDRS) {
8639 		struct flow_match_ipv6_addrs match;
8640 
8641 		flow_rule_match_ipv6_addrs(rule, &match);
8642 
8643 		/* src and dest IPV6 address should not be LOOPBACK
8644 		 * (0:0:0:0:0:0:0:1), which can be represented as ::1
8645 		 */
8646 		if (ipv6_addr_loopback(&match.key->dst) ||
8647 		    ipv6_addr_loopback(&match.key->src)) {
8648 			dev_err(&pf->pdev->dev,
8649 				"Bad ipv6, addr is LOOPBACK\n");
8650 			return I40E_ERR_CONFIG;
8651 		}
8652 		if (!ipv6_addr_any(&match.mask->dst) ||
8653 		    !ipv6_addr_any(&match.mask->src))
8654 			field_flags |= I40E_CLOUD_FIELD_IIP;
8655 
8656 		memcpy(&filter->src_ipv6, &match.key->src.s6_addr32,
8657 		       sizeof(filter->src_ipv6));
8658 		memcpy(&filter->dst_ipv6, &match.key->dst.s6_addr32,
8659 		       sizeof(filter->dst_ipv6));
8660 	}
8661 
8662 	if (flow_rule_match_key(rule, FLOW_DISSECTOR_KEY_PORTS)) {
8663 		struct flow_match_ports match;
8664 
8665 		flow_rule_match_ports(rule, &match);
8666 		if (match.mask->src) {
8667 			if (match.mask->src == cpu_to_be16(0xffff)) {
8668 				field_flags |= I40E_CLOUD_FIELD_IIP;
8669 			} else {
8670 				dev_err(&pf->pdev->dev, "Bad src port mask 0x%04x\n",
8671 					be16_to_cpu(match.mask->src));
8672 				return I40E_ERR_CONFIG;
8673 			}
8674 		}
8675 
8676 		if (match.mask->dst) {
8677 			if (match.mask->dst == cpu_to_be16(0xffff)) {
8678 				field_flags |= I40E_CLOUD_FIELD_IIP;
8679 			} else {
8680 				dev_err(&pf->pdev->dev, "Bad dst port mask 0x%04x\n",
8681 					be16_to_cpu(match.mask->dst));
8682 				return I40E_ERR_CONFIG;
8683 			}
8684 		}
8685 
8686 		filter->dst_port = match.key->dst;
8687 		filter->src_port = match.key->src;
8688 
8689 		switch (filter->ip_proto) {
8690 		case IPPROTO_TCP:
8691 		case IPPROTO_UDP:
8692 			break;
8693 		default:
8694 			dev_err(&pf->pdev->dev,
8695 				"Only UDP and TCP transport are supported\n");
8696 			return -EINVAL;
8697 		}
8698 	}
8699 	filter->flags = field_flags;
8700 	return 0;
8701 }
8702 
8703 /**
8704  * i40e_handle_tclass: Forward to a traffic class on the device
8705  * @vsi: Pointer to VSI
8706  * @tc: traffic class index on the device
8707  * @filter: Pointer to cloud filter structure
8708  *
8709  **/
8710 static int i40e_handle_tclass(struct i40e_vsi *vsi, u32 tc,
8711 			      struct i40e_cloud_filter *filter)
8712 {
8713 	struct i40e_channel *ch, *ch_tmp;
8714 
8715 	/* direct to a traffic class on the same device */
8716 	if (tc == 0) {
8717 		filter->seid = vsi->seid;
8718 		return 0;
8719 	} else if (vsi->tc_config.enabled_tc & BIT(tc)) {
8720 		if (!filter->dst_port) {
8721 			dev_err(&vsi->back->pdev->dev,
8722 				"Specify destination port to direct to traffic class that is not default\n");
8723 			return -EINVAL;
8724 		}
8725 		if (list_empty(&vsi->ch_list))
8726 			return -EINVAL;
8727 		list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list,
8728 					 list) {
8729 			if (ch->seid == vsi->tc_seid_map[tc])
8730 				filter->seid = ch->seid;
8731 		}
8732 		return 0;
8733 	}
8734 	dev_err(&vsi->back->pdev->dev, "TC is not enabled\n");
8735 	return -EINVAL;
8736 }
8737 
8738 /**
8739  * i40e_configure_clsflower - Configure tc flower filters
8740  * @vsi: Pointer to VSI
8741  * @cls_flower: Pointer to struct flow_cls_offload
8742  *
8743  **/
8744 static int i40e_configure_clsflower(struct i40e_vsi *vsi,
8745 				    struct flow_cls_offload *cls_flower)
8746 {
8747 	int tc = tc_classid_to_hwtc(vsi->netdev, cls_flower->classid);
8748 	struct i40e_cloud_filter *filter = NULL;
8749 	struct i40e_pf *pf = vsi->back;
8750 	int err = 0;
8751 
8752 	if (tc < 0) {
8753 		dev_err(&vsi->back->pdev->dev, "Invalid traffic class\n");
8754 		return -EOPNOTSUPP;
8755 	}
8756 
8757 	if (!tc) {
8758 		dev_err(&pf->pdev->dev, "Unable to add filter because of invalid destination");
8759 		return -EINVAL;
8760 	}
8761 
8762 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
8763 	    test_bit(__I40E_RESET_INTR_RECEIVED, pf->state))
8764 		return -EBUSY;
8765 
8766 	if (pf->fdir_pf_active_filters ||
8767 	    (!hlist_empty(&pf->fdir_filter_list))) {
8768 		dev_err(&vsi->back->pdev->dev,
8769 			"Flow Director Sideband filters exists, turn ntuple off to configure cloud filters\n");
8770 		return -EINVAL;
8771 	}
8772 
8773 	if (vsi->back->flags & I40E_FLAG_FD_SB_ENABLED) {
8774 		dev_err(&vsi->back->pdev->dev,
8775 			"Disable Flow Director Sideband, configuring Cloud filters via tc-flower\n");
8776 		vsi->back->flags &= ~I40E_FLAG_FD_SB_ENABLED;
8777 		vsi->back->flags |= I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8778 	}
8779 
8780 	filter = kzalloc(sizeof(*filter), GFP_KERNEL);
8781 	if (!filter)
8782 		return -ENOMEM;
8783 
8784 	filter->cookie = cls_flower->cookie;
8785 
8786 	err = i40e_parse_cls_flower(vsi, cls_flower, filter);
8787 	if (err < 0)
8788 		goto err;
8789 
8790 	err = i40e_handle_tclass(vsi, tc, filter);
8791 	if (err < 0)
8792 		goto err;
8793 
8794 	/* Add cloud filter */
8795 	if (filter->dst_port)
8796 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, true);
8797 	else
8798 		err = i40e_add_del_cloud_filter(vsi, filter, true);
8799 
8800 	if (err) {
8801 		dev_err(&pf->pdev->dev, "Failed to add cloud filter, err %d\n",
8802 			err);
8803 		goto err;
8804 	}
8805 
8806 	/* add filter to the ordered list */
8807 	INIT_HLIST_NODE(&filter->cloud_node);
8808 
8809 	hlist_add_head(&filter->cloud_node, &pf->cloud_filter_list);
8810 
8811 	pf->num_cloud_filters++;
8812 
8813 	return err;
8814 err:
8815 	kfree(filter);
8816 	return err;
8817 }
8818 
8819 /**
8820  * i40e_find_cloud_filter - Find the could filter in the list
8821  * @vsi: Pointer to VSI
8822  * @cookie: filter specific cookie
8823  *
8824  **/
8825 static struct i40e_cloud_filter *i40e_find_cloud_filter(struct i40e_vsi *vsi,
8826 							unsigned long *cookie)
8827 {
8828 	struct i40e_cloud_filter *filter = NULL;
8829 	struct hlist_node *node2;
8830 
8831 	hlist_for_each_entry_safe(filter, node2,
8832 				  &vsi->back->cloud_filter_list, cloud_node)
8833 		if (!memcmp(cookie, &filter->cookie, sizeof(filter->cookie)))
8834 			return filter;
8835 	return NULL;
8836 }
8837 
8838 /**
8839  * i40e_delete_clsflower - Remove tc flower filters
8840  * @vsi: Pointer to VSI
8841  * @cls_flower: Pointer to struct flow_cls_offload
8842  *
8843  **/
8844 static int i40e_delete_clsflower(struct i40e_vsi *vsi,
8845 				 struct flow_cls_offload *cls_flower)
8846 {
8847 	struct i40e_cloud_filter *filter = NULL;
8848 	struct i40e_pf *pf = vsi->back;
8849 	int err = 0;
8850 
8851 	filter = i40e_find_cloud_filter(vsi, &cls_flower->cookie);
8852 
8853 	if (!filter)
8854 		return -EINVAL;
8855 
8856 	hash_del(&filter->cloud_node);
8857 
8858 	if (filter->dst_port)
8859 		err = i40e_add_del_cloud_filter_big_buf(vsi, filter, false);
8860 	else
8861 		err = i40e_add_del_cloud_filter(vsi, filter, false);
8862 
8863 	kfree(filter);
8864 	if (err) {
8865 		dev_err(&pf->pdev->dev,
8866 			"Failed to delete cloud filter, err %s\n",
8867 			i40e_stat_str(&pf->hw, err));
8868 		return i40e_aq_rc_to_posix(err, pf->hw.aq.asq_last_status);
8869 	}
8870 
8871 	pf->num_cloud_filters--;
8872 	if (!pf->num_cloud_filters)
8873 		if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
8874 		    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
8875 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
8876 			pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
8877 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
8878 		}
8879 	return 0;
8880 }
8881 
8882 /**
8883  * i40e_setup_tc_cls_flower - flower classifier offloads
8884  * @np: net device to configure
8885  * @cls_flower: offload data
8886  **/
8887 static int i40e_setup_tc_cls_flower(struct i40e_netdev_priv *np,
8888 				    struct flow_cls_offload *cls_flower)
8889 {
8890 	struct i40e_vsi *vsi = np->vsi;
8891 
8892 	switch (cls_flower->command) {
8893 	case FLOW_CLS_REPLACE:
8894 		return i40e_configure_clsflower(vsi, cls_flower);
8895 	case FLOW_CLS_DESTROY:
8896 		return i40e_delete_clsflower(vsi, cls_flower);
8897 	case FLOW_CLS_STATS:
8898 		return -EOPNOTSUPP;
8899 	default:
8900 		return -EOPNOTSUPP;
8901 	}
8902 }
8903 
8904 static int i40e_setup_tc_block_cb(enum tc_setup_type type, void *type_data,
8905 				  void *cb_priv)
8906 {
8907 	struct i40e_netdev_priv *np = cb_priv;
8908 
8909 	if (!tc_cls_can_offload_and_chain0(np->vsi->netdev, type_data))
8910 		return -EOPNOTSUPP;
8911 
8912 	switch (type) {
8913 	case TC_SETUP_CLSFLOWER:
8914 		return i40e_setup_tc_cls_flower(np, type_data);
8915 
8916 	default:
8917 		return -EOPNOTSUPP;
8918 	}
8919 }
8920 
8921 static LIST_HEAD(i40e_block_cb_list);
8922 
8923 static int __i40e_setup_tc(struct net_device *netdev, enum tc_setup_type type,
8924 			   void *type_data)
8925 {
8926 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8927 
8928 	switch (type) {
8929 	case TC_SETUP_QDISC_MQPRIO:
8930 		return i40e_setup_tc(netdev, type_data);
8931 	case TC_SETUP_BLOCK:
8932 		return flow_block_cb_setup_simple(type_data,
8933 						  &i40e_block_cb_list,
8934 						  i40e_setup_tc_block_cb,
8935 						  np, np, true);
8936 	default:
8937 		return -EOPNOTSUPP;
8938 	}
8939 }
8940 
8941 /**
8942  * i40e_open - Called when a network interface is made active
8943  * @netdev: network interface device structure
8944  *
8945  * The open entry point is called when a network interface is made
8946  * active by the system (IFF_UP).  At this point all resources needed
8947  * for transmit and receive operations are allocated, the interrupt
8948  * handler is registered with the OS, the netdev watchdog subtask is
8949  * enabled, and the stack is notified that the interface is ready.
8950  *
8951  * Returns 0 on success, negative value on failure
8952  **/
8953 int i40e_open(struct net_device *netdev)
8954 {
8955 	struct i40e_netdev_priv *np = netdev_priv(netdev);
8956 	struct i40e_vsi *vsi = np->vsi;
8957 	struct i40e_pf *pf = vsi->back;
8958 	int err;
8959 
8960 	/* disallow open during test or if eeprom is broken */
8961 	if (test_bit(__I40E_TESTING, pf->state) ||
8962 	    test_bit(__I40E_BAD_EEPROM, pf->state))
8963 		return -EBUSY;
8964 
8965 	netif_carrier_off(netdev);
8966 
8967 	if (i40e_force_link_state(pf, true))
8968 		return -EAGAIN;
8969 
8970 	err = i40e_vsi_open(vsi);
8971 	if (err)
8972 		return err;
8973 
8974 	/* configure global TSO hardware offload settings */
8975 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_F, be32_to_cpu(TCP_FLAG_PSH |
8976 						       TCP_FLAG_FIN) >> 16);
8977 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_M, be32_to_cpu(TCP_FLAG_PSH |
8978 						       TCP_FLAG_FIN |
8979 						       TCP_FLAG_CWR) >> 16);
8980 	wr32(&pf->hw, I40E_GLLAN_TSOMSK_L, be32_to_cpu(TCP_FLAG_CWR) >> 16);
8981 	udp_tunnel_get_rx_info(netdev);
8982 
8983 	return 0;
8984 }
8985 
8986 /**
8987  * i40e_netif_set_realnum_tx_rx_queues - Update number of tx/rx queues
8988  * @vsi: vsi structure
8989  *
8990  * This updates netdev's number of tx/rx queues
8991  *
8992  * Returns status of setting tx/rx queues
8993  **/
8994 static int i40e_netif_set_realnum_tx_rx_queues(struct i40e_vsi *vsi)
8995 {
8996 	int ret;
8997 
8998 	ret = netif_set_real_num_rx_queues(vsi->netdev,
8999 					   vsi->num_queue_pairs);
9000 	if (ret)
9001 		return ret;
9002 
9003 	return netif_set_real_num_tx_queues(vsi->netdev,
9004 					    vsi->num_queue_pairs);
9005 }
9006 
9007 /**
9008  * i40e_vsi_open -
9009  * @vsi: the VSI to open
9010  *
9011  * Finish initialization of the VSI.
9012  *
9013  * Returns 0 on success, negative value on failure
9014  *
9015  * Note: expects to be called while under rtnl_lock()
9016  **/
9017 int i40e_vsi_open(struct i40e_vsi *vsi)
9018 {
9019 	struct i40e_pf *pf = vsi->back;
9020 	char int_name[I40E_INT_NAME_STR_LEN];
9021 	int err;
9022 
9023 	/* allocate descriptors */
9024 	err = i40e_vsi_setup_tx_resources(vsi);
9025 	if (err)
9026 		goto err_setup_tx;
9027 	err = i40e_vsi_setup_rx_resources(vsi);
9028 	if (err)
9029 		goto err_setup_rx;
9030 
9031 	err = i40e_vsi_configure(vsi);
9032 	if (err)
9033 		goto err_setup_rx;
9034 
9035 	if (vsi->netdev) {
9036 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s",
9037 			 dev_driver_string(&pf->pdev->dev), vsi->netdev->name);
9038 		err = i40e_vsi_request_irq(vsi, int_name);
9039 		if (err)
9040 			goto err_setup_rx;
9041 
9042 		/* Notify the stack of the actual queue counts. */
9043 		err = i40e_netif_set_realnum_tx_rx_queues(vsi);
9044 		if (err)
9045 			goto err_set_queues;
9046 
9047 	} else if (vsi->type == I40E_VSI_FDIR) {
9048 		snprintf(int_name, sizeof(int_name) - 1, "%s-%s:fdir",
9049 			 dev_driver_string(&pf->pdev->dev),
9050 			 dev_name(&pf->pdev->dev));
9051 		err = i40e_vsi_request_irq(vsi, int_name);
9052 		if (err)
9053 			goto err_setup_rx;
9054 
9055 	} else {
9056 		err = -EINVAL;
9057 		goto err_setup_rx;
9058 	}
9059 
9060 	err = i40e_up_complete(vsi);
9061 	if (err)
9062 		goto err_up_complete;
9063 
9064 	return 0;
9065 
9066 err_up_complete:
9067 	i40e_down(vsi);
9068 err_set_queues:
9069 	i40e_vsi_free_irq(vsi);
9070 err_setup_rx:
9071 	i40e_vsi_free_rx_resources(vsi);
9072 err_setup_tx:
9073 	i40e_vsi_free_tx_resources(vsi);
9074 	if (vsi == pf->vsi[pf->lan_vsi])
9075 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
9076 
9077 	return err;
9078 }
9079 
9080 /**
9081  * i40e_fdir_filter_exit - Cleans up the Flow Director accounting
9082  * @pf: Pointer to PF
9083  *
9084  * This function destroys the hlist where all the Flow Director
9085  * filters were saved.
9086  **/
9087 static void i40e_fdir_filter_exit(struct i40e_pf *pf)
9088 {
9089 	struct i40e_fdir_filter *filter;
9090 	struct i40e_flex_pit *pit_entry, *tmp;
9091 	struct hlist_node *node2;
9092 
9093 	hlist_for_each_entry_safe(filter, node2,
9094 				  &pf->fdir_filter_list, fdir_node) {
9095 		hlist_del(&filter->fdir_node);
9096 		kfree(filter);
9097 	}
9098 
9099 	list_for_each_entry_safe(pit_entry, tmp, &pf->l3_flex_pit_list, list) {
9100 		list_del(&pit_entry->list);
9101 		kfree(pit_entry);
9102 	}
9103 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
9104 
9105 	list_for_each_entry_safe(pit_entry, tmp, &pf->l4_flex_pit_list, list) {
9106 		list_del(&pit_entry->list);
9107 		kfree(pit_entry);
9108 	}
9109 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
9110 
9111 	pf->fdir_pf_active_filters = 0;
9112 	i40e_reset_fdir_filter_cnt(pf);
9113 
9114 	/* Reprogram the default input set for TCP/IPv4 */
9115 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9116 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9117 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9118 
9119 	/* Reprogram the default input set for TCP/IPv6 */
9120 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_TCP,
9121 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9122 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9123 
9124 	/* Reprogram the default input set for UDP/IPv4 */
9125 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_UDP,
9126 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9127 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9128 
9129 	/* Reprogram the default input set for UDP/IPv6 */
9130 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_UDP,
9131 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9132 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9133 
9134 	/* Reprogram the default input set for SCTP/IPv4 */
9135 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_SCTP,
9136 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9137 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9138 
9139 	/* Reprogram the default input set for SCTP/IPv6 */
9140 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_SCTP,
9141 				I40E_L3_V6_SRC_MASK | I40E_L3_V6_DST_MASK |
9142 				I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9143 
9144 	/* Reprogram the default input set for Other/IPv4 */
9145 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_OTHER,
9146 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9147 
9148 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV4,
9149 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9150 
9151 	/* Reprogram the default input set for Other/IPv6 */
9152 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV6_OTHER,
9153 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9154 
9155 	i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_FRAG_IPV6,
9156 				I40E_L3_SRC_MASK | I40E_L3_DST_MASK);
9157 }
9158 
9159 /**
9160  * i40e_cloud_filter_exit - Cleans up the cloud filters
9161  * @pf: Pointer to PF
9162  *
9163  * This function destroys the hlist where all the cloud filters
9164  * were saved.
9165  **/
9166 static void i40e_cloud_filter_exit(struct i40e_pf *pf)
9167 {
9168 	struct i40e_cloud_filter *cfilter;
9169 	struct hlist_node *node;
9170 
9171 	hlist_for_each_entry_safe(cfilter, node,
9172 				  &pf->cloud_filter_list, cloud_node) {
9173 		hlist_del(&cfilter->cloud_node);
9174 		kfree(cfilter);
9175 	}
9176 	pf->num_cloud_filters = 0;
9177 
9178 	if ((pf->flags & I40E_FLAG_FD_SB_TO_CLOUD_FILTER) &&
9179 	    !(pf->flags & I40E_FLAG_FD_SB_INACTIVE)) {
9180 		pf->flags |= I40E_FLAG_FD_SB_ENABLED;
9181 		pf->flags &= ~I40E_FLAG_FD_SB_TO_CLOUD_FILTER;
9182 		pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
9183 	}
9184 }
9185 
9186 /**
9187  * i40e_close - Disables a network interface
9188  * @netdev: network interface device structure
9189  *
9190  * The close entry point is called when an interface is de-activated
9191  * by the OS.  The hardware is still under the driver's control, but
9192  * this netdev interface is disabled.
9193  *
9194  * Returns 0, this is not allowed to fail
9195  **/
9196 int i40e_close(struct net_device *netdev)
9197 {
9198 	struct i40e_netdev_priv *np = netdev_priv(netdev);
9199 	struct i40e_vsi *vsi = np->vsi;
9200 
9201 	i40e_vsi_close(vsi);
9202 
9203 	return 0;
9204 }
9205 
9206 /**
9207  * i40e_do_reset - Start a PF or Core Reset sequence
9208  * @pf: board private structure
9209  * @reset_flags: which reset is requested
9210  * @lock_acquired: indicates whether or not the lock has been acquired
9211  * before this function was called.
9212  *
9213  * The essential difference in resets is that the PF Reset
9214  * doesn't clear the packet buffers, doesn't reset the PE
9215  * firmware, and doesn't bother the other PFs on the chip.
9216  **/
9217 void i40e_do_reset(struct i40e_pf *pf, u32 reset_flags, bool lock_acquired)
9218 {
9219 	u32 val;
9220 
9221 	/* do the biggest reset indicated */
9222 	if (reset_flags & BIT_ULL(__I40E_GLOBAL_RESET_REQUESTED)) {
9223 
9224 		/* Request a Global Reset
9225 		 *
9226 		 * This will start the chip's countdown to the actual full
9227 		 * chip reset event, and a warning interrupt to be sent
9228 		 * to all PFs, including the requestor.  Our handler
9229 		 * for the warning interrupt will deal with the shutdown
9230 		 * and recovery of the switch setup.
9231 		 */
9232 		dev_dbg(&pf->pdev->dev, "GlobalR requested\n");
9233 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9234 		val |= I40E_GLGEN_RTRIG_GLOBR_MASK;
9235 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9236 
9237 	} else if (reset_flags & BIT_ULL(__I40E_CORE_RESET_REQUESTED)) {
9238 
9239 		/* Request a Core Reset
9240 		 *
9241 		 * Same as Global Reset, except does *not* include the MAC/PHY
9242 		 */
9243 		dev_dbg(&pf->pdev->dev, "CoreR requested\n");
9244 		val = rd32(&pf->hw, I40E_GLGEN_RTRIG);
9245 		val |= I40E_GLGEN_RTRIG_CORER_MASK;
9246 		wr32(&pf->hw, I40E_GLGEN_RTRIG, val);
9247 		i40e_flush(&pf->hw);
9248 
9249 	} else if (reset_flags & I40E_PF_RESET_FLAG) {
9250 
9251 		/* Request a PF Reset
9252 		 *
9253 		 * Resets only the PF-specific registers
9254 		 *
9255 		 * This goes directly to the tear-down and rebuild of
9256 		 * the switch, since we need to do all the recovery as
9257 		 * for the Core Reset.
9258 		 */
9259 		dev_dbg(&pf->pdev->dev, "PFR requested\n");
9260 		i40e_handle_reset_warning(pf, lock_acquired);
9261 
9262 	} else if (reset_flags & I40E_PF_RESET_AND_REBUILD_FLAG) {
9263 		/* Request a PF Reset
9264 		 *
9265 		 * Resets PF and reinitializes PFs VSI.
9266 		 */
9267 		i40e_prep_for_reset(pf);
9268 		i40e_reset_and_rebuild(pf, true, lock_acquired);
9269 		dev_info(&pf->pdev->dev,
9270 			 pf->flags & I40E_FLAG_DISABLE_FW_LLDP ?
9271 			 "FW LLDP is disabled\n" :
9272 			 "FW LLDP is enabled\n");
9273 
9274 	} else if (reset_flags & BIT_ULL(__I40E_REINIT_REQUESTED)) {
9275 		int v;
9276 
9277 		/* Find the VSI(s) that requested a re-init */
9278 		dev_info(&pf->pdev->dev,
9279 			 "VSI reinit requested\n");
9280 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9281 			struct i40e_vsi *vsi = pf->vsi[v];
9282 
9283 			if (vsi != NULL &&
9284 			    test_and_clear_bit(__I40E_VSI_REINIT_REQUESTED,
9285 					       vsi->state))
9286 				i40e_vsi_reinit_locked(pf->vsi[v]);
9287 		}
9288 	} else if (reset_flags & BIT_ULL(__I40E_DOWN_REQUESTED)) {
9289 		int v;
9290 
9291 		/* Find the VSI(s) that needs to be brought down */
9292 		dev_info(&pf->pdev->dev, "VSI down requested\n");
9293 		for (v = 0; v < pf->num_alloc_vsi; v++) {
9294 			struct i40e_vsi *vsi = pf->vsi[v];
9295 
9296 			if (vsi != NULL &&
9297 			    test_and_clear_bit(__I40E_VSI_DOWN_REQUESTED,
9298 					       vsi->state)) {
9299 				set_bit(__I40E_VSI_DOWN, vsi->state);
9300 				i40e_down(vsi);
9301 			}
9302 		}
9303 	} else {
9304 		dev_info(&pf->pdev->dev,
9305 			 "bad reset request 0x%08x\n", reset_flags);
9306 	}
9307 }
9308 
9309 #ifdef CONFIG_I40E_DCB
9310 /**
9311  * i40e_dcb_need_reconfig - Check if DCB needs reconfig
9312  * @pf: board private structure
9313  * @old_cfg: current DCB config
9314  * @new_cfg: new DCB config
9315  **/
9316 bool i40e_dcb_need_reconfig(struct i40e_pf *pf,
9317 			    struct i40e_dcbx_config *old_cfg,
9318 			    struct i40e_dcbx_config *new_cfg)
9319 {
9320 	bool need_reconfig = false;
9321 
9322 	/* Check if ETS configuration has changed */
9323 	if (memcmp(&new_cfg->etscfg,
9324 		   &old_cfg->etscfg,
9325 		   sizeof(new_cfg->etscfg))) {
9326 		/* If Priority Table has changed reconfig is needed */
9327 		if (memcmp(&new_cfg->etscfg.prioritytable,
9328 			   &old_cfg->etscfg.prioritytable,
9329 			   sizeof(new_cfg->etscfg.prioritytable))) {
9330 			need_reconfig = true;
9331 			dev_dbg(&pf->pdev->dev, "ETS UP2TC changed.\n");
9332 		}
9333 
9334 		if (memcmp(&new_cfg->etscfg.tcbwtable,
9335 			   &old_cfg->etscfg.tcbwtable,
9336 			   sizeof(new_cfg->etscfg.tcbwtable)))
9337 			dev_dbg(&pf->pdev->dev, "ETS TC BW Table changed.\n");
9338 
9339 		if (memcmp(&new_cfg->etscfg.tsatable,
9340 			   &old_cfg->etscfg.tsatable,
9341 			   sizeof(new_cfg->etscfg.tsatable)))
9342 			dev_dbg(&pf->pdev->dev, "ETS TSA Table changed.\n");
9343 	}
9344 
9345 	/* Check if PFC configuration has changed */
9346 	if (memcmp(&new_cfg->pfc,
9347 		   &old_cfg->pfc,
9348 		   sizeof(new_cfg->pfc))) {
9349 		need_reconfig = true;
9350 		dev_dbg(&pf->pdev->dev, "PFC config change detected.\n");
9351 	}
9352 
9353 	/* Check if APP Table has changed */
9354 	if (memcmp(&new_cfg->app,
9355 		   &old_cfg->app,
9356 		   sizeof(new_cfg->app))) {
9357 		need_reconfig = true;
9358 		dev_dbg(&pf->pdev->dev, "APP Table change detected.\n");
9359 	}
9360 
9361 	dev_dbg(&pf->pdev->dev, "dcb need_reconfig=%d\n", need_reconfig);
9362 	return need_reconfig;
9363 }
9364 
9365 /**
9366  * i40e_handle_lldp_event - Handle LLDP Change MIB event
9367  * @pf: board private structure
9368  * @e: event info posted on ARQ
9369  **/
9370 static int i40e_handle_lldp_event(struct i40e_pf *pf,
9371 				  struct i40e_arq_event_info *e)
9372 {
9373 	struct i40e_aqc_lldp_get_mib *mib =
9374 		(struct i40e_aqc_lldp_get_mib *)&e->desc.params.raw;
9375 	struct i40e_hw *hw = &pf->hw;
9376 	struct i40e_dcbx_config tmp_dcbx_cfg;
9377 	bool need_reconfig = false;
9378 	int ret = 0;
9379 	u8 type;
9380 
9381 	/* X710-T*L 2.5G and 5G speeds don't support DCB */
9382 	if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9383 	    (hw->phy.link_info.link_speed &
9384 	     ~(I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB)) &&
9385 	     !(pf->flags & I40E_FLAG_DCB_CAPABLE))
9386 		/* let firmware decide if the DCB should be disabled */
9387 		pf->flags |= I40E_FLAG_DCB_CAPABLE;
9388 
9389 	/* Not DCB capable or capability disabled */
9390 	if (!(pf->flags & I40E_FLAG_DCB_CAPABLE))
9391 		return ret;
9392 
9393 	/* Ignore if event is not for Nearest Bridge */
9394 	type = ((mib->type >> I40E_AQ_LLDP_BRIDGE_TYPE_SHIFT)
9395 		& I40E_AQ_LLDP_BRIDGE_TYPE_MASK);
9396 	dev_dbg(&pf->pdev->dev, "LLDP event mib bridge type 0x%x\n", type);
9397 	if (type != I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE)
9398 		return ret;
9399 
9400 	/* Check MIB Type and return if event for Remote MIB update */
9401 	type = mib->type & I40E_AQ_LLDP_MIB_TYPE_MASK;
9402 	dev_dbg(&pf->pdev->dev,
9403 		"LLDP event mib type %s\n", type ? "remote" : "local");
9404 	if (type == I40E_AQ_LLDP_MIB_REMOTE) {
9405 		/* Update the remote cached instance and return */
9406 		ret = i40e_aq_get_dcb_config(hw, I40E_AQ_LLDP_MIB_REMOTE,
9407 				I40E_AQ_LLDP_BRIDGE_TYPE_NEAREST_BRIDGE,
9408 				&hw->remote_dcbx_config);
9409 		goto exit;
9410 	}
9411 
9412 	/* Store the old configuration */
9413 	tmp_dcbx_cfg = hw->local_dcbx_config;
9414 
9415 	/* Reset the old DCBx configuration data */
9416 	memset(&hw->local_dcbx_config, 0, sizeof(hw->local_dcbx_config));
9417 	/* Get updated DCBX data from firmware */
9418 	ret = i40e_get_dcb_config(&pf->hw);
9419 	if (ret) {
9420 		/* X710-T*L 2.5G and 5G speeds don't support DCB */
9421 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
9422 		    (hw->phy.link_info.link_speed &
9423 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
9424 			dev_warn(&pf->pdev->dev,
9425 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
9426 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
9427 		} else {
9428 			dev_info(&pf->pdev->dev,
9429 				 "Failed querying DCB configuration data from firmware, err %s aq_err %s\n",
9430 				 i40e_stat_str(&pf->hw, ret),
9431 				 i40e_aq_str(&pf->hw,
9432 					     pf->hw.aq.asq_last_status));
9433 		}
9434 		goto exit;
9435 	}
9436 
9437 	/* No change detected in DCBX configs */
9438 	if (!memcmp(&tmp_dcbx_cfg, &hw->local_dcbx_config,
9439 		    sizeof(tmp_dcbx_cfg))) {
9440 		dev_dbg(&pf->pdev->dev, "No change detected in DCBX configuration.\n");
9441 		goto exit;
9442 	}
9443 
9444 	need_reconfig = i40e_dcb_need_reconfig(pf, &tmp_dcbx_cfg,
9445 					       &hw->local_dcbx_config);
9446 
9447 	i40e_dcbnl_flush_apps(pf, &tmp_dcbx_cfg, &hw->local_dcbx_config);
9448 
9449 	if (!need_reconfig)
9450 		goto exit;
9451 
9452 	/* Enable DCB tagging only when more than one TC */
9453 	if (i40e_dcb_get_num_tc(&hw->local_dcbx_config) > 1)
9454 		pf->flags |= I40E_FLAG_DCB_ENABLED;
9455 	else
9456 		pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9457 
9458 	set_bit(__I40E_PORT_SUSPENDED, pf->state);
9459 	/* Reconfiguration needed quiesce all VSIs */
9460 	i40e_pf_quiesce_all_vsi(pf);
9461 
9462 	/* Changes in configuration update VEB/VSI */
9463 	i40e_dcb_reconfigure(pf);
9464 
9465 	ret = i40e_resume_port_tx(pf);
9466 
9467 	clear_bit(__I40E_PORT_SUSPENDED, pf->state);
9468 	/* In case of error no point in resuming VSIs */
9469 	if (ret)
9470 		goto exit;
9471 
9472 	/* Wait for the PF's queues to be disabled */
9473 	ret = i40e_pf_wait_queues_disabled(pf);
9474 	if (ret) {
9475 		/* Schedule PF reset to recover */
9476 		set_bit(__I40E_PF_RESET_REQUESTED, pf->state);
9477 		i40e_service_event_schedule(pf);
9478 	} else {
9479 		i40e_pf_unquiesce_all_vsi(pf);
9480 		set_bit(__I40E_CLIENT_SERVICE_REQUESTED, pf->state);
9481 		set_bit(__I40E_CLIENT_L2_CHANGE, pf->state);
9482 	}
9483 
9484 exit:
9485 	return ret;
9486 }
9487 #endif /* CONFIG_I40E_DCB */
9488 
9489 /**
9490  * i40e_do_reset_safe - Protected reset path for userland calls.
9491  * @pf: board private structure
9492  * @reset_flags: which reset is requested
9493  *
9494  **/
9495 void i40e_do_reset_safe(struct i40e_pf *pf, u32 reset_flags)
9496 {
9497 	rtnl_lock();
9498 	i40e_do_reset(pf, reset_flags, true);
9499 	rtnl_unlock();
9500 }
9501 
9502 /**
9503  * i40e_handle_lan_overflow_event - Handler for LAN queue overflow event
9504  * @pf: board private structure
9505  * @e: event info posted on ARQ
9506  *
9507  * Handler for LAN Queue Overflow Event generated by the firmware for PF
9508  * and VF queues
9509  **/
9510 static void i40e_handle_lan_overflow_event(struct i40e_pf *pf,
9511 					   struct i40e_arq_event_info *e)
9512 {
9513 	struct i40e_aqc_lan_overflow *data =
9514 		(struct i40e_aqc_lan_overflow *)&e->desc.params.raw;
9515 	u32 queue = le32_to_cpu(data->prtdcb_rupto);
9516 	u32 qtx_ctl = le32_to_cpu(data->otx_ctl);
9517 	struct i40e_hw *hw = &pf->hw;
9518 	struct i40e_vf *vf;
9519 	u16 vf_id;
9520 
9521 	dev_dbg(&pf->pdev->dev, "overflow Rx Queue Number = %d QTX_CTL=0x%08x\n",
9522 		queue, qtx_ctl);
9523 
9524 	/* Queue belongs to VF, find the VF and issue VF reset */
9525 	if (((qtx_ctl & I40E_QTX_CTL_PFVF_Q_MASK)
9526 	    >> I40E_QTX_CTL_PFVF_Q_SHIFT) == I40E_QTX_CTL_VF_QUEUE) {
9527 		vf_id = (u16)((qtx_ctl & I40E_QTX_CTL_VFVM_INDX_MASK)
9528 			 >> I40E_QTX_CTL_VFVM_INDX_SHIFT);
9529 		vf_id -= hw->func_caps.vf_base_id;
9530 		vf = &pf->vf[vf_id];
9531 		i40e_vc_notify_vf_reset(vf);
9532 		/* Allow VF to process pending reset notification */
9533 		msleep(20);
9534 		i40e_reset_vf(vf, false);
9535 	}
9536 }
9537 
9538 /**
9539  * i40e_get_cur_guaranteed_fd_count - Get the consumed guaranteed FD filters
9540  * @pf: board private structure
9541  **/
9542 u32 i40e_get_cur_guaranteed_fd_count(struct i40e_pf *pf)
9543 {
9544 	u32 val, fcnt_prog;
9545 
9546 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9547 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK);
9548 	return fcnt_prog;
9549 }
9550 
9551 /**
9552  * i40e_get_current_fd_count - Get total FD filters programmed for this PF
9553  * @pf: board private structure
9554  **/
9555 u32 i40e_get_current_fd_count(struct i40e_pf *pf)
9556 {
9557 	u32 val, fcnt_prog;
9558 
9559 	val = rd32(&pf->hw, I40E_PFQF_FDSTAT);
9560 	fcnt_prog = (val & I40E_PFQF_FDSTAT_GUARANT_CNT_MASK) +
9561 		    ((val & I40E_PFQF_FDSTAT_BEST_CNT_MASK) >>
9562 		      I40E_PFQF_FDSTAT_BEST_CNT_SHIFT);
9563 	return fcnt_prog;
9564 }
9565 
9566 /**
9567  * i40e_get_global_fd_count - Get total FD filters programmed on device
9568  * @pf: board private structure
9569  **/
9570 u32 i40e_get_global_fd_count(struct i40e_pf *pf)
9571 {
9572 	u32 val, fcnt_prog;
9573 
9574 	val = rd32(&pf->hw, I40E_GLQF_FDCNT_0);
9575 	fcnt_prog = (val & I40E_GLQF_FDCNT_0_GUARANT_CNT_MASK) +
9576 		    ((val & I40E_GLQF_FDCNT_0_BESTCNT_MASK) >>
9577 		     I40E_GLQF_FDCNT_0_BESTCNT_SHIFT);
9578 	return fcnt_prog;
9579 }
9580 
9581 /**
9582  * i40e_reenable_fdir_sb - Restore FDir SB capability
9583  * @pf: board private structure
9584  **/
9585 static void i40e_reenable_fdir_sb(struct i40e_pf *pf)
9586 {
9587 	if (test_and_clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state))
9588 		if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
9589 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9590 			dev_info(&pf->pdev->dev, "FD Sideband/ntuple is being enabled since we have space in the table now\n");
9591 }
9592 
9593 /**
9594  * i40e_reenable_fdir_atr - Restore FDir ATR capability
9595  * @pf: board private structure
9596  **/
9597 static void i40e_reenable_fdir_atr(struct i40e_pf *pf)
9598 {
9599 	if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state)) {
9600 		/* ATR uses the same filtering logic as SB rules. It only
9601 		 * functions properly if the input set mask is at the default
9602 		 * settings. It is safe to restore the default input set
9603 		 * because there are no active TCPv4 filter rules.
9604 		 */
9605 		i40e_write_fd_input_set(pf, I40E_FILTER_PCTYPE_NONF_IPV4_TCP,
9606 					I40E_L3_SRC_MASK | I40E_L3_DST_MASK |
9607 					I40E_L4_SRC_MASK | I40E_L4_DST_MASK);
9608 
9609 		if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
9610 		    (I40E_DEBUG_FD & pf->hw.debug_mask))
9611 			dev_info(&pf->pdev->dev, "ATR is being enabled since we have space in the table and there are no conflicting ntuple rules\n");
9612 	}
9613 }
9614 
9615 /**
9616  * i40e_delete_invalid_filter - Delete an invalid FDIR filter
9617  * @pf: board private structure
9618  * @filter: FDir filter to remove
9619  */
9620 static void i40e_delete_invalid_filter(struct i40e_pf *pf,
9621 				       struct i40e_fdir_filter *filter)
9622 {
9623 	/* Update counters */
9624 	pf->fdir_pf_active_filters--;
9625 	pf->fd_inv = 0;
9626 
9627 	switch (filter->flow_type) {
9628 	case TCP_V4_FLOW:
9629 		pf->fd_tcp4_filter_cnt--;
9630 		break;
9631 	case UDP_V4_FLOW:
9632 		pf->fd_udp4_filter_cnt--;
9633 		break;
9634 	case SCTP_V4_FLOW:
9635 		pf->fd_sctp4_filter_cnt--;
9636 		break;
9637 	case TCP_V6_FLOW:
9638 		pf->fd_tcp6_filter_cnt--;
9639 		break;
9640 	case UDP_V6_FLOW:
9641 		pf->fd_udp6_filter_cnt--;
9642 		break;
9643 	case SCTP_V6_FLOW:
9644 		pf->fd_udp6_filter_cnt--;
9645 		break;
9646 	case IP_USER_FLOW:
9647 		switch (filter->ipl4_proto) {
9648 		case IPPROTO_TCP:
9649 			pf->fd_tcp4_filter_cnt--;
9650 			break;
9651 		case IPPROTO_UDP:
9652 			pf->fd_udp4_filter_cnt--;
9653 			break;
9654 		case IPPROTO_SCTP:
9655 			pf->fd_sctp4_filter_cnt--;
9656 			break;
9657 		case IPPROTO_IP:
9658 			pf->fd_ip4_filter_cnt--;
9659 			break;
9660 		}
9661 		break;
9662 	case IPV6_USER_FLOW:
9663 		switch (filter->ipl4_proto) {
9664 		case IPPROTO_TCP:
9665 			pf->fd_tcp6_filter_cnt--;
9666 			break;
9667 		case IPPROTO_UDP:
9668 			pf->fd_udp6_filter_cnt--;
9669 			break;
9670 		case IPPROTO_SCTP:
9671 			pf->fd_sctp6_filter_cnt--;
9672 			break;
9673 		case IPPROTO_IP:
9674 			pf->fd_ip6_filter_cnt--;
9675 			break;
9676 		}
9677 		break;
9678 	}
9679 
9680 	/* Remove the filter from the list and free memory */
9681 	hlist_del(&filter->fdir_node);
9682 	kfree(filter);
9683 }
9684 
9685 /**
9686  * i40e_fdir_check_and_reenable - Function to reenabe FD ATR or SB if disabled
9687  * @pf: board private structure
9688  **/
9689 void i40e_fdir_check_and_reenable(struct i40e_pf *pf)
9690 {
9691 	struct i40e_fdir_filter *filter;
9692 	u32 fcnt_prog, fcnt_avail;
9693 	struct hlist_node *node;
9694 
9695 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9696 		return;
9697 
9698 	/* Check if we have enough room to re-enable FDir SB capability. */
9699 	fcnt_prog = i40e_get_global_fd_count(pf);
9700 	fcnt_avail = pf->fdir_pf_filter_count;
9701 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM)) ||
9702 	    (pf->fd_add_err == 0) ||
9703 	    (i40e_get_current_atr_cnt(pf) < pf->fd_atr_cnt))
9704 		i40e_reenable_fdir_sb(pf);
9705 
9706 	/* We should wait for even more space before re-enabling ATR.
9707 	 * Additionally, we cannot enable ATR as long as we still have TCP SB
9708 	 * rules active.
9709 	 */
9710 	if ((fcnt_prog < (fcnt_avail - I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) &&
9711 	    pf->fd_tcp4_filter_cnt == 0 && pf->fd_tcp6_filter_cnt == 0)
9712 		i40e_reenable_fdir_atr(pf);
9713 
9714 	/* if hw had a problem adding a filter, delete it */
9715 	if (pf->fd_inv > 0) {
9716 		hlist_for_each_entry_safe(filter, node,
9717 					  &pf->fdir_filter_list, fdir_node)
9718 			if (filter->fd_id == pf->fd_inv)
9719 				i40e_delete_invalid_filter(pf, filter);
9720 	}
9721 }
9722 
9723 #define I40E_MIN_FD_FLUSH_INTERVAL 10
9724 #define I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE 30
9725 /**
9726  * i40e_fdir_flush_and_replay - Function to flush all FD filters and replay SB
9727  * @pf: board private structure
9728  **/
9729 static void i40e_fdir_flush_and_replay(struct i40e_pf *pf)
9730 {
9731 	unsigned long min_flush_time;
9732 	int flush_wait_retry = 50;
9733 	bool disable_atr = false;
9734 	int fd_room;
9735 	int reg;
9736 
9737 	if (!time_after(jiffies, pf->fd_flush_timestamp +
9738 				 (I40E_MIN_FD_FLUSH_INTERVAL * HZ)))
9739 		return;
9740 
9741 	/* If the flush is happening too quick and we have mostly SB rules we
9742 	 * should not re-enable ATR for some time.
9743 	 */
9744 	min_flush_time = pf->fd_flush_timestamp +
9745 			 (I40E_MIN_FD_FLUSH_SB_ATR_UNSTABLE * HZ);
9746 	fd_room = pf->fdir_pf_filter_count - pf->fdir_pf_active_filters;
9747 
9748 	if (!(time_after(jiffies, min_flush_time)) &&
9749 	    (fd_room < I40E_FDIR_BUFFER_HEAD_ROOM_FOR_ATR)) {
9750 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9751 			dev_info(&pf->pdev->dev, "ATR disabled, not enough FD filter space.\n");
9752 		disable_atr = true;
9753 	}
9754 
9755 	pf->fd_flush_timestamp = jiffies;
9756 	set_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9757 	/* flush all filters */
9758 	wr32(&pf->hw, I40E_PFQF_CTL_1,
9759 	     I40E_PFQF_CTL_1_CLEARFDTABLE_MASK);
9760 	i40e_flush(&pf->hw);
9761 	pf->fd_flush_cnt++;
9762 	pf->fd_add_err = 0;
9763 	do {
9764 		/* Check FD flush status every 5-6msec */
9765 		usleep_range(5000, 6000);
9766 		reg = rd32(&pf->hw, I40E_PFQF_CTL_1);
9767 		if (!(reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK))
9768 			break;
9769 	} while (flush_wait_retry--);
9770 	if (reg & I40E_PFQF_CTL_1_CLEARFDTABLE_MASK) {
9771 		dev_warn(&pf->pdev->dev, "FD table did not flush, needs more time\n");
9772 	} else {
9773 		/* replay sideband filters */
9774 		i40e_fdir_filter_restore(pf->vsi[pf->lan_vsi]);
9775 		if (!disable_atr && !pf->fd_tcp4_filter_cnt)
9776 			clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state);
9777 		clear_bit(__I40E_FD_FLUSH_REQUESTED, pf->state);
9778 		if (I40E_DEBUG_FD & pf->hw.debug_mask)
9779 			dev_info(&pf->pdev->dev, "FD Filter table flushed and FD-SB replayed.\n");
9780 	}
9781 }
9782 
9783 /**
9784  * i40e_get_current_atr_cnt - Get the count of total FD ATR filters programmed
9785  * @pf: board private structure
9786  **/
9787 u32 i40e_get_current_atr_cnt(struct i40e_pf *pf)
9788 {
9789 	return i40e_get_current_fd_count(pf) - pf->fdir_pf_active_filters;
9790 }
9791 
9792 /**
9793  * i40e_fdir_reinit_subtask - Worker thread to reinit FDIR filter table
9794  * @pf: board private structure
9795  **/
9796 static void i40e_fdir_reinit_subtask(struct i40e_pf *pf)
9797 {
9798 
9799 	/* if interface is down do nothing */
9800 	if (test_bit(__I40E_DOWN, pf->state))
9801 		return;
9802 
9803 	if (test_bit(__I40E_FD_FLUSH_REQUESTED, pf->state))
9804 		i40e_fdir_flush_and_replay(pf);
9805 
9806 	i40e_fdir_check_and_reenable(pf);
9807 
9808 }
9809 
9810 /**
9811  * i40e_vsi_link_event - notify VSI of a link event
9812  * @vsi: vsi to be notified
9813  * @link_up: link up or down
9814  **/
9815 static void i40e_vsi_link_event(struct i40e_vsi *vsi, bool link_up)
9816 {
9817 	if (!vsi || test_bit(__I40E_VSI_DOWN, vsi->state))
9818 		return;
9819 
9820 	switch (vsi->type) {
9821 	case I40E_VSI_MAIN:
9822 		if (!vsi->netdev || !vsi->netdev_registered)
9823 			break;
9824 
9825 		if (link_up) {
9826 			netif_carrier_on(vsi->netdev);
9827 			netif_tx_wake_all_queues(vsi->netdev);
9828 		} else {
9829 			netif_carrier_off(vsi->netdev);
9830 			netif_tx_stop_all_queues(vsi->netdev);
9831 		}
9832 		break;
9833 
9834 	case I40E_VSI_SRIOV:
9835 	case I40E_VSI_VMDQ2:
9836 	case I40E_VSI_CTRL:
9837 	case I40E_VSI_IWARP:
9838 	case I40E_VSI_MIRROR:
9839 	default:
9840 		/* there is no notification for other VSIs */
9841 		break;
9842 	}
9843 }
9844 
9845 /**
9846  * i40e_veb_link_event - notify elements on the veb of a link event
9847  * @veb: veb to be notified
9848  * @link_up: link up or down
9849  **/
9850 static void i40e_veb_link_event(struct i40e_veb *veb, bool link_up)
9851 {
9852 	struct i40e_pf *pf;
9853 	int i;
9854 
9855 	if (!veb || !veb->pf)
9856 		return;
9857 	pf = veb->pf;
9858 
9859 	/* depth first... */
9860 	for (i = 0; i < I40E_MAX_VEB; i++)
9861 		if (pf->veb[i] && (pf->veb[i]->uplink_seid == veb->seid))
9862 			i40e_veb_link_event(pf->veb[i], link_up);
9863 
9864 	/* ... now the local VSIs */
9865 	for (i = 0; i < pf->num_alloc_vsi; i++)
9866 		if (pf->vsi[i] && (pf->vsi[i]->uplink_seid == veb->seid))
9867 			i40e_vsi_link_event(pf->vsi[i], link_up);
9868 }
9869 
9870 /**
9871  * i40e_link_event - Update netif_carrier status
9872  * @pf: board private structure
9873  **/
9874 static void i40e_link_event(struct i40e_pf *pf)
9875 {
9876 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
9877 	u8 new_link_speed, old_link_speed;
9878 	i40e_status status;
9879 	bool new_link, old_link;
9880 #ifdef CONFIG_I40E_DCB
9881 	int err;
9882 #endif /* CONFIG_I40E_DCB */
9883 
9884 	/* set this to force the get_link_status call to refresh state */
9885 	pf->hw.phy.get_link_info = true;
9886 	old_link = (pf->hw.phy.link_info_old.link_info & I40E_AQ_LINK_UP);
9887 	status = i40e_get_link_status(&pf->hw, &new_link);
9888 
9889 	/* On success, disable temp link polling */
9890 	if (status == I40E_SUCCESS) {
9891 		clear_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9892 	} else {
9893 		/* Enable link polling temporarily until i40e_get_link_status
9894 		 * returns I40E_SUCCESS
9895 		 */
9896 		set_bit(__I40E_TEMP_LINK_POLLING, pf->state);
9897 		dev_dbg(&pf->pdev->dev, "couldn't get link state, status: %d\n",
9898 			status);
9899 		return;
9900 	}
9901 
9902 	old_link_speed = pf->hw.phy.link_info_old.link_speed;
9903 	new_link_speed = pf->hw.phy.link_info.link_speed;
9904 
9905 	if (new_link == old_link &&
9906 	    new_link_speed == old_link_speed &&
9907 	    (test_bit(__I40E_VSI_DOWN, vsi->state) ||
9908 	     new_link == netif_carrier_ok(vsi->netdev)))
9909 		return;
9910 
9911 	i40e_print_link_message(vsi, new_link);
9912 
9913 	/* Notify the base of the switch tree connected to
9914 	 * the link.  Floating VEBs are not notified.
9915 	 */
9916 	if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
9917 		i40e_veb_link_event(pf->veb[pf->lan_veb], new_link);
9918 	else
9919 		i40e_vsi_link_event(vsi, new_link);
9920 
9921 	if (pf->vf)
9922 		i40e_vc_notify_link_state(pf);
9923 
9924 	if (pf->flags & I40E_FLAG_PTP)
9925 		i40e_ptp_set_increment(pf);
9926 #ifdef CONFIG_I40E_DCB
9927 	if (new_link == old_link)
9928 		return;
9929 	/* Not SW DCB so firmware will take care of default settings */
9930 	if (pf->dcbx_cap & DCB_CAP_DCBX_LLD_MANAGED)
9931 		return;
9932 
9933 	/* We cover here only link down, as after link up in case of SW DCB
9934 	 * SW LLDP agent will take care of setting it up
9935 	 */
9936 	if (!new_link) {
9937 		dev_dbg(&pf->pdev->dev, "Reconfig DCB to single TC as result of Link Down\n");
9938 		memset(&pf->tmp_cfg, 0, sizeof(pf->tmp_cfg));
9939 		err = i40e_dcb_sw_default_config(pf);
9940 		if (err) {
9941 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
9942 				       I40E_FLAG_DCB_ENABLED);
9943 		} else {
9944 			pf->dcbx_cap = DCB_CAP_DCBX_HOST |
9945 				       DCB_CAP_DCBX_VER_IEEE;
9946 			pf->flags |= I40E_FLAG_DCB_CAPABLE;
9947 			pf->flags &= ~I40E_FLAG_DCB_ENABLED;
9948 		}
9949 	}
9950 #endif /* CONFIG_I40E_DCB */
9951 }
9952 
9953 /**
9954  * i40e_watchdog_subtask - periodic checks not using event driven response
9955  * @pf: board private structure
9956  **/
9957 static void i40e_watchdog_subtask(struct i40e_pf *pf)
9958 {
9959 	int i;
9960 
9961 	/* if interface is down do nothing */
9962 	if (test_bit(__I40E_DOWN, pf->state) ||
9963 	    test_bit(__I40E_CONFIG_BUSY, pf->state))
9964 		return;
9965 
9966 	/* make sure we don't do these things too often */
9967 	if (time_before(jiffies, (pf->service_timer_previous +
9968 				  pf->service_timer_period)))
9969 		return;
9970 	pf->service_timer_previous = jiffies;
9971 
9972 	if ((pf->flags & I40E_FLAG_LINK_POLLING_ENABLED) ||
9973 	    test_bit(__I40E_TEMP_LINK_POLLING, pf->state))
9974 		i40e_link_event(pf);
9975 
9976 	/* Update the stats for active netdevs so the network stack
9977 	 * can look at updated numbers whenever it cares to
9978 	 */
9979 	for (i = 0; i < pf->num_alloc_vsi; i++)
9980 		if (pf->vsi[i] && pf->vsi[i]->netdev)
9981 			i40e_update_stats(pf->vsi[i]);
9982 
9983 	if (pf->flags & I40E_FLAG_VEB_STATS_ENABLED) {
9984 		/* Update the stats for the active switching components */
9985 		for (i = 0; i < I40E_MAX_VEB; i++)
9986 			if (pf->veb[i])
9987 				i40e_update_veb_stats(pf->veb[i]);
9988 	}
9989 
9990 	i40e_ptp_rx_hang(pf);
9991 	i40e_ptp_tx_hang(pf);
9992 }
9993 
9994 /**
9995  * i40e_reset_subtask - Set up for resetting the device and driver
9996  * @pf: board private structure
9997  **/
9998 static void i40e_reset_subtask(struct i40e_pf *pf)
9999 {
10000 	u32 reset_flags = 0;
10001 
10002 	if (test_bit(__I40E_REINIT_REQUESTED, pf->state)) {
10003 		reset_flags |= BIT(__I40E_REINIT_REQUESTED);
10004 		clear_bit(__I40E_REINIT_REQUESTED, pf->state);
10005 	}
10006 	if (test_bit(__I40E_PF_RESET_REQUESTED, pf->state)) {
10007 		reset_flags |= BIT(__I40E_PF_RESET_REQUESTED);
10008 		clear_bit(__I40E_PF_RESET_REQUESTED, pf->state);
10009 	}
10010 	if (test_bit(__I40E_CORE_RESET_REQUESTED, pf->state)) {
10011 		reset_flags |= BIT(__I40E_CORE_RESET_REQUESTED);
10012 		clear_bit(__I40E_CORE_RESET_REQUESTED, pf->state);
10013 	}
10014 	if (test_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state)) {
10015 		reset_flags |= BIT(__I40E_GLOBAL_RESET_REQUESTED);
10016 		clear_bit(__I40E_GLOBAL_RESET_REQUESTED, pf->state);
10017 	}
10018 	if (test_bit(__I40E_DOWN_REQUESTED, pf->state)) {
10019 		reset_flags |= BIT(__I40E_DOWN_REQUESTED);
10020 		clear_bit(__I40E_DOWN_REQUESTED, pf->state);
10021 	}
10022 
10023 	/* If there's a recovery already waiting, it takes
10024 	 * precedence before starting a new reset sequence.
10025 	 */
10026 	if (test_bit(__I40E_RESET_INTR_RECEIVED, pf->state)) {
10027 		i40e_prep_for_reset(pf);
10028 		i40e_reset(pf);
10029 		i40e_rebuild(pf, false, false);
10030 	}
10031 
10032 	/* If we're already down or resetting, just bail */
10033 	if (reset_flags &&
10034 	    !test_bit(__I40E_DOWN, pf->state) &&
10035 	    !test_bit(__I40E_CONFIG_BUSY, pf->state)) {
10036 		i40e_do_reset(pf, reset_flags, false);
10037 	}
10038 }
10039 
10040 /**
10041  * i40e_handle_link_event - Handle link event
10042  * @pf: board private structure
10043  * @e: event info posted on ARQ
10044  **/
10045 static void i40e_handle_link_event(struct i40e_pf *pf,
10046 				   struct i40e_arq_event_info *e)
10047 {
10048 	struct i40e_aqc_get_link_status *status =
10049 		(struct i40e_aqc_get_link_status *)&e->desc.params.raw;
10050 
10051 	/* Do a new status request to re-enable LSE reporting
10052 	 * and load new status information into the hw struct
10053 	 * This completely ignores any state information
10054 	 * in the ARQ event info, instead choosing to always
10055 	 * issue the AQ update link status command.
10056 	 */
10057 	i40e_link_event(pf);
10058 
10059 	/* Check if module meets thermal requirements */
10060 	if (status->phy_type == I40E_PHY_TYPE_NOT_SUPPORTED_HIGH_TEMP) {
10061 		dev_err(&pf->pdev->dev,
10062 			"Rx/Tx is disabled on this device because the module does not meet thermal requirements.\n");
10063 		dev_err(&pf->pdev->dev,
10064 			"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10065 	} else {
10066 		/* check for unqualified module, if link is down, suppress
10067 		 * the message if link was forced to be down.
10068 		 */
10069 		if ((status->link_info & I40E_AQ_MEDIA_AVAILABLE) &&
10070 		    (!(status->an_info & I40E_AQ_QUALIFIED_MODULE)) &&
10071 		    (!(status->link_info & I40E_AQ_LINK_UP)) &&
10072 		    (!(pf->flags & I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED))) {
10073 			dev_err(&pf->pdev->dev,
10074 				"Rx/Tx is disabled on this device because an unsupported SFP module type was detected.\n");
10075 			dev_err(&pf->pdev->dev,
10076 				"Refer to the Intel(R) Ethernet Adapters and Devices User Guide for a list of supported modules.\n");
10077 		}
10078 	}
10079 }
10080 
10081 /**
10082  * i40e_clean_adminq_subtask - Clean the AdminQ rings
10083  * @pf: board private structure
10084  **/
10085 static void i40e_clean_adminq_subtask(struct i40e_pf *pf)
10086 {
10087 	struct i40e_arq_event_info event;
10088 	struct i40e_hw *hw = &pf->hw;
10089 	u16 pending, i = 0;
10090 	i40e_status ret;
10091 	u16 opcode;
10092 	u32 oldval;
10093 	u32 val;
10094 
10095 	/* Do not run clean AQ when PF reset fails */
10096 	if (test_bit(__I40E_RESET_FAILED, pf->state))
10097 		return;
10098 
10099 	/* check for error indications */
10100 	val = rd32(&pf->hw, pf->hw.aq.arq.len);
10101 	oldval = val;
10102 	if (val & I40E_PF_ARQLEN_ARQVFE_MASK) {
10103 		if (hw->debug_mask & I40E_DEBUG_AQ)
10104 			dev_info(&pf->pdev->dev, "ARQ VF Error detected\n");
10105 		val &= ~I40E_PF_ARQLEN_ARQVFE_MASK;
10106 	}
10107 	if (val & I40E_PF_ARQLEN_ARQOVFL_MASK) {
10108 		if (hw->debug_mask & I40E_DEBUG_AQ)
10109 			dev_info(&pf->pdev->dev, "ARQ Overflow Error detected\n");
10110 		val &= ~I40E_PF_ARQLEN_ARQOVFL_MASK;
10111 		pf->arq_overflows++;
10112 	}
10113 	if (val & I40E_PF_ARQLEN_ARQCRIT_MASK) {
10114 		if (hw->debug_mask & I40E_DEBUG_AQ)
10115 			dev_info(&pf->pdev->dev, "ARQ Critical Error detected\n");
10116 		val &= ~I40E_PF_ARQLEN_ARQCRIT_MASK;
10117 	}
10118 	if (oldval != val)
10119 		wr32(&pf->hw, pf->hw.aq.arq.len, val);
10120 
10121 	val = rd32(&pf->hw, pf->hw.aq.asq.len);
10122 	oldval = val;
10123 	if (val & I40E_PF_ATQLEN_ATQVFE_MASK) {
10124 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10125 			dev_info(&pf->pdev->dev, "ASQ VF Error detected\n");
10126 		val &= ~I40E_PF_ATQLEN_ATQVFE_MASK;
10127 	}
10128 	if (val & I40E_PF_ATQLEN_ATQOVFL_MASK) {
10129 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10130 			dev_info(&pf->pdev->dev, "ASQ Overflow Error detected\n");
10131 		val &= ~I40E_PF_ATQLEN_ATQOVFL_MASK;
10132 	}
10133 	if (val & I40E_PF_ATQLEN_ATQCRIT_MASK) {
10134 		if (pf->hw.debug_mask & I40E_DEBUG_AQ)
10135 			dev_info(&pf->pdev->dev, "ASQ Critical Error detected\n");
10136 		val &= ~I40E_PF_ATQLEN_ATQCRIT_MASK;
10137 	}
10138 	if (oldval != val)
10139 		wr32(&pf->hw, pf->hw.aq.asq.len, val);
10140 
10141 	event.buf_len = I40E_MAX_AQ_BUF_SIZE;
10142 	event.msg_buf = kzalloc(event.buf_len, GFP_KERNEL);
10143 	if (!event.msg_buf)
10144 		return;
10145 
10146 	do {
10147 		ret = i40e_clean_arq_element(hw, &event, &pending);
10148 		if (ret == I40E_ERR_ADMIN_QUEUE_NO_WORK)
10149 			break;
10150 		else if (ret) {
10151 			dev_info(&pf->pdev->dev, "ARQ event error %d\n", ret);
10152 			break;
10153 		}
10154 
10155 		opcode = le16_to_cpu(event.desc.opcode);
10156 		switch (opcode) {
10157 
10158 		case i40e_aqc_opc_get_link_status:
10159 			rtnl_lock();
10160 			i40e_handle_link_event(pf, &event);
10161 			rtnl_unlock();
10162 			break;
10163 		case i40e_aqc_opc_send_msg_to_pf:
10164 			ret = i40e_vc_process_vf_msg(pf,
10165 					le16_to_cpu(event.desc.retval),
10166 					le32_to_cpu(event.desc.cookie_high),
10167 					le32_to_cpu(event.desc.cookie_low),
10168 					event.msg_buf,
10169 					event.msg_len);
10170 			break;
10171 		case i40e_aqc_opc_lldp_update_mib:
10172 			dev_dbg(&pf->pdev->dev, "ARQ: Update LLDP MIB event received\n");
10173 #ifdef CONFIG_I40E_DCB
10174 			rtnl_lock();
10175 			i40e_handle_lldp_event(pf, &event);
10176 			rtnl_unlock();
10177 #endif /* CONFIG_I40E_DCB */
10178 			break;
10179 		case i40e_aqc_opc_event_lan_overflow:
10180 			dev_dbg(&pf->pdev->dev, "ARQ LAN queue overflow event received\n");
10181 			i40e_handle_lan_overflow_event(pf, &event);
10182 			break;
10183 		case i40e_aqc_opc_send_msg_to_peer:
10184 			dev_info(&pf->pdev->dev, "ARQ: Msg from other pf\n");
10185 			break;
10186 		case i40e_aqc_opc_nvm_erase:
10187 		case i40e_aqc_opc_nvm_update:
10188 		case i40e_aqc_opc_oem_post_update:
10189 			i40e_debug(&pf->hw, I40E_DEBUG_NVM,
10190 				   "ARQ NVM operation 0x%04x completed\n",
10191 				   opcode);
10192 			break;
10193 		default:
10194 			dev_info(&pf->pdev->dev,
10195 				 "ARQ: Unknown event 0x%04x ignored\n",
10196 				 opcode);
10197 			break;
10198 		}
10199 	} while (i++ < pf->adminq_work_limit);
10200 
10201 	if (i < pf->adminq_work_limit)
10202 		clear_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state);
10203 
10204 	/* re-enable Admin queue interrupt cause */
10205 	val = rd32(hw, I40E_PFINT_ICR0_ENA);
10206 	val |=  I40E_PFINT_ICR0_ENA_ADMINQ_MASK;
10207 	wr32(hw, I40E_PFINT_ICR0_ENA, val);
10208 	i40e_flush(hw);
10209 
10210 	kfree(event.msg_buf);
10211 }
10212 
10213 /**
10214  * i40e_verify_eeprom - make sure eeprom is good to use
10215  * @pf: board private structure
10216  **/
10217 static void i40e_verify_eeprom(struct i40e_pf *pf)
10218 {
10219 	int err;
10220 
10221 	err = i40e_diag_eeprom_test(&pf->hw);
10222 	if (err) {
10223 		/* retry in case of garbage read */
10224 		err = i40e_diag_eeprom_test(&pf->hw);
10225 		if (err) {
10226 			dev_info(&pf->pdev->dev, "eeprom check failed (%d), Tx/Rx traffic disabled\n",
10227 				 err);
10228 			set_bit(__I40E_BAD_EEPROM, pf->state);
10229 		}
10230 	}
10231 
10232 	if (!err && test_bit(__I40E_BAD_EEPROM, pf->state)) {
10233 		dev_info(&pf->pdev->dev, "eeprom check passed, Tx/Rx traffic enabled\n");
10234 		clear_bit(__I40E_BAD_EEPROM, pf->state);
10235 	}
10236 }
10237 
10238 /**
10239  * i40e_enable_pf_switch_lb
10240  * @pf: pointer to the PF structure
10241  *
10242  * enable switch loop back or die - no point in a return value
10243  **/
10244 static void i40e_enable_pf_switch_lb(struct i40e_pf *pf)
10245 {
10246 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10247 	struct i40e_vsi_context ctxt;
10248 	int ret;
10249 
10250 	ctxt.seid = pf->main_vsi_seid;
10251 	ctxt.pf_num = pf->hw.pf_id;
10252 	ctxt.vf_num = 0;
10253 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10254 	if (ret) {
10255 		dev_info(&pf->pdev->dev,
10256 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10257 			 i40e_stat_str(&pf->hw, ret),
10258 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10259 		return;
10260 	}
10261 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10262 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10263 	ctxt.info.switch_id |= cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10264 
10265 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10266 	if (ret) {
10267 		dev_info(&pf->pdev->dev,
10268 			 "update vsi switch failed, err %s aq_err %s\n",
10269 			 i40e_stat_str(&pf->hw, ret),
10270 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10271 	}
10272 }
10273 
10274 /**
10275  * i40e_disable_pf_switch_lb
10276  * @pf: pointer to the PF structure
10277  *
10278  * disable switch loop back or die - no point in a return value
10279  **/
10280 static void i40e_disable_pf_switch_lb(struct i40e_pf *pf)
10281 {
10282 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10283 	struct i40e_vsi_context ctxt;
10284 	int ret;
10285 
10286 	ctxt.seid = pf->main_vsi_seid;
10287 	ctxt.pf_num = pf->hw.pf_id;
10288 	ctxt.vf_num = 0;
10289 	ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
10290 	if (ret) {
10291 		dev_info(&pf->pdev->dev,
10292 			 "couldn't get PF vsi config, err %s aq_err %s\n",
10293 			 i40e_stat_str(&pf->hw, ret),
10294 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10295 		return;
10296 	}
10297 	ctxt.flags = I40E_AQ_VSI_TYPE_PF;
10298 	ctxt.info.valid_sections = cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
10299 	ctxt.info.switch_id &= ~cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
10300 
10301 	ret = i40e_aq_update_vsi_params(&vsi->back->hw, &ctxt, NULL);
10302 	if (ret) {
10303 		dev_info(&pf->pdev->dev,
10304 			 "update vsi switch failed, err %s aq_err %s\n",
10305 			 i40e_stat_str(&pf->hw, ret),
10306 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10307 	}
10308 }
10309 
10310 /**
10311  * i40e_config_bridge_mode - Configure the HW bridge mode
10312  * @veb: pointer to the bridge instance
10313  *
10314  * Configure the loop back mode for the LAN VSI that is downlink to the
10315  * specified HW bridge instance. It is expected this function is called
10316  * when a new HW bridge is instantiated.
10317  **/
10318 static void i40e_config_bridge_mode(struct i40e_veb *veb)
10319 {
10320 	struct i40e_pf *pf = veb->pf;
10321 
10322 	if (pf->hw.debug_mask & I40E_DEBUG_LAN)
10323 		dev_info(&pf->pdev->dev, "enabling bridge mode: %s\n",
10324 			 veb->bridge_mode == BRIDGE_MODE_VEPA ? "VEPA" : "VEB");
10325 	if (veb->bridge_mode & BRIDGE_MODE_VEPA)
10326 		i40e_disable_pf_switch_lb(pf);
10327 	else
10328 		i40e_enable_pf_switch_lb(pf);
10329 }
10330 
10331 /**
10332  * i40e_reconstitute_veb - rebuild the VEB and anything connected to it
10333  * @veb: pointer to the VEB instance
10334  *
10335  * This is a recursive function that first builds the attached VSIs then
10336  * recurses in to build the next layer of VEB.  We track the connections
10337  * through our own index numbers because the seid's from the HW could
10338  * change across the reset.
10339  **/
10340 static int i40e_reconstitute_veb(struct i40e_veb *veb)
10341 {
10342 	struct i40e_vsi *ctl_vsi = NULL;
10343 	struct i40e_pf *pf = veb->pf;
10344 	int v, veb_idx;
10345 	int ret;
10346 
10347 	/* build VSI that owns this VEB, temporarily attached to base VEB */
10348 	for (v = 0; v < pf->num_alloc_vsi && !ctl_vsi; v++) {
10349 		if (pf->vsi[v] &&
10350 		    pf->vsi[v]->veb_idx == veb->idx &&
10351 		    pf->vsi[v]->flags & I40E_VSI_FLAG_VEB_OWNER) {
10352 			ctl_vsi = pf->vsi[v];
10353 			break;
10354 		}
10355 	}
10356 	if (!ctl_vsi) {
10357 		dev_info(&pf->pdev->dev,
10358 			 "missing owner VSI for veb_idx %d\n", veb->idx);
10359 		ret = -ENOENT;
10360 		goto end_reconstitute;
10361 	}
10362 	if (ctl_vsi != pf->vsi[pf->lan_vsi])
10363 		ctl_vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
10364 	ret = i40e_add_vsi(ctl_vsi);
10365 	if (ret) {
10366 		dev_info(&pf->pdev->dev,
10367 			 "rebuild of veb_idx %d owner VSI failed: %d\n",
10368 			 veb->idx, ret);
10369 		goto end_reconstitute;
10370 	}
10371 	i40e_vsi_reset_stats(ctl_vsi);
10372 
10373 	/* create the VEB in the switch and move the VSI onto the VEB */
10374 	ret = i40e_add_veb(veb, ctl_vsi);
10375 	if (ret)
10376 		goto end_reconstitute;
10377 
10378 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
10379 		veb->bridge_mode = BRIDGE_MODE_VEB;
10380 	else
10381 		veb->bridge_mode = BRIDGE_MODE_VEPA;
10382 	i40e_config_bridge_mode(veb);
10383 
10384 	/* create the remaining VSIs attached to this VEB */
10385 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10386 		if (!pf->vsi[v] || pf->vsi[v] == ctl_vsi)
10387 			continue;
10388 
10389 		if (pf->vsi[v]->veb_idx == veb->idx) {
10390 			struct i40e_vsi *vsi = pf->vsi[v];
10391 
10392 			vsi->uplink_seid = veb->seid;
10393 			ret = i40e_add_vsi(vsi);
10394 			if (ret) {
10395 				dev_info(&pf->pdev->dev,
10396 					 "rebuild of vsi_idx %d failed: %d\n",
10397 					 v, ret);
10398 				goto end_reconstitute;
10399 			}
10400 			i40e_vsi_reset_stats(vsi);
10401 		}
10402 	}
10403 
10404 	/* create any VEBs attached to this VEB - RECURSION */
10405 	for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
10406 		if (pf->veb[veb_idx] && pf->veb[veb_idx]->veb_idx == veb->idx) {
10407 			pf->veb[veb_idx]->uplink_seid = veb->seid;
10408 			ret = i40e_reconstitute_veb(pf->veb[veb_idx]);
10409 			if (ret)
10410 				break;
10411 		}
10412 	}
10413 
10414 end_reconstitute:
10415 	return ret;
10416 }
10417 
10418 /**
10419  * i40e_get_capabilities - get info about the HW
10420  * @pf: the PF struct
10421  * @list_type: AQ capability to be queried
10422  **/
10423 static int i40e_get_capabilities(struct i40e_pf *pf,
10424 				 enum i40e_admin_queue_opc list_type)
10425 {
10426 	struct i40e_aqc_list_capabilities_element_resp *cap_buf;
10427 	u16 data_size;
10428 	int buf_len;
10429 	int err;
10430 
10431 	buf_len = 40 * sizeof(struct i40e_aqc_list_capabilities_element_resp);
10432 	do {
10433 		cap_buf = kzalloc(buf_len, GFP_KERNEL);
10434 		if (!cap_buf)
10435 			return -ENOMEM;
10436 
10437 		/* this loads the data into the hw struct for us */
10438 		err = i40e_aq_discover_capabilities(&pf->hw, cap_buf, buf_len,
10439 						    &data_size, list_type,
10440 						    NULL);
10441 		/* data loaded, buffer no longer needed */
10442 		kfree(cap_buf);
10443 
10444 		if (pf->hw.aq.asq_last_status == I40E_AQ_RC_ENOMEM) {
10445 			/* retry with a larger buffer */
10446 			buf_len = data_size;
10447 		} else if (pf->hw.aq.asq_last_status != I40E_AQ_RC_OK || err) {
10448 			dev_info(&pf->pdev->dev,
10449 				 "capability discovery failed, err %s aq_err %s\n",
10450 				 i40e_stat_str(&pf->hw, err),
10451 				 i40e_aq_str(&pf->hw,
10452 					     pf->hw.aq.asq_last_status));
10453 			return -ENODEV;
10454 		}
10455 	} while (err);
10456 
10457 	if (pf->hw.debug_mask & I40E_DEBUG_USER) {
10458 		if (list_type == i40e_aqc_opc_list_func_capabilities) {
10459 			dev_info(&pf->pdev->dev,
10460 				 "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",
10461 				 pf->hw.pf_id, pf->hw.func_caps.num_vfs,
10462 				 pf->hw.func_caps.num_msix_vectors,
10463 				 pf->hw.func_caps.num_msix_vectors_vf,
10464 				 pf->hw.func_caps.fd_filters_guaranteed,
10465 				 pf->hw.func_caps.fd_filters_best_effort,
10466 				 pf->hw.func_caps.num_tx_qp,
10467 				 pf->hw.func_caps.num_vsis);
10468 		} else if (list_type == i40e_aqc_opc_list_dev_capabilities) {
10469 			dev_info(&pf->pdev->dev,
10470 				 "switch_mode=0x%04x, function_valid=0x%08x\n",
10471 				 pf->hw.dev_caps.switch_mode,
10472 				 pf->hw.dev_caps.valid_functions);
10473 			dev_info(&pf->pdev->dev,
10474 				 "SR-IOV=%d, num_vfs for all function=%u\n",
10475 				 pf->hw.dev_caps.sr_iov_1_1,
10476 				 pf->hw.dev_caps.num_vfs);
10477 			dev_info(&pf->pdev->dev,
10478 				 "num_vsis=%u, num_rx:%u, num_tx=%u\n",
10479 				 pf->hw.dev_caps.num_vsis,
10480 				 pf->hw.dev_caps.num_rx_qp,
10481 				 pf->hw.dev_caps.num_tx_qp);
10482 		}
10483 	}
10484 	if (list_type == i40e_aqc_opc_list_func_capabilities) {
10485 #define DEF_NUM_VSI (1 + (pf->hw.func_caps.fcoe ? 1 : 0) \
10486 		       + pf->hw.func_caps.num_vfs)
10487 		if (pf->hw.revision_id == 0 &&
10488 		    pf->hw.func_caps.num_vsis < DEF_NUM_VSI) {
10489 			dev_info(&pf->pdev->dev,
10490 				 "got num_vsis %d, setting num_vsis to %d\n",
10491 				 pf->hw.func_caps.num_vsis, DEF_NUM_VSI);
10492 			pf->hw.func_caps.num_vsis = DEF_NUM_VSI;
10493 		}
10494 	}
10495 	return 0;
10496 }
10497 
10498 static int i40e_vsi_clear(struct i40e_vsi *vsi);
10499 
10500 /**
10501  * i40e_fdir_sb_setup - initialize the Flow Director resources for Sideband
10502  * @pf: board private structure
10503  **/
10504 static void i40e_fdir_sb_setup(struct i40e_pf *pf)
10505 {
10506 	struct i40e_vsi *vsi;
10507 
10508 	/* quick workaround for an NVM issue that leaves a critical register
10509 	 * uninitialized
10510 	 */
10511 	if (!rd32(&pf->hw, I40E_GLQF_HKEY(0))) {
10512 		static const u32 hkey[] = {
10513 			0xe640d33f, 0xcdfe98ab, 0x73fa7161, 0x0d7a7d36,
10514 			0xeacb7d61, 0xaa4f05b6, 0x9c5c89ed, 0xfc425ddb,
10515 			0xa4654832, 0xfc7461d4, 0x8f827619, 0xf5c63c21,
10516 			0x95b3a76d};
10517 		int i;
10518 
10519 		for (i = 0; i <= I40E_GLQF_HKEY_MAX_INDEX; i++)
10520 			wr32(&pf->hw, I40E_GLQF_HKEY(i), hkey[i]);
10521 	}
10522 
10523 	if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
10524 		return;
10525 
10526 	/* find existing VSI and see if it needs configuring */
10527 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10528 
10529 	/* create a new VSI if none exists */
10530 	if (!vsi) {
10531 		vsi = i40e_vsi_setup(pf, I40E_VSI_FDIR,
10532 				     pf->vsi[pf->lan_vsi]->seid, 0);
10533 		if (!vsi) {
10534 			dev_info(&pf->pdev->dev, "Couldn't create FDir VSI\n");
10535 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
10536 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
10537 			return;
10538 		}
10539 	}
10540 
10541 	i40e_vsi_setup_irqhandler(vsi, i40e_fdir_clean_ring);
10542 }
10543 
10544 /**
10545  * i40e_fdir_teardown - release the Flow Director resources
10546  * @pf: board private structure
10547  **/
10548 static void i40e_fdir_teardown(struct i40e_pf *pf)
10549 {
10550 	struct i40e_vsi *vsi;
10551 
10552 	i40e_fdir_filter_exit(pf);
10553 	vsi = i40e_find_vsi_by_type(pf, I40E_VSI_FDIR);
10554 	if (vsi)
10555 		i40e_vsi_release(vsi);
10556 }
10557 
10558 /**
10559  * i40e_rebuild_cloud_filters - Rebuilds cloud filters for VSIs
10560  * @vsi: PF main vsi
10561  * @seid: seid of main or channel VSIs
10562  *
10563  * Rebuilds cloud filters associated with main VSI and channel VSIs if they
10564  * existed before reset
10565  **/
10566 static int i40e_rebuild_cloud_filters(struct i40e_vsi *vsi, u16 seid)
10567 {
10568 	struct i40e_cloud_filter *cfilter;
10569 	struct i40e_pf *pf = vsi->back;
10570 	struct hlist_node *node;
10571 	i40e_status ret;
10572 
10573 	/* Add cloud filters back if they exist */
10574 	hlist_for_each_entry_safe(cfilter, node, &pf->cloud_filter_list,
10575 				  cloud_node) {
10576 		if (cfilter->seid != seid)
10577 			continue;
10578 
10579 		if (cfilter->dst_port)
10580 			ret = i40e_add_del_cloud_filter_big_buf(vsi, cfilter,
10581 								true);
10582 		else
10583 			ret = i40e_add_del_cloud_filter(vsi, cfilter, true);
10584 
10585 		if (ret) {
10586 			dev_dbg(&pf->pdev->dev,
10587 				"Failed to rebuild cloud filter, err %s aq_err %s\n",
10588 				i40e_stat_str(&pf->hw, ret),
10589 				i40e_aq_str(&pf->hw,
10590 					    pf->hw.aq.asq_last_status));
10591 			return ret;
10592 		}
10593 	}
10594 	return 0;
10595 }
10596 
10597 /**
10598  * i40e_rebuild_channels - Rebuilds channel VSIs if they existed before reset
10599  * @vsi: PF main vsi
10600  *
10601  * Rebuilds channel VSIs if they existed before reset
10602  **/
10603 static int i40e_rebuild_channels(struct i40e_vsi *vsi)
10604 {
10605 	struct i40e_channel *ch, *ch_tmp;
10606 	i40e_status ret;
10607 
10608 	if (list_empty(&vsi->ch_list))
10609 		return 0;
10610 
10611 	list_for_each_entry_safe(ch, ch_tmp, &vsi->ch_list, list) {
10612 		if (!ch->initialized)
10613 			break;
10614 		/* Proceed with creation of channel (VMDq2) VSI */
10615 		ret = i40e_add_channel(vsi->back, vsi->uplink_seid, ch);
10616 		if (ret) {
10617 			dev_info(&vsi->back->pdev->dev,
10618 				 "failed to rebuild channels using uplink_seid %u\n",
10619 				 vsi->uplink_seid);
10620 			return ret;
10621 		}
10622 		/* Reconfigure TX queues using QTX_CTL register */
10623 		ret = i40e_channel_config_tx_ring(vsi->back, vsi, ch);
10624 		if (ret) {
10625 			dev_info(&vsi->back->pdev->dev,
10626 				 "failed to configure TX rings for channel %u\n",
10627 				 ch->seid);
10628 			return ret;
10629 		}
10630 		/* update 'next_base_queue' */
10631 		vsi->next_base_queue = vsi->next_base_queue +
10632 							ch->num_queue_pairs;
10633 		if (ch->max_tx_rate) {
10634 			u64 credits = ch->max_tx_rate;
10635 
10636 			if (i40e_set_bw_limit(vsi, ch->seid,
10637 					      ch->max_tx_rate))
10638 				return -EINVAL;
10639 
10640 			do_div(credits, I40E_BW_CREDIT_DIVISOR);
10641 			dev_dbg(&vsi->back->pdev->dev,
10642 				"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10643 				ch->max_tx_rate,
10644 				credits,
10645 				ch->seid);
10646 		}
10647 		ret = i40e_rebuild_cloud_filters(vsi, ch->seid);
10648 		if (ret) {
10649 			dev_dbg(&vsi->back->pdev->dev,
10650 				"Failed to rebuild cloud filters for channel VSI %u\n",
10651 				ch->seid);
10652 			return ret;
10653 		}
10654 	}
10655 	return 0;
10656 }
10657 
10658 /**
10659  * i40e_prep_for_reset - prep for the core to reset
10660  * @pf: board private structure
10661  *
10662  * Close up the VFs and other things in prep for PF Reset.
10663   **/
10664 static void i40e_prep_for_reset(struct i40e_pf *pf)
10665 {
10666 	struct i40e_hw *hw = &pf->hw;
10667 	i40e_status ret = 0;
10668 	u32 v;
10669 
10670 	clear_bit(__I40E_RESET_INTR_RECEIVED, pf->state);
10671 	if (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
10672 		return;
10673 	if (i40e_check_asq_alive(&pf->hw))
10674 		i40e_vc_notify_reset(pf);
10675 
10676 	dev_dbg(&pf->pdev->dev, "Tearing down internal switch for reset\n");
10677 
10678 	/* quiesce the VSIs and their queues that are not already DOWN */
10679 	i40e_pf_quiesce_all_vsi(pf);
10680 
10681 	for (v = 0; v < pf->num_alloc_vsi; v++) {
10682 		if (pf->vsi[v])
10683 			pf->vsi[v]->seid = 0;
10684 	}
10685 
10686 	i40e_shutdown_adminq(&pf->hw);
10687 
10688 	/* call shutdown HMC */
10689 	if (hw->hmc.hmc_obj) {
10690 		ret = i40e_shutdown_lan_hmc(hw);
10691 		if (ret)
10692 			dev_warn(&pf->pdev->dev,
10693 				 "shutdown_lan_hmc failed: %d\n", ret);
10694 	}
10695 
10696 	/* Save the current PTP time so that we can restore the time after the
10697 	 * reset completes.
10698 	 */
10699 	i40e_ptp_save_hw_time(pf);
10700 }
10701 
10702 /**
10703  * i40e_send_version - update firmware with driver version
10704  * @pf: PF struct
10705  */
10706 static void i40e_send_version(struct i40e_pf *pf)
10707 {
10708 	struct i40e_driver_version dv;
10709 
10710 	dv.major_version = 0xff;
10711 	dv.minor_version = 0xff;
10712 	dv.build_version = 0xff;
10713 	dv.subbuild_version = 0;
10714 	strscpy(dv.driver_string, UTS_RELEASE, sizeof(dv.driver_string));
10715 	i40e_aq_send_driver_version(&pf->hw, &dv, NULL);
10716 }
10717 
10718 /**
10719  * i40e_get_oem_version - get OEM specific version information
10720  * @hw: pointer to the hardware structure
10721  **/
10722 static void i40e_get_oem_version(struct i40e_hw *hw)
10723 {
10724 	u16 block_offset = 0xffff;
10725 	u16 block_length = 0;
10726 	u16 capabilities = 0;
10727 	u16 gen_snap = 0;
10728 	u16 release = 0;
10729 
10730 #define I40E_SR_NVM_OEM_VERSION_PTR		0x1B
10731 #define I40E_NVM_OEM_LENGTH_OFFSET		0x00
10732 #define I40E_NVM_OEM_CAPABILITIES_OFFSET	0x01
10733 #define I40E_NVM_OEM_GEN_OFFSET			0x02
10734 #define I40E_NVM_OEM_RELEASE_OFFSET		0x03
10735 #define I40E_NVM_OEM_CAPABILITIES_MASK		0x000F
10736 #define I40E_NVM_OEM_LENGTH			3
10737 
10738 	/* Check if pointer to OEM version block is valid. */
10739 	i40e_read_nvm_word(hw, I40E_SR_NVM_OEM_VERSION_PTR, &block_offset);
10740 	if (block_offset == 0xffff)
10741 		return;
10742 
10743 	/* Check if OEM version block has correct length. */
10744 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_LENGTH_OFFSET,
10745 			   &block_length);
10746 	if (block_length < I40E_NVM_OEM_LENGTH)
10747 		return;
10748 
10749 	/* Check if OEM version format is as expected. */
10750 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_CAPABILITIES_OFFSET,
10751 			   &capabilities);
10752 	if ((capabilities & I40E_NVM_OEM_CAPABILITIES_MASK) != 0)
10753 		return;
10754 
10755 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_GEN_OFFSET,
10756 			   &gen_snap);
10757 	i40e_read_nvm_word(hw, block_offset + I40E_NVM_OEM_RELEASE_OFFSET,
10758 			   &release);
10759 	hw->nvm.oem_ver = (gen_snap << I40E_OEM_SNAP_SHIFT) | release;
10760 	hw->nvm.eetrack = I40E_OEM_EETRACK_ID;
10761 }
10762 
10763 /**
10764  * i40e_reset - wait for core reset to finish reset, reset pf if corer not seen
10765  * @pf: board private structure
10766  **/
10767 static int i40e_reset(struct i40e_pf *pf)
10768 {
10769 	struct i40e_hw *hw = &pf->hw;
10770 	i40e_status ret;
10771 
10772 	ret = i40e_pf_reset(hw);
10773 	if (ret) {
10774 		dev_info(&pf->pdev->dev, "PF reset failed, %d\n", ret);
10775 		set_bit(__I40E_RESET_FAILED, pf->state);
10776 		clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
10777 	} else {
10778 		pf->pfr_count++;
10779 	}
10780 	return ret;
10781 }
10782 
10783 /**
10784  * i40e_rebuild - rebuild using a saved config
10785  * @pf: board private structure
10786  * @reinit: if the Main VSI needs to re-initialized.
10787  * @lock_acquired: indicates whether or not the lock has been acquired
10788  * before this function was called.
10789  **/
10790 static void i40e_rebuild(struct i40e_pf *pf, bool reinit, bool lock_acquired)
10791 {
10792 	const bool is_recovery_mode_reported = i40e_check_recovery_mode(pf);
10793 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
10794 	struct i40e_hw *hw = &pf->hw;
10795 	i40e_status ret;
10796 	u32 val;
10797 	int v;
10798 
10799 	if (test_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state) &&
10800 	    is_recovery_mode_reported)
10801 		i40e_set_ethtool_ops(pf->vsi[pf->lan_vsi]->netdev);
10802 
10803 	if (test_bit(__I40E_DOWN, pf->state) &&
10804 	    !test_bit(__I40E_RECOVERY_MODE, pf->state))
10805 		goto clear_recovery;
10806 	dev_dbg(&pf->pdev->dev, "Rebuilding internal switch\n");
10807 
10808 	/* rebuild the basics for the AdminQ, HMC, and initial HW switch */
10809 	ret = i40e_init_adminq(&pf->hw);
10810 	if (ret) {
10811 		dev_info(&pf->pdev->dev, "Rebuild AdminQ failed, err %s aq_err %s\n",
10812 			 i40e_stat_str(&pf->hw, ret),
10813 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10814 		goto clear_recovery;
10815 	}
10816 	i40e_get_oem_version(&pf->hw);
10817 
10818 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state)) {
10819 		/* The following delay is necessary for firmware update. */
10820 		mdelay(1000);
10821 	}
10822 
10823 	/* re-verify the eeprom if we just had an EMP reset */
10824 	if (test_and_clear_bit(__I40E_EMP_RESET_INTR_RECEIVED, pf->state))
10825 		i40e_verify_eeprom(pf);
10826 
10827 	/* if we are going out of or into recovery mode we have to act
10828 	 * accordingly with regard to resources initialization
10829 	 * and deinitialization
10830 	 */
10831 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
10832 		if (i40e_get_capabilities(pf,
10833 					  i40e_aqc_opc_list_func_capabilities))
10834 			goto end_unlock;
10835 
10836 		if (is_recovery_mode_reported) {
10837 			/* we're staying in recovery mode so we'll reinitialize
10838 			 * misc vector here
10839 			 */
10840 			if (i40e_setup_misc_vector_for_recovery_mode(pf))
10841 				goto end_unlock;
10842 		} else {
10843 			if (!lock_acquired)
10844 				rtnl_lock();
10845 			/* we're going out of recovery mode so we'll free
10846 			 * the IRQ allocated specifically for recovery mode
10847 			 * and restore the interrupt scheme
10848 			 */
10849 			free_irq(pf->pdev->irq, pf);
10850 			i40e_clear_interrupt_scheme(pf);
10851 			if (i40e_restore_interrupt_scheme(pf))
10852 				goto end_unlock;
10853 		}
10854 
10855 		/* tell the firmware that we're starting */
10856 		i40e_send_version(pf);
10857 
10858 		/* bail out in case recovery mode was detected, as there is
10859 		 * no need for further configuration.
10860 		 */
10861 		goto end_unlock;
10862 	}
10863 
10864 	i40e_clear_pxe_mode(hw);
10865 	ret = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
10866 	if (ret)
10867 		goto end_core_reset;
10868 
10869 	ret = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
10870 				hw->func_caps.num_rx_qp, 0, 0);
10871 	if (ret) {
10872 		dev_info(&pf->pdev->dev, "init_lan_hmc failed: %d\n", ret);
10873 		goto end_core_reset;
10874 	}
10875 	ret = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
10876 	if (ret) {
10877 		dev_info(&pf->pdev->dev, "configure_lan_hmc failed: %d\n", ret);
10878 		goto end_core_reset;
10879 	}
10880 
10881 #ifdef CONFIG_I40E_DCB
10882 	/* Enable FW to write a default DCB config on link-up
10883 	 * unless I40E_FLAG_TC_MQPRIO was enabled or DCB
10884 	 * is not supported with new link speed
10885 	 */
10886 	if (i40e_is_tc_mqprio_enabled(pf)) {
10887 		i40e_aq_set_dcb_parameters(hw, false, NULL);
10888 	} else {
10889 		if (I40E_IS_X710TL_DEVICE(hw->device_id) &&
10890 		    (hw->phy.link_info.link_speed &
10891 		     (I40E_LINK_SPEED_2_5GB | I40E_LINK_SPEED_5GB))) {
10892 			i40e_aq_set_dcb_parameters(hw, false, NULL);
10893 			dev_warn(&pf->pdev->dev,
10894 				 "DCB is not supported for X710-T*L 2.5/5G speeds\n");
10895 			pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10896 		} else {
10897 			i40e_aq_set_dcb_parameters(hw, true, NULL);
10898 			ret = i40e_init_pf_dcb(pf);
10899 			if (ret) {
10900 				dev_info(&pf->pdev->dev, "DCB init failed %d, disabled\n",
10901 					 ret);
10902 				pf->flags &= ~I40E_FLAG_DCB_CAPABLE;
10903 				/* Continue without DCB enabled */
10904 			}
10905 		}
10906 	}
10907 
10908 #endif /* CONFIG_I40E_DCB */
10909 	if (!lock_acquired)
10910 		rtnl_lock();
10911 	ret = i40e_setup_pf_switch(pf, reinit, true);
10912 	if (ret)
10913 		goto end_unlock;
10914 
10915 	/* The driver only wants link up/down and module qualification
10916 	 * reports from firmware.  Note the negative logic.
10917 	 */
10918 	ret = i40e_aq_set_phy_int_mask(&pf->hw,
10919 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
10920 					 I40E_AQ_EVENT_MEDIA_NA |
10921 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
10922 	if (ret)
10923 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
10924 			 i40e_stat_str(&pf->hw, ret),
10925 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
10926 
10927 	/* Rebuild the VSIs and VEBs that existed before reset.
10928 	 * They are still in our local switch element arrays, so only
10929 	 * need to rebuild the switch model in the HW.
10930 	 *
10931 	 * If there were VEBs but the reconstitution failed, we'll try
10932 	 * to recover minimal use by getting the basic PF VSI working.
10933 	 */
10934 	if (vsi->uplink_seid != pf->mac_seid) {
10935 		dev_dbg(&pf->pdev->dev, "attempting to rebuild switch\n");
10936 		/* find the one VEB connected to the MAC, and find orphans */
10937 		for (v = 0; v < I40E_MAX_VEB; v++) {
10938 			if (!pf->veb[v])
10939 				continue;
10940 
10941 			if (pf->veb[v]->uplink_seid == pf->mac_seid ||
10942 			    pf->veb[v]->uplink_seid == 0) {
10943 				ret = i40e_reconstitute_veb(pf->veb[v]);
10944 
10945 				if (!ret)
10946 					continue;
10947 
10948 				/* If Main VEB failed, we're in deep doodoo,
10949 				 * so give up rebuilding the switch and set up
10950 				 * for minimal rebuild of PF VSI.
10951 				 * If orphan failed, we'll report the error
10952 				 * but try to keep going.
10953 				 */
10954 				if (pf->veb[v]->uplink_seid == pf->mac_seid) {
10955 					dev_info(&pf->pdev->dev,
10956 						 "rebuild of switch failed: %d, will try to set up simple PF connection\n",
10957 						 ret);
10958 					vsi->uplink_seid = pf->mac_seid;
10959 					break;
10960 				} else if (pf->veb[v]->uplink_seid == 0) {
10961 					dev_info(&pf->pdev->dev,
10962 						 "rebuild of orphan VEB failed: %d\n",
10963 						 ret);
10964 				}
10965 			}
10966 		}
10967 	}
10968 
10969 	if (vsi->uplink_seid == pf->mac_seid) {
10970 		dev_dbg(&pf->pdev->dev, "attempting to rebuild PF VSI\n");
10971 		/* no VEB, so rebuild only the Main VSI */
10972 		ret = i40e_add_vsi(vsi);
10973 		if (ret) {
10974 			dev_info(&pf->pdev->dev,
10975 				 "rebuild of Main VSI failed: %d\n", ret);
10976 			goto end_unlock;
10977 		}
10978 	}
10979 
10980 	if (vsi->mqprio_qopt.max_rate[0]) {
10981 		u64 max_tx_rate = i40e_bw_bytes_to_mbits(vsi,
10982 						  vsi->mqprio_qopt.max_rate[0]);
10983 		u64 credits = 0;
10984 
10985 		ret = i40e_set_bw_limit(vsi, vsi->seid, max_tx_rate);
10986 		if (ret)
10987 			goto end_unlock;
10988 
10989 		credits = max_tx_rate;
10990 		do_div(credits, I40E_BW_CREDIT_DIVISOR);
10991 		dev_dbg(&vsi->back->pdev->dev,
10992 			"Set tx rate of %llu Mbps (count of 50Mbps %llu) for vsi->seid %u\n",
10993 			max_tx_rate,
10994 			credits,
10995 			vsi->seid);
10996 	}
10997 
10998 	ret = i40e_rebuild_cloud_filters(vsi, vsi->seid);
10999 	if (ret)
11000 		goto end_unlock;
11001 
11002 	/* PF Main VSI is rebuild by now, go ahead and rebuild channel VSIs
11003 	 * for this main VSI if they exist
11004 	 */
11005 	ret = i40e_rebuild_channels(vsi);
11006 	if (ret)
11007 		goto end_unlock;
11008 
11009 	/* Reconfigure hardware for allowing smaller MSS in the case
11010 	 * of TSO, so that we avoid the MDD being fired and causing
11011 	 * a reset in the case of small MSS+TSO.
11012 	 */
11013 #define I40E_REG_MSS          0x000E64DC
11014 #define I40E_REG_MSS_MIN_MASK 0x3FF0000
11015 #define I40E_64BYTE_MSS       0x400000
11016 	val = rd32(hw, I40E_REG_MSS);
11017 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
11018 		val &= ~I40E_REG_MSS_MIN_MASK;
11019 		val |= I40E_64BYTE_MSS;
11020 		wr32(hw, I40E_REG_MSS, val);
11021 	}
11022 
11023 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
11024 		msleep(75);
11025 		ret = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
11026 		if (ret)
11027 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
11028 				 i40e_stat_str(&pf->hw, ret),
11029 				 i40e_aq_str(&pf->hw,
11030 					     pf->hw.aq.asq_last_status));
11031 	}
11032 	/* reinit the misc interrupt */
11033 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11034 		ret = i40e_setup_misc_vector(pf);
11035 
11036 	/* Add a filter to drop all Flow control frames from any VSI from being
11037 	 * transmitted. By doing so we stop a malicious VF from sending out
11038 	 * PAUSE or PFC frames and potentially controlling traffic for other
11039 	 * PF/VF VSIs.
11040 	 * The FW can still send Flow control frames if enabled.
11041 	 */
11042 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
11043 						       pf->main_vsi_seid);
11044 
11045 	/* restart the VSIs that were rebuilt and running before the reset */
11046 	i40e_pf_unquiesce_all_vsi(pf);
11047 
11048 	/* Release the RTNL lock before we start resetting VFs */
11049 	if (!lock_acquired)
11050 		rtnl_unlock();
11051 
11052 	/* Restore promiscuous settings */
11053 	ret = i40e_set_promiscuous(pf, pf->cur_promisc);
11054 	if (ret)
11055 		dev_warn(&pf->pdev->dev,
11056 			 "Failed to restore promiscuous setting: %s, err %s aq_err %s\n",
11057 			 pf->cur_promisc ? "on" : "off",
11058 			 i40e_stat_str(&pf->hw, ret),
11059 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
11060 
11061 	i40e_reset_all_vfs(pf, true);
11062 
11063 	/* tell the firmware that we're starting */
11064 	i40e_send_version(pf);
11065 
11066 	/* We've already released the lock, so don't do it again */
11067 	goto end_core_reset;
11068 
11069 end_unlock:
11070 	if (!lock_acquired)
11071 		rtnl_unlock();
11072 end_core_reset:
11073 	clear_bit(__I40E_RESET_FAILED, pf->state);
11074 clear_recovery:
11075 	clear_bit(__I40E_RESET_RECOVERY_PENDING, pf->state);
11076 	clear_bit(__I40E_TIMEOUT_RECOVERY_PENDING, pf->state);
11077 }
11078 
11079 /**
11080  * i40e_reset_and_rebuild - reset and rebuild using a saved config
11081  * @pf: board private structure
11082  * @reinit: if the Main VSI needs to re-initialized.
11083  * @lock_acquired: indicates whether or not the lock has been acquired
11084  * before this function was called.
11085  **/
11086 static void i40e_reset_and_rebuild(struct i40e_pf *pf, bool reinit,
11087 				   bool lock_acquired)
11088 {
11089 	int ret;
11090 
11091 	if (test_bit(__I40E_IN_REMOVE, pf->state))
11092 		return;
11093 	/* Now we wait for GRST to settle out.
11094 	 * We don't have to delete the VEBs or VSIs from the hw switch
11095 	 * because the reset will make them disappear.
11096 	 */
11097 	ret = i40e_reset(pf);
11098 	if (!ret)
11099 		i40e_rebuild(pf, reinit, lock_acquired);
11100 }
11101 
11102 /**
11103  * i40e_handle_reset_warning - prep for the PF to reset, reset and rebuild
11104  * @pf: board private structure
11105  *
11106  * Close up the VFs and other things in prep for a Core Reset,
11107  * then get ready to rebuild the world.
11108  * @lock_acquired: indicates whether or not the lock has been acquired
11109  * before this function was called.
11110  **/
11111 static void i40e_handle_reset_warning(struct i40e_pf *pf, bool lock_acquired)
11112 {
11113 	i40e_prep_for_reset(pf);
11114 	i40e_reset_and_rebuild(pf, false, lock_acquired);
11115 }
11116 
11117 /**
11118  * i40e_handle_mdd_event
11119  * @pf: pointer to the PF structure
11120  *
11121  * Called from the MDD irq handler to identify possibly malicious vfs
11122  **/
11123 static void i40e_handle_mdd_event(struct i40e_pf *pf)
11124 {
11125 	struct i40e_hw *hw = &pf->hw;
11126 	bool mdd_detected = false;
11127 	struct i40e_vf *vf;
11128 	u32 reg;
11129 	int i;
11130 
11131 	if (!test_bit(__I40E_MDD_EVENT_PENDING, pf->state))
11132 		return;
11133 
11134 	/* find what triggered the MDD event */
11135 	reg = rd32(hw, I40E_GL_MDET_TX);
11136 	if (reg & I40E_GL_MDET_TX_VALID_MASK) {
11137 		u8 pf_num = (reg & I40E_GL_MDET_TX_PF_NUM_MASK) >>
11138 				I40E_GL_MDET_TX_PF_NUM_SHIFT;
11139 		u16 vf_num = (reg & I40E_GL_MDET_TX_VF_NUM_MASK) >>
11140 				I40E_GL_MDET_TX_VF_NUM_SHIFT;
11141 		u8 event = (reg & I40E_GL_MDET_TX_EVENT_MASK) >>
11142 				I40E_GL_MDET_TX_EVENT_SHIFT;
11143 		u16 queue = ((reg & I40E_GL_MDET_TX_QUEUE_MASK) >>
11144 				I40E_GL_MDET_TX_QUEUE_SHIFT) -
11145 				pf->hw.func_caps.base_queue;
11146 		if (netif_msg_tx_err(pf))
11147 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on TX queue %d PF number 0x%02x VF number 0x%02x\n",
11148 				 event, queue, pf_num, vf_num);
11149 		wr32(hw, I40E_GL_MDET_TX, 0xffffffff);
11150 		mdd_detected = true;
11151 	}
11152 	reg = rd32(hw, I40E_GL_MDET_RX);
11153 	if (reg & I40E_GL_MDET_RX_VALID_MASK) {
11154 		u8 func = (reg & I40E_GL_MDET_RX_FUNCTION_MASK) >>
11155 				I40E_GL_MDET_RX_FUNCTION_SHIFT;
11156 		u8 event = (reg & I40E_GL_MDET_RX_EVENT_MASK) >>
11157 				I40E_GL_MDET_RX_EVENT_SHIFT;
11158 		u16 queue = ((reg & I40E_GL_MDET_RX_QUEUE_MASK) >>
11159 				I40E_GL_MDET_RX_QUEUE_SHIFT) -
11160 				pf->hw.func_caps.base_queue;
11161 		if (netif_msg_rx_err(pf))
11162 			dev_info(&pf->pdev->dev, "Malicious Driver Detection event 0x%02x on RX queue %d of function 0x%02x\n",
11163 				 event, queue, func);
11164 		wr32(hw, I40E_GL_MDET_RX, 0xffffffff);
11165 		mdd_detected = true;
11166 	}
11167 
11168 	if (mdd_detected) {
11169 		reg = rd32(hw, I40E_PF_MDET_TX);
11170 		if (reg & I40E_PF_MDET_TX_VALID_MASK) {
11171 			wr32(hw, I40E_PF_MDET_TX, 0xFFFF);
11172 			dev_dbg(&pf->pdev->dev, "TX driver issue detected on PF\n");
11173 		}
11174 		reg = rd32(hw, I40E_PF_MDET_RX);
11175 		if (reg & I40E_PF_MDET_RX_VALID_MASK) {
11176 			wr32(hw, I40E_PF_MDET_RX, 0xFFFF);
11177 			dev_dbg(&pf->pdev->dev, "RX driver issue detected on PF\n");
11178 		}
11179 	}
11180 
11181 	/* see if one of the VFs needs its hand slapped */
11182 	for (i = 0; i < pf->num_alloc_vfs && mdd_detected; i++) {
11183 		vf = &(pf->vf[i]);
11184 		reg = rd32(hw, I40E_VP_MDET_TX(i));
11185 		if (reg & I40E_VP_MDET_TX_VALID_MASK) {
11186 			wr32(hw, I40E_VP_MDET_TX(i), 0xFFFF);
11187 			vf->num_mdd_events++;
11188 			dev_info(&pf->pdev->dev, "TX driver issue detected on VF %d\n",
11189 				 i);
11190 			dev_info(&pf->pdev->dev,
11191 				 "Use PF Control I/F to re-enable the VF\n");
11192 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11193 		}
11194 
11195 		reg = rd32(hw, I40E_VP_MDET_RX(i));
11196 		if (reg & I40E_VP_MDET_RX_VALID_MASK) {
11197 			wr32(hw, I40E_VP_MDET_RX(i), 0xFFFF);
11198 			vf->num_mdd_events++;
11199 			dev_info(&pf->pdev->dev, "RX driver issue detected on VF %d\n",
11200 				 i);
11201 			dev_info(&pf->pdev->dev,
11202 				 "Use PF Control I/F to re-enable the VF\n");
11203 			set_bit(I40E_VF_STATE_DISABLED, &vf->vf_states);
11204 		}
11205 	}
11206 
11207 	/* re-enable mdd interrupt cause */
11208 	clear_bit(__I40E_MDD_EVENT_PENDING, pf->state);
11209 	reg = rd32(hw, I40E_PFINT_ICR0_ENA);
11210 	reg |=  I40E_PFINT_ICR0_ENA_MAL_DETECT_MASK;
11211 	wr32(hw, I40E_PFINT_ICR0_ENA, reg);
11212 	i40e_flush(hw);
11213 }
11214 
11215 /**
11216  * i40e_service_task - Run the driver's async subtasks
11217  * @work: pointer to work_struct containing our data
11218  **/
11219 static void i40e_service_task(struct work_struct *work)
11220 {
11221 	struct i40e_pf *pf = container_of(work,
11222 					  struct i40e_pf,
11223 					  service_task);
11224 	unsigned long start_time = jiffies;
11225 
11226 	/* don't bother with service tasks if a reset is in progress */
11227 	if (test_bit(__I40E_RESET_RECOVERY_PENDING, pf->state) ||
11228 	    test_bit(__I40E_SUSPENDED, pf->state))
11229 		return;
11230 
11231 	if (test_and_set_bit(__I40E_SERVICE_SCHED, pf->state))
11232 		return;
11233 
11234 	if (!test_bit(__I40E_RECOVERY_MODE, pf->state)) {
11235 		i40e_detect_recover_hung(pf->vsi[pf->lan_vsi]);
11236 		i40e_sync_filters_subtask(pf);
11237 		i40e_reset_subtask(pf);
11238 		i40e_handle_mdd_event(pf);
11239 		i40e_vc_process_vflr_event(pf);
11240 		i40e_watchdog_subtask(pf);
11241 		i40e_fdir_reinit_subtask(pf);
11242 		if (test_and_clear_bit(__I40E_CLIENT_RESET, pf->state)) {
11243 			/* Client subtask will reopen next time through. */
11244 			i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi],
11245 							   true);
11246 		} else {
11247 			i40e_client_subtask(pf);
11248 			if (test_and_clear_bit(__I40E_CLIENT_L2_CHANGE,
11249 					       pf->state))
11250 				i40e_notify_client_of_l2_param_changes(
11251 								pf->vsi[pf->lan_vsi]);
11252 		}
11253 		i40e_sync_filters_subtask(pf);
11254 	} else {
11255 		i40e_reset_subtask(pf);
11256 	}
11257 
11258 	i40e_clean_adminq_subtask(pf);
11259 
11260 	/* flush memory to make sure state is correct before next watchdog */
11261 	smp_mb__before_atomic();
11262 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
11263 
11264 	/* If the tasks have taken longer than one timer cycle or there
11265 	 * is more work to be done, reschedule the service task now
11266 	 * rather than wait for the timer to tick again.
11267 	 */
11268 	if (time_after(jiffies, (start_time + pf->service_timer_period)) ||
11269 	    test_bit(__I40E_ADMINQ_EVENT_PENDING, pf->state)		 ||
11270 	    test_bit(__I40E_MDD_EVENT_PENDING, pf->state)		 ||
11271 	    test_bit(__I40E_VFLR_EVENT_PENDING, pf->state))
11272 		i40e_service_event_schedule(pf);
11273 }
11274 
11275 /**
11276  * i40e_service_timer - timer callback
11277  * @t: timer list pointer
11278  **/
11279 static void i40e_service_timer(struct timer_list *t)
11280 {
11281 	struct i40e_pf *pf = from_timer(pf, t, service_timer);
11282 
11283 	mod_timer(&pf->service_timer,
11284 		  round_jiffies(jiffies + pf->service_timer_period));
11285 	i40e_service_event_schedule(pf);
11286 }
11287 
11288 /**
11289  * i40e_set_num_rings_in_vsi - Determine number of rings in the VSI
11290  * @vsi: the VSI being configured
11291  **/
11292 static int i40e_set_num_rings_in_vsi(struct i40e_vsi *vsi)
11293 {
11294 	struct i40e_pf *pf = vsi->back;
11295 
11296 	switch (vsi->type) {
11297 	case I40E_VSI_MAIN:
11298 		vsi->alloc_queue_pairs = pf->num_lan_qps;
11299 		if (!vsi->num_tx_desc)
11300 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11301 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11302 		if (!vsi->num_rx_desc)
11303 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11304 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11305 		if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11306 			vsi->num_q_vectors = pf->num_lan_msix;
11307 		else
11308 			vsi->num_q_vectors = 1;
11309 
11310 		break;
11311 
11312 	case I40E_VSI_FDIR:
11313 		vsi->alloc_queue_pairs = 1;
11314 		vsi->num_tx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11315 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11316 		vsi->num_rx_desc = ALIGN(I40E_FDIR_RING_COUNT,
11317 					 I40E_REQ_DESCRIPTOR_MULTIPLE);
11318 		vsi->num_q_vectors = pf->num_fdsb_msix;
11319 		break;
11320 
11321 	case I40E_VSI_VMDQ2:
11322 		vsi->alloc_queue_pairs = pf->num_vmdq_qps;
11323 		if (!vsi->num_tx_desc)
11324 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11325 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11326 		if (!vsi->num_rx_desc)
11327 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11328 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11329 		vsi->num_q_vectors = pf->num_vmdq_msix;
11330 		break;
11331 
11332 	case I40E_VSI_SRIOV:
11333 		vsi->alloc_queue_pairs = pf->num_vf_qps;
11334 		if (!vsi->num_tx_desc)
11335 			vsi->num_tx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11336 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11337 		if (!vsi->num_rx_desc)
11338 			vsi->num_rx_desc = ALIGN(I40E_DEFAULT_NUM_DESCRIPTORS,
11339 						 I40E_REQ_DESCRIPTOR_MULTIPLE);
11340 		break;
11341 
11342 	default:
11343 		WARN_ON(1);
11344 		return -ENODATA;
11345 	}
11346 
11347 	if (is_kdump_kernel()) {
11348 		vsi->num_tx_desc = I40E_MIN_NUM_DESCRIPTORS;
11349 		vsi->num_rx_desc = I40E_MIN_NUM_DESCRIPTORS;
11350 	}
11351 
11352 	return 0;
11353 }
11354 
11355 /**
11356  * i40e_vsi_alloc_arrays - Allocate queue and vector pointer arrays for the vsi
11357  * @vsi: VSI pointer
11358  * @alloc_qvectors: a bool to specify if q_vectors need to be allocated.
11359  *
11360  * On error: returns error code (negative)
11361  * On success: returns 0
11362  **/
11363 static int i40e_vsi_alloc_arrays(struct i40e_vsi *vsi, bool alloc_qvectors)
11364 {
11365 	struct i40e_ring **next_rings;
11366 	int size;
11367 	int ret = 0;
11368 
11369 	/* allocate memory for both Tx, XDP Tx and Rx ring pointers */
11370 	size = sizeof(struct i40e_ring *) * vsi->alloc_queue_pairs *
11371 	       (i40e_enabled_xdp_vsi(vsi) ? 3 : 2);
11372 	vsi->tx_rings = kzalloc(size, GFP_KERNEL);
11373 	if (!vsi->tx_rings)
11374 		return -ENOMEM;
11375 	next_rings = vsi->tx_rings + vsi->alloc_queue_pairs;
11376 	if (i40e_enabled_xdp_vsi(vsi)) {
11377 		vsi->xdp_rings = next_rings;
11378 		next_rings += vsi->alloc_queue_pairs;
11379 	}
11380 	vsi->rx_rings = next_rings;
11381 
11382 	if (alloc_qvectors) {
11383 		/* allocate memory for q_vector pointers */
11384 		size = sizeof(struct i40e_q_vector *) * vsi->num_q_vectors;
11385 		vsi->q_vectors = kzalloc(size, GFP_KERNEL);
11386 		if (!vsi->q_vectors) {
11387 			ret = -ENOMEM;
11388 			goto err_vectors;
11389 		}
11390 	}
11391 	return ret;
11392 
11393 err_vectors:
11394 	kfree(vsi->tx_rings);
11395 	return ret;
11396 }
11397 
11398 /**
11399  * i40e_vsi_mem_alloc - Allocates the next available struct vsi in the PF
11400  * @pf: board private structure
11401  * @type: type of VSI
11402  *
11403  * On error: returns error code (negative)
11404  * On success: returns vsi index in PF (positive)
11405  **/
11406 static int i40e_vsi_mem_alloc(struct i40e_pf *pf, enum i40e_vsi_type type)
11407 {
11408 	int ret = -ENODEV;
11409 	struct i40e_vsi *vsi;
11410 	int vsi_idx;
11411 	int i;
11412 
11413 	/* Need to protect the allocation of the VSIs at the PF level */
11414 	mutex_lock(&pf->switch_mutex);
11415 
11416 	/* VSI list may be fragmented if VSI creation/destruction has
11417 	 * been happening.  We can afford to do a quick scan to look
11418 	 * for any free VSIs in the list.
11419 	 *
11420 	 * find next empty vsi slot, looping back around if necessary
11421 	 */
11422 	i = pf->next_vsi;
11423 	while (i < pf->num_alloc_vsi && pf->vsi[i])
11424 		i++;
11425 	if (i >= pf->num_alloc_vsi) {
11426 		i = 0;
11427 		while (i < pf->next_vsi && pf->vsi[i])
11428 			i++;
11429 	}
11430 
11431 	if (i < pf->num_alloc_vsi && !pf->vsi[i]) {
11432 		vsi_idx = i;             /* Found one! */
11433 	} else {
11434 		ret = -ENODEV;
11435 		goto unlock_pf;  /* out of VSI slots! */
11436 	}
11437 	pf->next_vsi = ++i;
11438 
11439 	vsi = kzalloc(sizeof(*vsi), GFP_KERNEL);
11440 	if (!vsi) {
11441 		ret = -ENOMEM;
11442 		goto unlock_pf;
11443 	}
11444 	vsi->type = type;
11445 	vsi->back = pf;
11446 	set_bit(__I40E_VSI_DOWN, vsi->state);
11447 	vsi->flags = 0;
11448 	vsi->idx = vsi_idx;
11449 	vsi->int_rate_limit = 0;
11450 	vsi->rss_table_size = (vsi->type == I40E_VSI_MAIN) ?
11451 				pf->rss_table_size : 64;
11452 	vsi->netdev_registered = false;
11453 	vsi->work_limit = I40E_DEFAULT_IRQ_WORK;
11454 	hash_init(vsi->mac_filter_hash);
11455 	vsi->irqs_ready = false;
11456 
11457 	if (type == I40E_VSI_MAIN) {
11458 		vsi->af_xdp_zc_qps = bitmap_zalloc(pf->num_lan_qps, GFP_KERNEL);
11459 		if (!vsi->af_xdp_zc_qps)
11460 			goto err_rings;
11461 	}
11462 
11463 	ret = i40e_set_num_rings_in_vsi(vsi);
11464 	if (ret)
11465 		goto err_rings;
11466 
11467 	ret = i40e_vsi_alloc_arrays(vsi, true);
11468 	if (ret)
11469 		goto err_rings;
11470 
11471 	/* Setup default MSIX irq handler for VSI */
11472 	i40e_vsi_setup_irqhandler(vsi, i40e_msix_clean_rings);
11473 
11474 	/* Initialize VSI lock */
11475 	spin_lock_init(&vsi->mac_filter_hash_lock);
11476 	pf->vsi[vsi_idx] = vsi;
11477 	ret = vsi_idx;
11478 	goto unlock_pf;
11479 
11480 err_rings:
11481 	bitmap_free(vsi->af_xdp_zc_qps);
11482 	pf->next_vsi = i - 1;
11483 	kfree(vsi);
11484 unlock_pf:
11485 	mutex_unlock(&pf->switch_mutex);
11486 	return ret;
11487 }
11488 
11489 /**
11490  * i40e_vsi_free_arrays - Free queue and vector pointer arrays for the VSI
11491  * @vsi: VSI pointer
11492  * @free_qvectors: a bool to specify if q_vectors need to be freed.
11493  *
11494  * On error: returns error code (negative)
11495  * On success: returns 0
11496  **/
11497 static void i40e_vsi_free_arrays(struct i40e_vsi *vsi, bool free_qvectors)
11498 {
11499 	/* free the ring and vector containers */
11500 	if (free_qvectors) {
11501 		kfree(vsi->q_vectors);
11502 		vsi->q_vectors = NULL;
11503 	}
11504 	kfree(vsi->tx_rings);
11505 	vsi->tx_rings = NULL;
11506 	vsi->rx_rings = NULL;
11507 	vsi->xdp_rings = NULL;
11508 }
11509 
11510 /**
11511  * i40e_clear_rss_config_user - clear the user configured RSS hash keys
11512  * and lookup table
11513  * @vsi: Pointer to VSI structure
11514  */
11515 static void i40e_clear_rss_config_user(struct i40e_vsi *vsi)
11516 {
11517 	if (!vsi)
11518 		return;
11519 
11520 	kfree(vsi->rss_hkey_user);
11521 	vsi->rss_hkey_user = NULL;
11522 
11523 	kfree(vsi->rss_lut_user);
11524 	vsi->rss_lut_user = NULL;
11525 }
11526 
11527 /**
11528  * i40e_vsi_clear - Deallocate the VSI provided
11529  * @vsi: the VSI being un-configured
11530  **/
11531 static int i40e_vsi_clear(struct i40e_vsi *vsi)
11532 {
11533 	struct i40e_pf *pf;
11534 
11535 	if (!vsi)
11536 		return 0;
11537 
11538 	if (!vsi->back)
11539 		goto free_vsi;
11540 	pf = vsi->back;
11541 
11542 	mutex_lock(&pf->switch_mutex);
11543 	if (!pf->vsi[vsi->idx]) {
11544 		dev_err(&pf->pdev->dev, "pf->vsi[%d] is NULL, just free vsi[%d](type %d)\n",
11545 			vsi->idx, vsi->idx, vsi->type);
11546 		goto unlock_vsi;
11547 	}
11548 
11549 	if (pf->vsi[vsi->idx] != vsi) {
11550 		dev_err(&pf->pdev->dev,
11551 			"pf->vsi[%d](type %d) != vsi[%d](type %d): no free!\n",
11552 			pf->vsi[vsi->idx]->idx,
11553 			pf->vsi[vsi->idx]->type,
11554 			vsi->idx, vsi->type);
11555 		goto unlock_vsi;
11556 	}
11557 
11558 	/* updates the PF for this cleared vsi */
11559 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
11560 	i40e_put_lump(pf->irq_pile, vsi->base_vector, vsi->idx);
11561 
11562 	bitmap_free(vsi->af_xdp_zc_qps);
11563 	i40e_vsi_free_arrays(vsi, true);
11564 	i40e_clear_rss_config_user(vsi);
11565 
11566 	pf->vsi[vsi->idx] = NULL;
11567 	if (vsi->idx < pf->next_vsi)
11568 		pf->next_vsi = vsi->idx;
11569 
11570 unlock_vsi:
11571 	mutex_unlock(&pf->switch_mutex);
11572 free_vsi:
11573 	kfree(vsi);
11574 
11575 	return 0;
11576 }
11577 
11578 /**
11579  * i40e_vsi_clear_rings - Deallocates the Rx and Tx rings for the provided VSI
11580  * @vsi: the VSI being cleaned
11581  **/
11582 static void i40e_vsi_clear_rings(struct i40e_vsi *vsi)
11583 {
11584 	int i;
11585 
11586 	if (vsi->tx_rings && vsi->tx_rings[0]) {
11587 		for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11588 			kfree_rcu(vsi->tx_rings[i], rcu);
11589 			WRITE_ONCE(vsi->tx_rings[i], NULL);
11590 			WRITE_ONCE(vsi->rx_rings[i], NULL);
11591 			if (vsi->xdp_rings)
11592 				WRITE_ONCE(vsi->xdp_rings[i], NULL);
11593 		}
11594 	}
11595 }
11596 
11597 /**
11598  * i40e_alloc_rings - Allocates the Rx and Tx rings for the provided VSI
11599  * @vsi: the VSI being configured
11600  **/
11601 static int i40e_alloc_rings(struct i40e_vsi *vsi)
11602 {
11603 	int i, qpv = i40e_enabled_xdp_vsi(vsi) ? 3 : 2;
11604 	struct i40e_pf *pf = vsi->back;
11605 	struct i40e_ring *ring;
11606 
11607 	/* Set basic values in the rings to be used later during open() */
11608 	for (i = 0; i < vsi->alloc_queue_pairs; i++) {
11609 		/* allocate space for both Tx and Rx in one shot */
11610 		ring = kcalloc(qpv, sizeof(struct i40e_ring), GFP_KERNEL);
11611 		if (!ring)
11612 			goto err_out;
11613 
11614 		ring->queue_index = i;
11615 		ring->reg_idx = vsi->base_queue + i;
11616 		ring->ring_active = false;
11617 		ring->vsi = vsi;
11618 		ring->netdev = vsi->netdev;
11619 		ring->dev = &pf->pdev->dev;
11620 		ring->count = vsi->num_tx_desc;
11621 		ring->size = 0;
11622 		ring->dcb_tc = 0;
11623 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11624 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11625 		ring->itr_setting = pf->tx_itr_default;
11626 		WRITE_ONCE(vsi->tx_rings[i], ring++);
11627 
11628 		if (!i40e_enabled_xdp_vsi(vsi))
11629 			goto setup_rx;
11630 
11631 		ring->queue_index = vsi->alloc_queue_pairs + i;
11632 		ring->reg_idx = vsi->base_queue + ring->queue_index;
11633 		ring->ring_active = false;
11634 		ring->vsi = vsi;
11635 		ring->netdev = NULL;
11636 		ring->dev = &pf->pdev->dev;
11637 		ring->count = vsi->num_tx_desc;
11638 		ring->size = 0;
11639 		ring->dcb_tc = 0;
11640 		if (vsi->back->hw_features & I40E_HW_WB_ON_ITR_CAPABLE)
11641 			ring->flags = I40E_TXR_FLAGS_WB_ON_ITR;
11642 		set_ring_xdp(ring);
11643 		ring->itr_setting = pf->tx_itr_default;
11644 		WRITE_ONCE(vsi->xdp_rings[i], ring++);
11645 
11646 setup_rx:
11647 		ring->queue_index = i;
11648 		ring->reg_idx = vsi->base_queue + i;
11649 		ring->ring_active = false;
11650 		ring->vsi = vsi;
11651 		ring->netdev = vsi->netdev;
11652 		ring->dev = &pf->pdev->dev;
11653 		ring->count = vsi->num_rx_desc;
11654 		ring->size = 0;
11655 		ring->dcb_tc = 0;
11656 		ring->itr_setting = pf->rx_itr_default;
11657 		WRITE_ONCE(vsi->rx_rings[i], ring);
11658 	}
11659 
11660 	return 0;
11661 
11662 err_out:
11663 	i40e_vsi_clear_rings(vsi);
11664 	return -ENOMEM;
11665 }
11666 
11667 /**
11668  * i40e_reserve_msix_vectors - Reserve MSI-X vectors in the kernel
11669  * @pf: board private structure
11670  * @vectors: the number of MSI-X vectors to request
11671  *
11672  * Returns the number of vectors reserved, or error
11673  **/
11674 static int i40e_reserve_msix_vectors(struct i40e_pf *pf, int vectors)
11675 {
11676 	vectors = pci_enable_msix_range(pf->pdev, pf->msix_entries,
11677 					I40E_MIN_MSIX, vectors);
11678 	if (vectors < 0) {
11679 		dev_info(&pf->pdev->dev,
11680 			 "MSI-X vector reservation failed: %d\n", vectors);
11681 		vectors = 0;
11682 	}
11683 
11684 	return vectors;
11685 }
11686 
11687 /**
11688  * i40e_init_msix - Setup the MSIX capability
11689  * @pf: board private structure
11690  *
11691  * Work with the OS to set up the MSIX vectors needed.
11692  *
11693  * Returns the number of vectors reserved or negative on failure
11694  **/
11695 static int i40e_init_msix(struct i40e_pf *pf)
11696 {
11697 	struct i40e_hw *hw = &pf->hw;
11698 	int cpus, extra_vectors;
11699 	int vectors_left;
11700 	int v_budget, i;
11701 	int v_actual;
11702 	int iwarp_requested = 0;
11703 
11704 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
11705 		return -ENODEV;
11706 
11707 	/* The number of vectors we'll request will be comprised of:
11708 	 *   - Add 1 for "other" cause for Admin Queue events, etc.
11709 	 *   - The number of LAN queue pairs
11710 	 *	- Queues being used for RSS.
11711 	 *		We don't need as many as max_rss_size vectors.
11712 	 *		use rss_size instead in the calculation since that
11713 	 *		is governed by number of cpus in the system.
11714 	 *	- assumes symmetric Tx/Rx pairing
11715 	 *   - The number of VMDq pairs
11716 	 *   - The CPU count within the NUMA node if iWARP is enabled
11717 	 * Once we count this up, try the request.
11718 	 *
11719 	 * If we can't get what we want, we'll simplify to nearly nothing
11720 	 * and try again.  If that still fails, we punt.
11721 	 */
11722 	vectors_left = hw->func_caps.num_msix_vectors;
11723 	v_budget = 0;
11724 
11725 	/* reserve one vector for miscellaneous handler */
11726 	if (vectors_left) {
11727 		v_budget++;
11728 		vectors_left--;
11729 	}
11730 
11731 	/* reserve some vectors for the main PF traffic queues. Initially we
11732 	 * only reserve at most 50% of the available vectors, in the case that
11733 	 * the number of online CPUs is large. This ensures that we can enable
11734 	 * extra features as well. Once we've enabled the other features, we
11735 	 * will use any remaining vectors to reach as close as we can to the
11736 	 * number of online CPUs.
11737 	 */
11738 	cpus = num_online_cpus();
11739 	pf->num_lan_msix = min_t(int, cpus, vectors_left / 2);
11740 	vectors_left -= pf->num_lan_msix;
11741 
11742 	/* reserve one vector for sideband flow director */
11743 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11744 		if (vectors_left) {
11745 			pf->num_fdsb_msix = 1;
11746 			v_budget++;
11747 			vectors_left--;
11748 		} else {
11749 			pf->num_fdsb_msix = 0;
11750 		}
11751 	}
11752 
11753 	/* can we reserve enough for iWARP? */
11754 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11755 		iwarp_requested = pf->num_iwarp_msix;
11756 
11757 		if (!vectors_left)
11758 			pf->num_iwarp_msix = 0;
11759 		else if (vectors_left < pf->num_iwarp_msix)
11760 			pf->num_iwarp_msix = 1;
11761 		v_budget += pf->num_iwarp_msix;
11762 		vectors_left -= pf->num_iwarp_msix;
11763 	}
11764 
11765 	/* any vectors left over go for VMDq support */
11766 	if (pf->flags & I40E_FLAG_VMDQ_ENABLED) {
11767 		if (!vectors_left) {
11768 			pf->num_vmdq_msix = 0;
11769 			pf->num_vmdq_qps = 0;
11770 		} else {
11771 			int vmdq_vecs_wanted =
11772 				pf->num_vmdq_vsis * pf->num_vmdq_qps;
11773 			int vmdq_vecs =
11774 				min_t(int, vectors_left, vmdq_vecs_wanted);
11775 
11776 			/* if we're short on vectors for what's desired, we limit
11777 			 * the queues per vmdq.  If this is still more than are
11778 			 * available, the user will need to change the number of
11779 			 * queues/vectors used by the PF later with the ethtool
11780 			 * channels command
11781 			 */
11782 			if (vectors_left < vmdq_vecs_wanted) {
11783 				pf->num_vmdq_qps = 1;
11784 				vmdq_vecs_wanted = pf->num_vmdq_vsis;
11785 				vmdq_vecs = min_t(int,
11786 						  vectors_left,
11787 						  vmdq_vecs_wanted);
11788 			}
11789 			pf->num_vmdq_msix = pf->num_vmdq_qps;
11790 
11791 			v_budget += vmdq_vecs;
11792 			vectors_left -= vmdq_vecs;
11793 		}
11794 	}
11795 
11796 	/* On systems with a large number of SMP cores, we previously limited
11797 	 * the number of vectors for num_lan_msix to be at most 50% of the
11798 	 * available vectors, to allow for other features. Now, we add back
11799 	 * the remaining vectors. However, we ensure that the total
11800 	 * num_lan_msix will not exceed num_online_cpus(). To do this, we
11801 	 * calculate the number of vectors we can add without going over the
11802 	 * cap of CPUs. For systems with a small number of CPUs this will be
11803 	 * zero.
11804 	 */
11805 	extra_vectors = min_t(int, cpus - pf->num_lan_msix, vectors_left);
11806 	pf->num_lan_msix += extra_vectors;
11807 	vectors_left -= extra_vectors;
11808 
11809 	WARN(vectors_left < 0,
11810 	     "Calculation of remaining vectors underflowed. This is an accounting bug when determining total MSI-X vectors.\n");
11811 
11812 	v_budget += pf->num_lan_msix;
11813 	pf->msix_entries = kcalloc(v_budget, sizeof(struct msix_entry),
11814 				   GFP_KERNEL);
11815 	if (!pf->msix_entries)
11816 		return -ENOMEM;
11817 
11818 	for (i = 0; i < v_budget; i++)
11819 		pf->msix_entries[i].entry = i;
11820 	v_actual = i40e_reserve_msix_vectors(pf, v_budget);
11821 
11822 	if (v_actual < I40E_MIN_MSIX) {
11823 		pf->flags &= ~I40E_FLAG_MSIX_ENABLED;
11824 		kfree(pf->msix_entries);
11825 		pf->msix_entries = NULL;
11826 		pci_disable_msix(pf->pdev);
11827 		return -ENODEV;
11828 
11829 	} else if (v_actual == I40E_MIN_MSIX) {
11830 		/* Adjust for minimal MSIX use */
11831 		pf->num_vmdq_vsis = 0;
11832 		pf->num_vmdq_qps = 0;
11833 		pf->num_lan_qps = 1;
11834 		pf->num_lan_msix = 1;
11835 
11836 	} else if (v_actual != v_budget) {
11837 		/* If we have limited resources, we will start with no vectors
11838 		 * for the special features and then allocate vectors to some
11839 		 * of these features based on the policy and at the end disable
11840 		 * the features that did not get any vectors.
11841 		 */
11842 		int vec;
11843 
11844 		dev_info(&pf->pdev->dev,
11845 			 "MSI-X vector limit reached with %d, wanted %d, attempting to redistribute vectors\n",
11846 			 v_actual, v_budget);
11847 		/* reserve the misc vector */
11848 		vec = v_actual - 1;
11849 
11850 		/* Scale vector usage down */
11851 		pf->num_vmdq_msix = 1;    /* force VMDqs to only one vector */
11852 		pf->num_vmdq_vsis = 1;
11853 		pf->num_vmdq_qps = 1;
11854 
11855 		/* partition out the remaining vectors */
11856 		switch (vec) {
11857 		case 2:
11858 			pf->num_lan_msix = 1;
11859 			break;
11860 		case 3:
11861 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11862 				pf->num_lan_msix = 1;
11863 				pf->num_iwarp_msix = 1;
11864 			} else {
11865 				pf->num_lan_msix = 2;
11866 			}
11867 			break;
11868 		default:
11869 			if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
11870 				pf->num_iwarp_msix = min_t(int, (vec / 3),
11871 						 iwarp_requested);
11872 				pf->num_vmdq_vsis = min_t(int, (vec / 3),
11873 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11874 			} else {
11875 				pf->num_vmdq_vsis = min_t(int, (vec / 2),
11876 						  I40E_DEFAULT_NUM_VMDQ_VSI);
11877 			}
11878 			if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
11879 				pf->num_fdsb_msix = 1;
11880 				vec--;
11881 			}
11882 			pf->num_lan_msix = min_t(int,
11883 			       (vec - (pf->num_iwarp_msix + pf->num_vmdq_vsis)),
11884 							      pf->num_lan_msix);
11885 			pf->num_lan_qps = pf->num_lan_msix;
11886 			break;
11887 		}
11888 	}
11889 
11890 	if ((pf->flags & I40E_FLAG_FD_SB_ENABLED) &&
11891 	    (pf->num_fdsb_msix == 0)) {
11892 		dev_info(&pf->pdev->dev, "Sideband Flowdir disabled, not enough MSI-X vectors\n");
11893 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
11894 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
11895 	}
11896 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
11897 	    (pf->num_vmdq_msix == 0)) {
11898 		dev_info(&pf->pdev->dev, "VMDq disabled, not enough MSI-X vectors\n");
11899 		pf->flags &= ~I40E_FLAG_VMDQ_ENABLED;
11900 	}
11901 
11902 	if ((pf->flags & I40E_FLAG_IWARP_ENABLED) &&
11903 	    (pf->num_iwarp_msix == 0)) {
11904 		dev_info(&pf->pdev->dev, "IWARP disabled, not enough MSI-X vectors\n");
11905 		pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
11906 	}
11907 	i40e_debug(&pf->hw, I40E_DEBUG_INIT,
11908 		   "MSI-X vector distribution: PF %d, VMDq %d, FDSB %d, iWARP %d\n",
11909 		   pf->num_lan_msix,
11910 		   pf->num_vmdq_msix * pf->num_vmdq_vsis,
11911 		   pf->num_fdsb_msix,
11912 		   pf->num_iwarp_msix);
11913 
11914 	return v_actual;
11915 }
11916 
11917 /**
11918  * i40e_vsi_alloc_q_vector - Allocate memory for a single interrupt vector
11919  * @vsi: the VSI being configured
11920  * @v_idx: index of the vector in the vsi struct
11921  *
11922  * We allocate one q_vector.  If allocation fails we return -ENOMEM.
11923  **/
11924 static int i40e_vsi_alloc_q_vector(struct i40e_vsi *vsi, int v_idx)
11925 {
11926 	struct i40e_q_vector *q_vector;
11927 
11928 	/* allocate q_vector */
11929 	q_vector = kzalloc(sizeof(struct i40e_q_vector), GFP_KERNEL);
11930 	if (!q_vector)
11931 		return -ENOMEM;
11932 
11933 	q_vector->vsi = vsi;
11934 	q_vector->v_idx = v_idx;
11935 	cpumask_copy(&q_vector->affinity_mask, cpu_possible_mask);
11936 
11937 	if (vsi->netdev)
11938 		netif_napi_add(vsi->netdev, &q_vector->napi, i40e_napi_poll);
11939 
11940 	/* tie q_vector and vsi together */
11941 	vsi->q_vectors[v_idx] = q_vector;
11942 
11943 	return 0;
11944 }
11945 
11946 /**
11947  * i40e_vsi_alloc_q_vectors - Allocate memory for interrupt vectors
11948  * @vsi: the VSI being configured
11949  *
11950  * We allocate one q_vector per queue interrupt.  If allocation fails we
11951  * return -ENOMEM.
11952  **/
11953 static int i40e_vsi_alloc_q_vectors(struct i40e_vsi *vsi)
11954 {
11955 	struct i40e_pf *pf = vsi->back;
11956 	int err, v_idx, num_q_vectors;
11957 
11958 	/* if not MSIX, give the one vector only to the LAN VSI */
11959 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
11960 		num_q_vectors = vsi->num_q_vectors;
11961 	else if (vsi == pf->vsi[pf->lan_vsi])
11962 		num_q_vectors = 1;
11963 	else
11964 		return -EINVAL;
11965 
11966 	for (v_idx = 0; v_idx < num_q_vectors; v_idx++) {
11967 		err = i40e_vsi_alloc_q_vector(vsi, v_idx);
11968 		if (err)
11969 			goto err_out;
11970 	}
11971 
11972 	return 0;
11973 
11974 err_out:
11975 	while (v_idx--)
11976 		i40e_free_q_vector(vsi, v_idx);
11977 
11978 	return err;
11979 }
11980 
11981 /**
11982  * i40e_init_interrupt_scheme - Determine proper interrupt scheme
11983  * @pf: board private structure to initialize
11984  **/
11985 static int i40e_init_interrupt_scheme(struct i40e_pf *pf)
11986 {
11987 	int vectors = 0;
11988 	ssize_t size;
11989 
11990 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
11991 		vectors = i40e_init_msix(pf);
11992 		if (vectors < 0) {
11993 			pf->flags &= ~(I40E_FLAG_MSIX_ENABLED	|
11994 				       I40E_FLAG_IWARP_ENABLED	|
11995 				       I40E_FLAG_RSS_ENABLED	|
11996 				       I40E_FLAG_DCB_CAPABLE	|
11997 				       I40E_FLAG_DCB_ENABLED	|
11998 				       I40E_FLAG_SRIOV_ENABLED	|
11999 				       I40E_FLAG_FD_SB_ENABLED	|
12000 				       I40E_FLAG_FD_ATR_ENABLED	|
12001 				       I40E_FLAG_VMDQ_ENABLED);
12002 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12003 
12004 			/* rework the queue expectations without MSIX */
12005 			i40e_determine_queue_usage(pf);
12006 		}
12007 	}
12008 
12009 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED) &&
12010 	    (pf->flags & I40E_FLAG_MSI_ENABLED)) {
12011 		dev_info(&pf->pdev->dev, "MSI-X not available, trying MSI\n");
12012 		vectors = pci_enable_msi(pf->pdev);
12013 		if (vectors < 0) {
12014 			dev_info(&pf->pdev->dev, "MSI init failed - %d\n",
12015 				 vectors);
12016 			pf->flags &= ~I40E_FLAG_MSI_ENABLED;
12017 		}
12018 		vectors = 1;  /* one MSI or Legacy vector */
12019 	}
12020 
12021 	if (!(pf->flags & (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED)))
12022 		dev_info(&pf->pdev->dev, "MSI-X and MSI not available, falling back to Legacy IRQ\n");
12023 
12024 	/* set up vector assignment tracking */
12025 	size = sizeof(struct i40e_lump_tracking) + (sizeof(u16) * vectors);
12026 	pf->irq_pile = kzalloc(size, GFP_KERNEL);
12027 	if (!pf->irq_pile)
12028 		return -ENOMEM;
12029 
12030 	pf->irq_pile->num_entries = vectors;
12031 
12032 	/* track first vector for misc interrupts, ignore return */
12033 	(void)i40e_get_lump(pf, pf->irq_pile, 1, I40E_PILE_VALID_BIT - 1);
12034 
12035 	return 0;
12036 }
12037 
12038 /**
12039  * i40e_restore_interrupt_scheme - Restore the interrupt scheme
12040  * @pf: private board data structure
12041  *
12042  * Restore the interrupt scheme that was cleared when we suspended the
12043  * device. This should be called during resume to re-allocate the q_vectors
12044  * and reacquire IRQs.
12045  */
12046 static int i40e_restore_interrupt_scheme(struct i40e_pf *pf)
12047 {
12048 	int err, i;
12049 
12050 	/* We cleared the MSI and MSI-X flags when disabling the old interrupt
12051 	 * scheme. We need to re-enabled them here in order to attempt to
12052 	 * re-acquire the MSI or MSI-X vectors
12053 	 */
12054 	pf->flags |= (I40E_FLAG_MSIX_ENABLED | I40E_FLAG_MSI_ENABLED);
12055 
12056 	err = i40e_init_interrupt_scheme(pf);
12057 	if (err)
12058 		return err;
12059 
12060 	/* Now that we've re-acquired IRQs, we need to remap the vectors and
12061 	 * rings together again.
12062 	 */
12063 	for (i = 0; i < pf->num_alloc_vsi; i++) {
12064 		if (pf->vsi[i]) {
12065 			err = i40e_vsi_alloc_q_vectors(pf->vsi[i]);
12066 			if (err)
12067 				goto err_unwind;
12068 			i40e_vsi_map_rings_to_vectors(pf->vsi[i]);
12069 		}
12070 	}
12071 
12072 	err = i40e_setup_misc_vector(pf);
12073 	if (err)
12074 		goto err_unwind;
12075 
12076 	if (pf->flags & I40E_FLAG_IWARP_ENABLED)
12077 		i40e_client_update_msix_info(pf);
12078 
12079 	return 0;
12080 
12081 err_unwind:
12082 	while (i--) {
12083 		if (pf->vsi[i])
12084 			i40e_vsi_free_q_vectors(pf->vsi[i]);
12085 	}
12086 
12087 	return err;
12088 }
12089 
12090 /**
12091  * i40e_setup_misc_vector_for_recovery_mode - Setup the misc vector to handle
12092  * non queue events in recovery mode
12093  * @pf: board private structure
12094  *
12095  * This sets up the handler for MSIX 0 or MSI/legacy, which is used to manage
12096  * the non-queue interrupts, e.g. AdminQ and errors in recovery mode.
12097  * This is handled differently than in recovery mode since no Tx/Rx resources
12098  * are being allocated.
12099  **/
12100 static int i40e_setup_misc_vector_for_recovery_mode(struct i40e_pf *pf)
12101 {
12102 	int err;
12103 
12104 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
12105 		err = i40e_setup_misc_vector(pf);
12106 
12107 		if (err) {
12108 			dev_info(&pf->pdev->dev,
12109 				 "MSI-X misc vector request failed, error %d\n",
12110 				 err);
12111 			return err;
12112 		}
12113 	} else {
12114 		u32 flags = pf->flags & I40E_FLAG_MSI_ENABLED ? 0 : IRQF_SHARED;
12115 
12116 		err = request_irq(pf->pdev->irq, i40e_intr, flags,
12117 				  pf->int_name, pf);
12118 
12119 		if (err) {
12120 			dev_info(&pf->pdev->dev,
12121 				 "MSI/legacy misc vector request failed, error %d\n",
12122 				 err);
12123 			return err;
12124 		}
12125 		i40e_enable_misc_int_causes(pf);
12126 		i40e_irq_dynamic_enable_icr0(pf);
12127 	}
12128 
12129 	return 0;
12130 }
12131 
12132 /**
12133  * i40e_setup_misc_vector - Setup the misc vector to handle non queue events
12134  * @pf: board private structure
12135  *
12136  * This sets up the handler for MSIX 0, which is used to manage the
12137  * non-queue interrupts, e.g. AdminQ and errors.  This is not used
12138  * when in MSI or Legacy interrupt mode.
12139  **/
12140 static int i40e_setup_misc_vector(struct i40e_pf *pf)
12141 {
12142 	struct i40e_hw *hw = &pf->hw;
12143 	int err = 0;
12144 
12145 	/* Only request the IRQ once, the first time through. */
12146 	if (!test_and_set_bit(__I40E_MISC_IRQ_REQUESTED, pf->state)) {
12147 		err = request_irq(pf->msix_entries[0].vector,
12148 				  i40e_intr, 0, pf->int_name, pf);
12149 		if (err) {
12150 			clear_bit(__I40E_MISC_IRQ_REQUESTED, pf->state);
12151 			dev_info(&pf->pdev->dev,
12152 				 "request_irq for %s failed: %d\n",
12153 				 pf->int_name, err);
12154 			return -EFAULT;
12155 		}
12156 	}
12157 
12158 	i40e_enable_misc_int_causes(pf);
12159 
12160 	/* associate no queues to the misc vector */
12161 	wr32(hw, I40E_PFINT_LNKLST0, I40E_QUEUE_END_OF_LIST);
12162 	wr32(hw, I40E_PFINT_ITR0(I40E_RX_ITR), I40E_ITR_8K >> 1);
12163 
12164 	i40e_flush(hw);
12165 
12166 	i40e_irq_dynamic_enable_icr0(pf);
12167 
12168 	return err;
12169 }
12170 
12171 /**
12172  * i40e_get_rss_aq - Get RSS keys and lut by using AQ commands
12173  * @vsi: Pointer to vsi structure
12174  * @seed: Buffter to store the hash keys
12175  * @lut: Buffer to store the lookup table entries
12176  * @lut_size: Size of buffer to store the lookup table entries
12177  *
12178  * Return 0 on success, negative on failure
12179  */
12180 static int i40e_get_rss_aq(struct i40e_vsi *vsi, const u8 *seed,
12181 			   u8 *lut, u16 lut_size)
12182 {
12183 	struct i40e_pf *pf = vsi->back;
12184 	struct i40e_hw *hw = &pf->hw;
12185 	int ret = 0;
12186 
12187 	if (seed) {
12188 		ret = i40e_aq_get_rss_key(hw, vsi->id,
12189 			(struct i40e_aqc_get_set_rss_key_data *)seed);
12190 		if (ret) {
12191 			dev_info(&pf->pdev->dev,
12192 				 "Cannot get RSS key, err %s aq_err %s\n",
12193 				 i40e_stat_str(&pf->hw, ret),
12194 				 i40e_aq_str(&pf->hw,
12195 					     pf->hw.aq.asq_last_status));
12196 			return ret;
12197 		}
12198 	}
12199 
12200 	if (lut) {
12201 		bool pf_lut = vsi->type == I40E_VSI_MAIN;
12202 
12203 		ret = i40e_aq_get_rss_lut(hw, vsi->id, pf_lut, lut, lut_size);
12204 		if (ret) {
12205 			dev_info(&pf->pdev->dev,
12206 				 "Cannot get RSS lut, err %s aq_err %s\n",
12207 				 i40e_stat_str(&pf->hw, ret),
12208 				 i40e_aq_str(&pf->hw,
12209 					     pf->hw.aq.asq_last_status));
12210 			return ret;
12211 		}
12212 	}
12213 
12214 	return ret;
12215 }
12216 
12217 /**
12218  * i40e_config_rss_reg - Configure RSS keys and lut by writing registers
12219  * @vsi: Pointer to vsi structure
12220  * @seed: RSS hash seed
12221  * @lut: Lookup table
12222  * @lut_size: Lookup table size
12223  *
12224  * Returns 0 on success, negative on failure
12225  **/
12226 static int i40e_config_rss_reg(struct i40e_vsi *vsi, const u8 *seed,
12227 			       const u8 *lut, u16 lut_size)
12228 {
12229 	struct i40e_pf *pf = vsi->back;
12230 	struct i40e_hw *hw = &pf->hw;
12231 	u16 vf_id = vsi->vf_id;
12232 	u8 i;
12233 
12234 	/* Fill out hash function seed */
12235 	if (seed) {
12236 		u32 *seed_dw = (u32 *)seed;
12237 
12238 		if (vsi->type == I40E_VSI_MAIN) {
12239 			for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12240 				wr32(hw, I40E_PFQF_HKEY(i), seed_dw[i]);
12241 		} else if (vsi->type == I40E_VSI_SRIOV) {
12242 			for (i = 0; i <= I40E_VFQF_HKEY1_MAX_INDEX; i++)
12243 				wr32(hw, I40E_VFQF_HKEY1(i, vf_id), seed_dw[i]);
12244 		} else {
12245 			dev_err(&pf->pdev->dev, "Cannot set RSS seed - invalid VSI type\n");
12246 		}
12247 	}
12248 
12249 	if (lut) {
12250 		u32 *lut_dw = (u32 *)lut;
12251 
12252 		if (vsi->type == I40E_VSI_MAIN) {
12253 			if (lut_size != I40E_HLUT_ARRAY_SIZE)
12254 				return -EINVAL;
12255 			for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12256 				wr32(hw, I40E_PFQF_HLUT(i), lut_dw[i]);
12257 		} else if (vsi->type == I40E_VSI_SRIOV) {
12258 			if (lut_size != I40E_VF_HLUT_ARRAY_SIZE)
12259 				return -EINVAL;
12260 			for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12261 				wr32(hw, I40E_VFQF_HLUT1(i, vf_id), lut_dw[i]);
12262 		} else {
12263 			dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12264 		}
12265 	}
12266 	i40e_flush(hw);
12267 
12268 	return 0;
12269 }
12270 
12271 /**
12272  * i40e_get_rss_reg - Get the RSS keys and lut by reading registers
12273  * @vsi: Pointer to VSI structure
12274  * @seed: Buffer to store the keys
12275  * @lut: Buffer to store the lookup table entries
12276  * @lut_size: Size of buffer to store the lookup table entries
12277  *
12278  * Returns 0 on success, negative on failure
12279  */
12280 static int i40e_get_rss_reg(struct i40e_vsi *vsi, u8 *seed,
12281 			    u8 *lut, u16 lut_size)
12282 {
12283 	struct i40e_pf *pf = vsi->back;
12284 	struct i40e_hw *hw = &pf->hw;
12285 	u16 i;
12286 
12287 	if (seed) {
12288 		u32 *seed_dw = (u32 *)seed;
12289 
12290 		for (i = 0; i <= I40E_PFQF_HKEY_MAX_INDEX; i++)
12291 			seed_dw[i] = i40e_read_rx_ctl(hw, I40E_PFQF_HKEY(i));
12292 	}
12293 	if (lut) {
12294 		u32 *lut_dw = (u32 *)lut;
12295 
12296 		if (lut_size != I40E_HLUT_ARRAY_SIZE)
12297 			return -EINVAL;
12298 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12299 			lut_dw[i] = rd32(hw, I40E_PFQF_HLUT(i));
12300 	}
12301 
12302 	return 0;
12303 }
12304 
12305 /**
12306  * i40e_config_rss - Configure RSS keys and lut
12307  * @vsi: Pointer to VSI structure
12308  * @seed: RSS hash seed
12309  * @lut: Lookup table
12310  * @lut_size: Lookup table size
12311  *
12312  * Returns 0 on success, negative on failure
12313  */
12314 int i40e_config_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12315 {
12316 	struct i40e_pf *pf = vsi->back;
12317 
12318 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12319 		return i40e_config_rss_aq(vsi, seed, lut, lut_size);
12320 	else
12321 		return i40e_config_rss_reg(vsi, seed, lut, lut_size);
12322 }
12323 
12324 /**
12325  * i40e_get_rss - Get RSS keys and lut
12326  * @vsi: Pointer to VSI structure
12327  * @seed: Buffer to store the keys
12328  * @lut: Buffer to store the lookup table entries
12329  * @lut_size: Size of buffer to store the lookup table entries
12330  *
12331  * Returns 0 on success, negative on failure
12332  */
12333 int i40e_get_rss(struct i40e_vsi *vsi, u8 *seed, u8 *lut, u16 lut_size)
12334 {
12335 	struct i40e_pf *pf = vsi->back;
12336 
12337 	if (pf->hw_features & I40E_HW_RSS_AQ_CAPABLE)
12338 		return i40e_get_rss_aq(vsi, seed, lut, lut_size);
12339 	else
12340 		return i40e_get_rss_reg(vsi, seed, lut, lut_size);
12341 }
12342 
12343 /**
12344  * i40e_fill_rss_lut - Fill the RSS lookup table with default values
12345  * @pf: Pointer to board private structure
12346  * @lut: Lookup table
12347  * @rss_table_size: Lookup table size
12348  * @rss_size: Range of queue number for hashing
12349  */
12350 void i40e_fill_rss_lut(struct i40e_pf *pf, u8 *lut,
12351 		       u16 rss_table_size, u16 rss_size)
12352 {
12353 	u16 i;
12354 
12355 	for (i = 0; i < rss_table_size; i++)
12356 		lut[i] = i % rss_size;
12357 }
12358 
12359 /**
12360  * i40e_pf_config_rss - Prepare for RSS if used
12361  * @pf: board private structure
12362  **/
12363 static int i40e_pf_config_rss(struct i40e_pf *pf)
12364 {
12365 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12366 	u8 seed[I40E_HKEY_ARRAY_SIZE];
12367 	u8 *lut;
12368 	struct i40e_hw *hw = &pf->hw;
12369 	u32 reg_val;
12370 	u64 hena;
12371 	int ret;
12372 
12373 	/* By default we enable TCP/UDP with IPv4/IPv6 ptypes */
12374 	hena = (u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(0)) |
12375 		((u64)i40e_read_rx_ctl(hw, I40E_PFQF_HENA(1)) << 32);
12376 	hena |= i40e_pf_get_default_rss_hena(pf);
12377 
12378 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), (u32)hena);
12379 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), (u32)(hena >> 32));
12380 
12381 	/* Determine the RSS table size based on the hardware capabilities */
12382 	reg_val = i40e_read_rx_ctl(hw, I40E_PFQF_CTL_0);
12383 	reg_val = (pf->rss_table_size == 512) ?
12384 			(reg_val | I40E_PFQF_CTL_0_HASHLUTSIZE_512) :
12385 			(reg_val & ~I40E_PFQF_CTL_0_HASHLUTSIZE_512);
12386 	i40e_write_rx_ctl(hw, I40E_PFQF_CTL_0, reg_val);
12387 
12388 	/* Determine the RSS size of the VSI */
12389 	if (!vsi->rss_size) {
12390 		u16 qcount;
12391 		/* If the firmware does something weird during VSI init, we
12392 		 * could end up with zero TCs. Check for that to avoid
12393 		 * divide-by-zero. It probably won't pass traffic, but it also
12394 		 * won't panic.
12395 		 */
12396 		qcount = vsi->num_queue_pairs /
12397 			 (vsi->tc_config.numtc ? vsi->tc_config.numtc : 1);
12398 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12399 	}
12400 	if (!vsi->rss_size)
12401 		return -EINVAL;
12402 
12403 	lut = kzalloc(vsi->rss_table_size, GFP_KERNEL);
12404 	if (!lut)
12405 		return -ENOMEM;
12406 
12407 	/* Use user configured lut if there is one, otherwise use default */
12408 	if (vsi->rss_lut_user)
12409 		memcpy(lut, vsi->rss_lut_user, vsi->rss_table_size);
12410 	else
12411 		i40e_fill_rss_lut(pf, lut, vsi->rss_table_size, vsi->rss_size);
12412 
12413 	/* Use user configured hash key if there is one, otherwise
12414 	 * use default.
12415 	 */
12416 	if (vsi->rss_hkey_user)
12417 		memcpy(seed, vsi->rss_hkey_user, I40E_HKEY_ARRAY_SIZE);
12418 	else
12419 		netdev_rss_key_fill((void *)seed, I40E_HKEY_ARRAY_SIZE);
12420 	ret = i40e_config_rss(vsi, seed, lut, vsi->rss_table_size);
12421 	kfree(lut);
12422 
12423 	return ret;
12424 }
12425 
12426 /**
12427  * i40e_reconfig_rss_queues - change number of queues for rss and rebuild
12428  * @pf: board private structure
12429  * @queue_count: the requested queue count for rss.
12430  *
12431  * returns 0 if rss is not enabled, if enabled returns the final rss queue
12432  * count which may be different from the requested queue count.
12433  * Note: expects to be called while under rtnl_lock()
12434  **/
12435 int i40e_reconfig_rss_queues(struct i40e_pf *pf, int queue_count)
12436 {
12437 	struct i40e_vsi *vsi = pf->vsi[pf->lan_vsi];
12438 	int new_rss_size;
12439 
12440 	if (!(pf->flags & I40E_FLAG_RSS_ENABLED))
12441 		return 0;
12442 
12443 	queue_count = min_t(int, queue_count, num_online_cpus());
12444 	new_rss_size = min_t(int, queue_count, pf->rss_size_max);
12445 
12446 	if (queue_count != vsi->num_queue_pairs) {
12447 		u16 qcount;
12448 
12449 		vsi->req_queue_pairs = queue_count;
12450 		i40e_prep_for_reset(pf);
12451 		if (test_bit(__I40E_IN_REMOVE, pf->state))
12452 			return pf->alloc_rss_size;
12453 
12454 		pf->alloc_rss_size = new_rss_size;
12455 
12456 		i40e_reset_and_rebuild(pf, true, true);
12457 
12458 		/* Discard the user configured hash keys and lut, if less
12459 		 * queues are enabled.
12460 		 */
12461 		if (queue_count < vsi->rss_size) {
12462 			i40e_clear_rss_config_user(vsi);
12463 			dev_dbg(&pf->pdev->dev,
12464 				"discard user configured hash keys and lut\n");
12465 		}
12466 
12467 		/* Reset vsi->rss_size, as number of enabled queues changed */
12468 		qcount = vsi->num_queue_pairs / vsi->tc_config.numtc;
12469 		vsi->rss_size = min_t(int, pf->alloc_rss_size, qcount);
12470 
12471 		i40e_pf_config_rss(pf);
12472 	}
12473 	dev_info(&pf->pdev->dev, "User requested queue count/HW max RSS count:  %d/%d\n",
12474 		 vsi->req_queue_pairs, pf->rss_size_max);
12475 	return pf->alloc_rss_size;
12476 }
12477 
12478 /**
12479  * i40e_get_partition_bw_setting - Retrieve BW settings for this PF partition
12480  * @pf: board private structure
12481  **/
12482 i40e_status i40e_get_partition_bw_setting(struct i40e_pf *pf)
12483 {
12484 	i40e_status status;
12485 	bool min_valid, max_valid;
12486 	u32 max_bw, min_bw;
12487 
12488 	status = i40e_read_bw_from_alt_ram(&pf->hw, &max_bw, &min_bw,
12489 					   &min_valid, &max_valid);
12490 
12491 	if (!status) {
12492 		if (min_valid)
12493 			pf->min_bw = min_bw;
12494 		if (max_valid)
12495 			pf->max_bw = max_bw;
12496 	}
12497 
12498 	return status;
12499 }
12500 
12501 /**
12502  * i40e_set_partition_bw_setting - Set BW settings for this PF partition
12503  * @pf: board private structure
12504  **/
12505 i40e_status i40e_set_partition_bw_setting(struct i40e_pf *pf)
12506 {
12507 	struct i40e_aqc_configure_partition_bw_data bw_data;
12508 	i40e_status status;
12509 
12510 	memset(&bw_data, 0, sizeof(bw_data));
12511 
12512 	/* Set the valid bit for this PF */
12513 	bw_data.pf_valid_bits = cpu_to_le16(BIT(pf->hw.pf_id));
12514 	bw_data.max_bw[pf->hw.pf_id] = pf->max_bw & I40E_ALT_BW_VALUE_MASK;
12515 	bw_data.min_bw[pf->hw.pf_id] = pf->min_bw & I40E_ALT_BW_VALUE_MASK;
12516 
12517 	/* Set the new bandwidths */
12518 	status = i40e_aq_configure_partition_bw(&pf->hw, &bw_data, NULL);
12519 
12520 	return status;
12521 }
12522 
12523 /**
12524  * i40e_commit_partition_bw_setting - Commit BW settings for this PF partition
12525  * @pf: board private structure
12526  **/
12527 i40e_status i40e_commit_partition_bw_setting(struct i40e_pf *pf)
12528 {
12529 	/* Commit temporary BW setting to permanent NVM image */
12530 	enum i40e_admin_queue_err last_aq_status;
12531 	i40e_status ret;
12532 	u16 nvm_word;
12533 
12534 	if (pf->hw.partition_id != 1) {
12535 		dev_info(&pf->pdev->dev,
12536 			 "Commit BW only works on partition 1! This is partition %d",
12537 			 pf->hw.partition_id);
12538 		ret = I40E_NOT_SUPPORTED;
12539 		goto bw_commit_out;
12540 	}
12541 
12542 	/* Acquire NVM for read access */
12543 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_READ);
12544 	last_aq_status = pf->hw.aq.asq_last_status;
12545 	if (ret) {
12546 		dev_info(&pf->pdev->dev,
12547 			 "Cannot acquire NVM for read access, err %s aq_err %s\n",
12548 			 i40e_stat_str(&pf->hw, ret),
12549 			 i40e_aq_str(&pf->hw, last_aq_status));
12550 		goto bw_commit_out;
12551 	}
12552 
12553 	/* Read word 0x10 of NVM - SW compatibility word 1 */
12554 	ret = i40e_aq_read_nvm(&pf->hw,
12555 			       I40E_SR_NVM_CONTROL_WORD,
12556 			       0x10, sizeof(nvm_word), &nvm_word,
12557 			       false, NULL);
12558 	/* Save off last admin queue command status before releasing
12559 	 * the NVM
12560 	 */
12561 	last_aq_status = pf->hw.aq.asq_last_status;
12562 	i40e_release_nvm(&pf->hw);
12563 	if (ret) {
12564 		dev_info(&pf->pdev->dev, "NVM read error, err %s aq_err %s\n",
12565 			 i40e_stat_str(&pf->hw, ret),
12566 			 i40e_aq_str(&pf->hw, last_aq_status));
12567 		goto bw_commit_out;
12568 	}
12569 
12570 	/* Wait a bit for NVM release to complete */
12571 	msleep(50);
12572 
12573 	/* Acquire NVM for write access */
12574 	ret = i40e_acquire_nvm(&pf->hw, I40E_RESOURCE_WRITE);
12575 	last_aq_status = pf->hw.aq.asq_last_status;
12576 	if (ret) {
12577 		dev_info(&pf->pdev->dev,
12578 			 "Cannot acquire NVM for write access, err %s aq_err %s\n",
12579 			 i40e_stat_str(&pf->hw, ret),
12580 			 i40e_aq_str(&pf->hw, last_aq_status));
12581 		goto bw_commit_out;
12582 	}
12583 	/* Write it back out unchanged to initiate update NVM,
12584 	 * which will force a write of the shadow (alt) RAM to
12585 	 * the NVM - thus storing the bandwidth values permanently.
12586 	 */
12587 	ret = i40e_aq_update_nvm(&pf->hw,
12588 				 I40E_SR_NVM_CONTROL_WORD,
12589 				 0x10, sizeof(nvm_word),
12590 				 &nvm_word, true, 0, NULL);
12591 	/* Save off last admin queue command status before releasing
12592 	 * the NVM
12593 	 */
12594 	last_aq_status = pf->hw.aq.asq_last_status;
12595 	i40e_release_nvm(&pf->hw);
12596 	if (ret)
12597 		dev_info(&pf->pdev->dev,
12598 			 "BW settings NOT SAVED, err %s aq_err %s\n",
12599 			 i40e_stat_str(&pf->hw, ret),
12600 			 i40e_aq_str(&pf->hw, last_aq_status));
12601 bw_commit_out:
12602 
12603 	return ret;
12604 }
12605 
12606 /**
12607  * i40e_is_total_port_shutdown_enabled - read NVM and return value
12608  * if total port shutdown feature is enabled for this PF
12609  * @pf: board private structure
12610  **/
12611 static bool i40e_is_total_port_shutdown_enabled(struct i40e_pf *pf)
12612 {
12613 #define I40E_TOTAL_PORT_SHUTDOWN_ENABLED	BIT(4)
12614 #define I40E_FEATURES_ENABLE_PTR		0x2A
12615 #define I40E_CURRENT_SETTING_PTR		0x2B
12616 #define I40E_LINK_BEHAVIOR_WORD_OFFSET		0x2D
12617 #define I40E_LINK_BEHAVIOR_WORD_LENGTH		0x1
12618 #define I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED	BIT(0)
12619 #define I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH	4
12620 	i40e_status read_status = I40E_SUCCESS;
12621 	u16 sr_emp_sr_settings_ptr = 0;
12622 	u16 features_enable = 0;
12623 	u16 link_behavior = 0;
12624 	bool ret = false;
12625 
12626 	read_status = i40e_read_nvm_word(&pf->hw,
12627 					 I40E_SR_EMP_SR_SETTINGS_PTR,
12628 					 &sr_emp_sr_settings_ptr);
12629 	if (read_status)
12630 		goto err_nvm;
12631 	read_status = i40e_read_nvm_word(&pf->hw,
12632 					 sr_emp_sr_settings_ptr +
12633 					 I40E_FEATURES_ENABLE_PTR,
12634 					 &features_enable);
12635 	if (read_status)
12636 		goto err_nvm;
12637 	if (I40E_TOTAL_PORT_SHUTDOWN_ENABLED & features_enable) {
12638 		read_status = i40e_read_nvm_module_data(&pf->hw,
12639 							I40E_SR_EMP_SR_SETTINGS_PTR,
12640 							I40E_CURRENT_SETTING_PTR,
12641 							I40E_LINK_BEHAVIOR_WORD_OFFSET,
12642 							I40E_LINK_BEHAVIOR_WORD_LENGTH,
12643 							&link_behavior);
12644 		if (read_status)
12645 			goto err_nvm;
12646 		link_behavior >>= (pf->hw.port * I40E_LINK_BEHAVIOR_PORT_BIT_LENGTH);
12647 		ret = I40E_LINK_BEHAVIOR_OS_FORCED_ENABLED & link_behavior;
12648 	}
12649 	return ret;
12650 
12651 err_nvm:
12652 	dev_warn(&pf->pdev->dev,
12653 		 "total-port-shutdown feature is off due to read nvm error: %s\n",
12654 		 i40e_stat_str(&pf->hw, read_status));
12655 	return ret;
12656 }
12657 
12658 /**
12659  * i40e_sw_init - Initialize general software structures (struct i40e_pf)
12660  * @pf: board private structure to initialize
12661  *
12662  * i40e_sw_init initializes the Adapter private data structure.
12663  * Fields are initialized based on PCI device information and
12664  * OS network device settings (MTU size).
12665  **/
12666 static int i40e_sw_init(struct i40e_pf *pf)
12667 {
12668 	int err = 0;
12669 	int size;
12670 	u16 pow;
12671 
12672 	/* Set default capability flags */
12673 	pf->flags = I40E_FLAG_RX_CSUM_ENABLED |
12674 		    I40E_FLAG_MSI_ENABLED     |
12675 		    I40E_FLAG_MSIX_ENABLED;
12676 
12677 	/* Set default ITR */
12678 	pf->rx_itr_default = I40E_ITR_RX_DEF;
12679 	pf->tx_itr_default = I40E_ITR_TX_DEF;
12680 
12681 	/* Depending on PF configurations, it is possible that the RSS
12682 	 * maximum might end up larger than the available queues
12683 	 */
12684 	pf->rss_size_max = BIT(pf->hw.func_caps.rss_table_entry_width);
12685 	pf->alloc_rss_size = 1;
12686 	pf->rss_table_size = pf->hw.func_caps.rss_table_size;
12687 	pf->rss_size_max = min_t(int, pf->rss_size_max,
12688 				 pf->hw.func_caps.num_tx_qp);
12689 
12690 	/* find the next higher power-of-2 of num cpus */
12691 	pow = roundup_pow_of_two(num_online_cpus());
12692 	pf->rss_size_max = min_t(int, pf->rss_size_max, pow);
12693 
12694 	if (pf->hw.func_caps.rss) {
12695 		pf->flags |= I40E_FLAG_RSS_ENABLED;
12696 		pf->alloc_rss_size = min_t(int, pf->rss_size_max,
12697 					   num_online_cpus());
12698 	}
12699 
12700 	/* MFP mode enabled */
12701 	if (pf->hw.func_caps.npar_enable || pf->hw.func_caps.flex10_enable) {
12702 		pf->flags |= I40E_FLAG_MFP_ENABLED;
12703 		dev_info(&pf->pdev->dev, "MFP mode Enabled\n");
12704 		if (i40e_get_partition_bw_setting(pf)) {
12705 			dev_warn(&pf->pdev->dev,
12706 				 "Could not get partition bw settings\n");
12707 		} else {
12708 			dev_info(&pf->pdev->dev,
12709 				 "Partition BW Min = %8.8x, Max = %8.8x\n",
12710 				 pf->min_bw, pf->max_bw);
12711 
12712 			/* nudge the Tx scheduler */
12713 			i40e_set_partition_bw_setting(pf);
12714 		}
12715 	}
12716 
12717 	if ((pf->hw.func_caps.fd_filters_guaranteed > 0) ||
12718 	    (pf->hw.func_caps.fd_filters_best_effort > 0)) {
12719 		pf->flags |= I40E_FLAG_FD_ATR_ENABLED;
12720 		pf->atr_sample_rate = I40E_DEFAULT_ATR_SAMPLE_RATE;
12721 		if (pf->flags & I40E_FLAG_MFP_ENABLED &&
12722 		    pf->hw.num_partitions > 1)
12723 			dev_info(&pf->pdev->dev,
12724 				 "Flow Director Sideband mode Disabled in MFP mode\n");
12725 		else
12726 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12727 		pf->fdir_pf_filter_count =
12728 				 pf->hw.func_caps.fd_filters_guaranteed;
12729 		pf->hw.fdir_shared_filter_count =
12730 				 pf->hw.func_caps.fd_filters_best_effort;
12731 	}
12732 
12733 	if (pf->hw.mac.type == I40E_MAC_X722) {
12734 		pf->hw_features |= (I40E_HW_RSS_AQ_CAPABLE |
12735 				    I40E_HW_128_QP_RSS_CAPABLE |
12736 				    I40E_HW_ATR_EVICT_CAPABLE |
12737 				    I40E_HW_WB_ON_ITR_CAPABLE |
12738 				    I40E_HW_MULTIPLE_TCP_UDP_RSS_PCTYPE |
12739 				    I40E_HW_NO_PCI_LINK_CHECK |
12740 				    I40E_HW_USE_SET_LLDP_MIB |
12741 				    I40E_HW_GENEVE_OFFLOAD_CAPABLE |
12742 				    I40E_HW_PTP_L4_CAPABLE |
12743 				    I40E_HW_WOL_MC_MAGIC_PKT_WAKE |
12744 				    I40E_HW_OUTER_UDP_CSUM_CAPABLE);
12745 
12746 #define I40E_FDEVICT_PCTYPE_DEFAULT 0xc03
12747 		if (rd32(&pf->hw, I40E_GLQF_FDEVICTENA(1)) !=
12748 		    I40E_FDEVICT_PCTYPE_DEFAULT) {
12749 			dev_warn(&pf->pdev->dev,
12750 				 "FD EVICT PCTYPES are not right, disable FD HW EVICT\n");
12751 			pf->hw_features &= ~I40E_HW_ATR_EVICT_CAPABLE;
12752 		}
12753 	} else if ((pf->hw.aq.api_maj_ver > 1) ||
12754 		   ((pf->hw.aq.api_maj_ver == 1) &&
12755 		    (pf->hw.aq.api_min_ver > 4))) {
12756 		/* Supported in FW API version higher than 1.4 */
12757 		pf->hw_features |= I40E_HW_GENEVE_OFFLOAD_CAPABLE;
12758 	}
12759 
12760 	/* Enable HW ATR eviction if possible */
12761 	if (pf->hw_features & I40E_HW_ATR_EVICT_CAPABLE)
12762 		pf->flags |= I40E_FLAG_HW_ATR_EVICT_ENABLED;
12763 
12764 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12765 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 33)) ||
12766 	    (pf->hw.aq.fw_maj_ver < 4))) {
12767 		pf->hw_features |= I40E_HW_RESTART_AUTONEG;
12768 		/* No DCB support  for FW < v4.33 */
12769 		pf->hw_features |= I40E_HW_NO_DCB_SUPPORT;
12770 	}
12771 
12772 	/* Disable FW LLDP if FW < v4.3 */
12773 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12774 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver < 3)) ||
12775 	    (pf->hw.aq.fw_maj_ver < 4)))
12776 		pf->hw_features |= I40E_HW_STOP_FW_LLDP;
12777 
12778 	/* Use the FW Set LLDP MIB API if FW > v4.40 */
12779 	if ((pf->hw.mac.type == I40E_MAC_XL710) &&
12780 	    (((pf->hw.aq.fw_maj_ver == 4) && (pf->hw.aq.fw_min_ver >= 40)) ||
12781 	    (pf->hw.aq.fw_maj_ver >= 5)))
12782 		pf->hw_features |= I40E_HW_USE_SET_LLDP_MIB;
12783 
12784 	/* Enable PTP L4 if FW > v6.0 */
12785 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12786 	    pf->hw.aq.fw_maj_ver >= 6)
12787 		pf->hw_features |= I40E_HW_PTP_L4_CAPABLE;
12788 
12789 	if (pf->hw.func_caps.vmdq && num_online_cpus() != 1) {
12790 		pf->num_vmdq_vsis = I40E_DEFAULT_NUM_VMDQ_VSI;
12791 		pf->flags |= I40E_FLAG_VMDQ_ENABLED;
12792 		pf->num_vmdq_qps = i40e_default_queues_per_vmdq(pf);
12793 	}
12794 
12795 	if (pf->hw.func_caps.iwarp && num_online_cpus() != 1) {
12796 		pf->flags |= I40E_FLAG_IWARP_ENABLED;
12797 		/* IWARP needs one extra vector for CQP just like MISC.*/
12798 		pf->num_iwarp_msix = (int)num_online_cpus() + 1;
12799 	}
12800 	/* Stopping FW LLDP engine is supported on XL710 and X722
12801 	 * starting from FW versions determined in i40e_init_adminq.
12802 	 * Stopping the FW LLDP engine is not supported on XL710
12803 	 * if NPAR is functioning so unset this hw flag in this case.
12804 	 */
12805 	if (pf->hw.mac.type == I40E_MAC_XL710 &&
12806 	    pf->hw.func_caps.npar_enable &&
12807 	    (pf->hw.flags & I40E_HW_FLAG_FW_LLDP_STOPPABLE))
12808 		pf->hw.flags &= ~I40E_HW_FLAG_FW_LLDP_STOPPABLE;
12809 
12810 #ifdef CONFIG_PCI_IOV
12811 	if (pf->hw.func_caps.num_vfs && pf->hw.partition_id == 1) {
12812 		pf->num_vf_qps = I40E_DEFAULT_QUEUES_PER_VF;
12813 		pf->flags |= I40E_FLAG_SRIOV_ENABLED;
12814 		pf->num_req_vfs = min_t(int,
12815 					pf->hw.func_caps.num_vfs,
12816 					I40E_MAX_VF_COUNT);
12817 	}
12818 #endif /* CONFIG_PCI_IOV */
12819 	pf->eeprom_version = 0xDEAD;
12820 	pf->lan_veb = I40E_NO_VEB;
12821 	pf->lan_vsi = I40E_NO_VSI;
12822 
12823 	/* By default FW has this off for performance reasons */
12824 	pf->flags &= ~I40E_FLAG_VEB_STATS_ENABLED;
12825 
12826 	/* set up queue assignment tracking */
12827 	size = sizeof(struct i40e_lump_tracking)
12828 		+ (sizeof(u16) * pf->hw.func_caps.num_tx_qp);
12829 	pf->qp_pile = kzalloc(size, GFP_KERNEL);
12830 	if (!pf->qp_pile) {
12831 		err = -ENOMEM;
12832 		goto sw_init_done;
12833 	}
12834 	pf->qp_pile->num_entries = pf->hw.func_caps.num_tx_qp;
12835 
12836 	pf->tx_timeout_recovery_level = 1;
12837 
12838 	if (pf->hw.mac.type != I40E_MAC_X722 &&
12839 	    i40e_is_total_port_shutdown_enabled(pf)) {
12840 		/* Link down on close must be on when total port shutdown
12841 		 * is enabled for a given port
12842 		 */
12843 		pf->flags |= (I40E_FLAG_TOTAL_PORT_SHUTDOWN_ENABLED |
12844 			      I40E_FLAG_LINK_DOWN_ON_CLOSE_ENABLED);
12845 		dev_info(&pf->pdev->dev,
12846 			 "total-port-shutdown was enabled, link-down-on-close is forced on\n");
12847 	}
12848 	mutex_init(&pf->switch_mutex);
12849 
12850 sw_init_done:
12851 	return err;
12852 }
12853 
12854 /**
12855  * i40e_set_ntuple - set the ntuple feature flag and take action
12856  * @pf: board private structure to initialize
12857  * @features: the feature set that the stack is suggesting
12858  *
12859  * returns a bool to indicate if reset needs to happen
12860  **/
12861 bool i40e_set_ntuple(struct i40e_pf *pf, netdev_features_t features)
12862 {
12863 	bool need_reset = false;
12864 
12865 	/* Check if Flow Director n-tuple support was enabled or disabled.  If
12866 	 * the state changed, we need to reset.
12867 	 */
12868 	if (features & NETIF_F_NTUPLE) {
12869 		/* Enable filters and mark for reset */
12870 		if (!(pf->flags & I40E_FLAG_FD_SB_ENABLED))
12871 			need_reset = true;
12872 		/* enable FD_SB only if there is MSI-X vector and no cloud
12873 		 * filters exist
12874 		 */
12875 		if (pf->num_fdsb_msix > 0 && !pf->num_cloud_filters) {
12876 			pf->flags |= I40E_FLAG_FD_SB_ENABLED;
12877 			pf->flags &= ~I40E_FLAG_FD_SB_INACTIVE;
12878 		}
12879 	} else {
12880 		/* turn off filters, mark for reset and clear SW filter list */
12881 		if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
12882 			need_reset = true;
12883 			i40e_fdir_filter_exit(pf);
12884 		}
12885 		pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
12886 		clear_bit(__I40E_FD_SB_AUTO_DISABLED, pf->state);
12887 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
12888 
12889 		/* reset fd counters */
12890 		pf->fd_add_err = 0;
12891 		pf->fd_atr_cnt = 0;
12892 		/* if ATR was auto disabled it can be re-enabled. */
12893 		if (test_and_clear_bit(__I40E_FD_ATR_AUTO_DISABLED, pf->state))
12894 			if ((pf->flags & I40E_FLAG_FD_ATR_ENABLED) &&
12895 			    (I40E_DEBUG_FD & pf->hw.debug_mask))
12896 				dev_info(&pf->pdev->dev, "ATR re-enabled.\n");
12897 	}
12898 	return need_reset;
12899 }
12900 
12901 /**
12902  * i40e_clear_rss_lut - clear the rx hash lookup table
12903  * @vsi: the VSI being configured
12904  **/
12905 static void i40e_clear_rss_lut(struct i40e_vsi *vsi)
12906 {
12907 	struct i40e_pf *pf = vsi->back;
12908 	struct i40e_hw *hw = &pf->hw;
12909 	u16 vf_id = vsi->vf_id;
12910 	u8 i;
12911 
12912 	if (vsi->type == I40E_VSI_MAIN) {
12913 		for (i = 0; i <= I40E_PFQF_HLUT_MAX_INDEX; i++)
12914 			wr32(hw, I40E_PFQF_HLUT(i), 0);
12915 	} else if (vsi->type == I40E_VSI_SRIOV) {
12916 		for (i = 0; i <= I40E_VFQF_HLUT_MAX_INDEX; i++)
12917 			i40e_write_rx_ctl(hw, I40E_VFQF_HLUT1(i, vf_id), 0);
12918 	} else {
12919 		dev_err(&pf->pdev->dev, "Cannot set RSS LUT - invalid VSI type\n");
12920 	}
12921 }
12922 
12923 /**
12924  * i40e_set_features - set the netdev feature flags
12925  * @netdev: ptr to the netdev being adjusted
12926  * @features: the feature set that the stack is suggesting
12927  * Note: expects to be called while under rtnl_lock()
12928  **/
12929 static int i40e_set_features(struct net_device *netdev,
12930 			     netdev_features_t features)
12931 {
12932 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12933 	struct i40e_vsi *vsi = np->vsi;
12934 	struct i40e_pf *pf = vsi->back;
12935 	bool need_reset;
12936 
12937 	if (features & NETIF_F_RXHASH && !(netdev->features & NETIF_F_RXHASH))
12938 		i40e_pf_config_rss(pf);
12939 	else if (!(features & NETIF_F_RXHASH) &&
12940 		 netdev->features & NETIF_F_RXHASH)
12941 		i40e_clear_rss_lut(vsi);
12942 
12943 	if (features & NETIF_F_HW_VLAN_CTAG_RX)
12944 		i40e_vlan_stripping_enable(vsi);
12945 	else
12946 		i40e_vlan_stripping_disable(vsi);
12947 
12948 	if (!(features & NETIF_F_HW_TC) &&
12949 	    (netdev->features & NETIF_F_HW_TC) && pf->num_cloud_filters) {
12950 		dev_err(&pf->pdev->dev,
12951 			"Offloaded tc filters active, can't turn hw_tc_offload off");
12952 		return -EINVAL;
12953 	}
12954 
12955 	if (!(features & NETIF_F_HW_L2FW_DOFFLOAD) && vsi->macvlan_cnt)
12956 		i40e_del_all_macvlans(vsi);
12957 
12958 	need_reset = i40e_set_ntuple(pf, features);
12959 
12960 	if (need_reset)
12961 		i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
12962 
12963 	return 0;
12964 }
12965 
12966 static int i40e_udp_tunnel_set_port(struct net_device *netdev,
12967 				    unsigned int table, unsigned int idx,
12968 				    struct udp_tunnel_info *ti)
12969 {
12970 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12971 	struct i40e_hw *hw = &np->vsi->back->hw;
12972 	u8 type, filter_index;
12973 	i40e_status ret;
12974 
12975 	type = ti->type == UDP_TUNNEL_TYPE_VXLAN ? I40E_AQC_TUNNEL_TYPE_VXLAN :
12976 						   I40E_AQC_TUNNEL_TYPE_NGE;
12977 
12978 	ret = i40e_aq_add_udp_tunnel(hw, ntohs(ti->port), type, &filter_index,
12979 				     NULL);
12980 	if (ret) {
12981 		netdev_info(netdev, "add UDP port failed, err %s aq_err %s\n",
12982 			    i40e_stat_str(hw, ret),
12983 			    i40e_aq_str(hw, hw->aq.asq_last_status));
12984 		return -EIO;
12985 	}
12986 
12987 	udp_tunnel_nic_set_port_priv(netdev, table, idx, filter_index);
12988 	return 0;
12989 }
12990 
12991 static int i40e_udp_tunnel_unset_port(struct net_device *netdev,
12992 				      unsigned int table, unsigned int idx,
12993 				      struct udp_tunnel_info *ti)
12994 {
12995 	struct i40e_netdev_priv *np = netdev_priv(netdev);
12996 	struct i40e_hw *hw = &np->vsi->back->hw;
12997 	i40e_status ret;
12998 
12999 	ret = i40e_aq_del_udp_tunnel(hw, ti->hw_priv, NULL);
13000 	if (ret) {
13001 		netdev_info(netdev, "delete UDP port failed, err %s aq_err %s\n",
13002 			    i40e_stat_str(hw, ret),
13003 			    i40e_aq_str(hw, hw->aq.asq_last_status));
13004 		return -EIO;
13005 	}
13006 
13007 	return 0;
13008 }
13009 
13010 static int i40e_get_phys_port_id(struct net_device *netdev,
13011 				 struct netdev_phys_item_id *ppid)
13012 {
13013 	struct i40e_netdev_priv *np = netdev_priv(netdev);
13014 	struct i40e_pf *pf = np->vsi->back;
13015 	struct i40e_hw *hw = &pf->hw;
13016 
13017 	if (!(pf->hw_features & I40E_HW_PORT_ID_VALID))
13018 		return -EOPNOTSUPP;
13019 
13020 	ppid->id_len = min_t(int, sizeof(hw->mac.port_addr), sizeof(ppid->id));
13021 	memcpy(ppid->id, hw->mac.port_addr, ppid->id_len);
13022 
13023 	return 0;
13024 }
13025 
13026 /**
13027  * i40e_ndo_fdb_add - add an entry to the hardware database
13028  * @ndm: the input from the stack
13029  * @tb: pointer to array of nladdr (unused)
13030  * @dev: the net device pointer
13031  * @addr: the MAC address entry being added
13032  * @vid: VLAN ID
13033  * @flags: instructions from stack about fdb operation
13034  * @extack: netlink extended ack, unused currently
13035  */
13036 static int i40e_ndo_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
13037 			    struct net_device *dev,
13038 			    const unsigned char *addr, u16 vid,
13039 			    u16 flags,
13040 			    struct netlink_ext_ack *extack)
13041 {
13042 	struct i40e_netdev_priv *np = netdev_priv(dev);
13043 	struct i40e_pf *pf = np->vsi->back;
13044 	int err = 0;
13045 
13046 	if (!(pf->flags & I40E_FLAG_SRIOV_ENABLED))
13047 		return -EOPNOTSUPP;
13048 
13049 	if (vid) {
13050 		pr_info("%s: vlans aren't supported yet for dev_uc|mc_add()\n", dev->name);
13051 		return -EINVAL;
13052 	}
13053 
13054 	/* Hardware does not support aging addresses so if a
13055 	 * ndm_state is given only allow permanent addresses
13056 	 */
13057 	if (ndm->ndm_state && !(ndm->ndm_state & NUD_PERMANENT)) {
13058 		netdev_info(dev, "FDB only supports static addresses\n");
13059 		return -EINVAL;
13060 	}
13061 
13062 	if (is_unicast_ether_addr(addr) || is_link_local_ether_addr(addr))
13063 		err = dev_uc_add_excl(dev, addr);
13064 	else if (is_multicast_ether_addr(addr))
13065 		err = dev_mc_add_excl(dev, addr);
13066 	else
13067 		err = -EINVAL;
13068 
13069 	/* Only return duplicate errors if NLM_F_EXCL is set */
13070 	if (err == -EEXIST && !(flags & NLM_F_EXCL))
13071 		err = 0;
13072 
13073 	return err;
13074 }
13075 
13076 /**
13077  * i40e_ndo_bridge_setlink - Set the hardware bridge mode
13078  * @dev: the netdev being configured
13079  * @nlh: RTNL message
13080  * @flags: bridge flags
13081  * @extack: netlink extended ack
13082  *
13083  * Inserts a new hardware bridge if not already created and
13084  * enables the bridging mode requested (VEB or VEPA). If the
13085  * hardware bridge has already been inserted and the request
13086  * is to change the mode then that requires a PF reset to
13087  * allow rebuild of the components with required hardware
13088  * bridge mode enabled.
13089  *
13090  * Note: expects to be called while under rtnl_lock()
13091  **/
13092 static int i40e_ndo_bridge_setlink(struct net_device *dev,
13093 				   struct nlmsghdr *nlh,
13094 				   u16 flags,
13095 				   struct netlink_ext_ack *extack)
13096 {
13097 	struct i40e_netdev_priv *np = netdev_priv(dev);
13098 	struct i40e_vsi *vsi = np->vsi;
13099 	struct i40e_pf *pf = vsi->back;
13100 	struct i40e_veb *veb = NULL;
13101 	struct nlattr *attr, *br_spec;
13102 	int i, rem;
13103 
13104 	/* Only for PF VSI for now */
13105 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13106 		return -EOPNOTSUPP;
13107 
13108 	/* Find the HW bridge for PF VSI */
13109 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13110 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13111 			veb = pf->veb[i];
13112 	}
13113 
13114 	br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC);
13115 
13116 	nla_for_each_nested(attr, br_spec, rem) {
13117 		__u16 mode;
13118 
13119 		if (nla_type(attr) != IFLA_BRIDGE_MODE)
13120 			continue;
13121 
13122 		mode = nla_get_u16(attr);
13123 		if ((mode != BRIDGE_MODE_VEPA) &&
13124 		    (mode != BRIDGE_MODE_VEB))
13125 			return -EINVAL;
13126 
13127 		/* Insert a new HW bridge */
13128 		if (!veb) {
13129 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
13130 					     vsi->tc_config.enabled_tc);
13131 			if (veb) {
13132 				veb->bridge_mode = mode;
13133 				i40e_config_bridge_mode(veb);
13134 			} else {
13135 				/* No Bridge HW offload available */
13136 				return -ENOENT;
13137 			}
13138 			break;
13139 		} else if (mode != veb->bridge_mode) {
13140 			/* Existing HW bridge but different mode needs reset */
13141 			veb->bridge_mode = mode;
13142 			/* TODO: If no VFs or VMDq VSIs, disallow VEB mode */
13143 			if (mode == BRIDGE_MODE_VEB)
13144 				pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
13145 			else
13146 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
13147 			i40e_do_reset(pf, I40E_PF_RESET_FLAG, true);
13148 			break;
13149 		}
13150 	}
13151 
13152 	return 0;
13153 }
13154 
13155 /**
13156  * i40e_ndo_bridge_getlink - Get the hardware bridge mode
13157  * @skb: skb buff
13158  * @pid: process id
13159  * @seq: RTNL message seq #
13160  * @dev: the netdev being configured
13161  * @filter_mask: unused
13162  * @nlflags: netlink flags passed in
13163  *
13164  * Return the mode in which the hardware bridge is operating in
13165  * i.e VEB or VEPA.
13166  **/
13167 static int i40e_ndo_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq,
13168 				   struct net_device *dev,
13169 				   u32 __always_unused filter_mask,
13170 				   int nlflags)
13171 {
13172 	struct i40e_netdev_priv *np = netdev_priv(dev);
13173 	struct i40e_vsi *vsi = np->vsi;
13174 	struct i40e_pf *pf = vsi->back;
13175 	struct i40e_veb *veb = NULL;
13176 	int i;
13177 
13178 	/* Only for PF VSI for now */
13179 	if (vsi->seid != pf->vsi[pf->lan_vsi]->seid)
13180 		return -EOPNOTSUPP;
13181 
13182 	/* Find the HW bridge for the PF VSI */
13183 	for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
13184 		if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
13185 			veb = pf->veb[i];
13186 	}
13187 
13188 	if (!veb)
13189 		return 0;
13190 
13191 	return ndo_dflt_bridge_getlink(skb, pid, seq, dev, veb->bridge_mode,
13192 				       0, 0, nlflags, filter_mask, NULL);
13193 }
13194 
13195 /**
13196  * i40e_features_check - Validate encapsulated packet conforms to limits
13197  * @skb: skb buff
13198  * @dev: This physical port's netdev
13199  * @features: Offload features that the stack believes apply
13200  **/
13201 static netdev_features_t i40e_features_check(struct sk_buff *skb,
13202 					     struct net_device *dev,
13203 					     netdev_features_t features)
13204 {
13205 	size_t len;
13206 
13207 	/* No point in doing any of this if neither checksum nor GSO are
13208 	 * being requested for this frame.  We can rule out both by just
13209 	 * checking for CHECKSUM_PARTIAL
13210 	 */
13211 	if (skb->ip_summed != CHECKSUM_PARTIAL)
13212 		return features;
13213 
13214 	/* We cannot support GSO if the MSS is going to be less than
13215 	 * 64 bytes.  If it is then we need to drop support for GSO.
13216 	 */
13217 	if (skb_is_gso(skb) && (skb_shinfo(skb)->gso_size < 64))
13218 		features &= ~NETIF_F_GSO_MASK;
13219 
13220 	/* MACLEN can support at most 63 words */
13221 	len = skb_network_header(skb) - skb->data;
13222 	if (len & ~(63 * 2))
13223 		goto out_err;
13224 
13225 	/* IPLEN and EIPLEN can support at most 127 dwords */
13226 	len = skb_transport_header(skb) - skb_network_header(skb);
13227 	if (len & ~(127 * 4))
13228 		goto out_err;
13229 
13230 	if (skb->encapsulation) {
13231 		/* L4TUNLEN can support 127 words */
13232 		len = skb_inner_network_header(skb) - skb_transport_header(skb);
13233 		if (len & ~(127 * 2))
13234 			goto out_err;
13235 
13236 		/* IPLEN can support at most 127 dwords */
13237 		len = skb_inner_transport_header(skb) -
13238 		      skb_inner_network_header(skb);
13239 		if (len & ~(127 * 4))
13240 			goto out_err;
13241 	}
13242 
13243 	/* No need to validate L4LEN as TCP is the only protocol with a
13244 	 * flexible value and we support all possible values supported
13245 	 * by TCP, which is at most 15 dwords
13246 	 */
13247 
13248 	return features;
13249 out_err:
13250 	return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
13251 }
13252 
13253 /**
13254  * i40e_xdp_setup - add/remove an XDP program
13255  * @vsi: VSI to changed
13256  * @prog: XDP program
13257  * @extack: netlink extended ack
13258  **/
13259 static int i40e_xdp_setup(struct i40e_vsi *vsi, struct bpf_prog *prog,
13260 			  struct netlink_ext_ack *extack)
13261 {
13262 	int frame_size = vsi->netdev->mtu + ETH_HLEN + ETH_FCS_LEN + VLAN_HLEN;
13263 	struct i40e_pf *pf = vsi->back;
13264 	struct bpf_prog *old_prog;
13265 	bool need_reset;
13266 	int i;
13267 
13268 	/* Don't allow frames that span over multiple buffers */
13269 	if (frame_size > vsi->rx_buf_len) {
13270 		NL_SET_ERR_MSG_MOD(extack, "MTU too large to enable XDP");
13271 		return -EINVAL;
13272 	}
13273 
13274 	/* When turning XDP on->off/off->on we reset and rebuild the rings. */
13275 	need_reset = (i40e_enabled_xdp_vsi(vsi) != !!prog);
13276 
13277 	if (need_reset)
13278 		i40e_prep_for_reset(pf);
13279 
13280 	/* VSI shall be deleted in a moment, just return EINVAL */
13281 	if (test_bit(__I40E_IN_REMOVE, pf->state))
13282 		return -EINVAL;
13283 
13284 	old_prog = xchg(&vsi->xdp_prog, prog);
13285 
13286 	if (need_reset) {
13287 		if (!prog)
13288 			/* Wait until ndo_xsk_wakeup completes. */
13289 			synchronize_rcu();
13290 		i40e_reset_and_rebuild(pf, true, true);
13291 	}
13292 
13293 	if (!i40e_enabled_xdp_vsi(vsi) && prog) {
13294 		if (i40e_realloc_rx_bi_zc(vsi, true))
13295 			return -ENOMEM;
13296 	} else if (i40e_enabled_xdp_vsi(vsi) && !prog) {
13297 		if (i40e_realloc_rx_bi_zc(vsi, false))
13298 			return -ENOMEM;
13299 	}
13300 
13301 	for (i = 0; i < vsi->num_queue_pairs; i++)
13302 		WRITE_ONCE(vsi->rx_rings[i]->xdp_prog, vsi->xdp_prog);
13303 
13304 	if (old_prog)
13305 		bpf_prog_put(old_prog);
13306 
13307 	/* Kick start the NAPI context if there is an AF_XDP socket open
13308 	 * on that queue id. This so that receiving will start.
13309 	 */
13310 	if (need_reset && prog)
13311 		for (i = 0; i < vsi->num_queue_pairs; i++)
13312 			if (vsi->xdp_rings[i]->xsk_pool)
13313 				(void)i40e_xsk_wakeup(vsi->netdev, i,
13314 						      XDP_WAKEUP_RX);
13315 
13316 	return 0;
13317 }
13318 
13319 /**
13320  * i40e_enter_busy_conf - Enters busy config state
13321  * @vsi: vsi
13322  *
13323  * Returns 0 on success, <0 for failure.
13324  **/
13325 static int i40e_enter_busy_conf(struct i40e_vsi *vsi)
13326 {
13327 	struct i40e_pf *pf = vsi->back;
13328 	int timeout = 50;
13329 
13330 	while (test_and_set_bit(__I40E_CONFIG_BUSY, pf->state)) {
13331 		timeout--;
13332 		if (!timeout)
13333 			return -EBUSY;
13334 		usleep_range(1000, 2000);
13335 	}
13336 
13337 	return 0;
13338 }
13339 
13340 /**
13341  * i40e_exit_busy_conf - Exits busy config state
13342  * @vsi: vsi
13343  **/
13344 static void i40e_exit_busy_conf(struct i40e_vsi *vsi)
13345 {
13346 	struct i40e_pf *pf = vsi->back;
13347 
13348 	clear_bit(__I40E_CONFIG_BUSY, pf->state);
13349 }
13350 
13351 /**
13352  * i40e_queue_pair_reset_stats - Resets all statistics for a queue pair
13353  * @vsi: vsi
13354  * @queue_pair: queue pair
13355  **/
13356 static void i40e_queue_pair_reset_stats(struct i40e_vsi *vsi, int queue_pair)
13357 {
13358 	memset(&vsi->rx_rings[queue_pair]->rx_stats, 0,
13359 	       sizeof(vsi->rx_rings[queue_pair]->rx_stats));
13360 	memset(&vsi->tx_rings[queue_pair]->stats, 0,
13361 	       sizeof(vsi->tx_rings[queue_pair]->stats));
13362 	if (i40e_enabled_xdp_vsi(vsi)) {
13363 		memset(&vsi->xdp_rings[queue_pair]->stats, 0,
13364 		       sizeof(vsi->xdp_rings[queue_pair]->stats));
13365 	}
13366 }
13367 
13368 /**
13369  * i40e_queue_pair_clean_rings - Cleans all the rings of a queue pair
13370  * @vsi: vsi
13371  * @queue_pair: queue pair
13372  **/
13373 static void i40e_queue_pair_clean_rings(struct i40e_vsi *vsi, int queue_pair)
13374 {
13375 	i40e_clean_tx_ring(vsi->tx_rings[queue_pair]);
13376 	if (i40e_enabled_xdp_vsi(vsi)) {
13377 		/* Make sure that in-progress ndo_xdp_xmit calls are
13378 		 * completed.
13379 		 */
13380 		synchronize_rcu();
13381 		i40e_clean_tx_ring(vsi->xdp_rings[queue_pair]);
13382 	}
13383 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13384 }
13385 
13386 /**
13387  * i40e_queue_pair_toggle_napi - Enables/disables NAPI for a queue pair
13388  * @vsi: vsi
13389  * @queue_pair: queue pair
13390  * @enable: true for enable, false for disable
13391  **/
13392 static void i40e_queue_pair_toggle_napi(struct i40e_vsi *vsi, int queue_pair,
13393 					bool enable)
13394 {
13395 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13396 	struct i40e_q_vector *q_vector = rxr->q_vector;
13397 
13398 	if (!vsi->netdev)
13399 		return;
13400 
13401 	/* All rings in a qp belong to the same qvector. */
13402 	if (q_vector->rx.ring || q_vector->tx.ring) {
13403 		if (enable)
13404 			napi_enable(&q_vector->napi);
13405 		else
13406 			napi_disable(&q_vector->napi);
13407 	}
13408 }
13409 
13410 /**
13411  * i40e_queue_pair_toggle_rings - Enables/disables all rings for a queue pair
13412  * @vsi: vsi
13413  * @queue_pair: queue pair
13414  * @enable: true for enable, false for disable
13415  *
13416  * Returns 0 on success, <0 on failure.
13417  **/
13418 static int i40e_queue_pair_toggle_rings(struct i40e_vsi *vsi, int queue_pair,
13419 					bool enable)
13420 {
13421 	struct i40e_pf *pf = vsi->back;
13422 	int pf_q, ret = 0;
13423 
13424 	pf_q = vsi->base_queue + queue_pair;
13425 	ret = i40e_control_wait_tx_q(vsi->seid, pf, pf_q,
13426 				     false /*is xdp*/, enable);
13427 	if (ret) {
13428 		dev_info(&pf->pdev->dev,
13429 			 "VSI seid %d Tx ring %d %sable timeout\n",
13430 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13431 		return ret;
13432 	}
13433 
13434 	i40e_control_rx_q(pf, pf_q, enable);
13435 	ret = i40e_pf_rxq_wait(pf, pf_q, enable);
13436 	if (ret) {
13437 		dev_info(&pf->pdev->dev,
13438 			 "VSI seid %d Rx ring %d %sable timeout\n",
13439 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13440 		return ret;
13441 	}
13442 
13443 	/* Due to HW errata, on Rx disable only, the register can
13444 	 * indicate done before it really is. Needs 50ms to be sure
13445 	 */
13446 	if (!enable)
13447 		mdelay(50);
13448 
13449 	if (!i40e_enabled_xdp_vsi(vsi))
13450 		return ret;
13451 
13452 	ret = i40e_control_wait_tx_q(vsi->seid, pf,
13453 				     pf_q + vsi->alloc_queue_pairs,
13454 				     true /*is xdp*/, enable);
13455 	if (ret) {
13456 		dev_info(&pf->pdev->dev,
13457 			 "VSI seid %d XDP Tx ring %d %sable timeout\n",
13458 			 vsi->seid, pf_q, (enable ? "en" : "dis"));
13459 	}
13460 
13461 	return ret;
13462 }
13463 
13464 /**
13465  * i40e_queue_pair_enable_irq - Enables interrupts for a queue pair
13466  * @vsi: vsi
13467  * @queue_pair: queue_pair
13468  **/
13469 static void i40e_queue_pair_enable_irq(struct i40e_vsi *vsi, int queue_pair)
13470 {
13471 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13472 	struct i40e_pf *pf = vsi->back;
13473 	struct i40e_hw *hw = &pf->hw;
13474 
13475 	/* All rings in a qp belong to the same qvector. */
13476 	if (pf->flags & I40E_FLAG_MSIX_ENABLED)
13477 		i40e_irq_dynamic_enable(vsi, rxr->q_vector->v_idx);
13478 	else
13479 		i40e_irq_dynamic_enable_icr0(pf);
13480 
13481 	i40e_flush(hw);
13482 }
13483 
13484 /**
13485  * i40e_queue_pair_disable_irq - Disables interrupts for a queue pair
13486  * @vsi: vsi
13487  * @queue_pair: queue_pair
13488  **/
13489 static void i40e_queue_pair_disable_irq(struct i40e_vsi *vsi, int queue_pair)
13490 {
13491 	struct i40e_ring *rxr = vsi->rx_rings[queue_pair];
13492 	struct i40e_pf *pf = vsi->back;
13493 	struct i40e_hw *hw = &pf->hw;
13494 
13495 	/* For simplicity, instead of removing the qp interrupt causes
13496 	 * from the interrupt linked list, we simply disable the interrupt, and
13497 	 * leave the list intact.
13498 	 *
13499 	 * All rings in a qp belong to the same qvector.
13500 	 */
13501 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
13502 		u32 intpf = vsi->base_vector + rxr->q_vector->v_idx;
13503 
13504 		wr32(hw, I40E_PFINT_DYN_CTLN(intpf - 1), 0);
13505 		i40e_flush(hw);
13506 		synchronize_irq(pf->msix_entries[intpf].vector);
13507 	} else {
13508 		/* Legacy and MSI mode - this stops all interrupt handling */
13509 		wr32(hw, I40E_PFINT_ICR0_ENA, 0);
13510 		wr32(hw, I40E_PFINT_DYN_CTL0, 0);
13511 		i40e_flush(hw);
13512 		synchronize_irq(pf->pdev->irq);
13513 	}
13514 }
13515 
13516 /**
13517  * i40e_queue_pair_disable - Disables a queue pair
13518  * @vsi: vsi
13519  * @queue_pair: queue pair
13520  *
13521  * Returns 0 on success, <0 on failure.
13522  **/
13523 int i40e_queue_pair_disable(struct i40e_vsi *vsi, int queue_pair)
13524 {
13525 	int err;
13526 
13527 	err = i40e_enter_busy_conf(vsi);
13528 	if (err)
13529 		return err;
13530 
13531 	i40e_queue_pair_disable_irq(vsi, queue_pair);
13532 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, false /* off */);
13533 	i40e_clean_rx_ring(vsi->rx_rings[queue_pair]);
13534 	i40e_queue_pair_toggle_napi(vsi, queue_pair, false /* off */);
13535 	i40e_queue_pair_clean_rings(vsi, queue_pair);
13536 	i40e_queue_pair_reset_stats(vsi, queue_pair);
13537 
13538 	return err;
13539 }
13540 
13541 /**
13542  * i40e_queue_pair_enable - Enables a queue pair
13543  * @vsi: vsi
13544  * @queue_pair: queue pair
13545  *
13546  * Returns 0 on success, <0 on failure.
13547  **/
13548 int i40e_queue_pair_enable(struct i40e_vsi *vsi, int queue_pair)
13549 {
13550 	int err;
13551 
13552 	err = i40e_configure_tx_ring(vsi->tx_rings[queue_pair]);
13553 	if (err)
13554 		return err;
13555 
13556 	if (i40e_enabled_xdp_vsi(vsi)) {
13557 		err = i40e_configure_tx_ring(vsi->xdp_rings[queue_pair]);
13558 		if (err)
13559 			return err;
13560 	}
13561 
13562 	err = i40e_configure_rx_ring(vsi->rx_rings[queue_pair]);
13563 	if (err)
13564 		return err;
13565 
13566 	err = i40e_queue_pair_toggle_rings(vsi, queue_pair, true /* on */);
13567 	i40e_queue_pair_toggle_napi(vsi, queue_pair, true /* on */);
13568 	i40e_queue_pair_enable_irq(vsi, queue_pair);
13569 
13570 	i40e_exit_busy_conf(vsi);
13571 
13572 	return err;
13573 }
13574 
13575 /**
13576  * i40e_xdp - implements ndo_bpf for i40e
13577  * @dev: netdevice
13578  * @xdp: XDP command
13579  **/
13580 static int i40e_xdp(struct net_device *dev,
13581 		    struct netdev_bpf *xdp)
13582 {
13583 	struct i40e_netdev_priv *np = netdev_priv(dev);
13584 	struct i40e_vsi *vsi = np->vsi;
13585 
13586 	if (vsi->type != I40E_VSI_MAIN)
13587 		return -EINVAL;
13588 
13589 	switch (xdp->command) {
13590 	case XDP_SETUP_PROG:
13591 		return i40e_xdp_setup(vsi, xdp->prog, xdp->extack);
13592 	case XDP_SETUP_XSK_POOL:
13593 		return i40e_xsk_pool_setup(vsi, xdp->xsk.pool,
13594 					   xdp->xsk.queue_id);
13595 	default:
13596 		return -EINVAL;
13597 	}
13598 }
13599 
13600 static const struct net_device_ops i40e_netdev_ops = {
13601 	.ndo_open		= i40e_open,
13602 	.ndo_stop		= i40e_close,
13603 	.ndo_start_xmit		= i40e_lan_xmit_frame,
13604 	.ndo_get_stats64	= i40e_get_netdev_stats_struct,
13605 	.ndo_set_rx_mode	= i40e_set_rx_mode,
13606 	.ndo_validate_addr	= eth_validate_addr,
13607 	.ndo_set_mac_address	= i40e_set_mac,
13608 	.ndo_change_mtu		= i40e_change_mtu,
13609 	.ndo_eth_ioctl		= i40e_ioctl,
13610 	.ndo_tx_timeout		= i40e_tx_timeout,
13611 	.ndo_vlan_rx_add_vid	= i40e_vlan_rx_add_vid,
13612 	.ndo_vlan_rx_kill_vid	= i40e_vlan_rx_kill_vid,
13613 #ifdef CONFIG_NET_POLL_CONTROLLER
13614 	.ndo_poll_controller	= i40e_netpoll,
13615 #endif
13616 	.ndo_setup_tc		= __i40e_setup_tc,
13617 	.ndo_select_queue	= i40e_lan_select_queue,
13618 	.ndo_set_features	= i40e_set_features,
13619 	.ndo_set_vf_mac		= i40e_ndo_set_vf_mac,
13620 	.ndo_set_vf_vlan	= i40e_ndo_set_vf_port_vlan,
13621 	.ndo_get_vf_stats	= i40e_get_vf_stats,
13622 	.ndo_set_vf_rate	= i40e_ndo_set_vf_bw,
13623 	.ndo_get_vf_config	= i40e_ndo_get_vf_config,
13624 	.ndo_set_vf_link_state	= i40e_ndo_set_vf_link_state,
13625 	.ndo_set_vf_spoofchk	= i40e_ndo_set_vf_spoofchk,
13626 	.ndo_set_vf_trust	= i40e_ndo_set_vf_trust,
13627 	.ndo_get_phys_port_id	= i40e_get_phys_port_id,
13628 	.ndo_fdb_add		= i40e_ndo_fdb_add,
13629 	.ndo_features_check	= i40e_features_check,
13630 	.ndo_bridge_getlink	= i40e_ndo_bridge_getlink,
13631 	.ndo_bridge_setlink	= i40e_ndo_bridge_setlink,
13632 	.ndo_bpf		= i40e_xdp,
13633 	.ndo_xdp_xmit		= i40e_xdp_xmit,
13634 	.ndo_xsk_wakeup	        = i40e_xsk_wakeup,
13635 	.ndo_dfwd_add_station	= i40e_fwd_add,
13636 	.ndo_dfwd_del_station	= i40e_fwd_del,
13637 };
13638 
13639 /**
13640  * i40e_config_netdev - Setup the netdev flags
13641  * @vsi: the VSI being configured
13642  *
13643  * Returns 0 on success, negative value on failure
13644  **/
13645 static int i40e_config_netdev(struct i40e_vsi *vsi)
13646 {
13647 	struct i40e_pf *pf = vsi->back;
13648 	struct i40e_hw *hw = &pf->hw;
13649 	struct i40e_netdev_priv *np;
13650 	struct net_device *netdev;
13651 	u8 broadcast[ETH_ALEN];
13652 	u8 mac_addr[ETH_ALEN];
13653 	int etherdev_size;
13654 	netdev_features_t hw_enc_features;
13655 	netdev_features_t hw_features;
13656 
13657 	etherdev_size = sizeof(struct i40e_netdev_priv);
13658 	netdev = alloc_etherdev_mq(etherdev_size, vsi->alloc_queue_pairs);
13659 	if (!netdev)
13660 		return -ENOMEM;
13661 
13662 	vsi->netdev = netdev;
13663 	np = netdev_priv(netdev);
13664 	np->vsi = vsi;
13665 
13666 	hw_enc_features = NETIF_F_SG			|
13667 			  NETIF_F_HW_CSUM		|
13668 			  NETIF_F_HIGHDMA		|
13669 			  NETIF_F_SOFT_FEATURES		|
13670 			  NETIF_F_TSO			|
13671 			  NETIF_F_TSO_ECN		|
13672 			  NETIF_F_TSO6			|
13673 			  NETIF_F_GSO_GRE		|
13674 			  NETIF_F_GSO_GRE_CSUM		|
13675 			  NETIF_F_GSO_PARTIAL		|
13676 			  NETIF_F_GSO_IPXIP4		|
13677 			  NETIF_F_GSO_IPXIP6		|
13678 			  NETIF_F_GSO_UDP_TUNNEL	|
13679 			  NETIF_F_GSO_UDP_TUNNEL_CSUM	|
13680 			  NETIF_F_GSO_UDP_L4		|
13681 			  NETIF_F_SCTP_CRC		|
13682 			  NETIF_F_RXHASH		|
13683 			  NETIF_F_RXCSUM		|
13684 			  0;
13685 
13686 	if (!(pf->hw_features & I40E_HW_OUTER_UDP_CSUM_CAPABLE))
13687 		netdev->gso_partial_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM;
13688 
13689 	netdev->udp_tunnel_nic_info = &pf->udp_tunnel_nic;
13690 
13691 	netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM;
13692 
13693 	netdev->hw_enc_features |= hw_enc_features;
13694 
13695 	/* record features VLANs can make use of */
13696 	netdev->vlan_features |= hw_enc_features | NETIF_F_TSO_MANGLEID;
13697 
13698 #define I40E_GSO_PARTIAL_FEATURES (NETIF_F_GSO_GRE |		\
13699 				   NETIF_F_GSO_GRE_CSUM |	\
13700 				   NETIF_F_GSO_IPXIP4 |		\
13701 				   NETIF_F_GSO_IPXIP6 |		\
13702 				   NETIF_F_GSO_UDP_TUNNEL |	\
13703 				   NETIF_F_GSO_UDP_TUNNEL_CSUM)
13704 
13705 	netdev->gso_partial_features = I40E_GSO_PARTIAL_FEATURES;
13706 	netdev->features |= NETIF_F_GSO_PARTIAL |
13707 			    I40E_GSO_PARTIAL_FEATURES;
13708 
13709 	netdev->mpls_features |= NETIF_F_SG;
13710 	netdev->mpls_features |= NETIF_F_HW_CSUM;
13711 	netdev->mpls_features |= NETIF_F_TSO;
13712 	netdev->mpls_features |= NETIF_F_TSO6;
13713 	netdev->mpls_features |= I40E_GSO_PARTIAL_FEATURES;
13714 
13715 	/* enable macvlan offloads */
13716 	netdev->hw_features |= NETIF_F_HW_L2FW_DOFFLOAD;
13717 
13718 	hw_features = hw_enc_features		|
13719 		      NETIF_F_HW_VLAN_CTAG_TX	|
13720 		      NETIF_F_HW_VLAN_CTAG_RX;
13721 
13722 	if (!(pf->flags & I40E_FLAG_MFP_ENABLED))
13723 		hw_features |= NETIF_F_NTUPLE | NETIF_F_HW_TC;
13724 
13725 	netdev->hw_features |= hw_features;
13726 
13727 	netdev->features |= hw_features | NETIF_F_HW_VLAN_CTAG_FILTER;
13728 	netdev->hw_enc_features |= NETIF_F_TSO_MANGLEID;
13729 
13730 	netdev->features &= ~NETIF_F_HW_TC;
13731 
13732 	if (vsi->type == I40E_VSI_MAIN) {
13733 		SET_NETDEV_DEV(netdev, &pf->pdev->dev);
13734 		ether_addr_copy(mac_addr, hw->mac.perm_addr);
13735 		/* The following steps are necessary for two reasons. First,
13736 		 * some older NVM configurations load a default MAC-VLAN
13737 		 * filter that will accept any tagged packet, and we want to
13738 		 * replace this with a normal filter. Additionally, it is
13739 		 * possible our MAC address was provided by the platform using
13740 		 * Open Firmware or similar.
13741 		 *
13742 		 * Thus, we need to remove the default filter and install one
13743 		 * specific to the MAC address.
13744 		 */
13745 		i40e_rm_default_mac_filter(vsi, mac_addr);
13746 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13747 		i40e_add_mac_filter(vsi, mac_addr);
13748 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13749 	} else {
13750 		/* Relate the VSI_VMDQ name to the VSI_MAIN name. Note that we
13751 		 * are still limited by IFNAMSIZ, but we're adding 'v%d\0' to
13752 		 * the end, which is 4 bytes long, so force truncation of the
13753 		 * original name by IFNAMSIZ - 4
13754 		 */
13755 		snprintf(netdev->name, IFNAMSIZ, "%.*sv%%d",
13756 			 IFNAMSIZ - 4,
13757 			 pf->vsi[pf->lan_vsi]->netdev->name);
13758 		eth_random_addr(mac_addr);
13759 
13760 		spin_lock_bh(&vsi->mac_filter_hash_lock);
13761 		i40e_add_mac_filter(vsi, mac_addr);
13762 		spin_unlock_bh(&vsi->mac_filter_hash_lock);
13763 	}
13764 
13765 	/* Add the broadcast filter so that we initially will receive
13766 	 * broadcast packets. Note that when a new VLAN is first added the
13767 	 * driver will convert all filters marked I40E_VLAN_ANY into VLAN
13768 	 * specific filters as part of transitioning into "vlan" operation.
13769 	 * When more VLANs are added, the driver will copy each existing MAC
13770 	 * filter and add it for the new VLAN.
13771 	 *
13772 	 * Broadcast filters are handled specially by
13773 	 * i40e_sync_filters_subtask, as the driver must to set the broadcast
13774 	 * promiscuous bit instead of adding this directly as a MAC/VLAN
13775 	 * filter. The subtask will update the correct broadcast promiscuous
13776 	 * bits as VLANs become active or inactive.
13777 	 */
13778 	eth_broadcast_addr(broadcast);
13779 	spin_lock_bh(&vsi->mac_filter_hash_lock);
13780 	i40e_add_mac_filter(vsi, broadcast);
13781 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
13782 
13783 	eth_hw_addr_set(netdev, mac_addr);
13784 	ether_addr_copy(netdev->perm_addr, mac_addr);
13785 
13786 	/* i40iw_net_event() reads 16 bytes from neigh->primary_key */
13787 	netdev->neigh_priv_len = sizeof(u32) * 4;
13788 
13789 	netdev->priv_flags |= IFF_UNICAST_FLT;
13790 	netdev->priv_flags |= IFF_SUPP_NOFCS;
13791 	/* Setup netdev TC information */
13792 	i40e_vsi_config_netdev_tc(vsi, vsi->tc_config.enabled_tc);
13793 
13794 	netdev->netdev_ops = &i40e_netdev_ops;
13795 	netdev->watchdog_timeo = 5 * HZ;
13796 	i40e_set_ethtool_ops(netdev);
13797 
13798 	/* MTU range: 68 - 9706 */
13799 	netdev->min_mtu = ETH_MIN_MTU;
13800 	netdev->max_mtu = I40E_MAX_RXBUFFER - I40E_PACKET_HDR_PAD;
13801 
13802 	return 0;
13803 }
13804 
13805 /**
13806  * i40e_vsi_delete - Delete a VSI from the switch
13807  * @vsi: the VSI being removed
13808  *
13809  * Returns 0 on success, negative value on failure
13810  **/
13811 static void i40e_vsi_delete(struct i40e_vsi *vsi)
13812 {
13813 	/* remove default VSI is not allowed */
13814 	if (vsi == vsi->back->vsi[vsi->back->lan_vsi])
13815 		return;
13816 
13817 	i40e_aq_delete_element(&vsi->back->hw, vsi->seid, NULL);
13818 }
13819 
13820 /**
13821  * i40e_is_vsi_uplink_mode_veb - Check if the VSI's uplink bridge mode is VEB
13822  * @vsi: the VSI being queried
13823  *
13824  * Returns 1 if HW bridge mode is VEB and return 0 in case of VEPA mode
13825  **/
13826 int i40e_is_vsi_uplink_mode_veb(struct i40e_vsi *vsi)
13827 {
13828 	struct i40e_veb *veb;
13829 	struct i40e_pf *pf = vsi->back;
13830 
13831 	/* Uplink is not a bridge so default to VEB */
13832 	if (vsi->veb_idx >= I40E_MAX_VEB)
13833 		return 1;
13834 
13835 	veb = pf->veb[vsi->veb_idx];
13836 	if (!veb) {
13837 		dev_info(&pf->pdev->dev,
13838 			 "There is no veb associated with the bridge\n");
13839 		return -ENOENT;
13840 	}
13841 
13842 	/* Uplink is a bridge in VEPA mode */
13843 	if (veb->bridge_mode & BRIDGE_MODE_VEPA) {
13844 		return 0;
13845 	} else {
13846 		/* Uplink is a bridge in VEB mode */
13847 		return 1;
13848 	}
13849 
13850 	/* VEPA is now default bridge, so return 0 */
13851 	return 0;
13852 }
13853 
13854 /**
13855  * i40e_add_vsi - Add a VSI to the switch
13856  * @vsi: the VSI being configured
13857  *
13858  * This initializes a VSI context depending on the VSI type to be added and
13859  * passes it down to the add_vsi aq command.
13860  **/
13861 static int i40e_add_vsi(struct i40e_vsi *vsi)
13862 {
13863 	int ret = -ENODEV;
13864 	struct i40e_pf *pf = vsi->back;
13865 	struct i40e_hw *hw = &pf->hw;
13866 	struct i40e_vsi_context ctxt;
13867 	struct i40e_mac_filter *f;
13868 	struct hlist_node *h;
13869 	int bkt;
13870 
13871 	u8 enabled_tc = 0x1; /* TC0 enabled */
13872 	int f_count = 0;
13873 
13874 	memset(&ctxt, 0, sizeof(ctxt));
13875 	switch (vsi->type) {
13876 	case I40E_VSI_MAIN:
13877 		/* The PF's main VSI is already setup as part of the
13878 		 * device initialization, so we'll not bother with
13879 		 * the add_vsi call, but we will retrieve the current
13880 		 * VSI context.
13881 		 */
13882 		ctxt.seid = pf->main_vsi_seid;
13883 		ctxt.pf_num = pf->hw.pf_id;
13884 		ctxt.vf_num = 0;
13885 		ret = i40e_aq_get_vsi_params(&pf->hw, &ctxt, NULL);
13886 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13887 		if (ret) {
13888 			dev_info(&pf->pdev->dev,
13889 				 "couldn't get PF vsi config, err %s aq_err %s\n",
13890 				 i40e_stat_str(&pf->hw, ret),
13891 				 i40e_aq_str(&pf->hw,
13892 					     pf->hw.aq.asq_last_status));
13893 			return -ENOENT;
13894 		}
13895 		vsi->info = ctxt.info;
13896 		vsi->info.valid_sections = 0;
13897 
13898 		vsi->seid = ctxt.seid;
13899 		vsi->id = ctxt.vsi_number;
13900 
13901 		enabled_tc = i40e_pf_get_tc_map(pf);
13902 
13903 		/* Source pruning is enabled by default, so the flag is
13904 		 * negative logic - if it's set, we need to fiddle with
13905 		 * the VSI to disable source pruning.
13906 		 */
13907 		if (pf->flags & I40E_FLAG_SOURCE_PRUNING_DISABLED) {
13908 			memset(&ctxt, 0, sizeof(ctxt));
13909 			ctxt.seid = pf->main_vsi_seid;
13910 			ctxt.pf_num = pf->hw.pf_id;
13911 			ctxt.vf_num = 0;
13912 			ctxt.info.valid_sections |=
13913 				     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13914 			ctxt.info.switch_id =
13915 				   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_LOCAL_LB);
13916 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13917 			if (ret) {
13918 				dev_info(&pf->pdev->dev,
13919 					 "update vsi failed, err %s aq_err %s\n",
13920 					 i40e_stat_str(&pf->hw, ret),
13921 					 i40e_aq_str(&pf->hw,
13922 						     pf->hw.aq.asq_last_status));
13923 				ret = -ENOENT;
13924 				goto err;
13925 			}
13926 		}
13927 
13928 		/* MFP mode setup queue map and update VSI */
13929 		if ((pf->flags & I40E_FLAG_MFP_ENABLED) &&
13930 		    !(pf->hw.func_caps.iscsi)) { /* NIC type PF */
13931 			memset(&ctxt, 0, sizeof(ctxt));
13932 			ctxt.seid = pf->main_vsi_seid;
13933 			ctxt.pf_num = pf->hw.pf_id;
13934 			ctxt.vf_num = 0;
13935 			i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, false);
13936 			ret = i40e_aq_update_vsi_params(hw, &ctxt, NULL);
13937 			if (ret) {
13938 				dev_info(&pf->pdev->dev,
13939 					 "update vsi failed, err %s aq_err %s\n",
13940 					 i40e_stat_str(&pf->hw, ret),
13941 					 i40e_aq_str(&pf->hw,
13942 						    pf->hw.aq.asq_last_status));
13943 				ret = -ENOENT;
13944 				goto err;
13945 			}
13946 			/* update the local VSI info queue map */
13947 			i40e_vsi_update_queue_map(vsi, &ctxt);
13948 			vsi->info.valid_sections = 0;
13949 		} else {
13950 			/* Default/Main VSI is only enabled for TC0
13951 			 * reconfigure it to enable all TCs that are
13952 			 * available on the port in SFP mode.
13953 			 * For MFP case the iSCSI PF would use this
13954 			 * flow to enable LAN+iSCSI TC.
13955 			 */
13956 			ret = i40e_vsi_config_tc(vsi, enabled_tc);
13957 			if (ret) {
13958 				/* Single TC condition is not fatal,
13959 				 * message and continue
13960 				 */
13961 				dev_info(&pf->pdev->dev,
13962 					 "failed to configure TCs for main VSI tc_map 0x%08x, err %s aq_err %s\n",
13963 					 enabled_tc,
13964 					 i40e_stat_str(&pf->hw, ret),
13965 					 i40e_aq_str(&pf->hw,
13966 						    pf->hw.aq.asq_last_status));
13967 			}
13968 		}
13969 		break;
13970 
13971 	case I40E_VSI_FDIR:
13972 		ctxt.pf_num = hw->pf_id;
13973 		ctxt.vf_num = 0;
13974 		ctxt.uplink_seid = vsi->uplink_seid;
13975 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13976 		ctxt.flags = I40E_AQ_VSI_TYPE_PF;
13977 		if ((pf->flags & I40E_FLAG_VEB_MODE_ENABLED) &&
13978 		    (i40e_is_vsi_uplink_mode_veb(vsi))) {
13979 			ctxt.info.valid_sections |=
13980 			     cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
13981 			ctxt.info.switch_id =
13982 			   cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
13983 		}
13984 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
13985 		break;
13986 
13987 	case I40E_VSI_VMDQ2:
13988 		ctxt.pf_num = hw->pf_id;
13989 		ctxt.vf_num = 0;
13990 		ctxt.uplink_seid = vsi->uplink_seid;
13991 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
13992 		ctxt.flags = I40E_AQ_VSI_TYPE_VMDQ2;
13993 
13994 		/* This VSI is connected to VEB so the switch_id
13995 		 * should be set to zero by default.
13996 		 */
13997 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
13998 			ctxt.info.valid_sections |=
13999 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14000 			ctxt.info.switch_id =
14001 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14002 		}
14003 
14004 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14005 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14006 		break;
14007 
14008 	case I40E_VSI_SRIOV:
14009 		ctxt.pf_num = hw->pf_id;
14010 		ctxt.vf_num = vsi->vf_id + hw->func_caps.vf_base_id;
14011 		ctxt.uplink_seid = vsi->uplink_seid;
14012 		ctxt.connection_type = I40E_AQ_VSI_CONN_TYPE_NORMAL;
14013 		ctxt.flags = I40E_AQ_VSI_TYPE_VF;
14014 
14015 		/* This VSI is connected to VEB so the switch_id
14016 		 * should be set to zero by default.
14017 		 */
14018 		if (i40e_is_vsi_uplink_mode_veb(vsi)) {
14019 			ctxt.info.valid_sections |=
14020 				cpu_to_le16(I40E_AQ_VSI_PROP_SWITCH_VALID);
14021 			ctxt.info.switch_id =
14022 				cpu_to_le16(I40E_AQ_VSI_SW_ID_FLAG_ALLOW_LB);
14023 		}
14024 
14025 		if (vsi->back->flags & I40E_FLAG_IWARP_ENABLED) {
14026 			ctxt.info.valid_sections |=
14027 				cpu_to_le16(I40E_AQ_VSI_PROP_QUEUE_OPT_VALID);
14028 			ctxt.info.queueing_opt_flags |=
14029 				(I40E_AQ_VSI_QUE_OPT_TCP_ENA |
14030 				 I40E_AQ_VSI_QUE_OPT_RSS_LUT_VSI);
14031 		}
14032 
14033 		ctxt.info.valid_sections |= cpu_to_le16(I40E_AQ_VSI_PROP_VLAN_VALID);
14034 		ctxt.info.port_vlan_flags |= I40E_AQ_VSI_PVLAN_MODE_ALL;
14035 		if (pf->vf[vsi->vf_id].spoofchk) {
14036 			ctxt.info.valid_sections |=
14037 				cpu_to_le16(I40E_AQ_VSI_PROP_SECURITY_VALID);
14038 			ctxt.info.sec_flags |=
14039 				(I40E_AQ_VSI_SEC_FLAG_ENABLE_VLAN_CHK |
14040 				 I40E_AQ_VSI_SEC_FLAG_ENABLE_MAC_CHK);
14041 		}
14042 		/* Setup the VSI tx/rx queue map for TC0 only for now */
14043 		i40e_vsi_setup_queue_map(vsi, &ctxt, enabled_tc, true);
14044 		break;
14045 
14046 	case I40E_VSI_IWARP:
14047 		/* send down message to iWARP */
14048 		break;
14049 
14050 	default:
14051 		return -ENODEV;
14052 	}
14053 
14054 	if (vsi->type != I40E_VSI_MAIN) {
14055 		ret = i40e_aq_add_vsi(hw, &ctxt, NULL);
14056 		if (ret) {
14057 			dev_info(&vsi->back->pdev->dev,
14058 				 "add vsi failed, err %s aq_err %s\n",
14059 				 i40e_stat_str(&pf->hw, ret),
14060 				 i40e_aq_str(&pf->hw,
14061 					     pf->hw.aq.asq_last_status));
14062 			ret = -ENOENT;
14063 			goto err;
14064 		}
14065 		vsi->info = ctxt.info;
14066 		vsi->info.valid_sections = 0;
14067 		vsi->seid = ctxt.seid;
14068 		vsi->id = ctxt.vsi_number;
14069 	}
14070 
14071 	vsi->active_filters = 0;
14072 	clear_bit(__I40E_VSI_OVERFLOW_PROMISC, vsi->state);
14073 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14074 	/* If macvlan filters already exist, force them to get loaded */
14075 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist) {
14076 		f->state = I40E_FILTER_NEW;
14077 		f_count++;
14078 	}
14079 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14080 
14081 	if (f_count) {
14082 		vsi->flags |= I40E_VSI_FLAG_FILTER_CHANGED;
14083 		set_bit(__I40E_MACVLAN_SYNC_PENDING, pf->state);
14084 	}
14085 
14086 	/* Update VSI BW information */
14087 	ret = i40e_vsi_get_bw_info(vsi);
14088 	if (ret) {
14089 		dev_info(&pf->pdev->dev,
14090 			 "couldn't get vsi bw info, err %s aq_err %s\n",
14091 			 i40e_stat_str(&pf->hw, ret),
14092 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14093 		/* VSI is already added so not tearing that up */
14094 		ret = 0;
14095 	}
14096 
14097 err:
14098 	return ret;
14099 }
14100 
14101 /**
14102  * i40e_vsi_release - Delete a VSI and free its resources
14103  * @vsi: the VSI being removed
14104  *
14105  * Returns 0 on success or < 0 on error
14106  **/
14107 int i40e_vsi_release(struct i40e_vsi *vsi)
14108 {
14109 	struct i40e_mac_filter *f;
14110 	struct hlist_node *h;
14111 	struct i40e_veb *veb = NULL;
14112 	struct i40e_pf *pf;
14113 	u16 uplink_seid;
14114 	int i, n, bkt;
14115 
14116 	pf = vsi->back;
14117 
14118 	/* release of a VEB-owner or last VSI is not allowed */
14119 	if (vsi->flags & I40E_VSI_FLAG_VEB_OWNER) {
14120 		dev_info(&pf->pdev->dev, "VSI %d has existing VEB %d\n",
14121 			 vsi->seid, vsi->uplink_seid);
14122 		return -ENODEV;
14123 	}
14124 	if (vsi == pf->vsi[pf->lan_vsi] &&
14125 	    !test_bit(__I40E_DOWN, pf->state)) {
14126 		dev_info(&pf->pdev->dev, "Can't remove PF VSI\n");
14127 		return -ENODEV;
14128 	}
14129 	set_bit(__I40E_VSI_RELEASING, vsi->state);
14130 	uplink_seid = vsi->uplink_seid;
14131 	if (vsi->type != I40E_VSI_SRIOV) {
14132 		if (vsi->netdev_registered) {
14133 			vsi->netdev_registered = false;
14134 			if (vsi->netdev) {
14135 				/* results in a call to i40e_close() */
14136 				unregister_netdev(vsi->netdev);
14137 			}
14138 		} else {
14139 			i40e_vsi_close(vsi);
14140 		}
14141 		i40e_vsi_disable_irq(vsi);
14142 	}
14143 
14144 	spin_lock_bh(&vsi->mac_filter_hash_lock);
14145 
14146 	/* clear the sync flag on all filters */
14147 	if (vsi->netdev) {
14148 		__dev_uc_unsync(vsi->netdev, NULL);
14149 		__dev_mc_unsync(vsi->netdev, NULL);
14150 	}
14151 
14152 	/* make sure any remaining filters are marked for deletion */
14153 	hash_for_each_safe(vsi->mac_filter_hash, bkt, h, f, hlist)
14154 		__i40e_del_filter(vsi, f);
14155 
14156 	spin_unlock_bh(&vsi->mac_filter_hash_lock);
14157 
14158 	i40e_sync_vsi_filters(vsi);
14159 
14160 	i40e_vsi_delete(vsi);
14161 	i40e_vsi_free_q_vectors(vsi);
14162 	if (vsi->netdev) {
14163 		free_netdev(vsi->netdev);
14164 		vsi->netdev = NULL;
14165 	}
14166 	i40e_vsi_clear_rings(vsi);
14167 	i40e_vsi_clear(vsi);
14168 
14169 	/* If this was the last thing on the VEB, except for the
14170 	 * controlling VSI, remove the VEB, which puts the controlling
14171 	 * VSI onto the next level down in the switch.
14172 	 *
14173 	 * Well, okay, there's one more exception here: don't remove
14174 	 * the orphan VEBs yet.  We'll wait for an explicit remove request
14175 	 * from up the network stack.
14176 	 */
14177 	for (n = 0, i = 0; i < pf->num_alloc_vsi; i++) {
14178 		if (pf->vsi[i] &&
14179 		    pf->vsi[i]->uplink_seid == uplink_seid &&
14180 		    (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14181 			n++;      /* count the VSIs */
14182 		}
14183 	}
14184 	for (i = 0; i < I40E_MAX_VEB; i++) {
14185 		if (!pf->veb[i])
14186 			continue;
14187 		if (pf->veb[i]->uplink_seid == uplink_seid)
14188 			n++;     /* count the VEBs */
14189 		if (pf->veb[i]->seid == uplink_seid)
14190 			veb = pf->veb[i];
14191 	}
14192 	if (n == 0 && veb && veb->uplink_seid != 0)
14193 		i40e_veb_release(veb);
14194 
14195 	return 0;
14196 }
14197 
14198 /**
14199  * i40e_vsi_setup_vectors - Set up the q_vectors for the given VSI
14200  * @vsi: ptr to the VSI
14201  *
14202  * This should only be called after i40e_vsi_mem_alloc() which allocates the
14203  * corresponding SW VSI structure and initializes num_queue_pairs for the
14204  * newly allocated VSI.
14205  *
14206  * Returns 0 on success or negative on failure
14207  **/
14208 static int i40e_vsi_setup_vectors(struct i40e_vsi *vsi)
14209 {
14210 	int ret = -ENOENT;
14211 	struct i40e_pf *pf = vsi->back;
14212 
14213 	if (vsi->q_vectors[0]) {
14214 		dev_info(&pf->pdev->dev, "VSI %d has existing q_vectors\n",
14215 			 vsi->seid);
14216 		return -EEXIST;
14217 	}
14218 
14219 	if (vsi->base_vector) {
14220 		dev_info(&pf->pdev->dev, "VSI %d has non-zero base vector %d\n",
14221 			 vsi->seid, vsi->base_vector);
14222 		return -EEXIST;
14223 	}
14224 
14225 	ret = i40e_vsi_alloc_q_vectors(vsi);
14226 	if (ret) {
14227 		dev_info(&pf->pdev->dev,
14228 			 "failed to allocate %d q_vector for VSI %d, ret=%d\n",
14229 			 vsi->num_q_vectors, vsi->seid, ret);
14230 		vsi->num_q_vectors = 0;
14231 		goto vector_setup_out;
14232 	}
14233 
14234 	/* In Legacy mode, we do not have to get any other vector since we
14235 	 * piggyback on the misc/ICR0 for queue interrupts.
14236 	*/
14237 	if (!(pf->flags & I40E_FLAG_MSIX_ENABLED))
14238 		return ret;
14239 	if (vsi->num_q_vectors)
14240 		vsi->base_vector = i40e_get_lump(pf, pf->irq_pile,
14241 						 vsi->num_q_vectors, vsi->idx);
14242 	if (vsi->base_vector < 0) {
14243 		dev_info(&pf->pdev->dev,
14244 			 "failed to get tracking for %d vectors for VSI %d, err=%d\n",
14245 			 vsi->num_q_vectors, vsi->seid, vsi->base_vector);
14246 		i40e_vsi_free_q_vectors(vsi);
14247 		ret = -ENOENT;
14248 		goto vector_setup_out;
14249 	}
14250 
14251 vector_setup_out:
14252 	return ret;
14253 }
14254 
14255 /**
14256  * i40e_vsi_reinit_setup - return and reallocate resources for a VSI
14257  * @vsi: pointer to the vsi.
14258  *
14259  * This re-allocates a vsi's queue resources.
14260  *
14261  * Returns pointer to the successfully allocated and configured VSI sw struct
14262  * on success, otherwise returns NULL on failure.
14263  **/
14264 static struct i40e_vsi *i40e_vsi_reinit_setup(struct i40e_vsi *vsi)
14265 {
14266 	u16 alloc_queue_pairs;
14267 	struct i40e_pf *pf;
14268 	u8 enabled_tc;
14269 	int ret;
14270 
14271 	if (!vsi)
14272 		return NULL;
14273 
14274 	pf = vsi->back;
14275 
14276 	i40e_put_lump(pf->qp_pile, vsi->base_queue, vsi->idx);
14277 	i40e_vsi_clear_rings(vsi);
14278 
14279 	i40e_vsi_free_arrays(vsi, false);
14280 	i40e_set_num_rings_in_vsi(vsi);
14281 	ret = i40e_vsi_alloc_arrays(vsi, false);
14282 	if (ret)
14283 		goto err_vsi;
14284 
14285 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14286 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14287 
14288 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14289 	if (ret < 0) {
14290 		dev_info(&pf->pdev->dev,
14291 			 "failed to get tracking for %d queues for VSI %d err %d\n",
14292 			 alloc_queue_pairs, vsi->seid, ret);
14293 		goto err_vsi;
14294 	}
14295 	vsi->base_queue = ret;
14296 
14297 	/* Update the FW view of the VSI. Force a reset of TC and queue
14298 	 * layout configurations.
14299 	 */
14300 	enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
14301 	pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
14302 	pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
14303 	i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
14304 	if (vsi->type == I40E_VSI_MAIN)
14305 		i40e_rm_default_mac_filter(vsi, pf->hw.mac.perm_addr);
14306 
14307 	/* assign it some queues */
14308 	ret = i40e_alloc_rings(vsi);
14309 	if (ret)
14310 		goto err_rings;
14311 
14312 	/* map all of the rings to the q_vectors */
14313 	i40e_vsi_map_rings_to_vectors(vsi);
14314 	return vsi;
14315 
14316 err_rings:
14317 	i40e_vsi_free_q_vectors(vsi);
14318 	if (vsi->netdev_registered) {
14319 		vsi->netdev_registered = false;
14320 		unregister_netdev(vsi->netdev);
14321 		free_netdev(vsi->netdev);
14322 		vsi->netdev = NULL;
14323 	}
14324 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14325 err_vsi:
14326 	i40e_vsi_clear(vsi);
14327 	return NULL;
14328 }
14329 
14330 /**
14331  * i40e_vsi_setup - Set up a VSI by a given type
14332  * @pf: board private structure
14333  * @type: VSI type
14334  * @uplink_seid: the switch element to link to
14335  * @param1: usage depends upon VSI type. For VF types, indicates VF id
14336  *
14337  * This allocates the sw VSI structure and its queue resources, then add a VSI
14338  * to the identified VEB.
14339  *
14340  * Returns pointer to the successfully allocated and configure VSI sw struct on
14341  * success, otherwise returns NULL on failure.
14342  **/
14343 struct i40e_vsi *i40e_vsi_setup(struct i40e_pf *pf, u8 type,
14344 				u16 uplink_seid, u32 param1)
14345 {
14346 	struct i40e_vsi *vsi = NULL;
14347 	struct i40e_veb *veb = NULL;
14348 	u16 alloc_queue_pairs;
14349 	int ret, i;
14350 	int v_idx;
14351 
14352 	/* The requested uplink_seid must be either
14353 	 *     - the PF's port seid
14354 	 *              no VEB is needed because this is the PF
14355 	 *              or this is a Flow Director special case VSI
14356 	 *     - seid of an existing VEB
14357 	 *     - seid of a VSI that owns an existing VEB
14358 	 *     - seid of a VSI that doesn't own a VEB
14359 	 *              a new VEB is created and the VSI becomes the owner
14360 	 *     - seid of the PF VSI, which is what creates the first VEB
14361 	 *              this is a special case of the previous
14362 	 *
14363 	 * Find which uplink_seid we were given and create a new VEB if needed
14364 	 */
14365 	for (i = 0; i < I40E_MAX_VEB; i++) {
14366 		if (pf->veb[i] && pf->veb[i]->seid == uplink_seid) {
14367 			veb = pf->veb[i];
14368 			break;
14369 		}
14370 	}
14371 
14372 	if (!veb && uplink_seid != pf->mac_seid) {
14373 
14374 		for (i = 0; i < pf->num_alloc_vsi; i++) {
14375 			if (pf->vsi[i] && pf->vsi[i]->seid == uplink_seid) {
14376 				vsi = pf->vsi[i];
14377 				break;
14378 			}
14379 		}
14380 		if (!vsi) {
14381 			dev_info(&pf->pdev->dev, "no such uplink_seid %d\n",
14382 				 uplink_seid);
14383 			return NULL;
14384 		}
14385 
14386 		if (vsi->uplink_seid == pf->mac_seid)
14387 			veb = i40e_veb_setup(pf, 0, pf->mac_seid, vsi->seid,
14388 					     vsi->tc_config.enabled_tc);
14389 		else if ((vsi->flags & I40E_VSI_FLAG_VEB_OWNER) == 0)
14390 			veb = i40e_veb_setup(pf, 0, vsi->uplink_seid, vsi->seid,
14391 					     vsi->tc_config.enabled_tc);
14392 		if (veb) {
14393 			if (vsi->seid != pf->vsi[pf->lan_vsi]->seid) {
14394 				dev_info(&vsi->back->pdev->dev,
14395 					 "New VSI creation error, uplink seid of LAN VSI expected.\n");
14396 				return NULL;
14397 			}
14398 			/* We come up by default in VEPA mode if SRIOV is not
14399 			 * already enabled, in which case we can't force VEPA
14400 			 * mode.
14401 			 */
14402 			if (!(pf->flags & I40E_FLAG_VEB_MODE_ENABLED)) {
14403 				veb->bridge_mode = BRIDGE_MODE_VEPA;
14404 				pf->flags &= ~I40E_FLAG_VEB_MODE_ENABLED;
14405 			}
14406 			i40e_config_bridge_mode(veb);
14407 		}
14408 		for (i = 0; i < I40E_MAX_VEB && !veb; i++) {
14409 			if (pf->veb[i] && pf->veb[i]->seid == vsi->uplink_seid)
14410 				veb = pf->veb[i];
14411 		}
14412 		if (!veb) {
14413 			dev_info(&pf->pdev->dev, "couldn't add VEB\n");
14414 			return NULL;
14415 		}
14416 
14417 		vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14418 		uplink_seid = veb->seid;
14419 	}
14420 
14421 	/* get vsi sw struct */
14422 	v_idx = i40e_vsi_mem_alloc(pf, type);
14423 	if (v_idx < 0)
14424 		goto err_alloc;
14425 	vsi = pf->vsi[v_idx];
14426 	if (!vsi)
14427 		goto err_alloc;
14428 	vsi->type = type;
14429 	vsi->veb_idx = (veb ? veb->idx : I40E_NO_VEB);
14430 
14431 	if (type == I40E_VSI_MAIN)
14432 		pf->lan_vsi = v_idx;
14433 	else if (type == I40E_VSI_SRIOV)
14434 		vsi->vf_id = param1;
14435 	/* assign it some queues */
14436 	alloc_queue_pairs = vsi->alloc_queue_pairs *
14437 			    (i40e_enabled_xdp_vsi(vsi) ? 2 : 1);
14438 
14439 	ret = i40e_get_lump(pf, pf->qp_pile, alloc_queue_pairs, vsi->idx);
14440 	if (ret < 0) {
14441 		dev_info(&pf->pdev->dev,
14442 			 "failed to get tracking for %d queues for VSI %d err=%d\n",
14443 			 alloc_queue_pairs, vsi->seid, ret);
14444 		goto err_vsi;
14445 	}
14446 	vsi->base_queue = ret;
14447 
14448 	/* get a VSI from the hardware */
14449 	vsi->uplink_seid = uplink_seid;
14450 	ret = i40e_add_vsi(vsi);
14451 	if (ret)
14452 		goto err_vsi;
14453 
14454 	switch (vsi->type) {
14455 	/* setup the netdev if needed */
14456 	case I40E_VSI_MAIN:
14457 	case I40E_VSI_VMDQ2:
14458 		ret = i40e_config_netdev(vsi);
14459 		if (ret)
14460 			goto err_netdev;
14461 		ret = i40e_netif_set_realnum_tx_rx_queues(vsi);
14462 		if (ret)
14463 			goto err_netdev;
14464 		ret = register_netdev(vsi->netdev);
14465 		if (ret)
14466 			goto err_netdev;
14467 		vsi->netdev_registered = true;
14468 		netif_carrier_off(vsi->netdev);
14469 #ifdef CONFIG_I40E_DCB
14470 		/* Setup DCB netlink interface */
14471 		i40e_dcbnl_setup(vsi);
14472 #endif /* CONFIG_I40E_DCB */
14473 		fallthrough;
14474 	case I40E_VSI_FDIR:
14475 		/* set up vectors and rings if needed */
14476 		ret = i40e_vsi_setup_vectors(vsi);
14477 		if (ret)
14478 			goto err_msix;
14479 
14480 		ret = i40e_alloc_rings(vsi);
14481 		if (ret)
14482 			goto err_rings;
14483 
14484 		/* map all of the rings to the q_vectors */
14485 		i40e_vsi_map_rings_to_vectors(vsi);
14486 
14487 		i40e_vsi_reset_stats(vsi);
14488 		break;
14489 	default:
14490 		/* no netdev or rings for the other VSI types */
14491 		break;
14492 	}
14493 
14494 	if ((pf->hw_features & I40E_HW_RSS_AQ_CAPABLE) &&
14495 	    (vsi->type == I40E_VSI_VMDQ2)) {
14496 		ret = i40e_vsi_config_rss(vsi);
14497 	}
14498 	return vsi;
14499 
14500 err_rings:
14501 	i40e_vsi_free_q_vectors(vsi);
14502 err_msix:
14503 	if (vsi->netdev_registered) {
14504 		vsi->netdev_registered = false;
14505 		unregister_netdev(vsi->netdev);
14506 		free_netdev(vsi->netdev);
14507 		vsi->netdev = NULL;
14508 	}
14509 err_netdev:
14510 	i40e_aq_delete_element(&pf->hw, vsi->seid, NULL);
14511 err_vsi:
14512 	i40e_vsi_clear(vsi);
14513 err_alloc:
14514 	return NULL;
14515 }
14516 
14517 /**
14518  * i40e_veb_get_bw_info - Query VEB BW information
14519  * @veb: the veb to query
14520  *
14521  * Query the Tx scheduler BW configuration data for given VEB
14522  **/
14523 static int i40e_veb_get_bw_info(struct i40e_veb *veb)
14524 {
14525 	struct i40e_aqc_query_switching_comp_ets_config_resp ets_data;
14526 	struct i40e_aqc_query_switching_comp_bw_config_resp bw_data;
14527 	struct i40e_pf *pf = veb->pf;
14528 	struct i40e_hw *hw = &pf->hw;
14529 	u32 tc_bw_max;
14530 	int ret = 0;
14531 	int i;
14532 
14533 	ret = i40e_aq_query_switch_comp_bw_config(hw, veb->seid,
14534 						  &bw_data, NULL);
14535 	if (ret) {
14536 		dev_info(&pf->pdev->dev,
14537 			 "query veb bw config failed, err %s aq_err %s\n",
14538 			 i40e_stat_str(&pf->hw, ret),
14539 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14540 		goto out;
14541 	}
14542 
14543 	ret = i40e_aq_query_switch_comp_ets_config(hw, veb->seid,
14544 						   &ets_data, NULL);
14545 	if (ret) {
14546 		dev_info(&pf->pdev->dev,
14547 			 "query veb bw ets config failed, err %s aq_err %s\n",
14548 			 i40e_stat_str(&pf->hw, ret),
14549 			 i40e_aq_str(&pf->hw, hw->aq.asq_last_status));
14550 		goto out;
14551 	}
14552 
14553 	veb->bw_limit = le16_to_cpu(ets_data.port_bw_limit);
14554 	veb->bw_max_quanta = ets_data.tc_bw_max;
14555 	veb->is_abs_credits = bw_data.absolute_credits_enable;
14556 	veb->enabled_tc = ets_data.tc_valid_bits;
14557 	tc_bw_max = le16_to_cpu(bw_data.tc_bw_max[0]) |
14558 		    (le16_to_cpu(bw_data.tc_bw_max[1]) << 16);
14559 	for (i = 0; i < I40E_MAX_TRAFFIC_CLASS; i++) {
14560 		veb->bw_tc_share_credits[i] = bw_data.tc_bw_share_credits[i];
14561 		veb->bw_tc_limit_credits[i] =
14562 					le16_to_cpu(bw_data.tc_bw_limits[i]);
14563 		veb->bw_tc_max_quanta[i] = ((tc_bw_max >> (i*4)) & 0x7);
14564 	}
14565 
14566 out:
14567 	return ret;
14568 }
14569 
14570 /**
14571  * i40e_veb_mem_alloc - Allocates the next available struct veb in the PF
14572  * @pf: board private structure
14573  *
14574  * On error: returns error code (negative)
14575  * On success: returns vsi index in PF (positive)
14576  **/
14577 static int i40e_veb_mem_alloc(struct i40e_pf *pf)
14578 {
14579 	int ret = -ENOENT;
14580 	struct i40e_veb *veb;
14581 	int i;
14582 
14583 	/* Need to protect the allocation of switch elements at the PF level */
14584 	mutex_lock(&pf->switch_mutex);
14585 
14586 	/* VEB list may be fragmented if VEB creation/destruction has
14587 	 * been happening.  We can afford to do a quick scan to look
14588 	 * for any free slots in the list.
14589 	 *
14590 	 * find next empty veb slot, looping back around if necessary
14591 	 */
14592 	i = 0;
14593 	while ((i < I40E_MAX_VEB) && (pf->veb[i] != NULL))
14594 		i++;
14595 	if (i >= I40E_MAX_VEB) {
14596 		ret = -ENOMEM;
14597 		goto err_alloc_veb;  /* out of VEB slots! */
14598 	}
14599 
14600 	veb = kzalloc(sizeof(*veb), GFP_KERNEL);
14601 	if (!veb) {
14602 		ret = -ENOMEM;
14603 		goto err_alloc_veb;
14604 	}
14605 	veb->pf = pf;
14606 	veb->idx = i;
14607 	veb->enabled_tc = 1;
14608 
14609 	pf->veb[i] = veb;
14610 	ret = i;
14611 err_alloc_veb:
14612 	mutex_unlock(&pf->switch_mutex);
14613 	return ret;
14614 }
14615 
14616 /**
14617  * i40e_switch_branch_release - Delete a branch of the switch tree
14618  * @branch: where to start deleting
14619  *
14620  * This uses recursion to find the tips of the branch to be
14621  * removed, deleting until we get back to and can delete this VEB.
14622  **/
14623 static void i40e_switch_branch_release(struct i40e_veb *branch)
14624 {
14625 	struct i40e_pf *pf = branch->pf;
14626 	u16 branch_seid = branch->seid;
14627 	u16 veb_idx = branch->idx;
14628 	int i;
14629 
14630 	/* release any VEBs on this VEB - RECURSION */
14631 	for (i = 0; i < I40E_MAX_VEB; i++) {
14632 		if (!pf->veb[i])
14633 			continue;
14634 		if (pf->veb[i]->uplink_seid == branch->seid)
14635 			i40e_switch_branch_release(pf->veb[i]);
14636 	}
14637 
14638 	/* Release the VSIs on this VEB, but not the owner VSI.
14639 	 *
14640 	 * NOTE: Removing the last VSI on a VEB has the SIDE EFFECT of removing
14641 	 *       the VEB itself, so don't use (*branch) after this loop.
14642 	 */
14643 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14644 		if (!pf->vsi[i])
14645 			continue;
14646 		if (pf->vsi[i]->uplink_seid == branch_seid &&
14647 		   (pf->vsi[i]->flags & I40E_VSI_FLAG_VEB_OWNER) == 0) {
14648 			i40e_vsi_release(pf->vsi[i]);
14649 		}
14650 	}
14651 
14652 	/* There's one corner case where the VEB might not have been
14653 	 * removed, so double check it here and remove it if needed.
14654 	 * This case happens if the veb was created from the debugfs
14655 	 * commands and no VSIs were added to it.
14656 	 */
14657 	if (pf->veb[veb_idx])
14658 		i40e_veb_release(pf->veb[veb_idx]);
14659 }
14660 
14661 /**
14662  * i40e_veb_clear - remove veb struct
14663  * @veb: the veb to remove
14664  **/
14665 static void i40e_veb_clear(struct i40e_veb *veb)
14666 {
14667 	if (!veb)
14668 		return;
14669 
14670 	if (veb->pf) {
14671 		struct i40e_pf *pf = veb->pf;
14672 
14673 		mutex_lock(&pf->switch_mutex);
14674 		if (pf->veb[veb->idx] == veb)
14675 			pf->veb[veb->idx] = NULL;
14676 		mutex_unlock(&pf->switch_mutex);
14677 	}
14678 
14679 	kfree(veb);
14680 }
14681 
14682 /**
14683  * i40e_veb_release - Delete a VEB and free its resources
14684  * @veb: the VEB being removed
14685  **/
14686 void i40e_veb_release(struct i40e_veb *veb)
14687 {
14688 	struct i40e_vsi *vsi = NULL;
14689 	struct i40e_pf *pf;
14690 	int i, n = 0;
14691 
14692 	pf = veb->pf;
14693 
14694 	/* find the remaining VSI and check for extras */
14695 	for (i = 0; i < pf->num_alloc_vsi; i++) {
14696 		if (pf->vsi[i] && pf->vsi[i]->uplink_seid == veb->seid) {
14697 			n++;
14698 			vsi = pf->vsi[i];
14699 		}
14700 	}
14701 	if (n != 1) {
14702 		dev_info(&pf->pdev->dev,
14703 			 "can't remove VEB %d with %d VSIs left\n",
14704 			 veb->seid, n);
14705 		return;
14706 	}
14707 
14708 	/* move the remaining VSI to uplink veb */
14709 	vsi->flags &= ~I40E_VSI_FLAG_VEB_OWNER;
14710 	if (veb->uplink_seid) {
14711 		vsi->uplink_seid = veb->uplink_seid;
14712 		if (veb->uplink_seid == pf->mac_seid)
14713 			vsi->veb_idx = I40E_NO_VEB;
14714 		else
14715 			vsi->veb_idx = veb->veb_idx;
14716 	} else {
14717 		/* floating VEB */
14718 		vsi->uplink_seid = pf->vsi[pf->lan_vsi]->uplink_seid;
14719 		vsi->veb_idx = pf->vsi[pf->lan_vsi]->veb_idx;
14720 	}
14721 
14722 	i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14723 	i40e_veb_clear(veb);
14724 }
14725 
14726 /**
14727  * i40e_add_veb - create the VEB in the switch
14728  * @veb: the VEB to be instantiated
14729  * @vsi: the controlling VSI
14730  **/
14731 static int i40e_add_veb(struct i40e_veb *veb, struct i40e_vsi *vsi)
14732 {
14733 	struct i40e_pf *pf = veb->pf;
14734 	bool enable_stats = !!(pf->flags & I40E_FLAG_VEB_STATS_ENABLED);
14735 	int ret;
14736 
14737 	ret = i40e_aq_add_veb(&pf->hw, veb->uplink_seid, vsi->seid,
14738 			      veb->enabled_tc, false,
14739 			      &veb->seid, enable_stats, NULL);
14740 
14741 	/* get a VEB from the hardware */
14742 	if (ret) {
14743 		dev_info(&pf->pdev->dev,
14744 			 "couldn't add VEB, err %s aq_err %s\n",
14745 			 i40e_stat_str(&pf->hw, ret),
14746 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14747 		return -EPERM;
14748 	}
14749 
14750 	/* get statistics counter */
14751 	ret = i40e_aq_get_veb_parameters(&pf->hw, veb->seid, NULL, NULL,
14752 					 &veb->stats_idx, NULL, NULL, NULL);
14753 	if (ret) {
14754 		dev_info(&pf->pdev->dev,
14755 			 "couldn't get VEB statistics idx, err %s aq_err %s\n",
14756 			 i40e_stat_str(&pf->hw, ret),
14757 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14758 		return -EPERM;
14759 	}
14760 	ret = i40e_veb_get_bw_info(veb);
14761 	if (ret) {
14762 		dev_info(&pf->pdev->dev,
14763 			 "couldn't get VEB bw info, err %s aq_err %s\n",
14764 			 i40e_stat_str(&pf->hw, ret),
14765 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
14766 		i40e_aq_delete_element(&pf->hw, veb->seid, NULL);
14767 		return -ENOENT;
14768 	}
14769 
14770 	vsi->uplink_seid = veb->seid;
14771 	vsi->veb_idx = veb->idx;
14772 	vsi->flags |= I40E_VSI_FLAG_VEB_OWNER;
14773 
14774 	return 0;
14775 }
14776 
14777 /**
14778  * i40e_veb_setup - Set up a VEB
14779  * @pf: board private structure
14780  * @flags: VEB setup flags
14781  * @uplink_seid: the switch element to link to
14782  * @vsi_seid: the initial VSI seid
14783  * @enabled_tc: Enabled TC bit-map
14784  *
14785  * This allocates the sw VEB structure and links it into the switch
14786  * It is possible and legal for this to be a duplicate of an already
14787  * existing VEB.  It is also possible for both uplink and vsi seids
14788  * to be zero, in order to create a floating VEB.
14789  *
14790  * Returns pointer to the successfully allocated VEB sw struct on
14791  * success, otherwise returns NULL on failure.
14792  **/
14793 struct i40e_veb *i40e_veb_setup(struct i40e_pf *pf, u16 flags,
14794 				u16 uplink_seid, u16 vsi_seid,
14795 				u8 enabled_tc)
14796 {
14797 	struct i40e_veb *veb, *uplink_veb = NULL;
14798 	int vsi_idx, veb_idx;
14799 	int ret;
14800 
14801 	/* if one seid is 0, the other must be 0 to create a floating relay */
14802 	if ((uplink_seid == 0 || vsi_seid == 0) &&
14803 	    (uplink_seid + vsi_seid != 0)) {
14804 		dev_info(&pf->pdev->dev,
14805 			 "one, not both seid's are 0: uplink=%d vsi=%d\n",
14806 			 uplink_seid, vsi_seid);
14807 		return NULL;
14808 	}
14809 
14810 	/* make sure there is such a vsi and uplink */
14811 	for (vsi_idx = 0; vsi_idx < pf->num_alloc_vsi; vsi_idx++)
14812 		if (pf->vsi[vsi_idx] && pf->vsi[vsi_idx]->seid == vsi_seid)
14813 			break;
14814 	if (vsi_idx == pf->num_alloc_vsi && vsi_seid != 0) {
14815 		dev_info(&pf->pdev->dev, "vsi seid %d not found\n",
14816 			 vsi_seid);
14817 		return NULL;
14818 	}
14819 
14820 	if (uplink_seid && uplink_seid != pf->mac_seid) {
14821 		for (veb_idx = 0; veb_idx < I40E_MAX_VEB; veb_idx++) {
14822 			if (pf->veb[veb_idx] &&
14823 			    pf->veb[veb_idx]->seid == uplink_seid) {
14824 				uplink_veb = pf->veb[veb_idx];
14825 				break;
14826 			}
14827 		}
14828 		if (!uplink_veb) {
14829 			dev_info(&pf->pdev->dev,
14830 				 "uplink seid %d not found\n", uplink_seid);
14831 			return NULL;
14832 		}
14833 	}
14834 
14835 	/* get veb sw struct */
14836 	veb_idx = i40e_veb_mem_alloc(pf);
14837 	if (veb_idx < 0)
14838 		goto err_alloc;
14839 	veb = pf->veb[veb_idx];
14840 	veb->flags = flags;
14841 	veb->uplink_seid = uplink_seid;
14842 	veb->veb_idx = (uplink_veb ? uplink_veb->idx : I40E_NO_VEB);
14843 	veb->enabled_tc = (enabled_tc ? enabled_tc : 0x1);
14844 
14845 	/* create the VEB in the switch */
14846 	ret = i40e_add_veb(veb, pf->vsi[vsi_idx]);
14847 	if (ret)
14848 		goto err_veb;
14849 	if (vsi_idx == pf->lan_vsi)
14850 		pf->lan_veb = veb->idx;
14851 
14852 	return veb;
14853 
14854 err_veb:
14855 	i40e_veb_clear(veb);
14856 err_alloc:
14857 	return NULL;
14858 }
14859 
14860 /**
14861  * i40e_setup_pf_switch_element - set PF vars based on switch type
14862  * @pf: board private structure
14863  * @ele: element we are building info from
14864  * @num_reported: total number of elements
14865  * @printconfig: should we print the contents
14866  *
14867  * helper function to assist in extracting a few useful SEID values.
14868  **/
14869 static void i40e_setup_pf_switch_element(struct i40e_pf *pf,
14870 				struct i40e_aqc_switch_config_element_resp *ele,
14871 				u16 num_reported, bool printconfig)
14872 {
14873 	u16 downlink_seid = le16_to_cpu(ele->downlink_seid);
14874 	u16 uplink_seid = le16_to_cpu(ele->uplink_seid);
14875 	u8 element_type = ele->element_type;
14876 	u16 seid = le16_to_cpu(ele->seid);
14877 
14878 	if (printconfig)
14879 		dev_info(&pf->pdev->dev,
14880 			 "type=%d seid=%d uplink=%d downlink=%d\n",
14881 			 element_type, seid, uplink_seid, downlink_seid);
14882 
14883 	switch (element_type) {
14884 	case I40E_SWITCH_ELEMENT_TYPE_MAC:
14885 		pf->mac_seid = seid;
14886 		break;
14887 	case I40E_SWITCH_ELEMENT_TYPE_VEB:
14888 		/* Main VEB? */
14889 		if (uplink_seid != pf->mac_seid)
14890 			break;
14891 		if (pf->lan_veb >= I40E_MAX_VEB) {
14892 			int v;
14893 
14894 			/* find existing or else empty VEB */
14895 			for (v = 0; v < I40E_MAX_VEB; v++) {
14896 				if (pf->veb[v] && (pf->veb[v]->seid == seid)) {
14897 					pf->lan_veb = v;
14898 					break;
14899 				}
14900 			}
14901 			if (pf->lan_veb >= I40E_MAX_VEB) {
14902 				v = i40e_veb_mem_alloc(pf);
14903 				if (v < 0)
14904 					break;
14905 				pf->lan_veb = v;
14906 			}
14907 		}
14908 		if (pf->lan_veb >= I40E_MAX_VEB)
14909 			break;
14910 
14911 		pf->veb[pf->lan_veb]->seid = seid;
14912 		pf->veb[pf->lan_veb]->uplink_seid = pf->mac_seid;
14913 		pf->veb[pf->lan_veb]->pf = pf;
14914 		pf->veb[pf->lan_veb]->veb_idx = I40E_NO_VEB;
14915 		break;
14916 	case I40E_SWITCH_ELEMENT_TYPE_VSI:
14917 		if (num_reported != 1)
14918 			break;
14919 		/* This is immediately after a reset so we can assume this is
14920 		 * the PF's VSI
14921 		 */
14922 		pf->mac_seid = uplink_seid;
14923 		pf->pf_seid = downlink_seid;
14924 		pf->main_vsi_seid = seid;
14925 		if (printconfig)
14926 			dev_info(&pf->pdev->dev,
14927 				 "pf_seid=%d main_vsi_seid=%d\n",
14928 				 pf->pf_seid, pf->main_vsi_seid);
14929 		break;
14930 	case I40E_SWITCH_ELEMENT_TYPE_PF:
14931 	case I40E_SWITCH_ELEMENT_TYPE_VF:
14932 	case I40E_SWITCH_ELEMENT_TYPE_EMP:
14933 	case I40E_SWITCH_ELEMENT_TYPE_BMC:
14934 	case I40E_SWITCH_ELEMENT_TYPE_PE:
14935 	case I40E_SWITCH_ELEMENT_TYPE_PA:
14936 		/* ignore these for now */
14937 		break;
14938 	default:
14939 		dev_info(&pf->pdev->dev, "unknown element type=%d seid=%d\n",
14940 			 element_type, seid);
14941 		break;
14942 	}
14943 }
14944 
14945 /**
14946  * i40e_fetch_switch_configuration - Get switch config from firmware
14947  * @pf: board private structure
14948  * @printconfig: should we print the contents
14949  *
14950  * Get the current switch configuration from the device and
14951  * extract a few useful SEID values.
14952  **/
14953 int i40e_fetch_switch_configuration(struct i40e_pf *pf, bool printconfig)
14954 {
14955 	struct i40e_aqc_get_switch_config_resp *sw_config;
14956 	u16 next_seid = 0;
14957 	int ret = 0;
14958 	u8 *aq_buf;
14959 	int i;
14960 
14961 	aq_buf = kzalloc(I40E_AQ_LARGE_BUF, GFP_KERNEL);
14962 	if (!aq_buf)
14963 		return -ENOMEM;
14964 
14965 	sw_config = (struct i40e_aqc_get_switch_config_resp *)aq_buf;
14966 	do {
14967 		u16 num_reported, num_total;
14968 
14969 		ret = i40e_aq_get_switch_config(&pf->hw, sw_config,
14970 						I40E_AQ_LARGE_BUF,
14971 						&next_seid, NULL);
14972 		if (ret) {
14973 			dev_info(&pf->pdev->dev,
14974 				 "get switch config failed err %s aq_err %s\n",
14975 				 i40e_stat_str(&pf->hw, ret),
14976 				 i40e_aq_str(&pf->hw,
14977 					     pf->hw.aq.asq_last_status));
14978 			kfree(aq_buf);
14979 			return -ENOENT;
14980 		}
14981 
14982 		num_reported = le16_to_cpu(sw_config->header.num_reported);
14983 		num_total = le16_to_cpu(sw_config->header.num_total);
14984 
14985 		if (printconfig)
14986 			dev_info(&pf->pdev->dev,
14987 				 "header: %d reported %d total\n",
14988 				 num_reported, num_total);
14989 
14990 		for (i = 0; i < num_reported; i++) {
14991 			struct i40e_aqc_switch_config_element_resp *ele =
14992 				&sw_config->element[i];
14993 
14994 			i40e_setup_pf_switch_element(pf, ele, num_reported,
14995 						     printconfig);
14996 		}
14997 	} while (next_seid != 0);
14998 
14999 	kfree(aq_buf);
15000 	return ret;
15001 }
15002 
15003 /**
15004  * i40e_setup_pf_switch - Setup the HW switch on startup or after reset
15005  * @pf: board private structure
15006  * @reinit: if the Main VSI needs to re-initialized.
15007  * @lock_acquired: indicates whether or not the lock has been acquired
15008  *
15009  * Returns 0 on success, negative value on failure
15010  **/
15011 static int i40e_setup_pf_switch(struct i40e_pf *pf, bool reinit, bool lock_acquired)
15012 {
15013 	u16 flags = 0;
15014 	int ret;
15015 
15016 	/* find out what's out there already */
15017 	ret = i40e_fetch_switch_configuration(pf, false);
15018 	if (ret) {
15019 		dev_info(&pf->pdev->dev,
15020 			 "couldn't fetch switch config, err %s aq_err %s\n",
15021 			 i40e_stat_str(&pf->hw, ret),
15022 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15023 		return ret;
15024 	}
15025 	i40e_pf_reset_stats(pf);
15026 
15027 	/* set the switch config bit for the whole device to
15028 	 * support limited promisc or true promisc
15029 	 * when user requests promisc. The default is limited
15030 	 * promisc.
15031 	*/
15032 
15033 	if ((pf->hw.pf_id == 0) &&
15034 	    !(pf->flags & I40E_FLAG_TRUE_PROMISC_SUPPORT)) {
15035 		flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15036 		pf->last_sw_conf_flags = flags;
15037 	}
15038 
15039 	if (pf->hw.pf_id == 0) {
15040 		u16 valid_flags;
15041 
15042 		valid_flags = I40E_AQ_SET_SWITCH_CFG_PROMISC;
15043 		ret = i40e_aq_set_switch_config(&pf->hw, flags, valid_flags, 0,
15044 						NULL);
15045 		if (ret && pf->hw.aq.asq_last_status != I40E_AQ_RC_ESRCH) {
15046 			dev_info(&pf->pdev->dev,
15047 				 "couldn't set switch config bits, err %s aq_err %s\n",
15048 				 i40e_stat_str(&pf->hw, ret),
15049 				 i40e_aq_str(&pf->hw,
15050 					     pf->hw.aq.asq_last_status));
15051 			/* not a fatal problem, just keep going */
15052 		}
15053 		pf->last_sw_conf_valid_flags = valid_flags;
15054 	}
15055 
15056 	/* first time setup */
15057 	if (pf->lan_vsi == I40E_NO_VSI || reinit) {
15058 		struct i40e_vsi *vsi = NULL;
15059 		u16 uplink_seid;
15060 
15061 		/* Set up the PF VSI associated with the PF's main VSI
15062 		 * that is already in the HW switch
15063 		 */
15064 		if (pf->lan_veb < I40E_MAX_VEB && pf->veb[pf->lan_veb])
15065 			uplink_seid = pf->veb[pf->lan_veb]->seid;
15066 		else
15067 			uplink_seid = pf->mac_seid;
15068 		if (pf->lan_vsi == I40E_NO_VSI)
15069 			vsi = i40e_vsi_setup(pf, I40E_VSI_MAIN, uplink_seid, 0);
15070 		else if (reinit)
15071 			vsi = i40e_vsi_reinit_setup(pf->vsi[pf->lan_vsi]);
15072 		if (!vsi) {
15073 			dev_info(&pf->pdev->dev, "setup of MAIN VSI failed\n");
15074 			i40e_cloud_filter_exit(pf);
15075 			i40e_fdir_teardown(pf);
15076 			return -EAGAIN;
15077 		}
15078 	} else {
15079 		/* force a reset of TC and queue layout configurations */
15080 		u8 enabled_tc = pf->vsi[pf->lan_vsi]->tc_config.enabled_tc;
15081 
15082 		pf->vsi[pf->lan_vsi]->tc_config.enabled_tc = 0;
15083 		pf->vsi[pf->lan_vsi]->seid = pf->main_vsi_seid;
15084 		i40e_vsi_config_tc(pf->vsi[pf->lan_vsi], enabled_tc);
15085 	}
15086 	i40e_vlan_stripping_disable(pf->vsi[pf->lan_vsi]);
15087 
15088 	i40e_fdir_sb_setup(pf);
15089 
15090 	/* Setup static PF queue filter control settings */
15091 	ret = i40e_setup_pf_filter_control(pf);
15092 	if (ret) {
15093 		dev_info(&pf->pdev->dev, "setup_pf_filter_control failed: %d\n",
15094 			 ret);
15095 		/* Failure here should not stop continuing other steps */
15096 	}
15097 
15098 	/* enable RSS in the HW, even for only one queue, as the stack can use
15099 	 * the hash
15100 	 */
15101 	if ((pf->flags & I40E_FLAG_RSS_ENABLED))
15102 		i40e_pf_config_rss(pf);
15103 
15104 	/* fill in link information and enable LSE reporting */
15105 	i40e_link_event(pf);
15106 
15107 	/* Initialize user-specific link properties */
15108 	pf->fc_autoneg_status = ((pf->hw.phy.link_info.an_info &
15109 				  I40E_AQ_AN_COMPLETED) ? true : false);
15110 
15111 	i40e_ptp_init(pf);
15112 
15113 	if (!lock_acquired)
15114 		rtnl_lock();
15115 
15116 	/* repopulate tunnel port filters */
15117 	udp_tunnel_nic_reset_ntf(pf->vsi[pf->lan_vsi]->netdev);
15118 
15119 	if (!lock_acquired)
15120 		rtnl_unlock();
15121 
15122 	return ret;
15123 }
15124 
15125 /**
15126  * i40e_determine_queue_usage - Work out queue distribution
15127  * @pf: board private structure
15128  **/
15129 static void i40e_determine_queue_usage(struct i40e_pf *pf)
15130 {
15131 	int queues_left;
15132 	int q_max;
15133 
15134 	pf->num_lan_qps = 0;
15135 
15136 	/* Find the max queues to be put into basic use.  We'll always be
15137 	 * using TC0, whether or not DCB is running, and TC0 will get the
15138 	 * big RSS set.
15139 	 */
15140 	queues_left = pf->hw.func_caps.num_tx_qp;
15141 
15142 	if ((queues_left == 1) ||
15143 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED)) {
15144 		/* one qp for PF, no queues for anything else */
15145 		queues_left = 0;
15146 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15147 
15148 		/* make sure all the fancies are disabled */
15149 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15150 			       I40E_FLAG_IWARP_ENABLED	|
15151 			       I40E_FLAG_FD_SB_ENABLED	|
15152 			       I40E_FLAG_FD_ATR_ENABLED	|
15153 			       I40E_FLAG_DCB_CAPABLE	|
15154 			       I40E_FLAG_DCB_ENABLED	|
15155 			       I40E_FLAG_SRIOV_ENABLED	|
15156 			       I40E_FLAG_VMDQ_ENABLED);
15157 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15158 	} else if (!(pf->flags & (I40E_FLAG_RSS_ENABLED |
15159 				  I40E_FLAG_FD_SB_ENABLED |
15160 				  I40E_FLAG_FD_ATR_ENABLED |
15161 				  I40E_FLAG_DCB_CAPABLE))) {
15162 		/* one qp for PF */
15163 		pf->alloc_rss_size = pf->num_lan_qps = 1;
15164 		queues_left -= pf->num_lan_qps;
15165 
15166 		pf->flags &= ~(I40E_FLAG_RSS_ENABLED	|
15167 			       I40E_FLAG_IWARP_ENABLED	|
15168 			       I40E_FLAG_FD_SB_ENABLED	|
15169 			       I40E_FLAG_FD_ATR_ENABLED	|
15170 			       I40E_FLAG_DCB_ENABLED	|
15171 			       I40E_FLAG_VMDQ_ENABLED);
15172 		pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15173 	} else {
15174 		/* Not enough queues for all TCs */
15175 		if ((pf->flags & I40E_FLAG_DCB_CAPABLE) &&
15176 		    (queues_left < I40E_MAX_TRAFFIC_CLASS)) {
15177 			pf->flags &= ~(I40E_FLAG_DCB_CAPABLE |
15178 					I40E_FLAG_DCB_ENABLED);
15179 			dev_info(&pf->pdev->dev, "not enough queues for DCB. DCB is disabled.\n");
15180 		}
15181 
15182 		/* limit lan qps to the smaller of qps, cpus or msix */
15183 		q_max = max_t(int, pf->rss_size_max, num_online_cpus());
15184 		q_max = min_t(int, q_max, pf->hw.func_caps.num_tx_qp);
15185 		q_max = min_t(int, q_max, pf->hw.func_caps.num_msix_vectors);
15186 		pf->num_lan_qps = q_max;
15187 
15188 		queues_left -= pf->num_lan_qps;
15189 	}
15190 
15191 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15192 		if (queues_left > 1) {
15193 			queues_left -= 1; /* save 1 queue for FD */
15194 		} else {
15195 			pf->flags &= ~I40E_FLAG_FD_SB_ENABLED;
15196 			pf->flags |= I40E_FLAG_FD_SB_INACTIVE;
15197 			dev_info(&pf->pdev->dev, "not enough queues for Flow Director. Flow Director feature is disabled\n");
15198 		}
15199 	}
15200 
15201 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15202 	    pf->num_vf_qps && pf->num_req_vfs && queues_left) {
15203 		pf->num_req_vfs = min_t(int, pf->num_req_vfs,
15204 					(queues_left / pf->num_vf_qps));
15205 		queues_left -= (pf->num_req_vfs * pf->num_vf_qps);
15206 	}
15207 
15208 	if ((pf->flags & I40E_FLAG_VMDQ_ENABLED) &&
15209 	    pf->num_vmdq_vsis && pf->num_vmdq_qps && queues_left) {
15210 		pf->num_vmdq_vsis = min_t(int, pf->num_vmdq_vsis,
15211 					  (queues_left / pf->num_vmdq_qps));
15212 		queues_left -= (pf->num_vmdq_vsis * pf->num_vmdq_qps);
15213 	}
15214 
15215 	pf->queues_left = queues_left;
15216 	dev_dbg(&pf->pdev->dev,
15217 		"qs_avail=%d FD SB=%d lan_qs=%d lan_tc0=%d vf=%d*%d vmdq=%d*%d, remaining=%d\n",
15218 		pf->hw.func_caps.num_tx_qp,
15219 		!!(pf->flags & I40E_FLAG_FD_SB_ENABLED),
15220 		pf->num_lan_qps, pf->alloc_rss_size, pf->num_req_vfs,
15221 		pf->num_vf_qps, pf->num_vmdq_vsis, pf->num_vmdq_qps,
15222 		queues_left);
15223 }
15224 
15225 /**
15226  * i40e_setup_pf_filter_control - Setup PF static filter control
15227  * @pf: PF to be setup
15228  *
15229  * i40e_setup_pf_filter_control sets up a PF's initial filter control
15230  * settings. If PE/FCoE are enabled then it will also set the per PF
15231  * based filter sizes required for them. It also enables Flow director,
15232  * ethertype and macvlan type filter settings for the pf.
15233  *
15234  * Returns 0 on success, negative on failure
15235  **/
15236 static int i40e_setup_pf_filter_control(struct i40e_pf *pf)
15237 {
15238 	struct i40e_filter_control_settings *settings = &pf->filter_settings;
15239 
15240 	settings->hash_lut_size = I40E_HASH_LUT_SIZE_128;
15241 
15242 	/* Flow Director is enabled */
15243 	if (pf->flags & (I40E_FLAG_FD_SB_ENABLED | I40E_FLAG_FD_ATR_ENABLED))
15244 		settings->enable_fdir = true;
15245 
15246 	/* Ethtype and MACVLAN filters enabled for PF */
15247 	settings->enable_ethtype = true;
15248 	settings->enable_macvlan = true;
15249 
15250 	if (i40e_set_filter_control(&pf->hw, settings))
15251 		return -ENOENT;
15252 
15253 	return 0;
15254 }
15255 
15256 #define INFO_STRING_LEN 255
15257 #define REMAIN(__x) (INFO_STRING_LEN - (__x))
15258 static void i40e_print_features(struct i40e_pf *pf)
15259 {
15260 	struct i40e_hw *hw = &pf->hw;
15261 	char *buf;
15262 	int i;
15263 
15264 	buf = kmalloc(INFO_STRING_LEN, GFP_KERNEL);
15265 	if (!buf)
15266 		return;
15267 
15268 	i = snprintf(buf, INFO_STRING_LEN, "Features: PF-id[%d]", hw->pf_id);
15269 #ifdef CONFIG_PCI_IOV
15270 	i += scnprintf(&buf[i], REMAIN(i), " VFs: %d", pf->num_req_vfs);
15271 #endif
15272 	i += scnprintf(&buf[i], REMAIN(i), " VSIs: %d QP: %d",
15273 		      pf->hw.func_caps.num_vsis,
15274 		      pf->vsi[pf->lan_vsi]->num_queue_pairs);
15275 	if (pf->flags & I40E_FLAG_RSS_ENABLED)
15276 		i += scnprintf(&buf[i], REMAIN(i), " RSS");
15277 	if (pf->flags & I40E_FLAG_FD_ATR_ENABLED)
15278 		i += scnprintf(&buf[i], REMAIN(i), " FD_ATR");
15279 	if (pf->flags & I40E_FLAG_FD_SB_ENABLED) {
15280 		i += scnprintf(&buf[i], REMAIN(i), " FD_SB");
15281 		i += scnprintf(&buf[i], REMAIN(i), " NTUPLE");
15282 	}
15283 	if (pf->flags & I40E_FLAG_DCB_CAPABLE)
15284 		i += scnprintf(&buf[i], REMAIN(i), " DCB");
15285 	i += scnprintf(&buf[i], REMAIN(i), " VxLAN");
15286 	i += scnprintf(&buf[i], REMAIN(i), " Geneve");
15287 	if (pf->flags & I40E_FLAG_PTP)
15288 		i += scnprintf(&buf[i], REMAIN(i), " PTP");
15289 	if (pf->flags & I40E_FLAG_VEB_MODE_ENABLED)
15290 		i += scnprintf(&buf[i], REMAIN(i), " VEB");
15291 	else
15292 		i += scnprintf(&buf[i], REMAIN(i), " VEPA");
15293 
15294 	dev_info(&pf->pdev->dev, "%s\n", buf);
15295 	kfree(buf);
15296 	WARN_ON(i > INFO_STRING_LEN);
15297 }
15298 
15299 /**
15300  * i40e_get_platform_mac_addr - get platform-specific MAC address
15301  * @pdev: PCI device information struct
15302  * @pf: board private structure
15303  *
15304  * Look up the MAC address for the device. First we'll try
15305  * eth_platform_get_mac_address, which will check Open Firmware, or arch
15306  * specific fallback. Otherwise, we'll default to the stored value in
15307  * firmware.
15308  **/
15309 static void i40e_get_platform_mac_addr(struct pci_dev *pdev, struct i40e_pf *pf)
15310 {
15311 	if (eth_platform_get_mac_address(&pdev->dev, pf->hw.mac.addr))
15312 		i40e_get_mac_addr(&pf->hw, pf->hw.mac.addr);
15313 }
15314 
15315 /**
15316  * i40e_set_fec_in_flags - helper function for setting FEC options in flags
15317  * @fec_cfg: FEC option to set in flags
15318  * @flags: ptr to flags in which we set FEC option
15319  **/
15320 void i40e_set_fec_in_flags(u8 fec_cfg, u32 *flags)
15321 {
15322 	if (fec_cfg & I40E_AQ_SET_FEC_AUTO)
15323 		*flags |= I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC;
15324 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_RS) ||
15325 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_RS)) {
15326 		*flags |= I40E_FLAG_RS_FEC;
15327 		*flags &= ~I40E_FLAG_BASE_R_FEC;
15328 	}
15329 	if ((fec_cfg & I40E_AQ_SET_FEC_REQUEST_KR) ||
15330 	    (fec_cfg & I40E_AQ_SET_FEC_ABILITY_KR)) {
15331 		*flags |= I40E_FLAG_BASE_R_FEC;
15332 		*flags &= ~I40E_FLAG_RS_FEC;
15333 	}
15334 	if (fec_cfg == 0)
15335 		*flags &= ~(I40E_FLAG_RS_FEC | I40E_FLAG_BASE_R_FEC);
15336 }
15337 
15338 /**
15339  * i40e_check_recovery_mode - check if we are running transition firmware
15340  * @pf: board private structure
15341  *
15342  * Check registers indicating the firmware runs in recovery mode. Sets the
15343  * appropriate driver state.
15344  *
15345  * Returns true if the recovery mode was detected, false otherwise
15346  **/
15347 static bool i40e_check_recovery_mode(struct i40e_pf *pf)
15348 {
15349 	u32 val = rd32(&pf->hw, I40E_GL_FWSTS);
15350 
15351 	if (val & I40E_GL_FWSTS_FWS1B_MASK) {
15352 		dev_crit(&pf->pdev->dev, "Firmware recovery mode detected. Limiting functionality.\n");
15353 		dev_crit(&pf->pdev->dev, "Refer to the Intel(R) Ethernet Adapters and Devices User Guide for details on firmware recovery mode.\n");
15354 		set_bit(__I40E_RECOVERY_MODE, pf->state);
15355 
15356 		return true;
15357 	}
15358 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15359 		dev_info(&pf->pdev->dev, "Please do Power-On Reset to initialize adapter in normal mode with full functionality.\n");
15360 
15361 	return false;
15362 }
15363 
15364 /**
15365  * i40e_pf_loop_reset - perform reset in a loop.
15366  * @pf: board private structure
15367  *
15368  * This function is useful when a NIC is about to enter recovery mode.
15369  * When a NIC's internal data structures are corrupted the NIC's
15370  * firmware is going to enter recovery mode.
15371  * Right after a POR it takes about 7 minutes for firmware to enter
15372  * recovery mode. Until that time a NIC is in some kind of intermediate
15373  * state. After that time period the NIC almost surely enters
15374  * recovery mode. The only way for a driver to detect intermediate
15375  * state is to issue a series of pf-resets and check a return value.
15376  * If a PF reset returns success then the firmware could be in recovery
15377  * mode so the caller of this code needs to check for recovery mode
15378  * if this function returns success. There is a little chance that
15379  * firmware will hang in intermediate state forever.
15380  * Since waiting 7 minutes is quite a lot of time this function waits
15381  * 10 seconds and then gives up by returning an error.
15382  *
15383  * Return 0 on success, negative on failure.
15384  **/
15385 static i40e_status i40e_pf_loop_reset(struct i40e_pf *pf)
15386 {
15387 	/* wait max 10 seconds for PF reset to succeed */
15388 	const unsigned long time_end = jiffies + 10 * HZ;
15389 
15390 	struct i40e_hw *hw = &pf->hw;
15391 	i40e_status ret;
15392 
15393 	ret = i40e_pf_reset(hw);
15394 	while (ret != I40E_SUCCESS && time_before(jiffies, time_end)) {
15395 		usleep_range(10000, 20000);
15396 		ret = i40e_pf_reset(hw);
15397 	}
15398 
15399 	if (ret == I40E_SUCCESS)
15400 		pf->pfr_count++;
15401 	else
15402 		dev_info(&pf->pdev->dev, "PF reset failed: %d\n", ret);
15403 
15404 	return ret;
15405 }
15406 
15407 /**
15408  * i40e_check_fw_empr - check if FW issued unexpected EMP Reset
15409  * @pf: board private structure
15410  *
15411  * Check FW registers to determine if FW issued unexpected EMP Reset.
15412  * Every time when unexpected EMP Reset occurs the FW increments
15413  * a counter of unexpected EMP Resets. When the counter reaches 10
15414  * the FW should enter the Recovery mode
15415  *
15416  * Returns true if FW issued unexpected EMP Reset
15417  **/
15418 static bool i40e_check_fw_empr(struct i40e_pf *pf)
15419 {
15420 	const u32 fw_sts = rd32(&pf->hw, I40E_GL_FWSTS) &
15421 			   I40E_GL_FWSTS_FWS1B_MASK;
15422 	return (fw_sts > I40E_GL_FWSTS_FWS1B_EMPR_0) &&
15423 	       (fw_sts <= I40E_GL_FWSTS_FWS1B_EMPR_10);
15424 }
15425 
15426 /**
15427  * i40e_handle_resets - handle EMP resets and PF resets
15428  * @pf: board private structure
15429  *
15430  * Handle both EMP resets and PF resets and conclude whether there are
15431  * any issues regarding these resets. If there are any issues then
15432  * generate log entry.
15433  *
15434  * Return 0 if NIC is healthy or negative value when there are issues
15435  * with resets
15436  **/
15437 static i40e_status i40e_handle_resets(struct i40e_pf *pf)
15438 {
15439 	const i40e_status pfr = i40e_pf_loop_reset(pf);
15440 	const bool is_empr = i40e_check_fw_empr(pf);
15441 
15442 	if (is_empr || pfr != I40E_SUCCESS)
15443 		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");
15444 
15445 	return is_empr ? I40E_ERR_RESET_FAILED : pfr;
15446 }
15447 
15448 /**
15449  * i40e_init_recovery_mode - initialize subsystems needed in recovery mode
15450  * @pf: board private structure
15451  * @hw: ptr to the hardware info
15452  *
15453  * This function does a minimal setup of all subsystems needed for running
15454  * recovery mode.
15455  *
15456  * Returns 0 on success, negative on failure
15457  **/
15458 static int i40e_init_recovery_mode(struct i40e_pf *pf, struct i40e_hw *hw)
15459 {
15460 	struct i40e_vsi *vsi;
15461 	int err;
15462 	int v_idx;
15463 
15464 	pci_save_state(pf->pdev);
15465 
15466 	/* set up periodic task facility */
15467 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15468 	pf->service_timer_period = HZ;
15469 
15470 	INIT_WORK(&pf->service_task, i40e_service_task);
15471 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15472 
15473 	err = i40e_init_interrupt_scheme(pf);
15474 	if (err)
15475 		goto err_switch_setup;
15476 
15477 	/* The number of VSIs reported by the FW is the minimum guaranteed
15478 	 * to us; HW supports far more and we share the remaining pool with
15479 	 * the other PFs. We allocate space for more than the guarantee with
15480 	 * the understanding that we might not get them all later.
15481 	 */
15482 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15483 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15484 	else
15485 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15486 
15487 	/* Set up the vsi struct and our local tracking of the MAIN PF vsi. */
15488 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15489 			  GFP_KERNEL);
15490 	if (!pf->vsi) {
15491 		err = -ENOMEM;
15492 		goto err_switch_setup;
15493 	}
15494 
15495 	/* We allocate one VSI which is needed as absolute minimum
15496 	 * in order to register the netdev
15497 	 */
15498 	v_idx = i40e_vsi_mem_alloc(pf, I40E_VSI_MAIN);
15499 	if (v_idx < 0) {
15500 		err = v_idx;
15501 		goto err_switch_setup;
15502 	}
15503 	pf->lan_vsi = v_idx;
15504 	vsi = pf->vsi[v_idx];
15505 	if (!vsi) {
15506 		err = -EFAULT;
15507 		goto err_switch_setup;
15508 	}
15509 	vsi->alloc_queue_pairs = 1;
15510 	err = i40e_config_netdev(vsi);
15511 	if (err)
15512 		goto err_switch_setup;
15513 	err = register_netdev(vsi->netdev);
15514 	if (err)
15515 		goto err_switch_setup;
15516 	vsi->netdev_registered = true;
15517 	i40e_dbg_pf_init(pf);
15518 
15519 	err = i40e_setup_misc_vector_for_recovery_mode(pf);
15520 	if (err)
15521 		goto err_switch_setup;
15522 
15523 	/* tell the firmware that we're starting */
15524 	i40e_send_version(pf);
15525 
15526 	/* since everything's happy, start the service_task timer */
15527 	mod_timer(&pf->service_timer,
15528 		  round_jiffies(jiffies + pf->service_timer_period));
15529 
15530 	return 0;
15531 
15532 err_switch_setup:
15533 	i40e_reset_interrupt_capability(pf);
15534 	del_timer_sync(&pf->service_timer);
15535 	i40e_shutdown_adminq(hw);
15536 	iounmap(hw->hw_addr);
15537 	pci_disable_pcie_error_reporting(pf->pdev);
15538 	pci_release_mem_regions(pf->pdev);
15539 	pci_disable_device(pf->pdev);
15540 	kfree(pf);
15541 
15542 	return err;
15543 }
15544 
15545 /**
15546  * i40e_set_subsystem_device_id - set subsystem device id
15547  * @hw: pointer to the hardware info
15548  *
15549  * Set PCI subsystem device id either from a pci_dev structure or
15550  * a specific FW register.
15551  **/
15552 static inline void i40e_set_subsystem_device_id(struct i40e_hw *hw)
15553 {
15554 	struct pci_dev *pdev = ((struct i40e_pf *)hw->back)->pdev;
15555 
15556 	hw->subsystem_device_id = pdev->subsystem_device ?
15557 		pdev->subsystem_device :
15558 		(ushort)(rd32(hw, I40E_PFPCI_SUBSYSID) & USHRT_MAX);
15559 }
15560 
15561 /**
15562  * i40e_probe - Device initialization routine
15563  * @pdev: PCI device information struct
15564  * @ent: entry in i40e_pci_tbl
15565  *
15566  * i40e_probe initializes a PF identified by a pci_dev structure.
15567  * The OS initialization, configuring of the PF private structure,
15568  * and a hardware reset occur.
15569  *
15570  * Returns 0 on success, negative on failure
15571  **/
15572 static int i40e_probe(struct pci_dev *pdev, const struct pci_device_id *ent)
15573 {
15574 	struct i40e_aq_get_phy_abilities_resp abilities;
15575 #ifdef CONFIG_I40E_DCB
15576 	enum i40e_get_fw_lldp_status_resp lldp_status;
15577 	i40e_status status;
15578 #endif /* CONFIG_I40E_DCB */
15579 	struct i40e_pf *pf;
15580 	struct i40e_hw *hw;
15581 	static u16 pfs_found;
15582 	u16 wol_nvm_bits;
15583 	u16 link_status;
15584 	int err;
15585 	u32 val;
15586 	u32 i;
15587 
15588 	err = pci_enable_device_mem(pdev);
15589 	if (err)
15590 		return err;
15591 
15592 	/* set up for high or low dma */
15593 	err = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
15594 	if (err) {
15595 		dev_err(&pdev->dev,
15596 			"DMA configuration failed: 0x%x\n", err);
15597 		goto err_dma;
15598 	}
15599 
15600 	/* set up pci connections */
15601 	err = pci_request_mem_regions(pdev, i40e_driver_name);
15602 	if (err) {
15603 		dev_info(&pdev->dev,
15604 			 "pci_request_selected_regions failed %d\n", err);
15605 		goto err_pci_reg;
15606 	}
15607 
15608 	pci_enable_pcie_error_reporting(pdev);
15609 	pci_set_master(pdev);
15610 
15611 	/* Now that we have a PCI connection, we need to do the
15612 	 * low level device setup.  This is primarily setting up
15613 	 * the Admin Queue structures and then querying for the
15614 	 * device's current profile information.
15615 	 */
15616 	pf = kzalloc(sizeof(*pf), GFP_KERNEL);
15617 	if (!pf) {
15618 		err = -ENOMEM;
15619 		goto err_pf_alloc;
15620 	}
15621 	pf->next_vsi = 0;
15622 	pf->pdev = pdev;
15623 	set_bit(__I40E_DOWN, pf->state);
15624 
15625 	hw = &pf->hw;
15626 	hw->back = pf;
15627 
15628 	pf->ioremap_len = min_t(int, pci_resource_len(pdev, 0),
15629 				I40E_MAX_CSR_SPACE);
15630 	/* We believe that the highest register to read is
15631 	 * I40E_GLGEN_STAT_CLEAR, so we check if the BAR size
15632 	 * is not less than that before mapping to prevent a
15633 	 * kernel panic.
15634 	 */
15635 	if (pf->ioremap_len < I40E_GLGEN_STAT_CLEAR) {
15636 		dev_err(&pdev->dev, "Cannot map registers, bar size 0x%X too small, aborting\n",
15637 			pf->ioremap_len);
15638 		err = -ENOMEM;
15639 		goto err_ioremap;
15640 	}
15641 	hw->hw_addr = ioremap(pci_resource_start(pdev, 0), pf->ioremap_len);
15642 	if (!hw->hw_addr) {
15643 		err = -EIO;
15644 		dev_info(&pdev->dev, "ioremap(0x%04x, 0x%04x) failed: 0x%x\n",
15645 			 (unsigned int)pci_resource_start(pdev, 0),
15646 			 pf->ioremap_len, err);
15647 		goto err_ioremap;
15648 	}
15649 	hw->vendor_id = pdev->vendor;
15650 	hw->device_id = pdev->device;
15651 	pci_read_config_byte(pdev, PCI_REVISION_ID, &hw->revision_id);
15652 	hw->subsystem_vendor_id = pdev->subsystem_vendor;
15653 	i40e_set_subsystem_device_id(hw);
15654 	hw->bus.device = PCI_SLOT(pdev->devfn);
15655 	hw->bus.func = PCI_FUNC(pdev->devfn);
15656 	hw->bus.bus_id = pdev->bus->number;
15657 	pf->instance = pfs_found;
15658 
15659 	/* Select something other than the 802.1ad ethertype for the
15660 	 * switch to use internally and drop on ingress.
15661 	 */
15662 	hw->switch_tag = 0xffff;
15663 	hw->first_tag = ETH_P_8021AD;
15664 	hw->second_tag = ETH_P_8021Q;
15665 
15666 	INIT_LIST_HEAD(&pf->l3_flex_pit_list);
15667 	INIT_LIST_HEAD(&pf->l4_flex_pit_list);
15668 	INIT_LIST_HEAD(&pf->ddp_old_prof);
15669 
15670 	/* set up the locks for the AQ, do this only once in probe
15671 	 * and destroy them only once in remove
15672 	 */
15673 	mutex_init(&hw->aq.asq_mutex);
15674 	mutex_init(&hw->aq.arq_mutex);
15675 
15676 	pf->msg_enable = netif_msg_init(debug,
15677 					NETIF_MSG_DRV |
15678 					NETIF_MSG_PROBE |
15679 					NETIF_MSG_LINK);
15680 	if (debug < -1)
15681 		pf->hw.debug_mask = debug;
15682 
15683 	/* do a special CORER for clearing PXE mode once at init */
15684 	if (hw->revision_id == 0 &&
15685 	    (rd32(hw, I40E_GLLAN_RCTL_0) & I40E_GLLAN_RCTL_0_PXE_MODE_MASK)) {
15686 		wr32(hw, I40E_GLGEN_RTRIG, I40E_GLGEN_RTRIG_CORER_MASK);
15687 		i40e_flush(hw);
15688 		msleep(200);
15689 		pf->corer_count++;
15690 
15691 		i40e_clear_pxe_mode(hw);
15692 	}
15693 
15694 	/* Reset here to make sure all is clean and to define PF 'n' */
15695 	i40e_clear_hw(hw);
15696 
15697 	err = i40e_set_mac_type(hw);
15698 	if (err) {
15699 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15700 			 err);
15701 		goto err_pf_reset;
15702 	}
15703 
15704 	err = i40e_handle_resets(pf);
15705 	if (err)
15706 		goto err_pf_reset;
15707 
15708 	i40e_check_recovery_mode(pf);
15709 
15710 	if (is_kdump_kernel()) {
15711 		hw->aq.num_arq_entries = I40E_MIN_ARQ_LEN;
15712 		hw->aq.num_asq_entries = I40E_MIN_ASQ_LEN;
15713 	} else {
15714 		hw->aq.num_arq_entries = I40E_AQ_LEN;
15715 		hw->aq.num_asq_entries = I40E_AQ_LEN;
15716 	}
15717 	hw->aq.arq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15718 	hw->aq.asq_buf_size = I40E_MAX_AQ_BUF_SIZE;
15719 	pf->adminq_work_limit = I40E_AQ_WORK_LIMIT;
15720 
15721 	snprintf(pf->int_name, sizeof(pf->int_name) - 1,
15722 		 "%s-%s:misc",
15723 		 dev_driver_string(&pf->pdev->dev), dev_name(&pdev->dev));
15724 
15725 	err = i40e_init_shared_code(hw);
15726 	if (err) {
15727 		dev_warn(&pdev->dev, "unidentified MAC or BLANK NVM: %d\n",
15728 			 err);
15729 		goto err_pf_reset;
15730 	}
15731 
15732 	/* set up a default setting for link flow control */
15733 	pf->hw.fc.requested_mode = I40E_FC_NONE;
15734 
15735 	err = i40e_init_adminq(hw);
15736 	if (err) {
15737 		if (err == I40E_ERR_FIRMWARE_API_VERSION)
15738 			dev_info(&pdev->dev,
15739 				 "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",
15740 				 hw->aq.api_maj_ver,
15741 				 hw->aq.api_min_ver,
15742 				 I40E_FW_API_VERSION_MAJOR,
15743 				 I40E_FW_MINOR_VERSION(hw));
15744 		else
15745 			dev_info(&pdev->dev,
15746 				 "The driver for the device stopped because the device firmware failed to init. Try updating your NVM image.\n");
15747 
15748 		goto err_pf_reset;
15749 	}
15750 	i40e_get_oem_version(hw);
15751 
15752 	/* provide nvm, fw, api versions, vendor:device id, subsys vendor:device id */
15753 	dev_info(&pdev->dev, "fw %d.%d.%05d api %d.%d nvm %s [%04x:%04x] [%04x:%04x]\n",
15754 		 hw->aq.fw_maj_ver, hw->aq.fw_min_ver, hw->aq.fw_build,
15755 		 hw->aq.api_maj_ver, hw->aq.api_min_ver,
15756 		 i40e_nvm_version_str(hw), hw->vendor_id, hw->device_id,
15757 		 hw->subsystem_vendor_id, hw->subsystem_device_id);
15758 
15759 	if (hw->aq.api_maj_ver == I40E_FW_API_VERSION_MAJOR &&
15760 	    hw->aq.api_min_ver > I40E_FW_MINOR_VERSION(hw))
15761 		dev_dbg(&pdev->dev,
15762 			"The driver for the device detected a newer version of the NVM image v%u.%u than v%u.%u.\n",
15763 			 hw->aq.api_maj_ver,
15764 			 hw->aq.api_min_ver,
15765 			 I40E_FW_API_VERSION_MAJOR,
15766 			 I40E_FW_MINOR_VERSION(hw));
15767 	else if (hw->aq.api_maj_ver == 1 && hw->aq.api_min_ver < 4)
15768 		dev_info(&pdev->dev,
15769 			 "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",
15770 			 hw->aq.api_maj_ver,
15771 			 hw->aq.api_min_ver,
15772 			 I40E_FW_API_VERSION_MAJOR,
15773 			 I40E_FW_MINOR_VERSION(hw));
15774 
15775 	i40e_verify_eeprom(pf);
15776 
15777 	/* Rev 0 hardware was never productized */
15778 	if (hw->revision_id < 1)
15779 		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");
15780 
15781 	i40e_clear_pxe_mode(hw);
15782 
15783 	err = i40e_get_capabilities(pf, i40e_aqc_opc_list_func_capabilities);
15784 	if (err)
15785 		goto err_adminq_setup;
15786 
15787 	err = i40e_sw_init(pf);
15788 	if (err) {
15789 		dev_info(&pdev->dev, "sw_init failed: %d\n", err);
15790 		goto err_sw_init;
15791 	}
15792 
15793 	if (test_bit(__I40E_RECOVERY_MODE, pf->state))
15794 		return i40e_init_recovery_mode(pf, hw);
15795 
15796 	err = i40e_init_lan_hmc(hw, hw->func_caps.num_tx_qp,
15797 				hw->func_caps.num_rx_qp, 0, 0);
15798 	if (err) {
15799 		dev_info(&pdev->dev, "init_lan_hmc failed: %d\n", err);
15800 		goto err_init_lan_hmc;
15801 	}
15802 
15803 	err = i40e_configure_lan_hmc(hw, I40E_HMC_MODEL_DIRECT_ONLY);
15804 	if (err) {
15805 		dev_info(&pdev->dev, "configure_lan_hmc failed: %d\n", err);
15806 		err = -ENOENT;
15807 		goto err_configure_lan_hmc;
15808 	}
15809 
15810 	/* Disable LLDP for NICs that have firmware versions lower than v4.3.
15811 	 * Ignore error return codes because if it was already disabled via
15812 	 * hardware settings this will fail
15813 	 */
15814 	if (pf->hw_features & I40E_HW_STOP_FW_LLDP) {
15815 		dev_info(&pdev->dev, "Stopping firmware LLDP agent.\n");
15816 		i40e_aq_stop_lldp(hw, true, false, NULL);
15817 	}
15818 
15819 	/* allow a platform config to override the HW addr */
15820 	i40e_get_platform_mac_addr(pdev, pf);
15821 
15822 	if (!is_valid_ether_addr(hw->mac.addr)) {
15823 		dev_info(&pdev->dev, "invalid MAC address %pM\n", hw->mac.addr);
15824 		err = -EIO;
15825 		goto err_mac_addr;
15826 	}
15827 	dev_info(&pdev->dev, "MAC address: %pM\n", hw->mac.addr);
15828 	ether_addr_copy(hw->mac.perm_addr, hw->mac.addr);
15829 	i40e_get_port_mac_addr(hw, hw->mac.port_addr);
15830 	if (is_valid_ether_addr(hw->mac.port_addr))
15831 		pf->hw_features |= I40E_HW_PORT_ID_VALID;
15832 
15833 	i40e_ptp_alloc_pins(pf);
15834 	pci_set_drvdata(pdev, pf);
15835 	pci_save_state(pdev);
15836 
15837 #ifdef CONFIG_I40E_DCB
15838 	status = i40e_get_fw_lldp_status(&pf->hw, &lldp_status);
15839 	(!status &&
15840 	 lldp_status == I40E_GET_FW_LLDP_STATUS_ENABLED) ?
15841 		(pf->flags &= ~I40E_FLAG_DISABLE_FW_LLDP) :
15842 		(pf->flags |= I40E_FLAG_DISABLE_FW_LLDP);
15843 	dev_info(&pdev->dev,
15844 		 (pf->flags & I40E_FLAG_DISABLE_FW_LLDP) ?
15845 			"FW LLDP is disabled\n" :
15846 			"FW LLDP is enabled\n");
15847 
15848 	/* Enable FW to write default DCB config on link-up */
15849 	i40e_aq_set_dcb_parameters(hw, true, NULL);
15850 
15851 	err = i40e_init_pf_dcb(pf);
15852 	if (err) {
15853 		dev_info(&pdev->dev, "DCB init failed %d, disabled\n", err);
15854 		pf->flags &= ~(I40E_FLAG_DCB_CAPABLE | I40E_FLAG_DCB_ENABLED);
15855 		/* Continue without DCB enabled */
15856 	}
15857 #endif /* CONFIG_I40E_DCB */
15858 
15859 	/* set up periodic task facility */
15860 	timer_setup(&pf->service_timer, i40e_service_timer, 0);
15861 	pf->service_timer_period = HZ;
15862 
15863 	INIT_WORK(&pf->service_task, i40e_service_task);
15864 	clear_bit(__I40E_SERVICE_SCHED, pf->state);
15865 
15866 	/* NVM bit on means WoL disabled for the port */
15867 	i40e_read_nvm_word(hw, I40E_SR_NVM_WAKE_ON_LAN, &wol_nvm_bits);
15868 	if (BIT (hw->port) & wol_nvm_bits || hw->partition_id != 1)
15869 		pf->wol_en = false;
15870 	else
15871 		pf->wol_en = true;
15872 	device_set_wakeup_enable(&pf->pdev->dev, pf->wol_en);
15873 
15874 	/* set up the main switch operations */
15875 	i40e_determine_queue_usage(pf);
15876 	err = i40e_init_interrupt_scheme(pf);
15877 	if (err)
15878 		goto err_switch_setup;
15879 
15880 	/* Reduce Tx and Rx pairs for kdump
15881 	 * When MSI-X is enabled, it's not allowed to use more TC queue
15882 	 * pairs than MSI-X vectors (pf->num_lan_msix) exist. Thus
15883 	 * vsi->num_queue_pairs will be equal to pf->num_lan_msix, i.e., 1.
15884 	 */
15885 	if (is_kdump_kernel())
15886 		pf->num_lan_msix = 1;
15887 
15888 	pf->udp_tunnel_nic.set_port = i40e_udp_tunnel_set_port;
15889 	pf->udp_tunnel_nic.unset_port = i40e_udp_tunnel_unset_port;
15890 	pf->udp_tunnel_nic.flags = UDP_TUNNEL_NIC_INFO_MAY_SLEEP;
15891 	pf->udp_tunnel_nic.shared = &pf->udp_tunnel_shared;
15892 	pf->udp_tunnel_nic.tables[0].n_entries = I40E_MAX_PF_UDP_OFFLOAD_PORTS;
15893 	pf->udp_tunnel_nic.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN |
15894 						    UDP_TUNNEL_TYPE_GENEVE;
15895 
15896 	/* The number of VSIs reported by the FW is the minimum guaranteed
15897 	 * to us; HW supports far more and we share the remaining pool with
15898 	 * the other PFs. We allocate space for more than the guarantee with
15899 	 * the understanding that we might not get them all later.
15900 	 */
15901 	if (pf->hw.func_caps.num_vsis < I40E_MIN_VSI_ALLOC)
15902 		pf->num_alloc_vsi = I40E_MIN_VSI_ALLOC;
15903 	else
15904 		pf->num_alloc_vsi = pf->hw.func_caps.num_vsis;
15905 	if (pf->num_alloc_vsi > UDP_TUNNEL_NIC_MAX_SHARING_DEVICES) {
15906 		dev_warn(&pf->pdev->dev,
15907 			 "limiting the VSI count due to UDP tunnel limitation %d > %d\n",
15908 			 pf->num_alloc_vsi, UDP_TUNNEL_NIC_MAX_SHARING_DEVICES);
15909 		pf->num_alloc_vsi = UDP_TUNNEL_NIC_MAX_SHARING_DEVICES;
15910 	}
15911 
15912 	/* Set up the *vsi struct and our local tracking of the MAIN PF vsi. */
15913 	pf->vsi = kcalloc(pf->num_alloc_vsi, sizeof(struct i40e_vsi *),
15914 			  GFP_KERNEL);
15915 	if (!pf->vsi) {
15916 		err = -ENOMEM;
15917 		goto err_switch_setup;
15918 	}
15919 
15920 #ifdef CONFIG_PCI_IOV
15921 	/* prep for VF support */
15922 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
15923 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
15924 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
15925 		if (pci_num_vf(pdev))
15926 			pf->flags |= I40E_FLAG_VEB_MODE_ENABLED;
15927 	}
15928 #endif
15929 	err = i40e_setup_pf_switch(pf, false, false);
15930 	if (err) {
15931 		dev_info(&pdev->dev, "setup_pf_switch failed: %d\n", err);
15932 		goto err_vsis;
15933 	}
15934 	INIT_LIST_HEAD(&pf->vsi[pf->lan_vsi]->ch_list);
15935 
15936 	/* if FDIR VSI was set up, start it now */
15937 	for (i = 0; i < pf->num_alloc_vsi; i++) {
15938 		if (pf->vsi[i] && pf->vsi[i]->type == I40E_VSI_FDIR) {
15939 			i40e_vsi_open(pf->vsi[i]);
15940 			break;
15941 		}
15942 	}
15943 
15944 	/* The driver only wants link up/down and module qualification
15945 	 * reports from firmware.  Note the negative logic.
15946 	 */
15947 	err = i40e_aq_set_phy_int_mask(&pf->hw,
15948 				       ~(I40E_AQ_EVENT_LINK_UPDOWN |
15949 					 I40E_AQ_EVENT_MEDIA_NA |
15950 					 I40E_AQ_EVENT_MODULE_QUAL_FAIL), NULL);
15951 	if (err)
15952 		dev_info(&pf->pdev->dev, "set phy mask fail, err %s aq_err %s\n",
15953 			 i40e_stat_str(&pf->hw, err),
15954 			 i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
15955 
15956 	/* Reconfigure hardware for allowing smaller MSS in the case
15957 	 * of TSO, so that we avoid the MDD being fired and causing
15958 	 * a reset in the case of small MSS+TSO.
15959 	 */
15960 	val = rd32(hw, I40E_REG_MSS);
15961 	if ((val & I40E_REG_MSS_MIN_MASK) > I40E_64BYTE_MSS) {
15962 		val &= ~I40E_REG_MSS_MIN_MASK;
15963 		val |= I40E_64BYTE_MSS;
15964 		wr32(hw, I40E_REG_MSS, val);
15965 	}
15966 
15967 	if (pf->hw_features & I40E_HW_RESTART_AUTONEG) {
15968 		msleep(75);
15969 		err = i40e_aq_set_link_restart_an(&pf->hw, true, NULL);
15970 		if (err)
15971 			dev_info(&pf->pdev->dev, "link restart failed, err %s aq_err %s\n",
15972 				 i40e_stat_str(&pf->hw, err),
15973 				 i40e_aq_str(&pf->hw,
15974 					     pf->hw.aq.asq_last_status));
15975 	}
15976 	/* The main driver is (mostly) up and happy. We need to set this state
15977 	 * before setting up the misc vector or we get a race and the vector
15978 	 * ends up disabled forever.
15979 	 */
15980 	clear_bit(__I40E_DOWN, pf->state);
15981 
15982 	/* In case of MSIX we are going to setup the misc vector right here
15983 	 * to handle admin queue events etc. In case of legacy and MSI
15984 	 * the misc functionality and queue processing is combined in
15985 	 * the same vector and that gets setup at open.
15986 	 */
15987 	if (pf->flags & I40E_FLAG_MSIX_ENABLED) {
15988 		err = i40e_setup_misc_vector(pf);
15989 		if (err) {
15990 			dev_info(&pdev->dev,
15991 				 "setup of misc vector failed: %d\n", err);
15992 			i40e_cloud_filter_exit(pf);
15993 			i40e_fdir_teardown(pf);
15994 			goto err_vsis;
15995 		}
15996 	}
15997 
15998 #ifdef CONFIG_PCI_IOV
15999 	/* prep for VF support */
16000 	if ((pf->flags & I40E_FLAG_SRIOV_ENABLED) &&
16001 	    (pf->flags & I40E_FLAG_MSIX_ENABLED) &&
16002 	    !test_bit(__I40E_BAD_EEPROM, pf->state)) {
16003 		/* disable link interrupts for VFs */
16004 		val = rd32(hw, I40E_PFGEN_PORTMDIO_NUM);
16005 		val &= ~I40E_PFGEN_PORTMDIO_NUM_VFLINK_STAT_ENA_MASK;
16006 		wr32(hw, I40E_PFGEN_PORTMDIO_NUM, val);
16007 		i40e_flush(hw);
16008 
16009 		if (pci_num_vf(pdev)) {
16010 			dev_info(&pdev->dev,
16011 				 "Active VFs found, allocating resources.\n");
16012 			err = i40e_alloc_vfs(pf, pci_num_vf(pdev));
16013 			if (err)
16014 				dev_info(&pdev->dev,
16015 					 "Error %d allocating resources for existing VFs\n",
16016 					 err);
16017 		}
16018 	}
16019 #endif /* CONFIG_PCI_IOV */
16020 
16021 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16022 		pf->iwarp_base_vector = i40e_get_lump(pf, pf->irq_pile,
16023 						      pf->num_iwarp_msix,
16024 						      I40E_IWARP_IRQ_PILE_ID);
16025 		if (pf->iwarp_base_vector < 0) {
16026 			dev_info(&pdev->dev,
16027 				 "failed to get tracking for %d vectors for IWARP err=%d\n",
16028 				 pf->num_iwarp_msix, pf->iwarp_base_vector);
16029 			pf->flags &= ~I40E_FLAG_IWARP_ENABLED;
16030 		}
16031 	}
16032 
16033 	i40e_dbg_pf_init(pf);
16034 
16035 	/* tell the firmware that we're starting */
16036 	i40e_send_version(pf);
16037 
16038 	/* since everything's happy, start the service_task timer */
16039 	mod_timer(&pf->service_timer,
16040 		  round_jiffies(jiffies + pf->service_timer_period));
16041 
16042 	/* add this PF to client device list and launch a client service task */
16043 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16044 		err = i40e_lan_add_device(pf);
16045 		if (err)
16046 			dev_info(&pdev->dev, "Failed to add PF to client API service list: %d\n",
16047 				 err);
16048 	}
16049 
16050 #define PCI_SPEED_SIZE 8
16051 #define PCI_WIDTH_SIZE 8
16052 	/* Devices on the IOSF bus do not have this information
16053 	 * and will report PCI Gen 1 x 1 by default so don't bother
16054 	 * checking them.
16055 	 */
16056 	if (!(pf->hw_features & I40E_HW_NO_PCI_LINK_CHECK)) {
16057 		char speed[PCI_SPEED_SIZE] = "Unknown";
16058 		char width[PCI_WIDTH_SIZE] = "Unknown";
16059 
16060 		/* Get the negotiated link width and speed from PCI config
16061 		 * space
16062 		 */
16063 		pcie_capability_read_word(pf->pdev, PCI_EXP_LNKSTA,
16064 					  &link_status);
16065 
16066 		i40e_set_pci_config_data(hw, link_status);
16067 
16068 		switch (hw->bus.speed) {
16069 		case i40e_bus_speed_8000:
16070 			strscpy(speed, "8.0", PCI_SPEED_SIZE); break;
16071 		case i40e_bus_speed_5000:
16072 			strscpy(speed, "5.0", PCI_SPEED_SIZE); break;
16073 		case i40e_bus_speed_2500:
16074 			strscpy(speed, "2.5", PCI_SPEED_SIZE); break;
16075 		default:
16076 			break;
16077 		}
16078 		switch (hw->bus.width) {
16079 		case i40e_bus_width_pcie_x8:
16080 			strscpy(width, "8", PCI_WIDTH_SIZE); break;
16081 		case i40e_bus_width_pcie_x4:
16082 			strscpy(width, "4", PCI_WIDTH_SIZE); break;
16083 		case i40e_bus_width_pcie_x2:
16084 			strscpy(width, "2", PCI_WIDTH_SIZE); break;
16085 		case i40e_bus_width_pcie_x1:
16086 			strscpy(width, "1", PCI_WIDTH_SIZE); break;
16087 		default:
16088 			break;
16089 		}
16090 
16091 		dev_info(&pdev->dev, "PCI-Express: Speed %sGT/s Width x%s\n",
16092 			 speed, width);
16093 
16094 		if (hw->bus.width < i40e_bus_width_pcie_x8 ||
16095 		    hw->bus.speed < i40e_bus_speed_8000) {
16096 			dev_warn(&pdev->dev, "PCI-Express bandwidth available for this device may be insufficient for optimal performance.\n");
16097 			dev_warn(&pdev->dev, "Please move the device to a different PCI-e link with more lanes and/or higher transfer rate.\n");
16098 		}
16099 	}
16100 
16101 	/* get the requested speeds from the fw */
16102 	err = i40e_aq_get_phy_capabilities(hw, false, false, &abilities, NULL);
16103 	if (err)
16104 		dev_dbg(&pf->pdev->dev, "get requested speeds ret =  %s last_status =  %s\n",
16105 			i40e_stat_str(&pf->hw, err),
16106 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16107 	pf->hw.phy.link_info.requested_speeds = abilities.link_speed;
16108 
16109 	/* set the FEC config due to the board capabilities */
16110 	i40e_set_fec_in_flags(abilities.fec_cfg_curr_mod_ext_info, &pf->flags);
16111 
16112 	/* get the supported phy types from the fw */
16113 	err = i40e_aq_get_phy_capabilities(hw, false, true, &abilities, NULL);
16114 	if (err)
16115 		dev_dbg(&pf->pdev->dev, "get supported phy types ret =  %s last_status =  %s\n",
16116 			i40e_stat_str(&pf->hw, err),
16117 			i40e_aq_str(&pf->hw, pf->hw.aq.asq_last_status));
16118 
16119 	/* make sure the MFS hasn't been set lower than the default */
16120 #define MAX_FRAME_SIZE_DEFAULT 0x2600
16121 	val = (rd32(&pf->hw, I40E_PRTGL_SAH) &
16122 	       I40E_PRTGL_SAH_MFS_MASK) >> I40E_PRTGL_SAH_MFS_SHIFT;
16123 	if (val < MAX_FRAME_SIZE_DEFAULT)
16124 		dev_warn(&pdev->dev, "MFS for port %x has been set below the default: %x\n",
16125 			 i, val);
16126 
16127 	/* Add a filter to drop all Flow control frames from any VSI from being
16128 	 * transmitted. By doing so we stop a malicious VF from sending out
16129 	 * PAUSE or PFC frames and potentially controlling traffic for other
16130 	 * PF/VF VSIs.
16131 	 * The FW can still send Flow control frames if enabled.
16132 	 */
16133 	i40e_add_filter_to_drop_tx_flow_control_frames(&pf->hw,
16134 						       pf->main_vsi_seid);
16135 
16136 	if ((pf->hw.device_id == I40E_DEV_ID_10G_BASE_T) ||
16137 		(pf->hw.device_id == I40E_DEV_ID_10G_BASE_T4))
16138 		pf->hw_features |= I40E_HW_PHY_CONTROLS_LEDS;
16139 	if (pf->hw.device_id == I40E_DEV_ID_SFP_I_X722)
16140 		pf->hw_features |= I40E_HW_HAVE_CRT_RETIMER;
16141 	/* print a string summarizing features */
16142 	i40e_print_features(pf);
16143 
16144 	return 0;
16145 
16146 	/* Unwind what we've done if something failed in the setup */
16147 err_vsis:
16148 	set_bit(__I40E_DOWN, pf->state);
16149 	i40e_clear_interrupt_scheme(pf);
16150 	kfree(pf->vsi);
16151 err_switch_setup:
16152 	i40e_reset_interrupt_capability(pf);
16153 	del_timer_sync(&pf->service_timer);
16154 err_mac_addr:
16155 err_configure_lan_hmc:
16156 	(void)i40e_shutdown_lan_hmc(hw);
16157 err_init_lan_hmc:
16158 	kfree(pf->qp_pile);
16159 err_sw_init:
16160 err_adminq_setup:
16161 err_pf_reset:
16162 	iounmap(hw->hw_addr);
16163 err_ioremap:
16164 	kfree(pf);
16165 err_pf_alloc:
16166 	pci_disable_pcie_error_reporting(pdev);
16167 	pci_release_mem_regions(pdev);
16168 err_pci_reg:
16169 err_dma:
16170 	pci_disable_device(pdev);
16171 	return err;
16172 }
16173 
16174 /**
16175  * i40e_remove - Device removal routine
16176  * @pdev: PCI device information struct
16177  *
16178  * i40e_remove is called by the PCI subsystem to alert the driver
16179  * that is should release a PCI device.  This could be caused by a
16180  * Hot-Plug event, or because the driver is going to be removed from
16181  * memory.
16182  **/
16183 static void i40e_remove(struct pci_dev *pdev)
16184 {
16185 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16186 	struct i40e_hw *hw = &pf->hw;
16187 	i40e_status ret_code;
16188 	int i;
16189 
16190 	i40e_dbg_pf_exit(pf);
16191 
16192 	i40e_ptp_stop(pf);
16193 
16194 	/* Disable RSS in hw */
16195 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(0), 0);
16196 	i40e_write_rx_ctl(hw, I40E_PFQF_HENA(1), 0);
16197 
16198 	/* Grab __I40E_RESET_RECOVERY_PENDING and set __I40E_IN_REMOVE
16199 	 * flags, once they are set, i40e_rebuild should not be called as
16200 	 * i40e_prep_for_reset always returns early.
16201 	 */
16202 	while (test_and_set_bit(__I40E_RESET_RECOVERY_PENDING, pf->state))
16203 		usleep_range(1000, 2000);
16204 	set_bit(__I40E_IN_REMOVE, pf->state);
16205 
16206 	if (pf->flags & I40E_FLAG_SRIOV_ENABLED) {
16207 		set_bit(__I40E_VF_RESETS_DISABLED, pf->state);
16208 		i40e_free_vfs(pf);
16209 		pf->flags &= ~I40E_FLAG_SRIOV_ENABLED;
16210 	}
16211 	/* no more scheduling of any task */
16212 	set_bit(__I40E_SUSPENDED, pf->state);
16213 	set_bit(__I40E_DOWN, pf->state);
16214 	if (pf->service_timer.function)
16215 		del_timer_sync(&pf->service_timer);
16216 	if (pf->service_task.func)
16217 		cancel_work_sync(&pf->service_task);
16218 
16219 	if (test_bit(__I40E_RECOVERY_MODE, pf->state)) {
16220 		struct i40e_vsi *vsi = pf->vsi[0];
16221 
16222 		/* We know that we have allocated only one vsi for this PF,
16223 		 * it was just for registering netdevice, so the interface
16224 		 * could be visible in the 'ifconfig' output
16225 		 */
16226 		unregister_netdev(vsi->netdev);
16227 		free_netdev(vsi->netdev);
16228 
16229 		goto unmap;
16230 	}
16231 
16232 	/* Client close must be called explicitly here because the timer
16233 	 * has been stopped.
16234 	 */
16235 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16236 
16237 	i40e_fdir_teardown(pf);
16238 
16239 	/* If there is a switch structure or any orphans, remove them.
16240 	 * This will leave only the PF's VSI remaining.
16241 	 */
16242 	for (i = 0; i < I40E_MAX_VEB; i++) {
16243 		if (!pf->veb[i])
16244 			continue;
16245 
16246 		if (pf->veb[i]->uplink_seid == pf->mac_seid ||
16247 		    pf->veb[i]->uplink_seid == 0)
16248 			i40e_switch_branch_release(pf->veb[i]);
16249 	}
16250 
16251 	/* Now we can shutdown the PF's VSI, just before we kill
16252 	 * adminq and hmc.
16253 	 */
16254 	if (pf->vsi[pf->lan_vsi])
16255 		i40e_vsi_release(pf->vsi[pf->lan_vsi]);
16256 
16257 	i40e_cloud_filter_exit(pf);
16258 
16259 	/* remove attached clients */
16260 	if (pf->flags & I40E_FLAG_IWARP_ENABLED) {
16261 		ret_code = i40e_lan_del_device(pf);
16262 		if (ret_code)
16263 			dev_warn(&pdev->dev, "Failed to delete client device: %d\n",
16264 				 ret_code);
16265 	}
16266 
16267 	/* shutdown and destroy the HMC */
16268 	if (hw->hmc.hmc_obj) {
16269 		ret_code = i40e_shutdown_lan_hmc(hw);
16270 		if (ret_code)
16271 			dev_warn(&pdev->dev,
16272 				 "Failed to destroy the HMC resources: %d\n",
16273 				 ret_code);
16274 	}
16275 
16276 unmap:
16277 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16278 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16279 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16280 		free_irq(pf->pdev->irq, pf);
16281 
16282 	/* shutdown the adminq */
16283 	i40e_shutdown_adminq(hw);
16284 
16285 	/* destroy the locks only once, here */
16286 	mutex_destroy(&hw->aq.arq_mutex);
16287 	mutex_destroy(&hw->aq.asq_mutex);
16288 
16289 	/* Clear all dynamic memory lists of rings, q_vectors, and VSIs */
16290 	rtnl_lock();
16291 	i40e_clear_interrupt_scheme(pf);
16292 	for (i = 0; i < pf->num_alloc_vsi; i++) {
16293 		if (pf->vsi[i]) {
16294 			if (!test_bit(__I40E_RECOVERY_MODE, pf->state))
16295 				i40e_vsi_clear_rings(pf->vsi[i]);
16296 			i40e_vsi_clear(pf->vsi[i]);
16297 			pf->vsi[i] = NULL;
16298 		}
16299 	}
16300 	rtnl_unlock();
16301 
16302 	for (i = 0; i < I40E_MAX_VEB; i++) {
16303 		kfree(pf->veb[i]);
16304 		pf->veb[i] = NULL;
16305 	}
16306 
16307 	kfree(pf->qp_pile);
16308 	kfree(pf->vsi);
16309 
16310 	iounmap(hw->hw_addr);
16311 	kfree(pf);
16312 	pci_release_mem_regions(pdev);
16313 
16314 	pci_disable_pcie_error_reporting(pdev);
16315 	pci_disable_device(pdev);
16316 }
16317 
16318 /**
16319  * i40e_pci_error_detected - warning that something funky happened in PCI land
16320  * @pdev: PCI device information struct
16321  * @error: the type of PCI error
16322  *
16323  * Called to warn that something happened and the error handling steps
16324  * are in progress.  Allows the driver to quiesce things, be ready for
16325  * remediation.
16326  **/
16327 static pci_ers_result_t i40e_pci_error_detected(struct pci_dev *pdev,
16328 						pci_channel_state_t error)
16329 {
16330 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16331 
16332 	dev_info(&pdev->dev, "%s: error %d\n", __func__, error);
16333 
16334 	if (!pf) {
16335 		dev_info(&pdev->dev,
16336 			 "Cannot recover - error happened during device probe\n");
16337 		return PCI_ERS_RESULT_DISCONNECT;
16338 	}
16339 
16340 	/* shutdown all operations */
16341 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16342 		i40e_prep_for_reset(pf);
16343 
16344 	/* Request a slot reset */
16345 	return PCI_ERS_RESULT_NEED_RESET;
16346 }
16347 
16348 /**
16349  * i40e_pci_error_slot_reset - a PCI slot reset just happened
16350  * @pdev: PCI device information struct
16351  *
16352  * Called to find if the driver can work with the device now that
16353  * the pci slot has been reset.  If a basic connection seems good
16354  * (registers are readable and have sane content) then return a
16355  * happy little PCI_ERS_RESULT_xxx.
16356  **/
16357 static pci_ers_result_t i40e_pci_error_slot_reset(struct pci_dev *pdev)
16358 {
16359 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16360 	pci_ers_result_t result;
16361 	u32 reg;
16362 
16363 	dev_dbg(&pdev->dev, "%s\n", __func__);
16364 	if (pci_enable_device_mem(pdev)) {
16365 		dev_info(&pdev->dev,
16366 			 "Cannot re-enable PCI device after reset.\n");
16367 		result = PCI_ERS_RESULT_DISCONNECT;
16368 	} else {
16369 		pci_set_master(pdev);
16370 		pci_restore_state(pdev);
16371 		pci_save_state(pdev);
16372 		pci_wake_from_d3(pdev, false);
16373 
16374 		reg = rd32(&pf->hw, I40E_GLGEN_RTRIG);
16375 		if (reg == 0)
16376 			result = PCI_ERS_RESULT_RECOVERED;
16377 		else
16378 			result = PCI_ERS_RESULT_DISCONNECT;
16379 	}
16380 
16381 	return result;
16382 }
16383 
16384 /**
16385  * i40e_pci_error_reset_prepare - prepare device driver for pci reset
16386  * @pdev: PCI device information struct
16387  */
16388 static void i40e_pci_error_reset_prepare(struct pci_dev *pdev)
16389 {
16390 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16391 
16392 	i40e_prep_for_reset(pf);
16393 }
16394 
16395 /**
16396  * i40e_pci_error_reset_done - pci reset done, device driver reset can begin
16397  * @pdev: PCI device information struct
16398  */
16399 static void i40e_pci_error_reset_done(struct pci_dev *pdev)
16400 {
16401 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16402 
16403 	if (test_bit(__I40E_IN_REMOVE, pf->state))
16404 		return;
16405 
16406 	i40e_reset_and_rebuild(pf, false, false);
16407 }
16408 
16409 /**
16410  * i40e_pci_error_resume - restart operations after PCI error recovery
16411  * @pdev: PCI device information struct
16412  *
16413  * Called to allow the driver to bring things back up after PCI error
16414  * and/or reset recovery has finished.
16415  **/
16416 static void i40e_pci_error_resume(struct pci_dev *pdev)
16417 {
16418 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16419 
16420 	dev_dbg(&pdev->dev, "%s\n", __func__);
16421 	if (test_bit(__I40E_SUSPENDED, pf->state))
16422 		return;
16423 
16424 	i40e_handle_reset_warning(pf, false);
16425 }
16426 
16427 /**
16428  * i40e_enable_mc_magic_wake - enable multicast magic packet wake up
16429  * using the mac_address_write admin q function
16430  * @pf: pointer to i40e_pf struct
16431  **/
16432 static void i40e_enable_mc_magic_wake(struct i40e_pf *pf)
16433 {
16434 	struct i40e_hw *hw = &pf->hw;
16435 	i40e_status ret;
16436 	u8 mac_addr[6];
16437 	u16 flags = 0;
16438 
16439 	/* Get current MAC address in case it's an LAA */
16440 	if (pf->vsi[pf->lan_vsi] && pf->vsi[pf->lan_vsi]->netdev) {
16441 		ether_addr_copy(mac_addr,
16442 				pf->vsi[pf->lan_vsi]->netdev->dev_addr);
16443 	} else {
16444 		dev_err(&pf->pdev->dev,
16445 			"Failed to retrieve MAC address; using default\n");
16446 		ether_addr_copy(mac_addr, hw->mac.addr);
16447 	}
16448 
16449 	/* The FW expects the mac address write cmd to first be called with
16450 	 * one of these flags before calling it again with the multicast
16451 	 * enable flags.
16452 	 */
16453 	flags = I40E_AQC_WRITE_TYPE_LAA_WOL;
16454 
16455 	if (hw->func_caps.flex10_enable && hw->partition_id != 1)
16456 		flags = I40E_AQC_WRITE_TYPE_LAA_ONLY;
16457 
16458 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16459 	if (ret) {
16460 		dev_err(&pf->pdev->dev,
16461 			"Failed to update MAC address registers; cannot enable Multicast Magic packet wake up");
16462 		return;
16463 	}
16464 
16465 	flags = I40E_AQC_MC_MAG_EN
16466 			| I40E_AQC_WOL_PRESERVE_ON_PFR
16467 			| I40E_AQC_WRITE_TYPE_UPDATE_MC_MAG;
16468 	ret = i40e_aq_mac_address_write(hw, flags, mac_addr, NULL);
16469 	if (ret)
16470 		dev_err(&pf->pdev->dev,
16471 			"Failed to enable Multicast Magic Packet wake up\n");
16472 }
16473 
16474 /**
16475  * i40e_shutdown - PCI callback for shutting down
16476  * @pdev: PCI device information struct
16477  **/
16478 static void i40e_shutdown(struct pci_dev *pdev)
16479 {
16480 	struct i40e_pf *pf = pci_get_drvdata(pdev);
16481 	struct i40e_hw *hw = &pf->hw;
16482 
16483 	set_bit(__I40E_SUSPENDED, pf->state);
16484 	set_bit(__I40E_DOWN, pf->state);
16485 
16486 	del_timer_sync(&pf->service_timer);
16487 	cancel_work_sync(&pf->service_task);
16488 	i40e_cloud_filter_exit(pf);
16489 	i40e_fdir_teardown(pf);
16490 
16491 	/* Client close must be called explicitly here because the timer
16492 	 * has been stopped.
16493 	 */
16494 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16495 
16496 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16497 		i40e_enable_mc_magic_wake(pf);
16498 
16499 	i40e_prep_for_reset(pf);
16500 
16501 	wr32(hw, I40E_PFPM_APM,
16502 	     (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16503 	wr32(hw, I40E_PFPM_WUFC,
16504 	     (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16505 
16506 	/* Free MSI/legacy interrupt 0 when in recovery mode. */
16507 	if (test_bit(__I40E_RECOVERY_MODE, pf->state) &&
16508 	    !(pf->flags & I40E_FLAG_MSIX_ENABLED))
16509 		free_irq(pf->pdev->irq, pf);
16510 
16511 	/* Since we're going to destroy queues during the
16512 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16513 	 * whole section
16514 	 */
16515 	rtnl_lock();
16516 	i40e_clear_interrupt_scheme(pf);
16517 	rtnl_unlock();
16518 
16519 	if (system_state == SYSTEM_POWER_OFF) {
16520 		pci_wake_from_d3(pdev, pf->wol_en);
16521 		pci_set_power_state(pdev, PCI_D3hot);
16522 	}
16523 }
16524 
16525 /**
16526  * i40e_suspend - PM callback for moving to D3
16527  * @dev: generic device information structure
16528  **/
16529 static int __maybe_unused i40e_suspend(struct device *dev)
16530 {
16531 	struct i40e_pf *pf = dev_get_drvdata(dev);
16532 	struct i40e_hw *hw = &pf->hw;
16533 
16534 	/* If we're already suspended, then there is nothing to do */
16535 	if (test_and_set_bit(__I40E_SUSPENDED, pf->state))
16536 		return 0;
16537 
16538 	set_bit(__I40E_DOWN, pf->state);
16539 
16540 	/* Ensure service task will not be running */
16541 	del_timer_sync(&pf->service_timer);
16542 	cancel_work_sync(&pf->service_task);
16543 
16544 	/* Client close must be called explicitly here because the timer
16545 	 * has been stopped.
16546 	 */
16547 	i40e_notify_client_of_netdev_close(pf->vsi[pf->lan_vsi], false);
16548 
16549 	if (pf->wol_en && (pf->hw_features & I40E_HW_WOL_MC_MAGIC_PKT_WAKE))
16550 		i40e_enable_mc_magic_wake(pf);
16551 
16552 	/* Since we're going to destroy queues during the
16553 	 * i40e_clear_interrupt_scheme() we should hold the RTNL lock for this
16554 	 * whole section
16555 	 */
16556 	rtnl_lock();
16557 
16558 	i40e_prep_for_reset(pf);
16559 
16560 	wr32(hw, I40E_PFPM_APM, (pf->wol_en ? I40E_PFPM_APM_APME_MASK : 0));
16561 	wr32(hw, I40E_PFPM_WUFC, (pf->wol_en ? I40E_PFPM_WUFC_MAG_MASK : 0));
16562 
16563 	/* Clear the interrupt scheme and release our IRQs so that the system
16564 	 * can safely hibernate even when there are a large number of CPUs.
16565 	 * Otherwise hibernation might fail when mapping all the vectors back
16566 	 * to CPU0.
16567 	 */
16568 	i40e_clear_interrupt_scheme(pf);
16569 
16570 	rtnl_unlock();
16571 
16572 	return 0;
16573 }
16574 
16575 /**
16576  * i40e_resume - PM callback for waking up from D3
16577  * @dev: generic device information structure
16578  **/
16579 static int __maybe_unused i40e_resume(struct device *dev)
16580 {
16581 	struct i40e_pf *pf = dev_get_drvdata(dev);
16582 	int err;
16583 
16584 	/* If we're not suspended, then there is nothing to do */
16585 	if (!test_bit(__I40E_SUSPENDED, pf->state))
16586 		return 0;
16587 
16588 	/* We need to hold the RTNL lock prior to restoring interrupt schemes,
16589 	 * since we're going to be restoring queues
16590 	 */
16591 	rtnl_lock();
16592 
16593 	/* We cleared the interrupt scheme when we suspended, so we need to
16594 	 * restore it now to resume device functionality.
16595 	 */
16596 	err = i40e_restore_interrupt_scheme(pf);
16597 	if (err) {
16598 		dev_err(dev, "Cannot restore interrupt scheme: %d\n",
16599 			err);
16600 	}
16601 
16602 	clear_bit(__I40E_DOWN, pf->state);
16603 	i40e_reset_and_rebuild(pf, false, true);
16604 
16605 	rtnl_unlock();
16606 
16607 	/* Clear suspended state last after everything is recovered */
16608 	clear_bit(__I40E_SUSPENDED, pf->state);
16609 
16610 	/* Restart the service task */
16611 	mod_timer(&pf->service_timer,
16612 		  round_jiffies(jiffies + pf->service_timer_period));
16613 
16614 	return 0;
16615 }
16616 
16617 static const struct pci_error_handlers i40e_err_handler = {
16618 	.error_detected = i40e_pci_error_detected,
16619 	.slot_reset = i40e_pci_error_slot_reset,
16620 	.reset_prepare = i40e_pci_error_reset_prepare,
16621 	.reset_done = i40e_pci_error_reset_done,
16622 	.resume = i40e_pci_error_resume,
16623 };
16624 
16625 static SIMPLE_DEV_PM_OPS(i40e_pm_ops, i40e_suspend, i40e_resume);
16626 
16627 static struct pci_driver i40e_driver = {
16628 	.name     = i40e_driver_name,
16629 	.id_table = i40e_pci_tbl,
16630 	.probe    = i40e_probe,
16631 	.remove   = i40e_remove,
16632 	.driver   = {
16633 		.pm = &i40e_pm_ops,
16634 	},
16635 	.shutdown = i40e_shutdown,
16636 	.err_handler = &i40e_err_handler,
16637 	.sriov_configure = i40e_pci_sriov_configure,
16638 };
16639 
16640 /**
16641  * i40e_init_module - Driver registration routine
16642  *
16643  * i40e_init_module is the first routine called when the driver is
16644  * loaded. All it does is register with the PCI subsystem.
16645  **/
16646 static int __init i40e_init_module(void)
16647 {
16648 	pr_info("%s: %s\n", i40e_driver_name, i40e_driver_string);
16649 	pr_info("%s: %s\n", i40e_driver_name, i40e_copyright);
16650 
16651 	/* There is no need to throttle the number of active tasks because
16652 	 * each device limits its own task using a state bit for scheduling
16653 	 * the service task, and the device tasks do not interfere with each
16654 	 * other, so we don't set a max task limit. We must set WQ_MEM_RECLAIM
16655 	 * since we need to be able to guarantee forward progress even under
16656 	 * memory pressure.
16657 	 */
16658 	i40e_wq = alloc_workqueue("%s", WQ_MEM_RECLAIM, 0, i40e_driver_name);
16659 	if (!i40e_wq) {
16660 		pr_err("%s: Failed to create workqueue\n", i40e_driver_name);
16661 		return -ENOMEM;
16662 	}
16663 
16664 	i40e_dbg_init();
16665 	return pci_register_driver(&i40e_driver);
16666 }
16667 module_init(i40e_init_module);
16668 
16669 /**
16670  * i40e_exit_module - Driver exit cleanup routine
16671  *
16672  * i40e_exit_module is called just before the driver is removed
16673  * from memory.
16674  **/
16675 static void __exit i40e_exit_module(void)
16676 {
16677 	pci_unregister_driver(&i40e_driver);
16678 	destroy_workqueue(i40e_wq);
16679 	ida_destroy(&i40e_client_ida);
16680 	i40e_dbg_exit();
16681 }
16682 module_exit(i40e_exit_module);
16683